radix-tree.c 27 KB

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