radix-tree.c 31 KB

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