radix-tree.c 40 KB

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