radix-tree.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. /**
  513. * radix_tree_next_hole - find the next hole (not-present entry)
  514. * @root: tree root
  515. * @index: index key
  516. * @max_scan: maximum range to search
  517. *
  518. * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
  519. * indexed hole.
  520. *
  521. * Returns: the index of the hole if found, otherwise returns an index
  522. * outside of the set specified (in which case 'return - index >= max_scan'
  523. * will be true).
  524. *
  525. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  526. * radix_tree_gang_lookup, this will not atomically search a snapshot of the
  527. * tree at a single point in time. For example, if a hole is created at index
  528. * 5, then subsequently a hole is created at index 10, radix_tree_next_hole
  529. * covering both indexes may return 10 if called under rcu_read_lock.
  530. */
  531. unsigned long radix_tree_next_hole(struct radix_tree_root *root,
  532. unsigned long index, unsigned long max_scan)
  533. {
  534. unsigned long i;
  535. for (i = 0; i < max_scan; i++) {
  536. if (!radix_tree_lookup(root, index))
  537. break;
  538. index++;
  539. if (index == 0)
  540. break;
  541. }
  542. return index;
  543. }
  544. EXPORT_SYMBOL(radix_tree_next_hole);
  545. static unsigned int
  546. __lookup(struct radix_tree_node *slot, void **results, unsigned long index,
  547. unsigned int max_items, unsigned long *next_index)
  548. {
  549. unsigned int nr_found = 0;
  550. unsigned int shift, height;
  551. unsigned long i;
  552. height = slot->height;
  553. if (height == 0)
  554. goto out;
  555. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  556. for ( ; height > 1; height--) {
  557. i = (index >> shift) & RADIX_TREE_MAP_MASK;
  558. for (;;) {
  559. if (slot->slots[i] != NULL)
  560. break;
  561. index &= ~((1UL << shift) - 1);
  562. index += 1UL << shift;
  563. if (index == 0)
  564. goto out; /* 32-bit wraparound */
  565. i++;
  566. if (i == RADIX_TREE_MAP_SIZE)
  567. goto out;
  568. }
  569. shift -= RADIX_TREE_MAP_SHIFT;
  570. slot = rcu_dereference(slot->slots[i]);
  571. if (slot == NULL)
  572. goto out;
  573. }
  574. /* Bottom level: grab some items */
  575. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  576. struct radix_tree_node *node;
  577. index++;
  578. node = slot->slots[i];
  579. if (node) {
  580. results[nr_found++] = rcu_dereference(node);
  581. if (nr_found == max_items)
  582. goto out;
  583. }
  584. }
  585. out:
  586. *next_index = index;
  587. return nr_found;
  588. }
  589. /**
  590. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  591. * @root: radix tree root
  592. * @results: where the results of the lookup are placed
  593. * @first_index: start the lookup from this key
  594. * @max_items: place up to this many items at *results
  595. *
  596. * Performs an index-ascending scan of the tree for present items. Places
  597. * them at *@results and returns the number of items which were placed at
  598. * *@results.
  599. *
  600. * The implementation is naive.
  601. *
  602. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  603. * rcu_read_lock. In this case, rather than the returned results being
  604. * an atomic snapshot of the tree at a single point in time, the semantics
  605. * of an RCU protected gang lookup are as though multiple radix_tree_lookups
  606. * have been issued in individual locks, and results stored in 'results'.
  607. */
  608. unsigned int
  609. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  610. unsigned long first_index, unsigned int max_items)
  611. {
  612. unsigned long max_index;
  613. struct radix_tree_node *node;
  614. unsigned long cur_index = first_index;
  615. unsigned int ret;
  616. node = rcu_dereference(root->rnode);
  617. if (!node)
  618. return 0;
  619. if (radix_tree_is_direct_ptr(node)) {
  620. if (first_index > 0)
  621. return 0;
  622. node = radix_tree_direct_to_ptr(node);
  623. results[0] = rcu_dereference(node);
  624. return 1;
  625. }
  626. max_index = radix_tree_maxindex(node->height);
  627. ret = 0;
  628. while (ret < max_items) {
  629. unsigned int nr_found;
  630. unsigned long next_index; /* Index of next search */
  631. if (cur_index > max_index)
  632. break;
  633. nr_found = __lookup(node, results + ret, cur_index,
  634. max_items - ret, &next_index);
  635. ret += nr_found;
  636. if (next_index == 0)
  637. break;
  638. cur_index = next_index;
  639. }
  640. return ret;
  641. }
  642. EXPORT_SYMBOL(radix_tree_gang_lookup);
  643. /*
  644. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  645. * open-coding the search.
  646. */
  647. static unsigned int
  648. __lookup_tag(struct radix_tree_node *slot, void **results, unsigned long index,
  649. unsigned int max_items, unsigned long *next_index, unsigned int tag)
  650. {
  651. unsigned int nr_found = 0;
  652. unsigned int shift, height;
  653. height = slot->height;
  654. if (height == 0)
  655. goto out;
  656. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  657. while (height > 0) {
  658. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  659. for (;;) {
  660. if (tag_get(slot, tag, i))
  661. break;
  662. index &= ~((1UL << shift) - 1);
  663. index += 1UL << shift;
  664. if (index == 0)
  665. goto out; /* 32-bit wraparound */
  666. i++;
  667. if (i == RADIX_TREE_MAP_SIZE)
  668. goto out;
  669. }
  670. height--;
  671. if (height == 0) { /* Bottom level: grab some items */
  672. unsigned long j = index & RADIX_TREE_MAP_MASK;
  673. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  674. struct radix_tree_node *node;
  675. index++;
  676. if (!tag_get(slot, tag, j))
  677. continue;
  678. node = slot->slots[j];
  679. /*
  680. * Even though the tag was found set, we need to
  681. * recheck that we have a non-NULL node, because
  682. * if this lookup is lockless, it may have been
  683. * subsequently deleted.
  684. *
  685. * Similar care must be taken in any place that
  686. * lookup ->slots[x] without a lock (ie. can't
  687. * rely on its value remaining the same).
  688. */
  689. if (node) {
  690. node = rcu_dereference(node);
  691. results[nr_found++] = node;
  692. if (nr_found == max_items)
  693. goto out;
  694. }
  695. }
  696. }
  697. shift -= RADIX_TREE_MAP_SHIFT;
  698. slot = rcu_dereference(slot->slots[i]);
  699. if (slot == NULL)
  700. break;
  701. }
  702. out:
  703. *next_index = index;
  704. return nr_found;
  705. }
  706. /**
  707. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  708. * based on a tag
  709. * @root: radix tree root
  710. * @results: where the results of the lookup are placed
  711. * @first_index: start the lookup from this key
  712. * @max_items: place up to this many items at *results
  713. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  714. *
  715. * Performs an index-ascending scan of the tree for present items which
  716. * have the tag indexed by @tag set. Places the items at *@results and
  717. * returns the number of items which were placed at *@results.
  718. */
  719. unsigned int
  720. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  721. unsigned long first_index, unsigned int max_items,
  722. unsigned int tag)
  723. {
  724. struct radix_tree_node *node;
  725. unsigned long max_index;
  726. unsigned long cur_index = first_index;
  727. unsigned int ret;
  728. /* check the root's tag bit */
  729. if (!root_tag_get(root, tag))
  730. return 0;
  731. node = rcu_dereference(root->rnode);
  732. if (!node)
  733. return 0;
  734. if (radix_tree_is_direct_ptr(node)) {
  735. if (first_index > 0)
  736. return 0;
  737. node = radix_tree_direct_to_ptr(node);
  738. results[0] = rcu_dereference(node);
  739. return 1;
  740. }
  741. max_index = radix_tree_maxindex(node->height);
  742. ret = 0;
  743. while (ret < max_items) {
  744. unsigned int nr_found;
  745. unsigned long next_index; /* Index of next search */
  746. if (cur_index > max_index)
  747. break;
  748. nr_found = __lookup_tag(node, results + ret, cur_index,
  749. max_items - ret, &next_index, tag);
  750. ret += nr_found;
  751. if (next_index == 0)
  752. break;
  753. cur_index = next_index;
  754. }
  755. return ret;
  756. }
  757. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  758. /**
  759. * radix_tree_shrink - shrink height of a radix tree to minimal
  760. * @root radix tree root
  761. */
  762. static inline void radix_tree_shrink(struct radix_tree_root *root)
  763. {
  764. /* try to shrink tree height */
  765. while (root->height > 0 &&
  766. root->rnode->count == 1 &&
  767. root->rnode->slots[0]) {
  768. struct radix_tree_node *to_free = root->rnode;
  769. void *newptr;
  770. /*
  771. * We don't need rcu_assign_pointer(), since we are simply
  772. * moving the node from one part of the tree to another. If
  773. * it was safe to dereference the old pointer to it
  774. * (to_free->slots[0]), it will be safe to dereference the new
  775. * one (root->rnode).
  776. */
  777. newptr = to_free->slots[0];
  778. if (root->height == 1)
  779. newptr = radix_tree_ptr_to_direct(newptr);
  780. root->rnode = newptr;
  781. root->height--;
  782. /* must only free zeroed nodes into the slab */
  783. tag_clear(to_free, 0, 0);
  784. tag_clear(to_free, 1, 0);
  785. to_free->slots[0] = NULL;
  786. to_free->count = 0;
  787. radix_tree_node_free(to_free);
  788. }
  789. }
  790. /**
  791. * radix_tree_delete - delete an item from a radix tree
  792. * @root: radix tree root
  793. * @index: index key
  794. *
  795. * Remove the item at @index from the radix tree rooted at @root.
  796. *
  797. * Returns the address of the deleted item, or NULL if it was not present.
  798. */
  799. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  800. {
  801. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  802. struct radix_tree_node *slot = NULL;
  803. struct radix_tree_node *to_free;
  804. unsigned int height, shift;
  805. int tag;
  806. int offset;
  807. height = root->height;
  808. if (index > radix_tree_maxindex(height))
  809. goto out;
  810. slot = root->rnode;
  811. if (height == 0 && root->rnode) {
  812. slot = radix_tree_direct_to_ptr(slot);
  813. root_tag_clear_all(root);
  814. root->rnode = NULL;
  815. goto out;
  816. }
  817. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  818. pathp->node = NULL;
  819. do {
  820. if (slot == NULL)
  821. goto out;
  822. pathp++;
  823. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  824. pathp->offset = offset;
  825. pathp->node = slot;
  826. slot = slot->slots[offset];
  827. shift -= RADIX_TREE_MAP_SHIFT;
  828. height--;
  829. } while (height > 0);
  830. if (slot == NULL)
  831. goto out;
  832. /*
  833. * Clear all tags associated with the just-deleted item
  834. */
  835. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  836. if (tag_get(pathp->node, tag, pathp->offset))
  837. radix_tree_tag_clear(root, index, tag);
  838. }
  839. to_free = NULL;
  840. /* Now free the nodes we do not need anymore */
  841. while (pathp->node) {
  842. pathp->node->slots[pathp->offset] = NULL;
  843. pathp->node->count--;
  844. /*
  845. * Queue the node for deferred freeing after the
  846. * last reference to it disappears (set NULL, above).
  847. */
  848. if (to_free)
  849. radix_tree_node_free(to_free);
  850. if (pathp->node->count) {
  851. if (pathp->node == root->rnode)
  852. radix_tree_shrink(root);
  853. goto out;
  854. }
  855. /* Node with zero slots in use so free it */
  856. to_free = pathp->node;
  857. pathp--;
  858. }
  859. root_tag_clear_all(root);
  860. root->height = 0;
  861. root->rnode = NULL;
  862. if (to_free)
  863. radix_tree_node_free(to_free);
  864. out:
  865. return slot;
  866. }
  867. EXPORT_SYMBOL(radix_tree_delete);
  868. /**
  869. * radix_tree_tagged - test whether any items in the tree are tagged
  870. * @root: radix tree root
  871. * @tag: tag to test
  872. */
  873. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  874. {
  875. return root_tag_get(root, tag);
  876. }
  877. EXPORT_SYMBOL(radix_tree_tagged);
  878. static void
  879. radix_tree_node_ctor(void *node, struct kmem_cache *cachep, unsigned long flags)
  880. {
  881. memset(node, 0, sizeof(struct radix_tree_node));
  882. }
  883. static __init unsigned long __maxindex(unsigned int height)
  884. {
  885. unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
  886. unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
  887. if (tmp >= RADIX_TREE_INDEX_BITS)
  888. index = ~0UL;
  889. return index;
  890. }
  891. static __init void radix_tree_init_maxindex(void)
  892. {
  893. unsigned int i;
  894. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  895. height_to_maxindex[i] = __maxindex(i);
  896. }
  897. static int radix_tree_callback(struct notifier_block *nfb,
  898. unsigned long action,
  899. void *hcpu)
  900. {
  901. int cpu = (long)hcpu;
  902. struct radix_tree_preload *rtp;
  903. /* Free per-cpu pool of perloaded nodes */
  904. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  905. rtp = &per_cpu(radix_tree_preloads, cpu);
  906. while (rtp->nr) {
  907. kmem_cache_free(radix_tree_node_cachep,
  908. rtp->nodes[rtp->nr-1]);
  909. rtp->nodes[rtp->nr-1] = NULL;
  910. rtp->nr--;
  911. }
  912. }
  913. return NOTIFY_OK;
  914. }
  915. void __init radix_tree_init(void)
  916. {
  917. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  918. sizeof(struct radix_tree_node), 0,
  919. SLAB_PANIC, radix_tree_node_ctor);
  920. radix_tree_init_maxindex();
  921. hotcpu_notifier(radix_tree_callback, 0);
  922. }