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