Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (C) 2001 Momchil Velikov
0004  * Portions Copyright (C) 2001 Christoph Hellwig
0005  * Copyright (C) 2006 Nick Piggin
0006  * Copyright (C) 2012 Konstantin Khlebnikov
0007  */
0008 #ifndef _LINUX_RADIX_TREE_H
0009 #define _LINUX_RADIX_TREE_H
0010 
0011 #include <linux/bitops.h>
0012 #include <linux/gfp_types.h>
0013 #include <linux/list.h>
0014 #include <linux/lockdep.h>
0015 #include <linux/math.h>
0016 #include <linux/percpu.h>
0017 #include <linux/preempt.h>
0018 #include <linux/rcupdate.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/types.h>
0021 #include <linux/xarray.h>
0022 #include <linux/local_lock.h>
0023 
0024 /* Keep unconverted code working */
0025 #define radix_tree_root     xarray
0026 #define radix_tree_node     xa_node
0027 
0028 struct radix_tree_preload {
0029     local_lock_t lock;
0030     unsigned nr;
0031     /* nodes->parent points to next preallocated node */
0032     struct radix_tree_node *nodes;
0033 };
0034 DECLARE_PER_CPU(struct radix_tree_preload, radix_tree_preloads);
0035 
0036 /*
0037  * The bottom two bits of the slot determine how the remaining bits in the
0038  * slot are interpreted:
0039  *
0040  * 00 - data pointer
0041  * 10 - internal entry
0042  * x1 - value entry
0043  *
0044  * The internal entry may be a pointer to the next level in the tree, a
0045  * sibling entry, or an indicator that the entry in this slot has been moved
0046  * to another location in the tree and the lookup should be restarted.  While
0047  * NULL fits the 'data pointer' pattern, it means that there is no entry in
0048  * the tree for this index (no matter what level of the tree it is found at).
0049  * This means that storing a NULL entry in the tree is the same as deleting
0050  * the entry from the tree.
0051  */
0052 #define RADIX_TREE_ENTRY_MASK       3UL
0053 #define RADIX_TREE_INTERNAL_NODE    2UL
0054 
0055 static inline bool radix_tree_is_internal_node(void *ptr)
0056 {
0057     return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
0058                 RADIX_TREE_INTERNAL_NODE;
0059 }
0060 
0061 /*** radix-tree API starts here ***/
0062 
0063 #define RADIX_TREE_MAP_SHIFT    XA_CHUNK_SHIFT
0064 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
0065 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
0066 
0067 #define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
0068 #define RADIX_TREE_TAG_LONGS    XA_MARK_LONGS
0069 
0070 #define RADIX_TREE_INDEX_BITS  (8 /* CHAR_BIT */ * sizeof(unsigned long))
0071 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
0072                       RADIX_TREE_MAP_SHIFT))
0073 
0074 /* The IDR tag is stored in the low bits of xa_flags */
0075 #define ROOT_IS_IDR ((__force gfp_t)4)
0076 /* The top bits of xa_flags are used to store the root tags */
0077 #define ROOT_TAG_SHIFT  (__GFP_BITS_SHIFT)
0078 
0079 #define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
0080 
0081 #define RADIX_TREE(name, mask) \
0082     struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
0083 
0084 #define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
0085 
0086 static inline bool radix_tree_empty(const struct radix_tree_root *root)
0087 {
0088     return root->xa_head == NULL;
0089 }
0090 
0091 /**
0092  * struct radix_tree_iter - radix tree iterator state
0093  *
0094  * @index:  index of current slot
0095  * @next_index: one beyond the last index for this chunk
0096  * @tags:   bit-mask for tag-iterating
0097  * @node:   node that contains current slot
0098  *
0099  * This radix tree iterator works in terms of "chunks" of slots.  A chunk is a
0100  * subinterval of slots contained within one radix tree leaf node.  It is
0101  * described by a pointer to its first slot and a struct radix_tree_iter
0102  * which holds the chunk's position in the tree and its size.  For tagged
0103  * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
0104  * radix tree tag.
0105  */
0106 struct radix_tree_iter {
0107     unsigned long   index;
0108     unsigned long   next_index;
0109     unsigned long   tags;
0110     struct radix_tree_node *node;
0111 };
0112 
0113 /**
0114  * Radix-tree synchronization
0115  *
0116  * The radix-tree API requires that users provide all synchronisation (with
0117  * specific exceptions, noted below).
0118  *
0119  * Synchronization of access to the data items being stored in the tree, and
0120  * management of their lifetimes must be completely managed by API users.
0121  *
0122  * For API usage, in general,
0123  * - any function _modifying_ the tree or tags (inserting or deleting
0124  *   items, setting or clearing tags) must exclude other modifications, and
0125  *   exclude any functions reading the tree.
0126  * - any function _reading_ the tree or tags (looking up items or tags,
0127  *   gang lookups) must exclude modifications to the tree, but may occur
0128  *   concurrently with other readers.
0129  *
0130  * The notable exceptions to this rule are the following functions:
0131  * __radix_tree_lookup
0132  * radix_tree_lookup
0133  * radix_tree_lookup_slot
0134  * radix_tree_tag_get
0135  * radix_tree_gang_lookup
0136  * radix_tree_gang_lookup_tag
0137  * radix_tree_gang_lookup_tag_slot
0138  * radix_tree_tagged
0139  *
0140  * The first 7 functions are able to be called locklessly, using RCU. The
0141  * caller must ensure calls to these functions are made within rcu_read_lock()
0142  * regions. Other readers (lock-free or otherwise) and modifications may be
0143  * running concurrently.
0144  *
0145  * It is still required that the caller manage the synchronization and lifetimes
0146  * of the items. So if RCU lock-free lookups are used, typically this would mean
0147  * that the items have their own locks, or are amenable to lock-free access; and
0148  * that the items are freed by RCU (or only freed after having been deleted from
0149  * the radix tree *and* a synchronize_rcu() grace period).
0150  *
0151  * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
0152  * access to data items when inserting into or looking up from the radix tree)
0153  *
0154  * Note that the value returned by radix_tree_tag_get() may not be relied upon
0155  * if only the RCU read lock is held.  Functions to set/clear tags and to
0156  * delete nodes running concurrently with it may affect its result such that
0157  * two consecutive reads in the same locked section may return different
0158  * values.  If reliability is required, modification functions must also be
0159  * excluded from concurrency.
0160  *
0161  * radix_tree_tagged is able to be called without locking or RCU.
0162  */
0163 
0164 /**
0165  * radix_tree_deref_slot - dereference a slot
0166  * @slot: slot pointer, returned by radix_tree_lookup_slot
0167  *
0168  * For use with radix_tree_lookup_slot().  Caller must hold tree at least read
0169  * locked across slot lookup and dereference. Not required if write lock is
0170  * held (ie. items cannot be concurrently inserted).
0171  *
0172  * radix_tree_deref_retry must be used to confirm validity of the pointer if
0173  * only the read lock is held.
0174  *
0175  * Return: entry stored in that slot.
0176  */
0177 static inline void *radix_tree_deref_slot(void __rcu **slot)
0178 {
0179     return rcu_dereference(*slot);
0180 }
0181 
0182 /**
0183  * radix_tree_deref_slot_protected - dereference a slot with tree lock held
0184  * @slot: slot pointer, returned by radix_tree_lookup_slot
0185  *
0186  * Similar to radix_tree_deref_slot.  The caller does not hold the RCU read
0187  * lock but it must hold the tree lock to prevent parallel updates.
0188  *
0189  * Return: entry stored in that slot.
0190  */
0191 static inline void *radix_tree_deref_slot_protected(void __rcu **slot,
0192                             spinlock_t *treelock)
0193 {
0194     return rcu_dereference_protected(*slot, lockdep_is_held(treelock));
0195 }
0196 
0197 /**
0198  * radix_tree_deref_retry   - check radix_tree_deref_slot
0199  * @arg:    pointer returned by radix_tree_deref_slot
0200  * Returns: 0 if retry is not required, otherwise retry is required
0201  *
0202  * radix_tree_deref_retry must be used with radix_tree_deref_slot.
0203  */
0204 static inline int radix_tree_deref_retry(void *arg)
0205 {
0206     return unlikely(radix_tree_is_internal_node(arg));
0207 }
0208 
0209 /**
0210  * radix_tree_exception - radix_tree_deref_slot returned either exception?
0211  * @arg:    value returned by radix_tree_deref_slot
0212  * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
0213  */
0214 static inline int radix_tree_exception(void *arg)
0215 {
0216     return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
0217 }
0218 
0219 int radix_tree_insert(struct radix_tree_root *, unsigned long index,
0220             void *);
0221 void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,
0222               struct radix_tree_node **nodep, void __rcu ***slotp);
0223 void *radix_tree_lookup(const struct radix_tree_root *, unsigned long);
0224 void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *,
0225                     unsigned long index);
0226 void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *,
0227               void __rcu **slot, void *entry);
0228 void radix_tree_iter_replace(struct radix_tree_root *,
0229         const struct radix_tree_iter *, void __rcu **slot, void *entry);
0230 void radix_tree_replace_slot(struct radix_tree_root *,
0231                  void __rcu **slot, void *entry);
0232 void radix_tree_iter_delete(struct radix_tree_root *,
0233             struct radix_tree_iter *iter, void __rcu **slot);
0234 void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
0235 void *radix_tree_delete(struct radix_tree_root *, unsigned long);
0236 unsigned int radix_tree_gang_lookup(const struct radix_tree_root *,
0237             void **results, unsigned long first_index,
0238             unsigned int max_items);
0239 int radix_tree_preload(gfp_t gfp_mask);
0240 int radix_tree_maybe_preload(gfp_t gfp_mask);
0241 void radix_tree_init(void);
0242 void *radix_tree_tag_set(struct radix_tree_root *,
0243             unsigned long index, unsigned int tag);
0244 void *radix_tree_tag_clear(struct radix_tree_root *,
0245             unsigned long index, unsigned int tag);
0246 int radix_tree_tag_get(const struct radix_tree_root *,
0247             unsigned long index, unsigned int tag);
0248 void radix_tree_iter_tag_clear(struct radix_tree_root *,
0249         const struct radix_tree_iter *iter, unsigned int tag);
0250 unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *,
0251         void **results, unsigned long first_index,
0252         unsigned int max_items, unsigned int tag);
0253 unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *,
0254         void __rcu ***results, unsigned long first_index,
0255         unsigned int max_items, unsigned int tag);
0256 int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag);
0257 
0258 static inline void radix_tree_preload_end(void)
0259 {
0260     local_unlock(&radix_tree_preloads.lock);
0261 }
0262 
0263 void __rcu **idr_get_free(struct radix_tree_root *root,
0264                   struct radix_tree_iter *iter, gfp_t gfp,
0265                   unsigned long max);
0266 
0267 enum {
0268     RADIX_TREE_ITER_TAG_MASK = 0x0f,    /* tag index in lower nybble */
0269     RADIX_TREE_ITER_TAGGED   = 0x10,    /* lookup tagged slots */
0270     RADIX_TREE_ITER_CONTIG   = 0x20,    /* stop at first hole */
0271 };
0272 
0273 /**
0274  * radix_tree_iter_init - initialize radix tree iterator
0275  *
0276  * @iter:   pointer to iterator state
0277  * @start:  iteration starting index
0278  * Returns: NULL
0279  */
0280 static __always_inline void __rcu **
0281 radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
0282 {
0283     /*
0284      * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
0285      * in the case of a successful tagged chunk lookup.  If the lookup was
0286      * unsuccessful or non-tagged then nobody cares about ->tags.
0287      *
0288      * Set index to zero to bypass next_index overflow protection.
0289      * See the comment in radix_tree_next_chunk() for details.
0290      */
0291     iter->index = 0;
0292     iter->next_index = start;
0293     return NULL;
0294 }
0295 
0296 /**
0297  * radix_tree_next_chunk - find next chunk of slots for iteration
0298  *
0299  * @root:   radix tree root
0300  * @iter:   iterator state
0301  * @flags:  RADIX_TREE_ITER_* flags and tag index
0302  * Returns: pointer to chunk first slot, or NULL if there no more left
0303  *
0304  * This function looks up the next chunk in the radix tree starting from
0305  * @iter->next_index.  It returns a pointer to the chunk's first slot.
0306  * Also it fills @iter with data about chunk: position in the tree (index),
0307  * its end (next_index), and constructs a bit mask for tagged iterating (tags).
0308  */
0309 void __rcu **radix_tree_next_chunk(const struct radix_tree_root *,
0310                  struct radix_tree_iter *iter, unsigned flags);
0311 
0312 /**
0313  * radix_tree_iter_lookup - look up an index in the radix tree
0314  * @root: radix tree root
0315  * @iter: iterator state
0316  * @index: key to look up
0317  *
0318  * If @index is present in the radix tree, this function returns the slot
0319  * containing it and updates @iter to describe the entry.  If @index is not
0320  * present, it returns NULL.
0321  */
0322 static inline void __rcu **
0323 radix_tree_iter_lookup(const struct radix_tree_root *root,
0324             struct radix_tree_iter *iter, unsigned long index)
0325 {
0326     radix_tree_iter_init(iter, index);
0327     return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG);
0328 }
0329 
0330 /**
0331  * radix_tree_iter_retry - retry this chunk of the iteration
0332  * @iter:   iterator state
0333  *
0334  * If we iterate over a tree protected only by the RCU lock, a race
0335  * against deletion or creation may result in seeing a slot for which
0336  * radix_tree_deref_retry() returns true.  If so, call this function
0337  * and continue the iteration.
0338  */
0339 static inline __must_check
0340 void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter)
0341 {
0342     iter->next_index = iter->index;
0343     iter->tags = 0;
0344     return NULL;
0345 }
0346 
0347 static inline unsigned long
0348 __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
0349 {
0350     return iter->index + slots;
0351 }
0352 
0353 /**
0354  * radix_tree_iter_resume - resume iterating when the chunk may be invalid
0355  * @slot: pointer to current slot
0356  * @iter: iterator state
0357  * Returns: New slot pointer
0358  *
0359  * If the iterator needs to release then reacquire a lock, the chunk may
0360  * have been invalidated by an insertion or deletion.  Call this function
0361  * before releasing the lock to continue the iteration from the next index.
0362  */
0363 void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot,
0364                     struct radix_tree_iter *iter);
0365 
0366 /**
0367  * radix_tree_chunk_size - get current chunk size
0368  *
0369  * @iter:   pointer to radix tree iterator
0370  * Returns: current chunk size
0371  */
0372 static __always_inline long
0373 radix_tree_chunk_size(struct radix_tree_iter *iter)
0374 {
0375     return iter->next_index - iter->index;
0376 }
0377 
0378 /**
0379  * radix_tree_next_slot - find next slot in chunk
0380  *
0381  * @slot:   pointer to current slot
0382  * @iter:   pointer to iterator state
0383  * @flags:  RADIX_TREE_ITER_*, should be constant
0384  * Returns: pointer to next slot, or NULL if there no more left
0385  *
0386  * This function updates @iter->index in the case of a successful lookup.
0387  * For tagged lookup it also eats @iter->tags.
0388  *
0389  * There are several cases where 'slot' can be passed in as NULL to this
0390  * function.  These cases result from the use of radix_tree_iter_resume() or
0391  * radix_tree_iter_retry().  In these cases we don't end up dereferencing
0392  * 'slot' because either:
0393  * a) we are doing tagged iteration and iter->tags has been set to 0, or
0394  * b) we are doing non-tagged iteration, and iter->index and iter->next_index
0395  *    have been set up so that radix_tree_chunk_size() returns 1 or 0.
0396  */
0397 static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot,
0398                 struct radix_tree_iter *iter, unsigned flags)
0399 {
0400     if (flags & RADIX_TREE_ITER_TAGGED) {
0401         iter->tags >>= 1;
0402         if (unlikely(!iter->tags))
0403             return NULL;
0404         if (likely(iter->tags & 1ul)) {
0405             iter->index = __radix_tree_iter_add(iter, 1);
0406             slot++;
0407             goto found;
0408         }
0409         if (!(flags & RADIX_TREE_ITER_CONTIG)) {
0410             unsigned offset = __ffs(iter->tags);
0411 
0412             iter->tags >>= offset++;
0413             iter->index = __radix_tree_iter_add(iter, offset);
0414             slot += offset;
0415             goto found;
0416         }
0417     } else {
0418         long count = radix_tree_chunk_size(iter);
0419 
0420         while (--count > 0) {
0421             slot++;
0422             iter->index = __radix_tree_iter_add(iter, 1);
0423 
0424             if (likely(*slot))
0425                 goto found;
0426             if (flags & RADIX_TREE_ITER_CONTIG) {
0427                 /* forbid switching to the next chunk */
0428                 iter->next_index = 0;
0429                 break;
0430             }
0431         }
0432     }
0433     return NULL;
0434 
0435  found:
0436     return slot;
0437 }
0438 
0439 /**
0440  * radix_tree_for_each_slot - iterate over non-empty slots
0441  *
0442  * @slot:   the void** variable for pointer to slot
0443  * @root:   the struct radix_tree_root pointer
0444  * @iter:   the struct radix_tree_iter pointer
0445  * @start:  iteration starting index
0446  *
0447  * @slot points to radix tree slot, @iter->index contains its index.
0448  */
0449 #define radix_tree_for_each_slot(slot, root, iter, start)       \
0450     for (slot = radix_tree_iter_init(iter, start) ;         \
0451          slot || (slot = radix_tree_next_chunk(root, iter, 0)) ;    \
0452          slot = radix_tree_next_slot(slot, iter, 0))
0453 
0454 /**
0455  * radix_tree_for_each_tagged - iterate over tagged slots
0456  *
0457  * @slot:   the void** variable for pointer to slot
0458  * @root:   the struct radix_tree_root pointer
0459  * @iter:   the struct radix_tree_iter pointer
0460  * @start:  iteration starting index
0461  * @tag:    tag index
0462  *
0463  * @slot points to radix tree slot, @iter->index contains its index.
0464  */
0465 #define radix_tree_for_each_tagged(slot, root, iter, start, tag)    \
0466     for (slot = radix_tree_iter_init(iter, start) ;         \
0467          slot || (slot = radix_tree_next_chunk(root, iter,      \
0468                   RADIX_TREE_ITER_TAGGED | tag)) ;      \
0469          slot = radix_tree_next_slot(slot, iter,            \
0470                 RADIX_TREE_ITER_TAGGED | tag))
0471 
0472 #endif /* _LINUX_RADIX_TREE_H */