radix-tree.c 27 KB

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