radix-tree.c 32 KB

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