radix-tree.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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/string.h>
  31. #include <linux/bitops.h>
  32. #include <linux/rcupdate.h>
  33. #ifdef __KERNEL__
  34. #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  35. #else
  36. #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
  37. #endif
  38. #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
  39. #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
  40. #define RADIX_TREE_TAG_LONGS \
  41. ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
  42. struct radix_tree_node {
  43. unsigned int height; /* Height from the bottom */
  44. unsigned int count;
  45. struct rcu_head rcu_head;
  46. void __rcu *slots[RADIX_TREE_MAP_SIZE];
  47. unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
  48. };
  49. struct radix_tree_path {
  50. struct radix_tree_node *node;
  51. int offset;
  52. };
  53. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  54. #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
  55. RADIX_TREE_MAP_SHIFT))
  56. /*
  57. * The height_to_maxindex array needs to be one deeper than the maximum
  58. * path as height 0 holds only 1 entry.
  59. */
  60. static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
  61. /*
  62. * Radix tree node cache.
  63. */
  64. static struct kmem_cache *radix_tree_node_cachep;
  65. /*
  66. * Per-cpu pool of preloaded nodes
  67. */
  68. struct radix_tree_preload {
  69. int nr;
  70. struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
  71. };
  72. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  73. static inline void *ptr_to_indirect(void *ptr)
  74. {
  75. return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
  76. }
  77. static inline void *indirect_to_ptr(void *ptr)
  78. {
  79. return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
  80. }
  81. static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
  82. {
  83. return root->gfp_mask & __GFP_BITS_MASK;
  84. }
  85. static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
  86. int offset)
  87. {
  88. __set_bit(offset, node->tags[tag]);
  89. }
  90. static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
  91. int offset)
  92. {
  93. __clear_bit(offset, node->tags[tag]);
  94. }
  95. static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
  96. int offset)
  97. {
  98. return test_bit(offset, node->tags[tag]);
  99. }
  100. static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
  101. {
  102. root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
  103. }
  104. static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
  105. {
  106. root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
  107. }
  108. static inline void root_tag_clear_all(struct radix_tree_root *root)
  109. {
  110. root->gfp_mask &= __GFP_BITS_MASK;
  111. }
  112. static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
  113. {
  114. return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
  115. }
  116. /*
  117. * Returns 1 if any slot in the node has this tag set.
  118. * Otherwise returns 0.
  119. */
  120. static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
  121. {
  122. int idx;
  123. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  124. if (node->tags[tag][idx])
  125. return 1;
  126. }
  127. return 0;
  128. }
  129. /*
  130. * This assumes that the caller has performed appropriate preallocation, and
  131. * that the caller has pinned this thread of control to the current CPU.
  132. */
  133. static struct radix_tree_node *
  134. radix_tree_node_alloc(struct radix_tree_root *root)
  135. {
  136. struct radix_tree_node *ret = NULL;
  137. gfp_t gfp_mask = root_gfp_mask(root);
  138. if (!(gfp_mask & __GFP_WAIT)) {
  139. struct radix_tree_preload *rtp;
  140. /*
  141. * Provided the caller has preloaded here, we will always
  142. * succeed in getting a node here (and never reach
  143. * kmem_cache_alloc)
  144. */
  145. rtp = &__get_cpu_var(radix_tree_preloads);
  146. if (rtp->nr) {
  147. ret = rtp->nodes[rtp->nr - 1];
  148. rtp->nodes[rtp->nr - 1] = NULL;
  149. rtp->nr--;
  150. }
  151. }
  152. if (ret == NULL)
  153. ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  154. BUG_ON(radix_tree_is_indirect_ptr(ret));
  155. return ret;
  156. }
  157. static void radix_tree_node_rcu_free(struct rcu_head *head)
  158. {
  159. struct radix_tree_node *node =
  160. container_of(head, struct radix_tree_node, rcu_head);
  161. int i;
  162. /*
  163. * must only free zeroed nodes into the slab. radix_tree_shrink
  164. * can leave us with a non-NULL entry in the first slot, so clear
  165. * that here to make sure.
  166. */
  167. for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
  168. tag_clear(node, i, 0);
  169. node->slots[0] = NULL;
  170. node->count = 0;
  171. kmem_cache_free(radix_tree_node_cachep, node);
  172. }
  173. static inline void
  174. radix_tree_node_free(struct radix_tree_node *node)
  175. {
  176. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  177. }
  178. /*
  179. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  180. * ensure that the addition of a single element in the tree cannot fail. On
  181. * success, return zero, with preemption disabled. On error, return -ENOMEM
  182. * with preemption not disabled.
  183. *
  184. * To make use of this facility, the radix tree must be initialised without
  185. * __GFP_WAIT being passed to INIT_RADIX_TREE().
  186. */
  187. int radix_tree_preload(gfp_t gfp_mask)
  188. {
  189. struct radix_tree_preload *rtp;
  190. struct radix_tree_node *node;
  191. int ret = -ENOMEM;
  192. preempt_disable();
  193. rtp = &__get_cpu_var(radix_tree_preloads);
  194. while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
  195. preempt_enable();
  196. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  197. if (node == NULL)
  198. goto out;
  199. preempt_disable();
  200. rtp = &__get_cpu_var(radix_tree_preloads);
  201. if (rtp->nr < ARRAY_SIZE(rtp->nodes))
  202. rtp->nodes[rtp->nr++] = node;
  203. else
  204. kmem_cache_free(radix_tree_node_cachep, node);
  205. }
  206. ret = 0;
  207. out:
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(radix_tree_preload);
  211. /*
  212. * Return the maximum key which can be store into a
  213. * radix tree with height HEIGHT.
  214. */
  215. static inline unsigned long radix_tree_maxindex(unsigned int height)
  216. {
  217. return height_to_maxindex[height];
  218. }
  219. /*
  220. * Extend a radix tree so it can store key @index.
  221. */
  222. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  223. {
  224. struct radix_tree_node *node;
  225. unsigned int height;
  226. int tag;
  227. /* Figure out what the height should be. */
  228. height = root->height + 1;
  229. while (index > radix_tree_maxindex(height))
  230. height++;
  231. if (root->rnode == NULL) {
  232. root->height = height;
  233. goto out;
  234. }
  235. do {
  236. unsigned int newheight;
  237. if (!(node = radix_tree_node_alloc(root)))
  238. return -ENOMEM;
  239. /* Increase the height. */
  240. node->slots[0] = indirect_to_ptr(root->rnode);
  241. /* Propagate the aggregated tag info into the new root */
  242. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  243. if (root_tag_get(root, tag))
  244. tag_set(node, tag, 0);
  245. }
  246. newheight = root->height+1;
  247. node->height = newheight;
  248. node->count = 1;
  249. node = ptr_to_indirect(node);
  250. rcu_assign_pointer(root->rnode, node);
  251. root->height = newheight;
  252. } while (height > root->height);
  253. out:
  254. return 0;
  255. }
  256. /**
  257. * radix_tree_insert - insert into a radix tree
  258. * @root: radix tree root
  259. * @index: index key
  260. * @item: item to insert
  261. *
  262. * Insert an item into the radix tree at position @index.
  263. */
  264. int radix_tree_insert(struct radix_tree_root *root,
  265. unsigned long index, void *item)
  266. {
  267. struct radix_tree_node *node = NULL, *slot;
  268. unsigned int height, shift;
  269. int offset;
  270. int error;
  271. BUG_ON(radix_tree_is_indirect_ptr(item));
  272. /* Make sure the tree is high enough. */
  273. if (index > radix_tree_maxindex(root->height)) {
  274. error = radix_tree_extend(root, index);
  275. if (error)
  276. return error;
  277. }
  278. slot = indirect_to_ptr(root->rnode);
  279. height = root->height;
  280. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  281. offset = 0; /* uninitialised var warning */
  282. while (height > 0) {
  283. if (slot == NULL) {
  284. /* Have to add a child node. */
  285. if (!(slot = radix_tree_node_alloc(root)))
  286. return -ENOMEM;
  287. slot->height = height;
  288. if (node) {
  289. rcu_assign_pointer(node->slots[offset], slot);
  290. node->count++;
  291. } else
  292. rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
  293. }
  294. /* Go a level down */
  295. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  296. node = slot;
  297. slot = node->slots[offset];
  298. shift -= RADIX_TREE_MAP_SHIFT;
  299. height--;
  300. }
  301. if (slot != NULL)
  302. return -EEXIST;
  303. if (node) {
  304. node->count++;
  305. rcu_assign_pointer(node->slots[offset], item);
  306. BUG_ON(tag_get(node, 0, offset));
  307. BUG_ON(tag_get(node, 1, offset));
  308. } else {
  309. rcu_assign_pointer(root->rnode, item);
  310. BUG_ON(root_tag_get(root, 0));
  311. BUG_ON(root_tag_get(root, 1));
  312. }
  313. return 0;
  314. }
  315. EXPORT_SYMBOL(radix_tree_insert);
  316. /*
  317. * is_slot == 1 : search for the slot.
  318. * is_slot == 0 : search for the node.
  319. */
  320. static void *radix_tree_lookup_element(struct radix_tree_root *root,
  321. unsigned long index, int is_slot)
  322. {
  323. unsigned int height, shift;
  324. struct radix_tree_node *node, **slot;
  325. node = rcu_dereference_raw(root->rnode);
  326. if (node == NULL)
  327. return NULL;
  328. if (!radix_tree_is_indirect_ptr(node)) {
  329. if (index > 0)
  330. return NULL;
  331. return is_slot ? (void *)&root->rnode : node;
  332. }
  333. node = indirect_to_ptr(node);
  334. height = node->height;
  335. if (index > radix_tree_maxindex(height))
  336. return NULL;
  337. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  338. do {
  339. slot = (struct radix_tree_node **)
  340. (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
  341. node = rcu_dereference_raw(*slot);
  342. if (node == NULL)
  343. return NULL;
  344. shift -= RADIX_TREE_MAP_SHIFT;
  345. height--;
  346. } while (height > 0);
  347. return is_slot ? (void *)slot : indirect_to_ptr(node);
  348. }
  349. /**
  350. * radix_tree_lookup_slot - lookup a slot in a radix tree
  351. * @root: radix tree root
  352. * @index: index key
  353. *
  354. * Returns: the slot corresponding to the position @index in the
  355. * radix tree @root. This is useful for update-if-exists operations.
  356. *
  357. * This function can be called under rcu_read_lock iff the slot is not
  358. * modified by radix_tree_replace_slot, otherwise it must be called
  359. * exclusive from other writers. Any dereference of the slot must be done
  360. * using radix_tree_deref_slot.
  361. */
  362. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  363. {
  364. return (void **)radix_tree_lookup_element(root, index, 1);
  365. }
  366. EXPORT_SYMBOL(radix_tree_lookup_slot);
  367. /**
  368. * radix_tree_lookup - perform lookup operation on a radix tree
  369. * @root: radix tree root
  370. * @index: index key
  371. *
  372. * Lookup the item at the position @index in the radix tree @root.
  373. *
  374. * This function can be called under rcu_read_lock, however the caller
  375. * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
  376. * them safely). No RCU barriers are required to access or modify the
  377. * returned item, however.
  378. */
  379. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  380. {
  381. return radix_tree_lookup_element(root, index, 0);
  382. }
  383. EXPORT_SYMBOL(radix_tree_lookup);
  384. /**
  385. * radix_tree_tag_set - set a tag on a radix tree node
  386. * @root: radix tree root
  387. * @index: index key
  388. * @tag: tag index
  389. *
  390. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  391. * corresponding to @index in the radix tree. From
  392. * the root all the way down to the leaf node.
  393. *
  394. * Returns the address of the tagged item. Setting a tag on a not-present
  395. * item is a bug.
  396. */
  397. void *radix_tree_tag_set(struct radix_tree_root *root,
  398. unsigned long index, unsigned int tag)
  399. {
  400. unsigned int height, shift;
  401. struct radix_tree_node *slot;
  402. height = root->height;
  403. BUG_ON(index > radix_tree_maxindex(height));
  404. slot = indirect_to_ptr(root->rnode);
  405. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  406. while (height > 0) {
  407. int offset;
  408. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  409. if (!tag_get(slot, tag, offset))
  410. tag_set(slot, tag, offset);
  411. slot = slot->slots[offset];
  412. BUG_ON(slot == NULL);
  413. shift -= RADIX_TREE_MAP_SHIFT;
  414. height--;
  415. }
  416. /* set the root's tag bit */
  417. if (slot && !root_tag_get(root, tag))
  418. root_tag_set(root, tag);
  419. return slot;
  420. }
  421. EXPORT_SYMBOL(radix_tree_tag_set);
  422. /**
  423. * radix_tree_tag_clear - clear a tag on a radix tree node
  424. * @root: radix tree root
  425. * @index: index key
  426. * @tag: tag index
  427. *
  428. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  429. * corresponding to @index in the radix tree. If
  430. * this causes the leaf node to have no tags set then clear the tag in the
  431. * next-to-leaf node, etc.
  432. *
  433. * Returns the address of the tagged item on success, else NULL. ie:
  434. * has the same return value and semantics as radix_tree_lookup().
  435. */
  436. void *radix_tree_tag_clear(struct radix_tree_root *root,
  437. unsigned long index, unsigned int tag)
  438. {
  439. /*
  440. * The radix tree path needs to be one longer than the maximum path
  441. * since the "list" is null terminated.
  442. */
  443. struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
  444. struct radix_tree_node *slot = NULL;
  445. unsigned int height, shift;
  446. height = root->height;
  447. if (index > radix_tree_maxindex(height))
  448. goto out;
  449. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  450. pathp->node = NULL;
  451. slot = indirect_to_ptr(root->rnode);
  452. while (height > 0) {
  453. int offset;
  454. if (slot == NULL)
  455. goto out;
  456. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  457. pathp[1].offset = offset;
  458. pathp[1].node = slot;
  459. slot = slot->slots[offset];
  460. pathp++;
  461. shift -= RADIX_TREE_MAP_SHIFT;
  462. height--;
  463. }
  464. if (slot == NULL)
  465. goto out;
  466. while (pathp->node) {
  467. if (!tag_get(pathp->node, tag, pathp->offset))
  468. goto out;
  469. tag_clear(pathp->node, tag, pathp->offset);
  470. if (any_tag_set(pathp->node, tag))
  471. goto out;
  472. pathp--;
  473. }
  474. /* clear the root's tag bit */
  475. if (root_tag_get(root, tag))
  476. root_tag_clear(root, tag);
  477. out:
  478. return slot;
  479. }
  480. EXPORT_SYMBOL(radix_tree_tag_clear);
  481. /**
  482. * radix_tree_tag_get - get a tag on a radix tree node
  483. * @root: radix tree root
  484. * @index: index key
  485. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  486. *
  487. * Return values:
  488. *
  489. * 0: tag not present or not set
  490. * 1: tag set
  491. *
  492. * Note that the return value of this function may not be relied on, even if
  493. * the RCU lock is held, unless tag modification and node deletion are excluded
  494. * from concurrency.
  495. */
  496. int radix_tree_tag_get(struct radix_tree_root *root,
  497. unsigned long index, unsigned int tag)
  498. {
  499. unsigned int height, shift;
  500. struct radix_tree_node *node;
  501. int saw_unset_tag = 0;
  502. /* check the root's tag bit */
  503. if (!root_tag_get(root, tag))
  504. return 0;
  505. node = rcu_dereference_raw(root->rnode);
  506. if (node == NULL)
  507. return 0;
  508. if (!radix_tree_is_indirect_ptr(node))
  509. return (index == 0);
  510. node = indirect_to_ptr(node);
  511. height = node->height;
  512. if (index > radix_tree_maxindex(height))
  513. return 0;
  514. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  515. for ( ; ; ) {
  516. int offset;
  517. if (node == NULL)
  518. return 0;
  519. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  520. /*
  521. * This is just a debug check. Later, we can bale as soon as
  522. * we see an unset tag.
  523. */
  524. if (!tag_get(node, tag, offset))
  525. saw_unset_tag = 1;
  526. if (height == 1)
  527. return !!tag_get(node, tag, offset);
  528. node = rcu_dereference_raw(node->slots[offset]);
  529. shift -= RADIX_TREE_MAP_SHIFT;
  530. height--;
  531. }
  532. }
  533. EXPORT_SYMBOL(radix_tree_tag_get);
  534. /**
  535. * radix_tree_range_tag_if_tagged - for each item in given range set given
  536. * tag if item has another tag set
  537. * @root: radix tree root
  538. * @first_indexp: pointer to a starting index of a range to scan
  539. * @last_index: last index of a range to scan
  540. * @nr_to_tag: maximum number items to tag
  541. * @iftag: tag index to test
  542. * @settag: tag index to set if tested tag is set
  543. *
  544. * This function scans range of radix tree from first_index to last_index
  545. * (inclusive). For each item in the range if iftag is set, the function sets
  546. * also settag. The function stops either after tagging nr_to_tag items or
  547. * after reaching last_index.
  548. *
  549. * The tags must be set from the leaf level only and propagated back up the
  550. * path to the root. We must do this so that we resolve the full path before
  551. * setting any tags on intermediate nodes. If we set tags as we descend, then
  552. * we can get to the leaf node and find that the index that has the iftag
  553. * set is outside the range we are scanning. This reults in dangling tags and
  554. * can lead to problems with later tag operations (e.g. livelocks on lookups).
  555. *
  556. * The function returns number of leaves where the tag was set and sets
  557. * *first_indexp to the first unscanned index.
  558. * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
  559. * be prepared to handle that.
  560. */
  561. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  562. unsigned long *first_indexp, unsigned long last_index,
  563. unsigned long nr_to_tag,
  564. unsigned int iftag, unsigned int settag)
  565. {
  566. unsigned int height = root->height;
  567. struct radix_tree_path path[height];
  568. struct radix_tree_path *pathp = path;
  569. struct radix_tree_node *slot;
  570. unsigned int shift;
  571. unsigned long tagged = 0;
  572. unsigned long index = *first_indexp;
  573. last_index = min(last_index, radix_tree_maxindex(height));
  574. if (index > last_index)
  575. return 0;
  576. if (!nr_to_tag)
  577. return 0;
  578. if (!root_tag_get(root, iftag)) {
  579. *first_indexp = last_index + 1;
  580. return 0;
  581. }
  582. if (height == 0) {
  583. *first_indexp = last_index + 1;
  584. root_tag_set(root, settag);
  585. return 1;
  586. }
  587. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  588. slot = indirect_to_ptr(root->rnode);
  589. /*
  590. * we fill the path from (root->height - 2) to 0, leaving the index at
  591. * (root->height - 1) as a terminator. Zero the node in the terminator
  592. * so that we can use this to end walk loops back up the path.
  593. */
  594. path[height - 1].node = NULL;
  595. for (;;) {
  596. int offset;
  597. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  598. if (!slot->slots[offset])
  599. goto next;
  600. if (!tag_get(slot, iftag, offset))
  601. goto next;
  602. if (height > 1) {
  603. /* Go down one level */
  604. height--;
  605. shift -= RADIX_TREE_MAP_SHIFT;
  606. path[height - 1].node = slot;
  607. path[height - 1].offset = offset;
  608. slot = slot->slots[offset];
  609. continue;
  610. }
  611. /* tag the leaf */
  612. tagged++;
  613. tag_set(slot, settag, offset);
  614. /* walk back up the path tagging interior nodes */
  615. pathp = &path[0];
  616. while (pathp->node) {
  617. /* stop if we find a node with the tag already set */
  618. if (tag_get(pathp->node, settag, pathp->offset))
  619. break;
  620. tag_set(pathp->node, settag, pathp->offset);
  621. pathp++;
  622. }
  623. next:
  624. /* Go to next item at level determined by 'shift' */
  625. index = ((index >> shift) + 1) << shift;
  626. /* Overflow can happen when last_index is ~0UL... */
  627. if (index > last_index || !index)
  628. break;
  629. if (tagged >= nr_to_tag)
  630. break;
  631. while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
  632. /*
  633. * We've fully scanned this node. Go up. Because
  634. * last_index is guaranteed to be in the tree, what
  635. * we do below cannot wander astray.
  636. */
  637. slot = path[height - 1].node;
  638. height++;
  639. shift += RADIX_TREE_MAP_SHIFT;
  640. }
  641. }
  642. /*
  643. * The iftag must have been set somewhere because otherwise
  644. * we would return immediated at the beginning of the function
  645. */
  646. root_tag_set(root, settag);
  647. *first_indexp = index;
  648. return tagged;
  649. }
  650. EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
  651. /**
  652. * radix_tree_next_hole - find the next hole (not-present entry)
  653. * @root: tree root
  654. * @index: index key
  655. * @max_scan: maximum range to search
  656. *
  657. * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
  658. * indexed hole.
  659. *
  660. * Returns: the index of the hole if found, otherwise returns an index
  661. * outside of the set specified (in which case 'return - index >= max_scan'
  662. * will be true). In rare cases of index wrap-around, 0 will be returned.
  663. *
  664. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  665. * radix_tree_gang_lookup, this will not atomically search a snapshot of
  666. * the tree at a single point in time. For example, if a hole is created
  667. * at index 5, then subsequently a hole is created at index 10,
  668. * radix_tree_next_hole covering both indexes may return 10 if called
  669. * under rcu_read_lock.
  670. */
  671. unsigned long radix_tree_next_hole(struct radix_tree_root *root,
  672. unsigned long index, unsigned long max_scan)
  673. {
  674. unsigned long i;
  675. for (i = 0; i < max_scan; i++) {
  676. if (!radix_tree_lookup(root, index))
  677. break;
  678. index++;
  679. if (index == 0)
  680. break;
  681. }
  682. return index;
  683. }
  684. EXPORT_SYMBOL(radix_tree_next_hole);
  685. /**
  686. * radix_tree_prev_hole - find the prev hole (not-present entry)
  687. * @root: tree root
  688. * @index: index key
  689. * @max_scan: maximum range to search
  690. *
  691. * Search backwards in the range [max(index-max_scan+1, 0), index]
  692. * for the first hole.
  693. *
  694. * Returns: the index of the hole if found, otherwise returns an index
  695. * outside of the set specified (in which case 'index - return >= max_scan'
  696. * will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
  697. *
  698. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  699. * radix_tree_gang_lookup, this will not atomically search a snapshot of
  700. * the tree at a single point in time. For example, if a hole is created
  701. * at index 10, then subsequently a hole is created at index 5,
  702. * radix_tree_prev_hole covering both indexes may return 5 if called under
  703. * rcu_read_lock.
  704. */
  705. unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
  706. unsigned long index, unsigned long max_scan)
  707. {
  708. unsigned long i;
  709. for (i = 0; i < max_scan; i++) {
  710. if (!radix_tree_lookup(root, index))
  711. break;
  712. index--;
  713. if (index == ULONG_MAX)
  714. break;
  715. }
  716. return index;
  717. }
  718. EXPORT_SYMBOL(radix_tree_prev_hole);
  719. static unsigned int
  720. __lookup(struct radix_tree_node *slot, void ***results, unsigned long index,
  721. unsigned int max_items, unsigned long *next_index)
  722. {
  723. unsigned int nr_found = 0;
  724. unsigned int shift, height;
  725. unsigned long i;
  726. height = slot->height;
  727. if (height == 0)
  728. goto out;
  729. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  730. for ( ; height > 1; height--) {
  731. i = (index >> shift) & RADIX_TREE_MAP_MASK;
  732. for (;;) {
  733. if (slot->slots[i] != NULL)
  734. break;
  735. index &= ~((1UL << shift) - 1);
  736. index += 1UL << shift;
  737. if (index == 0)
  738. goto out; /* 32-bit wraparound */
  739. i++;
  740. if (i == RADIX_TREE_MAP_SIZE)
  741. goto out;
  742. }
  743. shift -= RADIX_TREE_MAP_SHIFT;
  744. slot = rcu_dereference_raw(slot->slots[i]);
  745. if (slot == NULL)
  746. goto out;
  747. }
  748. /* Bottom level: grab some items */
  749. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  750. index++;
  751. if (slot->slots[i]) {
  752. results[nr_found++] = &(slot->slots[i]);
  753. if (nr_found == max_items)
  754. goto out;
  755. }
  756. }
  757. out:
  758. *next_index = index;
  759. return nr_found;
  760. }
  761. /**
  762. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  763. * @root: radix tree root
  764. * @results: where the results of the lookup are placed
  765. * @first_index: start the lookup from this key
  766. * @max_items: place up to this many items at *results
  767. *
  768. * Performs an index-ascending scan of the tree for present items. Places
  769. * them at *@results and returns the number of items which were placed at
  770. * *@results.
  771. *
  772. * The implementation is naive.
  773. *
  774. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  775. * rcu_read_lock. In this case, rather than the returned results being
  776. * an atomic snapshot of the tree at a single point in time, the semantics
  777. * of an RCU protected gang lookup are as though multiple radix_tree_lookups
  778. * have been issued in individual locks, and results stored in 'results'.
  779. */
  780. unsigned int
  781. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  782. unsigned long first_index, unsigned int max_items)
  783. {
  784. unsigned long max_index;
  785. struct radix_tree_node *node;
  786. unsigned long cur_index = first_index;
  787. unsigned int ret;
  788. node = rcu_dereference_raw(root->rnode);
  789. if (!node)
  790. return 0;
  791. if (!radix_tree_is_indirect_ptr(node)) {
  792. if (first_index > 0)
  793. return 0;
  794. results[0] = node;
  795. return 1;
  796. }
  797. node = indirect_to_ptr(node);
  798. max_index = radix_tree_maxindex(node->height);
  799. ret = 0;
  800. while (ret < max_items) {
  801. unsigned int nr_found, slots_found, i;
  802. unsigned long next_index; /* Index of next search */
  803. if (cur_index > max_index)
  804. break;
  805. slots_found = __lookup(node, (void ***)results + ret, cur_index,
  806. max_items - ret, &next_index);
  807. nr_found = 0;
  808. for (i = 0; i < slots_found; i++) {
  809. struct radix_tree_node *slot;
  810. slot = *(((void ***)results)[ret + i]);
  811. if (!slot)
  812. continue;
  813. results[ret + nr_found] =
  814. indirect_to_ptr(rcu_dereference_raw(slot));
  815. nr_found++;
  816. }
  817. ret += nr_found;
  818. if (next_index == 0)
  819. break;
  820. cur_index = next_index;
  821. }
  822. return ret;
  823. }
  824. EXPORT_SYMBOL(radix_tree_gang_lookup);
  825. /**
  826. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  827. * @root: radix tree root
  828. * @results: where the results of the lookup are placed
  829. * @first_index: start the lookup from this key
  830. * @max_items: place up to this many items at *results
  831. *
  832. * Performs an index-ascending scan of the tree for present items. Places
  833. * their slots at *@results and returns the number of items which were
  834. * placed at *@results.
  835. *
  836. * The implementation is naive.
  837. *
  838. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  839. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  840. * protection, radix_tree_deref_slot may fail requiring a retry.
  841. */
  842. unsigned int
  843. radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
  844. unsigned long first_index, unsigned int max_items)
  845. {
  846. unsigned long max_index;
  847. struct radix_tree_node *node;
  848. unsigned long cur_index = first_index;
  849. unsigned int ret;
  850. node = rcu_dereference_raw(root->rnode);
  851. if (!node)
  852. return 0;
  853. if (!radix_tree_is_indirect_ptr(node)) {
  854. if (first_index > 0)
  855. return 0;
  856. results[0] = (void **)&root->rnode;
  857. return 1;
  858. }
  859. node = indirect_to_ptr(node);
  860. max_index = radix_tree_maxindex(node->height);
  861. ret = 0;
  862. while (ret < max_items) {
  863. unsigned int slots_found;
  864. unsigned long next_index; /* Index of next search */
  865. if (cur_index > max_index)
  866. break;
  867. slots_found = __lookup(node, results + ret, cur_index,
  868. max_items - ret, &next_index);
  869. ret += slots_found;
  870. if (next_index == 0)
  871. break;
  872. cur_index = next_index;
  873. }
  874. return ret;
  875. }
  876. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  877. /*
  878. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  879. * open-coding the search.
  880. */
  881. static unsigned int
  882. __lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
  883. unsigned int max_items, unsigned long *next_index, unsigned int tag)
  884. {
  885. unsigned int nr_found = 0;
  886. unsigned int shift, height;
  887. height = slot->height;
  888. if (height == 0)
  889. goto out;
  890. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  891. while (height > 0) {
  892. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  893. for (;;) {
  894. if (tag_get(slot, tag, i))
  895. break;
  896. index &= ~((1UL << shift) - 1);
  897. index += 1UL << shift;
  898. if (index == 0)
  899. goto out; /* 32-bit wraparound */
  900. i++;
  901. if (i == RADIX_TREE_MAP_SIZE)
  902. goto out;
  903. }
  904. height--;
  905. if (height == 0) { /* Bottom level: grab some items */
  906. unsigned long j = index & RADIX_TREE_MAP_MASK;
  907. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  908. index++;
  909. if (!tag_get(slot, tag, j))
  910. continue;
  911. /*
  912. * Even though the tag was found set, we need to
  913. * recheck that we have a non-NULL node, because
  914. * if this lookup is lockless, it may have been
  915. * subsequently deleted.
  916. *
  917. * Similar care must be taken in any place that
  918. * lookup ->slots[x] without a lock (ie. can't
  919. * rely on its value remaining the same).
  920. */
  921. if (slot->slots[j]) {
  922. results[nr_found++] = &(slot->slots[j]);
  923. if (nr_found == max_items)
  924. goto out;
  925. }
  926. }
  927. }
  928. shift -= RADIX_TREE_MAP_SHIFT;
  929. slot = rcu_dereference_raw(slot->slots[i]);
  930. if (slot == NULL)
  931. break;
  932. }
  933. out:
  934. *next_index = index;
  935. return nr_found;
  936. }
  937. /**
  938. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  939. * based on a tag
  940. * @root: radix tree root
  941. * @results: where the results of the lookup are placed
  942. * @first_index: start the lookup from this key
  943. * @max_items: place up to this many items at *results
  944. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  945. *
  946. * Performs an index-ascending scan of the tree for present items which
  947. * have the tag indexed by @tag set. Places the items at *@results and
  948. * returns the number of items which were placed at *@results.
  949. */
  950. unsigned int
  951. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  952. unsigned long first_index, unsigned int max_items,
  953. unsigned int tag)
  954. {
  955. struct radix_tree_node *node;
  956. unsigned long max_index;
  957. unsigned long cur_index = first_index;
  958. unsigned int ret;
  959. /* check the root's tag bit */
  960. if (!root_tag_get(root, tag))
  961. return 0;
  962. node = rcu_dereference_raw(root->rnode);
  963. if (!node)
  964. return 0;
  965. if (!radix_tree_is_indirect_ptr(node)) {
  966. if (first_index > 0)
  967. return 0;
  968. results[0] = node;
  969. return 1;
  970. }
  971. node = indirect_to_ptr(node);
  972. max_index = radix_tree_maxindex(node->height);
  973. ret = 0;
  974. while (ret < max_items) {
  975. unsigned int nr_found, slots_found, i;
  976. unsigned long next_index; /* Index of next search */
  977. if (cur_index > max_index)
  978. break;
  979. slots_found = __lookup_tag(node, (void ***)results + ret,
  980. cur_index, max_items - ret, &next_index, tag);
  981. nr_found = 0;
  982. for (i = 0; i < slots_found; i++) {
  983. struct radix_tree_node *slot;
  984. slot = *(((void ***)results)[ret + i]);
  985. if (!slot)
  986. continue;
  987. results[ret + nr_found] =
  988. indirect_to_ptr(rcu_dereference_raw(slot));
  989. nr_found++;
  990. }
  991. ret += nr_found;
  992. if (next_index == 0)
  993. break;
  994. cur_index = next_index;
  995. }
  996. return ret;
  997. }
  998. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  999. /**
  1000. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  1001. * radix tree based on a tag
  1002. * @root: radix tree root
  1003. * @results: where the results of the lookup are placed
  1004. * @first_index: start the lookup from this key
  1005. * @max_items: place up to this many items at *results
  1006. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1007. *
  1008. * Performs an index-ascending scan of the tree for present items which
  1009. * have the tag indexed by @tag set. Places the slots at *@results and
  1010. * returns the number of slots which were placed at *@results.
  1011. */
  1012. unsigned int
  1013. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  1014. unsigned long first_index, unsigned int max_items,
  1015. unsigned int tag)
  1016. {
  1017. struct radix_tree_node *node;
  1018. unsigned long max_index;
  1019. unsigned long cur_index = first_index;
  1020. unsigned int ret;
  1021. /* check the root's tag bit */
  1022. if (!root_tag_get(root, tag))
  1023. return 0;
  1024. node = rcu_dereference_raw(root->rnode);
  1025. if (!node)
  1026. return 0;
  1027. if (!radix_tree_is_indirect_ptr(node)) {
  1028. if (first_index > 0)
  1029. return 0;
  1030. results[0] = (void **)&root->rnode;
  1031. return 1;
  1032. }
  1033. node = indirect_to_ptr(node);
  1034. max_index = radix_tree_maxindex(node->height);
  1035. ret = 0;
  1036. while (ret < max_items) {
  1037. unsigned int slots_found;
  1038. unsigned long next_index; /* Index of next search */
  1039. if (cur_index > max_index)
  1040. break;
  1041. slots_found = __lookup_tag(node, results + ret,
  1042. cur_index, max_items - ret, &next_index, tag);
  1043. ret += slots_found;
  1044. if (next_index == 0)
  1045. break;
  1046. cur_index = next_index;
  1047. }
  1048. return ret;
  1049. }
  1050. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  1051. /**
  1052. * radix_tree_shrink - shrink height of a radix tree to minimal
  1053. * @root radix tree root
  1054. */
  1055. static inline void radix_tree_shrink(struct radix_tree_root *root)
  1056. {
  1057. /* try to shrink tree height */
  1058. while (root->height > 0) {
  1059. struct radix_tree_node *to_free = root->rnode;
  1060. void *newptr;
  1061. BUG_ON(!radix_tree_is_indirect_ptr(to_free));
  1062. to_free = indirect_to_ptr(to_free);
  1063. /*
  1064. * The candidate node has more than one child, or its child
  1065. * is not at the leftmost slot, we cannot shrink.
  1066. */
  1067. if (to_free->count != 1)
  1068. break;
  1069. if (!to_free->slots[0])
  1070. break;
  1071. /*
  1072. * We don't need rcu_assign_pointer(), since we are simply
  1073. * moving the node from one part of the tree to another: if it
  1074. * was safe to dereference the old pointer to it
  1075. * (to_free->slots[0]), it will be safe to dereference the new
  1076. * one (root->rnode) as far as dependent read barriers go.
  1077. */
  1078. newptr = to_free->slots[0];
  1079. if (root->height > 1)
  1080. newptr = ptr_to_indirect(newptr);
  1081. root->rnode = newptr;
  1082. root->height--;
  1083. /*
  1084. * We have a dilemma here. The node's slot[0] must not be
  1085. * NULLed in case there are concurrent lookups expecting to
  1086. * find the item. However if this was a bottom-level node,
  1087. * then it may be subject to the slot pointer being visible
  1088. * to callers dereferencing it. If item corresponding to
  1089. * slot[0] is subsequently deleted, these callers would expect
  1090. * their slot to become empty sooner or later.
  1091. *
  1092. * For example, lockless pagecache will look up a slot, deref
  1093. * the page pointer, and if the page is 0 refcount it means it
  1094. * was concurrently deleted from pagecache so try the deref
  1095. * again. Fortunately there is already a requirement for logic
  1096. * to retry the entire slot lookup -- the indirect pointer
  1097. * problem (replacing direct root node with an indirect pointer
  1098. * also results in a stale slot). So tag the slot as indirect
  1099. * to force callers to retry.
  1100. */
  1101. if (root->height == 0)
  1102. *((unsigned long *)&to_free->slots[0]) |=
  1103. RADIX_TREE_INDIRECT_PTR;
  1104. radix_tree_node_free(to_free);
  1105. }
  1106. }
  1107. /**
  1108. * radix_tree_delete - delete an item from a radix tree
  1109. * @root: radix tree root
  1110. * @index: index key
  1111. *
  1112. * Remove the item at @index from the radix tree rooted at @root.
  1113. *
  1114. * Returns the address of the deleted item, or NULL if it was not present.
  1115. */
  1116. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  1117. {
  1118. /*
  1119. * The radix tree path needs to be one longer than the maximum path
  1120. * since the "list" is null terminated.
  1121. */
  1122. struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
  1123. struct radix_tree_node *slot = NULL;
  1124. struct radix_tree_node *to_free;
  1125. unsigned int height, shift;
  1126. int tag;
  1127. int offset;
  1128. height = root->height;
  1129. if (index > radix_tree_maxindex(height))
  1130. goto out;
  1131. slot = root->rnode;
  1132. if (height == 0) {
  1133. root_tag_clear_all(root);
  1134. root->rnode = NULL;
  1135. goto out;
  1136. }
  1137. slot = indirect_to_ptr(slot);
  1138. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  1139. pathp->node = NULL;
  1140. do {
  1141. if (slot == NULL)
  1142. goto out;
  1143. pathp++;
  1144. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  1145. pathp->offset = offset;
  1146. pathp->node = slot;
  1147. slot = slot->slots[offset];
  1148. shift -= RADIX_TREE_MAP_SHIFT;
  1149. height--;
  1150. } while (height > 0);
  1151. if (slot == NULL)
  1152. goto out;
  1153. /*
  1154. * Clear all tags associated with the just-deleted item
  1155. */
  1156. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  1157. if (tag_get(pathp->node, tag, pathp->offset))
  1158. radix_tree_tag_clear(root, index, tag);
  1159. }
  1160. to_free = NULL;
  1161. /* Now free the nodes we do not need anymore */
  1162. while (pathp->node) {
  1163. pathp->node->slots[pathp->offset] = NULL;
  1164. pathp->node->count--;
  1165. /*
  1166. * Queue the node for deferred freeing after the
  1167. * last reference to it disappears (set NULL, above).
  1168. */
  1169. if (to_free)
  1170. radix_tree_node_free(to_free);
  1171. if (pathp->node->count) {
  1172. if (pathp->node == indirect_to_ptr(root->rnode))
  1173. radix_tree_shrink(root);
  1174. goto out;
  1175. }
  1176. /* Node with zero slots in use so free it */
  1177. to_free = pathp->node;
  1178. pathp--;
  1179. }
  1180. root_tag_clear_all(root);
  1181. root->height = 0;
  1182. root->rnode = NULL;
  1183. if (to_free)
  1184. radix_tree_node_free(to_free);
  1185. out:
  1186. return slot;
  1187. }
  1188. EXPORT_SYMBOL(radix_tree_delete);
  1189. /**
  1190. * radix_tree_tagged - test whether any items in the tree are tagged
  1191. * @root: radix tree root
  1192. * @tag: tag to test
  1193. */
  1194. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1195. {
  1196. return root_tag_get(root, tag);
  1197. }
  1198. EXPORT_SYMBOL(radix_tree_tagged);
  1199. static void
  1200. radix_tree_node_ctor(void *node)
  1201. {
  1202. memset(node, 0, sizeof(struct radix_tree_node));
  1203. }
  1204. static __init unsigned long __maxindex(unsigned int height)
  1205. {
  1206. unsigned int width = height * RADIX_TREE_MAP_SHIFT;
  1207. int shift = RADIX_TREE_INDEX_BITS - width;
  1208. if (shift < 0)
  1209. return ~0UL;
  1210. if (shift >= BITS_PER_LONG)
  1211. return 0UL;
  1212. return ~0UL >> shift;
  1213. }
  1214. static __init void radix_tree_init_maxindex(void)
  1215. {
  1216. unsigned int i;
  1217. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  1218. height_to_maxindex[i] = __maxindex(i);
  1219. }
  1220. static int radix_tree_callback(struct notifier_block *nfb,
  1221. unsigned long action,
  1222. void *hcpu)
  1223. {
  1224. int cpu = (long)hcpu;
  1225. struct radix_tree_preload *rtp;
  1226. /* Free per-cpu pool of perloaded nodes */
  1227. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  1228. rtp = &per_cpu(radix_tree_preloads, cpu);
  1229. while (rtp->nr) {
  1230. kmem_cache_free(radix_tree_node_cachep,
  1231. rtp->nodes[rtp->nr-1]);
  1232. rtp->nodes[rtp->nr-1] = NULL;
  1233. rtp->nr--;
  1234. }
  1235. }
  1236. return NOTIFY_OK;
  1237. }
  1238. void __init radix_tree_init(void)
  1239. {
  1240. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1241. sizeof(struct radix_tree_node), 0,
  1242. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1243. radix_tree_node_ctor);
  1244. radix_tree_init_maxindex();
  1245. hotcpu_notifier(radix_tree_callback, 0);
  1246. }