radix-tree.c 39 KB

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