radix-tree.c 32 KB

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