radix-tree.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. /*
  2. * Copyright (C) 2001 Momchil Velikov
  3. * Portions Copyright (C) 2001 Christoph Hellwig
  4. * Copyright (C) 2005 SGI, Christoph Lameter
  5. * Copyright (C) 2006 Nick Piggin
  6. * Copyright (C) 2012 Konstantin Khlebnikov
  7. * Copyright (C) 2016 Intel, Matthew Wilcox
  8. * Copyright (C) 2016 Intel, Ross Zwisler
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/export.h>
  28. #include <linux/radix-tree.h>
  29. #include <linux/percpu.h>
  30. #include <linux/slab.h>
  31. #include <linux/kmemleak.h>
  32. #include <linux/notifier.h>
  33. #include <linux/cpu.h>
  34. #include <linux/string.h>
  35. #include <linux/bitops.h>
  36. #include <linux/rcupdate.h>
  37. #include <linux/preempt.h> /* in_interrupt() */
  38. /* Number of nodes in fully populated tree of given height */
  39. static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
  40. /*
  41. * Radix tree node cache.
  42. */
  43. static struct kmem_cache *radix_tree_node_cachep;
  44. /*
  45. * The radix tree is variable-height, so an insert operation not only has
  46. * to build the branch to its corresponding item, it also has to build the
  47. * branch to existing items if the size has to be increased (by
  48. * radix_tree_extend).
  49. *
  50. * The worst case is a zero height tree with just a single item at index 0,
  51. * and then inserting an item at index ULONG_MAX. This requires 2 new branches
  52. * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
  53. * Hence:
  54. */
  55. #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
  56. /*
  57. * Per-cpu pool of preloaded nodes
  58. */
  59. struct radix_tree_preload {
  60. unsigned nr;
  61. /* nodes->private_data points to next preallocated node */
  62. struct radix_tree_node *nodes;
  63. };
  64. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  65. static inline void *node_to_entry(void *ptr)
  66. {
  67. return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
  68. }
  69. #define RADIX_TREE_RETRY node_to_entry(NULL)
  70. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  71. /* Sibling slots point directly to another slot in the same node */
  72. static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
  73. {
  74. void **ptr = node;
  75. return (parent->slots <= ptr) &&
  76. (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
  77. }
  78. #else
  79. static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
  80. {
  81. return false;
  82. }
  83. #endif
  84. static inline unsigned long get_slot_offset(struct radix_tree_node *parent,
  85. void **slot)
  86. {
  87. return slot - parent->slots;
  88. }
  89. static unsigned int radix_tree_descend(struct radix_tree_node *parent,
  90. struct radix_tree_node **nodep, unsigned long index)
  91. {
  92. unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
  93. void **entry = rcu_dereference_raw(parent->slots[offset]);
  94. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  95. if (radix_tree_is_internal_node(entry)) {
  96. if (is_sibling_entry(parent, entry)) {
  97. void **sibentry = (void **) entry_to_node(entry);
  98. offset = get_slot_offset(parent, sibentry);
  99. entry = rcu_dereference_raw(*sibentry);
  100. }
  101. }
  102. #endif
  103. *nodep = (void *)entry;
  104. return offset;
  105. }
  106. static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
  107. {
  108. return root->gfp_mask & __GFP_BITS_MASK;
  109. }
  110. static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
  111. int offset)
  112. {
  113. __set_bit(offset, node->tags[tag]);
  114. }
  115. static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
  116. int offset)
  117. {
  118. __clear_bit(offset, node->tags[tag]);
  119. }
  120. static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
  121. int offset)
  122. {
  123. return test_bit(offset, node->tags[tag]);
  124. }
  125. static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
  126. {
  127. root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
  128. }
  129. static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
  130. {
  131. root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
  132. }
  133. static inline void root_tag_clear_all(struct radix_tree_root *root)
  134. {
  135. root->gfp_mask &= __GFP_BITS_MASK;
  136. }
  137. static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
  138. {
  139. return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
  140. }
  141. static inline unsigned root_tags_get(struct radix_tree_root *root)
  142. {
  143. return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
  144. }
  145. /*
  146. * Returns 1 if any slot in the node has this tag set.
  147. * Otherwise returns 0.
  148. */
  149. static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
  150. {
  151. unsigned idx;
  152. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  153. if (node->tags[tag][idx])
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. /**
  159. * radix_tree_find_next_bit - find the next set bit in a memory region
  160. *
  161. * @addr: The address to base the search on
  162. * @size: The bitmap size in bits
  163. * @offset: The bitnumber to start searching at
  164. *
  165. * Unrollable variant of find_next_bit() for constant size arrays.
  166. * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
  167. * Returns next bit offset, or size if nothing found.
  168. */
  169. static __always_inline unsigned long
  170. radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
  171. unsigned long offset)
  172. {
  173. const unsigned long *addr = node->tags[tag];
  174. if (offset < RADIX_TREE_MAP_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 < RADIX_TREE_MAP_SIZE) {
  182. tmp = *++addr;
  183. if (tmp)
  184. return __ffs(tmp) + offset;
  185. offset += BITS_PER_LONG;
  186. }
  187. }
  188. return RADIX_TREE_MAP_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 exceptional %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->exceptional, 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. INIT_LIST_HEAD(&node->private_list);
  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. } else {
  462. /* Moving an exceptional root->rnode to a node */
  463. if (radix_tree_exceptional_entry(slot))
  464. node->exceptional = 1;
  465. }
  466. node->slots[0] = slot;
  467. slot = node_to_entry(node);
  468. rcu_assign_pointer(root->rnode, slot);
  469. shift += RADIX_TREE_MAP_SHIFT;
  470. } while (shift <= maxshift);
  471. out:
  472. return maxshift + RADIX_TREE_MAP_SHIFT;
  473. }
  474. /**
  475. * radix_tree_shrink - shrink radix tree to minimum height
  476. * @root radix tree root
  477. */
  478. static inline void radix_tree_shrink(struct radix_tree_root *root,
  479. radix_tree_update_node_t update_node,
  480. void *private)
  481. {
  482. for (;;) {
  483. struct radix_tree_node *node = root->rnode;
  484. struct radix_tree_node *child;
  485. if (!radix_tree_is_internal_node(node))
  486. break;
  487. node = entry_to_node(node);
  488. /*
  489. * The candidate node has more than one child, or its child
  490. * is not at the leftmost slot, or the child is a multiorder
  491. * entry, we cannot shrink.
  492. */
  493. if (node->count != 1)
  494. break;
  495. child = node->slots[0];
  496. if (!child)
  497. break;
  498. if (!radix_tree_is_internal_node(child) && node->shift)
  499. break;
  500. if (radix_tree_is_internal_node(child))
  501. entry_to_node(child)->parent = NULL;
  502. /*
  503. * We don't need rcu_assign_pointer(), since we are simply
  504. * moving the node from one part of the tree to another: if it
  505. * was safe to dereference the old pointer to it
  506. * (node->slots[0]), it will be safe to dereference the new
  507. * one (root->rnode) as far as dependent read barriers go.
  508. */
  509. root->rnode = child;
  510. /*
  511. * We have a dilemma here. The node's slot[0] must not be
  512. * NULLed in case there are concurrent lookups expecting to
  513. * find the item. However if this was a bottom-level node,
  514. * then it may be subject to the slot pointer being visible
  515. * to callers dereferencing it. If item corresponding to
  516. * slot[0] is subsequently deleted, these callers would expect
  517. * their slot to become empty sooner or later.
  518. *
  519. * For example, lockless pagecache will look up a slot, deref
  520. * the page pointer, and if the page has 0 refcount it means it
  521. * was concurrently deleted from pagecache so try the deref
  522. * again. Fortunately there is already a requirement for logic
  523. * to retry the entire slot lookup -- the indirect pointer
  524. * problem (replacing direct root node with an indirect pointer
  525. * also results in a stale slot). So tag the slot as indirect
  526. * to force callers to retry.
  527. */
  528. node->count = 0;
  529. if (!radix_tree_is_internal_node(child)) {
  530. node->slots[0] = RADIX_TREE_RETRY;
  531. if (update_node)
  532. update_node(node, private);
  533. }
  534. radix_tree_node_free(node);
  535. }
  536. }
  537. static void delete_node(struct radix_tree_root *root,
  538. struct radix_tree_node *node,
  539. radix_tree_update_node_t update_node, void *private)
  540. {
  541. do {
  542. struct radix_tree_node *parent;
  543. if (node->count) {
  544. if (node == entry_to_node(root->rnode))
  545. radix_tree_shrink(root, update_node, private);
  546. return;
  547. }
  548. parent = node->parent;
  549. if (parent) {
  550. parent->slots[node->offset] = NULL;
  551. parent->count--;
  552. } else {
  553. root_tag_clear_all(root);
  554. root->rnode = NULL;
  555. }
  556. radix_tree_node_free(node);
  557. node = parent;
  558. } while (node);
  559. }
  560. /**
  561. * __radix_tree_create - create a slot in a radix tree
  562. * @root: radix tree root
  563. * @index: index key
  564. * @order: index occupies 2^order aligned slots
  565. * @nodep: returns node
  566. * @slotp: returns slot
  567. *
  568. * Create, if necessary, and return the node and slot for an item
  569. * at position @index in the radix tree @root.
  570. *
  571. * Until there is more than one item in the tree, no nodes are
  572. * allocated and @root->rnode is used as a direct slot instead of
  573. * pointing to a node, in which case *@nodep will be NULL.
  574. *
  575. * Returns -ENOMEM, or 0 for success.
  576. */
  577. int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
  578. unsigned order, struct radix_tree_node **nodep,
  579. void ***slotp)
  580. {
  581. struct radix_tree_node *node = NULL, *child;
  582. void **slot = (void **)&root->rnode;
  583. unsigned long maxindex;
  584. unsigned int shift, offset = 0;
  585. unsigned long max = index | ((1UL << order) - 1);
  586. shift = radix_tree_load_root(root, &child, &maxindex);
  587. /* Make sure the tree is high enough. */
  588. if (max > maxindex) {
  589. int error = radix_tree_extend(root, max, shift);
  590. if (error < 0)
  591. return error;
  592. shift = error;
  593. child = root->rnode;
  594. if (order == shift)
  595. shift += RADIX_TREE_MAP_SHIFT;
  596. }
  597. while (shift > order) {
  598. shift -= RADIX_TREE_MAP_SHIFT;
  599. if (child == NULL) {
  600. /* Have to add a child node. */
  601. child = radix_tree_node_alloc(root);
  602. if (!child)
  603. return -ENOMEM;
  604. child->shift = shift;
  605. child->offset = offset;
  606. child->parent = node;
  607. rcu_assign_pointer(*slot, node_to_entry(child));
  608. if (node)
  609. node->count++;
  610. } else if (!radix_tree_is_internal_node(child))
  611. break;
  612. /* Go a level down */
  613. node = entry_to_node(child);
  614. offset = radix_tree_descend(node, &child, index);
  615. slot = &node->slots[offset];
  616. }
  617. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  618. /* Insert pointers to the canonical entry */
  619. if (order > shift) {
  620. unsigned i, n = 1 << (order - shift);
  621. offset = offset & ~(n - 1);
  622. slot = &node->slots[offset];
  623. child = node_to_entry(slot);
  624. for (i = 0; i < n; i++) {
  625. if (slot[i])
  626. return -EEXIST;
  627. }
  628. for (i = 1; i < n; i++) {
  629. rcu_assign_pointer(slot[i], child);
  630. node->count++;
  631. }
  632. }
  633. #endif
  634. if (nodep)
  635. *nodep = node;
  636. if (slotp)
  637. *slotp = slot;
  638. return 0;
  639. }
  640. /**
  641. * __radix_tree_insert - insert into a radix tree
  642. * @root: radix tree root
  643. * @index: index key
  644. * @order: key covers the 2^order indices around index
  645. * @item: item to insert
  646. *
  647. * Insert an item into the radix tree at position @index.
  648. */
  649. int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
  650. unsigned order, void *item)
  651. {
  652. struct radix_tree_node *node;
  653. void **slot;
  654. int error;
  655. BUG_ON(radix_tree_is_internal_node(item));
  656. error = __radix_tree_create(root, index, order, &node, &slot);
  657. if (error)
  658. return error;
  659. if (*slot != NULL)
  660. return -EEXIST;
  661. rcu_assign_pointer(*slot, item);
  662. if (node) {
  663. unsigned offset = get_slot_offset(node, slot);
  664. node->count++;
  665. if (radix_tree_exceptional_entry(item))
  666. node->exceptional++;
  667. BUG_ON(tag_get(node, 0, offset));
  668. BUG_ON(tag_get(node, 1, offset));
  669. BUG_ON(tag_get(node, 2, offset));
  670. } else {
  671. BUG_ON(root_tags_get(root));
  672. }
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(__radix_tree_insert);
  676. /**
  677. * __radix_tree_lookup - lookup an item in a radix tree
  678. * @root: radix tree root
  679. * @index: index key
  680. * @nodep: returns node
  681. * @slotp: returns slot
  682. *
  683. * Lookup and return the item at position @index in the radix
  684. * tree @root.
  685. *
  686. * Until there is more than one item in the tree, no nodes are
  687. * allocated and @root->rnode is used as a direct slot instead of
  688. * pointing to a node, in which case *@nodep will be NULL.
  689. */
  690. void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
  691. struct radix_tree_node **nodep, void ***slotp)
  692. {
  693. struct radix_tree_node *node, *parent;
  694. unsigned long maxindex;
  695. void **slot;
  696. restart:
  697. parent = NULL;
  698. slot = (void **)&root->rnode;
  699. radix_tree_load_root(root, &node, &maxindex);
  700. if (index > maxindex)
  701. return NULL;
  702. while (radix_tree_is_internal_node(node)) {
  703. unsigned offset;
  704. if (node == RADIX_TREE_RETRY)
  705. goto restart;
  706. parent = entry_to_node(node);
  707. offset = radix_tree_descend(parent, &node, index);
  708. slot = parent->slots + offset;
  709. }
  710. if (nodep)
  711. *nodep = parent;
  712. if (slotp)
  713. *slotp = slot;
  714. return node;
  715. }
  716. /**
  717. * radix_tree_lookup_slot - lookup a slot in a radix tree
  718. * @root: radix tree root
  719. * @index: index key
  720. *
  721. * Returns: the slot corresponding to the position @index in the
  722. * radix tree @root. This is useful for update-if-exists operations.
  723. *
  724. * This function can be called under rcu_read_lock iff the slot is not
  725. * modified by radix_tree_replace_slot, otherwise it must be called
  726. * exclusive from other writers. Any dereference of the slot must be done
  727. * using radix_tree_deref_slot.
  728. */
  729. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  730. {
  731. void **slot;
  732. if (!__radix_tree_lookup(root, index, NULL, &slot))
  733. return NULL;
  734. return slot;
  735. }
  736. EXPORT_SYMBOL(radix_tree_lookup_slot);
  737. /**
  738. * radix_tree_lookup - perform lookup operation on a radix tree
  739. * @root: radix tree root
  740. * @index: index key
  741. *
  742. * Lookup the item at the position @index in the radix tree @root.
  743. *
  744. * This function can be called under rcu_read_lock, however the caller
  745. * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
  746. * them safely). No RCU barriers are required to access or modify the
  747. * returned item, however.
  748. */
  749. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  750. {
  751. return __radix_tree_lookup(root, index, NULL, NULL);
  752. }
  753. EXPORT_SYMBOL(radix_tree_lookup);
  754. static void replace_slot(struct radix_tree_root *root,
  755. struct radix_tree_node *node,
  756. void **slot, void *item,
  757. bool warn_typeswitch)
  758. {
  759. void *old = rcu_dereference_raw(*slot);
  760. int count, exceptional;
  761. WARN_ON_ONCE(radix_tree_is_internal_node(item));
  762. count = !!item - !!old;
  763. exceptional = !!radix_tree_exceptional_entry(item) -
  764. !!radix_tree_exceptional_entry(old);
  765. WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
  766. if (node) {
  767. node->count += count;
  768. node->exceptional += exceptional;
  769. }
  770. rcu_assign_pointer(*slot, item);
  771. }
  772. /**
  773. * __radix_tree_replace - replace item in a slot
  774. * @root: radix tree root
  775. * @node: pointer to tree node
  776. * @slot: pointer to slot in @node
  777. * @item: new item to store in the slot.
  778. * @update_node: callback for changing leaf nodes
  779. * @private: private data to pass to @update_node
  780. *
  781. * For use with __radix_tree_lookup(). Caller must hold tree write locked
  782. * across slot lookup and replacement.
  783. */
  784. void __radix_tree_replace(struct radix_tree_root *root,
  785. struct radix_tree_node *node,
  786. void **slot, void *item,
  787. radix_tree_update_node_t update_node, void *private)
  788. {
  789. /*
  790. * This function supports replacing exceptional entries and
  791. * deleting entries, but that needs accounting against the
  792. * node unless the slot is root->rnode.
  793. */
  794. replace_slot(root, node, slot, item,
  795. !node && slot != (void **)&root->rnode);
  796. if (!node)
  797. return;
  798. if (update_node)
  799. update_node(node, private);
  800. delete_node(root, node, update_node, private);
  801. }
  802. /**
  803. * radix_tree_replace_slot - replace item in a slot
  804. * @root: radix tree root
  805. * @slot: pointer to slot
  806. * @item: new item to store in the slot.
  807. *
  808. * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
  809. * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
  810. * across slot lookup and replacement.
  811. *
  812. * NOTE: This cannot be used to switch between non-entries (empty slots),
  813. * regular entries, and exceptional entries, as that requires accounting
  814. * inside the radix tree node. When switching from one type of entry or
  815. * deleting, use __radix_tree_lookup() and __radix_tree_replace().
  816. */
  817. void radix_tree_replace_slot(struct radix_tree_root *root,
  818. void **slot, void *item)
  819. {
  820. replace_slot(root, NULL, slot, item, true);
  821. }
  822. /**
  823. * radix_tree_tag_set - set a tag on a radix tree node
  824. * @root: radix tree root
  825. * @index: index key
  826. * @tag: tag index
  827. *
  828. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  829. * corresponding to @index in the radix tree. From
  830. * the root all the way down to the leaf node.
  831. *
  832. * Returns the address of the tagged item. Setting a tag on a not-present
  833. * item is a bug.
  834. */
  835. void *radix_tree_tag_set(struct radix_tree_root *root,
  836. unsigned long index, unsigned int tag)
  837. {
  838. struct radix_tree_node *node, *parent;
  839. unsigned long maxindex;
  840. radix_tree_load_root(root, &node, &maxindex);
  841. BUG_ON(index > maxindex);
  842. while (radix_tree_is_internal_node(node)) {
  843. unsigned offset;
  844. parent = entry_to_node(node);
  845. offset = radix_tree_descend(parent, &node, index);
  846. BUG_ON(!node);
  847. if (!tag_get(parent, tag, offset))
  848. tag_set(parent, tag, offset);
  849. }
  850. /* set the root's tag bit */
  851. if (!root_tag_get(root, tag))
  852. root_tag_set(root, tag);
  853. return node;
  854. }
  855. EXPORT_SYMBOL(radix_tree_tag_set);
  856. static void node_tag_clear(struct radix_tree_root *root,
  857. struct radix_tree_node *node,
  858. unsigned int tag, unsigned int offset)
  859. {
  860. while (node) {
  861. if (!tag_get(node, tag, offset))
  862. return;
  863. tag_clear(node, tag, offset);
  864. if (any_tag_set(node, tag))
  865. return;
  866. offset = node->offset;
  867. node = node->parent;
  868. }
  869. /* clear the root's tag bit */
  870. if (root_tag_get(root, tag))
  871. root_tag_clear(root, tag);
  872. }
  873. static void node_tag_set(struct radix_tree_root *root,
  874. struct radix_tree_node *node,
  875. unsigned int tag, unsigned int offset)
  876. {
  877. while (node) {
  878. if (tag_get(node, tag, offset))
  879. return;
  880. tag_set(node, tag, offset);
  881. offset = node->offset;
  882. node = node->parent;
  883. }
  884. if (!root_tag_get(root, tag))
  885. root_tag_set(root, tag);
  886. }
  887. /**
  888. * radix_tree_tag_clear - clear a tag on a radix tree node
  889. * @root: radix tree root
  890. * @index: index key
  891. * @tag: tag index
  892. *
  893. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  894. * corresponding to @index in the radix tree. If this causes
  895. * the leaf node to have no tags set then clear the tag in the
  896. * next-to-leaf node, etc.
  897. *
  898. * Returns the address of the tagged item on success, else NULL. ie:
  899. * has the same return value and semantics as radix_tree_lookup().
  900. */
  901. void *radix_tree_tag_clear(struct radix_tree_root *root,
  902. unsigned long index, unsigned int tag)
  903. {
  904. struct radix_tree_node *node, *parent;
  905. unsigned long maxindex;
  906. int uninitialized_var(offset);
  907. radix_tree_load_root(root, &node, &maxindex);
  908. if (index > maxindex)
  909. return NULL;
  910. parent = NULL;
  911. while (radix_tree_is_internal_node(node)) {
  912. parent = entry_to_node(node);
  913. offset = radix_tree_descend(parent, &node, index);
  914. }
  915. if (node)
  916. node_tag_clear(root, parent, tag, offset);
  917. return node;
  918. }
  919. EXPORT_SYMBOL(radix_tree_tag_clear);
  920. /**
  921. * radix_tree_tag_get - get a tag on a radix tree node
  922. * @root: radix tree root
  923. * @index: index key
  924. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  925. *
  926. * Return values:
  927. *
  928. * 0: tag not present or not set
  929. * 1: tag set
  930. *
  931. * Note that the return value of this function may not be relied on, even if
  932. * the RCU lock is held, unless tag modification and node deletion are excluded
  933. * from concurrency.
  934. */
  935. int radix_tree_tag_get(struct radix_tree_root *root,
  936. unsigned long index, unsigned int tag)
  937. {
  938. struct radix_tree_node *node, *parent;
  939. unsigned long maxindex;
  940. if (!root_tag_get(root, tag))
  941. return 0;
  942. radix_tree_load_root(root, &node, &maxindex);
  943. if (index > maxindex)
  944. return 0;
  945. if (node == NULL)
  946. return 0;
  947. while (radix_tree_is_internal_node(node)) {
  948. unsigned offset;
  949. parent = entry_to_node(node);
  950. offset = radix_tree_descend(parent, &node, index);
  951. if (!node)
  952. return 0;
  953. if (!tag_get(parent, tag, offset))
  954. return 0;
  955. if (node == RADIX_TREE_RETRY)
  956. break;
  957. }
  958. return 1;
  959. }
  960. EXPORT_SYMBOL(radix_tree_tag_get);
  961. static inline void __set_iter_shift(struct radix_tree_iter *iter,
  962. unsigned int shift)
  963. {
  964. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  965. iter->shift = shift;
  966. #endif
  967. }
  968. /**
  969. * radix_tree_next_chunk - find next chunk of slots for iteration
  970. *
  971. * @root: radix tree root
  972. * @iter: iterator state
  973. * @flags: RADIX_TREE_ITER_* flags and tag index
  974. * Returns: pointer to chunk first slot, or NULL if iteration is over
  975. */
  976. void **radix_tree_next_chunk(struct radix_tree_root *root,
  977. struct radix_tree_iter *iter, unsigned flags)
  978. {
  979. unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
  980. struct radix_tree_node *node, *child;
  981. unsigned long index, offset, maxindex;
  982. if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
  983. return NULL;
  984. /*
  985. * Catch next_index overflow after ~0UL. iter->index never overflows
  986. * during iterating; it can be zero only at the beginning.
  987. * And we cannot overflow iter->next_index in a single step,
  988. * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
  989. *
  990. * This condition also used by radix_tree_next_slot() to stop
  991. * contiguous iterating, and forbid switching to the next chunk.
  992. */
  993. index = iter->next_index;
  994. if (!index && iter->index)
  995. return NULL;
  996. restart:
  997. radix_tree_load_root(root, &child, &maxindex);
  998. if (index > maxindex)
  999. return NULL;
  1000. if (!child)
  1001. return NULL;
  1002. if (!radix_tree_is_internal_node(child)) {
  1003. /* Single-slot tree */
  1004. iter->index = index;
  1005. iter->next_index = maxindex + 1;
  1006. iter->tags = 1;
  1007. __set_iter_shift(iter, 0);
  1008. return (void **)&root->rnode;
  1009. }
  1010. do {
  1011. node = entry_to_node(child);
  1012. offset = radix_tree_descend(node, &child, index);
  1013. if ((flags & RADIX_TREE_ITER_TAGGED) ?
  1014. !tag_get(node, tag, offset) : !child) {
  1015. /* Hole detected */
  1016. if (flags & RADIX_TREE_ITER_CONTIG)
  1017. return NULL;
  1018. if (flags & RADIX_TREE_ITER_TAGGED)
  1019. offset = radix_tree_find_next_bit(node, tag,
  1020. offset + 1);
  1021. else
  1022. while (++offset < RADIX_TREE_MAP_SIZE) {
  1023. void *slot = node->slots[offset];
  1024. if (is_sibling_entry(node, slot))
  1025. continue;
  1026. if (slot)
  1027. break;
  1028. }
  1029. index &= ~node_maxindex(node);
  1030. index += offset << node->shift;
  1031. /* Overflow after ~0UL */
  1032. if (!index)
  1033. return NULL;
  1034. if (offset == RADIX_TREE_MAP_SIZE)
  1035. goto restart;
  1036. child = rcu_dereference_raw(node->slots[offset]);
  1037. }
  1038. if ((child == NULL) || (child == RADIX_TREE_RETRY))
  1039. goto restart;
  1040. } while (radix_tree_is_internal_node(child));
  1041. /* Update the iterator state */
  1042. iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
  1043. iter->next_index = (index | node_maxindex(node)) + 1;
  1044. __set_iter_shift(iter, node->shift);
  1045. /* Construct iter->tags bit-mask from node->tags[tag] array */
  1046. if (flags & RADIX_TREE_ITER_TAGGED) {
  1047. unsigned tag_long, tag_bit;
  1048. tag_long = offset / BITS_PER_LONG;
  1049. tag_bit = offset % BITS_PER_LONG;
  1050. iter->tags = node->tags[tag][tag_long] >> tag_bit;
  1051. /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
  1052. if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
  1053. /* Pick tags from next element */
  1054. if (tag_bit)
  1055. iter->tags |= node->tags[tag][tag_long + 1] <<
  1056. (BITS_PER_LONG - tag_bit);
  1057. /* Clip chunk size, here only BITS_PER_LONG tags */
  1058. iter->next_index = index + BITS_PER_LONG;
  1059. }
  1060. }
  1061. return node->slots + offset;
  1062. }
  1063. EXPORT_SYMBOL(radix_tree_next_chunk);
  1064. /**
  1065. * radix_tree_range_tag_if_tagged - for each item in given range set given
  1066. * tag if item has another tag set
  1067. * @root: radix tree root
  1068. * @first_indexp: pointer to a starting index of a range to scan
  1069. * @last_index: last index of a range to scan
  1070. * @nr_to_tag: maximum number items to tag
  1071. * @iftag: tag index to test
  1072. * @settag: tag index to set if tested tag is set
  1073. *
  1074. * This function scans range of radix tree from first_index to last_index
  1075. * (inclusive). For each item in the range if iftag is set, the function sets
  1076. * also settag. The function stops either after tagging nr_to_tag items or
  1077. * after reaching last_index.
  1078. *
  1079. * The tags must be set from the leaf level only and propagated back up the
  1080. * path to the root. We must do this so that we resolve the full path before
  1081. * setting any tags on intermediate nodes. If we set tags as we descend, then
  1082. * we can get to the leaf node and find that the index that has the iftag
  1083. * set is outside the range we are scanning. This reults in dangling tags and
  1084. * can lead to problems with later tag operations (e.g. livelocks on lookups).
  1085. *
  1086. * The function returns the number of leaves where the tag was set and sets
  1087. * *first_indexp to the first unscanned index.
  1088. * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
  1089. * be prepared to handle that.
  1090. */
  1091. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  1092. unsigned long *first_indexp, unsigned long last_index,
  1093. unsigned long nr_to_tag,
  1094. unsigned int iftag, unsigned int settag)
  1095. {
  1096. struct radix_tree_node *node, *child;
  1097. unsigned long maxindex;
  1098. unsigned long tagged = 0;
  1099. unsigned long index = *first_indexp;
  1100. radix_tree_load_root(root, &child, &maxindex);
  1101. last_index = min(last_index, maxindex);
  1102. if (index > last_index)
  1103. return 0;
  1104. if (!nr_to_tag)
  1105. return 0;
  1106. if (!root_tag_get(root, iftag)) {
  1107. *first_indexp = last_index + 1;
  1108. return 0;
  1109. }
  1110. if (!radix_tree_is_internal_node(child)) {
  1111. *first_indexp = last_index + 1;
  1112. root_tag_set(root, settag);
  1113. return 1;
  1114. }
  1115. node = entry_to_node(child);
  1116. for (;;) {
  1117. unsigned offset = radix_tree_descend(node, &child, index);
  1118. if (!child)
  1119. goto next;
  1120. if (!tag_get(node, iftag, offset))
  1121. goto next;
  1122. /* Sibling slots never have tags set on them */
  1123. if (radix_tree_is_internal_node(child)) {
  1124. node = entry_to_node(child);
  1125. continue;
  1126. }
  1127. tagged++;
  1128. node_tag_set(root, node, settag, offset);
  1129. next:
  1130. /* Go to next entry in node */
  1131. index = ((index >> node->shift) + 1) << node->shift;
  1132. /* Overflow can happen when last_index is ~0UL... */
  1133. if (index > last_index || !index)
  1134. break;
  1135. offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
  1136. while (offset == 0) {
  1137. /*
  1138. * We've fully scanned this node. Go up. Because
  1139. * last_index is guaranteed to be in the tree, what
  1140. * we do below cannot wander astray.
  1141. */
  1142. node = node->parent;
  1143. offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
  1144. }
  1145. if (is_sibling_entry(node, node->slots[offset]))
  1146. goto next;
  1147. if (tagged >= nr_to_tag)
  1148. break;
  1149. }
  1150. *first_indexp = index;
  1151. return tagged;
  1152. }
  1153. EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
  1154. /**
  1155. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  1156. * @root: radix tree root
  1157. * @results: where the results of the lookup are placed
  1158. * @first_index: start the lookup from this key
  1159. * @max_items: place up to this many items at *results
  1160. *
  1161. * Performs an index-ascending scan of the tree for present items. Places
  1162. * them at *@results and returns the number of items which were placed at
  1163. * *@results.
  1164. *
  1165. * The implementation is naive.
  1166. *
  1167. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  1168. * rcu_read_lock. In this case, rather than the returned results being
  1169. * an atomic snapshot of the tree at a single point in time, the
  1170. * semantics of an RCU protected gang lookup are as though multiple
  1171. * radix_tree_lookups have been issued in individual locks, and results
  1172. * stored in 'results'.
  1173. */
  1174. unsigned int
  1175. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  1176. unsigned long first_index, unsigned int max_items)
  1177. {
  1178. struct radix_tree_iter iter;
  1179. void **slot;
  1180. unsigned int ret = 0;
  1181. if (unlikely(!max_items))
  1182. return 0;
  1183. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1184. results[ret] = rcu_dereference_raw(*slot);
  1185. if (!results[ret])
  1186. continue;
  1187. if (radix_tree_is_internal_node(results[ret])) {
  1188. slot = radix_tree_iter_retry(&iter);
  1189. continue;
  1190. }
  1191. if (++ret == max_items)
  1192. break;
  1193. }
  1194. return ret;
  1195. }
  1196. EXPORT_SYMBOL(radix_tree_gang_lookup);
  1197. /**
  1198. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  1199. * @root: radix tree root
  1200. * @results: where the results of the lookup are placed
  1201. * @indices: where their indices should be placed (but usually NULL)
  1202. * @first_index: start the lookup from this key
  1203. * @max_items: place up to this many items at *results
  1204. *
  1205. * Performs an index-ascending scan of the tree for present items. Places
  1206. * their slots at *@results and returns the number of items which were
  1207. * placed at *@results.
  1208. *
  1209. * The implementation is naive.
  1210. *
  1211. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  1212. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  1213. * protection, radix_tree_deref_slot may fail requiring a retry.
  1214. */
  1215. unsigned int
  1216. radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  1217. void ***results, unsigned long *indices,
  1218. unsigned long first_index, unsigned int max_items)
  1219. {
  1220. struct radix_tree_iter iter;
  1221. void **slot;
  1222. unsigned int ret = 0;
  1223. if (unlikely(!max_items))
  1224. return 0;
  1225. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1226. results[ret] = slot;
  1227. if (indices)
  1228. indices[ret] = iter.index;
  1229. if (++ret == max_items)
  1230. break;
  1231. }
  1232. return ret;
  1233. }
  1234. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  1235. /**
  1236. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  1237. * based on a tag
  1238. * @root: radix tree root
  1239. * @results: where the results of the lookup are placed
  1240. * @first_index: start the lookup from this key
  1241. * @max_items: place up to this many items at *results
  1242. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1243. *
  1244. * Performs an index-ascending scan of the tree for present items which
  1245. * have the tag indexed by @tag set. Places the items at *@results and
  1246. * returns the number of items which were placed at *@results.
  1247. */
  1248. unsigned int
  1249. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  1250. unsigned long first_index, unsigned int max_items,
  1251. unsigned int tag)
  1252. {
  1253. struct radix_tree_iter iter;
  1254. void **slot;
  1255. unsigned int ret = 0;
  1256. if (unlikely(!max_items))
  1257. return 0;
  1258. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1259. results[ret] = rcu_dereference_raw(*slot);
  1260. if (!results[ret])
  1261. continue;
  1262. if (radix_tree_is_internal_node(results[ret])) {
  1263. slot = radix_tree_iter_retry(&iter);
  1264. continue;
  1265. }
  1266. if (++ret == max_items)
  1267. break;
  1268. }
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  1272. /**
  1273. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  1274. * radix tree based on a tag
  1275. * @root: radix tree root
  1276. * @results: where the results of the lookup are placed
  1277. * @first_index: start the lookup from this key
  1278. * @max_items: place up to this many items at *results
  1279. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1280. *
  1281. * Performs an index-ascending scan of the tree for present items which
  1282. * have the tag indexed by @tag set. Places the slots at *@results and
  1283. * returns the number of slots which were placed at *@results.
  1284. */
  1285. unsigned int
  1286. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  1287. unsigned long first_index, unsigned int max_items,
  1288. unsigned int tag)
  1289. {
  1290. struct radix_tree_iter iter;
  1291. void **slot;
  1292. unsigned int ret = 0;
  1293. if (unlikely(!max_items))
  1294. return 0;
  1295. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1296. results[ret] = slot;
  1297. if (++ret == max_items)
  1298. break;
  1299. }
  1300. return ret;
  1301. }
  1302. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  1303. #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
  1304. #include <linux/sched.h> /* for cond_resched() */
  1305. struct locate_info {
  1306. unsigned long found_index;
  1307. bool stop;
  1308. };
  1309. /*
  1310. * This linear search is at present only useful to shmem_unuse_inode().
  1311. */
  1312. static unsigned long __locate(struct radix_tree_node *slot, void *item,
  1313. unsigned long index, struct locate_info *info)
  1314. {
  1315. unsigned long i;
  1316. do {
  1317. unsigned int shift = slot->shift;
  1318. for (i = (index >> shift) & RADIX_TREE_MAP_MASK;
  1319. i < RADIX_TREE_MAP_SIZE;
  1320. i++, index += (1UL << shift)) {
  1321. struct radix_tree_node *node =
  1322. rcu_dereference_raw(slot->slots[i]);
  1323. if (node == RADIX_TREE_RETRY)
  1324. goto out;
  1325. if (!radix_tree_is_internal_node(node)) {
  1326. if (node == item) {
  1327. info->found_index = index;
  1328. info->stop = true;
  1329. goto out;
  1330. }
  1331. continue;
  1332. }
  1333. node = entry_to_node(node);
  1334. if (is_sibling_entry(slot, node))
  1335. continue;
  1336. slot = node;
  1337. break;
  1338. }
  1339. } while (i < RADIX_TREE_MAP_SIZE);
  1340. out:
  1341. if ((index == 0) && (i == RADIX_TREE_MAP_SIZE))
  1342. info->stop = true;
  1343. return index;
  1344. }
  1345. /**
  1346. * radix_tree_locate_item - search through radix tree for item
  1347. * @root: radix tree root
  1348. * @item: item to be found
  1349. *
  1350. * Returns index where item was found, or -1 if not found.
  1351. * Caller must hold no lock (since this time-consuming function needs
  1352. * to be preemptible), and must check afterwards if item is still there.
  1353. */
  1354. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1355. {
  1356. struct radix_tree_node *node;
  1357. unsigned long max_index;
  1358. unsigned long cur_index = 0;
  1359. struct locate_info info = {
  1360. .found_index = -1,
  1361. .stop = false,
  1362. };
  1363. do {
  1364. rcu_read_lock();
  1365. node = rcu_dereference_raw(root->rnode);
  1366. if (!radix_tree_is_internal_node(node)) {
  1367. rcu_read_unlock();
  1368. if (node == item)
  1369. info.found_index = 0;
  1370. break;
  1371. }
  1372. node = entry_to_node(node);
  1373. max_index = node_maxindex(node);
  1374. if (cur_index > max_index) {
  1375. rcu_read_unlock();
  1376. break;
  1377. }
  1378. cur_index = __locate(node, item, cur_index, &info);
  1379. rcu_read_unlock();
  1380. cond_resched();
  1381. } while (!info.stop && cur_index <= max_index);
  1382. return info.found_index;
  1383. }
  1384. #else
  1385. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1386. {
  1387. return -1;
  1388. }
  1389. #endif /* CONFIG_SHMEM && CONFIG_SWAP */
  1390. /**
  1391. * __radix_tree_delete_node - try to free node after clearing a slot
  1392. * @root: radix tree root
  1393. * @node: node containing @index
  1394. *
  1395. * After clearing the slot at @index in @node from radix tree
  1396. * rooted at @root, call this function to attempt freeing the
  1397. * node and shrinking the tree.
  1398. */
  1399. void __radix_tree_delete_node(struct radix_tree_root *root,
  1400. struct radix_tree_node *node)
  1401. {
  1402. delete_node(root, node, NULL, NULL);
  1403. }
  1404. static inline void delete_sibling_entries(struct radix_tree_node *node,
  1405. void *ptr, unsigned offset)
  1406. {
  1407. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  1408. int i;
  1409. for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
  1410. if (node->slots[offset + i] != ptr)
  1411. break;
  1412. node->slots[offset + i] = NULL;
  1413. node->count--;
  1414. }
  1415. #endif
  1416. }
  1417. /**
  1418. * radix_tree_delete_item - delete an item from a radix tree
  1419. * @root: radix tree root
  1420. * @index: index key
  1421. * @item: expected item
  1422. *
  1423. * Remove @item at @index from the radix tree rooted at @root.
  1424. *
  1425. * Returns the address of the deleted item, or NULL if it was not present
  1426. * or the entry at the given @index was not @item.
  1427. */
  1428. void *radix_tree_delete_item(struct radix_tree_root *root,
  1429. unsigned long index, void *item)
  1430. {
  1431. struct radix_tree_node *node;
  1432. unsigned int offset;
  1433. void **slot;
  1434. void *entry;
  1435. int tag;
  1436. entry = __radix_tree_lookup(root, index, &node, &slot);
  1437. if (!entry)
  1438. return NULL;
  1439. if (item && entry != item)
  1440. return NULL;
  1441. if (!node) {
  1442. root_tag_clear_all(root);
  1443. root->rnode = NULL;
  1444. return entry;
  1445. }
  1446. offset = get_slot_offset(node, slot);
  1447. /* Clear all tags associated with the item to be deleted. */
  1448. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
  1449. node_tag_clear(root, node, tag, offset);
  1450. delete_sibling_entries(node, node_to_entry(slot), offset);
  1451. __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
  1452. return entry;
  1453. }
  1454. EXPORT_SYMBOL(radix_tree_delete_item);
  1455. /**
  1456. * radix_tree_delete - delete an item from a radix tree
  1457. * @root: radix tree root
  1458. * @index: index key
  1459. *
  1460. * Remove the item at @index from the radix tree rooted at @root.
  1461. *
  1462. * Returns the address of the deleted item, or NULL if it was not present.
  1463. */
  1464. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  1465. {
  1466. return radix_tree_delete_item(root, index, NULL);
  1467. }
  1468. EXPORT_SYMBOL(radix_tree_delete);
  1469. void radix_tree_clear_tags(struct radix_tree_root *root,
  1470. struct radix_tree_node *node,
  1471. void **slot)
  1472. {
  1473. if (node) {
  1474. unsigned int tag, offset = get_slot_offset(node, slot);
  1475. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
  1476. node_tag_clear(root, node, tag, offset);
  1477. } else {
  1478. /* Clear root node tags */
  1479. root->gfp_mask &= __GFP_BITS_MASK;
  1480. }
  1481. }
  1482. /**
  1483. * radix_tree_tagged - test whether any items in the tree are tagged
  1484. * @root: radix tree root
  1485. * @tag: tag to test
  1486. */
  1487. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1488. {
  1489. return root_tag_get(root, tag);
  1490. }
  1491. EXPORT_SYMBOL(radix_tree_tagged);
  1492. static void
  1493. radix_tree_node_ctor(void *arg)
  1494. {
  1495. struct radix_tree_node *node = arg;
  1496. memset(node, 0, sizeof(*node));
  1497. INIT_LIST_HEAD(&node->private_list);
  1498. }
  1499. static __init unsigned long __maxindex(unsigned int height)
  1500. {
  1501. unsigned int width = height * RADIX_TREE_MAP_SHIFT;
  1502. int shift = RADIX_TREE_INDEX_BITS - width;
  1503. if (shift < 0)
  1504. return ~0UL;
  1505. if (shift >= BITS_PER_LONG)
  1506. return 0UL;
  1507. return ~0UL >> shift;
  1508. }
  1509. static __init void radix_tree_init_maxnodes(void)
  1510. {
  1511. unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
  1512. unsigned int i, j;
  1513. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  1514. height_to_maxindex[i] = __maxindex(i);
  1515. for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
  1516. for (j = i; j > 0; j--)
  1517. height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
  1518. }
  1519. }
  1520. static int radix_tree_cpu_dead(unsigned int cpu)
  1521. {
  1522. struct radix_tree_preload *rtp;
  1523. struct radix_tree_node *node;
  1524. /* Free per-cpu pool of preloaded nodes */
  1525. rtp = &per_cpu(radix_tree_preloads, cpu);
  1526. while (rtp->nr) {
  1527. node = rtp->nodes;
  1528. rtp->nodes = node->private_data;
  1529. kmem_cache_free(radix_tree_node_cachep, node);
  1530. rtp->nr--;
  1531. }
  1532. return 0;
  1533. }
  1534. void __init radix_tree_init(void)
  1535. {
  1536. int ret;
  1537. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1538. sizeof(struct radix_tree_node), 0,
  1539. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1540. radix_tree_node_ctor);
  1541. radix_tree_init_maxnodes();
  1542. ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
  1543. NULL, radix_tree_cpu_dead);
  1544. WARN_ON(ret < 0);
  1545. }