radix-tree.c 36 KB

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