radix-tree.c 40 KB

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