radix-tree.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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. * New allocate node must have node->private_list as INIT_LIST_HEAD
  315. * state by workingset shadow memory implementation.
  316. * If user pass __GFP_ZERO by mistake, slab allocator will clear
  317. * node->private_list, which makes a BUG. Rather than going Oops,
  318. * just fix and warn about it.
  319. */
  320. if (WARN_ON(gfp_mask & __GFP_ZERO))
  321. gfp_mask &= ~__GFP_ZERO;
  322. /*
  323. * Nodes preloaded by one cgroup can be be used by another cgroup, so
  324. * they should never be accounted to any particular memory cgroup.
  325. */
  326. gfp_mask &= ~__GFP_ACCOUNT;
  327. preempt_disable();
  328. rtp = this_cpu_ptr(&radix_tree_preloads);
  329. while (rtp->nr < nr) {
  330. preempt_enable();
  331. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  332. if (node == NULL)
  333. goto out;
  334. preempt_disable();
  335. rtp = this_cpu_ptr(&radix_tree_preloads);
  336. if (rtp->nr < nr) {
  337. node->private_data = rtp->nodes;
  338. rtp->nodes = node;
  339. rtp->nr++;
  340. } else {
  341. kmem_cache_free(radix_tree_node_cachep, node);
  342. }
  343. }
  344. ret = 0;
  345. out:
  346. return ret;
  347. }
  348. /*
  349. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  350. * ensure that the addition of a single element in the tree cannot fail. On
  351. * success, return zero, with preemption disabled. On error, return -ENOMEM
  352. * with preemption not disabled.
  353. *
  354. * To make use of this facility, the radix tree must be initialised without
  355. * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
  356. */
  357. int radix_tree_preload(gfp_t gfp_mask)
  358. {
  359. /* Warn on non-sensical use... */
  360. WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
  361. return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
  362. }
  363. EXPORT_SYMBOL(radix_tree_preload);
  364. /*
  365. * The same as above function, except we don't guarantee preloading happens.
  366. * We do it, if we decide it helps. On success, return zero with preemption
  367. * disabled. On error, return -ENOMEM with preemption not disabled.
  368. */
  369. int radix_tree_maybe_preload(gfp_t gfp_mask)
  370. {
  371. if (gfpflags_allow_blocking(gfp_mask))
  372. return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
  373. /* Preloading doesn't help anything with this gfp mask, skip it */
  374. preempt_disable();
  375. return 0;
  376. }
  377. EXPORT_SYMBOL(radix_tree_maybe_preload);
  378. /*
  379. * The same as function above, but preload number of nodes required to insert
  380. * (1 << order) continuous naturally-aligned elements.
  381. */
  382. int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
  383. {
  384. unsigned long nr_subtrees;
  385. int nr_nodes, subtree_height;
  386. /* Preloading doesn't help anything with this gfp mask, skip it */
  387. if (!gfpflags_allow_blocking(gfp_mask)) {
  388. preempt_disable();
  389. return 0;
  390. }
  391. /*
  392. * Calculate number and height of fully populated subtrees it takes to
  393. * store (1 << order) elements.
  394. */
  395. nr_subtrees = 1 << order;
  396. for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
  397. subtree_height++)
  398. nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
  399. /*
  400. * The worst case is zero height tree with a single item at index 0 and
  401. * then inserting items starting at ULONG_MAX - (1 << order).
  402. *
  403. * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
  404. * 0-index item.
  405. */
  406. nr_nodes = RADIX_TREE_MAX_PATH;
  407. /* Plus branch to fully populated subtrees. */
  408. nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
  409. /* Root node is shared. */
  410. nr_nodes--;
  411. /* Plus nodes required to build subtrees. */
  412. nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
  413. return __radix_tree_preload(gfp_mask, nr_nodes);
  414. }
  415. /*
  416. * The maximum index which can be stored in a radix tree
  417. */
  418. static inline unsigned long shift_maxindex(unsigned int shift)
  419. {
  420. return (RADIX_TREE_MAP_SIZE << shift) - 1;
  421. }
  422. static inline unsigned long node_maxindex(struct radix_tree_node *node)
  423. {
  424. return shift_maxindex(node->shift);
  425. }
  426. static unsigned radix_tree_load_root(struct radix_tree_root *root,
  427. struct radix_tree_node **nodep, unsigned long *maxindex)
  428. {
  429. struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
  430. *nodep = node;
  431. if (likely(radix_tree_is_internal_node(node))) {
  432. node = entry_to_node(node);
  433. *maxindex = node_maxindex(node);
  434. return node->shift + RADIX_TREE_MAP_SHIFT;
  435. }
  436. *maxindex = 0;
  437. return 0;
  438. }
  439. /*
  440. * Extend a radix tree so it can store key @index.
  441. */
  442. static int radix_tree_extend(struct radix_tree_root *root,
  443. unsigned long index, unsigned int shift)
  444. {
  445. struct radix_tree_node *slot;
  446. unsigned int maxshift;
  447. int tag;
  448. /* Figure out what the shift should be. */
  449. maxshift = shift;
  450. while (index > shift_maxindex(maxshift))
  451. maxshift += RADIX_TREE_MAP_SHIFT;
  452. slot = root->rnode;
  453. if (!slot)
  454. goto out;
  455. do {
  456. struct radix_tree_node *node = radix_tree_node_alloc(root);
  457. if (!node)
  458. return -ENOMEM;
  459. /* Propagate the aggregated tag info into the new root */
  460. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  461. if (root_tag_get(root, tag))
  462. tag_set(node, tag, 0);
  463. }
  464. BUG_ON(shift > BITS_PER_LONG);
  465. node->shift = shift;
  466. node->offset = 0;
  467. node->count = 1;
  468. node->parent = NULL;
  469. if (radix_tree_is_internal_node(slot))
  470. entry_to_node(slot)->parent = node;
  471. node->slots[0] = slot;
  472. slot = node_to_entry(node);
  473. rcu_assign_pointer(root->rnode, slot);
  474. shift += RADIX_TREE_MAP_SHIFT;
  475. } while (shift <= maxshift);
  476. out:
  477. return maxshift + RADIX_TREE_MAP_SHIFT;
  478. }
  479. /**
  480. * __radix_tree_create - create a slot in a radix tree
  481. * @root: radix tree root
  482. * @index: index key
  483. * @order: index occupies 2^order aligned slots
  484. * @nodep: returns node
  485. * @slotp: returns slot
  486. *
  487. * Create, if necessary, and return the node and slot for an item
  488. * at position @index in the radix tree @root.
  489. *
  490. * Until there is more than one item in the tree, no nodes are
  491. * allocated and @root->rnode is used as a direct slot instead of
  492. * pointing to a node, in which case *@nodep will be NULL.
  493. *
  494. * Returns -ENOMEM, or 0 for success.
  495. */
  496. int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
  497. unsigned order, struct radix_tree_node **nodep,
  498. void ***slotp)
  499. {
  500. struct radix_tree_node *node = NULL, *child;
  501. void **slot = (void **)&root->rnode;
  502. unsigned long maxindex;
  503. unsigned int shift, offset = 0;
  504. unsigned long max = index | ((1UL << order) - 1);
  505. shift = radix_tree_load_root(root, &child, &maxindex);
  506. /* Make sure the tree is high enough. */
  507. if (max > maxindex) {
  508. int error = radix_tree_extend(root, max, shift);
  509. if (error < 0)
  510. return error;
  511. shift = error;
  512. child = root->rnode;
  513. if (order == shift)
  514. shift += RADIX_TREE_MAP_SHIFT;
  515. }
  516. while (shift > order) {
  517. shift -= RADIX_TREE_MAP_SHIFT;
  518. if (child == NULL) {
  519. /* Have to add a child node. */
  520. child = radix_tree_node_alloc(root);
  521. if (!child)
  522. return -ENOMEM;
  523. child->shift = shift;
  524. child->offset = offset;
  525. child->parent = node;
  526. rcu_assign_pointer(*slot, node_to_entry(child));
  527. if (node)
  528. node->count++;
  529. } else if (!radix_tree_is_internal_node(child))
  530. break;
  531. /* Go a level down */
  532. node = entry_to_node(child);
  533. offset = radix_tree_descend(node, &child, index);
  534. slot = &node->slots[offset];
  535. }
  536. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  537. /* Insert pointers to the canonical entry */
  538. if (order > shift) {
  539. unsigned i, n = 1 << (order - shift);
  540. offset = offset & ~(n - 1);
  541. slot = &node->slots[offset];
  542. child = node_to_entry(slot);
  543. for (i = 0; i < n; i++) {
  544. if (slot[i])
  545. return -EEXIST;
  546. }
  547. for (i = 1; i < n; i++) {
  548. rcu_assign_pointer(slot[i], child);
  549. node->count++;
  550. }
  551. }
  552. #endif
  553. if (nodep)
  554. *nodep = node;
  555. if (slotp)
  556. *slotp = slot;
  557. return 0;
  558. }
  559. /**
  560. * __radix_tree_insert - insert into a radix tree
  561. * @root: radix tree root
  562. * @index: index key
  563. * @order: key covers the 2^order indices around index
  564. * @item: item to insert
  565. *
  566. * Insert an item into the radix tree at position @index.
  567. */
  568. int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
  569. unsigned order, void *item)
  570. {
  571. struct radix_tree_node *node;
  572. void **slot;
  573. int error;
  574. BUG_ON(radix_tree_is_internal_node(item));
  575. error = __radix_tree_create(root, index, order, &node, &slot);
  576. if (error)
  577. return error;
  578. if (*slot != NULL)
  579. return -EEXIST;
  580. rcu_assign_pointer(*slot, item);
  581. if (node) {
  582. unsigned offset = get_slot_offset(node, slot);
  583. node->count++;
  584. BUG_ON(tag_get(node, 0, offset));
  585. BUG_ON(tag_get(node, 1, offset));
  586. BUG_ON(tag_get(node, 2, offset));
  587. } else {
  588. BUG_ON(root_tags_get(root));
  589. }
  590. return 0;
  591. }
  592. EXPORT_SYMBOL(__radix_tree_insert);
  593. /**
  594. * __radix_tree_lookup - lookup an item in a radix tree
  595. * @root: radix tree root
  596. * @index: index key
  597. * @nodep: returns node
  598. * @slotp: returns slot
  599. *
  600. * Lookup and return the item at position @index in the radix
  601. * tree @root.
  602. *
  603. * Until there is more than one item in the tree, no nodes are
  604. * allocated and @root->rnode is used as a direct slot instead of
  605. * pointing to a node, in which case *@nodep will be NULL.
  606. */
  607. void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
  608. struct radix_tree_node **nodep, void ***slotp)
  609. {
  610. struct radix_tree_node *node, *parent;
  611. unsigned long maxindex;
  612. void **slot;
  613. restart:
  614. parent = NULL;
  615. slot = (void **)&root->rnode;
  616. radix_tree_load_root(root, &node, &maxindex);
  617. if (index > maxindex)
  618. return NULL;
  619. while (radix_tree_is_internal_node(node)) {
  620. unsigned offset;
  621. if (node == RADIX_TREE_RETRY)
  622. goto restart;
  623. parent = entry_to_node(node);
  624. offset = radix_tree_descend(parent, &node, index);
  625. slot = parent->slots + offset;
  626. }
  627. if (nodep)
  628. *nodep = parent;
  629. if (slotp)
  630. *slotp = slot;
  631. return node;
  632. }
  633. /**
  634. * radix_tree_lookup_slot - lookup a slot in a radix tree
  635. * @root: radix tree root
  636. * @index: index key
  637. *
  638. * Returns: the slot corresponding to the position @index in the
  639. * radix tree @root. This is useful for update-if-exists operations.
  640. *
  641. * This function can be called under rcu_read_lock iff the slot is not
  642. * modified by radix_tree_replace_slot, otherwise it must be called
  643. * exclusive from other writers. Any dereference of the slot must be done
  644. * using radix_tree_deref_slot.
  645. */
  646. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  647. {
  648. void **slot;
  649. if (!__radix_tree_lookup(root, index, NULL, &slot))
  650. return NULL;
  651. return slot;
  652. }
  653. EXPORT_SYMBOL(radix_tree_lookup_slot);
  654. /**
  655. * radix_tree_lookup - perform lookup operation on a radix tree
  656. * @root: radix tree root
  657. * @index: index key
  658. *
  659. * Lookup the item at the position @index in the radix tree @root.
  660. *
  661. * This function can be called under rcu_read_lock, however the caller
  662. * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
  663. * them safely). No RCU barriers are required to access or modify the
  664. * returned item, however.
  665. */
  666. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  667. {
  668. return __radix_tree_lookup(root, index, NULL, NULL);
  669. }
  670. EXPORT_SYMBOL(radix_tree_lookup);
  671. /**
  672. * radix_tree_tag_set - set a tag on a radix tree node
  673. * @root: radix tree root
  674. * @index: index key
  675. * @tag: tag index
  676. *
  677. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  678. * corresponding to @index in the radix tree. From
  679. * the root all the way down to the leaf node.
  680. *
  681. * Returns the address of the tagged item. Setting a tag on a not-present
  682. * item is a bug.
  683. */
  684. void *radix_tree_tag_set(struct radix_tree_root *root,
  685. unsigned long index, unsigned int tag)
  686. {
  687. struct radix_tree_node *node, *parent;
  688. unsigned long maxindex;
  689. radix_tree_load_root(root, &node, &maxindex);
  690. BUG_ON(index > maxindex);
  691. while (radix_tree_is_internal_node(node)) {
  692. unsigned offset;
  693. parent = entry_to_node(node);
  694. offset = radix_tree_descend(parent, &node, index);
  695. BUG_ON(!node);
  696. if (!tag_get(parent, tag, offset))
  697. tag_set(parent, tag, offset);
  698. }
  699. /* set the root's tag bit */
  700. if (!root_tag_get(root, tag))
  701. root_tag_set(root, tag);
  702. return node;
  703. }
  704. EXPORT_SYMBOL(radix_tree_tag_set);
  705. static void node_tag_clear(struct radix_tree_root *root,
  706. struct radix_tree_node *node,
  707. unsigned int tag, unsigned int offset)
  708. {
  709. while (node) {
  710. if (!tag_get(node, tag, offset))
  711. return;
  712. tag_clear(node, tag, offset);
  713. if (any_tag_set(node, tag))
  714. return;
  715. offset = node->offset;
  716. node = node->parent;
  717. }
  718. /* clear the root's tag bit */
  719. if (root_tag_get(root, tag))
  720. root_tag_clear(root, tag);
  721. }
  722. /**
  723. * radix_tree_tag_clear - clear a tag on a radix tree node
  724. * @root: radix tree root
  725. * @index: index key
  726. * @tag: tag index
  727. *
  728. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  729. * corresponding to @index in the radix tree. If this causes
  730. * the leaf node to have no tags set then clear the tag in the
  731. * next-to-leaf node, etc.
  732. *
  733. * Returns the address of the tagged item on success, else NULL. ie:
  734. * has the same return value and semantics as radix_tree_lookup().
  735. */
  736. void *radix_tree_tag_clear(struct radix_tree_root *root,
  737. unsigned long index, unsigned int tag)
  738. {
  739. struct radix_tree_node *node, *parent;
  740. unsigned long maxindex;
  741. int uninitialized_var(offset);
  742. radix_tree_load_root(root, &node, &maxindex);
  743. if (index > maxindex)
  744. return NULL;
  745. parent = NULL;
  746. while (radix_tree_is_internal_node(node)) {
  747. parent = entry_to_node(node);
  748. offset = radix_tree_descend(parent, &node, index);
  749. }
  750. if (node)
  751. node_tag_clear(root, parent, tag, offset);
  752. return node;
  753. }
  754. EXPORT_SYMBOL(radix_tree_tag_clear);
  755. /**
  756. * radix_tree_tag_get - get a tag on a radix tree node
  757. * @root: radix tree root
  758. * @index: index key
  759. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  760. *
  761. * Return values:
  762. *
  763. * 0: tag not present or not set
  764. * 1: tag set
  765. *
  766. * Note that the return value of this function may not be relied on, even if
  767. * the RCU lock is held, unless tag modification and node deletion are excluded
  768. * from concurrency.
  769. */
  770. int radix_tree_tag_get(struct radix_tree_root *root,
  771. unsigned long index, unsigned int tag)
  772. {
  773. struct radix_tree_node *node, *parent;
  774. unsigned long maxindex;
  775. if (!root_tag_get(root, tag))
  776. return 0;
  777. radix_tree_load_root(root, &node, &maxindex);
  778. if (index > maxindex)
  779. return 0;
  780. if (node == NULL)
  781. return 0;
  782. while (radix_tree_is_internal_node(node)) {
  783. unsigned offset;
  784. parent = entry_to_node(node);
  785. offset = radix_tree_descend(parent, &node, index);
  786. if (!node)
  787. return 0;
  788. if (!tag_get(parent, tag, offset))
  789. return 0;
  790. if (node == RADIX_TREE_RETRY)
  791. break;
  792. }
  793. return 1;
  794. }
  795. EXPORT_SYMBOL(radix_tree_tag_get);
  796. static inline void __set_iter_shift(struct radix_tree_iter *iter,
  797. unsigned int shift)
  798. {
  799. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  800. iter->shift = shift;
  801. #endif
  802. }
  803. /**
  804. * radix_tree_next_chunk - find next chunk of slots for iteration
  805. *
  806. * @root: radix tree root
  807. * @iter: iterator state
  808. * @flags: RADIX_TREE_ITER_* flags and tag index
  809. * Returns: pointer to chunk first slot, or NULL if iteration is over
  810. */
  811. void **radix_tree_next_chunk(struct radix_tree_root *root,
  812. struct radix_tree_iter *iter, unsigned flags)
  813. {
  814. unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
  815. struct radix_tree_node *node, *child;
  816. unsigned long index, offset, maxindex;
  817. if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
  818. return NULL;
  819. /*
  820. * Catch next_index overflow after ~0UL. iter->index never overflows
  821. * during iterating; it can be zero only at the beginning.
  822. * And we cannot overflow iter->next_index in a single step,
  823. * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
  824. *
  825. * This condition also used by radix_tree_next_slot() to stop
  826. * contiguous iterating, and forbid swithing to the next chunk.
  827. */
  828. index = iter->next_index;
  829. if (!index && iter->index)
  830. return NULL;
  831. restart:
  832. radix_tree_load_root(root, &child, &maxindex);
  833. if (index > maxindex)
  834. return NULL;
  835. if (!child)
  836. return NULL;
  837. if (!radix_tree_is_internal_node(child)) {
  838. /* Single-slot tree */
  839. iter->index = index;
  840. iter->next_index = maxindex + 1;
  841. iter->tags = 1;
  842. __set_iter_shift(iter, 0);
  843. return (void **)&root->rnode;
  844. }
  845. do {
  846. node = entry_to_node(child);
  847. offset = radix_tree_descend(node, &child, index);
  848. if ((flags & RADIX_TREE_ITER_TAGGED) ?
  849. !tag_get(node, tag, offset) : !child) {
  850. /* Hole detected */
  851. if (flags & RADIX_TREE_ITER_CONTIG)
  852. return NULL;
  853. if (flags & RADIX_TREE_ITER_TAGGED)
  854. offset = radix_tree_find_next_bit(
  855. node->tags[tag],
  856. RADIX_TREE_MAP_SIZE,
  857. offset + 1);
  858. else
  859. while (++offset < RADIX_TREE_MAP_SIZE) {
  860. void *slot = node->slots[offset];
  861. if (is_sibling_entry(node, slot))
  862. continue;
  863. if (slot)
  864. break;
  865. }
  866. index &= ~node_maxindex(node);
  867. index += offset << node->shift;
  868. /* Overflow after ~0UL */
  869. if (!index)
  870. return NULL;
  871. if (offset == RADIX_TREE_MAP_SIZE)
  872. goto restart;
  873. child = rcu_dereference_raw(node->slots[offset]);
  874. }
  875. if ((child == NULL) || (child == RADIX_TREE_RETRY))
  876. goto restart;
  877. } while (radix_tree_is_internal_node(child));
  878. /* Update the iterator state */
  879. iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
  880. iter->next_index = (index | node_maxindex(node)) + 1;
  881. __set_iter_shift(iter, node->shift);
  882. /* Construct iter->tags bit-mask from node->tags[tag] array */
  883. if (flags & RADIX_TREE_ITER_TAGGED) {
  884. unsigned tag_long, tag_bit;
  885. tag_long = offset / BITS_PER_LONG;
  886. tag_bit = offset % BITS_PER_LONG;
  887. iter->tags = node->tags[tag][tag_long] >> tag_bit;
  888. /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
  889. if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
  890. /* Pick tags from next element */
  891. if (tag_bit)
  892. iter->tags |= node->tags[tag][tag_long + 1] <<
  893. (BITS_PER_LONG - tag_bit);
  894. /* Clip chunk size, here only BITS_PER_LONG tags */
  895. iter->next_index = index + BITS_PER_LONG;
  896. }
  897. }
  898. return node->slots + offset;
  899. }
  900. EXPORT_SYMBOL(radix_tree_next_chunk);
  901. /**
  902. * radix_tree_range_tag_if_tagged - for each item in given range set given
  903. * tag if item has another tag set
  904. * @root: radix tree root
  905. * @first_indexp: pointer to a starting index of a range to scan
  906. * @last_index: last index of a range to scan
  907. * @nr_to_tag: maximum number items to tag
  908. * @iftag: tag index to test
  909. * @settag: tag index to set if tested tag is set
  910. *
  911. * This function scans range of radix tree from first_index to last_index
  912. * (inclusive). For each item in the range if iftag is set, the function sets
  913. * also settag. The function stops either after tagging nr_to_tag items or
  914. * after reaching last_index.
  915. *
  916. * The tags must be set from the leaf level only and propagated back up the
  917. * path to the root. We must do this so that we resolve the full path before
  918. * setting any tags on intermediate nodes. If we set tags as we descend, then
  919. * we can get to the leaf node and find that the index that has the iftag
  920. * set is outside the range we are scanning. This reults in dangling tags and
  921. * can lead to problems with later tag operations (e.g. livelocks on lookups).
  922. *
  923. * The function returns the number of leaves where the tag was set and sets
  924. * *first_indexp to the first unscanned index.
  925. * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
  926. * be prepared to handle that.
  927. */
  928. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  929. unsigned long *first_indexp, unsigned long last_index,
  930. unsigned long nr_to_tag,
  931. unsigned int iftag, unsigned int settag)
  932. {
  933. struct radix_tree_node *parent, *node, *child;
  934. unsigned long maxindex;
  935. unsigned long tagged = 0;
  936. unsigned long index = *first_indexp;
  937. radix_tree_load_root(root, &child, &maxindex);
  938. last_index = min(last_index, maxindex);
  939. if (index > last_index)
  940. return 0;
  941. if (!nr_to_tag)
  942. return 0;
  943. if (!root_tag_get(root, iftag)) {
  944. *first_indexp = last_index + 1;
  945. return 0;
  946. }
  947. if (!radix_tree_is_internal_node(child)) {
  948. *first_indexp = last_index + 1;
  949. root_tag_set(root, settag);
  950. return 1;
  951. }
  952. node = entry_to_node(child);
  953. for (;;) {
  954. unsigned offset = radix_tree_descend(node, &child, index);
  955. if (!child)
  956. goto next;
  957. if (!tag_get(node, iftag, offset))
  958. goto next;
  959. /* Sibling slots never have tags set on them */
  960. if (radix_tree_is_internal_node(child)) {
  961. node = entry_to_node(child);
  962. continue;
  963. }
  964. /* tag the leaf */
  965. tagged++;
  966. tag_set(node, settag, offset);
  967. /* walk back up the path tagging interior nodes */
  968. parent = node;
  969. for (;;) {
  970. offset = parent->offset;
  971. parent = parent->parent;
  972. if (!parent)
  973. break;
  974. /* stop if we find a node with the tag already set */
  975. if (tag_get(parent, settag, offset))
  976. break;
  977. tag_set(parent, settag, offset);
  978. }
  979. next:
  980. /* Go to next entry in node */
  981. index = ((index >> node->shift) + 1) << node->shift;
  982. /* Overflow can happen when last_index is ~0UL... */
  983. if (index > last_index || !index)
  984. break;
  985. offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
  986. while (offset == 0) {
  987. /*
  988. * We've fully scanned this node. Go up. Because
  989. * last_index is guaranteed to be in the tree, what
  990. * we do below cannot wander astray.
  991. */
  992. node = node->parent;
  993. offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
  994. }
  995. if (is_sibling_entry(node, node->slots[offset]))
  996. goto next;
  997. if (tagged >= nr_to_tag)
  998. break;
  999. }
  1000. /*
  1001. * We need not to tag the root tag if there is no tag which is set with
  1002. * settag within the range from *first_indexp to last_index.
  1003. */
  1004. if (tagged > 0)
  1005. root_tag_set(root, settag);
  1006. *first_indexp = index;
  1007. return tagged;
  1008. }
  1009. EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
  1010. /**
  1011. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  1012. * @root: radix tree root
  1013. * @results: where the results of the lookup are placed
  1014. * @first_index: start the lookup from this key
  1015. * @max_items: place up to this many items at *results
  1016. *
  1017. * Performs an index-ascending scan of the tree for present items. Places
  1018. * them at *@results and returns the number of items which were placed at
  1019. * *@results.
  1020. *
  1021. * The implementation is naive.
  1022. *
  1023. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  1024. * rcu_read_lock. In this case, rather than the returned results being
  1025. * an atomic snapshot of the tree at a single point in time, the
  1026. * semantics of an RCU protected gang lookup are as though multiple
  1027. * radix_tree_lookups have been issued in individual locks, and results
  1028. * stored in 'results'.
  1029. */
  1030. unsigned int
  1031. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  1032. unsigned long first_index, unsigned int max_items)
  1033. {
  1034. struct radix_tree_iter iter;
  1035. void **slot;
  1036. unsigned int ret = 0;
  1037. if (unlikely(!max_items))
  1038. return 0;
  1039. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1040. results[ret] = rcu_dereference_raw(*slot);
  1041. if (!results[ret])
  1042. continue;
  1043. if (radix_tree_is_internal_node(results[ret])) {
  1044. slot = radix_tree_iter_retry(&iter);
  1045. continue;
  1046. }
  1047. if (++ret == max_items)
  1048. break;
  1049. }
  1050. return ret;
  1051. }
  1052. EXPORT_SYMBOL(radix_tree_gang_lookup);
  1053. /**
  1054. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  1055. * @root: radix tree root
  1056. * @results: where the results of the lookup are placed
  1057. * @indices: where their indices should be placed (but usually NULL)
  1058. * @first_index: start the lookup from this key
  1059. * @max_items: place up to this many items at *results
  1060. *
  1061. * Performs an index-ascending scan of the tree for present items. Places
  1062. * their slots at *@results and returns the number of items which were
  1063. * placed at *@results.
  1064. *
  1065. * The implementation is naive.
  1066. *
  1067. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  1068. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  1069. * protection, radix_tree_deref_slot may fail requiring a retry.
  1070. */
  1071. unsigned int
  1072. radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  1073. void ***results, unsigned long *indices,
  1074. unsigned long first_index, unsigned int max_items)
  1075. {
  1076. struct radix_tree_iter iter;
  1077. void **slot;
  1078. unsigned int ret = 0;
  1079. if (unlikely(!max_items))
  1080. return 0;
  1081. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1082. results[ret] = slot;
  1083. if (indices)
  1084. indices[ret] = iter.index;
  1085. if (++ret == max_items)
  1086. break;
  1087. }
  1088. return ret;
  1089. }
  1090. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  1091. /**
  1092. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  1093. * based on a tag
  1094. * @root: radix tree root
  1095. * @results: where the results of the lookup are placed
  1096. * @first_index: start the lookup from this key
  1097. * @max_items: place up to this many items at *results
  1098. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1099. *
  1100. * Performs an index-ascending scan of the tree for present items which
  1101. * have the tag indexed by @tag set. Places the items at *@results and
  1102. * returns the number of items which were placed at *@results.
  1103. */
  1104. unsigned int
  1105. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  1106. unsigned long first_index, unsigned int max_items,
  1107. unsigned int tag)
  1108. {
  1109. struct radix_tree_iter iter;
  1110. void **slot;
  1111. unsigned int ret = 0;
  1112. if (unlikely(!max_items))
  1113. return 0;
  1114. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1115. results[ret] = rcu_dereference_raw(*slot);
  1116. if (!results[ret])
  1117. continue;
  1118. if (radix_tree_is_internal_node(results[ret])) {
  1119. slot = radix_tree_iter_retry(&iter);
  1120. continue;
  1121. }
  1122. if (++ret == max_items)
  1123. break;
  1124. }
  1125. return ret;
  1126. }
  1127. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  1128. /**
  1129. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  1130. * radix tree based on a tag
  1131. * @root: radix tree root
  1132. * @results: where the results of the lookup are placed
  1133. * @first_index: start the lookup from this key
  1134. * @max_items: place up to this many items at *results
  1135. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1136. *
  1137. * Performs an index-ascending scan of the tree for present items which
  1138. * have the tag indexed by @tag set. Places the slots at *@results and
  1139. * returns the number of slots which were placed at *@results.
  1140. */
  1141. unsigned int
  1142. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  1143. unsigned long first_index, unsigned int max_items,
  1144. unsigned int tag)
  1145. {
  1146. struct radix_tree_iter iter;
  1147. void **slot;
  1148. unsigned int ret = 0;
  1149. if (unlikely(!max_items))
  1150. return 0;
  1151. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1152. results[ret] = slot;
  1153. if (++ret == max_items)
  1154. break;
  1155. }
  1156. return ret;
  1157. }
  1158. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  1159. #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
  1160. #include <linux/sched.h> /* for cond_resched() */
  1161. struct locate_info {
  1162. unsigned long found_index;
  1163. bool stop;
  1164. };
  1165. /*
  1166. * This linear search is at present only useful to shmem_unuse_inode().
  1167. */
  1168. static unsigned long __locate(struct radix_tree_node *slot, void *item,
  1169. unsigned long index, struct locate_info *info)
  1170. {
  1171. unsigned long i;
  1172. do {
  1173. unsigned int shift = slot->shift;
  1174. for (i = (index >> shift) & RADIX_TREE_MAP_MASK;
  1175. i < RADIX_TREE_MAP_SIZE;
  1176. i++, index += (1UL << shift)) {
  1177. struct radix_tree_node *node =
  1178. rcu_dereference_raw(slot->slots[i]);
  1179. if (node == RADIX_TREE_RETRY)
  1180. goto out;
  1181. if (!radix_tree_is_internal_node(node)) {
  1182. if (node == item) {
  1183. info->found_index = index;
  1184. info->stop = true;
  1185. goto out;
  1186. }
  1187. continue;
  1188. }
  1189. node = entry_to_node(node);
  1190. if (is_sibling_entry(slot, node))
  1191. continue;
  1192. slot = node;
  1193. break;
  1194. }
  1195. } while (i < RADIX_TREE_MAP_SIZE);
  1196. out:
  1197. if ((index == 0) && (i == RADIX_TREE_MAP_SIZE))
  1198. info->stop = true;
  1199. return index;
  1200. }
  1201. /**
  1202. * radix_tree_locate_item - search through radix tree for item
  1203. * @root: radix tree root
  1204. * @item: item to be found
  1205. *
  1206. * Returns index where item was found, or -1 if not found.
  1207. * Caller must hold no lock (since this time-consuming function needs
  1208. * to be preemptible), and must check afterwards if item is still there.
  1209. */
  1210. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1211. {
  1212. struct radix_tree_node *node;
  1213. unsigned long max_index;
  1214. unsigned long cur_index = 0;
  1215. struct locate_info info = {
  1216. .found_index = -1,
  1217. .stop = false,
  1218. };
  1219. do {
  1220. rcu_read_lock();
  1221. node = rcu_dereference_raw(root->rnode);
  1222. if (!radix_tree_is_internal_node(node)) {
  1223. rcu_read_unlock();
  1224. if (node == item)
  1225. info.found_index = 0;
  1226. break;
  1227. }
  1228. node = entry_to_node(node);
  1229. max_index = node_maxindex(node);
  1230. if (cur_index > max_index) {
  1231. rcu_read_unlock();
  1232. break;
  1233. }
  1234. cur_index = __locate(node, item, cur_index, &info);
  1235. rcu_read_unlock();
  1236. cond_resched();
  1237. } while (!info.stop && cur_index <= max_index);
  1238. return info.found_index;
  1239. }
  1240. #else
  1241. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1242. {
  1243. return -1;
  1244. }
  1245. #endif /* CONFIG_SHMEM && CONFIG_SWAP */
  1246. /**
  1247. * radix_tree_shrink - shrink radix tree to minimum height
  1248. * @root radix tree root
  1249. */
  1250. static inline bool radix_tree_shrink(struct radix_tree_root *root)
  1251. {
  1252. bool shrunk = false;
  1253. for (;;) {
  1254. struct radix_tree_node *node = root->rnode;
  1255. struct radix_tree_node *child;
  1256. if (!radix_tree_is_internal_node(node))
  1257. break;
  1258. node = entry_to_node(node);
  1259. /*
  1260. * The candidate node has more than one child, or its child
  1261. * is not at the leftmost slot, or the child is a multiorder
  1262. * entry, we cannot shrink.
  1263. */
  1264. if (node->count != 1)
  1265. break;
  1266. child = node->slots[0];
  1267. if (!child)
  1268. break;
  1269. if (!radix_tree_is_internal_node(child) && node->shift)
  1270. break;
  1271. if (radix_tree_is_internal_node(child))
  1272. entry_to_node(child)->parent = NULL;
  1273. /*
  1274. * We don't need rcu_assign_pointer(), since we are simply
  1275. * moving the node from one part of the tree to another: if it
  1276. * was safe to dereference the old pointer to it
  1277. * (node->slots[0]), it will be safe to dereference the new
  1278. * one (root->rnode) as far as dependent read barriers go.
  1279. */
  1280. root->rnode = child;
  1281. /*
  1282. * We have a dilemma here. The node's slot[0] must not be
  1283. * NULLed in case there are concurrent lookups expecting to
  1284. * find the item. However if this was a bottom-level node,
  1285. * then it may be subject to the slot pointer being visible
  1286. * to callers dereferencing it. If item corresponding to
  1287. * slot[0] is subsequently deleted, these callers would expect
  1288. * their slot to become empty sooner or later.
  1289. *
  1290. * For example, lockless pagecache will look up a slot, deref
  1291. * the page pointer, and if the page has 0 refcount it means it
  1292. * was concurrently deleted from pagecache so try the deref
  1293. * again. Fortunately there is already a requirement for logic
  1294. * to retry the entire slot lookup -- the indirect pointer
  1295. * problem (replacing direct root node with an indirect pointer
  1296. * also results in a stale slot). So tag the slot as indirect
  1297. * to force callers to retry.
  1298. */
  1299. if (!radix_tree_is_internal_node(child))
  1300. node->slots[0] = RADIX_TREE_RETRY;
  1301. radix_tree_node_free(node);
  1302. shrunk = true;
  1303. }
  1304. return shrunk;
  1305. }
  1306. /**
  1307. * __radix_tree_delete_node - try to free node after clearing a slot
  1308. * @root: radix tree root
  1309. * @node: node containing @index
  1310. *
  1311. * After clearing the slot at @index in @node from radix tree
  1312. * rooted at @root, call this function to attempt freeing the
  1313. * node and shrinking the tree.
  1314. *
  1315. * Returns %true if @node was freed, %false otherwise.
  1316. */
  1317. bool __radix_tree_delete_node(struct radix_tree_root *root,
  1318. struct radix_tree_node *node)
  1319. {
  1320. bool deleted = false;
  1321. do {
  1322. struct radix_tree_node *parent;
  1323. if (node->count) {
  1324. if (node == entry_to_node(root->rnode))
  1325. deleted |= radix_tree_shrink(root);
  1326. return deleted;
  1327. }
  1328. parent = node->parent;
  1329. if (parent) {
  1330. parent->slots[node->offset] = NULL;
  1331. parent->count--;
  1332. } else {
  1333. root_tag_clear_all(root);
  1334. root->rnode = NULL;
  1335. }
  1336. radix_tree_node_free(node);
  1337. deleted = true;
  1338. node = parent;
  1339. } while (node);
  1340. return deleted;
  1341. }
  1342. static inline void delete_sibling_entries(struct radix_tree_node *node,
  1343. void *ptr, unsigned offset)
  1344. {
  1345. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  1346. int i;
  1347. for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
  1348. if (node->slots[offset + i] != ptr)
  1349. break;
  1350. node->slots[offset + i] = NULL;
  1351. node->count--;
  1352. }
  1353. #endif
  1354. }
  1355. /**
  1356. * radix_tree_delete_item - delete an item from a radix tree
  1357. * @root: radix tree root
  1358. * @index: index key
  1359. * @item: expected item
  1360. *
  1361. * Remove @item at @index from the radix tree rooted at @root.
  1362. *
  1363. * Returns the address of the deleted item, or NULL if it was not present
  1364. * or the entry at the given @index was not @item.
  1365. */
  1366. void *radix_tree_delete_item(struct radix_tree_root *root,
  1367. unsigned long index, void *item)
  1368. {
  1369. struct radix_tree_node *node;
  1370. unsigned int offset;
  1371. void **slot;
  1372. void *entry;
  1373. int tag;
  1374. entry = __radix_tree_lookup(root, index, &node, &slot);
  1375. if (!entry)
  1376. return NULL;
  1377. if (item && entry != item)
  1378. return NULL;
  1379. if (!node) {
  1380. root_tag_clear_all(root);
  1381. root->rnode = NULL;
  1382. return entry;
  1383. }
  1384. offset = get_slot_offset(node, slot);
  1385. /* Clear all tags associated with the item to be deleted. */
  1386. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
  1387. node_tag_clear(root, node, tag, offset);
  1388. delete_sibling_entries(node, node_to_entry(slot), offset);
  1389. node->slots[offset] = NULL;
  1390. node->count--;
  1391. __radix_tree_delete_node(root, node);
  1392. return entry;
  1393. }
  1394. EXPORT_SYMBOL(radix_tree_delete_item);
  1395. /**
  1396. * radix_tree_delete - delete an item from a radix tree
  1397. * @root: radix tree root
  1398. * @index: index key
  1399. *
  1400. * Remove the item at @index from the radix tree rooted at @root.
  1401. *
  1402. * Returns the address of the deleted item, or NULL if it was not present.
  1403. */
  1404. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  1405. {
  1406. return radix_tree_delete_item(root, index, NULL);
  1407. }
  1408. EXPORT_SYMBOL(radix_tree_delete);
  1409. void radix_tree_clear_tags(struct radix_tree_root *root,
  1410. struct radix_tree_node *node,
  1411. void **slot)
  1412. {
  1413. if (node) {
  1414. unsigned int tag, offset = get_slot_offset(node, slot);
  1415. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
  1416. node_tag_clear(root, node, tag, offset);
  1417. } else {
  1418. /* Clear root node tags */
  1419. root->gfp_mask &= __GFP_BITS_MASK;
  1420. }
  1421. }
  1422. /**
  1423. * radix_tree_tagged - test whether any items in the tree are tagged
  1424. * @root: radix tree root
  1425. * @tag: tag to test
  1426. */
  1427. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1428. {
  1429. return root_tag_get(root, tag);
  1430. }
  1431. EXPORT_SYMBOL(radix_tree_tagged);
  1432. static void
  1433. radix_tree_node_ctor(void *arg)
  1434. {
  1435. struct radix_tree_node *node = arg;
  1436. memset(node, 0, sizeof(*node));
  1437. INIT_LIST_HEAD(&node->private_list);
  1438. }
  1439. static __init unsigned long __maxindex(unsigned int height)
  1440. {
  1441. unsigned int width = height * RADIX_TREE_MAP_SHIFT;
  1442. int shift = RADIX_TREE_INDEX_BITS - width;
  1443. if (shift < 0)
  1444. return ~0UL;
  1445. if (shift >= BITS_PER_LONG)
  1446. return 0UL;
  1447. return ~0UL >> shift;
  1448. }
  1449. static __init void radix_tree_init_maxnodes(void)
  1450. {
  1451. unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
  1452. unsigned int i, j;
  1453. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  1454. height_to_maxindex[i] = __maxindex(i);
  1455. for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
  1456. for (j = i; j > 0; j--)
  1457. height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
  1458. }
  1459. }
  1460. static int radix_tree_callback(struct notifier_block *nfb,
  1461. unsigned long action, void *hcpu)
  1462. {
  1463. int cpu = (long)hcpu;
  1464. struct radix_tree_preload *rtp;
  1465. struct radix_tree_node *node;
  1466. /* Free per-cpu pool of preloaded nodes */
  1467. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  1468. rtp = &per_cpu(radix_tree_preloads, cpu);
  1469. while (rtp->nr) {
  1470. node = rtp->nodes;
  1471. rtp->nodes = node->private_data;
  1472. kmem_cache_free(radix_tree_node_cachep, node);
  1473. rtp->nr--;
  1474. }
  1475. }
  1476. return NOTIFY_OK;
  1477. }
  1478. void __init radix_tree_init(void)
  1479. {
  1480. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1481. sizeof(struct radix_tree_node), 0,
  1482. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1483. radix_tree_node_ctor);
  1484. radix_tree_init_maxnodes();
  1485. hotcpu_notifier(radix_tree_callback, 0);
  1486. }