radix-tree.c 44 KB

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