radix-tree.c 40 KB

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