radix-tree.c 39 KB

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