radix-tree.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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(gfp_t 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. * Returns 1 if any slot in the node has this tag set.
  135. * Otherwise returns 0.
  136. */
  137. static inline int any_tag_set(struct radix_tree_node *node, int tag)
  138. {
  139. int idx;
  140. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  141. if (node->tags[tag][idx])
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. /*
  147. * Return the maximum key which can be store into a
  148. * radix tree with height HEIGHT.
  149. */
  150. static inline unsigned long radix_tree_maxindex(unsigned int height)
  151. {
  152. return height_to_maxindex[height];
  153. }
  154. /*
  155. * Extend a radix tree so it can store key @index.
  156. */
  157. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  158. {
  159. struct radix_tree_node *node;
  160. unsigned int height;
  161. char tags[RADIX_TREE_TAGS];
  162. int tag;
  163. /* Figure out what the height should be. */
  164. height = root->height + 1;
  165. while (index > radix_tree_maxindex(height))
  166. height++;
  167. if (root->rnode == NULL) {
  168. root->height = height;
  169. goto out;
  170. }
  171. /*
  172. * Prepare the tag status of the top-level node for propagation
  173. * into the newly-pushed top-level node(s)
  174. */
  175. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  176. tags[tag] = 0;
  177. if (any_tag_set(root->rnode, tag))
  178. tags[tag] = 1;
  179. }
  180. do {
  181. if (!(node = radix_tree_node_alloc(root)))
  182. return -ENOMEM;
  183. /* Increase the height. */
  184. node->slots[0] = root->rnode;
  185. /* Propagate the aggregated tag info into the new root */
  186. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  187. if (tags[tag])
  188. tag_set(node, tag, 0);
  189. }
  190. node->count = 1;
  191. root->rnode = node;
  192. root->height++;
  193. } while (height > root->height);
  194. out:
  195. return 0;
  196. }
  197. /**
  198. * radix_tree_insert - insert into a radix tree
  199. * @root: radix tree root
  200. * @index: index key
  201. * @item: item to insert
  202. *
  203. * Insert an item into the radix tree at position @index.
  204. */
  205. int radix_tree_insert(struct radix_tree_root *root,
  206. unsigned long index, void *item)
  207. {
  208. struct radix_tree_node *node = NULL, *slot;
  209. unsigned int height, shift;
  210. int offset;
  211. int error;
  212. /* Make sure the tree is high enough. */
  213. if ((!index && !root->rnode) ||
  214. index > radix_tree_maxindex(root->height)) {
  215. error = radix_tree_extend(root, index);
  216. if (error)
  217. return error;
  218. }
  219. slot = root->rnode;
  220. height = root->height;
  221. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  222. offset = 0; /* uninitialised var warning */
  223. while (height > 0) {
  224. if (slot == NULL) {
  225. /* Have to add a child node. */
  226. if (!(slot = radix_tree_node_alloc(root)))
  227. return -ENOMEM;
  228. if (node) {
  229. node->slots[offset] = slot;
  230. node->count++;
  231. } else
  232. root->rnode = slot;
  233. }
  234. /* Go a level down */
  235. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  236. node = slot;
  237. slot = node->slots[offset];
  238. shift -= RADIX_TREE_MAP_SHIFT;
  239. height--;
  240. }
  241. if (slot != NULL)
  242. return -EEXIST;
  243. if (node) {
  244. node->count++;
  245. node->slots[offset] = item;
  246. BUG_ON(tag_get(node, 0, offset));
  247. BUG_ON(tag_get(node, 1, offset));
  248. } else
  249. root->rnode = item;
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(radix_tree_insert);
  253. static inline void **__lookup_slot(struct radix_tree_root *root,
  254. unsigned long index)
  255. {
  256. unsigned int height, shift;
  257. struct radix_tree_node **slot;
  258. height = root->height;
  259. if (index > radix_tree_maxindex(height))
  260. return NULL;
  261. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  262. slot = &root->rnode;
  263. while (height > 0) {
  264. if (*slot == NULL)
  265. return NULL;
  266. slot = (struct radix_tree_node **)
  267. ((*slot)->slots +
  268. ((index >> shift) & RADIX_TREE_MAP_MASK));
  269. shift -= RADIX_TREE_MAP_SHIFT;
  270. height--;
  271. }
  272. return (void **)slot;
  273. }
  274. /**
  275. * radix_tree_lookup_slot - lookup a slot in a radix tree
  276. * @root: radix tree root
  277. * @index: index key
  278. *
  279. * Lookup the slot corresponding to the position @index in the radix tree
  280. * @root. This is useful for update-if-exists operations.
  281. */
  282. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  283. {
  284. return __lookup_slot(root, index);
  285. }
  286. EXPORT_SYMBOL(radix_tree_lookup_slot);
  287. /**
  288. * radix_tree_lookup - perform lookup operation on a radix tree
  289. * @root: radix tree root
  290. * @index: index key
  291. *
  292. * Lookup the item at the position @index in the radix tree @root.
  293. */
  294. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  295. {
  296. void **slot;
  297. slot = __lookup_slot(root, index);
  298. return slot != NULL ? *slot : NULL;
  299. }
  300. EXPORT_SYMBOL(radix_tree_lookup);
  301. /**
  302. * radix_tree_tag_set - set a tag on a radix tree node
  303. * @root: radix tree root
  304. * @index: index key
  305. * @tag: tag index
  306. *
  307. * Set the search tag corresponging to @index in the radix tree. From
  308. * the root all the way down to the leaf node.
  309. *
  310. * Returns the address of the tagged item. Setting a tag on a not-present
  311. * item is a bug.
  312. */
  313. void *radix_tree_tag_set(struct radix_tree_root *root,
  314. unsigned long index, int tag)
  315. {
  316. unsigned int height, shift;
  317. struct radix_tree_node *slot;
  318. height = root->height;
  319. if (index > radix_tree_maxindex(height))
  320. return NULL;
  321. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  322. slot = root->rnode;
  323. while (height > 0) {
  324. int offset;
  325. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  326. tag_set(slot, tag, offset);
  327. slot = slot->slots[offset];
  328. BUG_ON(slot == NULL);
  329. shift -= RADIX_TREE_MAP_SHIFT;
  330. height--;
  331. }
  332. return slot;
  333. }
  334. EXPORT_SYMBOL(radix_tree_tag_set);
  335. /**
  336. * radix_tree_tag_clear - clear a tag on a radix tree node
  337. * @root: radix tree root
  338. * @index: index key
  339. * @tag: tag index
  340. *
  341. * Clear the search tag corresponging to @index in the radix tree. If
  342. * this causes the leaf node to have no tags set then clear the tag in the
  343. * next-to-leaf node, etc.
  344. *
  345. * Returns the address of the tagged item on success, else NULL. ie:
  346. * has the same return value and semantics as radix_tree_lookup().
  347. */
  348. void *radix_tree_tag_clear(struct radix_tree_root *root,
  349. unsigned long index, int tag)
  350. {
  351. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  352. struct radix_tree_node *slot;
  353. unsigned int height, shift;
  354. void *ret = NULL;
  355. height = root->height;
  356. if (index > radix_tree_maxindex(height))
  357. goto out;
  358. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  359. pathp->node = NULL;
  360. slot = root->rnode;
  361. while (height > 0) {
  362. int offset;
  363. if (slot == NULL)
  364. goto out;
  365. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  366. pathp[1].offset = offset;
  367. pathp[1].node = slot;
  368. slot = slot->slots[offset];
  369. pathp++;
  370. shift -= RADIX_TREE_MAP_SHIFT;
  371. height--;
  372. }
  373. ret = slot;
  374. if (ret == NULL)
  375. goto out;
  376. do {
  377. tag_clear(pathp->node, tag, pathp->offset);
  378. if (any_tag_set(pathp->node, tag))
  379. goto out;
  380. pathp--;
  381. } while (pathp->node);
  382. out:
  383. return ret;
  384. }
  385. EXPORT_SYMBOL(radix_tree_tag_clear);
  386. #ifndef __KERNEL__ /* Only the test harness uses this at present */
  387. /**
  388. * radix_tree_tag_get - get a tag on a radix tree node
  389. * @root: radix tree root
  390. * @index: index key
  391. * @tag: tag index
  392. *
  393. * Return values:
  394. *
  395. * 0: tag not present
  396. * 1: tag present, set
  397. * -1: tag present, unset
  398. */
  399. int radix_tree_tag_get(struct radix_tree_root *root,
  400. unsigned long index, int tag)
  401. {
  402. unsigned int height, shift;
  403. struct radix_tree_node *slot;
  404. int saw_unset_tag = 0;
  405. height = root->height;
  406. if (index > radix_tree_maxindex(height))
  407. return 0;
  408. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  409. slot = root->rnode;
  410. for ( ; ; ) {
  411. int offset;
  412. if (slot == NULL)
  413. return 0;
  414. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  415. /*
  416. * This is just a debug check. Later, we can bale as soon as
  417. * we see an unset tag.
  418. */
  419. if (!tag_get(slot, tag, offset))
  420. saw_unset_tag = 1;
  421. if (height == 1) {
  422. int ret = tag_get(slot, tag, offset);
  423. BUG_ON(ret && saw_unset_tag);
  424. return ret ? 1 : -1;
  425. }
  426. slot = slot->slots[offset];
  427. shift -= RADIX_TREE_MAP_SHIFT;
  428. height--;
  429. }
  430. }
  431. EXPORT_SYMBOL(radix_tree_tag_get);
  432. #endif
  433. static unsigned int
  434. __lookup(struct radix_tree_root *root, void **results, unsigned long index,
  435. unsigned int max_items, unsigned long *next_index)
  436. {
  437. unsigned int nr_found = 0;
  438. unsigned int shift, height;
  439. struct radix_tree_node *slot;
  440. unsigned long i;
  441. height = root->height;
  442. if (height == 0)
  443. goto out;
  444. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  445. slot = root->rnode;
  446. for ( ; height > 1; height--) {
  447. for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  448. i < RADIX_TREE_MAP_SIZE; i++) {
  449. if (slot->slots[i] != NULL)
  450. break;
  451. index &= ~((1UL << shift) - 1);
  452. index += 1UL << shift;
  453. if (index == 0)
  454. goto out; /* 32-bit wraparound */
  455. }
  456. if (i == RADIX_TREE_MAP_SIZE)
  457. goto out;
  458. shift -= RADIX_TREE_MAP_SHIFT;
  459. slot = slot->slots[i];
  460. }
  461. /* Bottom level: grab some items */
  462. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  463. index++;
  464. if (slot->slots[i]) {
  465. results[nr_found++] = slot->slots[i];
  466. if (nr_found == max_items)
  467. goto out;
  468. }
  469. }
  470. out:
  471. *next_index = index;
  472. return nr_found;
  473. }
  474. /**
  475. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  476. * @root: radix tree root
  477. * @results: where the results of the lookup are placed
  478. * @first_index: start the lookup from this key
  479. * @max_items: place up to this many items at *results
  480. *
  481. * Performs an index-ascending scan of the tree for present items. Places
  482. * them at *@results and returns the number of items which were placed at
  483. * *@results.
  484. *
  485. * The implementation is naive.
  486. */
  487. unsigned int
  488. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  489. unsigned long first_index, unsigned int max_items)
  490. {
  491. const unsigned long max_index = radix_tree_maxindex(root->height);
  492. unsigned long cur_index = first_index;
  493. unsigned int ret = 0;
  494. while (ret < max_items) {
  495. unsigned int nr_found;
  496. unsigned long next_index; /* Index of next search */
  497. if (cur_index > max_index)
  498. break;
  499. nr_found = __lookup(root, results + ret, cur_index,
  500. max_items - ret, &next_index);
  501. ret += nr_found;
  502. if (next_index == 0)
  503. break;
  504. cur_index = next_index;
  505. }
  506. return ret;
  507. }
  508. EXPORT_SYMBOL(radix_tree_gang_lookup);
  509. /*
  510. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  511. * open-coding the search.
  512. */
  513. static unsigned int
  514. __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
  515. unsigned int max_items, unsigned long *next_index, int tag)
  516. {
  517. unsigned int nr_found = 0;
  518. unsigned int shift;
  519. unsigned int height = root->height;
  520. struct radix_tree_node *slot;
  521. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  522. slot = root->rnode;
  523. while (height > 0) {
  524. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
  525. for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
  526. if (tag_get(slot, tag, i)) {
  527. BUG_ON(slot->slots[i] == NULL);
  528. break;
  529. }
  530. index &= ~((1UL << shift) - 1);
  531. index += 1UL << shift;
  532. if (index == 0)
  533. goto out; /* 32-bit wraparound */
  534. }
  535. if (i == RADIX_TREE_MAP_SIZE)
  536. goto out;
  537. height--;
  538. if (height == 0) { /* Bottom level: grab some items */
  539. unsigned long j = index & RADIX_TREE_MAP_MASK;
  540. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  541. index++;
  542. if (tag_get(slot, tag, j)) {
  543. BUG_ON(slot->slots[j] == NULL);
  544. results[nr_found++] = slot->slots[j];
  545. if (nr_found == max_items)
  546. goto out;
  547. }
  548. }
  549. }
  550. shift -= RADIX_TREE_MAP_SHIFT;
  551. slot = slot->slots[i];
  552. }
  553. out:
  554. *next_index = index;
  555. return nr_found;
  556. }
  557. /**
  558. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  559. * based on a tag
  560. * @root: radix tree root
  561. * @results: where the results of the lookup are placed
  562. * @first_index: start the lookup from this key
  563. * @max_items: place up to this many items at *results
  564. * @tag: the tag index
  565. *
  566. * Performs an index-ascending scan of the tree for present items which
  567. * have the tag indexed by @tag set. Places the items at *@results and
  568. * returns the number of items which were placed at *@results.
  569. */
  570. unsigned int
  571. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  572. unsigned long first_index, unsigned int max_items, int tag)
  573. {
  574. const unsigned long max_index = radix_tree_maxindex(root->height);
  575. unsigned long cur_index = first_index;
  576. unsigned int ret = 0;
  577. while (ret < max_items) {
  578. unsigned int nr_found;
  579. unsigned long next_index; /* Index of next search */
  580. if (cur_index > max_index)
  581. break;
  582. nr_found = __lookup_tag(root, results + ret, cur_index,
  583. max_items - ret, &next_index, tag);
  584. ret += nr_found;
  585. if (next_index == 0)
  586. break;
  587. cur_index = next_index;
  588. }
  589. return ret;
  590. }
  591. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  592. /**
  593. * radix_tree_delete - delete an item from a radix tree
  594. * @root: radix tree root
  595. * @index: index key
  596. *
  597. * Remove the item at @index from the radix tree rooted at @root.
  598. *
  599. * Returns the address of the deleted item, or NULL if it was not present.
  600. */
  601. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  602. {
  603. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  604. struct radix_tree_path *orig_pathp;
  605. struct radix_tree_node *slot;
  606. unsigned int height, shift;
  607. void *ret = NULL;
  608. char tags[RADIX_TREE_TAGS];
  609. int nr_cleared_tags;
  610. height = root->height;
  611. if (index > radix_tree_maxindex(height))
  612. goto out;
  613. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  614. pathp->node = NULL;
  615. slot = root->rnode;
  616. for ( ; height > 0; height--) {
  617. int offset;
  618. if (slot == NULL)
  619. goto out;
  620. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  621. pathp[1].offset = offset;
  622. pathp[1].node = slot;
  623. slot = slot->slots[offset];
  624. pathp++;
  625. shift -= RADIX_TREE_MAP_SHIFT;
  626. }
  627. ret = slot;
  628. if (ret == NULL)
  629. goto out;
  630. orig_pathp = pathp;
  631. /*
  632. * Clear all tags associated with the just-deleted item
  633. */
  634. memset(tags, 0, sizeof(tags));
  635. do {
  636. int tag;
  637. nr_cleared_tags = RADIX_TREE_TAGS;
  638. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  639. if (tags[tag])
  640. continue;
  641. tag_clear(pathp->node, tag, pathp->offset);
  642. if (any_tag_set(pathp->node, tag)) {
  643. tags[tag] = 1;
  644. nr_cleared_tags--;
  645. }
  646. }
  647. pathp--;
  648. } while (pathp->node && nr_cleared_tags);
  649. /* Now free the nodes we do not need anymore */
  650. for (pathp = orig_pathp; pathp->node; pathp--) {
  651. pathp->node->slots[pathp->offset] = NULL;
  652. if (--pathp->node->count)
  653. goto out;
  654. /* Node with zero slots in use so free it */
  655. radix_tree_node_free(pathp->node);
  656. }
  657. root->rnode = NULL;
  658. root->height = 0;
  659. out:
  660. return ret;
  661. }
  662. EXPORT_SYMBOL(radix_tree_delete);
  663. /**
  664. * radix_tree_tagged - test whether any items in the tree are tagged
  665. * @root: radix tree root
  666. * @tag: tag to test
  667. */
  668. int radix_tree_tagged(struct radix_tree_root *root, int tag)
  669. {
  670. struct radix_tree_node *rnode;
  671. rnode = root->rnode;
  672. if (!rnode)
  673. return 0;
  674. return any_tag_set(rnode, tag);
  675. }
  676. EXPORT_SYMBOL(radix_tree_tagged);
  677. static void
  678. radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
  679. {
  680. memset(node, 0, sizeof(struct radix_tree_node));
  681. }
  682. static __init unsigned long __maxindex(unsigned int height)
  683. {
  684. unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
  685. unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
  686. if (tmp >= RADIX_TREE_INDEX_BITS)
  687. index = ~0UL;
  688. return index;
  689. }
  690. static __init void radix_tree_init_maxindex(void)
  691. {
  692. unsigned int i;
  693. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  694. height_to_maxindex[i] = __maxindex(i);
  695. }
  696. #ifdef CONFIG_HOTPLUG_CPU
  697. static int radix_tree_callback(struct notifier_block *nfb,
  698. unsigned long action,
  699. void *hcpu)
  700. {
  701. int cpu = (long)hcpu;
  702. struct radix_tree_preload *rtp;
  703. /* Free per-cpu pool of perloaded nodes */
  704. if (action == CPU_DEAD) {
  705. rtp = &per_cpu(radix_tree_preloads, cpu);
  706. while (rtp->nr) {
  707. kmem_cache_free(radix_tree_node_cachep,
  708. rtp->nodes[rtp->nr-1]);
  709. rtp->nodes[rtp->nr-1] = NULL;
  710. rtp->nr--;
  711. }
  712. }
  713. return NOTIFY_OK;
  714. }
  715. #endif /* CONFIG_HOTPLUG_CPU */
  716. void __init radix_tree_init(void)
  717. {
  718. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  719. sizeof(struct radix_tree_node), 0,
  720. SLAB_PANIC, radix_tree_node_ctor, NULL);
  721. radix_tree_init_maxindex();
  722. hotcpu_notifier(radix_tree_callback, 0);
  723. }