dm-cache-policy-mq.c 30 KB

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