radix-tree.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. * We need not to tag the root tag if there is no tag which is set with
  644. * settag within the range from *first_indexp to last_index.
  645. */
  646. if (tagged > 0)
  647. root_tag_set(root, settag);
  648. *first_indexp = index;
  649. return tagged;
  650. }
  651. EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
  652. /**
  653. * radix_tree_next_hole - find the next hole (not-present entry)
  654. * @root: tree root
  655. * @index: index key
  656. * @max_scan: maximum range to search
  657. *
  658. * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
  659. * indexed hole.
  660. *
  661. * Returns: the index of the hole if found, otherwise returns an index
  662. * outside of the set specified (in which case 'return - index >= max_scan'
  663. * will be true). In rare cases of index wrap-around, 0 will be returned.
  664. *
  665. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  666. * radix_tree_gang_lookup, this will not atomically search a snapshot of
  667. * the tree at a single point in time. For example, if a hole is created
  668. * at index 5, then subsequently a hole is created at index 10,
  669. * radix_tree_next_hole covering both indexes may return 10 if called
  670. * under rcu_read_lock.
  671. */
  672. unsigned long radix_tree_next_hole(struct radix_tree_root *root,
  673. unsigned long index, unsigned long max_scan)
  674. {
  675. unsigned long i;
  676. for (i = 0; i < max_scan; i++) {
  677. if (!radix_tree_lookup(root, index))
  678. break;
  679. index++;
  680. if (index == 0)
  681. break;
  682. }
  683. return index;
  684. }
  685. EXPORT_SYMBOL(radix_tree_next_hole);
  686. /**
  687. * radix_tree_prev_hole - find the prev hole (not-present entry)
  688. * @root: tree root
  689. * @index: index key
  690. * @max_scan: maximum range to search
  691. *
  692. * Search backwards in the range [max(index-max_scan+1, 0), index]
  693. * for the first hole.
  694. *
  695. * Returns: the index of the hole if found, otherwise returns an index
  696. * outside of the set specified (in which case 'index - return >= max_scan'
  697. * will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
  698. *
  699. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  700. * radix_tree_gang_lookup, this will not atomically search a snapshot of
  701. * the tree at a single point in time. For example, if a hole is created
  702. * at index 10, then subsequently a hole is created at index 5,
  703. * radix_tree_prev_hole covering both indexes may return 5 if called under
  704. * rcu_read_lock.
  705. */
  706. unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
  707. unsigned long index, unsigned long max_scan)
  708. {
  709. unsigned long i;
  710. for (i = 0; i < max_scan; i++) {
  711. if (!radix_tree_lookup(root, index))
  712. break;
  713. index--;
  714. if (index == ULONG_MAX)
  715. break;
  716. }
  717. return index;
  718. }
  719. EXPORT_SYMBOL(radix_tree_prev_hole);
  720. static unsigned int
  721. __lookup(struct radix_tree_node *slot, void ***results, unsigned long *indices,
  722. unsigned long index, unsigned int max_items, unsigned long *next_index)
  723. {
  724. unsigned int nr_found = 0;
  725. unsigned int shift, height;
  726. unsigned long i;
  727. height = slot->height;
  728. if (height == 0)
  729. goto out;
  730. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  731. for ( ; height > 1; height--) {
  732. i = (index >> shift) & RADIX_TREE_MAP_MASK;
  733. for (;;) {
  734. if (slot->slots[i] != NULL)
  735. break;
  736. index &= ~((1UL << shift) - 1);
  737. index += 1UL << shift;
  738. if (index == 0)
  739. goto out; /* 32-bit wraparound */
  740. i++;
  741. if (i == RADIX_TREE_MAP_SIZE)
  742. goto out;
  743. }
  744. shift -= RADIX_TREE_MAP_SHIFT;
  745. slot = rcu_dereference_raw(slot->slots[i]);
  746. if (slot == NULL)
  747. goto out;
  748. }
  749. /* Bottom level: grab some items */
  750. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  751. if (slot->slots[i]) {
  752. results[nr_found] = &(slot->slots[i]);
  753. if (indices)
  754. indices[nr_found] = index;
  755. if (++nr_found == max_items) {
  756. index++;
  757. goto out;
  758. }
  759. }
  760. index++;
  761. }
  762. out:
  763. *next_index = index;
  764. return nr_found;
  765. }
  766. /**
  767. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  768. * @root: radix tree root
  769. * @results: where the results of the lookup are placed
  770. * @first_index: start the lookup from this key
  771. * @max_items: place up to this many items at *results
  772. *
  773. * Performs an index-ascending scan of the tree for present items. Places
  774. * them at *@results and returns the number of items which were placed at
  775. * *@results.
  776. *
  777. * The implementation is naive.
  778. *
  779. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  780. * rcu_read_lock. In this case, rather than the returned results being
  781. * an atomic snapshot of the tree at a single point in time, the semantics
  782. * of an RCU protected gang lookup are as though multiple radix_tree_lookups
  783. * have been issued in individual locks, and results stored in 'results'.
  784. */
  785. unsigned int
  786. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  787. unsigned long first_index, unsigned int max_items)
  788. {
  789. unsigned long max_index;
  790. struct radix_tree_node *node;
  791. unsigned long cur_index = first_index;
  792. unsigned int ret;
  793. node = rcu_dereference_raw(root->rnode);
  794. if (!node)
  795. return 0;
  796. if (!radix_tree_is_indirect_ptr(node)) {
  797. if (first_index > 0)
  798. return 0;
  799. results[0] = node;
  800. return 1;
  801. }
  802. node = indirect_to_ptr(node);
  803. max_index = radix_tree_maxindex(node->height);
  804. ret = 0;
  805. while (ret < max_items) {
  806. unsigned int nr_found, slots_found, i;
  807. unsigned long next_index; /* Index of next search */
  808. if (cur_index > max_index)
  809. break;
  810. slots_found = __lookup(node, (void ***)results + ret, NULL,
  811. cur_index, max_items - ret, &next_index);
  812. nr_found = 0;
  813. for (i = 0; i < slots_found; i++) {
  814. struct radix_tree_node *slot;
  815. slot = *(((void ***)results)[ret + i]);
  816. if (!slot)
  817. continue;
  818. results[ret + nr_found] =
  819. indirect_to_ptr(rcu_dereference_raw(slot));
  820. nr_found++;
  821. }
  822. ret += nr_found;
  823. if (next_index == 0)
  824. break;
  825. cur_index = next_index;
  826. }
  827. return ret;
  828. }
  829. EXPORT_SYMBOL(radix_tree_gang_lookup);
  830. /**
  831. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  832. * @root: radix tree root
  833. * @results: where the results of the lookup are placed
  834. * @indices: where their indices should be placed (but usually NULL)
  835. * @first_index: start the lookup from this key
  836. * @max_items: place up to this many items at *results
  837. *
  838. * Performs an index-ascending scan of the tree for present items. Places
  839. * their slots at *@results and returns the number of items which were
  840. * placed at *@results.
  841. *
  842. * The implementation is naive.
  843. *
  844. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  845. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  846. * protection, radix_tree_deref_slot may fail requiring a retry.
  847. */
  848. unsigned int
  849. radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  850. void ***results, unsigned long *indices,
  851. unsigned long first_index, unsigned int max_items)
  852. {
  853. unsigned long max_index;
  854. struct radix_tree_node *node;
  855. unsigned long cur_index = first_index;
  856. unsigned int ret;
  857. node = rcu_dereference_raw(root->rnode);
  858. if (!node)
  859. return 0;
  860. if (!radix_tree_is_indirect_ptr(node)) {
  861. if (first_index > 0)
  862. return 0;
  863. results[0] = (void **)&root->rnode;
  864. if (indices)
  865. indices[0] = 0;
  866. return 1;
  867. }
  868. node = indirect_to_ptr(node);
  869. max_index = radix_tree_maxindex(node->height);
  870. ret = 0;
  871. while (ret < max_items) {
  872. unsigned int slots_found;
  873. unsigned long next_index; /* Index of next search */
  874. if (cur_index > max_index)
  875. break;
  876. slots_found = __lookup(node, results + ret,
  877. indices ? indices + ret : NULL,
  878. cur_index, max_items - ret, &next_index);
  879. ret += slots_found;
  880. if (next_index == 0)
  881. break;
  882. cur_index = next_index;
  883. }
  884. return ret;
  885. }
  886. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  887. /*
  888. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  889. * open-coding the search.
  890. */
  891. static unsigned int
  892. __lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
  893. unsigned int max_items, unsigned long *next_index, unsigned int tag)
  894. {
  895. unsigned int nr_found = 0;
  896. unsigned int shift, height;
  897. height = slot->height;
  898. if (height == 0)
  899. goto out;
  900. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  901. while (height > 0) {
  902. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  903. for (;;) {
  904. if (tag_get(slot, tag, i))
  905. break;
  906. index &= ~((1UL << shift) - 1);
  907. index += 1UL << shift;
  908. if (index == 0)
  909. goto out; /* 32-bit wraparound */
  910. i++;
  911. if (i == RADIX_TREE_MAP_SIZE)
  912. goto out;
  913. }
  914. height--;
  915. if (height == 0) { /* Bottom level: grab some items */
  916. unsigned long j = index & RADIX_TREE_MAP_MASK;
  917. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  918. index++;
  919. if (!tag_get(slot, tag, j))
  920. continue;
  921. /*
  922. * Even though the tag was found set, we need to
  923. * recheck that we have a non-NULL node, because
  924. * if this lookup is lockless, it may have been
  925. * subsequently deleted.
  926. *
  927. * Similar care must be taken in any place that
  928. * lookup ->slots[x] without a lock (ie. can't
  929. * rely on its value remaining the same).
  930. */
  931. if (slot->slots[j]) {
  932. results[nr_found++] = &(slot->slots[j]);
  933. if (nr_found == max_items)
  934. goto out;
  935. }
  936. }
  937. }
  938. shift -= RADIX_TREE_MAP_SHIFT;
  939. slot = rcu_dereference_raw(slot->slots[i]);
  940. if (slot == NULL)
  941. break;
  942. }
  943. out:
  944. *next_index = index;
  945. return nr_found;
  946. }
  947. /**
  948. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  949. * based on a tag
  950. * @root: radix tree root
  951. * @results: where the results of the lookup are placed
  952. * @first_index: start the lookup from this key
  953. * @max_items: place up to this many items at *results
  954. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  955. *
  956. * Performs an index-ascending scan of the tree for present items which
  957. * have the tag indexed by @tag set. Places the items at *@results and
  958. * returns the number of items which were placed at *@results.
  959. */
  960. unsigned int
  961. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  962. unsigned long first_index, unsigned int max_items,
  963. unsigned int tag)
  964. {
  965. struct radix_tree_node *node;
  966. unsigned long max_index;
  967. unsigned long cur_index = first_index;
  968. unsigned int ret;
  969. /* check the root's tag bit */
  970. if (!root_tag_get(root, tag))
  971. return 0;
  972. node = rcu_dereference_raw(root->rnode);
  973. if (!node)
  974. return 0;
  975. if (!radix_tree_is_indirect_ptr(node)) {
  976. if (first_index > 0)
  977. return 0;
  978. results[0] = node;
  979. return 1;
  980. }
  981. node = indirect_to_ptr(node);
  982. max_index = radix_tree_maxindex(node->height);
  983. ret = 0;
  984. while (ret < max_items) {
  985. unsigned int nr_found, slots_found, i;
  986. unsigned long next_index; /* Index of next search */
  987. if (cur_index > max_index)
  988. break;
  989. slots_found = __lookup_tag(node, (void ***)results + ret,
  990. cur_index, max_items - ret, &next_index, tag);
  991. nr_found = 0;
  992. for (i = 0; i < slots_found; i++) {
  993. struct radix_tree_node *slot;
  994. slot = *(((void ***)results)[ret + i]);
  995. if (!slot)
  996. continue;
  997. results[ret + nr_found] =
  998. indirect_to_ptr(rcu_dereference_raw(slot));
  999. nr_found++;
  1000. }
  1001. ret += nr_found;
  1002. if (next_index == 0)
  1003. break;
  1004. cur_index = next_index;
  1005. }
  1006. return ret;
  1007. }
  1008. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  1009. /**
  1010. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  1011. * radix tree based on a tag
  1012. * @root: radix tree root
  1013. * @results: where the results of the lookup are placed
  1014. * @first_index: start the lookup from this key
  1015. * @max_items: place up to this many items at *results
  1016. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1017. *
  1018. * Performs an index-ascending scan of the tree for present items which
  1019. * have the tag indexed by @tag set. Places the slots at *@results and
  1020. * returns the number of slots which were placed at *@results.
  1021. */
  1022. unsigned int
  1023. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  1024. unsigned long first_index, unsigned int max_items,
  1025. unsigned int tag)
  1026. {
  1027. struct radix_tree_node *node;
  1028. unsigned long max_index;
  1029. unsigned long cur_index = first_index;
  1030. unsigned int ret;
  1031. /* check the root's tag bit */
  1032. if (!root_tag_get(root, tag))
  1033. return 0;
  1034. node = rcu_dereference_raw(root->rnode);
  1035. if (!node)
  1036. return 0;
  1037. if (!radix_tree_is_indirect_ptr(node)) {
  1038. if (first_index > 0)
  1039. return 0;
  1040. results[0] = (void **)&root->rnode;
  1041. return 1;
  1042. }
  1043. node = indirect_to_ptr(node);
  1044. max_index = radix_tree_maxindex(node->height);
  1045. ret = 0;
  1046. while (ret < max_items) {
  1047. unsigned int slots_found;
  1048. unsigned long next_index; /* Index of next search */
  1049. if (cur_index > max_index)
  1050. break;
  1051. slots_found = __lookup_tag(node, results + ret,
  1052. cur_index, max_items - ret, &next_index, tag);
  1053. ret += slots_found;
  1054. if (next_index == 0)
  1055. break;
  1056. cur_index = next_index;
  1057. }
  1058. return ret;
  1059. }
  1060. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  1061. #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
  1062. #include <linux/sched.h> /* for cond_resched() */
  1063. /*
  1064. * This linear search is at present only useful to shmem_unuse_inode().
  1065. */
  1066. static unsigned long __locate(struct radix_tree_node *slot, void *item,
  1067. unsigned long index, unsigned long *found_index)
  1068. {
  1069. unsigned int shift, height;
  1070. unsigned long i;
  1071. height = slot->height;
  1072. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  1073. for ( ; height > 1; height--) {
  1074. i = (index >> shift) & RADIX_TREE_MAP_MASK;
  1075. for (;;) {
  1076. if (slot->slots[i] != NULL)
  1077. break;
  1078. index &= ~((1UL << shift) - 1);
  1079. index += 1UL << shift;
  1080. if (index == 0)
  1081. goto out; /* 32-bit wraparound */
  1082. i++;
  1083. if (i == RADIX_TREE_MAP_SIZE)
  1084. goto out;
  1085. }
  1086. shift -= RADIX_TREE_MAP_SHIFT;
  1087. slot = rcu_dereference_raw(slot->slots[i]);
  1088. if (slot == NULL)
  1089. goto out;
  1090. }
  1091. /* Bottom level: check items */
  1092. for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
  1093. if (slot->slots[i] == item) {
  1094. *found_index = index + i;
  1095. index = 0;
  1096. goto out;
  1097. }
  1098. }
  1099. index += RADIX_TREE_MAP_SIZE;
  1100. out:
  1101. return index;
  1102. }
  1103. /**
  1104. * radix_tree_locate_item - search through radix tree for item
  1105. * @root: radix tree root
  1106. * @item: item to be found
  1107. *
  1108. * Returns index where item was found, or -1 if not found.
  1109. * Caller must hold no lock (since this time-consuming function needs
  1110. * to be preemptible), and must check afterwards if item is still there.
  1111. */
  1112. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1113. {
  1114. struct radix_tree_node *node;
  1115. unsigned long max_index;
  1116. unsigned long cur_index = 0;
  1117. unsigned long found_index = -1;
  1118. do {
  1119. rcu_read_lock();
  1120. node = rcu_dereference_raw(root->rnode);
  1121. if (!radix_tree_is_indirect_ptr(node)) {
  1122. rcu_read_unlock();
  1123. if (node == item)
  1124. found_index = 0;
  1125. break;
  1126. }
  1127. node = indirect_to_ptr(node);
  1128. max_index = radix_tree_maxindex(node->height);
  1129. if (cur_index > max_index)
  1130. break;
  1131. cur_index = __locate(node, item, cur_index, &found_index);
  1132. rcu_read_unlock();
  1133. cond_resched();
  1134. } while (cur_index != 0 && cur_index <= max_index);
  1135. return found_index;
  1136. }
  1137. #else
  1138. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1139. {
  1140. return -1;
  1141. }
  1142. #endif /* CONFIG_SHMEM && CONFIG_SWAP */
  1143. /**
  1144. * radix_tree_shrink - shrink height of a radix tree to minimal
  1145. * @root radix tree root
  1146. */
  1147. static inline void radix_tree_shrink(struct radix_tree_root *root)
  1148. {
  1149. /* try to shrink tree height */
  1150. while (root->height > 0) {
  1151. struct radix_tree_node *to_free = root->rnode;
  1152. void *newptr;
  1153. BUG_ON(!radix_tree_is_indirect_ptr(to_free));
  1154. to_free = indirect_to_ptr(to_free);
  1155. /*
  1156. * The candidate node has more than one child, or its child
  1157. * is not at the leftmost slot, we cannot shrink.
  1158. */
  1159. if (to_free->count != 1)
  1160. break;
  1161. if (!to_free->slots[0])
  1162. break;
  1163. /*
  1164. * We don't need rcu_assign_pointer(), since we are simply
  1165. * moving the node from one part of the tree to another: if it
  1166. * was safe to dereference the old pointer to it
  1167. * (to_free->slots[0]), it will be safe to dereference the new
  1168. * one (root->rnode) as far as dependent read barriers go.
  1169. */
  1170. newptr = to_free->slots[0];
  1171. if (root->height > 1)
  1172. newptr = ptr_to_indirect(newptr);
  1173. root->rnode = newptr;
  1174. root->height--;
  1175. /*
  1176. * We have a dilemma here. The node's slot[0] must not be
  1177. * NULLed in case there are concurrent lookups expecting to
  1178. * find the item. However if this was a bottom-level node,
  1179. * then it may be subject to the slot pointer being visible
  1180. * to callers dereferencing it. If item corresponding to
  1181. * slot[0] is subsequently deleted, these callers would expect
  1182. * their slot to become empty sooner or later.
  1183. *
  1184. * For example, lockless pagecache will look up a slot, deref
  1185. * the page pointer, and if the page is 0 refcount it means it
  1186. * was concurrently deleted from pagecache so try the deref
  1187. * again. Fortunately there is already a requirement for logic
  1188. * to retry the entire slot lookup -- the indirect pointer
  1189. * problem (replacing direct root node with an indirect pointer
  1190. * also results in a stale slot). So tag the slot as indirect
  1191. * to force callers to retry.
  1192. */
  1193. if (root->height == 0)
  1194. *((unsigned long *)&to_free->slots[0]) |=
  1195. RADIX_TREE_INDIRECT_PTR;
  1196. radix_tree_node_free(to_free);
  1197. }
  1198. }
  1199. /**
  1200. * radix_tree_delete - delete an item from a radix tree
  1201. * @root: radix tree root
  1202. * @index: index key
  1203. *
  1204. * Remove the item at @index from the radix tree rooted at @root.
  1205. *
  1206. * Returns the address of the deleted item, or NULL if it was not present.
  1207. */
  1208. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  1209. {
  1210. /*
  1211. * The radix tree path needs to be one longer than the maximum path
  1212. * since the "list" is null terminated.
  1213. */
  1214. struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
  1215. struct radix_tree_node *slot = NULL;
  1216. struct radix_tree_node *to_free;
  1217. unsigned int height, shift;
  1218. int tag;
  1219. int offset;
  1220. height = root->height;
  1221. if (index > radix_tree_maxindex(height))
  1222. goto out;
  1223. slot = root->rnode;
  1224. if (height == 0) {
  1225. root_tag_clear_all(root);
  1226. root->rnode = NULL;
  1227. goto out;
  1228. }
  1229. slot = indirect_to_ptr(slot);
  1230. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  1231. pathp->node = NULL;
  1232. do {
  1233. if (slot == NULL)
  1234. goto out;
  1235. pathp++;
  1236. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  1237. pathp->offset = offset;
  1238. pathp->node = slot;
  1239. slot = slot->slots[offset];
  1240. shift -= RADIX_TREE_MAP_SHIFT;
  1241. height--;
  1242. } while (height > 0);
  1243. if (slot == NULL)
  1244. goto out;
  1245. /*
  1246. * Clear all tags associated with the just-deleted item
  1247. */
  1248. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  1249. if (tag_get(pathp->node, tag, pathp->offset))
  1250. radix_tree_tag_clear(root, index, tag);
  1251. }
  1252. to_free = NULL;
  1253. /* Now free the nodes we do not need anymore */
  1254. while (pathp->node) {
  1255. pathp->node->slots[pathp->offset] = NULL;
  1256. pathp->node->count--;
  1257. /*
  1258. * Queue the node for deferred freeing after the
  1259. * last reference to it disappears (set NULL, above).
  1260. */
  1261. if (to_free)
  1262. radix_tree_node_free(to_free);
  1263. if (pathp->node->count) {
  1264. if (pathp->node == indirect_to_ptr(root->rnode))
  1265. radix_tree_shrink(root);
  1266. goto out;
  1267. }
  1268. /* Node with zero slots in use so free it */
  1269. to_free = pathp->node;
  1270. pathp--;
  1271. }
  1272. root_tag_clear_all(root);
  1273. root->height = 0;
  1274. root->rnode = NULL;
  1275. if (to_free)
  1276. radix_tree_node_free(to_free);
  1277. out:
  1278. return slot;
  1279. }
  1280. EXPORT_SYMBOL(radix_tree_delete);
  1281. /**
  1282. * radix_tree_tagged - test whether any items in the tree are tagged
  1283. * @root: radix tree root
  1284. * @tag: tag to test
  1285. */
  1286. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1287. {
  1288. return root_tag_get(root, tag);
  1289. }
  1290. EXPORT_SYMBOL(radix_tree_tagged);
  1291. static void
  1292. radix_tree_node_ctor(void *node)
  1293. {
  1294. memset(node, 0, sizeof(struct radix_tree_node));
  1295. }
  1296. static __init unsigned long __maxindex(unsigned int height)
  1297. {
  1298. unsigned int width = height * RADIX_TREE_MAP_SHIFT;
  1299. int shift = RADIX_TREE_INDEX_BITS - width;
  1300. if (shift < 0)
  1301. return ~0UL;
  1302. if (shift >= BITS_PER_LONG)
  1303. return 0UL;
  1304. return ~0UL >> shift;
  1305. }
  1306. static __init void radix_tree_init_maxindex(void)
  1307. {
  1308. unsigned int i;
  1309. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  1310. height_to_maxindex[i] = __maxindex(i);
  1311. }
  1312. static int radix_tree_callback(struct notifier_block *nfb,
  1313. unsigned long action,
  1314. void *hcpu)
  1315. {
  1316. int cpu = (long)hcpu;
  1317. struct radix_tree_preload *rtp;
  1318. /* Free per-cpu pool of perloaded nodes */
  1319. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  1320. rtp = &per_cpu(radix_tree_preloads, cpu);
  1321. while (rtp->nr) {
  1322. kmem_cache_free(radix_tree_node_cachep,
  1323. rtp->nodes[rtp->nr-1]);
  1324. rtp->nodes[rtp->nr-1] = NULL;
  1325. rtp->nr--;
  1326. }
  1327. }
  1328. return NOTIFY_OK;
  1329. }
  1330. void __init radix_tree_init(void)
  1331. {
  1332. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1333. sizeof(struct radix_tree_node), 0,
  1334. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1335. radix_tree_node_ctor);
  1336. radix_tree_init_maxindex();
  1337. hotcpu_notifier(radix_tree_callback, 0);
  1338. }