radix-tree.c 31 KB

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