2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001 Momchil Velikov
|
|
|
|
* Portions Copyright (C) 2001 Christoph Hellwig
|
2006-12-07 12:33:44 +08:00
|
|
|
* Copyright (C) 2006 Nick Piggin
|
2012-03-29 05:42:53 +08:00
|
|
|
* Copyright (C) 2012 Konstantin Khlebnikov
|
2005-04-17 06:20:36 +08: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
|
|
|
|
|
|
|
|
#include <linux/preempt.h>
|
|
|
|
#include <linux/types.h>
|
2011-11-24 09:12:59 +08:00
|
|
|
#include <linux/bug.h>
|
2006-12-07 12:33:44 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/rcupdate.h>
|
|
|
|
|
|
|
|
/*
|
2007-10-16 16:24:42 +08:00
|
|
|
* An indirect pointer (root->rnode pointing to a radix_tree_node, rather
|
|
|
|
* than a data item) is signalled by the low bit set in the root->rnode
|
|
|
|
* pointer.
|
2006-12-07 12:33:44 +08:00
|
|
|
*
|
2007-10-16 16:24:42 +08:00
|
|
|
* In this case root->height is > 0, but the indirect pointer tests are
|
|
|
|
* needed for RCU lookups (because root->height is unreliable). The only
|
|
|
|
* time callers need worry about this is when doing a lookup_slot under
|
|
|
|
* RCU.
|
2010-11-12 06:05:19 +08:00
|
|
|
*
|
|
|
|
* Indirect pointer in fact is also used to tag the last pointer of a node
|
|
|
|
* when it is shrunk, before we rcu free the node. See shrink code for
|
|
|
|
* details.
|
2006-12-07 12:33:44 +08: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-04 07:21:18 +08:00
|
|
|
#define RADIX_TREE_INDIRECT_PTR 1
|
|
|
|
/*
|
|
|
|
* A common use of the radix tree is to store pointers to struct pages;
|
|
|
|
* but shmem/tmpfs needs also to store swap entries in the same tree:
|
|
|
|
* those are marked as exceptional entries to distinguish them.
|
|
|
|
* 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 12:33:44 +08:00
|
|
|
|
2007-10-16 16:24:42 +08:00
|
|
|
static inline int radix_tree_is_indirect_ptr(void *ptr)
|
2006-12-07 12:33:44 +08:00
|
|
|
{
|
2007-10-16 16:24:42 +08:00
|
|
|
return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
|
2006-12-07 12:33:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** radix-tree API starts here ***/
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-08-10 08:19:12 +08:00
|
|
|
#define RADIX_TREE_MAX_TAGS 3
|
2006-06-23 17:03:22 +08:00
|
|
|
|
|
|
|
/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
|
2005-04-17 06:20:36 +08:00
|
|
|
struct radix_tree_root {
|
|
|
|
unsigned int height;
|
2005-10-21 15:18:50 +08:00
|
|
|
gfp_t gfp_mask;
|
2010-02-26 06:43:52 +08:00
|
|
|
struct radix_tree_node __rcu *rnode;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define RADIX_TREE_INIT(mask) { \
|
|
|
|
.height = 0, \
|
|
|
|
.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)->height = 0; \
|
|
|
|
(root)->gfp_mask = (mask); \
|
|
|
|
(root)->rnode = NULL; \
|
|
|
|
} while (0)
|
|
|
|
|
2006-12-07 12:33:44 +08: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 14:57:56 +08:00
|
|
|
* - any function _modifying_ the tree or tags (inserting or deleting
|
2008-02-03 22:12:47 +08:00
|
|
|
* items, setting or clearing tags) must exclude other modifications, and
|
2006-12-07 12:33:44 +08:00
|
|
|
* exclude any functions reading the tree.
|
2007-05-09 14:57:56 +08:00
|
|
|
* - any function _reading_ the tree or tags (looking up items or tags,
|
2006-12-07 12:33:44 +08: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:
|
|
|
|
* radix_tree_lookup
|
2008-07-26 10:45:29 +08:00
|
|
|
* radix_tree_lookup_slot
|
2006-12-07 12:33:44 +08:00
|
|
|
* radix_tree_tag_get
|
|
|
|
* radix_tree_gang_lookup
|
2008-07-26 10:45:29 +08:00
|
|
|
* radix_tree_gang_lookup_slot
|
2006-12-07 12:33:44 +08:00
|
|
|
* radix_tree_gang_lookup_tag
|
2008-07-26 10:45:29 +08:00
|
|
|
* radix_tree_gang_lookup_tag_slot
|
2006-12-07 12:33:44 +08:00
|
|
|
* radix_tree_tagged
|
|
|
|
*
|
2008-07-26 10:45:29 +08:00
|
|
|
* The first 7 functions are able to be called locklessly, using RCU. The
|
2006-12-07 12:33:44 +08: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-07 05:36:20 +08: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 12:33:44 +08: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-12 06:05:19 +08: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 12:33:44 +08:00
|
|
|
*/
|
|
|
|
static inline void *radix_tree_deref_slot(void **pslot)
|
|
|
|
{
|
2010-11-12 06:05:19 +08:00
|
|
|
return rcu_dereference(*pslot);
|
2006-12-07 12:33:44 +08:00
|
|
|
}
|
2010-11-12 06:05:19 +08:00
|
|
|
|
2011-01-14 07:47:21 +08: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-12 06:05:19 +08: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)
|
|
|
|
{
|
|
|
|
return unlikely((unsigned long)arg & RADIX_TREE_INDIRECT_PTR);
|
|
|
|
}
|
|
|
|
|
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-04 07:21:18 +08: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)
|
|
|
|
{
|
|
|
|
return unlikely((unsigned long)arg &
|
|
|
|
(RADIX_TREE_INDIRECT_PTR | RADIX_TREE_EXCEPTIONAL_ENTRY));
|
|
|
|
}
|
|
|
|
|
2006-12-07 12:33:44 +08:00
|
|
|
/**
|
|
|
|
* radix_tree_replace_slot - replace item in a slot
|
|
|
|
* @pslot: pointer to slot, returned by radix_tree_lookup_slot
|
|
|
|
* @item: new item to store in the slot.
|
|
|
|
*
|
|
|
|
* For use with radix_tree_lookup_slot(). Caller must hold tree write locked
|
|
|
|
* across slot lookup and replacement.
|
|
|
|
*/
|
|
|
|
static inline void radix_tree_replace_slot(void **pslot, void *item)
|
|
|
|
{
|
2007-10-16 16:24:42 +08:00
|
|
|
BUG_ON(radix_tree_is_indirect_ptr(item));
|
|
|
|
rcu_assign_pointer(*pslot, item);
|
2006-12-07 12:33:44 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
|
|
|
|
void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
|
2005-11-07 16:59:29 +08:00
|
|
|
void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
|
2005-04-17 06:20:36 +08:00
|
|
|
void *radix_tree_delete(struct radix_tree_root *, unsigned long);
|
|
|
|
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-04 07:21:18 +08:00
|
|
|
unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
|
|
|
|
void ***results, unsigned long *indices,
|
2008-07-26 10:45:29 +08:00
|
|
|
unsigned long first_index, unsigned int max_items);
|
2007-10-16 16:24:33 +08:00
|
|
|
unsigned long radix_tree_next_hole(struct radix_tree_root *root,
|
|
|
|
unsigned long index, unsigned long max_scan);
|
2009-06-17 06:31:32 +08:00
|
|
|
unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
|
|
|
|
unsigned long index, unsigned long max_scan);
|
2005-10-07 14:46:04 +08:00
|
|
|
int radix_tree_preload(gfp_t gfp_mask);
|
2005-04-17 06:20:36 +08:00
|
|
|
void radix_tree_init(void);
|
|
|
|
void *radix_tree_tag_set(struct radix_tree_root *root,
|
2006-03-25 19:08:05 +08:00
|
|
|
unsigned long index, unsigned int tag);
|
2005-04-17 06:20:36 +08:00
|
|
|
void *radix_tree_tag_clear(struct radix_tree_root *root,
|
2006-03-25 19:08:05 +08:00
|
|
|
unsigned long index, unsigned int tag);
|
2005-04-17 06:20:36 +08:00
|
|
|
int radix_tree_tag_get(struct radix_tree_root *root,
|
2006-03-25 19:08:05 +08:00
|
|
|
unsigned long index, unsigned int tag);
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned int
|
|
|
|
radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
|
2006-03-25 19:08:05 +08:00
|
|
|
unsigned long first_index, unsigned int max_items,
|
|
|
|
unsigned int tag);
|
2008-07-26 10:45:29 +08: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);
|
2010-08-10 08:19:11 +08:00
|
|
|
unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
|
|
|
|
unsigned long *first_indexp, unsigned long last_index,
|
|
|
|
unsigned long nr_to_tag,
|
|
|
|
unsigned int fromtag, unsigned int totag);
|
2006-03-25 19:08:05 +08:00
|
|
|
int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
|
tmpfs radix_tree: locate_item to speed up swapoff
We have already acknowledged that swapoff of a tmpfs file is slower than
it was before conversion to the generic radix_tree: a little slower
there will be acceptable, if the hotter paths are faster.
But it was a shock to find swapoff of a 500MB file 20 times slower on my
laptop, taking 10 minutes; and at that rate it significantly slows down
my testing.
Now, most of that turned out to be overhead from PROVE_LOCKING and
PROVE_RCU: without those it was only 4 times slower than before; and
more realistic tests on other machines don't fare as badly.
I've tried a number of things to improve it, including tagging the swap
entries, then doing lookup by tag: I'd expected that to halve the time,
but in practice it's erratic, and often counter-productive.
The only change I've so far found to make a consistent improvement, is
to short-circuit the way we go back and forth, gang lookup packing
entries into the array supplied, then shmem scanning that array for the
target entry. Scanning in place doubles the speed, so it's now only
twice as slow as before (or three times slower when the PROVEs are on).
So, add radix_tree_locate_item() as an expedient, once-off,
single-caller hack to do the lookup directly in place. #ifdef it on
CONFIG_SHMEM and CONFIG_SWAP, as much to document its limited
applicability as save space in other configurations. And, sadly,
#include sched.h for cond_resched().
Signed-off-by: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2011-08-04 07:21:27 +08:00
|
|
|
unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static inline void radix_tree_preload_end(void)
|
|
|
|
{
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
2012-03-29 05:42:53 +08:00
|
|
|
/**
|
|
|
|
* struct radix_tree_iter - radix tree iterator state
|
|
|
|
*
|
|
|
|
* @index: index of current slot
|
|
|
|
* @next_index: next-to-last index for this chunk
|
|
|
|
* @tags: bit-mask for tag-iterating
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
#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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_chunk_size - get current chunk size
|
|
|
|
*
|
|
|
|
* @iter: pointer to radix tree iterator
|
|
|
|
* Returns: current chunk size
|
|
|
|
*/
|
|
|
|
static __always_inline unsigned
|
|
|
|
radix_tree_chunk_size(struct radix_tree_iter *iter)
|
|
|
|
{
|
|
|
|
return iter->next_index - iter->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
if (likely(iter->tags & 1ul)) {
|
|
|
|
iter->index++;
|
|
|
|
return slot + 1;
|
|
|
|
}
|
|
|
|
if (!(flags & RADIX_TREE_ITER_CONTIG) && likely(iter->tags)) {
|
|
|
|
unsigned offset = __ffs(iter->tags);
|
|
|
|
|
|
|
|
iter->tags >>= offset;
|
|
|
|
iter->index += offset + 1;
|
|
|
|
return slot + offset + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned size = radix_tree_chunk_size(iter) - 1;
|
|
|
|
|
|
|
|
while (size--) {
|
|
|
|
slot++;
|
|
|
|
iter->index++;
|
|
|
|
if (likely(*slot))
|
|
|
|
return slot;
|
|
|
|
if (flags & RADIX_TREE_ITER_CONTIG)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_for_each_chunk - iterate over chunks
|
|
|
|
*
|
|
|
|
* @slot: the void** variable for pointer to chunk first slot
|
|
|
|
* @root: the struct radix_tree_root pointer
|
|
|
|
* @iter: the struct radix_tree_iter pointer
|
|
|
|
* @start: iteration starting index
|
|
|
|
* @flags: RADIX_TREE_ITER_* and tag index
|
|
|
|
*
|
|
|
|
* Locks can be released and reacquired between iterations.
|
|
|
|
*/
|
|
|
|
#define radix_tree_for_each_chunk(slot, root, iter, start, flags) \
|
|
|
|
for (slot = radix_tree_iter_init(iter, start) ; \
|
|
|
|
(slot = radix_tree_next_chunk(root, iter, flags)) ;)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* radix_tree_for_each_chunk_slot - iterate over slots in one chunk
|
|
|
|
*
|
|
|
|
* @slot: the void** variable, at the beginning points to chunk first slot
|
|
|
|
* @iter: the struct radix_tree_iter pointer
|
|
|
|
* @flags: RADIX_TREE_ITER_*, should be constant
|
|
|
|
*
|
|
|
|
* This macro is designed to be nested inside radix_tree_for_each_chunk().
|
|
|
|
* @slot points to the radix tree slot, @iter->index contains its index.
|
|
|
|
*/
|
|
|
|
#define radix_tree_for_each_chunk_slot(slot, iter, flags) \
|
|
|
|
for (; slot ; slot = radix_tree_next_slot(slot, iter, flags))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, \
|
|
|
|
RADIX_TREE_ITER_TAGGED))
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif /* _LINUX_RADIX_TREE_H */
|