radix-tree.c 39 KB

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