radix-tree.c 43 KB

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