radix-tree.c 53 KB

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