radix-tree.c 27 KB

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