dm-cache-policy-mq.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. /*
  2. * Copyright (C) 2012 Red Hat. All rights reserved.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-cache-policy.h"
  7. #include "dm.h"
  8. #include <linux/hash.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/slab.h>
  12. #include <linux/vmalloc.h>
  13. #define DM_MSG_PREFIX "cache-policy-mq"
  14. static struct kmem_cache *mq_entry_cache;
  15. /*----------------------------------------------------------------*/
  16. static unsigned next_power(unsigned n, unsigned min)
  17. {
  18. return roundup_pow_of_two(max(n, min));
  19. }
  20. /*----------------------------------------------------------------*/
  21. static unsigned long *alloc_bitset(unsigned nr_entries)
  22. {
  23. size_t s = sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
  24. return vzalloc(s);
  25. }
  26. static void free_bitset(unsigned long *bits)
  27. {
  28. vfree(bits);
  29. }
  30. /*----------------------------------------------------------------*/
  31. /*
  32. * Large, sequential ios are probably better left on the origin device since
  33. * spindles tend to have good bandwidth.
  34. *
  35. * The io_tracker tries to spot when the io is in one of these sequential
  36. * modes.
  37. *
  38. * Two thresholds to switch between random and sequential io mode are defaulting
  39. * as follows and can be adjusted via the constructor and message interfaces.
  40. */
  41. #define RANDOM_THRESHOLD_DEFAULT 4
  42. #define SEQUENTIAL_THRESHOLD_DEFAULT 512
  43. enum io_pattern {
  44. PATTERN_SEQUENTIAL,
  45. PATTERN_RANDOM
  46. };
  47. struct io_tracker {
  48. enum io_pattern pattern;
  49. unsigned nr_seq_samples;
  50. unsigned nr_rand_samples;
  51. unsigned thresholds[2];
  52. dm_oblock_t last_end_oblock;
  53. };
  54. static void iot_init(struct io_tracker *t,
  55. int sequential_threshold, int random_threshold)
  56. {
  57. t->pattern = PATTERN_RANDOM;
  58. t->nr_seq_samples = 0;
  59. t->nr_rand_samples = 0;
  60. t->last_end_oblock = 0;
  61. t->thresholds[PATTERN_RANDOM] = random_threshold;
  62. t->thresholds[PATTERN_SEQUENTIAL] = sequential_threshold;
  63. }
  64. static enum io_pattern iot_pattern(struct io_tracker *t)
  65. {
  66. return t->pattern;
  67. }
  68. static void iot_update_stats(struct io_tracker *t, struct bio *bio)
  69. {
  70. if (bio->bi_sector == from_oblock(t->last_end_oblock) + 1)
  71. t->nr_seq_samples++;
  72. else {
  73. /*
  74. * Just one non-sequential IO is enough to reset the
  75. * counters.
  76. */
  77. if (t->nr_seq_samples) {
  78. t->nr_seq_samples = 0;
  79. t->nr_rand_samples = 0;
  80. }
  81. t->nr_rand_samples++;
  82. }
  83. t->last_end_oblock = to_oblock(bio->bi_sector + bio_sectors(bio) - 1);
  84. }
  85. static void iot_check_for_pattern_switch(struct io_tracker *t)
  86. {
  87. switch (t->pattern) {
  88. case PATTERN_SEQUENTIAL:
  89. if (t->nr_rand_samples >= t->thresholds[PATTERN_RANDOM]) {
  90. t->pattern = PATTERN_RANDOM;
  91. t->nr_seq_samples = t->nr_rand_samples = 0;
  92. }
  93. break;
  94. case PATTERN_RANDOM:
  95. if (t->nr_seq_samples >= t->thresholds[PATTERN_SEQUENTIAL]) {
  96. t->pattern = PATTERN_SEQUENTIAL;
  97. t->nr_seq_samples = t->nr_rand_samples = 0;
  98. }
  99. break;
  100. }
  101. }
  102. static void iot_examine_bio(struct io_tracker *t, struct bio *bio)
  103. {
  104. iot_update_stats(t, bio);
  105. iot_check_for_pattern_switch(t);
  106. }
  107. /*----------------------------------------------------------------*/
  108. /*
  109. * This queue is divided up into different levels. Allowing us to push
  110. * entries to the back of any of the levels. Think of it as a partially
  111. * sorted queue.
  112. */
  113. #define NR_QUEUE_LEVELS 16u
  114. struct queue {
  115. struct list_head qs[NR_QUEUE_LEVELS];
  116. };
  117. static void queue_init(struct queue *q)
  118. {
  119. unsigned i;
  120. for (i = 0; i < NR_QUEUE_LEVELS; i++)
  121. INIT_LIST_HEAD(q->qs + i);
  122. }
  123. /*
  124. * Insert an entry to the back of the given level.
  125. */
  126. static void queue_push(struct queue *q, unsigned level, struct list_head *elt)
  127. {
  128. list_add_tail(elt, q->qs + level);
  129. }
  130. static void queue_remove(struct list_head *elt)
  131. {
  132. list_del(elt);
  133. }
  134. /*
  135. * Shifts all regions down one level. This has no effect on the order of
  136. * the queue.
  137. */
  138. static void queue_shift_down(struct queue *q)
  139. {
  140. unsigned level;
  141. for (level = 1; level < NR_QUEUE_LEVELS; level++)
  142. list_splice_init(q->qs + level, q->qs + level - 1);
  143. }
  144. /*
  145. * Gives us the oldest entry of the lowest popoulated level. If the first
  146. * level is emptied then we shift down one level.
  147. */
  148. static struct list_head *queue_pop(struct queue *q)
  149. {
  150. unsigned level;
  151. struct list_head *r;
  152. for (level = 0; level < NR_QUEUE_LEVELS; level++)
  153. if (!list_empty(q->qs + level)) {
  154. r = q->qs[level].next;
  155. list_del(r);
  156. /* have we just emptied the bottom level? */
  157. if (level == 0 && list_empty(q->qs))
  158. queue_shift_down(q);
  159. return r;
  160. }
  161. return NULL;
  162. }
  163. static struct list_head *list_pop(struct list_head *lh)
  164. {
  165. struct list_head *r = lh->next;
  166. BUG_ON(!r);
  167. list_del_init(r);
  168. return r;
  169. }
  170. /*----------------------------------------------------------------*/
  171. /*
  172. * Describes a cache entry. Used in both the cache and the pre_cache.
  173. */
  174. struct entry {
  175. struct hlist_node hlist;
  176. struct list_head list;
  177. dm_oblock_t oblock;
  178. dm_cblock_t cblock; /* valid iff in_cache */
  179. /*
  180. * FIXME: pack these better
  181. */
  182. bool in_cache:1;
  183. unsigned hit_count;
  184. unsigned generation;
  185. unsigned tick;
  186. };
  187. struct mq_policy {
  188. struct dm_cache_policy policy;
  189. /* protects everything */
  190. struct mutex lock;
  191. dm_cblock_t cache_size;
  192. struct io_tracker tracker;
  193. /*
  194. * We maintain two queues of entries. The cache proper contains
  195. * the currently active mappings. Whereas the pre_cache tracks
  196. * blocks that are being hit frequently and potential candidates
  197. * for promotion to the cache.
  198. */
  199. struct queue pre_cache;
  200. struct queue cache;
  201. /*
  202. * Keeps track of time, incremented by the core. We use this to
  203. * avoid attributing multiple hits within the same tick.
  204. *
  205. * Access to tick_protected should be done with the spin lock held.
  206. * It's copied to tick at the start of the map function (within the
  207. * mutex).
  208. */
  209. spinlock_t tick_lock;
  210. unsigned tick_protected;
  211. unsigned tick;
  212. /*
  213. * A count of the number of times the map function has been called
  214. * and found an entry in the pre_cache or cache. Currently used to
  215. * calculate the generation.
  216. */
  217. unsigned hit_count;
  218. /*
  219. * A generation is a longish period that is used to trigger some
  220. * book keeping effects. eg, decrementing hit counts on entries.
  221. * This is needed to allow the cache to evolve as io patterns
  222. * change.
  223. */
  224. unsigned generation;
  225. unsigned generation_period; /* in lookups (will probably change) */
  226. /*
  227. * Entries in the pre_cache whose hit count passes the promotion
  228. * threshold move to the cache proper. Working out the correct
  229. * value for the promotion_threshold is crucial to this policy.
  230. */
  231. unsigned promote_threshold;
  232. /*
  233. * We need cache_size entries for the cache, and choose to have
  234. * cache_size entries for the pre_cache too. One motivation for
  235. * using the same size is to make the hit counts directly
  236. * comparable between pre_cache and cache.
  237. */
  238. unsigned nr_entries;
  239. unsigned nr_entries_allocated;
  240. struct list_head free;
  241. /*
  242. * Cache blocks may be unallocated. We store this info in a
  243. * bitset.
  244. */
  245. unsigned long *allocation_bitset;
  246. unsigned nr_cblocks_allocated;
  247. unsigned find_free_nr_words;
  248. unsigned find_free_last_word;
  249. /*
  250. * The hash table allows us to quickly find an entry by origin
  251. * block. Both pre_cache and cache entries are in here.
  252. */
  253. unsigned nr_buckets;
  254. dm_block_t hash_bits;
  255. struct hlist_head *table;
  256. };
  257. /*----------------------------------------------------------------*/
  258. /* Free/alloc mq cache entry structures. */
  259. static void takeout_queue(struct list_head *lh, struct queue *q)
  260. {
  261. unsigned level;
  262. for (level = 0; level < NR_QUEUE_LEVELS; level++)
  263. list_splice(q->qs + level, lh);
  264. }
  265. static void free_entries(struct mq_policy *mq)
  266. {
  267. struct entry *e, *tmp;
  268. takeout_queue(&mq->free, &mq->pre_cache);
  269. takeout_queue(&mq->free, &mq->cache);
  270. list_for_each_entry_safe(e, tmp, &mq->free, list)
  271. kmem_cache_free(mq_entry_cache, e);
  272. }
  273. static int alloc_entries(struct mq_policy *mq, unsigned elts)
  274. {
  275. unsigned u = mq->nr_entries;
  276. INIT_LIST_HEAD(&mq->free);
  277. mq->nr_entries_allocated = 0;
  278. while (u--) {
  279. struct entry *e = kmem_cache_zalloc(mq_entry_cache, GFP_KERNEL);
  280. if (!e) {
  281. free_entries(mq);
  282. return -ENOMEM;
  283. }
  284. list_add(&e->list, &mq->free);
  285. }
  286. return 0;
  287. }
  288. /*----------------------------------------------------------------*/
  289. /*
  290. * Simple hash table implementation. Should replace with the standard hash
  291. * table that's making its way upstream.
  292. */
  293. static void hash_insert(struct mq_policy *mq, struct entry *e)
  294. {
  295. unsigned h = hash_64(from_oblock(e->oblock), mq->hash_bits);
  296. hlist_add_head(&e->hlist, mq->table + h);
  297. }
  298. static struct entry *hash_lookup(struct mq_policy *mq, dm_oblock_t oblock)
  299. {
  300. unsigned h = hash_64(from_oblock(oblock), mq->hash_bits);
  301. struct hlist_head *bucket = mq->table + h;
  302. struct entry *e;
  303. hlist_for_each_entry(e, bucket, hlist)
  304. if (e->oblock == oblock) {
  305. hlist_del(&e->hlist);
  306. hlist_add_head(&e->hlist, bucket);
  307. return e;
  308. }
  309. return NULL;
  310. }
  311. static void hash_remove(struct entry *e)
  312. {
  313. hlist_del(&e->hlist);
  314. }
  315. /*----------------------------------------------------------------*/
  316. /*
  317. * Allocates a new entry structure. The memory is allocated in one lump,
  318. * so we just handing it out here. Returns NULL if all entries have
  319. * already been allocated. Cannot fail otherwise.
  320. */
  321. static struct entry *alloc_entry(struct mq_policy *mq)
  322. {
  323. struct entry *e;
  324. if (mq->nr_entries_allocated >= mq->nr_entries) {
  325. BUG_ON(!list_empty(&mq->free));
  326. return NULL;
  327. }
  328. e = list_entry(list_pop(&mq->free), struct entry, list);
  329. INIT_LIST_HEAD(&e->list);
  330. INIT_HLIST_NODE(&e->hlist);
  331. mq->nr_entries_allocated++;
  332. return e;
  333. }
  334. /*----------------------------------------------------------------*/
  335. /*
  336. * Mark cache blocks allocated or not in the bitset.
  337. */
  338. static void alloc_cblock(struct mq_policy *mq, dm_cblock_t cblock)
  339. {
  340. BUG_ON(from_cblock(cblock) > from_cblock(mq->cache_size));
  341. BUG_ON(test_bit(from_cblock(cblock), mq->allocation_bitset));
  342. set_bit(from_cblock(cblock), mq->allocation_bitset);
  343. mq->nr_cblocks_allocated++;
  344. }
  345. static void free_cblock(struct mq_policy *mq, dm_cblock_t cblock)
  346. {
  347. BUG_ON(from_cblock(cblock) > from_cblock(mq->cache_size));
  348. BUG_ON(!test_bit(from_cblock(cblock), mq->allocation_bitset));
  349. clear_bit(from_cblock(cblock), mq->allocation_bitset);
  350. mq->nr_cblocks_allocated--;
  351. }
  352. static bool any_free_cblocks(struct mq_policy *mq)
  353. {
  354. return mq->nr_cblocks_allocated < from_cblock(mq->cache_size);
  355. }
  356. /*
  357. * Fills result out with a cache block that isn't in use, or return
  358. * -ENOSPC. This does _not_ mark the cblock as allocated, the caller is
  359. * reponsible for that.
  360. */
  361. static int __find_free_cblock(struct mq_policy *mq, unsigned begin, unsigned end,
  362. dm_cblock_t *result, unsigned *last_word)
  363. {
  364. int r = -ENOSPC;
  365. unsigned w;
  366. for (w = begin; w < end; w++) {
  367. /*
  368. * ffz is undefined if no zero exists
  369. */
  370. if (mq->allocation_bitset[w] != ~0UL) {
  371. *last_word = w;
  372. *result = to_cblock((w * BITS_PER_LONG) + ffz(mq->allocation_bitset[w]));
  373. if (from_cblock(*result) < from_cblock(mq->cache_size))
  374. r = 0;
  375. break;
  376. }
  377. }
  378. return r;
  379. }
  380. static int find_free_cblock(struct mq_policy *mq, dm_cblock_t *result)
  381. {
  382. int r;
  383. if (!any_free_cblocks(mq))
  384. return -ENOSPC;
  385. r = __find_free_cblock(mq, mq->find_free_last_word, mq->find_free_nr_words, result, &mq->find_free_last_word);
  386. if (r == -ENOSPC && mq->find_free_last_word)
  387. r = __find_free_cblock(mq, 0, mq->find_free_last_word, result, &mq->find_free_last_word);
  388. return r;
  389. }
  390. /*----------------------------------------------------------------*/
  391. /*
  392. * Now we get to the meat of the policy. This section deals with deciding
  393. * when to to add entries to the pre_cache and cache, and move between
  394. * them.
  395. */
  396. /*
  397. * The queue level is based on the log2 of the hit count.
  398. */
  399. static unsigned queue_level(struct entry *e)
  400. {
  401. return min((unsigned) ilog2(e->hit_count), NR_QUEUE_LEVELS - 1u);
  402. }
  403. /*
  404. * Inserts the entry into the pre_cache or the cache. Ensures the cache
  405. * block is marked as allocated if necc. Inserts into the hash table. Sets the
  406. * tick which records when the entry was last moved about.
  407. */
  408. static void push(struct mq_policy *mq, struct entry *e)
  409. {
  410. e->tick = mq->tick;
  411. hash_insert(mq, e);
  412. if (e->in_cache) {
  413. alloc_cblock(mq, e->cblock);
  414. queue_push(&mq->cache, queue_level(e), &e->list);
  415. } else
  416. queue_push(&mq->pre_cache, queue_level(e), &e->list);
  417. }
  418. /*
  419. * Removes an entry from pre_cache or cache. Removes from the hash table.
  420. * Frees off the cache block if necc.
  421. */
  422. static void del(struct mq_policy *mq, struct entry *e)
  423. {
  424. queue_remove(&e->list);
  425. hash_remove(e);
  426. if (e->in_cache)
  427. free_cblock(mq, e->cblock);
  428. }
  429. /*
  430. * Like del, except it removes the first entry in the queue (ie. the least
  431. * recently used).
  432. */
  433. static struct entry *pop(struct mq_policy *mq, struct queue *q)
  434. {
  435. struct entry *e = container_of(queue_pop(q), struct entry, list);
  436. if (e) {
  437. hash_remove(e);
  438. if (e->in_cache)
  439. free_cblock(mq, e->cblock);
  440. }
  441. return e;
  442. }
  443. /*
  444. * Has this entry already been updated?
  445. */
  446. static bool updated_this_tick(struct mq_policy *mq, struct entry *e)
  447. {
  448. return mq->tick == e->tick;
  449. }
  450. /*
  451. * The promotion threshold is adjusted every generation. As are the counts
  452. * of the entries.
  453. *
  454. * At the moment the threshold is taken by averaging the hit counts of some
  455. * of the entries in the cache (the first 20 entries of the first level).
  456. *
  457. * We can be much cleverer than this though. For example, each promotion
  458. * could bump up the threshold helping to prevent churn. Much more to do
  459. * here.
  460. */
  461. #define MAX_TO_AVERAGE 20
  462. static void check_generation(struct mq_policy *mq)
  463. {
  464. unsigned total = 0, nr = 0, count = 0, level;
  465. struct list_head *head;
  466. struct entry *e;
  467. if ((mq->hit_count >= mq->generation_period) &&
  468. (mq->nr_cblocks_allocated == from_cblock(mq->cache_size))) {
  469. mq->hit_count = 0;
  470. mq->generation++;
  471. for (level = 0; level < NR_QUEUE_LEVELS && count < MAX_TO_AVERAGE; level++) {
  472. head = mq->cache.qs + level;
  473. list_for_each_entry(e, head, list) {
  474. nr++;
  475. total += e->hit_count;
  476. if (++count >= MAX_TO_AVERAGE)
  477. break;
  478. }
  479. }
  480. mq->promote_threshold = nr ? total / nr : 1;
  481. if (mq->promote_threshold * nr < total)
  482. mq->promote_threshold++;
  483. }
  484. }
  485. /*
  486. * Whenever we use an entry we bump up it's hit counter, and push it to the
  487. * back to it's current level.
  488. */
  489. static void requeue_and_update_tick(struct mq_policy *mq, struct entry *e)
  490. {
  491. if (updated_this_tick(mq, e))
  492. return;
  493. e->hit_count++;
  494. mq->hit_count++;
  495. check_generation(mq);
  496. /* generation adjustment, to stop the counts increasing forever. */
  497. /* FIXME: divide? */
  498. /* e->hit_count -= min(e->hit_count - 1, mq->generation - e->generation); */
  499. e->generation = mq->generation;
  500. del(mq, e);
  501. push(mq, e);
  502. }
  503. /*
  504. * Demote the least recently used entry from the cache to the pre_cache.
  505. * Returns the new cache entry to use, and the old origin block it was
  506. * mapped to.
  507. *
  508. * We drop the hit count on the demoted entry back to 1 to stop it bouncing
  509. * straight back into the cache if it's subsequently hit. There are
  510. * various options here, and more experimentation would be good:
  511. *
  512. * - just forget about the demoted entry completely (ie. don't insert it
  513. into the pre_cache).
  514. * - divide the hit count rather that setting to some hard coded value.
  515. * - set the hit count to a hard coded value other than 1, eg, is it better
  516. * if it goes in at level 2?
  517. */
  518. static dm_cblock_t demote_cblock(struct mq_policy *mq, dm_oblock_t *oblock)
  519. {
  520. dm_cblock_t result;
  521. struct entry *demoted = pop(mq, &mq->cache);
  522. BUG_ON(!demoted);
  523. result = demoted->cblock;
  524. *oblock = demoted->oblock;
  525. demoted->in_cache = false;
  526. demoted->hit_count = 1;
  527. push(mq, demoted);
  528. return result;
  529. }
  530. /*
  531. * We modify the basic promotion_threshold depending on the specific io.
  532. *
  533. * If the origin block has been discarded then there's no cost to copy it
  534. * to the cache.
  535. *
  536. * We bias towards reads, since they can be demoted at no cost if they
  537. * haven't been dirtied.
  538. */
  539. #define DISCARDED_PROMOTE_THRESHOLD 1
  540. #define READ_PROMOTE_THRESHOLD 4
  541. #define WRITE_PROMOTE_THRESHOLD 8
  542. static unsigned adjusted_promote_threshold(struct mq_policy *mq,
  543. bool discarded_oblock, int data_dir)
  544. {
  545. if (discarded_oblock && any_free_cblocks(mq) && data_dir == WRITE)
  546. /*
  547. * We don't need to do any copying at all, so give this a
  548. * very low threshold. In practice this only triggers
  549. * during initial population after a format.
  550. */
  551. return DISCARDED_PROMOTE_THRESHOLD;
  552. return data_dir == READ ?
  553. (mq->promote_threshold + READ_PROMOTE_THRESHOLD) :
  554. (mq->promote_threshold + WRITE_PROMOTE_THRESHOLD);
  555. }
  556. static bool should_promote(struct mq_policy *mq, struct entry *e,
  557. bool discarded_oblock, int data_dir)
  558. {
  559. return e->hit_count >=
  560. adjusted_promote_threshold(mq, discarded_oblock, data_dir);
  561. }
  562. static int cache_entry_found(struct mq_policy *mq,
  563. struct entry *e,
  564. struct policy_result *result)
  565. {
  566. requeue_and_update_tick(mq, e);
  567. if (e->in_cache) {
  568. result->op = POLICY_HIT;
  569. result->cblock = e->cblock;
  570. }
  571. return 0;
  572. }
  573. /*
  574. * Moves and entry from the pre_cache to the cache. The main work is
  575. * finding which cache block to use.
  576. */
  577. static int pre_cache_to_cache(struct mq_policy *mq, struct entry *e,
  578. struct policy_result *result)
  579. {
  580. dm_cblock_t cblock;
  581. if (find_free_cblock(mq, &cblock) == -ENOSPC) {
  582. result->op = POLICY_REPLACE;
  583. cblock = demote_cblock(mq, &result->old_oblock);
  584. } else
  585. result->op = POLICY_NEW;
  586. result->cblock = e->cblock = cblock;
  587. del(mq, e);
  588. e->in_cache = true;
  589. push(mq, e);
  590. return 0;
  591. }
  592. static int pre_cache_entry_found(struct mq_policy *mq, struct entry *e,
  593. bool can_migrate, bool discarded_oblock,
  594. int data_dir, struct policy_result *result)
  595. {
  596. int r = 0;
  597. bool updated = updated_this_tick(mq, e);
  598. requeue_and_update_tick(mq, e);
  599. if ((!discarded_oblock && updated) ||
  600. !should_promote(mq, e, discarded_oblock, data_dir))
  601. result->op = POLICY_MISS;
  602. else if (!can_migrate)
  603. r = -EWOULDBLOCK;
  604. else
  605. r = pre_cache_to_cache(mq, e, result);
  606. return r;
  607. }
  608. static void insert_in_pre_cache(struct mq_policy *mq,
  609. dm_oblock_t oblock)
  610. {
  611. struct entry *e = alloc_entry(mq);
  612. if (!e)
  613. /*
  614. * There's no spare entry structure, so we grab the least
  615. * used one from the pre_cache.
  616. */
  617. e = pop(mq, &mq->pre_cache);
  618. if (unlikely(!e)) {
  619. DMWARN("couldn't pop from pre cache");
  620. return;
  621. }
  622. e->in_cache = false;
  623. e->oblock = oblock;
  624. e->hit_count = 1;
  625. e->generation = mq->generation;
  626. push(mq, e);
  627. }
  628. static void insert_in_cache(struct mq_policy *mq, dm_oblock_t oblock,
  629. struct policy_result *result)
  630. {
  631. struct entry *e;
  632. dm_cblock_t cblock;
  633. if (find_free_cblock(mq, &cblock) == -ENOSPC) {
  634. result->op = POLICY_MISS;
  635. insert_in_pre_cache(mq, oblock);
  636. return;
  637. }
  638. e = alloc_entry(mq);
  639. if (unlikely(!e)) {
  640. result->op = POLICY_MISS;
  641. return;
  642. }
  643. e->oblock = oblock;
  644. e->cblock = cblock;
  645. e->in_cache = true;
  646. e->hit_count = 1;
  647. e->generation = mq->generation;
  648. push(mq, e);
  649. result->op = POLICY_NEW;
  650. result->cblock = e->cblock;
  651. }
  652. static int no_entry_found(struct mq_policy *mq, dm_oblock_t oblock,
  653. bool can_migrate, bool discarded_oblock,
  654. int data_dir, struct policy_result *result)
  655. {
  656. if (adjusted_promote_threshold(mq, discarded_oblock, data_dir) == 1) {
  657. if (can_migrate)
  658. insert_in_cache(mq, oblock, result);
  659. else
  660. return -EWOULDBLOCK;
  661. } else {
  662. insert_in_pre_cache(mq, oblock);
  663. result->op = POLICY_MISS;
  664. }
  665. return 0;
  666. }
  667. /*
  668. * Looks the oblock up in the hash table, then decides whether to put in
  669. * pre_cache, or cache etc.
  670. */
  671. static int map(struct mq_policy *mq, dm_oblock_t oblock,
  672. bool can_migrate, bool discarded_oblock,
  673. int data_dir, struct policy_result *result)
  674. {
  675. int r = 0;
  676. struct entry *e = hash_lookup(mq, oblock);
  677. if (e && e->in_cache)
  678. r = cache_entry_found(mq, e, result);
  679. else if (iot_pattern(&mq->tracker) == PATTERN_SEQUENTIAL)
  680. result->op = POLICY_MISS;
  681. else if (e)
  682. r = pre_cache_entry_found(mq, e, can_migrate, discarded_oblock,
  683. data_dir, result);
  684. else
  685. r = no_entry_found(mq, oblock, can_migrate, discarded_oblock,
  686. data_dir, result);
  687. if (r == -EWOULDBLOCK)
  688. result->op = POLICY_MISS;
  689. return r;
  690. }
  691. /*----------------------------------------------------------------*/
  692. /*
  693. * Public interface, via the policy struct. See dm-cache-policy.h for a
  694. * description of these.
  695. */
  696. static struct mq_policy *to_mq_policy(struct dm_cache_policy *p)
  697. {
  698. return container_of(p, struct mq_policy, policy);
  699. }
  700. static void mq_destroy(struct dm_cache_policy *p)
  701. {
  702. struct mq_policy *mq = to_mq_policy(p);
  703. free_bitset(mq->allocation_bitset);
  704. kfree(mq->table);
  705. free_entries(mq);
  706. kfree(mq);
  707. }
  708. static void copy_tick(struct mq_policy *mq)
  709. {
  710. unsigned long flags;
  711. spin_lock_irqsave(&mq->tick_lock, flags);
  712. mq->tick = mq->tick_protected;
  713. spin_unlock_irqrestore(&mq->tick_lock, flags);
  714. }
  715. static int mq_map(struct dm_cache_policy *p, dm_oblock_t oblock,
  716. bool can_block, bool can_migrate, bool discarded_oblock,
  717. struct bio *bio, struct policy_result *result)
  718. {
  719. int r;
  720. struct mq_policy *mq = to_mq_policy(p);
  721. result->op = POLICY_MISS;
  722. if (can_block)
  723. mutex_lock(&mq->lock);
  724. else if (!mutex_trylock(&mq->lock))
  725. return -EWOULDBLOCK;
  726. copy_tick(mq);
  727. iot_examine_bio(&mq->tracker, bio);
  728. r = map(mq, oblock, can_migrate, discarded_oblock,
  729. bio_data_dir(bio), result);
  730. mutex_unlock(&mq->lock);
  731. return r;
  732. }
  733. static int mq_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock)
  734. {
  735. int r;
  736. struct mq_policy *mq = to_mq_policy(p);
  737. struct entry *e;
  738. if (!mutex_trylock(&mq->lock))
  739. return -EWOULDBLOCK;
  740. e = hash_lookup(mq, oblock);
  741. if (e && e->in_cache) {
  742. *cblock = e->cblock;
  743. r = 0;
  744. } else
  745. r = -ENOENT;
  746. mutex_unlock(&mq->lock);
  747. return r;
  748. }
  749. static int mq_load_mapping(struct dm_cache_policy *p,
  750. dm_oblock_t oblock, dm_cblock_t cblock,
  751. uint32_t hint, bool hint_valid)
  752. {
  753. struct mq_policy *mq = to_mq_policy(p);
  754. struct entry *e;
  755. e = alloc_entry(mq);
  756. if (!e)
  757. return -ENOMEM;
  758. e->cblock = cblock;
  759. e->oblock = oblock;
  760. e->in_cache = true;
  761. e->hit_count = hint_valid ? hint : 1;
  762. e->generation = mq->generation;
  763. push(mq, e);
  764. return 0;
  765. }
  766. static int mq_walk_mappings(struct dm_cache_policy *p, policy_walk_fn fn,
  767. void *context)
  768. {
  769. struct mq_policy *mq = to_mq_policy(p);
  770. int r = 0;
  771. struct entry *e;
  772. unsigned level;
  773. mutex_lock(&mq->lock);
  774. for (level = 0; level < NR_QUEUE_LEVELS; level++)
  775. list_for_each_entry(e, &mq->cache.qs[level], list) {
  776. r = fn(context, e->cblock, e->oblock, e->hit_count);
  777. if (r)
  778. goto out;
  779. }
  780. out:
  781. mutex_unlock(&mq->lock);
  782. return r;
  783. }
  784. static void remove_mapping(struct mq_policy *mq, dm_oblock_t oblock)
  785. {
  786. struct entry *e = hash_lookup(mq, oblock);
  787. BUG_ON(!e || !e->in_cache);
  788. del(mq, e);
  789. e->in_cache = false;
  790. push(mq, e);
  791. }
  792. static void mq_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
  793. {
  794. struct mq_policy *mq = to_mq_policy(p);
  795. mutex_lock(&mq->lock);
  796. remove_mapping(mq, oblock);
  797. mutex_unlock(&mq->lock);
  798. }
  799. static void force_mapping(struct mq_policy *mq,
  800. dm_oblock_t current_oblock, dm_oblock_t new_oblock)
  801. {
  802. struct entry *e = hash_lookup(mq, current_oblock);
  803. BUG_ON(!e || !e->in_cache);
  804. del(mq, e);
  805. e->oblock = new_oblock;
  806. push(mq, e);
  807. }
  808. static void mq_force_mapping(struct dm_cache_policy *p,
  809. dm_oblock_t current_oblock, dm_oblock_t new_oblock)
  810. {
  811. struct mq_policy *mq = to_mq_policy(p);
  812. mutex_lock(&mq->lock);
  813. force_mapping(mq, current_oblock, new_oblock);
  814. mutex_unlock(&mq->lock);
  815. }
  816. static dm_cblock_t mq_residency(struct dm_cache_policy *p)
  817. {
  818. struct mq_policy *mq = to_mq_policy(p);
  819. /* FIXME: lock mutex, not sure we can block here */
  820. return to_cblock(mq->nr_cblocks_allocated);
  821. }
  822. static void mq_tick(struct dm_cache_policy *p)
  823. {
  824. struct mq_policy *mq = to_mq_policy(p);
  825. unsigned long flags;
  826. spin_lock_irqsave(&mq->tick_lock, flags);
  827. mq->tick_protected++;
  828. spin_unlock_irqrestore(&mq->tick_lock, flags);
  829. }
  830. static int mq_set_config_value(struct dm_cache_policy *p,
  831. const char *key, const char *value)
  832. {
  833. struct mq_policy *mq = to_mq_policy(p);
  834. enum io_pattern pattern;
  835. unsigned long tmp;
  836. if (!strcasecmp(key, "random_threshold"))
  837. pattern = PATTERN_RANDOM;
  838. else if (!strcasecmp(key, "sequential_threshold"))
  839. pattern = PATTERN_SEQUENTIAL;
  840. else
  841. return -EINVAL;
  842. if (kstrtoul(value, 10, &tmp))
  843. return -EINVAL;
  844. mq->tracker.thresholds[pattern] = tmp;
  845. return 0;
  846. }
  847. static int mq_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen)
  848. {
  849. ssize_t sz = 0;
  850. struct mq_policy *mq = to_mq_policy(p);
  851. DMEMIT("4 random_threshold %u sequential_threshold %u",
  852. mq->tracker.thresholds[PATTERN_RANDOM],
  853. mq->tracker.thresholds[PATTERN_SEQUENTIAL]);
  854. return 0;
  855. }
  856. /* Init the policy plugin interface function pointers. */
  857. static void init_policy_functions(struct mq_policy *mq)
  858. {
  859. mq->policy.destroy = mq_destroy;
  860. mq->policy.map = mq_map;
  861. mq->policy.lookup = mq_lookup;
  862. mq->policy.load_mapping = mq_load_mapping;
  863. mq->policy.walk_mappings = mq_walk_mappings;
  864. mq->policy.remove_mapping = mq_remove_mapping;
  865. mq->policy.writeback_work = NULL;
  866. mq->policy.force_mapping = mq_force_mapping;
  867. mq->policy.residency = mq_residency;
  868. mq->policy.tick = mq_tick;
  869. mq->policy.emit_config_values = mq_emit_config_values;
  870. mq->policy.set_config_value = mq_set_config_value;
  871. }
  872. static struct dm_cache_policy *mq_create(dm_cblock_t cache_size,
  873. sector_t origin_size,
  874. sector_t cache_block_size)
  875. {
  876. int r;
  877. struct mq_policy *mq = kzalloc(sizeof(*mq), GFP_KERNEL);
  878. if (!mq)
  879. return NULL;
  880. init_policy_functions(mq);
  881. iot_init(&mq->tracker, SEQUENTIAL_THRESHOLD_DEFAULT, RANDOM_THRESHOLD_DEFAULT);
  882. mq->cache_size = cache_size;
  883. mq->tick_protected = 0;
  884. mq->tick = 0;
  885. mq->hit_count = 0;
  886. mq->generation = 0;
  887. mq->promote_threshold = 0;
  888. mutex_init(&mq->lock);
  889. spin_lock_init(&mq->tick_lock);
  890. mq->find_free_nr_words = dm_div_up(from_cblock(mq->cache_size), BITS_PER_LONG);
  891. mq->find_free_last_word = 0;
  892. queue_init(&mq->pre_cache);
  893. queue_init(&mq->cache);
  894. mq->generation_period = max((unsigned) from_cblock(cache_size), 1024U);
  895. mq->nr_entries = 2 * from_cblock(cache_size);
  896. r = alloc_entries(mq, mq->nr_entries);
  897. if (r)
  898. goto bad_cache_alloc;
  899. mq->nr_entries_allocated = 0;
  900. mq->nr_cblocks_allocated = 0;
  901. mq->nr_buckets = next_power(from_cblock(cache_size) / 2, 16);
  902. mq->hash_bits = ffs(mq->nr_buckets) - 1;
  903. mq->table = kzalloc(sizeof(*mq->table) * mq->nr_buckets, GFP_KERNEL);
  904. if (!mq->table)
  905. goto bad_alloc_table;
  906. mq->allocation_bitset = alloc_bitset(from_cblock(cache_size));
  907. if (!mq->allocation_bitset)
  908. goto bad_alloc_bitset;
  909. return &mq->policy;
  910. bad_alloc_bitset:
  911. kfree(mq->table);
  912. bad_alloc_table:
  913. free_entries(mq);
  914. bad_cache_alloc:
  915. kfree(mq);
  916. return NULL;
  917. }
  918. /*----------------------------------------------------------------*/
  919. static struct dm_cache_policy_type mq_policy_type = {
  920. .name = "mq",
  921. .version = {1, 0, 0},
  922. .hint_size = 4,
  923. .owner = THIS_MODULE,
  924. .create = mq_create
  925. };
  926. static struct dm_cache_policy_type default_policy_type = {
  927. .name = "default",
  928. .version = {1, 0, 0},
  929. .hint_size = 4,
  930. .owner = THIS_MODULE,
  931. .create = mq_create
  932. };
  933. static int __init mq_init(void)
  934. {
  935. int r;
  936. mq_entry_cache = kmem_cache_create("dm_mq_policy_cache_entry",
  937. sizeof(struct entry),
  938. __alignof__(struct entry),
  939. 0, NULL);
  940. if (!mq_entry_cache)
  941. goto bad;
  942. r = dm_cache_policy_register(&mq_policy_type);
  943. if (r) {
  944. DMERR("register failed %d", r);
  945. goto bad_register_mq;
  946. }
  947. r = dm_cache_policy_register(&default_policy_type);
  948. if (!r) {
  949. DMINFO("version %u.%u.%u loaded",
  950. mq_policy_type.version[0],
  951. mq_policy_type.version[1],
  952. mq_policy_type.version[2]);
  953. return 0;
  954. }
  955. DMERR("register failed (as default) %d", r);
  956. dm_cache_policy_unregister(&mq_policy_type);
  957. bad_register_mq:
  958. kmem_cache_destroy(mq_entry_cache);
  959. bad:
  960. return -ENOMEM;
  961. }
  962. static void __exit mq_exit(void)
  963. {
  964. dm_cache_policy_unregister(&mq_policy_type);
  965. dm_cache_policy_unregister(&default_policy_type);
  966. kmem_cache_destroy(mq_entry_cache);
  967. }
  968. module_init(mq_init);
  969. module_exit(mq_exit);
  970. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  971. MODULE_LICENSE("GPL");
  972. MODULE_DESCRIPTION("mq cache policy");
  973. MODULE_ALIAS("dm-cache-default");