radix-tree.c 37 KB

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