radix-tree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. __set_bit(offset, node->tags[tag]);
  123. }
  124. static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
  125. {
  126. __clear_bit(offset, node->tags[tag]);
  127. }
  128. static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
  129. {
  130. return test_bit(offset, node->tags[tag]);
  131. }
  132. /*
  133. * Returns 1 if any slot in the node has this tag set.
  134. * Otherwise returns 0.
  135. */
  136. static inline int any_tag_set(struct radix_tree_node *node, int tag)
  137. {
  138. int idx;
  139. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  140. if (node->tags[tag][idx])
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Return the maximum key which can be store into a
  147. * radix tree with height HEIGHT.
  148. */
  149. static inline unsigned long radix_tree_maxindex(unsigned int height)
  150. {
  151. return height_to_maxindex[height];
  152. }
  153. /*
  154. * Extend a radix tree so it can store key @index.
  155. */
  156. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  157. {
  158. struct radix_tree_node *node;
  159. unsigned int height;
  160. char tags[RADIX_TREE_TAGS];
  161. int tag;
  162. /* Figure out what the height should be. */
  163. height = root->height + 1;
  164. while (index > radix_tree_maxindex(height))
  165. height++;
  166. if (root->rnode == NULL) {
  167. root->height = height;
  168. goto out;
  169. }
  170. /*
  171. * Prepare the tag status of the top-level node for propagation
  172. * into the newly-pushed top-level node(s)
  173. */
  174. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  175. tags[tag] = 0;
  176. if (any_tag_set(root->rnode, tag))
  177. tags[tag] = 1;
  178. }
  179. do {
  180. if (!(node = radix_tree_node_alloc(root)))
  181. return -ENOMEM;
  182. /* Increase the height. */
  183. node->slots[0] = root->rnode;
  184. /* Propagate the aggregated tag info into the new root */
  185. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  186. if (tags[tag])
  187. tag_set(node, tag, 0);
  188. }
  189. node->count = 1;
  190. root->rnode = node;
  191. root->height++;
  192. } while (height > root->height);
  193. out:
  194. return 0;
  195. }
  196. /**
  197. * radix_tree_insert - insert into a radix tree
  198. * @root: radix tree root
  199. * @index: index key
  200. * @item: item to insert
  201. *
  202. * Insert an item into the radix tree at position @index.
  203. */
  204. int radix_tree_insert(struct radix_tree_root *root,
  205. unsigned long index, void *item)
  206. {
  207. struct radix_tree_node *node = NULL, *slot;
  208. unsigned int height, shift;
  209. int offset;
  210. int error;
  211. /* Make sure the tree is high enough. */
  212. if ((!index && !root->rnode) ||
  213. index > radix_tree_maxindex(root->height)) {
  214. error = radix_tree_extend(root, index);
  215. if (error)
  216. return error;
  217. }
  218. slot = root->rnode;
  219. height = root->height;
  220. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  221. offset = 0; /* uninitialised var warning */
  222. do {
  223. if (slot == NULL) {
  224. /* Have to add a child node. */
  225. if (!(slot = radix_tree_node_alloc(root)))
  226. return -ENOMEM;
  227. if (node) {
  228. node->slots[offset] = slot;
  229. node->count++;
  230. } else
  231. root->rnode = slot;
  232. }
  233. /* Go a level down */
  234. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  235. node = slot;
  236. slot = node->slots[offset];
  237. shift -= RADIX_TREE_MAP_SHIFT;
  238. height--;
  239. } while (height > 0);
  240. if (slot != NULL)
  241. return -EEXIST;
  242. BUG_ON(!node);
  243. node->count++;
  244. node->slots[offset] = item;
  245. BUG_ON(tag_get(node, 0, offset));
  246. BUG_ON(tag_get(node, 1, offset));
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(radix_tree_insert);
  250. static inline void **__lookup_slot(struct radix_tree_root *root,
  251. unsigned long index)
  252. {
  253. unsigned int height, shift;
  254. struct radix_tree_node **slot;
  255. height = root->height;
  256. if (index > radix_tree_maxindex(height))
  257. return NULL;
  258. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  259. slot = &root->rnode;
  260. while (height > 0) {
  261. if (*slot == NULL)
  262. return NULL;
  263. slot = (struct radix_tree_node **)
  264. ((*slot)->slots +
  265. ((index >> shift) & RADIX_TREE_MAP_MASK));
  266. shift -= RADIX_TREE_MAP_SHIFT;
  267. height--;
  268. }
  269. return (void **)slot;
  270. }
  271. /**
  272. * radix_tree_lookup_slot - lookup a slot in a radix tree
  273. * @root: radix tree root
  274. * @index: index key
  275. *
  276. * Lookup the slot corresponding to the position @index in the radix tree
  277. * @root. This is useful for update-if-exists operations.
  278. */
  279. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  280. {
  281. return __lookup_slot(root, index);
  282. }
  283. EXPORT_SYMBOL(radix_tree_lookup_slot);
  284. /**
  285. * radix_tree_lookup - perform lookup operation on a radix tree
  286. * @root: radix tree root
  287. * @index: index key
  288. *
  289. * Lookup the item at the position @index in the radix tree @root.
  290. */
  291. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  292. {
  293. void **slot;
  294. slot = __lookup_slot(root, index);
  295. return slot != NULL ? *slot : NULL;
  296. }
  297. EXPORT_SYMBOL(radix_tree_lookup);
  298. /**
  299. * radix_tree_tag_set - set a tag on a radix tree node
  300. * @root: radix tree root
  301. * @index: index key
  302. * @tag: tag index
  303. *
  304. * Set the search tag corresponging to @index in the radix tree. From
  305. * the root all the way down to the leaf node.
  306. *
  307. * Returns the address of the tagged item. Setting a tag on a not-present
  308. * item is a bug.
  309. */
  310. void *radix_tree_tag_set(struct radix_tree_root *root,
  311. unsigned long index, int tag)
  312. {
  313. unsigned int height, shift;
  314. struct radix_tree_node *slot;
  315. height = root->height;
  316. if (index > radix_tree_maxindex(height))
  317. return NULL;
  318. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  319. slot = root->rnode;
  320. while (height > 0) {
  321. int offset;
  322. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  323. if (!tag_get(slot, tag, offset))
  324. tag_set(slot, tag, offset);
  325. slot = slot->slots[offset];
  326. BUG_ON(slot == NULL);
  327. shift -= RADIX_TREE_MAP_SHIFT;
  328. height--;
  329. }
  330. return slot;
  331. }
  332. EXPORT_SYMBOL(radix_tree_tag_set);
  333. /**
  334. * radix_tree_tag_clear - clear a tag on a radix tree node
  335. * @root: radix tree root
  336. * @index: index key
  337. * @tag: tag index
  338. *
  339. * Clear the search tag corresponging to @index in the radix tree. If
  340. * this causes the leaf node to have no tags set then clear the tag in the
  341. * next-to-leaf node, etc.
  342. *
  343. * Returns the address of the tagged item on success, else NULL. ie:
  344. * has the same return value and semantics as radix_tree_lookup().
  345. */
  346. void *radix_tree_tag_clear(struct radix_tree_root *root,
  347. unsigned long index, int tag)
  348. {
  349. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  350. struct radix_tree_node *slot;
  351. unsigned int height, shift;
  352. void *ret = NULL;
  353. height = root->height;
  354. if (index > radix_tree_maxindex(height))
  355. goto out;
  356. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  357. pathp->node = NULL;
  358. slot = root->rnode;
  359. while (height > 0) {
  360. int offset;
  361. if (slot == NULL)
  362. goto out;
  363. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  364. pathp[1].offset = offset;
  365. pathp[1].node = slot;
  366. slot = slot->slots[offset];
  367. pathp++;
  368. shift -= RADIX_TREE_MAP_SHIFT;
  369. height--;
  370. }
  371. ret = slot;
  372. if (ret == NULL)
  373. goto out;
  374. do {
  375. if (!tag_get(pathp->node, tag, pathp->offset))
  376. goto out;
  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_shrink - shrink height of a radix tree to minimal
  594. * @root radix tree root
  595. */
  596. static inline void radix_tree_shrink(struct radix_tree_root *root)
  597. {
  598. /* try to shrink tree height */
  599. while (root->height > 1 &&
  600. root->rnode->count == 1 &&
  601. root->rnode->slots[0]) {
  602. struct radix_tree_node *to_free = root->rnode;
  603. root->rnode = to_free->slots[0];
  604. root->height--;
  605. /* must only free zeroed nodes into the slab */
  606. tag_clear(to_free, 0, 0);
  607. tag_clear(to_free, 1, 0);
  608. to_free->slots[0] = NULL;
  609. to_free->count = 0;
  610. radix_tree_node_free(to_free);
  611. }
  612. }
  613. /**
  614. * radix_tree_delete - delete an item from a radix tree
  615. * @root: radix tree root
  616. * @index: index key
  617. *
  618. * Remove the item at @index from the radix tree rooted at @root.
  619. *
  620. * Returns the address of the deleted item, or NULL if it was not present.
  621. */
  622. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  623. {
  624. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  625. struct radix_tree_path *orig_pathp;
  626. struct radix_tree_node *slot;
  627. unsigned int height, shift;
  628. void *ret = NULL;
  629. char tags[RADIX_TREE_TAGS];
  630. int nr_cleared_tags;
  631. int tag;
  632. int offset;
  633. height = root->height;
  634. if (index > radix_tree_maxindex(height))
  635. goto out;
  636. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  637. pathp->node = NULL;
  638. slot = root->rnode;
  639. for ( ; height > 0; height--) {
  640. if (slot == NULL)
  641. goto out;
  642. pathp++;
  643. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  644. pathp->offset = offset;
  645. pathp->node = slot;
  646. slot = slot->slots[offset];
  647. shift -= RADIX_TREE_MAP_SHIFT;
  648. }
  649. ret = slot;
  650. if (ret == NULL)
  651. goto out;
  652. orig_pathp = pathp;
  653. /*
  654. * Clear all tags associated with the just-deleted item
  655. */
  656. nr_cleared_tags = 0;
  657. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  658. if (tag_get(pathp->node, tag, pathp->offset)) {
  659. tag_clear(pathp->node, tag, pathp->offset);
  660. tags[tag] = 0;
  661. nr_cleared_tags++;
  662. } else
  663. tags[tag] = 1;
  664. }
  665. for (pathp--; nr_cleared_tags && pathp->node; pathp--) {
  666. for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
  667. if (tags[tag])
  668. continue;
  669. tag_clear(pathp->node, tag, pathp->offset);
  670. if (any_tag_set(pathp->node, tag)) {
  671. tags[tag] = 1;
  672. nr_cleared_tags--;
  673. }
  674. }
  675. }
  676. /* Now free the nodes we do not need anymore */
  677. for (pathp = orig_pathp; pathp->node; pathp--) {
  678. pathp->node->slots[pathp->offset] = NULL;
  679. pathp->node->count--;
  680. if (pathp->node->count) {
  681. if (pathp->node == root->rnode)
  682. radix_tree_shrink(root);
  683. goto out;
  684. }
  685. /* Node with zero slots in use so free it */
  686. radix_tree_node_free(pathp->node);
  687. }
  688. root->rnode = NULL;
  689. root->height = 0;
  690. out:
  691. return ret;
  692. }
  693. EXPORT_SYMBOL(radix_tree_delete);
  694. /**
  695. * radix_tree_tagged - test whether any items in the tree are tagged
  696. * @root: radix tree root
  697. * @tag: tag to test
  698. */
  699. int radix_tree_tagged(struct radix_tree_root *root, int tag)
  700. {
  701. struct radix_tree_node *rnode;
  702. rnode = root->rnode;
  703. if (!rnode)
  704. return 0;
  705. return any_tag_set(rnode, tag);
  706. }
  707. EXPORT_SYMBOL(radix_tree_tagged);
  708. static void
  709. radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
  710. {
  711. memset(node, 0, sizeof(struct radix_tree_node));
  712. }
  713. static __init unsigned long __maxindex(unsigned int height)
  714. {
  715. unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
  716. unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
  717. if (tmp >= RADIX_TREE_INDEX_BITS)
  718. index = ~0UL;
  719. return index;
  720. }
  721. static __init void radix_tree_init_maxindex(void)
  722. {
  723. unsigned int i;
  724. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  725. height_to_maxindex[i] = __maxindex(i);
  726. }
  727. #ifdef CONFIG_HOTPLUG_CPU
  728. static int radix_tree_callback(struct notifier_block *nfb,
  729. unsigned long action,
  730. void *hcpu)
  731. {
  732. int cpu = (long)hcpu;
  733. struct radix_tree_preload *rtp;
  734. /* Free per-cpu pool of perloaded nodes */
  735. if (action == CPU_DEAD) {
  736. rtp = &per_cpu(radix_tree_preloads, cpu);
  737. while (rtp->nr) {
  738. kmem_cache_free(radix_tree_node_cachep,
  739. rtp->nodes[rtp->nr-1]);
  740. rtp->nodes[rtp->nr-1] = NULL;
  741. rtp->nr--;
  742. }
  743. }
  744. return NOTIFY_OK;
  745. }
  746. #endif /* CONFIG_HOTPLUG_CPU */
  747. void __init radix_tree_init(void)
  748. {
  749. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  750. sizeof(struct radix_tree_node), 0,
  751. SLAB_PANIC, radix_tree_node_ctor, NULL);
  752. radix_tree_init_maxindex();
  753. hotcpu_notifier(radix_tree_callback, 0);
  754. }