dm-cache-policy-mq.c 29 KB

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