radix-tree.c 22 KB

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