radix-tree.c 20 KB

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