radix-tree.c 38 KB

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