radix-tree.c 27 KB

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