dm-cache-policy-mq.c 31 KB

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