radix-tree.h 18 KB

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