radix-tree.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. * Copyright (C) 2006 Nick Piggin
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/radix-tree.h>
  26. #include <linux/percpu.h>
  27. #include <linux/slab.h>
  28. #include <linux/notifier.h>
  29. #include <linux/cpu.h>
  30. #include <linux/gfp.h>
  31. #include <linux/string.h>
  32. #include <linux/bitops.h>
  33. #include <linux/rcupdate.h>
  34. #ifdef __KERNEL__
  35. #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  36. #else
  37. #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
  38. #endif
  39. #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
  40. #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
  41. #define RADIX_TREE_TAG_LONGS \
  42. ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
  43. struct radix_tree_node {
  44. unsigned int height; /* Height from the bottom */
  45. unsigned int count;
  46. struct rcu_head rcu_head;
  47. void *slots[RADIX_TREE_MAP_SIZE];
  48. unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
  49. };
  50. struct radix_tree_path {
  51. struct radix_tree_node *node;
  52. int offset;
  53. };
  54. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  55. #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
  56. static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
  57. /*
  58. * Radix tree node cache.
  59. */
  60. static struct kmem_cache *radix_tree_node_cachep;
  61. /*
  62. * Per-cpu pool of preloaded nodes
  63. */
  64. struct radix_tree_preload {
  65. int nr;
  66. struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
  67. };
  68. DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  69. static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
  70. {
  71. return root->gfp_mask & __GFP_BITS_MASK;
  72. }
  73. /*
  74. * This assumes that the caller has performed appropriate preallocation, and
  75. * that the caller has pinned this thread of control to the current CPU.
  76. */
  77. static struct radix_tree_node *
  78. radix_tree_node_alloc(struct radix_tree_root *root)
  79. {
  80. struct radix_tree_node *ret;
  81. gfp_t gfp_mask = root_gfp_mask(root);
  82. ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  83. if (ret == NULL && !(gfp_mask & __GFP_WAIT)) {
  84. struct radix_tree_preload *rtp;
  85. rtp = &__get_cpu_var(radix_tree_preloads);
  86. if (rtp->nr) {
  87. ret = rtp->nodes[rtp->nr - 1];
  88. rtp->nodes[rtp->nr - 1] = NULL;
  89. rtp->nr--;
  90. }
  91. }
  92. BUG_ON(radix_tree_is_direct_ptr(ret));
  93. return ret;
  94. }
  95. static void radix_tree_node_rcu_free(struct rcu_head *head)
  96. {
  97. struct radix_tree_node *node =
  98. container_of(head, struct radix_tree_node, rcu_head);
  99. kmem_cache_free(radix_tree_node_cachep, node);
  100. }
  101. static inline void
  102. radix_tree_node_free(struct radix_tree_node *node)
  103. {
  104. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  105. }
  106. /*
  107. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  108. * ensure that the addition of a single element in the tree cannot fail. On
  109. * success, return zero, with preemption disabled. On error, return -ENOMEM
  110. * with preemption not disabled.
  111. */
  112. int radix_tree_preload(gfp_t gfp_mask)
  113. {
  114. struct radix_tree_preload *rtp;
  115. struct radix_tree_node *node;
  116. int ret = -ENOMEM;
  117. preempt_disable();
  118. rtp = &__get_cpu_var(radix_tree_preloads);
  119. while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
  120. preempt_enable();
  121. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  122. if (node == NULL)
  123. goto out;
  124. preempt_disable();
  125. rtp = &__get_cpu_var(radix_tree_preloads);
  126. if (rtp->nr < ARRAY_SIZE(rtp->nodes))
  127. rtp->nodes[rtp->nr++] = node;
  128. else
  129. kmem_cache_free(radix_tree_node_cachep, node);
  130. }
  131. ret = 0;
  132. out:
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(radix_tree_preload);
  136. static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
  137. int offset)
  138. {
  139. __set_bit(offset, node->tags[tag]);
  140. }
  141. static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
  142. int offset)
  143. {
  144. __clear_bit(offset, node->tags[tag]);
  145. }
  146. static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
  147. int offset)
  148. {
  149. return test_bit(offset, node->tags[tag]);
  150. }
  151. static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
  152. {
  153. root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
  154. }
  155. static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
  156. {
  157. root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
  158. }
  159. static inline void root_tag_clear_all(struct radix_tree_root *root)
  160. {
  161. root->gfp_mask &= __GFP_BITS_MASK;
  162. }
  163. static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
  164. {
  165. return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
  166. }
  167. /*
  168. * Returns 1 if any slot in the node has this tag set.
  169. * Otherwise returns 0.
  170. */
  171. static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
  172. {
  173. int idx;
  174. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  175. if (node->tags[tag][idx])
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. /*
  181. * Return the maximum key which can be store into a
  182. * radix tree with height HEIGHT.
  183. */
  184. static inline unsigned long radix_tree_maxindex(unsigned int height)
  185. {
  186. return height_to_maxindex[height];
  187. }
  188. /*
  189. * Extend a radix tree so it can store key @index.
  190. */
  191. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  192. {
  193. struct radix_tree_node *node;
  194. unsigned int height;
  195. int tag;
  196. /* Figure out what the height should be. */
  197. height = root->height + 1;
  198. while (index > radix_tree_maxindex(height))
  199. height++;
  200. if (root->rnode == NULL) {
  201. root->height = height;
  202. goto out;
  203. }
  204. do {
  205. unsigned int newheight;
  206. if (!(node = radix_tree_node_alloc(root)))
  207. return -ENOMEM;
  208. /* Increase the height. */
  209. node->slots[0] = radix_tree_direct_to_ptr(root->rnode);
  210. /* Propagate the aggregated tag info into the new root */
  211. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  212. if (root_tag_get(root, tag))
  213. tag_set(node, tag, 0);
  214. }
  215. newheight = root->height+1;
  216. node->height = newheight;
  217. node->count = 1;
  218. rcu_assign_pointer(root->rnode, node);
  219. root->height = newheight;
  220. } while (height > root->height);
  221. out:
  222. return 0;
  223. }
  224. /**
  225. * radix_tree_insert - insert into a radix tree
  226. * @root: radix tree root
  227. * @index: index key
  228. * @item: item to insert
  229. *
  230. * Insert an item into the radix tree at position @index.
  231. */
  232. int radix_tree_insert(struct radix_tree_root *root,
  233. unsigned long index, void *item)
  234. {
  235. struct radix_tree_node *node = NULL, *slot;
  236. unsigned int height, shift;
  237. int offset;
  238. int error;
  239. BUG_ON(radix_tree_is_direct_ptr(item));
  240. /* Make sure the tree is high enough. */
  241. if (index > radix_tree_maxindex(root->height)) {
  242. error = radix_tree_extend(root, index);
  243. if (error)
  244. return error;
  245. }
  246. slot = root->rnode;
  247. height = root->height;
  248. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  249. offset = 0; /* uninitialised var warning */
  250. while (height > 0) {
  251. if (slot == NULL) {
  252. /* Have to add a child node. */
  253. if (!(slot = radix_tree_node_alloc(root)))
  254. return -ENOMEM;
  255. slot->height = height;
  256. if (node) {
  257. rcu_assign_pointer(node->slots[offset], slot);
  258. node->count++;
  259. } else
  260. rcu_assign_pointer(root->rnode, slot);
  261. }
  262. /* Go a level down */
  263. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  264. node = slot;
  265. slot = node->slots[offset];
  266. shift -= RADIX_TREE_MAP_SHIFT;
  267. height--;
  268. }
  269. if (slot != NULL)
  270. return -EEXIST;
  271. if (node) {
  272. node->count++;
  273. rcu_assign_pointer(node->slots[offset], item);
  274. BUG_ON(tag_get(node, 0, offset));
  275. BUG_ON(tag_get(node, 1, offset));
  276. } else {
  277. rcu_assign_pointer(root->rnode, radix_tree_ptr_to_direct(item));
  278. BUG_ON(root_tag_get(root, 0));
  279. BUG_ON(root_tag_get(root, 1));
  280. }
  281. return 0;
  282. }
  283. EXPORT_SYMBOL(radix_tree_insert);
  284. /**
  285. * radix_tree_lookup_slot - lookup a slot in a radix tree
  286. * @root: radix tree root
  287. * @index: index key
  288. *
  289. * Returns: the slot corresponding to the position @index in the
  290. * radix tree @root. This is useful for update-if-exists operations.
  291. *
  292. * This function cannot be called under rcu_read_lock, it must be
  293. * excluded from writers, as must the returned slot for subsequent
  294. * use by radix_tree_deref_slot() and radix_tree_replace slot.
  295. * Caller must hold tree write locked across slot lookup and
  296. * replace.
  297. */
  298. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  299. {
  300. unsigned int height, shift;
  301. struct radix_tree_node *node, **slot;
  302. node = root->rnode;
  303. if (node == NULL)
  304. return NULL;
  305. if (radix_tree_is_direct_ptr(node)) {
  306. if (index > 0)
  307. return NULL;
  308. return (void **)&root->rnode;
  309. }
  310. height = node->height;
  311. if (index > radix_tree_maxindex(height))
  312. return NULL;
  313. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  314. do {
  315. slot = (struct radix_tree_node **)
  316. (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
  317. node = *slot;
  318. if (node == NULL)
  319. return NULL;
  320. shift -= RADIX_TREE_MAP_SHIFT;
  321. height--;
  322. } while (height > 0);
  323. return (void **)slot;
  324. }
  325. EXPORT_SYMBOL(radix_tree_lookup_slot);
  326. /**
  327. * radix_tree_lookup - perform lookup operation on a radix tree
  328. * @root: radix tree root
  329. * @index: index key
  330. *
  331. * Lookup the item at the position @index in the radix tree @root.
  332. *
  333. * This function can be called under rcu_read_lock, however the caller
  334. * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
  335. * them safely). No RCU barriers are required to access or modify the
  336. * returned item, however.
  337. */
  338. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  339. {
  340. unsigned int height, shift;
  341. struct radix_tree_node *node, **slot;
  342. node = rcu_dereference(root->rnode);
  343. if (node == NULL)
  344. return NULL;
  345. if (radix_tree_is_direct_ptr(node)) {
  346. if (index > 0)
  347. return NULL;
  348. return radix_tree_direct_to_ptr(node);
  349. }
  350. height = node->height;
  351. if (index > radix_tree_maxindex(height))
  352. return NULL;
  353. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  354. do {
  355. slot = (struct radix_tree_node **)
  356. (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
  357. node = rcu_dereference(*slot);
  358. if (node == NULL)
  359. return NULL;
  360. shift -= RADIX_TREE_MAP_SHIFT;
  361. height--;
  362. } while (height > 0);
  363. return node;
  364. }
  365. EXPORT_SYMBOL(radix_tree_lookup);
  366. /**
  367. * radix_tree_tag_set - set a tag on a radix tree node
  368. * @root: radix tree root
  369. * @index: index key
  370. * @tag: tag index
  371. *
  372. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  373. * corresponding to @index in the radix tree. From
  374. * the root all the way down to the leaf node.
  375. *
  376. * Returns the address of the tagged item. Setting a tag on a not-present
  377. * item is a bug.
  378. */
  379. void *radix_tree_tag_set(struct radix_tree_root *root,
  380. unsigned long index, unsigned int tag)
  381. {
  382. unsigned int height, shift;
  383. struct radix_tree_node *slot;
  384. height = root->height;
  385. BUG_ON(index > radix_tree_maxindex(height));
  386. slot = root->rnode;
  387. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  388. while (height > 0) {
  389. int offset;
  390. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  391. if (!tag_get(slot, tag, offset))
  392. tag_set(slot, tag, offset);
  393. slot = slot->slots[offset];
  394. BUG_ON(slot == NULL);
  395. shift -= RADIX_TREE_MAP_SHIFT;
  396. height--;
  397. }
  398. /* set the root's tag bit */
  399. if (slot && !root_tag_get(root, tag))
  400. root_tag_set(root, tag);
  401. return slot;
  402. }
  403. EXPORT_SYMBOL(radix_tree_tag_set);
  404. /**
  405. * radix_tree_tag_clear - clear a tag on a radix tree node
  406. * @root: radix tree root
  407. * @index: index key
  408. * @tag: tag index
  409. *
  410. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  411. * corresponding to @index in the radix tree. If
  412. * this causes the leaf node to have no tags set then clear the tag in the
  413. * next-to-leaf node, etc.
  414. *
  415. * Returns the address of the tagged item on success, else NULL. ie:
  416. * has the same return value and semantics as radix_tree_lookup().
  417. */
  418. void *radix_tree_tag_clear(struct radix_tree_root *root,
  419. unsigned long index, unsigned int tag)
  420. {
  421. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  422. struct radix_tree_node *slot = NULL;
  423. unsigned int height, shift;
  424. height = root->height;
  425. if (index > radix_tree_maxindex(height))
  426. goto out;
  427. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  428. pathp->node = NULL;
  429. slot = root->rnode;
  430. while (height > 0) {
  431. int offset;
  432. if (slot == NULL)
  433. goto out;
  434. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  435. pathp[1].offset = offset;
  436. pathp[1].node = slot;
  437. slot = slot->slots[offset];
  438. pathp++;
  439. shift -= RADIX_TREE_MAP_SHIFT;
  440. height--;
  441. }
  442. if (slot == NULL)
  443. goto out;
  444. while (pathp->node) {
  445. if (!tag_get(pathp->node, tag, pathp->offset))
  446. goto out;
  447. tag_clear(pathp->node, tag, pathp->offset);
  448. if (any_tag_set(pathp->node, tag))
  449. goto out;
  450. pathp--;
  451. }
  452. /* clear the root's tag bit */
  453. if (root_tag_get(root, tag))
  454. root_tag_clear(root, tag);
  455. out:
  456. return slot;
  457. }
  458. EXPORT_SYMBOL(radix_tree_tag_clear);
  459. #ifndef __KERNEL__ /* Only the test harness uses this at present */
  460. /**
  461. * radix_tree_tag_get - get a tag on a radix tree node
  462. * @root: radix tree root
  463. * @index: index key
  464. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  465. *
  466. * Return values:
  467. *
  468. * 0: tag not present or not set
  469. * 1: tag set
  470. */
  471. int radix_tree_tag_get(struct radix_tree_root *root,
  472. unsigned long index, unsigned int tag)
  473. {
  474. unsigned int height, shift;
  475. struct radix_tree_node *node;
  476. int saw_unset_tag = 0;
  477. /* check the root's tag bit */
  478. if (!root_tag_get(root, tag))
  479. return 0;
  480. node = rcu_dereference(root->rnode);
  481. if (node == NULL)
  482. return 0;
  483. if (radix_tree_is_direct_ptr(node))
  484. return (index == 0);
  485. height = node->height;
  486. if (index > radix_tree_maxindex(height))
  487. return 0;
  488. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  489. for ( ; ; ) {
  490. int offset;
  491. if (node == NULL)
  492. return 0;
  493. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  494. /*
  495. * This is just a debug check. Later, we can bale as soon as
  496. * we see an unset tag.
  497. */
  498. if (!tag_get(node, tag, offset))
  499. saw_unset_tag = 1;
  500. if (height == 1) {
  501. int ret = tag_get(node, tag, offset);
  502. BUG_ON(ret && saw_unset_tag);
  503. return !!ret;
  504. }
  505. node = rcu_dereference(node->slots[offset]);
  506. shift -= RADIX_TREE_MAP_SHIFT;
  507. height--;
  508. }
  509. }
  510. EXPORT_SYMBOL(radix_tree_tag_get);
  511. #endif
  512. static unsigned int
  513. __lookup(struct radix_tree_node *slot, void **results, unsigned long index,
  514. unsigned int max_items, unsigned long *next_index)
  515. {
  516. unsigned int nr_found = 0;
  517. unsigned int shift, height;
  518. unsigned long i;
  519. height = slot->height;
  520. if (height == 0)
  521. goto out;
  522. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  523. for ( ; height > 1; height--) {
  524. i = (index >> shift) & RADIX_TREE_MAP_MASK;
  525. for (;;) {
  526. if (slot->slots[i] != NULL)
  527. break;
  528. index &= ~((1UL << shift) - 1);
  529. index += 1UL << shift;
  530. if (index == 0)
  531. goto out; /* 32-bit wraparound */
  532. i++;
  533. if (i == RADIX_TREE_MAP_SIZE)
  534. goto out;
  535. }
  536. shift -= RADIX_TREE_MAP_SHIFT;
  537. slot = rcu_dereference(slot->slots[i]);
  538. if (slot == NULL)
  539. goto out;
  540. }
  541. /* Bottom level: grab some items */
  542. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  543. struct radix_tree_node *node;
  544. index++;
  545. node = slot->slots[i];
  546. if (node) {
  547. results[nr_found++] = rcu_dereference(node);
  548. if (nr_found == max_items)
  549. goto out;
  550. }
  551. }
  552. out:
  553. *next_index = index;
  554. return nr_found;
  555. }
  556. /**
  557. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  558. * @root: radix tree root
  559. * @results: where the results of the lookup are placed
  560. * @first_index: start the lookup from this key
  561. * @max_items: place up to this many items at *results
  562. *
  563. * Performs an index-ascending scan of the tree for present items. Places
  564. * them at *@results and returns the number of items which were placed at
  565. * *@results.
  566. *
  567. * The implementation is naive.
  568. *
  569. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  570. * rcu_read_lock. In this case, rather than the returned results being
  571. * an atomic snapshot of the tree at a single point in time, the semantics
  572. * of an RCU protected gang lookup are as though multiple radix_tree_lookups
  573. * have been issued in individual locks, and results stored in 'results'.
  574. */
  575. unsigned int
  576. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  577. unsigned long first_index, unsigned int max_items)
  578. {
  579. unsigned long max_index;
  580. struct radix_tree_node *node;
  581. unsigned long cur_index = first_index;
  582. unsigned int ret;
  583. node = rcu_dereference(root->rnode);
  584. if (!node)
  585. return 0;
  586. if (radix_tree_is_direct_ptr(node)) {
  587. if (first_index > 0)
  588. return 0;
  589. node = radix_tree_direct_to_ptr(node);
  590. results[0] = rcu_dereference(node);
  591. return 1;
  592. }
  593. max_index = radix_tree_maxindex(node->height);
  594. ret = 0;
  595. while (ret < max_items) {
  596. unsigned int nr_found;
  597. unsigned long next_index; /* Index of next search */
  598. if (cur_index > max_index)
  599. break;
  600. nr_found = __lookup(node, results + ret, cur_index,
  601. max_items - ret, &next_index);
  602. ret += nr_found;
  603. if (next_index == 0)
  604. break;
  605. cur_index = next_index;
  606. }
  607. return ret;
  608. }
  609. EXPORT_SYMBOL(radix_tree_gang_lookup);
  610. /*
  611. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  612. * open-coding the search.
  613. */
  614. static unsigned int
  615. __lookup_tag(struct radix_tree_node *slot, void **results, unsigned long index,
  616. unsigned int max_items, unsigned long *next_index, unsigned int tag)
  617. {
  618. unsigned int nr_found = 0;
  619. unsigned int shift, height;
  620. height = slot->height;
  621. if (height == 0)
  622. goto out;
  623. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  624. while (height > 0) {
  625. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  626. for (;;) {
  627. if (tag_get(slot, tag, i))
  628. break;
  629. index &= ~((1UL << shift) - 1);
  630. index += 1UL << shift;
  631. if (index == 0)
  632. goto out; /* 32-bit wraparound */
  633. i++;
  634. if (i == RADIX_TREE_MAP_SIZE)
  635. goto out;
  636. }
  637. height--;
  638. if (height == 0) { /* Bottom level: grab some items */
  639. unsigned long j = index & RADIX_TREE_MAP_MASK;
  640. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  641. struct radix_tree_node *node;
  642. index++;
  643. if (!tag_get(slot, tag, j))
  644. continue;
  645. node = slot->slots[j];
  646. /*
  647. * Even though the tag was found set, we need to
  648. * recheck that we have a non-NULL node, because
  649. * if this lookup is lockless, it may have been
  650. * subsequently deleted.
  651. *
  652. * Similar care must be taken in any place that
  653. * lookup ->slots[x] without a lock (ie. can't
  654. * rely on its value remaining the same).
  655. */
  656. if (node) {
  657. node = rcu_dereference(node);
  658. results[nr_found++] = node;
  659. if (nr_found == max_items)
  660. goto out;
  661. }
  662. }
  663. }
  664. shift -= RADIX_TREE_MAP_SHIFT;
  665. slot = rcu_dereference(slot->slots[i]);
  666. if (slot == NULL)
  667. break;
  668. }
  669. out:
  670. *next_index = index;
  671. return nr_found;
  672. }
  673. /**
  674. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  675. * based on a tag
  676. * @root: radix tree root
  677. * @results: where the results of the lookup are placed
  678. * @first_index: start the lookup from this key
  679. * @max_items: place up to this many items at *results
  680. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  681. *
  682. * Performs an index-ascending scan of the tree for present items which
  683. * have the tag indexed by @tag set. Places the items at *@results and
  684. * returns the number of items which were placed at *@results.
  685. */
  686. unsigned int
  687. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  688. unsigned long first_index, unsigned int max_items,
  689. unsigned int tag)
  690. {
  691. struct radix_tree_node *node;
  692. unsigned long max_index;
  693. unsigned long cur_index = first_index;
  694. unsigned int ret;
  695. /* check the root's tag bit */
  696. if (!root_tag_get(root, tag))
  697. return 0;
  698. node = rcu_dereference(root->rnode);
  699. if (!node)
  700. return 0;
  701. if (radix_tree_is_direct_ptr(node)) {
  702. if (first_index > 0)
  703. return 0;
  704. node = radix_tree_direct_to_ptr(node);
  705. results[0] = rcu_dereference(node);
  706. return 1;
  707. }
  708. max_index = radix_tree_maxindex(node->height);
  709. ret = 0;
  710. while (ret < max_items) {
  711. unsigned int nr_found;
  712. unsigned long next_index; /* Index of next search */
  713. if (cur_index > max_index)
  714. break;
  715. nr_found = __lookup_tag(node, results + ret, cur_index,
  716. max_items - ret, &next_index, tag);
  717. ret += nr_found;
  718. if (next_index == 0)
  719. break;
  720. cur_index = next_index;
  721. }
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  725. /**
  726. * radix_tree_shrink - shrink height of a radix tree to minimal
  727. * @root radix tree root
  728. */
  729. static inline void radix_tree_shrink(struct radix_tree_root *root)
  730. {
  731. /* try to shrink tree height */
  732. while (root->height > 0 &&
  733. root->rnode->count == 1 &&
  734. root->rnode->slots[0]) {
  735. struct radix_tree_node *to_free = root->rnode;
  736. void *newptr;
  737. /*
  738. * We don't need rcu_assign_pointer(), since we are simply
  739. * moving the node from one part of the tree to another. If
  740. * it was safe to dereference the old pointer to it
  741. * (to_free->slots[0]), it will be safe to dereference the new
  742. * one (root->rnode).
  743. */
  744. newptr = to_free->slots[0];
  745. if (root->height == 1)
  746. newptr = radix_tree_ptr_to_direct(newptr);
  747. root->rnode = newptr;
  748. root->height--;
  749. /* must only free zeroed nodes into the slab */
  750. tag_clear(to_free, 0, 0);
  751. tag_clear(to_free, 1, 0);
  752. to_free->slots[0] = NULL;
  753. to_free->count = 0;
  754. radix_tree_node_free(to_free);
  755. }
  756. }
  757. /**
  758. * radix_tree_delete - delete an item from a radix tree
  759. * @root: radix tree root
  760. * @index: index key
  761. *
  762. * Remove the item at @index from the radix tree rooted at @root.
  763. *
  764. * Returns the address of the deleted item, or NULL if it was not present.
  765. */
  766. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  767. {
  768. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  769. struct radix_tree_node *slot = NULL;
  770. struct radix_tree_node *to_free;
  771. unsigned int height, shift;
  772. int tag;
  773. int offset;
  774. height = root->height;
  775. if (index > radix_tree_maxindex(height))
  776. goto out;
  777. slot = root->rnode;
  778. if (height == 0 && root->rnode) {
  779. slot = radix_tree_direct_to_ptr(slot);
  780. root_tag_clear_all(root);
  781. root->rnode = NULL;
  782. goto out;
  783. }
  784. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  785. pathp->node = NULL;
  786. do {
  787. if (slot == NULL)
  788. goto out;
  789. pathp++;
  790. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  791. pathp->offset = offset;
  792. pathp->node = slot;
  793. slot = slot->slots[offset];
  794. shift -= RADIX_TREE_MAP_SHIFT;
  795. height--;
  796. } while (height > 0);
  797. if (slot == NULL)
  798. goto out;
  799. /*
  800. * Clear all tags associated with the just-deleted item
  801. */
  802. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  803. if (tag_get(pathp->node, tag, pathp->offset))
  804. radix_tree_tag_clear(root, index, tag);
  805. }
  806. to_free = NULL;
  807. /* Now free the nodes we do not need anymore */
  808. while (pathp->node) {
  809. pathp->node->slots[pathp->offset] = NULL;
  810. pathp->node->count--;
  811. /*
  812. * Queue the node for deferred freeing after the
  813. * last reference to it disappears (set NULL, above).
  814. */
  815. if (to_free)
  816. radix_tree_node_free(to_free);
  817. if (pathp->node->count) {
  818. if (pathp->node == root->rnode)
  819. radix_tree_shrink(root);
  820. goto out;
  821. }
  822. /* Node with zero slots in use so free it */
  823. to_free = pathp->node;
  824. pathp--;
  825. }
  826. root_tag_clear_all(root);
  827. root->height = 0;
  828. root->rnode = NULL;
  829. if (to_free)
  830. radix_tree_node_free(to_free);
  831. out:
  832. return slot;
  833. }
  834. EXPORT_SYMBOL(radix_tree_delete);
  835. /**
  836. * radix_tree_tagged - test whether any items in the tree are tagged
  837. * @root: radix tree root
  838. * @tag: tag to test
  839. */
  840. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  841. {
  842. return root_tag_get(root, tag);
  843. }
  844. EXPORT_SYMBOL(radix_tree_tagged);
  845. static void
  846. radix_tree_node_ctor(void *node, struct kmem_cache *cachep, unsigned long flags)
  847. {
  848. memset(node, 0, sizeof(struct radix_tree_node));
  849. }
  850. static __init unsigned long __maxindex(unsigned int height)
  851. {
  852. unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
  853. unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
  854. if (tmp >= RADIX_TREE_INDEX_BITS)
  855. index = ~0UL;
  856. return index;
  857. }
  858. static __init void radix_tree_init_maxindex(void)
  859. {
  860. unsigned int i;
  861. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  862. height_to_maxindex[i] = __maxindex(i);
  863. }
  864. static int radix_tree_callback(struct notifier_block *nfb,
  865. unsigned long action,
  866. void *hcpu)
  867. {
  868. int cpu = (long)hcpu;
  869. struct radix_tree_preload *rtp;
  870. /* Free per-cpu pool of perloaded nodes */
  871. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  872. rtp = &per_cpu(radix_tree_preloads, cpu);
  873. while (rtp->nr) {
  874. kmem_cache_free(radix_tree_node_cachep,
  875. rtp->nodes[rtp->nr-1]);
  876. rtp->nodes[rtp->nr-1] = NULL;
  877. rtp->nr--;
  878. }
  879. }
  880. return NOTIFY_OK;
  881. }
  882. void __init radix_tree_init(void)
  883. {
  884. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  885. sizeof(struct radix_tree_node), 0,
  886. SLAB_PANIC, radix_tree_node_ctor);
  887. radix_tree_init_maxindex();
  888. hotcpu_notifier(radix_tree_callback, 0);
  889. }