radix-tree.c 53 KB

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