radix-tree.c 35 KB

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