dm-cache-policy-mq.c 29 KB

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