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