radix-tree.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. /*
  2. * Copyright (C) 2001 Momchil Velikov
  3. * Portions Copyright (C) 2001 Christoph Hellwig
  4. * Copyright (C) 2005 SGI, Christoph Lameter
  5. * Copyright (C) 2006 Nick Piggin
  6. * Copyright (C) 2012 Konstantin Khlebnikov
  7. * Copyright (C) 2016 Intel, Matthew Wilcox
  8. * Copyright (C) 2016 Intel, Ross Zwisler
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/export.h>
  28. #include <linux/radix-tree.h>
  29. #include <linux/percpu.h>
  30. #include <linux/slab.h>
  31. #include <linux/kmemleak.h>
  32. #include <linux/notifier.h>
  33. #include <linux/cpu.h>
  34. #include <linux/string.h>
  35. #include <linux/bitops.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/preempt.h> /* in_interrupt() */
  38. /* Number of nodes in fully populated tree of given height */
  39. static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
  40. /*
  41. * Radix tree node cache.
  42. */
  43. static struct kmem_cache *radix_tree_node_cachep;
  44. /*
  45. * The radix tree is variable-height, so an insert operation not only has
  46. * to build the branch to its corresponding item, it also has to build the
  47. * branch to existing items if the size has to be increased (by
  48. * radix_tree_extend).
  49. *
  50. * The worst case is a zero height tree with just a single item at index 0,
  51. * and then inserting an item at index ULONG_MAX. This requires 2 new branches
  52. * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
  53. * Hence:
  54. */
  55. #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
  56. /*
  57. * Per-cpu pool of preloaded nodes
  58. */
  59. struct radix_tree_preload {
  60. unsigned nr;
  61. /* nodes->private_data points to next preallocated node */
  62. struct radix_tree_node *nodes;
  63. };
  64. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  65. static inline struct radix_tree_node *entry_to_node(void *ptr)
  66. {
  67. return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
  68. }
  69. static inline void *node_to_entry(void *ptr)
  70. {
  71. return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
  72. }
  73. #define RADIX_TREE_RETRY node_to_entry(NULL)
  74. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  75. /* Sibling slots point directly to another slot in the same node */
  76. static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
  77. {
  78. void **ptr = node;
  79. return (parent->slots <= ptr) &&
  80. (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
  81. }
  82. #else
  83. static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
  84. {
  85. return false;
  86. }
  87. #endif
  88. static inline unsigned long get_slot_offset(struct radix_tree_node *parent,
  89. void **slot)
  90. {
  91. return slot - parent->slots;
  92. }
  93. static unsigned int radix_tree_descend(struct radix_tree_node *parent,
  94. struct radix_tree_node **nodep, unsigned long index)
  95. {
  96. unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
  97. void **entry = rcu_dereference_raw(parent->slots[offset]);
  98. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  99. if (radix_tree_is_internal_node(entry)) {
  100. if (is_sibling_entry(parent, entry)) {
  101. void **sibentry = (void **) entry_to_node(entry);
  102. offset = get_slot_offset(parent, sibentry);
  103. entry = rcu_dereference_raw(*sibentry);
  104. }
  105. }
  106. #endif
  107. *nodep = (void *)entry;
  108. return offset;
  109. }
  110. static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
  111. {
  112. return root->gfp_mask & __GFP_BITS_MASK;
  113. }
  114. static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
  115. int offset)
  116. {
  117. __set_bit(offset, node->tags[tag]);
  118. }
  119. static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
  120. int offset)
  121. {
  122. __clear_bit(offset, node->tags[tag]);
  123. }
  124. static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
  125. int offset)
  126. {
  127. return test_bit(offset, node->tags[tag]);
  128. }
  129. static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
  130. {
  131. root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
  132. }
  133. static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
  134. {
  135. root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
  136. }
  137. static inline void root_tag_clear_all(struct radix_tree_root *root)
  138. {
  139. root->gfp_mask &= __GFP_BITS_MASK;
  140. }
  141. static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
  142. {
  143. return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
  144. }
  145. static inline unsigned root_tags_get(struct radix_tree_root *root)
  146. {
  147. return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
  148. }
  149. /*
  150. * Returns 1 if any slot in the node has this tag set.
  151. * Otherwise returns 0.
  152. */
  153. static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
  154. {
  155. unsigned idx;
  156. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  157. if (node->tags[tag][idx])
  158. return 1;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * radix_tree_find_next_bit - find the next set bit in a memory region
  164. *
  165. * @addr: The address to base the search on
  166. * @size: The bitmap size in bits
  167. * @offset: The bitnumber to start searching at
  168. *
  169. * Unrollable variant of find_next_bit() for constant size arrays.
  170. * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
  171. * Returns next bit offset, or size if nothing found.
  172. */
  173. static __always_inline unsigned long
  174. radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
  175. unsigned long offset)
  176. {
  177. const unsigned long *addr = node->tags[tag];
  178. if (offset < RADIX_TREE_MAP_SIZE) {
  179. unsigned long tmp;
  180. addr += offset / BITS_PER_LONG;
  181. tmp = *addr >> (offset % BITS_PER_LONG);
  182. if (tmp)
  183. return __ffs(tmp) + offset;
  184. offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
  185. while (offset < RADIX_TREE_MAP_SIZE) {
  186. tmp = *++addr;
  187. if (tmp)
  188. return __ffs(tmp) + offset;
  189. offset += BITS_PER_LONG;
  190. }
  191. }
  192. return RADIX_TREE_MAP_SIZE;
  193. }
  194. static unsigned int iter_offset(const struct radix_tree_iter *iter)
  195. {
  196. return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
  197. }
  198. /*
  199. * The maximum index which can be stored in a radix tree
  200. */
  201. static inline unsigned long shift_maxindex(unsigned int shift)
  202. {
  203. return (RADIX_TREE_MAP_SIZE << shift) - 1;
  204. }
  205. static inline unsigned long node_maxindex(struct radix_tree_node *node)
  206. {
  207. return shift_maxindex(node->shift);
  208. }
  209. #ifndef __KERNEL__
  210. static void dump_node(struct radix_tree_node *node, unsigned long index)
  211. {
  212. unsigned long i;
  213. pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
  214. node, node->offset, index, index | node_maxindex(node),
  215. node->parent,
  216. node->tags[0][0], node->tags[1][0], node->tags[2][0],
  217. node->shift, node->count, node->exceptional);
  218. for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
  219. unsigned long first = index | (i << node->shift);
  220. unsigned long last = first | ((1UL << node->shift) - 1);
  221. void *entry = node->slots[i];
  222. if (!entry)
  223. continue;
  224. if (entry == RADIX_TREE_RETRY) {
  225. pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
  226. i, first, last, node);
  227. } else if (!radix_tree_is_internal_node(entry)) {
  228. pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
  229. entry, i, first, last, node);
  230. } else if (is_sibling_entry(node, entry)) {
  231. pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
  232. entry, i, first, last, node,
  233. *(void **)entry_to_node(entry));
  234. } else {
  235. dump_node(entry_to_node(entry), first);
  236. }
  237. }
  238. }
  239. /* For debug */
  240. static void radix_tree_dump(struct radix_tree_root *root)
  241. {
  242. pr_debug("radix root: %p rnode %p tags %x\n",
  243. root, root->rnode,
  244. root->gfp_mask >> __GFP_BITS_SHIFT);
  245. if (!radix_tree_is_internal_node(root->rnode))
  246. return;
  247. dump_node(entry_to_node(root->rnode), 0);
  248. }
  249. #endif
  250. /*
  251. * This assumes that the caller has performed appropriate preallocation, and
  252. * that the caller has pinned this thread of control to the current CPU.
  253. */
  254. static struct radix_tree_node *
  255. radix_tree_node_alloc(struct radix_tree_root *root)
  256. {
  257. struct radix_tree_node *ret = NULL;
  258. gfp_t gfp_mask = root_gfp_mask(root);
  259. /*
  260. * Preload code isn't irq safe and it doesn't make sense to use
  261. * preloading during an interrupt anyway as all the allocations have
  262. * to be atomic. So just do normal allocation when in interrupt.
  263. */
  264. if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
  265. struct radix_tree_preload *rtp;
  266. /*
  267. * Even if the caller has preloaded, try to allocate from the
  268. * cache first for the new node to get accounted to the memory
  269. * cgroup.
  270. */
  271. ret = kmem_cache_alloc(radix_tree_node_cachep,
  272. gfp_mask | __GFP_NOWARN);
  273. if (ret)
  274. goto out;
  275. /*
  276. * Provided the caller has preloaded here, we will always
  277. * succeed in getting a node here (and never reach
  278. * kmem_cache_alloc)
  279. */
  280. rtp = this_cpu_ptr(&radix_tree_preloads);
  281. if (rtp->nr) {
  282. ret = rtp->nodes;
  283. rtp->nodes = ret->private_data;
  284. ret->private_data = NULL;
  285. rtp->nr--;
  286. }
  287. /*
  288. * Update the allocation stack trace as this is more useful
  289. * for debugging.
  290. */
  291. kmemleak_update_trace(ret);
  292. goto out;
  293. }
  294. ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  295. out:
  296. BUG_ON(radix_tree_is_internal_node(ret));
  297. return ret;
  298. }
  299. static void radix_tree_node_rcu_free(struct rcu_head *head)
  300. {
  301. struct radix_tree_node *node =
  302. container_of(head, struct radix_tree_node, rcu_head);
  303. int i;
  304. /*
  305. * must only free zeroed nodes into the slab. radix_tree_shrink
  306. * can leave us with a non-NULL entry in the first slot, so clear
  307. * that here to make sure.
  308. */
  309. for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
  310. tag_clear(node, i, 0);
  311. node->slots[0] = NULL;
  312. INIT_LIST_HEAD(&node->private_list);
  313. kmem_cache_free(radix_tree_node_cachep, node);
  314. }
  315. static inline void
  316. radix_tree_node_free(struct radix_tree_node *node)
  317. {
  318. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  319. }
  320. /*
  321. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  322. * ensure that the addition of a single element in the tree cannot fail. On
  323. * success, return zero, with preemption disabled. On error, return -ENOMEM
  324. * with preemption not disabled.
  325. *
  326. * To make use of this facility, the radix tree must be initialised without
  327. * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
  328. */
  329. static int __radix_tree_preload(gfp_t gfp_mask, int nr)
  330. {
  331. struct radix_tree_preload *rtp;
  332. struct radix_tree_node *node;
  333. int ret = -ENOMEM;
  334. /*
  335. * Nodes preloaded by one cgroup can be be used by another cgroup, so
  336. * they should never be accounted to any particular memory cgroup.
  337. */
  338. gfp_mask &= ~__GFP_ACCOUNT;
  339. preempt_disable();
  340. rtp = this_cpu_ptr(&radix_tree_preloads);
  341. while (rtp->nr < nr) {
  342. preempt_enable();
  343. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  344. if (node == NULL)
  345. goto out;
  346. preempt_disable();
  347. rtp = this_cpu_ptr(&radix_tree_preloads);
  348. if (rtp->nr < nr) {
  349. node->private_data = rtp->nodes;
  350. rtp->nodes = node;
  351. rtp->nr++;
  352. } else {
  353. kmem_cache_free(radix_tree_node_cachep, node);
  354. }
  355. }
  356. ret = 0;
  357. out:
  358. return ret;
  359. }
  360. /*
  361. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  362. * ensure that the addition of a single element in the tree cannot fail. On
  363. * success, return zero, with preemption disabled. On error, return -ENOMEM
  364. * with preemption not disabled.
  365. *
  366. * To make use of this facility, the radix tree must be initialised without
  367. * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
  368. */
  369. int radix_tree_preload(gfp_t gfp_mask)
  370. {
  371. /* Warn on non-sensical use... */
  372. WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
  373. return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
  374. }
  375. EXPORT_SYMBOL(radix_tree_preload);
  376. /*
  377. * The same as above function, except we don't guarantee preloading happens.
  378. * We do it, if we decide it helps. On success, return zero with preemption
  379. * disabled. On error, return -ENOMEM with preemption not disabled.
  380. */
  381. int radix_tree_maybe_preload(gfp_t gfp_mask)
  382. {
  383. if (gfpflags_allow_blocking(gfp_mask))
  384. return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
  385. /* Preloading doesn't help anything with this gfp mask, skip it */
  386. preempt_disable();
  387. return 0;
  388. }
  389. EXPORT_SYMBOL(radix_tree_maybe_preload);
  390. /*
  391. * The same as function above, but preload number of nodes required to insert
  392. * (1 << order) continuous naturally-aligned elements.
  393. */
  394. int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
  395. {
  396. unsigned long nr_subtrees;
  397. int nr_nodes, subtree_height;
  398. /* Preloading doesn't help anything with this gfp mask, skip it */
  399. if (!gfpflags_allow_blocking(gfp_mask)) {
  400. preempt_disable();
  401. return 0;
  402. }
  403. /*
  404. * Calculate number and height of fully populated subtrees it takes to
  405. * store (1 << order) elements.
  406. */
  407. nr_subtrees = 1 << order;
  408. for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
  409. subtree_height++)
  410. nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
  411. /*
  412. * The worst case is zero height tree with a single item at index 0 and
  413. * then inserting items starting at ULONG_MAX - (1 << order).
  414. *
  415. * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
  416. * 0-index item.
  417. */
  418. nr_nodes = RADIX_TREE_MAX_PATH;
  419. /* Plus branch to fully populated subtrees. */
  420. nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
  421. /* Root node is shared. */
  422. nr_nodes--;
  423. /* Plus nodes required to build subtrees. */
  424. nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
  425. return __radix_tree_preload(gfp_mask, nr_nodes);
  426. }
  427. static unsigned radix_tree_load_root(struct radix_tree_root *root,
  428. struct radix_tree_node **nodep, unsigned long *maxindex)
  429. {
  430. struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
  431. *nodep = node;
  432. if (likely(radix_tree_is_internal_node(node))) {
  433. node = entry_to_node(node);
  434. *maxindex = node_maxindex(node);
  435. return node->shift + RADIX_TREE_MAP_SHIFT;
  436. }
  437. *maxindex = 0;
  438. return 0;
  439. }
  440. /*
  441. * Extend a radix tree so it can store key @index.
  442. */
  443. static int radix_tree_extend(struct radix_tree_root *root,
  444. unsigned long index, unsigned int shift)
  445. {
  446. struct radix_tree_node *slot;
  447. unsigned int maxshift;
  448. int tag;
  449. /* Figure out what the shift should be. */
  450. maxshift = shift;
  451. while (index > shift_maxindex(maxshift))
  452. maxshift += RADIX_TREE_MAP_SHIFT;
  453. slot = root->rnode;
  454. if (!slot)
  455. goto out;
  456. do {
  457. struct radix_tree_node *node = radix_tree_node_alloc(root);
  458. if (!node)
  459. return -ENOMEM;
  460. /* Propagate the aggregated tag info into the new root */
  461. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  462. if (root_tag_get(root, tag))
  463. tag_set(node, tag, 0);
  464. }
  465. BUG_ON(shift > BITS_PER_LONG);
  466. node->shift = shift;
  467. node->offset = 0;
  468. node->count = 1;
  469. node->parent = NULL;
  470. if (radix_tree_is_internal_node(slot)) {
  471. entry_to_node(slot)->parent = node;
  472. } else {
  473. /* Moving an exceptional root->rnode to a node */
  474. if (radix_tree_exceptional_entry(slot))
  475. node->exceptional = 1;
  476. }
  477. node->slots[0] = slot;
  478. slot = node_to_entry(node);
  479. rcu_assign_pointer(root->rnode, slot);
  480. shift += RADIX_TREE_MAP_SHIFT;
  481. } while (shift <= maxshift);
  482. out:
  483. return maxshift + RADIX_TREE_MAP_SHIFT;
  484. }
  485. /**
  486. * radix_tree_shrink - shrink radix tree to minimum height
  487. * @root radix tree root
  488. */
  489. static inline void radix_tree_shrink(struct radix_tree_root *root,
  490. radix_tree_update_node_t update_node,
  491. void *private)
  492. {
  493. for (;;) {
  494. struct radix_tree_node *node = root->rnode;
  495. struct radix_tree_node *child;
  496. if (!radix_tree_is_internal_node(node))
  497. break;
  498. node = entry_to_node(node);
  499. /*
  500. * The candidate node has more than one child, or its child
  501. * is not at the leftmost slot, or the child is a multiorder
  502. * entry, we cannot shrink.
  503. */
  504. if (node->count != 1)
  505. break;
  506. child = node->slots[0];
  507. if (!child)
  508. break;
  509. if (!radix_tree_is_internal_node(child) && node->shift)
  510. break;
  511. if (radix_tree_is_internal_node(child))
  512. entry_to_node(child)->parent = NULL;
  513. /*
  514. * We don't need rcu_assign_pointer(), since we are simply
  515. * moving the node from one part of the tree to another: if it
  516. * was safe to dereference the old pointer to it
  517. * (node->slots[0]), it will be safe to dereference the new
  518. * one (root->rnode) as far as dependent read barriers go.
  519. */
  520. root->rnode = child;
  521. /*
  522. * We have a dilemma here. The node's slot[0] must not be
  523. * NULLed in case there are concurrent lookups expecting to
  524. * find the item. However if this was a bottom-level node,
  525. * then it may be subject to the slot pointer being visible
  526. * to callers dereferencing it. If item corresponding to
  527. * slot[0] is subsequently deleted, these callers would expect
  528. * their slot to become empty sooner or later.
  529. *
  530. * For example, lockless pagecache will look up a slot, deref
  531. * the page pointer, and if the page has 0 refcount it means it
  532. * was concurrently deleted from pagecache so try the deref
  533. * again. Fortunately there is already a requirement for logic
  534. * to retry the entire slot lookup -- the indirect pointer
  535. * problem (replacing direct root node with an indirect pointer
  536. * also results in a stale slot). So tag the slot as indirect
  537. * to force callers to retry.
  538. */
  539. node->count = 0;
  540. if (!radix_tree_is_internal_node(child)) {
  541. node->slots[0] = RADIX_TREE_RETRY;
  542. if (update_node)
  543. update_node(node, private);
  544. }
  545. radix_tree_node_free(node);
  546. }
  547. }
  548. static void delete_node(struct radix_tree_root *root,
  549. struct radix_tree_node *node,
  550. radix_tree_update_node_t update_node, void *private)
  551. {
  552. do {
  553. struct radix_tree_node *parent;
  554. if (node->count) {
  555. if (node == entry_to_node(root->rnode))
  556. radix_tree_shrink(root, update_node, private);
  557. return;
  558. }
  559. parent = node->parent;
  560. if (parent) {
  561. parent->slots[node->offset] = NULL;
  562. parent->count--;
  563. } else {
  564. root_tag_clear_all(root);
  565. root->rnode = NULL;
  566. }
  567. radix_tree_node_free(node);
  568. node = parent;
  569. } while (node);
  570. }
  571. /**
  572. * __radix_tree_create - create a slot in a radix tree
  573. * @root: radix tree root
  574. * @index: index key
  575. * @order: index occupies 2^order aligned slots
  576. * @nodep: returns node
  577. * @slotp: returns slot
  578. *
  579. * Create, if necessary, and return the node and slot for an item
  580. * at position @index in the radix tree @root.
  581. *
  582. * Until there is more than one item in the tree, no nodes are
  583. * allocated and @root->rnode is used as a direct slot instead of
  584. * pointing to a node, in which case *@nodep will be NULL.
  585. *
  586. * Returns -ENOMEM, or 0 for success.
  587. */
  588. int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
  589. unsigned order, struct radix_tree_node **nodep,
  590. void ***slotp)
  591. {
  592. struct radix_tree_node *node = NULL, *child;
  593. void **slot = (void **)&root->rnode;
  594. unsigned long maxindex;
  595. unsigned int shift, offset = 0;
  596. unsigned long max = index | ((1UL << order) - 1);
  597. shift = radix_tree_load_root(root, &child, &maxindex);
  598. /* Make sure the tree is high enough. */
  599. if (max > maxindex) {
  600. int error = radix_tree_extend(root, max, shift);
  601. if (error < 0)
  602. return error;
  603. shift = error;
  604. child = root->rnode;
  605. if (order == shift)
  606. shift += RADIX_TREE_MAP_SHIFT;
  607. }
  608. while (shift > order) {
  609. shift -= RADIX_TREE_MAP_SHIFT;
  610. if (child == NULL) {
  611. /* Have to add a child node. */
  612. child = radix_tree_node_alloc(root);
  613. if (!child)
  614. return -ENOMEM;
  615. child->shift = shift;
  616. child->offset = offset;
  617. child->parent = node;
  618. rcu_assign_pointer(*slot, node_to_entry(child));
  619. if (node)
  620. node->count++;
  621. } else if (!radix_tree_is_internal_node(child))
  622. break;
  623. /* Go a level down */
  624. node = entry_to_node(child);
  625. offset = radix_tree_descend(node, &child, index);
  626. slot = &node->slots[offset];
  627. }
  628. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  629. /* Insert pointers to the canonical entry */
  630. if (order > shift) {
  631. unsigned i, n = 1 << (order - shift);
  632. offset = offset & ~(n - 1);
  633. slot = &node->slots[offset];
  634. child = node_to_entry(slot);
  635. for (i = 0; i < n; i++) {
  636. if (slot[i])
  637. return -EEXIST;
  638. }
  639. for (i = 1; i < n; i++) {
  640. rcu_assign_pointer(slot[i], child);
  641. node->count++;
  642. }
  643. }
  644. #endif
  645. if (nodep)
  646. *nodep = node;
  647. if (slotp)
  648. *slotp = slot;
  649. return 0;
  650. }
  651. /**
  652. * __radix_tree_insert - insert into a radix tree
  653. * @root: radix tree root
  654. * @index: index key
  655. * @order: key covers the 2^order indices around index
  656. * @item: item to insert
  657. *
  658. * Insert an item into the radix tree at position @index.
  659. */
  660. int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
  661. unsigned order, void *item)
  662. {
  663. struct radix_tree_node *node;
  664. void **slot;
  665. int error;
  666. BUG_ON(radix_tree_is_internal_node(item));
  667. error = __radix_tree_create(root, index, order, &node, &slot);
  668. if (error)
  669. return error;
  670. if (*slot != NULL)
  671. return -EEXIST;
  672. rcu_assign_pointer(*slot, item);
  673. if (node) {
  674. unsigned offset = get_slot_offset(node, slot);
  675. node->count++;
  676. if (radix_tree_exceptional_entry(item))
  677. node->exceptional++;
  678. BUG_ON(tag_get(node, 0, offset));
  679. BUG_ON(tag_get(node, 1, offset));
  680. BUG_ON(tag_get(node, 2, offset));
  681. } else {
  682. BUG_ON(root_tags_get(root));
  683. }
  684. return 0;
  685. }
  686. EXPORT_SYMBOL(__radix_tree_insert);
  687. /**
  688. * __radix_tree_lookup - lookup an item in a radix tree
  689. * @root: radix tree root
  690. * @index: index key
  691. * @nodep: returns node
  692. * @slotp: returns slot
  693. *
  694. * Lookup and return the item at position @index in the radix
  695. * tree @root.
  696. *
  697. * Until there is more than one item in the tree, no nodes are
  698. * allocated and @root->rnode is used as a direct slot instead of
  699. * pointing to a node, in which case *@nodep will be NULL.
  700. */
  701. void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
  702. struct radix_tree_node **nodep, void ***slotp)
  703. {
  704. struct radix_tree_node *node, *parent;
  705. unsigned long maxindex;
  706. void **slot;
  707. restart:
  708. parent = NULL;
  709. slot = (void **)&root->rnode;
  710. radix_tree_load_root(root, &node, &maxindex);
  711. if (index > maxindex)
  712. return NULL;
  713. while (radix_tree_is_internal_node(node)) {
  714. unsigned offset;
  715. if (node == RADIX_TREE_RETRY)
  716. goto restart;
  717. parent = entry_to_node(node);
  718. offset = radix_tree_descend(parent, &node, index);
  719. slot = parent->slots + offset;
  720. }
  721. if (nodep)
  722. *nodep = parent;
  723. if (slotp)
  724. *slotp = slot;
  725. return node;
  726. }
  727. /**
  728. * radix_tree_lookup_slot - lookup a slot in a radix tree
  729. * @root: radix tree root
  730. * @index: index key
  731. *
  732. * Returns: the slot corresponding to the position @index in the
  733. * radix tree @root. This is useful for update-if-exists operations.
  734. *
  735. * This function can be called under rcu_read_lock iff the slot is not
  736. * modified by radix_tree_replace_slot, otherwise it must be called
  737. * exclusive from other writers. Any dereference of the slot must be done
  738. * using radix_tree_deref_slot.
  739. */
  740. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  741. {
  742. void **slot;
  743. if (!__radix_tree_lookup(root, index, NULL, &slot))
  744. return NULL;
  745. return slot;
  746. }
  747. EXPORT_SYMBOL(radix_tree_lookup_slot);
  748. /**
  749. * radix_tree_lookup - perform lookup operation on a radix tree
  750. * @root: radix tree root
  751. * @index: index key
  752. *
  753. * Lookup the item at the position @index in the radix tree @root.
  754. *
  755. * This function can be called under rcu_read_lock, however the caller
  756. * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
  757. * them safely). No RCU barriers are required to access or modify the
  758. * returned item, however.
  759. */
  760. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  761. {
  762. return __radix_tree_lookup(root, index, NULL, NULL);
  763. }
  764. EXPORT_SYMBOL(radix_tree_lookup);
  765. static void replace_slot(struct radix_tree_root *root,
  766. struct radix_tree_node *node,
  767. void **slot, void *item,
  768. bool warn_typeswitch)
  769. {
  770. void *old = rcu_dereference_raw(*slot);
  771. int count, exceptional;
  772. WARN_ON_ONCE(radix_tree_is_internal_node(item));
  773. count = !!item - !!old;
  774. exceptional = !!radix_tree_exceptional_entry(item) -
  775. !!radix_tree_exceptional_entry(old);
  776. WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
  777. if (node) {
  778. node->count += count;
  779. node->exceptional += exceptional;
  780. }
  781. rcu_assign_pointer(*slot, item);
  782. }
  783. /**
  784. * __radix_tree_replace - replace item in a slot
  785. * @root: radix tree root
  786. * @node: pointer to tree node
  787. * @slot: pointer to slot in @node
  788. * @item: new item to store in the slot.
  789. * @update_node: callback for changing leaf nodes
  790. * @private: private data to pass to @update_node
  791. *
  792. * For use with __radix_tree_lookup(). Caller must hold tree write locked
  793. * across slot lookup and replacement.
  794. */
  795. void __radix_tree_replace(struct radix_tree_root *root,
  796. struct radix_tree_node *node,
  797. void **slot, void *item,
  798. radix_tree_update_node_t update_node, void *private)
  799. {
  800. /*
  801. * This function supports replacing exceptional entries and
  802. * deleting entries, but that needs accounting against the
  803. * node unless the slot is root->rnode.
  804. */
  805. replace_slot(root, node, slot, item,
  806. !node && slot != (void **)&root->rnode);
  807. if (!node)
  808. return;
  809. if (update_node)
  810. update_node(node, private);
  811. delete_node(root, node, update_node, private);
  812. }
  813. /**
  814. * radix_tree_replace_slot - replace item in a slot
  815. * @root: radix tree root
  816. * @slot: pointer to slot
  817. * @item: new item to store in the slot.
  818. *
  819. * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
  820. * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
  821. * across slot lookup and replacement.
  822. *
  823. * NOTE: This cannot be used to switch between non-entries (empty slots),
  824. * regular entries, and exceptional entries, as that requires accounting
  825. * inside the radix tree node. When switching from one type of entry or
  826. * deleting, use __radix_tree_lookup() and __radix_tree_replace().
  827. */
  828. void radix_tree_replace_slot(struct radix_tree_root *root,
  829. void **slot, void *item)
  830. {
  831. replace_slot(root, NULL, slot, item, true);
  832. }
  833. /**
  834. * radix_tree_tag_set - set a tag on a radix tree node
  835. * @root: radix tree root
  836. * @index: index key
  837. * @tag: tag index
  838. *
  839. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  840. * corresponding to @index in the radix tree. From
  841. * the root all the way down to the leaf node.
  842. *
  843. * Returns the address of the tagged item. Setting a tag on a not-present
  844. * item is a bug.
  845. */
  846. void *radix_tree_tag_set(struct radix_tree_root *root,
  847. unsigned long index, unsigned int tag)
  848. {
  849. struct radix_tree_node *node, *parent;
  850. unsigned long maxindex;
  851. radix_tree_load_root(root, &node, &maxindex);
  852. BUG_ON(index > maxindex);
  853. while (radix_tree_is_internal_node(node)) {
  854. unsigned offset;
  855. parent = entry_to_node(node);
  856. offset = radix_tree_descend(parent, &node, index);
  857. BUG_ON(!node);
  858. if (!tag_get(parent, tag, offset))
  859. tag_set(parent, tag, offset);
  860. }
  861. /* set the root's tag bit */
  862. if (!root_tag_get(root, tag))
  863. root_tag_set(root, tag);
  864. return node;
  865. }
  866. EXPORT_SYMBOL(radix_tree_tag_set);
  867. static void node_tag_clear(struct radix_tree_root *root,
  868. struct radix_tree_node *node,
  869. unsigned int tag, unsigned int offset)
  870. {
  871. while (node) {
  872. if (!tag_get(node, tag, offset))
  873. return;
  874. tag_clear(node, tag, offset);
  875. if (any_tag_set(node, tag))
  876. return;
  877. offset = node->offset;
  878. node = node->parent;
  879. }
  880. /* clear the root's tag bit */
  881. if (root_tag_get(root, tag))
  882. root_tag_clear(root, tag);
  883. }
  884. static void node_tag_set(struct radix_tree_root *root,
  885. struct radix_tree_node *node,
  886. unsigned int tag, unsigned int offset)
  887. {
  888. while (node) {
  889. if (tag_get(node, tag, offset))
  890. return;
  891. tag_set(node, tag, offset);
  892. offset = node->offset;
  893. node = node->parent;
  894. }
  895. if (!root_tag_get(root, tag))
  896. root_tag_set(root, tag);
  897. }
  898. /**
  899. * radix_tree_iter_tag_set - set a tag on the current iterator entry
  900. * @root: radix tree root
  901. * @iter: iterator state
  902. * @tag: tag to set
  903. */
  904. void radix_tree_iter_tag_set(struct radix_tree_root *root,
  905. const struct radix_tree_iter *iter, unsigned int tag)
  906. {
  907. node_tag_set(root, iter->node, tag, iter_offset(iter));
  908. }
  909. /**
  910. * radix_tree_tag_clear - clear a tag on a radix tree node
  911. * @root: radix tree root
  912. * @index: index key
  913. * @tag: tag index
  914. *
  915. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  916. * corresponding to @index in the radix tree. If this causes
  917. * the leaf node to have no tags set then clear the tag in the
  918. * next-to-leaf node, etc.
  919. *
  920. * Returns the address of the tagged item on success, else NULL. ie:
  921. * has the same return value and semantics as radix_tree_lookup().
  922. */
  923. void *radix_tree_tag_clear(struct radix_tree_root *root,
  924. unsigned long index, unsigned int tag)
  925. {
  926. struct radix_tree_node *node, *parent;
  927. unsigned long maxindex;
  928. int uninitialized_var(offset);
  929. radix_tree_load_root(root, &node, &maxindex);
  930. if (index > maxindex)
  931. return NULL;
  932. parent = NULL;
  933. while (radix_tree_is_internal_node(node)) {
  934. parent = entry_to_node(node);
  935. offset = radix_tree_descend(parent, &node, index);
  936. }
  937. if (node)
  938. node_tag_clear(root, parent, tag, offset);
  939. return node;
  940. }
  941. EXPORT_SYMBOL(radix_tree_tag_clear);
  942. /**
  943. * radix_tree_tag_get - get a tag on a radix tree node
  944. * @root: radix tree root
  945. * @index: index key
  946. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  947. *
  948. * Return values:
  949. *
  950. * 0: tag not present or not set
  951. * 1: tag set
  952. *
  953. * Note that the return value of this function may not be relied on, even if
  954. * the RCU lock is held, unless tag modification and node deletion are excluded
  955. * from concurrency.
  956. */
  957. int radix_tree_tag_get(struct radix_tree_root *root,
  958. unsigned long index, unsigned int tag)
  959. {
  960. struct radix_tree_node *node, *parent;
  961. unsigned long maxindex;
  962. if (!root_tag_get(root, tag))
  963. return 0;
  964. radix_tree_load_root(root, &node, &maxindex);
  965. if (index > maxindex)
  966. return 0;
  967. if (node == NULL)
  968. return 0;
  969. while (radix_tree_is_internal_node(node)) {
  970. unsigned offset;
  971. parent = entry_to_node(node);
  972. offset = radix_tree_descend(parent, &node, index);
  973. if (!node)
  974. return 0;
  975. if (!tag_get(parent, tag, offset))
  976. return 0;
  977. if (node == RADIX_TREE_RETRY)
  978. break;
  979. }
  980. return 1;
  981. }
  982. EXPORT_SYMBOL(radix_tree_tag_get);
  983. static inline void __set_iter_shift(struct radix_tree_iter *iter,
  984. unsigned int shift)
  985. {
  986. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  987. iter->shift = shift;
  988. #endif
  989. }
  990. /* Construct iter->tags bit-mask from node->tags[tag] array */
  991. static void set_iter_tags(struct radix_tree_iter *iter,
  992. struct radix_tree_node *node, unsigned offset,
  993. unsigned tag)
  994. {
  995. unsigned tag_long = offset / BITS_PER_LONG;
  996. unsigned tag_bit = offset % BITS_PER_LONG;
  997. iter->tags = node->tags[tag][tag_long] >> tag_bit;
  998. /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
  999. if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
  1000. /* Pick tags from next element */
  1001. if (tag_bit)
  1002. iter->tags |= node->tags[tag][tag_long + 1] <<
  1003. (BITS_PER_LONG - tag_bit);
  1004. /* Clip chunk size, here only BITS_PER_LONG tags */
  1005. iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
  1006. }
  1007. }
  1008. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  1009. static void **skip_siblings(struct radix_tree_node **nodep,
  1010. void **slot, struct radix_tree_iter *iter)
  1011. {
  1012. void *sib = node_to_entry(slot - 1);
  1013. while (iter->index < iter->next_index) {
  1014. *nodep = rcu_dereference_raw(*slot);
  1015. if (*nodep && *nodep != sib)
  1016. return slot;
  1017. slot++;
  1018. iter->index = __radix_tree_iter_add(iter, 1);
  1019. iter->tags >>= 1;
  1020. }
  1021. *nodep = NULL;
  1022. return NULL;
  1023. }
  1024. void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
  1025. unsigned flags)
  1026. {
  1027. unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
  1028. struct radix_tree_node *node = rcu_dereference_raw(*slot);
  1029. slot = skip_siblings(&node, slot, iter);
  1030. while (radix_tree_is_internal_node(node)) {
  1031. unsigned offset;
  1032. unsigned long next_index;
  1033. if (node == RADIX_TREE_RETRY)
  1034. return slot;
  1035. node = entry_to_node(node);
  1036. iter->node = node;
  1037. iter->shift = node->shift;
  1038. if (flags & RADIX_TREE_ITER_TAGGED) {
  1039. offset = radix_tree_find_next_bit(node, tag, 0);
  1040. if (offset == RADIX_TREE_MAP_SIZE)
  1041. return NULL;
  1042. slot = &node->slots[offset];
  1043. iter->index = __radix_tree_iter_add(iter, offset);
  1044. set_iter_tags(iter, node, offset, tag);
  1045. node = rcu_dereference_raw(*slot);
  1046. } else {
  1047. offset = 0;
  1048. slot = &node->slots[0];
  1049. for (;;) {
  1050. node = rcu_dereference_raw(*slot);
  1051. if (node)
  1052. break;
  1053. slot++;
  1054. offset++;
  1055. if (offset == RADIX_TREE_MAP_SIZE)
  1056. return NULL;
  1057. }
  1058. iter->index = __radix_tree_iter_add(iter, offset);
  1059. }
  1060. if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
  1061. goto none;
  1062. next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
  1063. if (next_index < iter->next_index)
  1064. iter->next_index = next_index;
  1065. }
  1066. return slot;
  1067. none:
  1068. iter->next_index = 0;
  1069. return NULL;
  1070. }
  1071. EXPORT_SYMBOL(__radix_tree_next_slot);
  1072. #else
  1073. static void **skip_siblings(struct radix_tree_node **nodep,
  1074. void **slot, struct radix_tree_iter *iter)
  1075. {
  1076. return slot;
  1077. }
  1078. #endif
  1079. void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
  1080. {
  1081. struct radix_tree_node *node;
  1082. slot++;
  1083. iter->index = __radix_tree_iter_add(iter, 1);
  1084. node = rcu_dereference_raw(*slot);
  1085. skip_siblings(&node, slot, iter);
  1086. iter->next_index = iter->index;
  1087. iter->tags = 0;
  1088. return NULL;
  1089. }
  1090. EXPORT_SYMBOL(radix_tree_iter_resume);
  1091. /**
  1092. * radix_tree_next_chunk - find next chunk of slots for iteration
  1093. *
  1094. * @root: radix tree root
  1095. * @iter: iterator state
  1096. * @flags: RADIX_TREE_ITER_* flags and tag index
  1097. * Returns: pointer to chunk first slot, or NULL if iteration is over
  1098. */
  1099. void **radix_tree_next_chunk(struct radix_tree_root *root,
  1100. struct radix_tree_iter *iter, unsigned flags)
  1101. {
  1102. unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
  1103. struct radix_tree_node *node, *child;
  1104. unsigned long index, offset, maxindex;
  1105. if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
  1106. return NULL;
  1107. /*
  1108. * Catch next_index overflow after ~0UL. iter->index never overflows
  1109. * during iterating; it can be zero only at the beginning.
  1110. * And we cannot overflow iter->next_index in a single step,
  1111. * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
  1112. *
  1113. * This condition also used by radix_tree_next_slot() to stop
  1114. * contiguous iterating, and forbid switching to the next chunk.
  1115. */
  1116. index = iter->next_index;
  1117. if (!index && iter->index)
  1118. return NULL;
  1119. restart:
  1120. radix_tree_load_root(root, &child, &maxindex);
  1121. if (index > maxindex)
  1122. return NULL;
  1123. if (!child)
  1124. return NULL;
  1125. if (!radix_tree_is_internal_node(child)) {
  1126. /* Single-slot tree */
  1127. iter->index = index;
  1128. iter->next_index = maxindex + 1;
  1129. iter->tags = 1;
  1130. iter->node = NULL;
  1131. __set_iter_shift(iter, 0);
  1132. return (void **)&root->rnode;
  1133. }
  1134. do {
  1135. node = entry_to_node(child);
  1136. offset = radix_tree_descend(node, &child, index);
  1137. if ((flags & RADIX_TREE_ITER_TAGGED) ?
  1138. !tag_get(node, tag, offset) : !child) {
  1139. /* Hole detected */
  1140. if (flags & RADIX_TREE_ITER_CONTIG)
  1141. return NULL;
  1142. if (flags & RADIX_TREE_ITER_TAGGED)
  1143. offset = radix_tree_find_next_bit(node, tag,
  1144. offset + 1);
  1145. else
  1146. while (++offset < RADIX_TREE_MAP_SIZE) {
  1147. void *slot = node->slots[offset];
  1148. if (is_sibling_entry(node, slot))
  1149. continue;
  1150. if (slot)
  1151. break;
  1152. }
  1153. index &= ~node_maxindex(node);
  1154. index += offset << node->shift;
  1155. /* Overflow after ~0UL */
  1156. if (!index)
  1157. return NULL;
  1158. if (offset == RADIX_TREE_MAP_SIZE)
  1159. goto restart;
  1160. child = rcu_dereference_raw(node->slots[offset]);
  1161. }
  1162. if ((child == NULL) || (child == RADIX_TREE_RETRY))
  1163. goto restart;
  1164. } while (radix_tree_is_internal_node(child));
  1165. /* Update the iterator state */
  1166. iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
  1167. iter->next_index = (index | node_maxindex(node)) + 1;
  1168. iter->node = node;
  1169. __set_iter_shift(iter, node->shift);
  1170. if (flags & RADIX_TREE_ITER_TAGGED)
  1171. set_iter_tags(iter, node, offset, tag);
  1172. return node->slots + offset;
  1173. }
  1174. EXPORT_SYMBOL(radix_tree_next_chunk);
  1175. /**
  1176. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  1177. * @root: radix tree root
  1178. * @results: where the results of the lookup are placed
  1179. * @first_index: start the lookup from this key
  1180. * @max_items: place up to this many items at *results
  1181. *
  1182. * Performs an index-ascending scan of the tree for present items. Places
  1183. * them at *@results and returns the number of items which were placed at
  1184. * *@results.
  1185. *
  1186. * The implementation is naive.
  1187. *
  1188. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  1189. * rcu_read_lock. In this case, rather than the returned results being
  1190. * an atomic snapshot of the tree at a single point in time, the
  1191. * semantics of an RCU protected gang lookup are as though multiple
  1192. * radix_tree_lookups have been issued in individual locks, and results
  1193. * stored in 'results'.
  1194. */
  1195. unsigned int
  1196. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  1197. unsigned long first_index, unsigned int max_items)
  1198. {
  1199. struct radix_tree_iter iter;
  1200. void **slot;
  1201. unsigned int ret = 0;
  1202. if (unlikely(!max_items))
  1203. return 0;
  1204. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1205. results[ret] = rcu_dereference_raw(*slot);
  1206. if (!results[ret])
  1207. continue;
  1208. if (radix_tree_is_internal_node(results[ret])) {
  1209. slot = radix_tree_iter_retry(&iter);
  1210. continue;
  1211. }
  1212. if (++ret == max_items)
  1213. break;
  1214. }
  1215. return ret;
  1216. }
  1217. EXPORT_SYMBOL(radix_tree_gang_lookup);
  1218. /**
  1219. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  1220. * @root: radix tree root
  1221. * @results: where the results of the lookup are placed
  1222. * @indices: where their indices should be placed (but usually NULL)
  1223. * @first_index: start the lookup from this key
  1224. * @max_items: place up to this many items at *results
  1225. *
  1226. * Performs an index-ascending scan of the tree for present items. Places
  1227. * their slots at *@results and returns the number of items which were
  1228. * placed at *@results.
  1229. *
  1230. * The implementation is naive.
  1231. *
  1232. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  1233. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  1234. * protection, radix_tree_deref_slot may fail requiring a retry.
  1235. */
  1236. unsigned int
  1237. radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  1238. void ***results, unsigned long *indices,
  1239. unsigned long first_index, unsigned int max_items)
  1240. {
  1241. struct radix_tree_iter iter;
  1242. void **slot;
  1243. unsigned int ret = 0;
  1244. if (unlikely(!max_items))
  1245. return 0;
  1246. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1247. results[ret] = slot;
  1248. if (indices)
  1249. indices[ret] = iter.index;
  1250. if (++ret == max_items)
  1251. break;
  1252. }
  1253. return ret;
  1254. }
  1255. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  1256. /**
  1257. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  1258. * based on a tag
  1259. * @root: radix tree root
  1260. * @results: where the results of the lookup are placed
  1261. * @first_index: start the lookup from this key
  1262. * @max_items: place up to this many items at *results
  1263. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1264. *
  1265. * Performs an index-ascending scan of the tree for present items which
  1266. * have the tag indexed by @tag set. Places the items at *@results and
  1267. * returns the number of items which were placed at *@results.
  1268. */
  1269. unsigned int
  1270. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  1271. unsigned long first_index, unsigned int max_items,
  1272. unsigned int tag)
  1273. {
  1274. struct radix_tree_iter iter;
  1275. void **slot;
  1276. unsigned int ret = 0;
  1277. if (unlikely(!max_items))
  1278. return 0;
  1279. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1280. results[ret] = rcu_dereference_raw(*slot);
  1281. if (!results[ret])
  1282. continue;
  1283. if (radix_tree_is_internal_node(results[ret])) {
  1284. slot = radix_tree_iter_retry(&iter);
  1285. continue;
  1286. }
  1287. if (++ret == max_items)
  1288. break;
  1289. }
  1290. return ret;
  1291. }
  1292. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  1293. /**
  1294. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  1295. * radix tree based on a tag
  1296. * @root: radix tree root
  1297. * @results: where the results of the lookup are placed
  1298. * @first_index: start the lookup from this key
  1299. * @max_items: place up to this many items at *results
  1300. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1301. *
  1302. * Performs an index-ascending scan of the tree for present items which
  1303. * have the tag indexed by @tag set. Places the slots at *@results and
  1304. * returns the number of slots which were placed at *@results.
  1305. */
  1306. unsigned int
  1307. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  1308. unsigned long first_index, unsigned int max_items,
  1309. unsigned int tag)
  1310. {
  1311. struct radix_tree_iter iter;
  1312. void **slot;
  1313. unsigned int ret = 0;
  1314. if (unlikely(!max_items))
  1315. return 0;
  1316. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1317. results[ret] = slot;
  1318. if (++ret == max_items)
  1319. break;
  1320. }
  1321. return ret;
  1322. }
  1323. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  1324. /**
  1325. * __radix_tree_delete_node - try to free node after clearing a slot
  1326. * @root: radix tree root
  1327. * @node: node containing @index
  1328. *
  1329. * After clearing the slot at @index in @node from radix tree
  1330. * rooted at @root, call this function to attempt freeing the
  1331. * node and shrinking the tree.
  1332. */
  1333. void __radix_tree_delete_node(struct radix_tree_root *root,
  1334. struct radix_tree_node *node)
  1335. {
  1336. delete_node(root, node, NULL, NULL);
  1337. }
  1338. static inline void delete_sibling_entries(struct radix_tree_node *node,
  1339. void *ptr, unsigned offset)
  1340. {
  1341. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  1342. int i;
  1343. for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
  1344. if (node->slots[offset + i] != ptr)
  1345. break;
  1346. node->slots[offset + i] = NULL;
  1347. node->count--;
  1348. }
  1349. #endif
  1350. }
  1351. /**
  1352. * radix_tree_delete_item - delete an item from a radix tree
  1353. * @root: radix tree root
  1354. * @index: index key
  1355. * @item: expected item
  1356. *
  1357. * Remove @item at @index from the radix tree rooted at @root.
  1358. *
  1359. * Returns the address of the deleted item, or NULL if it was not present
  1360. * or the entry at the given @index was not @item.
  1361. */
  1362. void *radix_tree_delete_item(struct radix_tree_root *root,
  1363. unsigned long index, void *item)
  1364. {
  1365. struct radix_tree_node *node;
  1366. unsigned int offset;
  1367. void **slot;
  1368. void *entry;
  1369. int tag;
  1370. entry = __radix_tree_lookup(root, index, &node, &slot);
  1371. if (!entry)
  1372. return NULL;
  1373. if (item && entry != item)
  1374. return NULL;
  1375. if (!node) {
  1376. root_tag_clear_all(root);
  1377. root->rnode = NULL;
  1378. return entry;
  1379. }
  1380. offset = get_slot_offset(node, slot);
  1381. /* Clear all tags associated with the item to be deleted. */
  1382. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
  1383. node_tag_clear(root, node, tag, offset);
  1384. delete_sibling_entries(node, node_to_entry(slot), offset);
  1385. __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
  1386. return entry;
  1387. }
  1388. EXPORT_SYMBOL(radix_tree_delete_item);
  1389. /**
  1390. * radix_tree_delete - delete an item from a radix tree
  1391. * @root: radix tree root
  1392. * @index: index key
  1393. *
  1394. * Remove the item at @index from the radix tree rooted at @root.
  1395. *
  1396. * Returns the address of the deleted item, or NULL if it was not present.
  1397. */
  1398. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  1399. {
  1400. return radix_tree_delete_item(root, index, NULL);
  1401. }
  1402. EXPORT_SYMBOL(radix_tree_delete);
  1403. void radix_tree_clear_tags(struct radix_tree_root *root,
  1404. struct radix_tree_node *node,
  1405. void **slot)
  1406. {
  1407. if (node) {
  1408. unsigned int tag, offset = get_slot_offset(node, slot);
  1409. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
  1410. node_tag_clear(root, node, tag, offset);
  1411. } else {
  1412. /* Clear root node tags */
  1413. root->gfp_mask &= __GFP_BITS_MASK;
  1414. }
  1415. }
  1416. /**
  1417. * radix_tree_tagged - test whether any items in the tree are tagged
  1418. * @root: radix tree root
  1419. * @tag: tag to test
  1420. */
  1421. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1422. {
  1423. return root_tag_get(root, tag);
  1424. }
  1425. EXPORT_SYMBOL(radix_tree_tagged);
  1426. static void
  1427. radix_tree_node_ctor(void *arg)
  1428. {
  1429. struct radix_tree_node *node = arg;
  1430. memset(node, 0, sizeof(*node));
  1431. INIT_LIST_HEAD(&node->private_list);
  1432. }
  1433. static __init unsigned long __maxindex(unsigned int height)
  1434. {
  1435. unsigned int width = height * RADIX_TREE_MAP_SHIFT;
  1436. int shift = RADIX_TREE_INDEX_BITS - width;
  1437. if (shift < 0)
  1438. return ~0UL;
  1439. if (shift >= BITS_PER_LONG)
  1440. return 0UL;
  1441. return ~0UL >> shift;
  1442. }
  1443. static __init void radix_tree_init_maxnodes(void)
  1444. {
  1445. unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
  1446. unsigned int i, j;
  1447. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  1448. height_to_maxindex[i] = __maxindex(i);
  1449. for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
  1450. for (j = i; j > 0; j--)
  1451. height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
  1452. }
  1453. }
  1454. static int radix_tree_cpu_dead(unsigned int cpu)
  1455. {
  1456. struct radix_tree_preload *rtp;
  1457. struct radix_tree_node *node;
  1458. /* Free per-cpu pool of preloaded nodes */
  1459. rtp = &per_cpu(radix_tree_preloads, cpu);
  1460. while (rtp->nr) {
  1461. node = rtp->nodes;
  1462. rtp->nodes = node->private_data;
  1463. kmem_cache_free(radix_tree_node_cachep, node);
  1464. rtp->nr--;
  1465. }
  1466. return 0;
  1467. }
  1468. void __init radix_tree_init(void)
  1469. {
  1470. int ret;
  1471. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1472. sizeof(struct radix_tree_node), 0,
  1473. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1474. radix_tree_node_ctor);
  1475. radix_tree_init_maxnodes();
  1476. ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
  1477. NULL, radix_tree_cpu_dead);
  1478. WARN_ON(ret < 0);
  1479. }