radix-tree.c 35 KB

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