2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001 Momchil Velikov
|
|
|
|
* Portions Copyright (C) 2001 Christoph Hellwig
|
2006-12-07 04:33:44 +00:00
|
|
|
* Copyright (C) 2006 Nick Piggin
|
2012-03-28 21:42:53 +00:00
|
|
|
* Copyright (C) 2012 Konstantin Khlebnikov
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
#ifndef _LINUX_RADIX_TREE_H
|
|
|
|
#define _LINUX_RADIX_TREE_H
|
|
|
|
|
2016-03-17 21:21:42 +00:00
|
|
|
#include <linux/bitops.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/preempt.h>
|
|
|
|
#include <linux/types.h>
|
2011-11-24 01:12:59 +00:00
|
|
|
#include <linux/bug.h>
|
2006-12-07 04:33:44 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/rcupdate.h>
|
|
|
|
|
|
|
|
/*
|
2016-05-21 00:03:54 +00:00
|
|
|
* The bottom two bits of the slot determine how the remaining bits in the
|
|
|
|
* slot are interpreted:
|
2006-12-07 04:33:44 +00:00
|
|
|
*
|
2016-05-21 00:03:54 +00:00
|
|
|
* 00 - data pointer
|
|
|
|
* 01 - internal entry
|
|
|
|
* 10 - exceptional entry
|
2016-08-02 21:04:19 +00:00
|
|
|
* 11 - this bit combination is currently unused/reserved
|
2016-05-21 00:03:54 +00:00
|
|
|
*
|
|
|
|
* The internal entry may be a pointer to the next level in the tree, a
|
|
|
|
* sibling entry, or an indicator that the entry in this slot has been moved
|
|
|
|
* to another location in the tree and the lookup should be restarted. While
|
|
|
|
* NULL fits the 'data pointer' pattern, it means that there is no entry in
|
|
|
|
* the tree for this index (no matter what level of the tree it is found at).
|
|
|
|
* This means that you cannot store NULL in the tree as a value for the index.
|
2006-12-07 04:33:44 +00:00
|
|
|
*/
|
2016-05-21 00:03:54 +00:00
|
|
|
#define RADIX_TREE_ENTRY_MASK 3UL
|
|
|
|
#define RADIX_TREE_INTERNAL_NODE 1UL
|
2016-05-21 00:03:22 +00:00
|
|
|
|
radix_tree: exceptional entries and indices
A patchset to extend tmpfs to MAX_LFS_FILESIZE by abandoning its
peculiar swap vector, instead keeping a file's swap entries in the same
radix tree as its struct page pointers: thus saving memory, and
simplifying its code and locking.
This patch:
The radix_tree is used by several subsystems for different purposes. A
major use is to store the struct page pointers of a file's pagecache for
memory management. But what if mm wanted to store something other than
page pointers there too?
The low bit of a radix_tree entry is already used to denote an indirect
pointer, for internal use, and the unlikely radix_tree_deref_retry()
case.
Define the next bit as denoting an exceptional entry, and supply inline
functions radix_tree_exception() to return non-0 in either unlikely
case, and radix_tree_exceptional_entry() to return non-0 in the second
case.
If a subsystem already uses radix_tree with that bit set, no problem: it
does not affect internal workings at all, but is defined for the
convenience of those storing well-aligned pointers in the radix_tree.
The radix_tree_gang_lookups have an implicit assumption that the caller
can deduce the offset of each entry returned e.g. by the page->index of
a struct page. But that may not be feasible for some kinds of item to
be stored there.
radix_tree_gang_lookup_slot() allow for an optional indices argument,
output array in which to return those offsets. The same could be added
to other radix_tree_gang_lookups, but for now keep it to the only one
for which we need it.
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-08-03 23:21:18 +00:00
|
|
|
/*
|
2016-05-21 00:03:54 +00:00
|
|
|
* Most users of the radix tree store pointers but shmem/tmpfs stores swap
|
|
|
|
* entries in the same tree. They are marked as exceptional entries to
|
|
|
|
* distinguish them from pointers to struct page.
|
radix_tree: exceptional entries and indices
A patchset to extend tmpfs to MAX_LFS_FILESIZE by abandoning its
peculiar swap vector, instead keeping a file's swap entries in the same
radix tree as its struct page pointers: thus saving memory, and
simplifying its code and locking.
This patch:
The radix_tree is used by several subsystems for different purposes. A
major use is to store the struct page pointers of a file's pagecache for
memory management. But what if mm wanted to store something other than
page pointers there too?
The low bit of a radix_tree entry is already used to denote an indirect
pointer, for internal use, and the unlikely radix_tree_deref_retry()
case.
Define the next bit as denoting an exceptional entry, and supply inline
functions radix_tree_exception() to return non-0 in either unlikely
case, and radix_tree_exceptional_entry() to return non-0 in the second
case.
If a subsystem already uses radix_tree with that bit set, no problem: it
does not affect internal workings at all, but is defined for the
convenience of those storing well-aligned pointers in the radix_tree.
The radix_tree_gang_lookups have an implicit assumption that the caller
can deduce the offset of each entry returned e.g. by the page->index of
a struct page. But that may not be feasible for some kinds of item to
be stored there.
radix_tree_gang_lookup_slot() allow for an optional indices argument,
output array in which to return those offsets. The same could be added
to other radix_tree_gang_lookups, but for now keep it to the only one
for which we need it.
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-08-03 23:21:18 +00:00
|
|
|
* EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it.
|
|
|
|
*/
|
|
|
|
#define RADIX_TREE_EXCEPTIONAL_ENTRY 2
|
|
|
|
#define RADIX_TREE_EXCEPTIONAL_SHIFT 2
|
2006-12-07 04:33:44 +00:00
|
|
|
|
2016-05-21 00:03:54 +00:00
|
|
|
static inline bool radix_tree_is_internal_node(void *ptr)
|
2006-12-07 04:33:44 +00:00
|
|
|
{
|
2016-05-21 00:03:54 +00:00
|
|
|
return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
|
|
|
|
RADIX_TREE_INTERNAL_NODE;
|
2006-12-07 04:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** radix-tree API starts here ***/
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2010-08-10 00:19:12 +00:00
|
|
|
#define RADIX_TREE_MAX_TAGS 3
|
2006-06-23 09:03:22 +00:00
|
|
|
|
2016-05-21 00:01:42 +00:00
|
|
|
#ifndef RADIX_TREE_MAP_SHIFT
|
2014-04-03 21:47:54 +00:00
|
|
|
#define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
|
|
|
|
#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
|
|
|
|
|
|
|
|
#define RADIX_TREE_TAG_LONGS \
|
|
|
|
((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
|
|
|
|
|
mm: keep page cache radix tree nodes in check
Previously, page cache radix tree nodes were freed after reclaim emptied
out their page pointers. But now reclaim stores shadow entries in their
place, which are only reclaimed when the inodes themselves are
reclaimed. This is problematic for bigger files that are still in use
after they have a significant amount of their cache reclaimed, without
any of those pages actually refaulting. The shadow entries will just
sit there and waste memory. In the worst case, the shadow entries will
accumulate until the machine runs out of memory.
To get this under control, the VM will track radix tree nodes
exclusively containing shadow entries on a per-NUMA node list. Per-NUMA
rather than global because we expect the radix tree nodes themselves to
be allocated node-locally and we want to reduce cross-node references of
otherwise independent cache workloads. A simple shrinker will then
reclaim these nodes on memory pressure.
A few things need to be stored in the radix tree node to implement the
shadow node LRU and allow tree deletions coming from the list:
1. There is no index available that would describe the reverse path
from the node up to the tree root, which is needed to perform a
deletion. To solve this, encode in each node its offset inside the
parent. This can be stored in the unused upper bits of the same
member that stores the node's height at no extra space cost.
2. The number of shadow entries needs to be counted in addition to the
regular entries, to quickly detect when the node is ready to go to
the shadow node LRU list. The current entry count is an unsigned
int but the maximum number of entries is 64, so a shadow counter
can easily be stored in the unused upper bits.
3. Tree modification needs tree lock and tree root, which are located
in the address space, so store an address_space backpointer in the
node. The parent pointer of the node is in a union with the 2-word
rcu_head, so the backpointer comes at no extra cost as well.
4. The node needs to be linked to an LRU list, which requires a list
head inside the node. This does increase the size of the node, but
it does not change the number of objects that fit into a slab page.
[akpm@linux-foundation.org: export the right function]
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Rik van Riel <riel@redhat.com>
Reviewed-by: Minchan Kim <minchan@kernel.org>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Bob Liu <bob.liu@oracle.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Dave Chinner <david@fromorbit.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jan Kara <jack@suse.cz>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Luigi Semenzato <semenzato@google.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Metin Doslu <metin@citusdata.com>
Cc: Michel Lespinasse <walken@google.com>
Cc: Ozgun Erdogan <ozgun@citusdata.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Roman Gushchin <klamm@yandex-team.ru>
Cc: Ryan Mallon <rmallon@gmail.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2014-04-03 21:47:56 +00:00
|
|
|
#define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
|
|
|
|
#define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
|
|
|
|
RADIX_TREE_MAP_SHIFT))
|
|
|
|
|
2016-12-14 23:09:01 +00:00
|
|
|
/*
|
|
|
|
* @count is the count of every non-NULL element in the ->slots array
|
|
|
|
* whether that is an exceptional entry, a retry entry, a user pointer,
|
|
|
|
* a sibling entry or a pointer to the next level of the tree.
|
|
|
|
* @exceptional is the count of every element in ->slots which is
|
|
|
|
* either radix_tree_exceptional_entry() or is a sibling entry for an
|
|
|
|
* exceptional entry.
|
|
|
|
*/
|
2014-04-03 21:47:54 +00:00
|
|
|
struct radix_tree_node {
|
2016-12-13 00:43:41 +00:00
|
|
|
unsigned char shift; /* Bits remaining in each slot */
|
|
|
|
unsigned char offset; /* Slot offset in parent */
|
2016-12-13 00:43:52 +00:00
|
|
|
unsigned char count; /* Total entry count */
|
2016-12-13 00:43:41 +00:00
|
|
|
unsigned char exceptional; /* Exceptional entry count */
|
2016-12-14 23:08:34 +00:00
|
|
|
struct radix_tree_node *parent; /* Used when ascending tree */
|
|
|
|
void *private_data; /* For tree user */
|
2014-04-03 21:47:54 +00:00
|
|
|
union {
|
2016-12-14 23:08:34 +00:00
|
|
|
struct list_head private_list; /* For tree user */
|
|
|
|
struct rcu_head rcu_head; /* Used when freeing node */
|
2014-04-03 21:47:54 +00:00
|
|
|
};
|
|
|
|
void __rcu *slots[RADIX_TREE_MAP_SIZE];
|
|
|
|
unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
|
|
|
|
};
|
|
|
|
|
2006-06-23 09:03:22 +00:00
|
|
|
/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
|
2005-04-16 22:20:36 +00:00
|
|
|
struct radix_tree_root {
|
2005-10-21 07:18:50 +00:00
|
|
|
gfp_t gfp_mask;
|
2010-02-25 22:43:52 +00:00
|
|
|
struct radix_tree_node __rcu *rnode;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define RADIX_TREE_INIT(mask) { \
|
|
|
|
.gfp_mask = (mask), \
|
|
|
|
.rnode = NULL, \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RADIX_TREE(name, mask) \
|
|
|
|
struct radix_tree_root name = RADIX_TREE_INIT(mask)
|
|
|
|
|
|
|
|
#define INIT_RADIX_TREE(root, mask) \
|
|
|
|
do { \
|
|
|
|
(root)->gfp_mask = (mask); \
|
|
|
|
(root)->rnode = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
radix-tree: introduce radix_tree_empty
Commit e61452365372 ("radix_tree: add support for multi-order entries")
left the impression that the support for multiorder radix tree entries
was functional. As soon as Ross tried to use it, it became apparent
that my testing was completely inadequate, and it didn't even work a
little bit for orders that were not a multiple of shift.
This series of patches is the result of about 6 weeks of redesign,
reimplementation, testing, arguing and hair-pulling. The great news is
that the test-suite is now far better than it was. That's reflected in
the diffstat for the test-suite alone:
12 files changed, 436 insertions(+), 28 deletions(-)
The highlight for users of the tree is that the restriction on the order
of inserted entries being >= RADIX_TREE_MAP_SHIFT is now gone; the radix
tree now supports any order between 0 and 64.
For those who are interested in how the tree works, patch 9 is probably
the most interesting one as it introduces the new machinery for handling
sibling entries.
I've tried to be fair in attributing authorship to the person who
contributed the majority of the code in each patch; Ross has been an
invaluable partner in the development of this support and it's fair to
say that each of us has code in every commit.
I should also express my appreciation of the 0day testing. It prompted
me that I was bloating the tinyconfig in an unacceptable way, and it
bisected to a commit which contained a rather nasty memory-corruption
bug.
This patch (of 29):
The irqdomain code was checking for 0 or 1 entries, not 0 entries like
the comment said they were. Introduce a new helper that will actually
check for an empty tree.
Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Konstantin Khlebnikov <koct9i@gmail.com>
Cc: Kirill Shutemov <kirill.shutemov@linux.intel.com>
Cc: Neil Brown <neilb@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-05-21 00:01:33 +00:00
|
|
|
static inline bool radix_tree_empty(struct radix_tree_root *root)
|
|
|
|
{
|
|
|
|
return root->rnode == NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:08:55 +00:00
|
|
|
/**
|
|
|
|
* struct radix_tree_iter - radix tree iterator state
|
|
|
|
*
|
|
|
|
* @index: index of current slot
|
|
|
|
* @next_index: one beyond the last index for this chunk
|
|
|
|
* @tags: bit-mask for tag-iterating
|
|
|
|
* @node: node that contains current slot
|
|
|
|
* @shift: shift for the node that holds our slots
|
|
|
|
*
|
|
|
|
* This radix tree iterator works in terms of "chunks" of slots. A chunk is a
|
|
|
|
* subinterval of slots contained within one radix tree leaf node. It is
|
|
|
|
* described by a pointer to its first slot and a struct radix_tree_iter
|
|
|
|
* which holds the chunk's position in the tree and its size. For tagged
|
|
|
|
* iteration radix_tree_iter also holds the slots' bit-mask for one chosen
|
|
|
|
* radix tree tag.
|
|
|
|
*/
|
|
|
|
struct radix_tree_iter {
|
|
|
|
unsigned long index;
|
|
|
|
unsigned long next_index;
|
|
|
|
unsigned long tags;
|
|
|
|
struct radix_tree_node *node;
|
|
|
|
#ifdef CONFIG_RADIX_TREE_MULTIORDER
|
|
|
|
unsigned int shift;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline unsigned int iter_shift(const struct radix_tree_iter *iter)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_RADIX_TREE_MULTIORDER
|
|
|
|
return iter->shift;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-12-07 04:33:44 +00:00
|
|
|
/**
|
|
|
|
* Radix-tree synchronization
|
|
|
|
*
|
|
|
|
* The radix-tree API requires that users provide all synchronisation (with
|
|
|
|
* specific exceptions, noted below).
|
|
|
|
*
|
|
|
|
* Synchronization of access to the data items being stored in the tree, and
|
|
|
|
* management of their lifetimes must be completely managed by API users.
|
|
|
|
*
|
|
|
|
* For API usage, in general,
|
2007-05-09 06:57:56 +00:00
|
|
|
* - any function _modifying_ the tree or tags (inserting or deleting
|
2008-02-03 14:12:47 +00:00
|
|
|
* items, setting or clearing tags) must exclude other modifications, and
|
2006-12-07 04:33:44 +00:00
|
|
|
* exclude any functions reading the tree.
|
2007-05-09 06:57:56 +00:00
|
|
|
* - any function _reading_ the tree or tags (looking up items or tags,
|
2006-12-07 04:33:44 +00:00
|
|
|
* gang lookups) must exclude modifications to the tree, but may occur
|
|
|
|
* concurrently with other readers.
|
|
|
|
*
|
|
|
|
* The notable exceptions to this rule are the following functions:
|
2014-04-03 21:47:54 +00:00
|
|
|
* __radix_tree_lookup
|
2006-12-07 04:33:44 +00:00
|
|
|
* radix_tree_lookup
|
2008-07-26 02:45:29 +00:00
|
|
|
* radix_tree_lookup_slot
|
2006-12-07 04:33:44 +00:00
|
|
|
* radix_tree_tag_get
|
|
|
|
* radix_tree_gang_lookup
|
2008-07-26 02:45:29 +00:00
|
|
|
* radix_tree_gang_lookup_slot
|
2006-12-07 04:33:44 +00:00
|
|
|
* radix_tree_gang_lookup_tag
|
2008-07-26 02:45:29 +00:00
|
|
|
* radix_tree_gang_lookup_tag_slot
|
2006-12-07 04:33:44 +00:00
|
|
|
* radix_tree_tagged
|
|
|
|
*
|
2016-01-20 22:59:09 +00:00
|
|
|
* The first 8 functions are able to be called locklessly, using RCU. The
|
2006-12-07 04:33:44 +00:00
|
|
|
* caller must ensure calls to these functions are made within rcu_read_lock()
|
|
|
|
* regions. Other readers (lock-free or otherwise) and modifications may be
|
|
|
|
* running concurrently.
|
|
|
|
*
|
|
|
|
* It is still required that the caller manage the synchronization and lifetimes
|
|
|
|
* of the items. So if RCU lock-free lookups are used, typically this would mean
|
|
|
|
* that the items have their own locks, or are amenable to lock-free access; and
|
|
|
|
* that the items are freed by RCU (or only freed after having been deleted from
|
|
|
|
* the radix tree *and* a synchronize_rcu() grace period).
|
|
|
|
*
|
|
|
|
* (Note, rcu_assign_pointer and rcu_dereference are not needed to control
|
|
|
|
* access to data items when inserting into or looking up from the radix tree)
|
|
|
|
*
|
radix_tree_tag_get() is not as safe as the docs make out [ver #2]
radix_tree_tag_get() is not safe to use concurrently with radix_tree_tag_set()
or radix_tree_tag_clear(). The problem is that the double tag_get() in
radix_tree_tag_get():
if (!tag_get(node, tag, offset))
saw_unset_tag = 1;
if (height == 1) {
int ret = tag_get(node, tag, offset);
may see the value change due to the action of set/clear. RCU is no protection
against this as no pointers are being changed, no nodes are being replaced
according to a COW protocol - set/clear alter the node directly.
The documentation in linux/radix-tree.h, however, says that
radix_tree_tag_get() is an exception to the rule that "any function modifying
the tree or tags (...) must exclude other modifications, and exclude any
functions reading the tree".
The problem is that the next statement in radix_tree_tag_get() checks that the
tag doesn't vary over time:
BUG_ON(ret && saw_unset_tag);
This has been seen happening in FS-Cache:
https://www.redhat.com/archives/linux-cachefs/2010-April/msg00013.html
To this end, remove the BUG_ON() from radix_tree_tag_get() and note in various
comments that the value of the tag may change whilst the RCU read lock is held,
and thus that the return value of radix_tree_tag_get() may not be relied upon
unless radix_tree_tag_set/clear() and radix_tree_delete() are excluded from
running concurrently with it.
Reported-by: Romain DEGEZ <romain.degez@smartjog.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Nick Piggin <npiggin@suse.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2010-04-06 21:36:20 +00:00
|
|
|
* Note that the value returned by radix_tree_tag_get() may not be relied upon
|
|
|
|
* if only the RCU read lock is held. Functions to set/clear tags and to
|
|
|
|
* delete nodes running concurrently with it may affect its result such that
|
|
|
|
* two consecutive reads in the same locked section may return different
|
|
|
|
* values. If reliability is required, modification functions must also be
|
|
|
|
* excluded from concurrency.
|
|
|
|
*
|
2006-12-07 04:33:44 +00:00
|
|
|
* radix_tree_tagged is able to be called without locking or RCU.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_deref_slot - dereference a slot
|
|
|
|
* @pslot: pointer to slot, returned by radix_tree_lookup_slot
|
|
|
|
* Returns: item that was stored in that slot with any direct pointer flag
|
|
|
|
* removed.
|
|
|
|
*
|
|
|
|
* For use with radix_tree_lookup_slot(). Caller must hold tree at least read
|
2010-11-11 22:05:19 +00:00
|
|
|
* locked across slot lookup and dereference. Not required if write lock is
|
|
|
|
* held (ie. items cannot be concurrently inserted).
|
|
|
|
*
|
|
|
|
* radix_tree_deref_retry must be used to confirm validity of the pointer if
|
|
|
|
* only the read lock is held.
|
2006-12-07 04:33:44 +00:00
|
|
|
*/
|
|
|
|
static inline void *radix_tree_deref_slot(void **pslot)
|
|
|
|
{
|
2010-11-11 22:05:19 +00:00
|
|
|
return rcu_dereference(*pslot);
|
2006-12-07 04:33:44 +00:00
|
|
|
}
|
2010-11-11 22:05:19 +00:00
|
|
|
|
2011-01-13 23:47:21 +00:00
|
|
|
/**
|
|
|
|
* radix_tree_deref_slot_protected - dereference a slot without RCU lock but with tree lock held
|
|
|
|
* @pslot: pointer to slot, returned by radix_tree_lookup_slot
|
|
|
|
* Returns: item that was stored in that slot with any direct pointer flag
|
|
|
|
* removed.
|
|
|
|
*
|
|
|
|
* Similar to radix_tree_deref_slot but only used during migration when a pages
|
|
|
|
* mapping is being moved. The caller does not hold the RCU read lock but it
|
|
|
|
* must hold the tree lock to prevent parallel updates.
|
|
|
|
*/
|
|
|
|
static inline void *radix_tree_deref_slot_protected(void **pslot,
|
|
|
|
spinlock_t *treelock)
|
|
|
|
{
|
|
|
|
return rcu_dereference_protected(*pslot, lockdep_is_held(treelock));
|
|
|
|
}
|
|
|
|
|
2010-11-11 22:05:19 +00:00
|
|
|
/**
|
|
|
|
* radix_tree_deref_retry - check radix_tree_deref_slot
|
|
|
|
* @arg: pointer returned by radix_tree_deref_slot
|
|
|
|
* Returns: 0 if retry is not required, otherwise retry is required
|
|
|
|
*
|
|
|
|
* radix_tree_deref_retry must be used with radix_tree_deref_slot.
|
|
|
|
*/
|
|
|
|
static inline int radix_tree_deref_retry(void *arg)
|
|
|
|
{
|
2016-05-21 00:03:30 +00:00
|
|
|
return unlikely(radix_tree_is_internal_node(arg));
|
2010-11-11 22:05:19 +00:00
|
|
|
}
|
|
|
|
|
radix_tree: exceptional entries and indices
A patchset to extend tmpfs to MAX_LFS_FILESIZE by abandoning its
peculiar swap vector, instead keeping a file's swap entries in the same
radix tree as its struct page pointers: thus saving memory, and
simplifying its code and locking.
This patch:
The radix_tree is used by several subsystems for different purposes. A
major use is to store the struct page pointers of a file's pagecache for
memory management. But what if mm wanted to store something other than
page pointers there too?
The low bit of a radix_tree entry is already used to denote an indirect
pointer, for internal use, and the unlikely radix_tree_deref_retry()
case.
Define the next bit as denoting an exceptional entry, and supply inline
functions radix_tree_exception() to return non-0 in either unlikely
case, and radix_tree_exceptional_entry() to return non-0 in the second
case.
If a subsystem already uses radix_tree with that bit set, no problem: it
does not affect internal workings at all, but is defined for the
convenience of those storing well-aligned pointers in the radix_tree.
The radix_tree_gang_lookups have an implicit assumption that the caller
can deduce the offset of each entry returned e.g. by the page->index of
a struct page. But that may not be feasible for some kinds of item to
be stored there.
radix_tree_gang_lookup_slot() allow for an optional indices argument,
output array in which to return those offsets. The same could be added
to other radix_tree_gang_lookups, but for now keep it to the only one
for which we need it.
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-08-03 23:21:18 +00:00
|
|
|
/**
|
|
|
|
* radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry?
|
|
|
|
* @arg: value returned by radix_tree_deref_slot
|
|
|
|
* Returns: 0 if well-aligned pointer, non-0 if exceptional entry.
|
|
|
|
*/
|
|
|
|
static inline int radix_tree_exceptional_entry(void *arg)
|
|
|
|
{
|
|
|
|
/* Not unlikely because radix_tree_exception often tested first */
|
|
|
|
return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_exception - radix_tree_deref_slot returned either exception?
|
|
|
|
* @arg: value returned by radix_tree_deref_slot
|
|
|
|
* Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
|
|
|
|
*/
|
|
|
|
static inline int radix_tree_exception(void *arg)
|
|
|
|
{
|
2016-05-21 00:03:54 +00:00
|
|
|
return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
|
radix_tree: exceptional entries and indices
A patchset to extend tmpfs to MAX_LFS_FILESIZE by abandoning its
peculiar swap vector, instead keeping a file's swap entries in the same
radix tree as its struct page pointers: thus saving memory, and
simplifying its code and locking.
This patch:
The radix_tree is used by several subsystems for different purposes. A
major use is to store the struct page pointers of a file's pagecache for
memory management. But what if mm wanted to store something other than
page pointers there too?
The low bit of a radix_tree entry is already used to denote an indirect
pointer, for internal use, and the unlikely radix_tree_deref_retry()
case.
Define the next bit as denoting an exceptional entry, and supply inline
functions radix_tree_exception() to return non-0 in either unlikely
case, and radix_tree_exceptional_entry() to return non-0 in the second
case.
If a subsystem already uses radix_tree with that bit set, no problem: it
does not affect internal workings at all, but is defined for the
convenience of those storing well-aligned pointers in the radix_tree.
The radix_tree_gang_lookups have an implicit assumption that the caller
can deduce the offset of each entry returned e.g. by the page->index of
a struct page. But that may not be feasible for some kinds of item to
be stored there.
radix_tree_gang_lookup_slot() allow for an optional indices argument,
output array in which to return those offsets. The same could be added
to other radix_tree_gang_lookups, but for now keep it to the only one
for which we need it.
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-08-03 23:21:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 21:47:54 +00:00
|
|
|
int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
|
2016-03-17 21:21:54 +00:00
|
|
|
unsigned order, struct radix_tree_node **nodep,
|
|
|
|
void ***slotp);
|
|
|
|
int __radix_tree_insert(struct radix_tree_root *, unsigned long index,
|
|
|
|
unsigned order, void *);
|
|
|
|
static inline int radix_tree_insert(struct radix_tree_root *root,
|
|
|
|
unsigned long index, void *entry)
|
|
|
|
{
|
|
|
|
return __radix_tree_insert(root, index, 0, entry);
|
|
|
|
}
|
2014-04-03 21:47:54 +00:00
|
|
|
void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
|
|
|
|
struct radix_tree_node **nodep, void ***slotp);
|
2005-04-16 22:20:36 +00:00
|
|
|
void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
|
2005-11-07 08:59:29 +00:00
|
|
|
void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
|
2016-12-13 00:43:49 +00:00
|
|
|
typedef void (*radix_tree_update_node_t)(struct radix_tree_node *, void *);
|
2016-12-13 00:43:41 +00:00
|
|
|
void __radix_tree_replace(struct radix_tree_root *root,
|
|
|
|
struct radix_tree_node *node,
|
2016-12-13 00:43:49 +00:00
|
|
|
void **slot, void *item,
|
|
|
|
radix_tree_update_node_t update_node, void *private);
|
2016-12-14 23:09:01 +00:00
|
|
|
void radix_tree_iter_replace(struct radix_tree_root *,
|
|
|
|
const struct radix_tree_iter *, void **slot, void *item);
|
2016-12-13 00:43:43 +00:00
|
|
|
void radix_tree_replace_slot(struct radix_tree_root *root,
|
|
|
|
void **slot, void *item);
|
2016-12-13 00:43:52 +00:00
|
|
|
void __radix_tree_delete_node(struct radix_tree_root *root,
|
2014-04-03 21:47:54 +00:00
|
|
|
struct radix_tree_node *node);
|
2014-04-03 21:47:39 +00:00
|
|
|
void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
|
2005-04-16 22:20:36 +00:00
|
|
|
void *radix_tree_delete(struct radix_tree_root *, unsigned long);
|
mm: filemap: don't plant shadow entries without radix tree node
When the underflow checks were added to workingset_node_shadow_dec(),
they triggered immediately:
kernel BUG at ./include/linux/swap.h:276!
invalid opcode: 0000 [#1] SMP
Modules linked in: isofs usb_storage fuse xt_CHECKSUM ipt_MASQUERADE nf_nat_masquerade_ipv4 tun nf_conntrack_netbios_ns nf_conntrack_broadcast ip6t_REJECT nf_reject_ipv6
soundcore wmi acpi_als pinctrl_sunrisepoint kfifo_buf tpm_tis industrialio acpi_pad pinctrl_intel tpm_tis_core tpm nfsd auth_rpcgss nfs_acl lockd grace sunrpc dm_crypt
CPU: 0 PID: 20929 Comm: blkid Not tainted 4.8.0-rc8-00087-gbe67d60ba944 #1
Hardware name: System manufacturer System Product Name/Z170-K, BIOS 1803 05/06/2016
task: ffff8faa93ecd940 task.stack: ffff8faa7f478000
RIP: page_cache_tree_insert+0xf1/0x100
Call Trace:
__add_to_page_cache_locked+0x12e/0x270
add_to_page_cache_lru+0x4e/0xe0
mpage_readpages+0x112/0x1d0
blkdev_readpages+0x1d/0x20
__do_page_cache_readahead+0x1ad/0x290
force_page_cache_readahead+0xaa/0x100
page_cache_sync_readahead+0x3f/0x50
generic_file_read_iter+0x5af/0x740
blkdev_read_iter+0x35/0x40
__vfs_read+0xe1/0x130
vfs_read+0x96/0x130
SyS_read+0x55/0xc0
entry_SYSCALL_64_fastpath+0x13/0x8f
Code: 03 00 48 8b 5d d8 65 48 33 1c 25 28 00 00 00 44 89 e8 75 19 48 83 c4 18 5b 41 5c 41 5d 41 5e 5d c3 0f 0b 41 bd ef ff ff ff eb d7 <0f> 0b e8 88 68 ef ff 0f 1f 84 00
RIP page_cache_tree_insert+0xf1/0x100
This is a long-standing bug in the way shadow entries are accounted in
the radix tree nodes. The shrinker needs to know when radix tree nodes
contain only shadow entries, no pages, so node->count is split in half
to count shadows in the upper bits and pages in the lower bits.
Unfortunately, the radix tree implementation doesn't know of this and
assumes all entries are in node->count. When there is a shadow entry
directly in root->rnode and the tree is later extended, the radix tree
implementation will copy that entry into the new node and and bump its
node->count, i.e. increases the page count bits. Once the shadow gets
removed and we subtract from the upper counter, node->count underflows
and triggers the warning. Afterwards, without node->count reaching 0
again, the radix tree node is leaked.
Limit shadow entries to when we have actual radix tree nodes and can
count them properly. That means we lose the ability to detect refaults
from files that had only the first page faulted in at eviction time.
Fixes: 449dd6984d0e ("mm: keep page cache radix tree nodes in check")
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
Reported-and-tested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: stable@vger.kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-10-04 20:02:08 +00:00
|
|
|
void radix_tree_clear_tags(struct radix_tree_root *root,
|
|
|
|
struct radix_tree_node *node,
|
|
|
|
void **slot);
|
2016-05-21 00:03:45 +00:00
|
|
|
unsigned int radix_tree_gang_lookup(struct radix_tree_root *root,
|
|
|
|
void **results, unsigned long first_index,
|
|
|
|
unsigned int max_items);
|
radix_tree: exceptional entries and indices
A patchset to extend tmpfs to MAX_LFS_FILESIZE by abandoning its
peculiar swap vector, instead keeping a file's swap entries in the same
radix tree as its struct page pointers: thus saving memory, and
simplifying its code and locking.
This patch:
The radix_tree is used by several subsystems for different purposes. A
major use is to store the struct page pointers of a file's pagecache for
memory management. But what if mm wanted to store something other than
page pointers there too?
The low bit of a radix_tree entry is already used to denote an indirect
pointer, for internal use, and the unlikely radix_tree_deref_retry()
case.
Define the next bit as denoting an exceptional entry, and supply inline
functions radix_tree_exception() to return non-0 in either unlikely
case, and radix_tree_exceptional_entry() to return non-0 in the second
case.
If a subsystem already uses radix_tree with that bit set, no problem: it
does not affect internal workings at all, but is defined for the
convenience of those storing well-aligned pointers in the radix_tree.
The radix_tree_gang_lookups have an implicit assumption that the caller
can deduce the offset of each entry returned e.g. by the page->index of
a struct page. But that may not be feasible for some kinds of item to
be stored there.
radix_tree_gang_lookup_slot() allow for an optional indices argument,
output array in which to return those offsets. The same could be added
to other radix_tree_gang_lookups, but for now keep it to the only one
for which we need it.
Signed-off-by: Hugh Dickins <hughd@google.com>
Acked-by: Rik van Riel <riel@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-08-03 23:21:18 +00:00
|
|
|
unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
|
|
|
|
void ***results, unsigned long *indices,
|
2008-07-26 02:45:29 +00:00
|
|
|
unsigned long first_index, unsigned int max_items);
|
2005-10-07 06:46:04 +00:00
|
|
|
int radix_tree_preload(gfp_t gfp_mask);
|
2013-09-11 21:26:05 +00:00
|
|
|
int radix_tree_maybe_preload(gfp_t gfp_mask);
|
2016-07-26 22:26:02 +00:00
|
|
|
int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order);
|
2005-04-16 22:20:36 +00:00
|
|
|
void radix_tree_init(void);
|
|
|
|
void *radix_tree_tag_set(struct radix_tree_root *root,
|
2006-03-25 11:08:05 +00:00
|
|
|
unsigned long index, unsigned int tag);
|
2005-04-16 22:20:36 +00:00
|
|
|
void *radix_tree_tag_clear(struct radix_tree_root *root,
|
2006-03-25 11:08:05 +00:00
|
|
|
unsigned long index, unsigned int tag);
|
2005-04-16 22:20:36 +00:00
|
|
|
int radix_tree_tag_get(struct radix_tree_root *root,
|
2006-03-25 11:08:05 +00:00
|
|
|
unsigned long index, unsigned int tag);
|
2016-12-14 23:08:55 +00:00
|
|
|
void radix_tree_iter_tag_set(struct radix_tree_root *root,
|
|
|
|
const struct radix_tree_iter *iter, unsigned int tag);
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned int
|
|
|
|
radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
|
2006-03-25 11:08:05 +00:00
|
|
|
unsigned long first_index, unsigned int max_items,
|
|
|
|
unsigned int tag);
|
2008-07-26 02:45:29 +00:00
|
|
|
unsigned int
|
|
|
|
radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
|
|
|
|
unsigned long first_index, unsigned int max_items,
|
|
|
|
unsigned int tag);
|
2006-03-25 11:08:05 +00:00
|
|
|
int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static inline void radix_tree_preload_end(void)
|
|
|
|
{
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:09:04 +00:00
|
|
|
int radix_tree_split_preload(unsigned old_order, unsigned new_order, gfp_t);
|
2016-12-14 23:09:01 +00:00
|
|
|
int radix_tree_split(struct radix_tree_root *, unsigned long index,
|
|
|
|
unsigned new_order);
|
2016-12-14 23:08:58 +00:00
|
|
|
int radix_tree_join(struct radix_tree_root *, unsigned long index,
|
|
|
|
unsigned new_order, void *);
|
|
|
|
|
2012-03-28 21:42:53 +00:00
|
|
|
#define RADIX_TREE_ITER_TAG_MASK 0x00FF /* tag index in lower byte */
|
|
|
|
#define RADIX_TREE_ITER_TAGGED 0x0100 /* lookup tagged slots */
|
|
|
|
#define RADIX_TREE_ITER_CONTIG 0x0200 /* stop at first hole */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_iter_init - initialize radix tree iterator
|
|
|
|
*
|
|
|
|
* @iter: pointer to iterator state
|
|
|
|
* @start: iteration starting index
|
|
|
|
* Returns: NULL
|
|
|
|
*/
|
|
|
|
static __always_inline void **
|
|
|
|
radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
|
|
|
|
* in the case of a successful tagged chunk lookup. If the lookup was
|
|
|
|
* unsuccessful or non-tagged then nobody cares about ->tags.
|
|
|
|
*
|
|
|
|
* Set index to zero to bypass next_index overflow protection.
|
|
|
|
* See the comment in radix_tree_next_chunk() for details.
|
|
|
|
*/
|
|
|
|
iter->index = 0;
|
|
|
|
iter->next_index = start;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_next_chunk - find next chunk of slots for iteration
|
|
|
|
*
|
|
|
|
* @root: radix tree root
|
|
|
|
* @iter: iterator state
|
|
|
|
* @flags: RADIX_TREE_ITER_* flags and tag index
|
|
|
|
* Returns: pointer to chunk first slot, or NULL if there no more left
|
|
|
|
*
|
|
|
|
* This function looks up the next chunk in the radix tree starting from
|
|
|
|
* @iter->next_index. It returns a pointer to the chunk's first slot.
|
|
|
|
* Also it fills @iter with data about chunk: position in the tree (index),
|
|
|
|
* its end (next_index), and constructs a bit mask for tagged iterating (tags).
|
|
|
|
*/
|
|
|
|
void **radix_tree_next_chunk(struct radix_tree_root *root,
|
|
|
|
struct radix_tree_iter *iter, unsigned flags);
|
|
|
|
|
2016-02-03 00:57:52 +00:00
|
|
|
/**
|
|
|
|
* radix_tree_iter_retry - retry this chunk of the iteration
|
|
|
|
* @iter: iterator state
|
|
|
|
*
|
|
|
|
* If we iterate over a tree protected only by the RCU lock, a race
|
|
|
|
* against deletion or creation may result in seeing a slot for which
|
|
|
|
* radix_tree_deref_retry() returns true. If so, call this function
|
|
|
|
* and continue the iteration.
|
|
|
|
*/
|
|
|
|
static inline __must_check
|
|
|
|
void **radix_tree_iter_retry(struct radix_tree_iter *iter)
|
|
|
|
{
|
|
|
|
iter->next_index = iter->index;
|
2016-07-20 22:45:00 +00:00
|
|
|
iter->tags = 0;
|
2016-02-03 00:57:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-21 00:02:26 +00:00
|
|
|
static inline unsigned long
|
|
|
|
__radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
|
|
|
|
{
|
|
|
|
return iter->index + (slots << iter_shift(iter));
|
|
|
|
}
|
|
|
|
|
2016-03-17 21:22:06 +00:00
|
|
|
/**
|
2016-12-14 23:08:49 +00:00
|
|
|
* radix_tree_iter_resume - resume iterating when the chunk may be invalid
|
|
|
|
* @slot: pointer to current slot
|
|
|
|
* @iter: iterator state
|
|
|
|
* Returns: New slot pointer
|
2016-03-17 21:22:06 +00:00
|
|
|
*
|
|
|
|
* If the iterator needs to release then reacquire a lock, the chunk may
|
|
|
|
* have been invalidated by an insertion or deletion. Call this function
|
2016-12-14 23:08:49 +00:00
|
|
|
* before releasing the lock to continue the iteration from the next index.
|
2016-03-17 21:22:06 +00:00
|
|
|
*/
|
2016-12-14 23:08:49 +00:00
|
|
|
void **__must_check radix_tree_iter_resume(void **slot,
|
|
|
|
struct radix_tree_iter *iter);
|
2016-03-17 21:22:06 +00:00
|
|
|
|
2012-03-28 21:42:53 +00:00
|
|
|
/**
|
|
|
|
* radix_tree_chunk_size - get current chunk size
|
|
|
|
*
|
|
|
|
* @iter: pointer to radix tree iterator
|
|
|
|
* Returns: current chunk size
|
|
|
|
*/
|
2016-02-05 23:37:01 +00:00
|
|
|
static __always_inline long
|
2012-03-28 21:42:53 +00:00
|
|
|
radix_tree_chunk_size(struct radix_tree_iter *iter)
|
|
|
|
{
|
2016-05-21 00:02:26 +00:00
|
|
|
return (iter->next_index - iter->index) >> iter_shift(iter);
|
|
|
|
}
|
|
|
|
|
2016-12-14 23:08:49 +00:00
|
|
|
#ifdef CONFIG_RADIX_TREE_MULTIORDER
|
|
|
|
void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
|
|
|
|
unsigned flags);
|
|
|
|
#else
|
|
|
|
/* Can't happen without sibling entries, but the compiler can't tell that */
|
|
|
|
static inline void ** __radix_tree_next_slot(void **slot,
|
|
|
|
struct radix_tree_iter *iter, unsigned flags)
|
2016-05-21 00:02:26 +00:00
|
|
|
{
|
2016-12-14 23:08:49 +00:00
|
|
|
return slot;
|
2012-03-28 21:42:53 +00:00
|
|
|
}
|
2016-12-14 23:08:49 +00:00
|
|
|
#endif
|
2012-03-28 21:42:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_next_slot - find next slot in chunk
|
|
|
|
*
|
|
|
|
* @slot: pointer to current slot
|
|
|
|
* @iter: pointer to interator state
|
|
|
|
* @flags: RADIX_TREE_ITER_*, should be constant
|
|
|
|
* Returns: pointer to next slot, or NULL if there no more left
|
|
|
|
*
|
|
|
|
* This function updates @iter->index in the case of a successful lookup.
|
|
|
|
* For tagged lookup it also eats @iter->tags.
|
radix-tree: 'slot' can be NULL in radix_tree_next_slot()
There are four cases I can see where we could end up with a NULL 'slot' in
radix_tree_next_slot(). Yet radix_tree_next_slot() never actually checks
whether 'slot' is NULL. It just happens that for the cases where 'slot'
is NULL, some other combination of factors prevents us from dereferencing
it.
It would be very easy for someone to unwittingly change one of these
factors without realizing that we are implicitly depending on it to save
us from a NULL pointer dereference.
Add a comment documenting the things that allow 'slot' to be safely passed
as NULL to radix_tree_next_slot().
Here are details on the four cases:
1) radix_tree_iter_retry() via a non-tagged iteration like
radix_tree_for_each_slot(). In this case we currently aren't seeing a bug
because radix_tree_iter_retry() sets
iter->next_index = iter->index;
which means that in in the else case in radix_tree_next_slot(), 'count' is
zero, so we skip over the while() loop and effectively just return NULL
without ever dereferencing 'slot'.
2) radix_tree_iter_retry() via tagged iteration like
radix_tree_for_each_tagged(). This case was giving us NULL pointer
dereferences in testing, and was fixed with this commit:
commit 3cb9185c6730 ("radix-tree: fix radix_tree_iter_retry() for tagged
iterators.")
This fix doesn't explicitly check for 'slot' being NULL, though, it works
around the NULL pointer dereference by instead zeroing iter->tags in
radix_tree_iter_retry(), which makes us bail out of the if() case in
radix_tree_next_slot() before we dereference 'slot'.
3) radix_tree_iter_next() via via a non-tagged iteration like
radix_tree_for_each_slot(). This currently happens in shmem_tag_pins()
and shmem_partial_swap_usage().
As with non-tagged iteration, 'count' in the else case of
radix_tree_next_slot() is zero, so we skip over the while() loop and
effectively just return NULL without ever dereferencing 'slot'.
4) radix_tree_iter_next() via tagged iteration like
radix_tree_for_each_tagged(). This happens in shmem_wait_for_pins().
radix_tree_iter_next() zeros out iter->tags, so we end up exiting
radix_tree_next_slot() here:
if (flags & RADIX_TREE_ITER_TAGGED) {
void *canon = slot;
iter->tags >>= 1;
if (unlikely(!iter->tags))
return NULL;
Link: http://lkml.kernel.org/r/20160815194237.25967-2-ross.zwisler@linux.intel.com
Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Konstantin Khlebnikov <koct9i@gmail.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Shuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-10-11 20:51:18 +00:00
|
|
|
*
|
|
|
|
* There are several cases where 'slot' can be passed in as NULL to this
|
2016-12-14 23:08:49 +00:00
|
|
|
* function. These cases result from the use of radix_tree_iter_resume() or
|
radix-tree: 'slot' can be NULL in radix_tree_next_slot()
There are four cases I can see where we could end up with a NULL 'slot' in
radix_tree_next_slot(). Yet radix_tree_next_slot() never actually checks
whether 'slot' is NULL. It just happens that for the cases where 'slot'
is NULL, some other combination of factors prevents us from dereferencing
it.
It would be very easy for someone to unwittingly change one of these
factors without realizing that we are implicitly depending on it to save
us from a NULL pointer dereference.
Add a comment documenting the things that allow 'slot' to be safely passed
as NULL to radix_tree_next_slot().
Here are details on the four cases:
1) radix_tree_iter_retry() via a non-tagged iteration like
radix_tree_for_each_slot(). In this case we currently aren't seeing a bug
because radix_tree_iter_retry() sets
iter->next_index = iter->index;
which means that in in the else case in radix_tree_next_slot(), 'count' is
zero, so we skip over the while() loop and effectively just return NULL
without ever dereferencing 'slot'.
2) radix_tree_iter_retry() via tagged iteration like
radix_tree_for_each_tagged(). This case was giving us NULL pointer
dereferences in testing, and was fixed with this commit:
commit 3cb9185c6730 ("radix-tree: fix radix_tree_iter_retry() for tagged
iterators.")
This fix doesn't explicitly check for 'slot' being NULL, though, it works
around the NULL pointer dereference by instead zeroing iter->tags in
radix_tree_iter_retry(), which makes us bail out of the if() case in
radix_tree_next_slot() before we dereference 'slot'.
3) radix_tree_iter_next() via via a non-tagged iteration like
radix_tree_for_each_slot(). This currently happens in shmem_tag_pins()
and shmem_partial_swap_usage().
As with non-tagged iteration, 'count' in the else case of
radix_tree_next_slot() is zero, so we skip over the while() loop and
effectively just return NULL without ever dereferencing 'slot'.
4) radix_tree_iter_next() via tagged iteration like
radix_tree_for_each_tagged(). This happens in shmem_wait_for_pins().
radix_tree_iter_next() zeros out iter->tags, so we end up exiting
radix_tree_next_slot() here:
if (flags & RADIX_TREE_ITER_TAGGED) {
void *canon = slot;
iter->tags >>= 1;
if (unlikely(!iter->tags))
return NULL;
Link: http://lkml.kernel.org/r/20160815194237.25967-2-ross.zwisler@linux.intel.com
Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
Cc: Konstantin Khlebnikov <koct9i@gmail.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Shuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-10-11 20:51:18 +00:00
|
|
|
* radix_tree_iter_retry(). In these cases we don't end up dereferencing
|
|
|
|
* 'slot' because either:
|
|
|
|
* a) we are doing tagged iteration and iter->tags has been set to 0, or
|
|
|
|
* b) we are doing non-tagged iteration, and iter->index and iter->next_index
|
|
|
|
* have been set up so that radix_tree_chunk_size() returns 1 or 0.
|
2012-03-28 21:42:53 +00:00
|
|
|
*/
|
|
|
|
static __always_inline void **
|
|
|
|
radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, unsigned flags)
|
|
|
|
{
|
|
|
|
if (flags & RADIX_TREE_ITER_TAGGED) {
|
|
|
|
iter->tags >>= 1;
|
2016-05-21 00:02:26 +00:00
|
|
|
if (unlikely(!iter->tags))
|
|
|
|
return NULL;
|
2012-03-28 21:42:53 +00:00
|
|
|
if (likely(iter->tags & 1ul)) {
|
2016-05-21 00:02:26 +00:00
|
|
|
iter->index = __radix_tree_iter_add(iter, 1);
|
2016-12-14 23:08:49 +00:00
|
|
|
slot++;
|
|
|
|
goto found;
|
2012-03-28 21:42:53 +00:00
|
|
|
}
|
2016-05-21 00:02:26 +00:00
|
|
|
if (!(flags & RADIX_TREE_ITER_CONTIG)) {
|
2012-03-28 21:42:53 +00:00
|
|
|
unsigned offset = __ffs(iter->tags);
|
|
|
|
|
2016-12-14 23:08:49 +00:00
|
|
|
iter->tags >>= offset++;
|
|
|
|
iter->index = __radix_tree_iter_add(iter, offset);
|
|
|
|
slot += offset;
|
|
|
|
goto found;
|
2012-03-28 21:42:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-21 00:02:26 +00:00
|
|
|
long count = radix_tree_chunk_size(iter);
|
2012-03-28 21:42:53 +00:00
|
|
|
|
2016-05-21 00:02:26 +00:00
|
|
|
while (--count > 0) {
|
2012-03-28 21:42:53 +00:00
|
|
|
slot++;
|
2016-05-21 00:02:26 +00:00
|
|
|
iter->index = __radix_tree_iter_add(iter, 1);
|
|
|
|
|
2012-03-28 21:42:53 +00:00
|
|
|
if (likely(*slot))
|
2016-12-14 23:08:49 +00:00
|
|
|
goto found;
|
2012-06-05 17:36:33 +00:00
|
|
|
if (flags & RADIX_TREE_ITER_CONTIG) {
|
|
|
|
/* forbid switching to the next chunk */
|
|
|
|
iter->next_index = 0;
|
2012-03-28 21:42:53 +00:00
|
|
|
break;
|
2012-06-05 17:36:33 +00:00
|
|
|
}
|
2012-03-28 21:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2016-12-14 23:08:49 +00:00
|
|
|
|
|
|
|
found:
|
|
|
|
if (unlikely(radix_tree_is_internal_node(*slot)))
|
|
|
|
return __radix_tree_next_slot(slot, iter, flags);
|
|
|
|
return slot;
|
2012-03-28 21:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_for_each_slot - iterate over non-empty slots
|
|
|
|
*
|
|
|
|
* @slot: the void** variable for pointer to slot
|
|
|
|
* @root: the struct radix_tree_root pointer
|
|
|
|
* @iter: the struct radix_tree_iter pointer
|
|
|
|
* @start: iteration starting index
|
|
|
|
*
|
|
|
|
* @slot points to radix tree slot, @iter->index contains its index.
|
|
|
|
*/
|
|
|
|
#define radix_tree_for_each_slot(slot, root, iter, start) \
|
|
|
|
for (slot = radix_tree_iter_init(iter, start) ; \
|
|
|
|
slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
|
|
|
|
slot = radix_tree_next_slot(slot, iter, 0))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_for_each_contig - iterate over contiguous slots
|
|
|
|
*
|
|
|
|
* @slot: the void** variable for pointer to slot
|
|
|
|
* @root: the struct radix_tree_root pointer
|
|
|
|
* @iter: the struct radix_tree_iter pointer
|
|
|
|
* @start: iteration starting index
|
|
|
|
*
|
|
|
|
* @slot points to radix tree slot, @iter->index contains its index.
|
|
|
|
*/
|
|
|
|
#define radix_tree_for_each_contig(slot, root, iter, start) \
|
|
|
|
for (slot = radix_tree_iter_init(iter, start) ; \
|
|
|
|
slot || (slot = radix_tree_next_chunk(root, iter, \
|
|
|
|
RADIX_TREE_ITER_CONTIG)) ; \
|
|
|
|
slot = radix_tree_next_slot(slot, iter, \
|
|
|
|
RADIX_TREE_ITER_CONTIG))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_for_each_tagged - iterate over tagged slots
|
|
|
|
*
|
|
|
|
* @slot: the void** variable for pointer to slot
|
|
|
|
* @root: the struct radix_tree_root pointer
|
|
|
|
* @iter: the struct radix_tree_iter pointer
|
|
|
|
* @start: iteration starting index
|
|
|
|
* @tag: tag index
|
|
|
|
*
|
|
|
|
* @slot points to radix tree slot, @iter->index contains its index.
|
|
|
|
*/
|
|
|
|
#define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
|
|
|
|
for (slot = radix_tree_iter_init(iter, start) ; \
|
|
|
|
slot || (slot = radix_tree_next_chunk(root, iter, \
|
|
|
|
RADIX_TREE_ITER_TAGGED | tag)) ; \
|
|
|
|
slot = radix_tree_next_slot(slot, iter, \
|
2016-12-14 23:08:49 +00:00
|
|
|
RADIX_TREE_ITER_TAGGED | tag))
|
2012-03-28 21:42:53 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif /* _LINUX_RADIX_TREE_H */
|