radix-tree.c 27 KB

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