radix-tree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Copyright (C) 2001 Momchil Velikov
  3. * Portions Copyright (C) 2001 Christoph Hellwig
  4. * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/radix-tree.h>
  25. #include <linux/percpu.h>
  26. #include <linux/slab.h>
  27. #include <linux/notifier.h>
  28. #include <linux/cpu.h>
  29. #include <linux/gfp.h>
  30. #include <linux/string.h>
  31. #include <linux/bitops.h>
  32. #ifdef __KERNEL__
  33. #define RADIX_TREE_MAP_SHIFT 6
  34. #else
  35. #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
  36. #endif
  37. #define RADIX_TREE_TAGS 2
  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 count;
  44. void *slots[RADIX_TREE_MAP_SIZE];
  45. unsigned long tags[RADIX_TREE_TAGS][RADIX_TREE_TAG_LONGS];
  46. };
  47. struct radix_tree_path {
  48. struct radix_tree_node *node;
  49. int offset;
  50. };
  51. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  52. #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
  53. static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
  54. /*
  55. * Radix tree node cache.
  56. */
  57. static kmem_cache_t *radix_tree_node_cachep;
  58. /*
  59. * Per-cpu pool of preloaded nodes
  60. */
  61. struct radix_tree_preload {
  62. int nr;
  63. struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
  64. };
  65. DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  66. /*
  67. * This assumes that the caller has performed appropriate preallocation, and
  68. * that the caller has pinned this thread of control to the current CPU.
  69. */
  70. static struct radix_tree_node *
  71. radix_tree_node_alloc(struct radix_tree_root *root)
  72. {
  73. struct radix_tree_node *ret;
  74. ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
  75. if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
  76. struct radix_tree_preload *rtp;
  77. rtp = &__get_cpu_var(radix_tree_preloads);
  78. if (rtp->nr) {
  79. ret = rtp->nodes[rtp->nr - 1];
  80. rtp->nodes[rtp->nr - 1] = NULL;
  81. rtp->nr--;
  82. }
  83. }
  84. return ret;
  85. }
  86. static inline void
  87. radix_tree_node_free(struct radix_tree_node *node)
  88. {
  89. kmem_cache_free(radix_tree_node_cachep, node);
  90. }
  91. /*
  92. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  93. * ensure that the addition of a single element in the tree cannot fail. On
  94. * success, return zero, with preemption disabled. On error, return -ENOMEM
  95. * with preemption not disabled.
  96. */
  97. int radix_tree_preload(int gfp_mask)
  98. {
  99. struct radix_tree_preload *rtp;
  100. struct radix_tree_node *node;
  101. int ret = -ENOMEM;
  102. preempt_disable();
  103. rtp = &__get_cpu_var(radix_tree_preloads);
  104. while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
  105. preempt_enable();
  106. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  107. if (node == NULL)
  108. goto out;
  109. preempt_disable();
  110. rtp = &__get_cpu_var(radix_tree_preloads);
  111. if (rtp->nr < ARRAY_SIZE(rtp->nodes))
  112. rtp->nodes[rtp->nr++] = node;
  113. else
  114. kmem_cache_free(radix_tree_node_cachep, node);
  115. }
  116. ret = 0;
  117. out:
  118. return ret;
  119. }
  120. static inline void tag_set(struct radix_tree_node *node, int tag, int offset)
  121. {
  122. if (!test_bit(offset, &node->tags[tag][0]))
  123. __set_bit(offset, &node->tags[tag][0]);
  124. }
  125. static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
  126. {
  127. __clear_bit(offset, &node->tags[tag][0]);
  128. }
  129. static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
  130. {
  131. return test_bit(offset, &node->tags[tag][0]);
  132. }
  133. /*
  134. * Return the maximum key which can be store into a
  135. * radix tree with height HEIGHT.
  136. */
  137. static inline unsigned long radix_tree_maxindex(unsigned int height)
  138. {
  139. return height_to_maxindex[height];
  140. }
  141. /*
  142. * Extend a radix tree so it can store key @index.
  143. */
  144. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  145. {
  146. struct radix_tree_node *node;
  147. unsigned int height;
  148. char tags[RADIX_TREE_TAGS];
  149. int tag;
  150. /* Figure out what the height should be. */
  151. height = root->height + 1;
  152. while (index > radix_tree_maxindex(height))
  153. height++;
  154. if (root->rnode == NULL) {
  155. root->height = height;
  156. goto out;
  157. }
  158. /*
  159. * Prepare the tag status of the top-level node for propagation
  160. * into the newly-pushed top-level node(s)
  161. */
  162. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  163. int idx;
  164. tags[tag] = 0;
  165. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  166. if (root->rnode->tags[tag][idx]) {
  167. tags[tag] = 1;
  168. break;
  169. }
  170. }
  171. }
  172. do {
  173. if (!(node = radix_tree_node_alloc(root)))
  174. return -ENOMEM;
  175. /* Increase the height. */
  176. node->slots[0] = root->rnode;
  177. /* Propagate the aggregated tag info into the new root */
  178. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  179. if (tags[tag])
  180. tag_set(node, tag, 0);
  181. }
  182. node->count = 1;
  183. root->rnode = node;
  184. root->height++;
  185. } while (height > root->height);
  186. out:
  187. return 0;
  188. }
  189. /**
  190. * radix_tree_insert - insert into a radix tree
  191. * @root: radix tree root
  192. * @index: index key
  193. * @item: item to insert
  194. *
  195. * Insert an item into the radix tree at position @index.
  196. */
  197. int radix_tree_insert(struct radix_tree_root *root,
  198. unsigned long index, void *item)
  199. {
  200. struct radix_tree_node *node = NULL, *slot;
  201. unsigned int height, shift;
  202. int offset;
  203. int error;
  204. /* Make sure the tree is high enough. */
  205. if ((!index && !root->rnode) ||
  206. index > radix_tree_maxindex(root->height)) {
  207. error = radix_tree_extend(root, index);
  208. if (error)
  209. return error;
  210. }
  211. slot = root->rnode;
  212. height = root->height;
  213. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  214. offset = 0; /* uninitialised var warning */
  215. while (height > 0) {
  216. if (slot == NULL) {
  217. /* Have to add a child node. */
  218. if (!(slot = radix_tree_node_alloc(root)))
  219. return -ENOMEM;
  220. if (node) {
  221. node->slots[offset] = slot;
  222. node->count++;
  223. } else
  224. root->rnode = slot;
  225. }
  226. /* Go a level down */
  227. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  228. node = slot;
  229. slot = node->slots[offset];
  230. shift -= RADIX_TREE_MAP_SHIFT;
  231. height--;
  232. }
  233. if (slot != NULL)
  234. return -EEXIST;
  235. if (node) {
  236. node->count++;
  237. node->slots[offset] = item;
  238. BUG_ON(tag_get(node, 0, offset));
  239. BUG_ON(tag_get(node, 1, offset));
  240. } else
  241. root->rnode = item;
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(radix_tree_insert);
  245. /**
  246. * radix_tree_lookup - perform lookup operation on a radix tree
  247. * @root: radix tree root
  248. * @index: index key
  249. *
  250. * Lookup the item at the position @index in the radix tree @root.
  251. */
  252. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  253. {
  254. unsigned int height, shift;
  255. struct radix_tree_node *slot;
  256. height = root->height;
  257. if (index > radix_tree_maxindex(height))
  258. return NULL;
  259. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  260. slot = root->rnode;
  261. while (height > 0) {
  262. if (slot == NULL)
  263. return NULL;
  264. slot = slot->slots[(index >> shift) & RADIX_TREE_MAP_MASK];
  265. shift -= RADIX_TREE_MAP_SHIFT;
  266. height--;
  267. }
  268. return slot;
  269. }
  270. EXPORT_SYMBOL(radix_tree_lookup);
  271. /**
  272. * radix_tree_tag_set - set a tag on a radix tree node
  273. * @root: radix tree root
  274. * @index: index key
  275. * @tag: tag index
  276. *
  277. * Set the search tag corresponging to @index in the radix tree. From
  278. * the root all the way down to the leaf node.
  279. *
  280. * Returns the address of the tagged item. Setting a tag on a not-present
  281. * item is a bug.
  282. */
  283. void *radix_tree_tag_set(struct radix_tree_root *root,
  284. unsigned long index, int tag)
  285. {
  286. unsigned int height, shift;
  287. struct radix_tree_node *slot;
  288. height = root->height;
  289. if (index > radix_tree_maxindex(height))
  290. return NULL;
  291. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  292. slot = root->rnode;
  293. while (height > 0) {
  294. int offset;
  295. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  296. tag_set(slot, tag, offset);
  297. slot = slot->slots[offset];
  298. BUG_ON(slot == NULL);
  299. shift -= RADIX_TREE_MAP_SHIFT;
  300. height--;
  301. }
  302. return slot;
  303. }
  304. EXPORT_SYMBOL(radix_tree_tag_set);
  305. /**
  306. * radix_tree_tag_clear - clear a tag on a radix tree node
  307. * @root: radix tree root
  308. * @index: index key
  309. * @tag: tag index
  310. *
  311. * Clear the search tag corresponging to @index in the radix tree. If
  312. * this causes the leaf node to have no tags set then clear the tag in the
  313. * next-to-leaf node, etc.
  314. *
  315. * Returns the address of the tagged item on success, else NULL. ie:
  316. * has the same return value and semantics as radix_tree_lookup().
  317. */
  318. void *radix_tree_tag_clear(struct radix_tree_root *root,
  319. unsigned long index, int tag)
  320. {
  321. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  322. struct radix_tree_node *slot;
  323. unsigned int height, shift;
  324. void *ret = NULL;
  325. height = root->height;
  326. if (index > radix_tree_maxindex(height))
  327. goto out;
  328. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  329. pathp->node = NULL;
  330. slot = root->rnode;
  331. while (height > 0) {
  332. int offset;
  333. if (slot == NULL)
  334. goto out;
  335. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  336. pathp[1].offset = offset;
  337. pathp[1].node = slot;
  338. slot = slot->slots[offset];
  339. pathp++;
  340. shift -= RADIX_TREE_MAP_SHIFT;
  341. height--;
  342. }
  343. ret = slot;
  344. if (ret == NULL)
  345. goto out;
  346. do {
  347. int idx;
  348. tag_clear(pathp->node, tag, pathp->offset);
  349. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  350. if (pathp->node->tags[tag][idx])
  351. goto out;
  352. }
  353. pathp--;
  354. } while (pathp->node);
  355. out:
  356. return ret;
  357. }
  358. EXPORT_SYMBOL(radix_tree_tag_clear);
  359. #ifndef __KERNEL__ /* Only the test harness uses this at present */
  360. /**
  361. * radix_tree_tag_get - get a tag on a radix tree node
  362. * @root: radix tree root
  363. * @index: index key
  364. * @tag: tag index
  365. *
  366. * Return the search tag corresponging to @index in the radix tree.
  367. *
  368. * Returns zero if the tag is unset, or if there is no corresponding item
  369. * in the tree.
  370. */
  371. int radix_tree_tag_get(struct radix_tree_root *root,
  372. unsigned long index, int tag)
  373. {
  374. unsigned int height, shift;
  375. struct radix_tree_node *slot;
  376. int saw_unset_tag = 0;
  377. height = root->height;
  378. if (index > radix_tree_maxindex(height))
  379. return 0;
  380. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  381. slot = root->rnode;
  382. for ( ; ; ) {
  383. int offset;
  384. if (slot == NULL)
  385. return 0;
  386. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  387. /*
  388. * This is just a debug check. Later, we can bale as soon as
  389. * we see an unset tag.
  390. */
  391. if (!tag_get(slot, tag, offset))
  392. saw_unset_tag = 1;
  393. if (height == 1) {
  394. int ret = tag_get(slot, tag, offset);
  395. BUG_ON(ret && saw_unset_tag);
  396. return ret;
  397. }
  398. slot = slot->slots[offset];
  399. shift -= RADIX_TREE_MAP_SHIFT;
  400. height--;
  401. }
  402. }
  403. EXPORT_SYMBOL(radix_tree_tag_get);
  404. #endif
  405. static unsigned int
  406. __lookup(struct radix_tree_root *root, void **results, unsigned long index,
  407. unsigned int max_items, unsigned long *next_index)
  408. {
  409. unsigned int nr_found = 0;
  410. unsigned int shift, height;
  411. struct radix_tree_node *slot;
  412. unsigned long i;
  413. height = root->height;
  414. if (height == 0)
  415. goto out;
  416. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  417. slot = root->rnode;
  418. for ( ; height > 1; height--) {
  419. for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  420. i < RADIX_TREE_MAP_SIZE; i++) {
  421. if (slot->slots[i] != NULL)
  422. break;
  423. index &= ~((1UL << shift) - 1);
  424. index += 1UL << shift;
  425. if (index == 0)
  426. goto out; /* 32-bit wraparound */
  427. }
  428. if (i == RADIX_TREE_MAP_SIZE)
  429. goto out;
  430. shift -= RADIX_TREE_MAP_SHIFT;
  431. slot = slot->slots[i];
  432. }
  433. /* Bottom level: grab some items */
  434. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  435. index++;
  436. if (slot->slots[i]) {
  437. results[nr_found++] = slot->slots[i];
  438. if (nr_found == max_items)
  439. goto out;
  440. }
  441. }
  442. out:
  443. *next_index = index;
  444. return nr_found;
  445. }
  446. /**
  447. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  448. * @root: radix tree root
  449. * @results: where the results of the lookup are placed
  450. * @first_index: start the lookup from this key
  451. * @max_items: place up to this many items at *results
  452. *
  453. * Performs an index-ascending scan of the tree for present items. Places
  454. * them at *@results and returns the number of items which were placed at
  455. * *@results.
  456. *
  457. * The implementation is naive.
  458. */
  459. unsigned int
  460. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  461. unsigned long first_index, unsigned int max_items)
  462. {
  463. const unsigned long max_index = radix_tree_maxindex(root->height);
  464. unsigned long cur_index = first_index;
  465. unsigned int ret = 0;
  466. while (ret < max_items) {
  467. unsigned int nr_found;
  468. unsigned long next_index; /* Index of next search */
  469. if (cur_index > max_index)
  470. break;
  471. nr_found = __lookup(root, results + ret, cur_index,
  472. max_items - ret, &next_index);
  473. ret += nr_found;
  474. if (next_index == 0)
  475. break;
  476. cur_index = next_index;
  477. }
  478. return ret;
  479. }
  480. EXPORT_SYMBOL(radix_tree_gang_lookup);
  481. /*
  482. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  483. * open-coding the search.
  484. */
  485. static unsigned int
  486. __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
  487. unsigned int max_items, unsigned long *next_index, int tag)
  488. {
  489. unsigned int nr_found = 0;
  490. unsigned int shift;
  491. unsigned int height = root->height;
  492. struct radix_tree_node *slot;
  493. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  494. slot = root->rnode;
  495. while (height > 0) {
  496. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
  497. for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
  498. if (tag_get(slot, tag, i)) {
  499. BUG_ON(slot->slots[i] == NULL);
  500. break;
  501. }
  502. index &= ~((1UL << shift) - 1);
  503. index += 1UL << shift;
  504. if (index == 0)
  505. goto out; /* 32-bit wraparound */
  506. }
  507. if (i == RADIX_TREE_MAP_SIZE)
  508. goto out;
  509. height--;
  510. if (height == 0) { /* Bottom level: grab some items */
  511. unsigned long j = index & RADIX_TREE_MAP_MASK;
  512. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  513. index++;
  514. if (tag_get(slot, tag, j)) {
  515. BUG_ON(slot->slots[j] == NULL);
  516. results[nr_found++] = slot->slots[j];
  517. if (nr_found == max_items)
  518. goto out;
  519. }
  520. }
  521. }
  522. shift -= RADIX_TREE_MAP_SHIFT;
  523. slot = slot->slots[i];
  524. }
  525. out:
  526. *next_index = index;
  527. return nr_found;
  528. }
  529. /**
  530. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  531. * based on a tag
  532. * @root: radix tree root
  533. * @results: where the results of the lookup are placed
  534. * @first_index: start the lookup from this key
  535. * @max_items: place up to this many items at *results
  536. * @tag: the tag index
  537. *
  538. * Performs an index-ascending scan of the tree for present items which
  539. * have the tag indexed by @tag set. Places the items at *@results and
  540. * returns the number of items which were placed at *@results.
  541. */
  542. unsigned int
  543. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  544. unsigned long first_index, unsigned int max_items, int tag)
  545. {
  546. const unsigned long max_index = radix_tree_maxindex(root->height);
  547. unsigned long cur_index = first_index;
  548. unsigned int ret = 0;
  549. while (ret < max_items) {
  550. unsigned int nr_found;
  551. unsigned long next_index; /* Index of next search */
  552. if (cur_index > max_index)
  553. break;
  554. nr_found = __lookup_tag(root, results + ret, cur_index,
  555. max_items - ret, &next_index, tag);
  556. ret += nr_found;
  557. if (next_index == 0)
  558. break;
  559. cur_index = next_index;
  560. }
  561. return ret;
  562. }
  563. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  564. /**
  565. * radix_tree_delete - delete an item from a radix tree
  566. * @root: radix tree root
  567. * @index: index key
  568. *
  569. * Remove the item at @index from the radix tree rooted at @root.
  570. *
  571. * Returns the address of the deleted item, or NULL if it was not present.
  572. */
  573. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  574. {
  575. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  576. struct radix_tree_path *orig_pathp;
  577. struct radix_tree_node *slot;
  578. unsigned int height, shift;
  579. void *ret = NULL;
  580. char tags[RADIX_TREE_TAGS];
  581. int nr_cleared_tags;
  582. height = root->height;
  583. if (index > radix_tree_maxindex(height))
  584. goto out;
  585. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  586. pathp->node = NULL;
  587. slot = root->rnode;
  588. for ( ; height > 0; height--) {
  589. int offset;
  590. if (slot == NULL)
  591. goto out;
  592. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  593. pathp[1].offset = offset;
  594. pathp[1].node = slot;
  595. slot = slot->slots[offset];
  596. pathp++;
  597. shift -= RADIX_TREE_MAP_SHIFT;
  598. }
  599. ret = slot;
  600. if (ret == NULL)
  601. goto out;
  602. orig_pathp = pathp;
  603. /*
  604. * Clear all tags associated with the just-deleted item
  605. */
  606. memset(tags, 0, sizeof(tags));
  607. do {
  608. int tag;
  609. nr_cleared_tags = RADIX_TREE_TAGS;
  610. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  611. int idx;
  612. if (tags[tag])
  613. continue;
  614. tag_clear(pathp->node, tag, pathp->offset);
  615. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  616. if (pathp->node->tags[tag][idx]) {
  617. tags[tag] = 1;
  618. nr_cleared_tags--;
  619. break;
  620. }
  621. }
  622. }
  623. pathp--;
  624. } while (pathp->node && nr_cleared_tags);
  625. /* Now free the nodes we do not need anymore */
  626. for (pathp = orig_pathp; pathp->node; pathp--) {
  627. pathp->node->slots[pathp->offset] = NULL;
  628. if (--pathp->node->count)
  629. goto out;
  630. /* Node with zero slots in use so free it */
  631. radix_tree_node_free(pathp->node);
  632. }
  633. root->rnode = NULL;
  634. root->height = 0;
  635. out:
  636. return ret;
  637. }
  638. EXPORT_SYMBOL(radix_tree_delete);
  639. /**
  640. * radix_tree_tagged - test whether any items in the tree are tagged
  641. * @root: radix tree root
  642. * @tag: tag to test
  643. */
  644. int radix_tree_tagged(struct radix_tree_root *root, int tag)
  645. {
  646. int idx;
  647. if (!root->rnode)
  648. return 0;
  649. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  650. if (root->rnode->tags[tag][idx])
  651. return 1;
  652. }
  653. return 0;
  654. }
  655. EXPORT_SYMBOL(radix_tree_tagged);
  656. static void
  657. radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
  658. {
  659. memset(node, 0, sizeof(struct radix_tree_node));
  660. }
  661. static __init unsigned long __maxindex(unsigned int height)
  662. {
  663. unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
  664. unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
  665. if (tmp >= RADIX_TREE_INDEX_BITS)
  666. index = ~0UL;
  667. return index;
  668. }
  669. static __init void radix_tree_init_maxindex(void)
  670. {
  671. unsigned int i;
  672. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  673. height_to_maxindex[i] = __maxindex(i);
  674. }
  675. #ifdef CONFIG_HOTPLUG_CPU
  676. static int radix_tree_callback(struct notifier_block *nfb,
  677. unsigned long action,
  678. void *hcpu)
  679. {
  680. int cpu = (long)hcpu;
  681. struct radix_tree_preload *rtp;
  682. /* Free per-cpu pool of perloaded nodes */
  683. if (action == CPU_DEAD) {
  684. rtp = &per_cpu(radix_tree_preloads, cpu);
  685. while (rtp->nr) {
  686. kmem_cache_free(radix_tree_node_cachep,
  687. rtp->nodes[rtp->nr-1]);
  688. rtp->nodes[rtp->nr-1] = NULL;
  689. rtp->nr--;
  690. }
  691. }
  692. return NOTIFY_OK;
  693. }
  694. #endif /* CONFIG_HOTPLUG_CPU */
  695. void __init radix_tree_init(void)
  696. {
  697. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  698. sizeof(struct radix_tree_node), 0,
  699. SLAB_PANIC, radix_tree_node_ctor, NULL);
  700. radix_tree_init_maxindex();
  701. hotcpu_notifier(radix_tree_callback, 0);
  702. }