radix-tree.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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 *rnode, *node;
  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, &rnode, &maxindex);
  792. if (index > maxindex)
  793. return NULL;
  794. if (radix_tree_is_internal_node(rnode)) {
  795. rnode = entry_to_node(rnode);
  796. } else if (rnode) {
  797. /* Single-slot tree */
  798. iter->index = index;
  799. iter->next_index = maxindex + 1;
  800. iter->tags = 1;
  801. __set_iter_shift(iter, shift);
  802. return (void **)&root->rnode;
  803. } else
  804. return NULL;
  805. shift -= RADIX_TREE_MAP_SHIFT;
  806. offset = index >> shift;
  807. node = rnode;
  808. while (1) {
  809. struct radix_tree_node *slot;
  810. unsigned new_off = radix_tree_descend(node, &slot, offset);
  811. if (new_off < offset) {
  812. offset = new_off;
  813. index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
  814. index |= offset << shift;
  815. }
  816. if ((flags & RADIX_TREE_ITER_TAGGED) ?
  817. !tag_get(node, tag, offset) : !slot) {
  818. /* Hole detected */
  819. if (flags & RADIX_TREE_ITER_CONTIG)
  820. return NULL;
  821. if (flags & RADIX_TREE_ITER_TAGGED)
  822. offset = radix_tree_find_next_bit(
  823. node->tags[tag],
  824. RADIX_TREE_MAP_SIZE,
  825. offset + 1);
  826. else
  827. while (++offset < RADIX_TREE_MAP_SIZE) {
  828. void *slot = node->slots[offset];
  829. if (is_sibling_entry(node, slot))
  830. continue;
  831. if (slot)
  832. break;
  833. }
  834. index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
  835. index += offset << shift;
  836. /* Overflow after ~0UL */
  837. if (!index)
  838. return NULL;
  839. if (offset == RADIX_TREE_MAP_SIZE)
  840. goto restart;
  841. slot = rcu_dereference_raw(node->slots[offset]);
  842. }
  843. if ((slot == NULL) || (slot == RADIX_TREE_RETRY))
  844. goto restart;
  845. if (!radix_tree_is_internal_node(slot))
  846. break;
  847. node = entry_to_node(slot);
  848. shift -= RADIX_TREE_MAP_SHIFT;
  849. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  850. }
  851. /* Update the iterator state */
  852. iter->index = index & ~((1 << shift) - 1);
  853. iter->next_index = (index | ((RADIX_TREE_MAP_SIZE << shift) - 1)) + 1;
  854. __set_iter_shift(iter, shift);
  855. /* Construct iter->tags bit-mask from node->tags[tag] array */
  856. if (flags & RADIX_TREE_ITER_TAGGED) {
  857. unsigned tag_long, tag_bit;
  858. tag_long = offset / BITS_PER_LONG;
  859. tag_bit = offset % BITS_PER_LONG;
  860. iter->tags = node->tags[tag][tag_long] >> tag_bit;
  861. /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
  862. if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
  863. /* Pick tags from next element */
  864. if (tag_bit)
  865. iter->tags |= node->tags[tag][tag_long + 1] <<
  866. (BITS_PER_LONG - tag_bit);
  867. /* Clip chunk size, here only BITS_PER_LONG tags */
  868. iter->next_index = index + BITS_PER_LONG;
  869. }
  870. }
  871. return node->slots + offset;
  872. }
  873. EXPORT_SYMBOL(radix_tree_next_chunk);
  874. /**
  875. * radix_tree_range_tag_if_tagged - for each item in given range set given
  876. * tag if item has another tag set
  877. * @root: radix tree root
  878. * @first_indexp: pointer to a starting index of a range to scan
  879. * @last_index: last index of a range to scan
  880. * @nr_to_tag: maximum number items to tag
  881. * @iftag: tag index to test
  882. * @settag: tag index to set if tested tag is set
  883. *
  884. * This function scans range of radix tree from first_index to last_index
  885. * (inclusive). For each item in the range if iftag is set, the function sets
  886. * also settag. The function stops either after tagging nr_to_tag items or
  887. * after reaching last_index.
  888. *
  889. * The tags must be set from the leaf level only and propagated back up the
  890. * path to the root. We must do this so that we resolve the full path before
  891. * setting any tags on intermediate nodes. If we set tags as we descend, then
  892. * we can get to the leaf node and find that the index that has the iftag
  893. * set is outside the range we are scanning. This reults in dangling tags and
  894. * can lead to problems with later tag operations (e.g. livelocks on lookups).
  895. *
  896. * The function returns the number of leaves where the tag was set and sets
  897. * *first_indexp to the first unscanned index.
  898. * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
  899. * be prepared to handle that.
  900. */
  901. unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  902. unsigned long *first_indexp, unsigned long last_index,
  903. unsigned long nr_to_tag,
  904. unsigned int iftag, unsigned int settag)
  905. {
  906. struct radix_tree_node *slot, *node = NULL;
  907. unsigned long maxindex;
  908. unsigned int shift = radix_tree_load_root(root, &slot, &maxindex);
  909. unsigned long tagged = 0;
  910. unsigned long index = *first_indexp;
  911. last_index = min(last_index, maxindex);
  912. if (index > last_index)
  913. return 0;
  914. if (!nr_to_tag)
  915. return 0;
  916. if (!root_tag_get(root, iftag)) {
  917. *first_indexp = last_index + 1;
  918. return 0;
  919. }
  920. if (!radix_tree_is_internal_node(slot)) {
  921. *first_indexp = last_index + 1;
  922. root_tag_set(root, settag);
  923. return 1;
  924. }
  925. node = entry_to_node(slot);
  926. shift -= RADIX_TREE_MAP_SHIFT;
  927. for (;;) {
  928. unsigned long upindex;
  929. unsigned offset;
  930. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  931. offset = radix_tree_descend(node, &slot, offset);
  932. if (!slot)
  933. goto next;
  934. if (!tag_get(node, iftag, offset))
  935. goto next;
  936. /* Sibling slots never have tags set on them */
  937. if (radix_tree_is_internal_node(slot)) {
  938. node = entry_to_node(slot);
  939. shift -= RADIX_TREE_MAP_SHIFT;
  940. continue;
  941. }
  942. /* tag the leaf */
  943. tagged++;
  944. tag_set(node, settag, offset);
  945. slot = node->parent;
  946. /* walk back up the path tagging interior nodes */
  947. upindex = index >> shift;
  948. while (slot) {
  949. upindex >>= RADIX_TREE_MAP_SHIFT;
  950. offset = upindex & RADIX_TREE_MAP_MASK;
  951. /* stop if we find a node with the tag already set */
  952. if (tag_get(slot, settag, offset))
  953. break;
  954. tag_set(slot, settag, offset);
  955. slot = slot->parent;
  956. }
  957. next:
  958. /* Go to next item at level determined by 'shift' */
  959. index = ((index >> shift) + 1) << shift;
  960. /* Overflow can happen when last_index is ~0UL... */
  961. if (index > last_index || !index)
  962. break;
  963. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  964. while (offset == 0) {
  965. /*
  966. * We've fully scanned this node. Go up. Because
  967. * last_index is guaranteed to be in the tree, what
  968. * we do below cannot wander astray.
  969. */
  970. node = node->parent;
  971. shift += RADIX_TREE_MAP_SHIFT;
  972. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  973. }
  974. if (is_sibling_entry(node, node->slots[offset]))
  975. goto next;
  976. if (tagged >= nr_to_tag)
  977. break;
  978. }
  979. /*
  980. * We need not to tag the root tag if there is no tag which is set with
  981. * settag within the range from *first_indexp to last_index.
  982. */
  983. if (tagged > 0)
  984. root_tag_set(root, settag);
  985. *first_indexp = index;
  986. return tagged;
  987. }
  988. EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
  989. /**
  990. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  991. * @root: radix tree root
  992. * @results: where the results of the lookup are placed
  993. * @first_index: start the lookup from this key
  994. * @max_items: place up to this many items at *results
  995. *
  996. * Performs an index-ascending scan of the tree for present items. Places
  997. * them at *@results and returns the number of items which were placed at
  998. * *@results.
  999. *
  1000. * The implementation is naive.
  1001. *
  1002. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  1003. * rcu_read_lock. In this case, rather than the returned results being
  1004. * an atomic snapshot of the tree at a single point in time, the
  1005. * semantics of an RCU protected gang lookup are as though multiple
  1006. * radix_tree_lookups have been issued in individual locks, and results
  1007. * stored in 'results'.
  1008. */
  1009. unsigned int
  1010. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  1011. unsigned long first_index, unsigned int max_items)
  1012. {
  1013. struct radix_tree_iter iter;
  1014. void **slot;
  1015. unsigned int ret = 0;
  1016. if (unlikely(!max_items))
  1017. return 0;
  1018. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1019. results[ret] = rcu_dereference_raw(*slot);
  1020. if (!results[ret])
  1021. continue;
  1022. if (radix_tree_is_internal_node(results[ret])) {
  1023. slot = radix_tree_iter_retry(&iter);
  1024. continue;
  1025. }
  1026. if (++ret == max_items)
  1027. break;
  1028. }
  1029. return ret;
  1030. }
  1031. EXPORT_SYMBOL(radix_tree_gang_lookup);
  1032. /**
  1033. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  1034. * @root: radix tree root
  1035. * @results: where the results of the lookup are placed
  1036. * @indices: where their indices should be placed (but usually NULL)
  1037. * @first_index: start the lookup from this key
  1038. * @max_items: place up to this many items at *results
  1039. *
  1040. * Performs an index-ascending scan of the tree for present items. Places
  1041. * their slots at *@results and returns the number of items which were
  1042. * placed at *@results.
  1043. *
  1044. * The implementation is naive.
  1045. *
  1046. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  1047. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  1048. * protection, radix_tree_deref_slot may fail requiring a retry.
  1049. */
  1050. unsigned int
  1051. radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  1052. void ***results, unsigned long *indices,
  1053. unsigned long first_index, unsigned int max_items)
  1054. {
  1055. struct radix_tree_iter iter;
  1056. void **slot;
  1057. unsigned int ret = 0;
  1058. if (unlikely(!max_items))
  1059. return 0;
  1060. radix_tree_for_each_slot(slot, root, &iter, first_index) {
  1061. results[ret] = slot;
  1062. if (indices)
  1063. indices[ret] = iter.index;
  1064. if (++ret == max_items)
  1065. break;
  1066. }
  1067. return ret;
  1068. }
  1069. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  1070. /**
  1071. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  1072. * based on a tag
  1073. * @root: radix tree root
  1074. * @results: where the results of the lookup are placed
  1075. * @first_index: start the lookup from this key
  1076. * @max_items: place up to this many items at *results
  1077. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1078. *
  1079. * Performs an index-ascending scan of the tree for present items which
  1080. * have the tag indexed by @tag set. Places the items at *@results and
  1081. * returns the number of items which were placed at *@results.
  1082. */
  1083. unsigned int
  1084. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  1085. unsigned long first_index, unsigned int max_items,
  1086. unsigned int tag)
  1087. {
  1088. struct radix_tree_iter iter;
  1089. void **slot;
  1090. unsigned int ret = 0;
  1091. if (unlikely(!max_items))
  1092. return 0;
  1093. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1094. results[ret] = rcu_dereference_raw(*slot);
  1095. if (!results[ret])
  1096. continue;
  1097. if (radix_tree_is_internal_node(results[ret])) {
  1098. slot = radix_tree_iter_retry(&iter);
  1099. continue;
  1100. }
  1101. if (++ret == max_items)
  1102. break;
  1103. }
  1104. return ret;
  1105. }
  1106. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  1107. /**
  1108. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  1109. * radix tree based on a tag
  1110. * @root: radix tree root
  1111. * @results: where the results of the lookup are placed
  1112. * @first_index: start the lookup from this key
  1113. * @max_items: place up to this many items at *results
  1114. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  1115. *
  1116. * Performs an index-ascending scan of the tree for present items which
  1117. * have the tag indexed by @tag set. Places the slots at *@results and
  1118. * returns the number of slots which were placed at *@results.
  1119. */
  1120. unsigned int
  1121. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  1122. unsigned long first_index, unsigned int max_items,
  1123. unsigned int tag)
  1124. {
  1125. struct radix_tree_iter iter;
  1126. void **slot;
  1127. unsigned int ret = 0;
  1128. if (unlikely(!max_items))
  1129. return 0;
  1130. radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
  1131. results[ret] = slot;
  1132. if (++ret == max_items)
  1133. break;
  1134. }
  1135. return ret;
  1136. }
  1137. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  1138. #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
  1139. #include <linux/sched.h> /* for cond_resched() */
  1140. struct locate_info {
  1141. unsigned long found_index;
  1142. bool stop;
  1143. };
  1144. /*
  1145. * This linear search is at present only useful to shmem_unuse_inode().
  1146. */
  1147. static unsigned long __locate(struct radix_tree_node *slot, void *item,
  1148. unsigned long index, struct locate_info *info)
  1149. {
  1150. unsigned int shift;
  1151. unsigned long i;
  1152. shift = slot->shift + RADIX_TREE_MAP_SHIFT;
  1153. do {
  1154. shift -= RADIX_TREE_MAP_SHIFT;
  1155. for (i = (index >> shift) & RADIX_TREE_MAP_MASK;
  1156. i < RADIX_TREE_MAP_SIZE;
  1157. i++, index += (1UL << shift)) {
  1158. struct radix_tree_node *node =
  1159. rcu_dereference_raw(slot->slots[i]);
  1160. if (node == RADIX_TREE_RETRY)
  1161. goto out;
  1162. if (!radix_tree_is_internal_node(node)) {
  1163. if (node == item) {
  1164. info->found_index = index;
  1165. info->stop = true;
  1166. goto out;
  1167. }
  1168. continue;
  1169. }
  1170. node = entry_to_node(node);
  1171. if (is_sibling_entry(slot, node))
  1172. continue;
  1173. slot = node;
  1174. break;
  1175. }
  1176. if (i == RADIX_TREE_MAP_SIZE)
  1177. break;
  1178. } while (shift);
  1179. out:
  1180. if ((index == 0) && (i == RADIX_TREE_MAP_SIZE))
  1181. info->stop = true;
  1182. return index;
  1183. }
  1184. /**
  1185. * radix_tree_locate_item - search through radix tree for item
  1186. * @root: radix tree root
  1187. * @item: item to be found
  1188. *
  1189. * Returns index where item was found, or -1 if not found.
  1190. * Caller must hold no lock (since this time-consuming function needs
  1191. * to be preemptible), and must check afterwards if item is still there.
  1192. */
  1193. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1194. {
  1195. struct radix_tree_node *node;
  1196. unsigned long max_index;
  1197. unsigned long cur_index = 0;
  1198. struct locate_info info = {
  1199. .found_index = -1,
  1200. .stop = false,
  1201. };
  1202. do {
  1203. rcu_read_lock();
  1204. node = rcu_dereference_raw(root->rnode);
  1205. if (!radix_tree_is_internal_node(node)) {
  1206. rcu_read_unlock();
  1207. if (node == item)
  1208. info.found_index = 0;
  1209. break;
  1210. }
  1211. node = entry_to_node(node);
  1212. max_index = node_maxindex(node);
  1213. if (cur_index > max_index) {
  1214. rcu_read_unlock();
  1215. break;
  1216. }
  1217. cur_index = __locate(node, item, cur_index, &info);
  1218. rcu_read_unlock();
  1219. cond_resched();
  1220. } while (!info.stop && cur_index <= max_index);
  1221. return info.found_index;
  1222. }
  1223. #else
  1224. unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
  1225. {
  1226. return -1;
  1227. }
  1228. #endif /* CONFIG_SHMEM && CONFIG_SWAP */
  1229. /**
  1230. * radix_tree_shrink - shrink radix tree to minimum height
  1231. * @root radix tree root
  1232. */
  1233. static inline bool radix_tree_shrink(struct radix_tree_root *root)
  1234. {
  1235. bool shrunk = false;
  1236. for (;;) {
  1237. struct radix_tree_node *to_free = root->rnode;
  1238. struct radix_tree_node *slot;
  1239. if (!radix_tree_is_internal_node(to_free))
  1240. break;
  1241. to_free = entry_to_node(to_free);
  1242. /*
  1243. * The candidate node has more than one child, or its child
  1244. * is not at the leftmost slot, or the child is a multiorder
  1245. * entry, we cannot shrink.
  1246. */
  1247. if (to_free->count != 1)
  1248. break;
  1249. slot = to_free->slots[0];
  1250. if (!slot)
  1251. break;
  1252. if (!radix_tree_is_internal_node(slot) && to_free->shift)
  1253. break;
  1254. if (radix_tree_is_internal_node(slot))
  1255. entry_to_node(slot)->parent = NULL;
  1256. /*
  1257. * We don't need rcu_assign_pointer(), since we are simply
  1258. * moving the node from one part of the tree to another: if it
  1259. * was safe to dereference the old pointer to it
  1260. * (to_free->slots[0]), it will be safe to dereference the new
  1261. * one (root->rnode) as far as dependent read barriers go.
  1262. */
  1263. root->rnode = slot;
  1264. /*
  1265. * We have a dilemma here. The node's slot[0] must not be
  1266. * NULLed in case there are concurrent lookups expecting to
  1267. * find the item. However if this was a bottom-level node,
  1268. * then it may be subject to the slot pointer being visible
  1269. * to callers dereferencing it. If item corresponding to
  1270. * slot[0] is subsequently deleted, these callers would expect
  1271. * their slot to become empty sooner or later.
  1272. *
  1273. * For example, lockless pagecache will look up a slot, deref
  1274. * the page pointer, and if the page has 0 refcount it means it
  1275. * was concurrently deleted from pagecache so try the deref
  1276. * again. Fortunately there is already a requirement for logic
  1277. * to retry the entire slot lookup -- the indirect pointer
  1278. * problem (replacing direct root node with an indirect pointer
  1279. * also results in a stale slot). So tag the slot as indirect
  1280. * to force callers to retry.
  1281. */
  1282. if (!radix_tree_is_internal_node(slot))
  1283. to_free->slots[0] = RADIX_TREE_RETRY;
  1284. radix_tree_node_free(to_free);
  1285. shrunk = true;
  1286. }
  1287. return shrunk;
  1288. }
  1289. /**
  1290. * __radix_tree_delete_node - try to free node after clearing a slot
  1291. * @root: radix tree root
  1292. * @node: node containing @index
  1293. *
  1294. * After clearing the slot at @index in @node from radix tree
  1295. * rooted at @root, call this function to attempt freeing the
  1296. * node and shrinking the tree.
  1297. *
  1298. * Returns %true if @node was freed, %false otherwise.
  1299. */
  1300. bool __radix_tree_delete_node(struct radix_tree_root *root,
  1301. struct radix_tree_node *node)
  1302. {
  1303. bool deleted = false;
  1304. do {
  1305. struct radix_tree_node *parent;
  1306. if (node->count) {
  1307. if (node == entry_to_node(root->rnode))
  1308. deleted |= radix_tree_shrink(root);
  1309. return deleted;
  1310. }
  1311. parent = node->parent;
  1312. if (parent) {
  1313. parent->slots[node->offset] = NULL;
  1314. parent->count--;
  1315. } else {
  1316. root_tag_clear_all(root);
  1317. root->rnode = NULL;
  1318. }
  1319. radix_tree_node_free(node);
  1320. deleted = true;
  1321. node = parent;
  1322. } while (node);
  1323. return deleted;
  1324. }
  1325. static inline void delete_sibling_entries(struct radix_tree_node *node,
  1326. void *ptr, unsigned offset)
  1327. {
  1328. #ifdef CONFIG_RADIX_TREE_MULTIORDER
  1329. int i;
  1330. for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
  1331. if (node->slots[offset + i] != ptr)
  1332. break;
  1333. node->slots[offset + i] = NULL;
  1334. node->count--;
  1335. }
  1336. #endif
  1337. }
  1338. /**
  1339. * radix_tree_delete_item - delete an item from a radix tree
  1340. * @root: radix tree root
  1341. * @index: index key
  1342. * @item: expected item
  1343. *
  1344. * Remove @item at @index from the radix tree rooted at @root.
  1345. *
  1346. * Returns the address of the deleted item, or NULL if it was not present
  1347. * or the entry at the given @index was not @item.
  1348. */
  1349. void *radix_tree_delete_item(struct radix_tree_root *root,
  1350. unsigned long index, void *item)
  1351. {
  1352. struct radix_tree_node *node;
  1353. unsigned int offset;
  1354. void **slot;
  1355. void *entry;
  1356. int tag;
  1357. entry = __radix_tree_lookup(root, index, &node, &slot);
  1358. if (!entry)
  1359. return NULL;
  1360. if (item && entry != item)
  1361. return NULL;
  1362. if (!node) {
  1363. root_tag_clear_all(root);
  1364. root->rnode = NULL;
  1365. return entry;
  1366. }
  1367. offset = get_slot_offset(node, slot);
  1368. /*
  1369. * Clear all tags associated with the item to be deleted.
  1370. * This way of doing it would be inefficient, but seldom is any set.
  1371. */
  1372. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  1373. if (tag_get(node, tag, offset))
  1374. radix_tree_tag_clear(root, index, tag);
  1375. }
  1376. delete_sibling_entries(node, node_to_entry(slot), offset);
  1377. node->slots[offset] = NULL;
  1378. node->count--;
  1379. __radix_tree_delete_node(root, node);
  1380. return entry;
  1381. }
  1382. EXPORT_SYMBOL(radix_tree_delete_item);
  1383. /**
  1384. * radix_tree_delete - delete an item from a radix tree
  1385. * @root: radix tree root
  1386. * @index: index key
  1387. *
  1388. * Remove the item at @index from the radix tree rooted at @root.
  1389. *
  1390. * Returns the address of the deleted item, or NULL if it was not present.
  1391. */
  1392. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  1393. {
  1394. return radix_tree_delete_item(root, index, NULL);
  1395. }
  1396. EXPORT_SYMBOL(radix_tree_delete);
  1397. /**
  1398. * radix_tree_tagged - test whether any items in the tree are tagged
  1399. * @root: radix tree root
  1400. * @tag: tag to test
  1401. */
  1402. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1403. {
  1404. return root_tag_get(root, tag);
  1405. }
  1406. EXPORT_SYMBOL(radix_tree_tagged);
  1407. static void
  1408. radix_tree_node_ctor(void *arg)
  1409. {
  1410. struct radix_tree_node *node = arg;
  1411. memset(node, 0, sizeof(*node));
  1412. INIT_LIST_HEAD(&node->private_list);
  1413. }
  1414. static int radix_tree_callback(struct notifier_block *nfb,
  1415. unsigned long action, void *hcpu)
  1416. {
  1417. int cpu = (long)hcpu;
  1418. struct radix_tree_preload *rtp;
  1419. struct radix_tree_node *node;
  1420. /* Free per-cpu pool of preloaded nodes */
  1421. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  1422. rtp = &per_cpu(radix_tree_preloads, cpu);
  1423. while (rtp->nr) {
  1424. node = rtp->nodes;
  1425. rtp->nodes = node->private_data;
  1426. kmem_cache_free(radix_tree_node_cachep, node);
  1427. rtp->nr--;
  1428. }
  1429. }
  1430. return NOTIFY_OK;
  1431. }
  1432. void __init radix_tree_init(void)
  1433. {
  1434. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1435. sizeof(struct radix_tree_node), 0,
  1436. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1437. radix_tree_node_ctor);
  1438. hotcpu_notifier(radix_tree_callback, 0);
  1439. }