radix-tree.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Copyright (C) 2001 Momchil Velikov
  3. * Portions Copyright (C) 2001 Christoph Hellwig
  4. * Copyright (C) 2006 Nick Piggin
  5. * Copyright (C) 2012 Konstantin Khlebnikov
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #ifndef _LINUX_RADIX_TREE_H
  22. #define _LINUX_RADIX_TREE_H
  23. #include <linux/preempt.h>
  24. #include <linux/types.h>
  25. #include <linux/bug.h>
  26. #include <linux/kernel.h>
  27. #include <linux/rcupdate.h>
  28. /*
  29. * An indirect pointer (root->rnode pointing to a radix_tree_node, rather
  30. * than a data item) is signalled by the low bit set in the root->rnode
  31. * pointer.
  32. *
  33. * In this case root->height is > 0, but the indirect pointer tests are
  34. * needed for RCU lookups (because root->height is unreliable). The only
  35. * time callers need worry about this is when doing a lookup_slot under
  36. * RCU.
  37. *
  38. * Indirect pointer in fact is also used to tag the last pointer of a node
  39. * when it is shrunk, before we rcu free the node. See shrink code for
  40. * details.
  41. */
  42. #define RADIX_TREE_INDIRECT_PTR 1
  43. /*
  44. * A common use of the radix tree is to store pointers to struct pages;
  45. * but shmem/tmpfs needs also to store swap entries in the same tree:
  46. * those are marked as exceptional entries to distinguish them.
  47. * EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it.
  48. */
  49. #define RADIX_TREE_EXCEPTIONAL_ENTRY 2
  50. #define RADIX_TREE_EXCEPTIONAL_SHIFT 2
  51. static inline int radix_tree_is_indirect_ptr(void *ptr)
  52. {
  53. return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
  54. }
  55. /*** radix-tree API starts here ***/
  56. #define RADIX_TREE_MAX_TAGS 3
  57. #ifdef __KERNEL__
  58. #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  59. #else
  60. #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
  61. #endif
  62. #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
  63. #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
  64. #define RADIX_TREE_TAG_LONGS \
  65. ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
  66. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  67. #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
  68. RADIX_TREE_MAP_SHIFT))
  69. /* Height component in node->path */
  70. #define RADIX_TREE_HEIGHT_SHIFT (RADIX_TREE_MAX_PATH + 1)
  71. #define RADIX_TREE_HEIGHT_MASK ((1UL << RADIX_TREE_HEIGHT_SHIFT) - 1)
  72. /* Internally used bits of node->count */
  73. #define RADIX_TREE_COUNT_SHIFT (RADIX_TREE_MAP_SHIFT + 1)
  74. #define RADIX_TREE_COUNT_MASK ((1UL << RADIX_TREE_COUNT_SHIFT) - 1)
  75. struct radix_tree_node {
  76. unsigned int path; /* Offset in parent & height from the bottom */
  77. unsigned int count;
  78. union {
  79. struct {
  80. /* Used when ascending tree */
  81. struct radix_tree_node *parent;
  82. /* For tree user */
  83. void *private_data;
  84. };
  85. /* Used when freeing node */
  86. struct rcu_head rcu_head;
  87. };
  88. /* For tree user */
  89. struct list_head private_list;
  90. void __rcu *slots[RADIX_TREE_MAP_SIZE];
  91. unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
  92. };
  93. /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
  94. struct radix_tree_root {
  95. unsigned int height;
  96. gfp_t gfp_mask;
  97. struct radix_tree_node __rcu *rnode;
  98. };
  99. #define RADIX_TREE_INIT(mask) { \
  100. .height = 0, \
  101. .gfp_mask = (mask), \
  102. .rnode = NULL, \
  103. }
  104. #define RADIX_TREE(name, mask) \
  105. struct radix_tree_root name = RADIX_TREE_INIT(mask)
  106. #define INIT_RADIX_TREE(root, mask) \
  107. do { \
  108. (root)->height = 0; \
  109. (root)->gfp_mask = (mask); \
  110. (root)->rnode = NULL; \
  111. } while (0)
  112. /**
  113. * Radix-tree synchronization
  114. *
  115. * The radix-tree API requires that users provide all synchronisation (with
  116. * specific exceptions, noted below).
  117. *
  118. * Synchronization of access to the data items being stored in the tree, and
  119. * management of their lifetimes must be completely managed by API users.
  120. *
  121. * For API usage, in general,
  122. * - any function _modifying_ the tree or tags (inserting or deleting
  123. * items, setting or clearing tags) must exclude other modifications, and
  124. * exclude any functions reading the tree.
  125. * - any function _reading_ the tree or tags (looking up items or tags,
  126. * gang lookups) must exclude modifications to the tree, but may occur
  127. * concurrently with other readers.
  128. *
  129. * The notable exceptions to this rule are the following functions:
  130. * __radix_tree_lookup
  131. * radix_tree_lookup
  132. * radix_tree_lookup_slot
  133. * radix_tree_tag_get
  134. * radix_tree_gang_lookup
  135. * radix_tree_gang_lookup_slot
  136. * radix_tree_gang_lookup_tag
  137. * radix_tree_gang_lookup_tag_slot
  138. * radix_tree_tagged
  139. *
  140. * The first 7 functions are able to be called locklessly, using RCU. The
  141. * caller must ensure calls to these functions are made within rcu_read_lock()
  142. * regions. Other readers (lock-free or otherwise) and modifications may be
  143. * running concurrently.
  144. *
  145. * It is still required that the caller manage the synchronization and lifetimes
  146. * of the items. So if RCU lock-free lookups are used, typically this would mean
  147. * that the items have their own locks, or are amenable to lock-free access; and
  148. * that the items are freed by RCU (or only freed after having been deleted from
  149. * the radix tree *and* a synchronize_rcu() grace period).
  150. *
  151. * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
  152. * access to data items when inserting into or looking up from the radix tree)
  153. *
  154. * Note that the value returned by radix_tree_tag_get() may not be relied upon
  155. * if only the RCU read lock is held. Functions to set/clear tags and to
  156. * delete nodes running concurrently with it may affect its result such that
  157. * two consecutive reads in the same locked section may return different
  158. * values. If reliability is required, modification functions must also be
  159. * excluded from concurrency.
  160. *
  161. * radix_tree_tagged is able to be called without locking or RCU.
  162. */
  163. /**
  164. * radix_tree_deref_slot - dereference a slot
  165. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  166. * Returns: item that was stored in that slot with any direct pointer flag
  167. * removed.
  168. *
  169. * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
  170. * locked across slot lookup and dereference. Not required if write lock is
  171. * held (ie. items cannot be concurrently inserted).
  172. *
  173. * radix_tree_deref_retry must be used to confirm validity of the pointer if
  174. * only the read lock is held.
  175. */
  176. static inline void *radix_tree_deref_slot(void **pslot)
  177. {
  178. return rcu_dereference(*pslot);
  179. }
  180. /**
  181. * radix_tree_deref_slot_protected - dereference a slot without RCU lock but with tree lock held
  182. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  183. * Returns: item that was stored in that slot with any direct pointer flag
  184. * removed.
  185. *
  186. * Similar to radix_tree_deref_slot but only used during migration when a pages
  187. * mapping is being moved. The caller does not hold the RCU read lock but it
  188. * must hold the tree lock to prevent parallel updates.
  189. */
  190. static inline void *radix_tree_deref_slot_protected(void **pslot,
  191. spinlock_t *treelock)
  192. {
  193. return rcu_dereference_protected(*pslot, lockdep_is_held(treelock));
  194. }
  195. /**
  196. * radix_tree_deref_retry - check radix_tree_deref_slot
  197. * @arg: pointer returned by radix_tree_deref_slot
  198. * Returns: 0 if retry is not required, otherwise retry is required
  199. *
  200. * radix_tree_deref_retry must be used with radix_tree_deref_slot.
  201. */
  202. static inline int radix_tree_deref_retry(void *arg)
  203. {
  204. return unlikely((unsigned long)arg & RADIX_TREE_INDIRECT_PTR);
  205. }
  206. /**
  207. * radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry?
  208. * @arg: value returned by radix_tree_deref_slot
  209. * Returns: 0 if well-aligned pointer, non-0 if exceptional entry.
  210. */
  211. static inline int radix_tree_exceptional_entry(void *arg)
  212. {
  213. /* Not unlikely because radix_tree_exception often tested first */
  214. return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY;
  215. }
  216. /**
  217. * radix_tree_exception - radix_tree_deref_slot returned either exception?
  218. * @arg: value returned by radix_tree_deref_slot
  219. * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
  220. */
  221. static inline int radix_tree_exception(void *arg)
  222. {
  223. return unlikely((unsigned long)arg &
  224. (RADIX_TREE_INDIRECT_PTR | RADIX_TREE_EXCEPTIONAL_ENTRY));
  225. }
  226. /**
  227. * radix_tree_replace_slot - replace item in a slot
  228. * @pslot: pointer to slot, returned by radix_tree_lookup_slot
  229. * @item: new item to store in the slot.
  230. *
  231. * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
  232. * across slot lookup and replacement.
  233. */
  234. static inline void radix_tree_replace_slot(void **pslot, void *item)
  235. {
  236. BUG_ON(radix_tree_is_indirect_ptr(item));
  237. rcu_assign_pointer(*pslot, item);
  238. }
  239. int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
  240. struct radix_tree_node **nodep, void ***slotp);
  241. int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
  242. void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
  243. struct radix_tree_node **nodep, void ***slotp);
  244. void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
  245. void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
  246. bool __radix_tree_delete_node(struct radix_tree_root *root,
  247. struct radix_tree_node *node);
  248. void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
  249. void *radix_tree_delete(struct radix_tree_root *, unsigned long);
  250. unsigned int
  251. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  252. unsigned long first_index, unsigned int max_items);
  253. unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  254. void ***results, unsigned long *indices,
  255. unsigned long first_index, unsigned int max_items);
  256. int radix_tree_preload(gfp_t gfp_mask);
  257. int radix_tree_maybe_preload(gfp_t gfp_mask);
  258. void radix_tree_init(void);
  259. void *radix_tree_tag_set(struct radix_tree_root *root,
  260. unsigned long index, unsigned int tag);
  261. void *radix_tree_tag_clear(struct radix_tree_root *root,
  262. unsigned long index, unsigned int tag);
  263. int radix_tree_tag_get(struct radix_tree_root *root,
  264. unsigned long index, unsigned int tag);
  265. unsigned int
  266. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  267. unsigned long first_index, unsigned int max_items,
  268. unsigned int tag);
  269. unsigned int
  270. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  271. unsigned long first_index, unsigned int max_items,
  272. unsigned int tag);
  273. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  274. unsigned long *first_indexp, unsigned long last_index,
  275. unsigned long nr_to_tag,
  276. unsigned int fromtag, unsigned int totag);
  277. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
  278. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
  279. static inline void radix_tree_preload_end(void)
  280. {
  281. preempt_enable();
  282. }
  283. /**
  284. * struct radix_tree_iter - radix tree iterator state
  285. *
  286. * @index: index of current slot
  287. * @next_index: next-to-last index for this chunk
  288. * @tags: bit-mask for tag-iterating
  289. *
  290. * This radix tree iterator works in terms of "chunks" of slots. A chunk is a
  291. * subinterval of slots contained within one radix tree leaf node. It is
  292. * described by a pointer to its first slot and a struct radix_tree_iter
  293. * which holds the chunk's position in the tree and its size. For tagged
  294. * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
  295. * radix tree tag.
  296. */
  297. struct radix_tree_iter {
  298. unsigned long index;
  299. unsigned long next_index;
  300. unsigned long tags;
  301. };
  302. #define RADIX_TREE_ITER_TAG_MASK 0x00FF /* tag index in lower byte */
  303. #define RADIX_TREE_ITER_TAGGED 0x0100 /* lookup tagged slots */
  304. #define RADIX_TREE_ITER_CONTIG 0x0200 /* stop at first hole */
  305. /**
  306. * radix_tree_iter_init - initialize radix tree iterator
  307. *
  308. * @iter: pointer to iterator state
  309. * @start: iteration starting index
  310. * Returns: NULL
  311. */
  312. static __always_inline void **
  313. radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
  314. {
  315. /*
  316. * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
  317. * in the case of a successful tagged chunk lookup. If the lookup was
  318. * unsuccessful or non-tagged then nobody cares about ->tags.
  319. *
  320. * Set index to zero to bypass next_index overflow protection.
  321. * See the comment in radix_tree_next_chunk() for details.
  322. */
  323. iter->index = 0;
  324. iter->next_index = start;
  325. return NULL;
  326. }
  327. /**
  328. * radix_tree_next_chunk - find next chunk of slots for iteration
  329. *
  330. * @root: radix tree root
  331. * @iter: iterator state
  332. * @flags: RADIX_TREE_ITER_* flags and tag index
  333. * Returns: pointer to chunk first slot, or NULL if there no more left
  334. *
  335. * This function looks up the next chunk in the radix tree starting from
  336. * @iter->next_index. It returns a pointer to the chunk's first slot.
  337. * Also it fills @iter with data about chunk: position in the tree (index),
  338. * its end (next_index), and constructs a bit mask for tagged iterating (tags).
  339. */
  340. void **radix_tree_next_chunk(struct radix_tree_root *root,
  341. struct radix_tree_iter *iter, unsigned flags);
  342. /**
  343. * radix_tree_chunk_size - get current chunk size
  344. *
  345. * @iter: pointer to radix tree iterator
  346. * Returns: current chunk size
  347. */
  348. static __always_inline unsigned
  349. radix_tree_chunk_size(struct radix_tree_iter *iter)
  350. {
  351. return iter->next_index - iter->index;
  352. }
  353. /**
  354. * radix_tree_next_slot - find next slot in chunk
  355. *
  356. * @slot: pointer to current slot
  357. * @iter: pointer to interator state
  358. * @flags: RADIX_TREE_ITER_*, should be constant
  359. * Returns: pointer to next slot, or NULL if there no more left
  360. *
  361. * This function updates @iter->index in the case of a successful lookup.
  362. * For tagged lookup it also eats @iter->tags.
  363. */
  364. static __always_inline void **
  365. radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, unsigned flags)
  366. {
  367. if (flags & RADIX_TREE_ITER_TAGGED) {
  368. iter->tags >>= 1;
  369. if (likely(iter->tags & 1ul)) {
  370. iter->index++;
  371. return slot + 1;
  372. }
  373. if (!(flags & RADIX_TREE_ITER_CONTIG) && likely(iter->tags)) {
  374. unsigned offset = __ffs(iter->tags);
  375. iter->tags >>= offset;
  376. iter->index += offset + 1;
  377. return slot + offset + 1;
  378. }
  379. } else {
  380. unsigned size = radix_tree_chunk_size(iter) - 1;
  381. while (size--) {
  382. slot++;
  383. iter->index++;
  384. if (likely(*slot))
  385. return slot;
  386. if (flags & RADIX_TREE_ITER_CONTIG) {
  387. /* forbid switching to the next chunk */
  388. iter->next_index = 0;
  389. break;
  390. }
  391. }
  392. }
  393. return NULL;
  394. }
  395. /**
  396. * radix_tree_for_each_chunk - iterate over chunks
  397. *
  398. * @slot: the void** variable for pointer to chunk first slot
  399. * @root: the struct radix_tree_root pointer
  400. * @iter: the struct radix_tree_iter pointer
  401. * @start: iteration starting index
  402. * @flags: RADIX_TREE_ITER_* and tag index
  403. *
  404. * Locks can be released and reacquired between iterations.
  405. */
  406. #define radix_tree_for_each_chunk(slot, root, iter, start, flags) \
  407. for (slot = radix_tree_iter_init(iter, start) ; \
  408. (slot = radix_tree_next_chunk(root, iter, flags)) ;)
  409. /**
  410. * radix_tree_for_each_chunk_slot - iterate over slots in one chunk
  411. *
  412. * @slot: the void** variable, at the beginning points to chunk first slot
  413. * @iter: the struct radix_tree_iter pointer
  414. * @flags: RADIX_TREE_ITER_*, should be constant
  415. *
  416. * This macro is designed to be nested inside radix_tree_for_each_chunk().
  417. * @slot points to the radix tree slot, @iter->index contains its index.
  418. */
  419. #define radix_tree_for_each_chunk_slot(slot, iter, flags) \
  420. for (; slot ; slot = radix_tree_next_slot(slot, iter, flags))
  421. /**
  422. * radix_tree_for_each_slot - iterate over non-empty slots
  423. *
  424. * @slot: the void** variable for pointer to slot
  425. * @root: the struct radix_tree_root pointer
  426. * @iter: the struct radix_tree_iter pointer
  427. * @start: iteration starting index
  428. *
  429. * @slot points to radix tree slot, @iter->index contains its index.
  430. */
  431. #define radix_tree_for_each_slot(slot, root, iter, start) \
  432. for (slot = radix_tree_iter_init(iter, start) ; \
  433. slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
  434. slot = radix_tree_next_slot(slot, iter, 0))
  435. /**
  436. * radix_tree_for_each_contig - iterate over contiguous slots
  437. *
  438. * @slot: the void** variable for pointer to slot
  439. * @root: the struct radix_tree_root pointer
  440. * @iter: the struct radix_tree_iter pointer
  441. * @start: iteration starting index
  442. *
  443. * @slot points to radix tree slot, @iter->index contains its index.
  444. */
  445. #define radix_tree_for_each_contig(slot, root, iter, start) \
  446. for (slot = radix_tree_iter_init(iter, start) ; \
  447. slot || (slot = radix_tree_next_chunk(root, iter, \
  448. RADIX_TREE_ITER_CONTIG)) ; \
  449. slot = radix_tree_next_slot(slot, iter, \
  450. RADIX_TREE_ITER_CONTIG))
  451. /**
  452. * radix_tree_for_each_tagged - iterate over tagged slots
  453. *
  454. * @slot: the void** variable for pointer to slot
  455. * @root: the struct radix_tree_root pointer
  456. * @iter: the struct radix_tree_iter pointer
  457. * @start: iteration starting index
  458. * @tag: tag index
  459. *
  460. * @slot points to radix tree slot, @iter->index contains its index.
  461. */
  462. #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
  463. for (slot = radix_tree_iter_init(iter, start) ; \
  464. slot || (slot = radix_tree_next_chunk(root, iter, \
  465. RADIX_TREE_ITER_TAGGED | tag)) ; \
  466. slot = radix_tree_next_slot(slot, iter, \
  467. RADIX_TREE_ITER_TAGGED))
  468. #endif /* _LINUX_RADIX_TREE_H */