radix-tree.c 43 KB

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