radix-tree.c 44 KB

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