radix-tree.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/radix-tree.h>
  26. #include <linux/percpu.h>
  27. #include <linux/slab.h>
  28. #include <linux/notifier.h>
  29. #include <linux/cpu.h>
  30. #include <linux/gfp.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. struct rcu_head rcu_head;
  47. void *slots[RADIX_TREE_MAP_SIZE];
  48. unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
  49. };
  50. struct radix_tree_path {
  51. struct radix_tree_node *node;
  52. int offset;
  53. };
  54. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  55. #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
  56. RADIX_TREE_MAP_SHIFT))
  57. /*
  58. * The height_to_maxindex array needs to be one deeper than the maximum
  59. * path as height 0 holds only 1 entry.
  60. */
  61. static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
  62. /*
  63. * Radix tree node cache.
  64. */
  65. static struct kmem_cache *radix_tree_node_cachep;
  66. /*
  67. * Per-cpu pool of preloaded nodes
  68. */
  69. struct radix_tree_preload {
  70. int nr;
  71. struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
  72. };
  73. static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
  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. * This assumes that the caller has performed appropriate preallocation, and
  124. * that the caller has pinned this thread of control to the current CPU.
  125. */
  126. static struct radix_tree_node *
  127. radix_tree_node_alloc(struct radix_tree_root *root)
  128. {
  129. struct radix_tree_node *ret = NULL;
  130. gfp_t gfp_mask = root_gfp_mask(root);
  131. if (!(gfp_mask & __GFP_WAIT)) {
  132. struct radix_tree_preload *rtp;
  133. /*
  134. * Provided the caller has preloaded here, we will always
  135. * succeed in getting a node here (and never reach
  136. * kmem_cache_alloc)
  137. */
  138. rtp = &__get_cpu_var(radix_tree_preloads);
  139. if (rtp->nr) {
  140. ret = rtp->nodes[rtp->nr - 1];
  141. rtp->nodes[rtp->nr - 1] = NULL;
  142. rtp->nr--;
  143. }
  144. }
  145. if (ret == NULL)
  146. ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  147. BUG_ON(radix_tree_is_indirect_ptr(ret));
  148. return ret;
  149. }
  150. static void radix_tree_node_rcu_free(struct rcu_head *head)
  151. {
  152. struct radix_tree_node *node =
  153. container_of(head, struct radix_tree_node, rcu_head);
  154. /*
  155. * must only free zeroed nodes into the slab. radix_tree_shrink
  156. * can leave us with a non-NULL entry in the first slot, so clear
  157. * that here to make sure.
  158. */
  159. tag_clear(node, 0, 0);
  160. tag_clear(node, 1, 0);
  161. node->slots[0] = NULL;
  162. node->count = 0;
  163. kmem_cache_free(radix_tree_node_cachep, node);
  164. }
  165. static inline void
  166. radix_tree_node_free(struct radix_tree_node *node)
  167. {
  168. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  169. }
  170. /*
  171. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  172. * ensure that the addition of a single element in the tree cannot fail. On
  173. * success, return zero, with preemption disabled. On error, return -ENOMEM
  174. * with preemption not disabled.
  175. */
  176. int radix_tree_preload(gfp_t gfp_mask)
  177. {
  178. struct radix_tree_preload *rtp;
  179. struct radix_tree_node *node;
  180. int ret = -ENOMEM;
  181. preempt_disable();
  182. rtp = &__get_cpu_var(radix_tree_preloads);
  183. while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
  184. preempt_enable();
  185. node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
  186. if (node == NULL)
  187. goto out;
  188. preempt_disable();
  189. rtp = &__get_cpu_var(radix_tree_preloads);
  190. if (rtp->nr < ARRAY_SIZE(rtp->nodes))
  191. rtp->nodes[rtp->nr++] = node;
  192. else
  193. kmem_cache_free(radix_tree_node_cachep, node);
  194. }
  195. ret = 0;
  196. out:
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(radix_tree_preload);
  200. /*
  201. * Return the maximum key which can be store into a
  202. * radix tree with height HEIGHT.
  203. */
  204. static inline unsigned long radix_tree_maxindex(unsigned int height)
  205. {
  206. return height_to_maxindex[height];
  207. }
  208. /*
  209. * Extend a radix tree so it can store key @index.
  210. */
  211. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  212. {
  213. struct radix_tree_node *node;
  214. unsigned int height;
  215. int tag;
  216. /* Figure out what the height should be. */
  217. height = root->height + 1;
  218. while (index > radix_tree_maxindex(height))
  219. height++;
  220. if (root->rnode == NULL) {
  221. root->height = height;
  222. goto out;
  223. }
  224. do {
  225. unsigned int newheight;
  226. if (!(node = radix_tree_node_alloc(root)))
  227. return -ENOMEM;
  228. /* Increase the height. */
  229. node->slots[0] = radix_tree_indirect_to_ptr(root->rnode);
  230. /* Propagate the aggregated tag info into the new root */
  231. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  232. if (root_tag_get(root, tag))
  233. tag_set(node, tag, 0);
  234. }
  235. newheight = root->height+1;
  236. node->height = newheight;
  237. node->count = 1;
  238. node = radix_tree_ptr_to_indirect(node);
  239. rcu_assign_pointer(root->rnode, node);
  240. root->height = newheight;
  241. } while (height > root->height);
  242. out:
  243. return 0;
  244. }
  245. /**
  246. * radix_tree_insert - insert into a radix tree
  247. * @root: radix tree root
  248. * @index: index key
  249. * @item: item to insert
  250. *
  251. * Insert an item into the radix tree at position @index.
  252. */
  253. int radix_tree_insert(struct radix_tree_root *root,
  254. unsigned long index, void *item)
  255. {
  256. struct radix_tree_node *node = NULL, *slot;
  257. unsigned int height, shift;
  258. int offset;
  259. int error;
  260. BUG_ON(radix_tree_is_indirect_ptr(item));
  261. /* Make sure the tree is high enough. */
  262. if (index > radix_tree_maxindex(root->height)) {
  263. error = radix_tree_extend(root, index);
  264. if (error)
  265. return error;
  266. }
  267. slot = radix_tree_indirect_to_ptr(root->rnode);
  268. height = root->height;
  269. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  270. offset = 0; /* uninitialised var warning */
  271. while (height > 0) {
  272. if (slot == NULL) {
  273. /* Have to add a child node. */
  274. if (!(slot = radix_tree_node_alloc(root)))
  275. return -ENOMEM;
  276. slot->height = height;
  277. if (node) {
  278. rcu_assign_pointer(node->slots[offset], slot);
  279. node->count++;
  280. } else
  281. rcu_assign_pointer(root->rnode,
  282. radix_tree_ptr_to_indirect(slot));
  283. }
  284. /* Go a level down */
  285. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  286. node = slot;
  287. slot = node->slots[offset];
  288. shift -= RADIX_TREE_MAP_SHIFT;
  289. height--;
  290. }
  291. if (slot != NULL)
  292. return -EEXIST;
  293. if (node) {
  294. node->count++;
  295. rcu_assign_pointer(node->slots[offset], item);
  296. BUG_ON(tag_get(node, 0, offset));
  297. BUG_ON(tag_get(node, 1, offset));
  298. } else {
  299. rcu_assign_pointer(root->rnode, item);
  300. BUG_ON(root_tag_get(root, 0));
  301. BUG_ON(root_tag_get(root, 1));
  302. }
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(radix_tree_insert);
  306. /*
  307. * is_slot == 1 : search for the slot.
  308. * is_slot == 0 : search for the node.
  309. */
  310. static void *radix_tree_lookup_element(struct radix_tree_root *root,
  311. unsigned long index, int is_slot)
  312. {
  313. unsigned int height, shift;
  314. struct radix_tree_node *node, **slot;
  315. node = rcu_dereference(root->rnode);
  316. if (node == NULL)
  317. return NULL;
  318. if (!radix_tree_is_indirect_ptr(node)) {
  319. if (index > 0)
  320. return NULL;
  321. return is_slot ? (void *)&root->rnode : node;
  322. }
  323. node = radix_tree_indirect_to_ptr(node);
  324. height = node->height;
  325. if (index > radix_tree_maxindex(height))
  326. return NULL;
  327. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  328. do {
  329. slot = (struct radix_tree_node **)
  330. (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
  331. node = rcu_dereference(*slot);
  332. if (node == NULL)
  333. return NULL;
  334. shift -= RADIX_TREE_MAP_SHIFT;
  335. height--;
  336. } while (height > 0);
  337. return is_slot ? (void *)slot:node;
  338. }
  339. /**
  340. * radix_tree_lookup_slot - lookup a slot in a radix tree
  341. * @root: radix tree root
  342. * @index: index key
  343. *
  344. * Returns: the slot corresponding to the position @index in the
  345. * radix tree @root. This is useful for update-if-exists operations.
  346. *
  347. * This function can be called under rcu_read_lock iff the slot is not
  348. * modified by radix_tree_replace_slot, otherwise it must be called
  349. * exclusive from other writers. Any dereference of the slot must be done
  350. * using radix_tree_deref_slot.
  351. */
  352. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  353. {
  354. return (void **)radix_tree_lookup_element(root, index, 1);
  355. }
  356. EXPORT_SYMBOL(radix_tree_lookup_slot);
  357. /**
  358. * radix_tree_lookup - perform lookup operation on a radix tree
  359. * @root: radix tree root
  360. * @index: index key
  361. *
  362. * Lookup the item at the position @index in the radix tree @root.
  363. *
  364. * This function can be called under rcu_read_lock, however the caller
  365. * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
  366. * them safely). No RCU barriers are required to access or modify the
  367. * returned item, however.
  368. */
  369. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  370. {
  371. return radix_tree_lookup_element(root, index, 0);
  372. }
  373. EXPORT_SYMBOL(radix_tree_lookup);
  374. /**
  375. * radix_tree_tag_set - set a tag on a radix tree node
  376. * @root: radix tree root
  377. * @index: index key
  378. * @tag: tag index
  379. *
  380. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  381. * corresponding to @index in the radix tree. From
  382. * the root all the way down to the leaf node.
  383. *
  384. * Returns the address of the tagged item. Setting a tag on a not-present
  385. * item is a bug.
  386. */
  387. void *radix_tree_tag_set(struct radix_tree_root *root,
  388. unsigned long index, unsigned int tag)
  389. {
  390. unsigned int height, shift;
  391. struct radix_tree_node *slot;
  392. height = root->height;
  393. BUG_ON(index > radix_tree_maxindex(height));
  394. slot = radix_tree_indirect_to_ptr(root->rnode);
  395. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  396. while (height > 0) {
  397. int offset;
  398. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  399. if (!tag_get(slot, tag, offset))
  400. tag_set(slot, tag, offset);
  401. slot = slot->slots[offset];
  402. BUG_ON(slot == NULL);
  403. shift -= RADIX_TREE_MAP_SHIFT;
  404. height--;
  405. }
  406. /* set the root's tag bit */
  407. if (slot && !root_tag_get(root, tag))
  408. root_tag_set(root, tag);
  409. return slot;
  410. }
  411. EXPORT_SYMBOL(radix_tree_tag_set);
  412. /**
  413. * radix_tree_tag_clear - clear a tag on a radix tree node
  414. * @root: radix tree root
  415. * @index: index key
  416. * @tag: tag index
  417. *
  418. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  419. * corresponding to @index in the radix tree. If
  420. * this causes the leaf node to have no tags set then clear the tag in the
  421. * next-to-leaf node, etc.
  422. *
  423. * Returns the address of the tagged item on success, else NULL. ie:
  424. * has the same return value and semantics as radix_tree_lookup().
  425. */
  426. void *radix_tree_tag_clear(struct radix_tree_root *root,
  427. unsigned long index, unsigned int tag)
  428. {
  429. /*
  430. * The radix tree path needs to be one longer than the maximum path
  431. * since the "list" is null terminated.
  432. */
  433. struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
  434. struct radix_tree_node *slot = NULL;
  435. unsigned int height, shift;
  436. height = root->height;
  437. if (index > radix_tree_maxindex(height))
  438. goto out;
  439. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  440. pathp->node = NULL;
  441. slot = radix_tree_indirect_to_ptr(root->rnode);
  442. while (height > 0) {
  443. int offset;
  444. if (slot == NULL)
  445. goto out;
  446. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  447. pathp[1].offset = offset;
  448. pathp[1].node = slot;
  449. slot = slot->slots[offset];
  450. pathp++;
  451. shift -= RADIX_TREE_MAP_SHIFT;
  452. height--;
  453. }
  454. if (slot == NULL)
  455. goto out;
  456. while (pathp->node) {
  457. if (!tag_get(pathp->node, tag, pathp->offset))
  458. goto out;
  459. tag_clear(pathp->node, tag, pathp->offset);
  460. if (any_tag_set(pathp->node, tag))
  461. goto out;
  462. pathp--;
  463. }
  464. /* clear the root's tag bit */
  465. if (root_tag_get(root, tag))
  466. root_tag_clear(root, tag);
  467. out:
  468. return slot;
  469. }
  470. EXPORT_SYMBOL(radix_tree_tag_clear);
  471. #ifndef __KERNEL__ /* Only the test harness uses this at present */
  472. /**
  473. * radix_tree_tag_get - get a tag on a radix tree node
  474. * @root: radix tree root
  475. * @index: index key
  476. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  477. *
  478. * Return values:
  479. *
  480. * 0: tag not present or not set
  481. * 1: tag set
  482. */
  483. int radix_tree_tag_get(struct radix_tree_root *root,
  484. unsigned long index, unsigned int tag)
  485. {
  486. unsigned int height, shift;
  487. struct radix_tree_node *node;
  488. int saw_unset_tag = 0;
  489. /* check the root's tag bit */
  490. if (!root_tag_get(root, tag))
  491. return 0;
  492. node = rcu_dereference(root->rnode);
  493. if (node == NULL)
  494. return 0;
  495. if (!radix_tree_is_indirect_ptr(node))
  496. return (index == 0);
  497. node = radix_tree_indirect_to_ptr(node);
  498. height = node->height;
  499. if (index > radix_tree_maxindex(height))
  500. return 0;
  501. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  502. for ( ; ; ) {
  503. int offset;
  504. if (node == NULL)
  505. return 0;
  506. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  507. /*
  508. * This is just a debug check. Later, we can bale as soon as
  509. * we see an unset tag.
  510. */
  511. if (!tag_get(node, tag, offset))
  512. saw_unset_tag = 1;
  513. if (height == 1) {
  514. int ret = tag_get(node, tag, offset);
  515. BUG_ON(ret && saw_unset_tag);
  516. return !!ret;
  517. }
  518. node = rcu_dereference(node->slots[offset]);
  519. shift -= RADIX_TREE_MAP_SHIFT;
  520. height--;
  521. }
  522. }
  523. EXPORT_SYMBOL(radix_tree_tag_get);
  524. #endif
  525. /**
  526. * radix_tree_next_hole - find the next hole (not-present entry)
  527. * @root: tree root
  528. * @index: index key
  529. * @max_scan: maximum range to search
  530. *
  531. * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
  532. * indexed hole.
  533. *
  534. * Returns: the index of the hole if found, otherwise returns an index
  535. * outside of the set specified (in which case 'return - index >= max_scan'
  536. * will be true). In rare cases of index wrap-around, 0 will be returned.
  537. *
  538. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  539. * radix_tree_gang_lookup, this will not atomically search a snapshot of
  540. * the tree at a single point in time. For example, if a hole is created
  541. * at index 5, then subsequently a hole is created at index 10,
  542. * radix_tree_next_hole covering both indexes may return 10 if called
  543. * under rcu_read_lock.
  544. */
  545. unsigned long radix_tree_next_hole(struct radix_tree_root *root,
  546. unsigned long index, unsigned long max_scan)
  547. {
  548. unsigned long i;
  549. for (i = 0; i < max_scan; i++) {
  550. if (!radix_tree_lookup(root, index))
  551. break;
  552. index++;
  553. if (index == 0)
  554. break;
  555. }
  556. return index;
  557. }
  558. EXPORT_SYMBOL(radix_tree_next_hole);
  559. /**
  560. * radix_tree_prev_hole - find the prev hole (not-present entry)
  561. * @root: tree root
  562. * @index: index key
  563. * @max_scan: maximum range to search
  564. *
  565. * Search backwards in the range [max(index-max_scan+1, 0), index]
  566. * for the first hole.
  567. *
  568. * Returns: the index of the hole if found, otherwise returns an index
  569. * outside of the set specified (in which case 'index - return >= max_scan'
  570. * will be true). In rare cases of wrap-around, LONG_MAX will be returned.
  571. *
  572. * radix_tree_next_hole may be called under rcu_read_lock. However, like
  573. * radix_tree_gang_lookup, this will not atomically search a snapshot of
  574. * the tree at a single point in time. For example, if a hole is created
  575. * at index 10, then subsequently a hole is created at index 5,
  576. * radix_tree_prev_hole covering both indexes may return 5 if called under
  577. * rcu_read_lock.
  578. */
  579. unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
  580. unsigned long index, unsigned long max_scan)
  581. {
  582. unsigned long i;
  583. for (i = 0; i < max_scan; i++) {
  584. if (!radix_tree_lookup(root, index))
  585. break;
  586. index--;
  587. if (index == LONG_MAX)
  588. break;
  589. }
  590. return index;
  591. }
  592. EXPORT_SYMBOL(radix_tree_prev_hole);
  593. static unsigned int
  594. __lookup(struct radix_tree_node *slot, void ***results, unsigned long index,
  595. unsigned int max_items, unsigned long *next_index)
  596. {
  597. unsigned int nr_found = 0;
  598. unsigned int shift, height;
  599. unsigned long i;
  600. height = slot->height;
  601. if (height == 0)
  602. goto out;
  603. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  604. for ( ; height > 1; height--) {
  605. i = (index >> shift) & RADIX_TREE_MAP_MASK;
  606. for (;;) {
  607. if (slot->slots[i] != NULL)
  608. break;
  609. index &= ~((1UL << shift) - 1);
  610. index += 1UL << shift;
  611. if (index == 0)
  612. goto out; /* 32-bit wraparound */
  613. i++;
  614. if (i == RADIX_TREE_MAP_SIZE)
  615. goto out;
  616. }
  617. shift -= RADIX_TREE_MAP_SHIFT;
  618. slot = rcu_dereference(slot->slots[i]);
  619. if (slot == NULL)
  620. goto out;
  621. }
  622. /* Bottom level: grab some items */
  623. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  624. index++;
  625. if (slot->slots[i]) {
  626. results[nr_found++] = &(slot->slots[i]);
  627. if (nr_found == max_items)
  628. goto out;
  629. }
  630. }
  631. out:
  632. *next_index = index;
  633. return nr_found;
  634. }
  635. /**
  636. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  637. * @root: radix tree root
  638. * @results: where the results of the lookup are placed
  639. * @first_index: start the lookup from this key
  640. * @max_items: place up to this many items at *results
  641. *
  642. * Performs an index-ascending scan of the tree for present items. Places
  643. * them at *@results and returns the number of items which were placed at
  644. * *@results.
  645. *
  646. * The implementation is naive.
  647. *
  648. * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
  649. * rcu_read_lock. In this case, rather than the returned results being
  650. * an atomic snapshot of the tree at a single point in time, the semantics
  651. * of an RCU protected gang lookup are as though multiple radix_tree_lookups
  652. * have been issued in individual locks, and results stored in 'results'.
  653. */
  654. unsigned int
  655. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  656. unsigned long first_index, unsigned int max_items)
  657. {
  658. unsigned long max_index;
  659. struct radix_tree_node *node;
  660. unsigned long cur_index = first_index;
  661. unsigned int ret;
  662. node = rcu_dereference(root->rnode);
  663. if (!node)
  664. return 0;
  665. if (!radix_tree_is_indirect_ptr(node)) {
  666. if (first_index > 0)
  667. return 0;
  668. results[0] = node;
  669. return 1;
  670. }
  671. node = radix_tree_indirect_to_ptr(node);
  672. max_index = radix_tree_maxindex(node->height);
  673. ret = 0;
  674. while (ret < max_items) {
  675. unsigned int nr_found, slots_found, i;
  676. unsigned long next_index; /* Index of next search */
  677. if (cur_index > max_index)
  678. break;
  679. slots_found = __lookup(node, (void ***)results + ret, cur_index,
  680. max_items - ret, &next_index);
  681. nr_found = 0;
  682. for (i = 0; i < slots_found; i++) {
  683. struct radix_tree_node *slot;
  684. slot = *(((void ***)results)[ret + i]);
  685. if (!slot)
  686. continue;
  687. results[ret + nr_found] = rcu_dereference(slot);
  688. nr_found++;
  689. }
  690. ret += nr_found;
  691. if (next_index == 0)
  692. break;
  693. cur_index = next_index;
  694. }
  695. return ret;
  696. }
  697. EXPORT_SYMBOL(radix_tree_gang_lookup);
  698. /**
  699. * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
  700. * @root: radix tree root
  701. * @results: where the results of the lookup are placed
  702. * @first_index: start the lookup from this key
  703. * @max_items: place up to this many items at *results
  704. *
  705. * Performs an index-ascending scan of the tree for present items. Places
  706. * their slots at *@results and returns the number of items which were
  707. * placed at *@results.
  708. *
  709. * The implementation is naive.
  710. *
  711. * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
  712. * be dereferenced with radix_tree_deref_slot, and if using only RCU
  713. * protection, radix_tree_deref_slot may fail requiring a retry.
  714. */
  715. unsigned int
  716. radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
  717. unsigned long first_index, unsigned int max_items)
  718. {
  719. unsigned long max_index;
  720. struct radix_tree_node *node;
  721. unsigned long cur_index = first_index;
  722. unsigned int ret;
  723. node = rcu_dereference(root->rnode);
  724. if (!node)
  725. return 0;
  726. if (!radix_tree_is_indirect_ptr(node)) {
  727. if (first_index > 0)
  728. return 0;
  729. results[0] = (void **)&root->rnode;
  730. return 1;
  731. }
  732. node = radix_tree_indirect_to_ptr(node);
  733. max_index = radix_tree_maxindex(node->height);
  734. ret = 0;
  735. while (ret < max_items) {
  736. unsigned int slots_found;
  737. unsigned long next_index; /* Index of next search */
  738. if (cur_index > max_index)
  739. break;
  740. slots_found = __lookup(node, results + ret, cur_index,
  741. max_items - ret, &next_index);
  742. ret += slots_found;
  743. if (next_index == 0)
  744. break;
  745. cur_index = next_index;
  746. }
  747. return ret;
  748. }
  749. EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
  750. /*
  751. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  752. * open-coding the search.
  753. */
  754. static unsigned int
  755. __lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
  756. unsigned int max_items, unsigned long *next_index, unsigned int tag)
  757. {
  758. unsigned int nr_found = 0;
  759. unsigned int shift, height;
  760. height = slot->height;
  761. if (height == 0)
  762. goto out;
  763. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  764. while (height > 0) {
  765. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  766. for (;;) {
  767. if (tag_get(slot, tag, i))
  768. break;
  769. index &= ~((1UL << shift) - 1);
  770. index += 1UL << shift;
  771. if (index == 0)
  772. goto out; /* 32-bit wraparound */
  773. i++;
  774. if (i == RADIX_TREE_MAP_SIZE)
  775. goto out;
  776. }
  777. height--;
  778. if (height == 0) { /* Bottom level: grab some items */
  779. unsigned long j = index & RADIX_TREE_MAP_MASK;
  780. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  781. index++;
  782. if (!tag_get(slot, tag, j))
  783. continue;
  784. /*
  785. * Even though the tag was found set, we need to
  786. * recheck that we have a non-NULL node, because
  787. * if this lookup is lockless, it may have been
  788. * subsequently deleted.
  789. *
  790. * Similar care must be taken in any place that
  791. * lookup ->slots[x] without a lock (ie. can't
  792. * rely on its value remaining the same).
  793. */
  794. if (slot->slots[j]) {
  795. results[nr_found++] = &(slot->slots[j]);
  796. if (nr_found == max_items)
  797. goto out;
  798. }
  799. }
  800. }
  801. shift -= RADIX_TREE_MAP_SHIFT;
  802. slot = rcu_dereference(slot->slots[i]);
  803. if (slot == NULL)
  804. break;
  805. }
  806. out:
  807. *next_index = index;
  808. return nr_found;
  809. }
  810. /**
  811. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  812. * based on a tag
  813. * @root: radix tree root
  814. * @results: where the results of the lookup are placed
  815. * @first_index: start the lookup from this key
  816. * @max_items: place up to this many items at *results
  817. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  818. *
  819. * Performs an index-ascending scan of the tree for present items which
  820. * have the tag indexed by @tag set. Places the items at *@results and
  821. * returns the number of items which were placed at *@results.
  822. */
  823. unsigned int
  824. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  825. unsigned long first_index, unsigned int max_items,
  826. unsigned int tag)
  827. {
  828. struct radix_tree_node *node;
  829. unsigned long max_index;
  830. unsigned long cur_index = first_index;
  831. unsigned int ret;
  832. /* check the root's tag bit */
  833. if (!root_tag_get(root, tag))
  834. return 0;
  835. node = rcu_dereference(root->rnode);
  836. if (!node)
  837. return 0;
  838. if (!radix_tree_is_indirect_ptr(node)) {
  839. if (first_index > 0)
  840. return 0;
  841. results[0] = node;
  842. return 1;
  843. }
  844. node = radix_tree_indirect_to_ptr(node);
  845. max_index = radix_tree_maxindex(node->height);
  846. ret = 0;
  847. while (ret < max_items) {
  848. unsigned int nr_found, slots_found, i;
  849. unsigned long next_index; /* Index of next search */
  850. if (cur_index > max_index)
  851. break;
  852. slots_found = __lookup_tag(node, (void ***)results + ret,
  853. cur_index, max_items - ret, &next_index, tag);
  854. nr_found = 0;
  855. for (i = 0; i < slots_found; i++) {
  856. struct radix_tree_node *slot;
  857. slot = *(((void ***)results)[ret + i]);
  858. if (!slot)
  859. continue;
  860. results[ret + nr_found] = rcu_dereference(slot);
  861. nr_found++;
  862. }
  863. ret += nr_found;
  864. if (next_index == 0)
  865. break;
  866. cur_index = next_index;
  867. }
  868. return ret;
  869. }
  870. EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
  871. /**
  872. * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
  873. * radix tree based on a tag
  874. * @root: radix tree root
  875. * @results: where the results of the lookup are placed
  876. * @first_index: start the lookup from this key
  877. * @max_items: place up to this many items at *results
  878. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  879. *
  880. * Performs an index-ascending scan of the tree for present items which
  881. * have the tag indexed by @tag set. Places the slots at *@results and
  882. * returns the number of slots which were placed at *@results.
  883. */
  884. unsigned int
  885. radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  886. unsigned long first_index, unsigned int max_items,
  887. unsigned int tag)
  888. {
  889. struct radix_tree_node *node;
  890. unsigned long max_index;
  891. unsigned long cur_index = first_index;
  892. unsigned int ret;
  893. /* check the root's tag bit */
  894. if (!root_tag_get(root, tag))
  895. return 0;
  896. node = rcu_dereference(root->rnode);
  897. if (!node)
  898. return 0;
  899. if (!radix_tree_is_indirect_ptr(node)) {
  900. if (first_index > 0)
  901. return 0;
  902. results[0] = (void **)&root->rnode;
  903. return 1;
  904. }
  905. node = radix_tree_indirect_to_ptr(node);
  906. max_index = radix_tree_maxindex(node->height);
  907. ret = 0;
  908. while (ret < max_items) {
  909. unsigned int slots_found;
  910. unsigned long next_index; /* Index of next search */
  911. if (cur_index > max_index)
  912. break;
  913. slots_found = __lookup_tag(node, results + ret,
  914. cur_index, max_items - ret, &next_index, tag);
  915. ret += slots_found;
  916. if (next_index == 0)
  917. break;
  918. cur_index = next_index;
  919. }
  920. return ret;
  921. }
  922. EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
  923. /**
  924. * radix_tree_shrink - shrink height of a radix tree to minimal
  925. * @root radix tree root
  926. */
  927. static inline void radix_tree_shrink(struct radix_tree_root *root)
  928. {
  929. /* try to shrink tree height */
  930. while (root->height > 0) {
  931. struct radix_tree_node *to_free = root->rnode;
  932. void *newptr;
  933. BUG_ON(!radix_tree_is_indirect_ptr(to_free));
  934. to_free = radix_tree_indirect_to_ptr(to_free);
  935. /*
  936. * The candidate node has more than one child, or its child
  937. * is not at the leftmost slot, we cannot shrink.
  938. */
  939. if (to_free->count != 1)
  940. break;
  941. if (!to_free->slots[0])
  942. break;
  943. /*
  944. * We don't need rcu_assign_pointer(), since we are simply
  945. * moving the node from one part of the tree to another. If
  946. * it was safe to dereference the old pointer to it
  947. * (to_free->slots[0]), it will be safe to dereference the new
  948. * one (root->rnode).
  949. */
  950. newptr = to_free->slots[0];
  951. if (root->height > 1)
  952. newptr = radix_tree_ptr_to_indirect(newptr);
  953. root->rnode = newptr;
  954. root->height--;
  955. radix_tree_node_free(to_free);
  956. }
  957. }
  958. /**
  959. * radix_tree_delete - delete an item from a radix tree
  960. * @root: radix tree root
  961. * @index: index key
  962. *
  963. * Remove the item at @index from the radix tree rooted at @root.
  964. *
  965. * Returns the address of the deleted item, or NULL if it was not present.
  966. */
  967. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  968. {
  969. /*
  970. * The radix tree path needs to be one longer than the maximum path
  971. * since the "list" is null terminated.
  972. */
  973. struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
  974. struct radix_tree_node *slot = NULL;
  975. struct radix_tree_node *to_free;
  976. unsigned int height, shift;
  977. int tag;
  978. int offset;
  979. height = root->height;
  980. if (index > radix_tree_maxindex(height))
  981. goto out;
  982. slot = root->rnode;
  983. if (height == 0) {
  984. root_tag_clear_all(root);
  985. root->rnode = NULL;
  986. goto out;
  987. }
  988. slot = radix_tree_indirect_to_ptr(slot);
  989. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  990. pathp->node = NULL;
  991. do {
  992. if (slot == NULL)
  993. goto out;
  994. pathp++;
  995. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  996. pathp->offset = offset;
  997. pathp->node = slot;
  998. slot = slot->slots[offset];
  999. shift -= RADIX_TREE_MAP_SHIFT;
  1000. height--;
  1001. } while (height > 0);
  1002. if (slot == NULL)
  1003. goto out;
  1004. /*
  1005. * Clear all tags associated with the just-deleted item
  1006. */
  1007. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  1008. if (tag_get(pathp->node, tag, pathp->offset))
  1009. radix_tree_tag_clear(root, index, tag);
  1010. }
  1011. to_free = NULL;
  1012. /* Now free the nodes we do not need anymore */
  1013. while (pathp->node) {
  1014. pathp->node->slots[pathp->offset] = NULL;
  1015. pathp->node->count--;
  1016. /*
  1017. * Queue the node for deferred freeing after the
  1018. * last reference to it disappears (set NULL, above).
  1019. */
  1020. if (to_free)
  1021. radix_tree_node_free(to_free);
  1022. if (pathp->node->count) {
  1023. if (pathp->node ==
  1024. radix_tree_indirect_to_ptr(root->rnode))
  1025. radix_tree_shrink(root);
  1026. goto out;
  1027. }
  1028. /* Node with zero slots in use so free it */
  1029. to_free = pathp->node;
  1030. pathp--;
  1031. }
  1032. root_tag_clear_all(root);
  1033. root->height = 0;
  1034. root->rnode = NULL;
  1035. if (to_free)
  1036. radix_tree_node_free(to_free);
  1037. out:
  1038. return slot;
  1039. }
  1040. EXPORT_SYMBOL(radix_tree_delete);
  1041. /**
  1042. * radix_tree_tagged - test whether any items in the tree are tagged
  1043. * @root: radix tree root
  1044. * @tag: tag to test
  1045. */
  1046. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  1047. {
  1048. return root_tag_get(root, tag);
  1049. }
  1050. EXPORT_SYMBOL(radix_tree_tagged);
  1051. static void
  1052. radix_tree_node_ctor(void *node)
  1053. {
  1054. memset(node, 0, sizeof(struct radix_tree_node));
  1055. }
  1056. static __init unsigned long __maxindex(unsigned int height)
  1057. {
  1058. unsigned int width = height * RADIX_TREE_MAP_SHIFT;
  1059. int shift = RADIX_TREE_INDEX_BITS - width;
  1060. if (shift < 0)
  1061. return ~0UL;
  1062. if (shift >= BITS_PER_LONG)
  1063. return 0UL;
  1064. return ~0UL >> shift;
  1065. }
  1066. static __init void radix_tree_init_maxindex(void)
  1067. {
  1068. unsigned int i;
  1069. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  1070. height_to_maxindex[i] = __maxindex(i);
  1071. }
  1072. static int radix_tree_callback(struct notifier_block *nfb,
  1073. unsigned long action,
  1074. void *hcpu)
  1075. {
  1076. int cpu = (long)hcpu;
  1077. struct radix_tree_preload *rtp;
  1078. /* Free per-cpu pool of perloaded nodes */
  1079. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  1080. rtp = &per_cpu(radix_tree_preloads, cpu);
  1081. while (rtp->nr) {
  1082. kmem_cache_free(radix_tree_node_cachep,
  1083. rtp->nodes[rtp->nr-1]);
  1084. rtp->nodes[rtp->nr-1] = NULL;
  1085. rtp->nr--;
  1086. }
  1087. }
  1088. return NOTIFY_OK;
  1089. }
  1090. void __init radix_tree_init(void)
  1091. {
  1092. radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
  1093. sizeof(struct radix_tree_node), 0,
  1094. SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
  1095. radix_tree_node_ctor);
  1096. radix_tree_init_maxindex();
  1097. hotcpu_notifier(radix_tree_callback, 0);
  1098. }