radix-tree.c 43 KB

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