blob: ca05d69191e89843ac27bec5e20e0637f23fbf6a [file] [log] [blame]
Joe Thornberf2836352013-03-01 22:45:51 +00001/*
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"
Joe Thornberf2836352013-03-01 22:45:51 +000017
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
Joe Thornberf2836352013-03-01 22:45:51 +000029/*
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{
Kent Overstreet4f024f32013-10-11 15:44:27 -070075 if (bio->bi_iter.bi_sector == from_oblock(t->last_end_oblock) + 1)
Joe Thornberf2836352013-03-01 22:45:51 +000076 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
Kent Overstreet4f024f32013-10-11 15:44:27 -070090 t->last_end_oblock = to_oblock(bio_end_sector(bio) - 1);
Joe Thornberf2836352013-03-01 22:45:51 +000091}
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 {
Joe Thornber75da39b2015-02-20 12:58:03 +0000129 unsigned nr_elts;
Joe Thornberf2836352013-03-01 22:45:51 +0000130 struct list_head qs[NR_QUEUE_LEVELS];
131};
132
133static void queue_init(struct queue *q)
134{
135 unsigned i;
136
Joe Thornber75da39b2015-02-20 12:58:03 +0000137 q->nr_elts = 0;
Joe Thornberf2836352013-03-01 22:45:51 +0000138 for (i = 0; i < NR_QUEUE_LEVELS; i++)
139 INIT_LIST_HEAD(q->qs + i);
140}
141
Joe Thornberc86c3072013-10-24 14:10:28 -0400142static bool queue_empty(struct queue *q)
143{
Joe Thornber75da39b2015-02-20 12:58:03 +0000144 return q->nr_elts == 0;
Joe Thornberc86c3072013-10-24 14:10:28 -0400145}
146
147/*
Joe Thornberf2836352013-03-01 22:45:51 +0000148 * Insert an entry to the back of the given level.
149 */
150static void queue_push(struct queue *q, unsigned level, struct list_head *elt)
151{
Joe Thornber75da39b2015-02-20 12:58:03 +0000152 q->nr_elts++;
Joe Thornberf2836352013-03-01 22:45:51 +0000153 list_add_tail(elt, q->qs + level);
154}
155
Joe Thornber75da39b2015-02-20 12:58:03 +0000156static void queue_remove(struct queue *q, struct list_head *elt)
Joe Thornberf2836352013-03-01 22:45:51 +0000157{
Joe Thornber75da39b2015-02-20 12:58:03 +0000158 q->nr_elts--;
Joe Thornberf2836352013-03-01 22:45:51 +0000159 list_del(elt);
160}
161
162/*
163 * Shifts all regions down one level. This has no effect on the order of
164 * the queue.
165 */
166static void queue_shift_down(struct queue *q)
167{
168 unsigned level;
169
170 for (level = 1; level < NR_QUEUE_LEVELS; level++)
171 list_splice_init(q->qs + level, q->qs + level - 1);
172}
173
174/*
175 * Gives us the oldest entry of the lowest popoulated level. If the first
176 * level is emptied then we shift down one level.
177 */
Joe Thornberb155aa02014-10-22 14:30:58 +0100178static struct list_head *queue_peek(struct queue *q)
Joe Thornberf2836352013-03-01 22:45:51 +0000179{
180 unsigned level;
Joe Thornberf2836352013-03-01 22:45:51 +0000181
182 for (level = 0; level < NR_QUEUE_LEVELS; level++)
Joe Thornberb155aa02014-10-22 14:30:58 +0100183 if (!list_empty(q->qs + level))
184 return q->qs[level].next;
Joe Thornberf2836352013-03-01 22:45:51 +0000185
186 return NULL;
187}
188
Joe Thornberb155aa02014-10-22 14:30:58 +0100189static struct list_head *queue_pop(struct queue *q)
190{
191 struct list_head *r = queue_peek(q);
192
193 if (r) {
Joe Thornber75da39b2015-02-20 12:58:03 +0000194 q->nr_elts--;
Joe Thornberb155aa02014-10-22 14:30:58 +0100195 list_del(r);
196
197 /* have we just emptied the bottom level? */
198 if (list_empty(q->qs))
199 queue_shift_down(q);
200 }
201
202 return r;
203}
204
Joe Thornberf2836352013-03-01 22:45:51 +0000205static struct list_head *list_pop(struct list_head *lh)
206{
207 struct list_head *r = lh->next;
208
209 BUG_ON(!r);
210 list_del_init(r);
211
212 return r;
213}
214
215/*----------------------------------------------------------------*/
216
217/*
218 * Describes a cache entry. Used in both the cache and the pre_cache.
219 */
220struct entry {
221 struct hlist_node hlist;
222 struct list_head list;
223 dm_oblock_t oblock;
Joe Thornberf2836352013-03-01 22:45:51 +0000224
225 /*
226 * FIXME: pack these better
227 */
Joe Thornber01911c12013-10-24 14:10:28 -0400228 bool dirty:1;
Joe Thornberf2836352013-03-01 22:45:51 +0000229 unsigned hit_count;
230 unsigned generation;
231 unsigned tick;
232};
233
Joe Thornber633618e2013-11-09 11:12:51 +0000234/*
235 * Rather than storing the cblock in an entry, we allocate all entries in
236 * an array, and infer the cblock from the entry position.
237 *
238 * Free entries are linked together into a list.
239 */
240struct entry_pool {
241 struct entry *entries, *entries_end;
242 struct list_head free;
243 unsigned nr_allocated;
244};
245
246static int epool_init(struct entry_pool *ep, unsigned nr_entries)
247{
248 unsigned i;
249
250 ep->entries = vzalloc(sizeof(struct entry) * nr_entries);
251 if (!ep->entries)
252 return -ENOMEM;
253
254 ep->entries_end = ep->entries + nr_entries;
255
256 INIT_LIST_HEAD(&ep->free);
257 for (i = 0; i < nr_entries; i++)
258 list_add(&ep->entries[i].list, &ep->free);
259
260 ep->nr_allocated = 0;
261
262 return 0;
263}
264
265static void epool_exit(struct entry_pool *ep)
266{
267 vfree(ep->entries);
268}
269
270static struct entry *alloc_entry(struct entry_pool *ep)
271{
272 struct entry *e;
273
274 if (list_empty(&ep->free))
275 return NULL;
276
277 e = list_entry(list_pop(&ep->free), struct entry, list);
278 INIT_LIST_HEAD(&e->list);
279 INIT_HLIST_NODE(&e->hlist);
280 ep->nr_allocated++;
281
282 return e;
283}
284
285/*
286 * This assumes the cblock hasn't already been allocated.
287 */
288static struct entry *alloc_particular_entry(struct entry_pool *ep, dm_cblock_t cblock)
289{
290 struct entry *e = ep->entries + from_cblock(cblock);
Joe Thornber633618e2013-11-09 11:12:51 +0000291
Wei Yongjunb8158052013-11-18 13:32:43 -0500292 list_del_init(&e->list);
Joe Thornber633618e2013-11-09 11:12:51 +0000293 INIT_HLIST_NODE(&e->hlist);
294 ep->nr_allocated++;
295
296 return e;
297}
298
299static void free_entry(struct entry_pool *ep, struct entry *e)
300{
301 BUG_ON(!ep->nr_allocated);
302 ep->nr_allocated--;
303 INIT_HLIST_NODE(&e->hlist);
304 list_add(&e->list, &ep->free);
305}
306
Joe Thornber532906a2013-11-08 16:36:17 +0000307/*
308 * Returns NULL if the entry is free.
309 */
310static struct entry *epool_find(struct entry_pool *ep, dm_cblock_t cblock)
311{
312 struct entry *e = ep->entries + from_cblock(cblock);
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -0500313 return !hlist_unhashed(&e->hlist) ? e : NULL;
Joe Thornber532906a2013-11-08 16:36:17 +0000314}
315
Joe Thornber633618e2013-11-09 11:12:51 +0000316static bool epool_empty(struct entry_pool *ep)
317{
318 return list_empty(&ep->free);
319}
320
321static bool in_pool(struct entry_pool *ep, struct entry *e)
322{
323 return e >= ep->entries && e < ep->entries_end;
324}
325
326static dm_cblock_t infer_cblock(struct entry_pool *ep, struct entry *e)
327{
328 return to_cblock(e - ep->entries);
329}
330
331/*----------------------------------------------------------------*/
332
Joe Thornberf2836352013-03-01 22:45:51 +0000333struct mq_policy {
334 struct dm_cache_policy policy;
335
336 /* protects everything */
337 struct mutex lock;
338 dm_cblock_t cache_size;
339 struct io_tracker tracker;
340
341 /*
Joe Thornber633618e2013-11-09 11:12:51 +0000342 * Entries come from two pools, one of pre-cache entries, and one
343 * for the cache proper.
344 */
345 struct entry_pool pre_cache_pool;
346 struct entry_pool cache_pool;
347
348 /*
Joe Thornber01911c12013-10-24 14:10:28 -0400349 * We maintain three queues of entries. The cache proper,
350 * consisting of a clean and dirty queue, contains the currently
351 * active mappings. Whereas the pre_cache tracks blocks that
352 * are being hit frequently and potential candidates for promotion
353 * to the cache.
Joe Thornberf2836352013-03-01 22:45:51 +0000354 */
355 struct queue pre_cache;
Joe Thornber01911c12013-10-24 14:10:28 -0400356 struct queue cache_clean;
357 struct queue cache_dirty;
Joe Thornberf2836352013-03-01 22:45:51 +0000358
359 /*
360 * Keeps track of time, incremented by the core. We use this to
361 * avoid attributing multiple hits within the same tick.
362 *
363 * Access to tick_protected should be done with the spin lock held.
364 * It's copied to tick at the start of the map function (within the
365 * mutex).
366 */
367 spinlock_t tick_lock;
368 unsigned tick_protected;
369 unsigned tick;
370
371 /*
372 * A count of the number of times the map function has been called
373 * and found an entry in the pre_cache or cache. Currently used to
374 * calculate the generation.
375 */
376 unsigned hit_count;
377
378 /*
379 * A generation is a longish period that is used to trigger some
380 * book keeping effects. eg, decrementing hit counts on entries.
381 * This is needed to allow the cache to evolve as io patterns
382 * change.
383 */
384 unsigned generation;
385 unsigned generation_period; /* in lookups (will probably change) */
386
Joe Thornber78e03d62013-12-09 12:53:05 +0000387 unsigned discard_promote_adjustment;
388 unsigned read_promote_adjustment;
389 unsigned write_promote_adjustment;
390
Joe Thornberf2836352013-03-01 22:45:51 +0000391 /*
Joe Thornberf2836352013-03-01 22:45:51 +0000392 * The hash table allows us to quickly find an entry by origin
393 * block. Both pre_cache and cache entries are in here.
394 */
395 unsigned nr_buckets;
396 dm_block_t hash_bits;
397 struct hlist_head *table;
398};
399
Joe Thornber78e03d62013-12-09 12:53:05 +0000400#define DEFAULT_DISCARD_PROMOTE_ADJUSTMENT 1
401#define DEFAULT_READ_PROMOTE_ADJUSTMENT 4
402#define DEFAULT_WRITE_PROMOTE_ADJUSTMENT 8
Joe Thornberb155aa02014-10-22 14:30:58 +0100403#define DISCOURAGE_DEMOTING_DIRTY_THRESHOLD 128
Joe Thornber78e03d62013-12-09 12:53:05 +0000404
Joe Thornberf2836352013-03-01 22:45:51 +0000405/*----------------------------------------------------------------*/
Joe Thornberf2836352013-03-01 22:45:51 +0000406
407/*
408 * Simple hash table implementation. Should replace with the standard hash
409 * table that's making its way upstream.
410 */
411static void hash_insert(struct mq_policy *mq, struct entry *e)
412{
413 unsigned h = hash_64(from_oblock(e->oblock), mq->hash_bits);
414
415 hlist_add_head(&e->hlist, mq->table + h);
416}
417
418static struct entry *hash_lookup(struct mq_policy *mq, dm_oblock_t oblock)
419{
420 unsigned h = hash_64(from_oblock(oblock), mq->hash_bits);
421 struct hlist_head *bucket = mq->table + h;
422 struct entry *e;
423
424 hlist_for_each_entry(e, bucket, hlist)
425 if (e->oblock == oblock) {
426 hlist_del(&e->hlist);
427 hlist_add_head(&e->hlist, bucket);
428 return e;
429 }
430
431 return NULL;
432}
433
434static void hash_remove(struct entry *e)
435{
436 hlist_del(&e->hlist);
437}
438
439/*----------------------------------------------------------------*/
440
Joe Thornberf2836352013-03-01 22:45:51 +0000441static bool any_free_cblocks(struct mq_policy *mq)
442{
Joe Thornber633618e2013-11-09 11:12:51 +0000443 return !epool_empty(&mq->cache_pool);
Joe Thornberf2836352013-03-01 22:45:51 +0000444}
445
Joe Thornberc86c3072013-10-24 14:10:28 -0400446static bool any_clean_cblocks(struct mq_policy *mq)
447{
448 return !queue_empty(&mq->cache_clean);
449}
450
Joe Thornberf2836352013-03-01 22:45:51 +0000451/*----------------------------------------------------------------*/
452
453/*
454 * Now we get to the meat of the policy. This section deals with deciding
455 * when to to add entries to the pre_cache and cache, and move between
456 * them.
457 */
458
459/*
460 * The queue level is based on the log2 of the hit count.
461 */
462static unsigned queue_level(struct entry *e)
463{
464 return min((unsigned) ilog2(e->hit_count), NR_QUEUE_LEVELS - 1u);
465}
466
Joe Thornber633618e2013-11-09 11:12:51 +0000467static bool in_cache(struct mq_policy *mq, struct entry *e)
468{
469 return in_pool(&mq->cache_pool, e);
470}
471
Joe Thornberf2836352013-03-01 22:45:51 +0000472/*
473 * Inserts the entry into the pre_cache or the cache. Ensures the cache
Joe Thornber633618e2013-11-09 11:12:51 +0000474 * block is marked as allocated if necc. Inserts into the hash table.
475 * Sets the tick which records when the entry was last moved about.
Joe Thornberf2836352013-03-01 22:45:51 +0000476 */
477static void push(struct mq_policy *mq, struct entry *e)
478{
479 e->tick = mq->tick;
480 hash_insert(mq, e);
481
Joe Thornber633618e2013-11-09 11:12:51 +0000482 if (in_cache(mq, e))
Joe Thornber01911c12013-10-24 14:10:28 -0400483 queue_push(e->dirty ? &mq->cache_dirty : &mq->cache_clean,
484 queue_level(e), &e->list);
Joe Thornber633618e2013-11-09 11:12:51 +0000485 else
Joe Thornberf2836352013-03-01 22:45:51 +0000486 queue_push(&mq->pre_cache, queue_level(e), &e->list);
487}
488
489/*
490 * Removes an entry from pre_cache or cache. Removes from the hash table.
Joe Thornberf2836352013-03-01 22:45:51 +0000491 */
492static void del(struct mq_policy *mq, struct entry *e)
493{
Joe Thornber75da39b2015-02-20 12:58:03 +0000494 if (in_cache(mq, e))
495 queue_remove(e->dirty ? &mq->cache_dirty : &mq->cache_clean, &e->list);
496 else
497 queue_remove(&mq->pre_cache, &e->list);
498
Joe Thornberf2836352013-03-01 22:45:51 +0000499 hash_remove(e);
Joe Thornberf2836352013-03-01 22:45:51 +0000500}
501
502/*
503 * Like del, except it removes the first entry in the queue (ie. the least
504 * recently used).
505 */
506static struct entry *pop(struct mq_policy *mq, struct queue *q)
507{
Joe Thornber0184b442013-10-24 14:10:28 -0400508 struct entry *e;
509 struct list_head *h = queue_pop(q);
Joe Thornberf2836352013-03-01 22:45:51 +0000510
Joe Thornber0184b442013-10-24 14:10:28 -0400511 if (!h)
512 return NULL;
Joe Thornberf2836352013-03-01 22:45:51 +0000513
Joe Thornber0184b442013-10-24 14:10:28 -0400514 e = container_of(h, struct entry, list);
515 hash_remove(e);
Joe Thornberf2836352013-03-01 22:45:51 +0000516
517 return e;
518}
519
Joe Thornberb155aa02014-10-22 14:30:58 +0100520static struct entry *peek(struct queue *q)
521{
522 struct list_head *h = queue_peek(q);
523 return h ? container_of(h, struct entry, list) : NULL;
524}
525
Joe Thornberf2836352013-03-01 22:45:51 +0000526/*
527 * Has this entry already been updated?
528 */
529static bool updated_this_tick(struct mq_policy *mq, struct entry *e)
530{
531 return mq->tick == e->tick;
532}
533
534/*
535 * The promotion threshold is adjusted every generation. As are the counts
536 * of the entries.
537 *
538 * At the moment the threshold is taken by averaging the hit counts of some
Joe Thornber01911c12013-10-24 14:10:28 -0400539 * of the entries in the cache (the first 20 entries across all levels in
540 * ascending order, giving preference to the clean entries at each level).
Joe Thornberf2836352013-03-01 22:45:51 +0000541 *
542 * We can be much cleverer than this though. For example, each promotion
543 * could bump up the threshold helping to prevent churn. Much more to do
544 * here.
545 */
546
547#define MAX_TO_AVERAGE 20
548
549static void check_generation(struct mq_policy *mq)
550{
551 unsigned total = 0, nr = 0, count = 0, level;
552 struct list_head *head;
553 struct entry *e;
554
Joe Thornber633618e2013-11-09 11:12:51 +0000555 if ((mq->hit_count >= mq->generation_period) && (epool_empty(&mq->cache_pool))) {
Joe Thornberf2836352013-03-01 22:45:51 +0000556 mq->hit_count = 0;
557 mq->generation++;
558
559 for (level = 0; level < NR_QUEUE_LEVELS && count < MAX_TO_AVERAGE; level++) {
Joe Thornber01911c12013-10-24 14:10:28 -0400560 head = mq->cache_clean.qs + level;
561 list_for_each_entry(e, head, list) {
562 nr++;
563 total += e->hit_count;
564
565 if (++count >= MAX_TO_AVERAGE)
566 break;
567 }
568
569 head = mq->cache_dirty.qs + level;
Joe Thornberf2836352013-03-01 22:45:51 +0000570 list_for_each_entry(e, head, list) {
571 nr++;
572 total += e->hit_count;
573
574 if (++count >= MAX_TO_AVERAGE)
575 break;
576 }
577 }
Joe Thornberf2836352013-03-01 22:45:51 +0000578 }
579}
580
581/*
582 * Whenever we use an entry we bump up it's hit counter, and push it to the
583 * back to it's current level.
584 */
585static void requeue_and_update_tick(struct mq_policy *mq, struct entry *e)
586{
587 if (updated_this_tick(mq, e))
588 return;
589
590 e->hit_count++;
591 mq->hit_count++;
592 check_generation(mq);
593
594 /* generation adjustment, to stop the counts increasing forever. */
595 /* FIXME: divide? */
596 /* e->hit_count -= min(e->hit_count - 1, mq->generation - e->generation); */
597 e->generation = mq->generation;
598
599 del(mq, e);
600 push(mq, e);
601}
602
603/*
604 * Demote the least recently used entry from the cache to the pre_cache.
605 * Returns the new cache entry to use, and the old origin block it was
606 * mapped to.
607 *
608 * We drop the hit count on the demoted entry back to 1 to stop it bouncing
609 * straight back into the cache if it's subsequently hit. There are
610 * various options here, and more experimentation would be good:
611 *
612 * - just forget about the demoted entry completely (ie. don't insert it
613 into the pre_cache).
614 * - divide the hit count rather that setting to some hard coded value.
615 * - set the hit count to a hard coded value other than 1, eg, is it better
616 * if it goes in at level 2?
617 */
Joe Thornber633618e2013-11-09 11:12:51 +0000618static int demote_cblock(struct mq_policy *mq, dm_oblock_t *oblock)
Joe Thornberf2836352013-03-01 22:45:51 +0000619{
Joe Thornber01911c12013-10-24 14:10:28 -0400620 struct entry *demoted = pop(mq, &mq->cache_clean);
Joe Thornberf2836352013-03-01 22:45:51 +0000621
Joe Thornber01911c12013-10-24 14:10:28 -0400622 if (!demoted)
623 /*
624 * We could get a block from mq->cache_dirty, but that
625 * would add extra latency to the triggering bio as it
626 * waits for the writeback. Better to not promote this
627 * time and hope there's a clean block next time this block
628 * is hit.
629 */
630 return -ENOSPC;
631
Joe Thornberf2836352013-03-01 22:45:51 +0000632 *oblock = demoted->oblock;
Joe Thornber633618e2013-11-09 11:12:51 +0000633 free_entry(&mq->cache_pool, demoted);
634
635 /*
636 * We used to put the demoted block into the pre-cache, but I think
637 * it's simpler to just let it work it's way up from zero again.
638 * Stops blocks flickering in and out of the cache.
639 */
Joe Thornberf2836352013-03-01 22:45:51 +0000640
Joe Thornber01911c12013-10-24 14:10:28 -0400641 return 0;
Joe Thornberf2836352013-03-01 22:45:51 +0000642}
643
644/*
Joe Thornberb155aa02014-10-22 14:30:58 +0100645 * Entries in the pre_cache whose hit count passes the promotion
646 * threshold move to the cache proper. Working out the correct
647 * value for the promotion_threshold is crucial to this policy.
648 */
649static unsigned promote_threshold(struct mq_policy *mq)
650{
651 struct entry *e;
652
653 if (any_free_cblocks(mq))
654 return 0;
655
656 e = peek(&mq->cache_clean);
657 if (e)
658 return e->hit_count;
659
660 e = peek(&mq->cache_dirty);
661 if (e)
662 return e->hit_count + DISCOURAGE_DEMOTING_DIRTY_THRESHOLD;
663
664 /* This should never happen */
665 return 0;
666}
667
668/*
Joe Thornberf2836352013-03-01 22:45:51 +0000669 * We modify the basic promotion_threshold depending on the specific io.
670 *
671 * If the origin block has been discarded then there's no cost to copy it
672 * to the cache.
673 *
674 * We bias towards reads, since they can be demoted at no cost if they
675 * haven't been dirtied.
676 */
Joe Thornberf2836352013-03-01 22:45:51 +0000677static unsigned adjusted_promote_threshold(struct mq_policy *mq,
678 bool discarded_oblock, int data_dir)
679{
Joe Thornberc86c3072013-10-24 14:10:28 -0400680 if (data_dir == READ)
Joe Thornberb155aa02014-10-22 14:30:58 +0100681 return promote_threshold(mq) + mq->read_promote_adjustment;
Joe Thornberc86c3072013-10-24 14:10:28 -0400682
683 if (discarded_oblock && (any_free_cblocks(mq) || any_clean_cblocks(mq))) {
Joe Thornberf2836352013-03-01 22:45:51 +0000684 /*
685 * We don't need to do any copying at all, so give this a
Joe Thornberc86c3072013-10-24 14:10:28 -0400686 * very low threshold.
Joe Thornberf2836352013-03-01 22:45:51 +0000687 */
Joe Thornber78e03d62013-12-09 12:53:05 +0000688 return mq->discard_promote_adjustment;
Joe Thornberc86c3072013-10-24 14:10:28 -0400689 }
Joe Thornberf2836352013-03-01 22:45:51 +0000690
Joe Thornberb155aa02014-10-22 14:30:58 +0100691 return promote_threshold(mq) + mq->write_promote_adjustment;
Joe Thornberf2836352013-03-01 22:45:51 +0000692}
693
694static bool should_promote(struct mq_policy *mq, struct entry *e,
695 bool discarded_oblock, int data_dir)
696{
697 return e->hit_count >=
698 adjusted_promote_threshold(mq, discarded_oblock, data_dir);
699}
700
701static int cache_entry_found(struct mq_policy *mq,
702 struct entry *e,
703 struct policy_result *result)
704{
705 requeue_and_update_tick(mq, e);
706
Joe Thornber633618e2013-11-09 11:12:51 +0000707 if (in_cache(mq, e)) {
Joe Thornberf2836352013-03-01 22:45:51 +0000708 result->op = POLICY_HIT;
Joe Thornber633618e2013-11-09 11:12:51 +0000709 result->cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000710 }
711
712 return 0;
713}
714
715/*
Joe Thornber0184b442013-10-24 14:10:28 -0400716 * Moves an entry from the pre_cache to the cache. The main work is
Joe Thornberf2836352013-03-01 22:45:51 +0000717 * finding which cache block to use.
718 */
719static int pre_cache_to_cache(struct mq_policy *mq, struct entry *e,
720 struct policy_result *result)
721{
Joe Thornber01911c12013-10-24 14:10:28 -0400722 int r;
Joe Thornber633618e2013-11-09 11:12:51 +0000723 struct entry *new_e;
Joe Thornberf2836352013-03-01 22:45:51 +0000724
Joe Thornber633618e2013-11-09 11:12:51 +0000725 /* Ensure there's a free cblock in the cache */
726 if (epool_empty(&mq->cache_pool)) {
Joe Thornberf2836352013-03-01 22:45:51 +0000727 result->op = POLICY_REPLACE;
Joe Thornber633618e2013-11-09 11:12:51 +0000728 r = demote_cblock(mq, &result->old_oblock);
Joe Thornber01911c12013-10-24 14:10:28 -0400729 if (r) {
730 result->op = POLICY_MISS;
731 return 0;
732 }
Joe Thornberf2836352013-03-01 22:45:51 +0000733 } else
734 result->op = POLICY_NEW;
735
Joe Thornber633618e2013-11-09 11:12:51 +0000736 new_e = alloc_entry(&mq->cache_pool);
737 BUG_ON(!new_e);
738
739 new_e->oblock = e->oblock;
740 new_e->dirty = false;
741 new_e->hit_count = e->hit_count;
742 new_e->generation = e->generation;
743 new_e->tick = e->tick;
Joe Thornberf2836352013-03-01 22:45:51 +0000744
745 del(mq, e);
Joe Thornber633618e2013-11-09 11:12:51 +0000746 free_entry(&mq->pre_cache_pool, e);
747 push(mq, new_e);
748
749 result->cblock = infer_cblock(&mq->cache_pool, new_e);
Joe Thornberf2836352013-03-01 22:45:51 +0000750
751 return 0;
752}
753
754static int pre_cache_entry_found(struct mq_policy *mq, struct entry *e,
755 bool can_migrate, bool discarded_oblock,
756 int data_dir, struct policy_result *result)
757{
758 int r = 0;
759 bool updated = updated_this_tick(mq, e);
760
Joe Thornberf2836352013-03-01 22:45:51 +0000761 if ((!discarded_oblock && updated) ||
Joe Thornberaf95e7a2013-11-15 10:51:20 +0000762 !should_promote(mq, e, discarded_oblock, data_dir)) {
763 requeue_and_update_tick(mq, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000764 result->op = POLICY_MISS;
Joe Thornberaf95e7a2013-11-15 10:51:20 +0000765
766 } else if (!can_migrate)
Joe Thornberf2836352013-03-01 22:45:51 +0000767 r = -EWOULDBLOCK;
Joe Thornberaf95e7a2013-11-15 10:51:20 +0000768
769 else {
770 requeue_and_update_tick(mq, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000771 r = pre_cache_to_cache(mq, e, result);
Joe Thornberaf95e7a2013-11-15 10:51:20 +0000772 }
Joe Thornberf2836352013-03-01 22:45:51 +0000773
774 return r;
775}
776
777static void insert_in_pre_cache(struct mq_policy *mq,
778 dm_oblock_t oblock)
779{
Joe Thornber633618e2013-11-09 11:12:51 +0000780 struct entry *e = alloc_entry(&mq->pre_cache_pool);
Joe Thornberf2836352013-03-01 22:45:51 +0000781
782 if (!e)
783 /*
784 * There's no spare entry structure, so we grab the least
785 * used one from the pre_cache.
786 */
787 e = pop(mq, &mq->pre_cache);
788
789 if (unlikely(!e)) {
790 DMWARN("couldn't pop from pre cache");
791 return;
792 }
793
Joe Thornber633618e2013-11-09 11:12:51 +0000794 e->dirty = false;
795 e->oblock = oblock;
796 e->hit_count = 1;
797 e->generation = mq->generation;
798 push(mq, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000799}
800
801static void insert_in_cache(struct mq_policy *mq, dm_oblock_t oblock,
802 struct policy_result *result)
803{
Joe Thornberc86c3072013-10-24 14:10:28 -0400804 int r;
Joe Thornberf2836352013-03-01 22:45:51 +0000805 struct entry *e;
Joe Thornberf2836352013-03-01 22:45:51 +0000806
Joe Thornber633618e2013-11-09 11:12:51 +0000807 if (epool_empty(&mq->cache_pool)) {
808 result->op = POLICY_REPLACE;
809 r = demote_cblock(mq, &result->old_oblock);
Joe Thornberc86c3072013-10-24 14:10:28 -0400810 if (unlikely(r)) {
811 result->op = POLICY_MISS;
812 insert_in_pre_cache(mq, oblock);
813 return;
814 }
Joe Thornberf2836352013-03-01 22:45:51 +0000815
Joe Thornberc86c3072013-10-24 14:10:28 -0400816 /*
817 * This will always succeed, since we've just demoted.
818 */
Joe Thornber633618e2013-11-09 11:12:51 +0000819 e = alloc_entry(&mq->cache_pool);
820 BUG_ON(!e);
Joe Thornberc86c3072013-10-24 14:10:28 -0400821
822 } else {
Joe Thornber633618e2013-11-09 11:12:51 +0000823 e = alloc_entry(&mq->cache_pool);
Joe Thornberc86c3072013-10-24 14:10:28 -0400824 result->op = POLICY_NEW;
Joe Thornberf2836352013-03-01 22:45:51 +0000825 }
826
827 e->oblock = oblock;
Joe Thornber01911c12013-10-24 14:10:28 -0400828 e->dirty = false;
Joe Thornberf2836352013-03-01 22:45:51 +0000829 e->hit_count = 1;
830 e->generation = mq->generation;
831 push(mq, e);
832
Joe Thornber633618e2013-11-09 11:12:51 +0000833 result->cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000834}
835
836static int no_entry_found(struct mq_policy *mq, dm_oblock_t oblock,
837 bool can_migrate, bool discarded_oblock,
838 int data_dir, struct policy_result *result)
839{
Joe Thornber78e03d62013-12-09 12:53:05 +0000840 if (adjusted_promote_threshold(mq, discarded_oblock, data_dir) <= 1) {
Joe Thornberf2836352013-03-01 22:45:51 +0000841 if (can_migrate)
842 insert_in_cache(mq, oblock, result);
843 else
844 return -EWOULDBLOCK;
845 } else {
846 insert_in_pre_cache(mq, oblock);
847 result->op = POLICY_MISS;
848 }
849
850 return 0;
851}
852
853/*
854 * Looks the oblock up in the hash table, then decides whether to put in
855 * pre_cache, or cache etc.
856 */
857static int map(struct mq_policy *mq, dm_oblock_t oblock,
858 bool can_migrate, bool discarded_oblock,
859 int data_dir, struct policy_result *result)
860{
861 int r = 0;
862 struct entry *e = hash_lookup(mq, oblock);
863
Joe Thornber633618e2013-11-09 11:12:51 +0000864 if (e && in_cache(mq, e))
Joe Thornberf2836352013-03-01 22:45:51 +0000865 r = cache_entry_found(mq, e, result);
Joe Thornber633618e2013-11-09 11:12:51 +0000866
Mike Snitzerf1afb362014-10-30 10:02:01 -0400867 else if (mq->tracker.thresholds[PATTERN_SEQUENTIAL] &&
868 iot_pattern(&mq->tracker) == PATTERN_SEQUENTIAL)
Joe Thornberf2836352013-03-01 22:45:51 +0000869 result->op = POLICY_MISS;
Joe Thornber633618e2013-11-09 11:12:51 +0000870
Joe Thornberf2836352013-03-01 22:45:51 +0000871 else if (e)
872 r = pre_cache_entry_found(mq, e, can_migrate, discarded_oblock,
873 data_dir, result);
Joe Thornber633618e2013-11-09 11:12:51 +0000874
Joe Thornberf2836352013-03-01 22:45:51 +0000875 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
Heinz Mauelshagen14f398c2014-02-28 12:02:56 -0500901 vfree(mq->table);
Joe Thornber633618e2013-11-09 11:12:51 +0000902 epool_exit(&mq->cache_pool);
903 epool_exit(&mq->pre_cache_pool);
Joe Thornberf2836352013-03-01 22:45:51 +0000904 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);
Joe Thornber633618e2013-11-09 11:12:51 +0000951 if (e && in_cache(mq, e)) {
952 *cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000953 r = 0;
954 } else
955 r = -ENOENT;
956
957 mutex_unlock(&mq->lock);
958
959 return r;
960}
961
Joe Thornber633618e2013-11-09 11:12:51 +0000962static void __mq_set_clear_dirty(struct mq_policy *mq, dm_oblock_t oblock, bool set)
Joe Thornber01911c12013-10-24 14:10:28 -0400963{
Joe Thornber01911c12013-10-24 14:10:28 -0400964 struct entry *e;
965
Joe Thornber01911c12013-10-24 14:10:28 -0400966 e = hash_lookup(mq, oblock);
Joe Thornber633618e2013-11-09 11:12:51 +0000967 BUG_ON(!e || !in_cache(mq, e));
Joe Thornber01911c12013-10-24 14:10:28 -0400968
Joe Thornber633618e2013-11-09 11:12:51 +0000969 del(mq, e);
970 e->dirty = set;
971 push(mq, e);
Joe Thornber01911c12013-10-24 14:10:28 -0400972}
973
974static void mq_set_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
975{
Joe Thornber633618e2013-11-09 11:12:51 +0000976 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);
Joe Thornber01911c12013-10-24 14:10:28 -0400981}
982
983static void mq_clear_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
984{
Joe Thornber633618e2013-11-09 11:12:51 +0000985 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);
Joe Thornber01911c12013-10-24 14:10:28 -0400990}
991
Joe Thornberf2836352013-03-01 22:45:51 +0000992static 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
Joe Thornber633618e2013-11-09 11:12:51 +0000999 e = alloc_particular_entry(&mq->cache_pool, cblock);
Joe Thornberf2836352013-03-01 22:45:51 +00001000 e->oblock = oblock;
Joe Thornber01911c12013-10-24 14:10:28 -04001001 e->dirty = false; /* this gets corrected in a minute */
Joe Thornberf2836352013-03-01 22:45:51 +00001002 e->hit_count = hint_valid ? hint : 1;
1003 e->generation = mq->generation;
1004 push(mq, e);
1005
1006 return 0;
1007}
1008
Joe Thornber633618e2013-11-09 11:12:51 +00001009static 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
Joe Thornberf2836352013-03-01 22:45:51 +00001027static 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;
Joe Thornberf2836352013-03-01 22:45:51 +00001032
1033 mutex_lock(&mq->lock);
1034
Joe Thornber633618e2013-11-09 11:12:51 +00001035 r = mq_save_hints(mq, &mq->cache_clean, fn, context);
1036 if (!r)
1037 r = mq_save_hints(mq, &mq->cache_dirty, fn, context);
Joe Thornber01911c12013-10-24 14:10:28 -04001038
Joe Thornberf2836352013-03-01 22:45:51 +00001039 mutex_unlock(&mq->lock);
1040
1041 return r;
1042}
1043
Joe Thornber633618e2013-11-09 11:12:51 +00001044static void __remove_mapping(struct mq_policy *mq, dm_oblock_t oblock)
1045{
1046 struct entry *e;
1047
1048 e = hash_lookup(mq, oblock);
1049 BUG_ON(!e || !in_cache(mq, e));
1050
1051 del(mq, e);
1052 free_entry(&mq->cache_pool, e);
1053}
1054
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +02001055static void mq_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
Joe Thornberf2836352013-03-01 22:45:51 +00001056{
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +02001057 struct mq_policy *mq = to_mq_policy(p);
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +02001058
1059 mutex_lock(&mq->lock);
Joe Thornber633618e2013-11-09 11:12:51 +00001060 __remove_mapping(mq, oblock);
Joe Thornberf2836352013-03-01 22:45:51 +00001061 mutex_unlock(&mq->lock);
1062}
1063
Joe Thornber532906a2013-11-08 16:36:17 +00001064static 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
Joe Thornber01911c12013-10-24 14:10:28 -04001089static 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;
Joe Thornber633618e2013-11-09 11:12:51 +00001098 *cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornber01911c12013-10-24 14:10:28 -04001099 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
Joe Thornber633618e2013-11-09 11:12:51 +00001118static void __force_mapping(struct mq_policy *mq,
1119 dm_oblock_t current_oblock, dm_oblock_t new_oblock)
Joe Thornberf2836352013-03-01 22:45:51 +00001120{
1121 struct entry *e = hash_lookup(mq, current_oblock);
1122
Joe Thornber633618e2013-11-09 11:12:51 +00001123 if (e && in_cache(mq, e)) {
1124 del(mq, e);
1125 e->oblock = new_oblock;
1126 e->dirty = true;
1127 push(mq, e);
1128 }
Joe Thornberf2836352013-03-01 22:45:51 +00001129}
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);
Joe Thornber633618e2013-11-09 11:12:51 +00001137 __force_mapping(mq, current_oblock, new_oblock);
Joe Thornberf2836352013-03-01 22:45:51 +00001138 mutex_unlock(&mq->lock);
1139}
1140
1141static dm_cblock_t mq_residency(struct dm_cache_policy *p)
1142{
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001143 dm_cblock_t r;
Joe Thornberf2836352013-03-01 22:45:51 +00001144 struct mq_policy *mq = to_mq_policy(p);
1145
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001146 mutex_lock(&mq->lock);
Joe Thornber633618e2013-11-09 11:12:51 +00001147 r = to_cblock(mq->cache_pool.nr_allocated);
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001148 mutex_unlock(&mq->lock);
1149
1150 return r;
Joe Thornberf2836352013-03-01 22:45:51 +00001151}
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);
Joe Thornberf2836352013-03-01 22:45:51 +00001167 unsigned long tmp;
1168
Joe Thornberf2836352013-03-01 22:45:51 +00001169 if (kstrtoul(value, 10, &tmp))
1170 return -EINVAL;
1171
Joe Thornber78e03d62013-12-09 12:53:05 +00001172 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;
Joe Thornberf2836352013-03-01 22:45:51 +00001189
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
Joe Thornber78e03d62013-12-09 12:53:05 +00001198 DMEMIT("10 random_threshold %u "
1199 "sequential_threshold %u "
1200 "discard_promote_adjustment %u "
1201 "read_promote_adjustment %u "
1202 "write_promote_adjustment %u",
Joe Thornberf2836352013-03-01 22:45:51 +00001203 mq->tracker.thresholds[PATTERN_RANDOM],
Joe Thornber78e03d62013-12-09 12:53:05 +00001204 mq->tracker.thresholds[PATTERN_SEQUENTIAL],
1205 mq->discard_promote_adjustment,
1206 mq->read_promote_adjustment,
1207 mq->write_promote_adjustment);
Joe Thornberf2836352013-03-01 22:45:51 +00001208
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;
Joe Thornber01911c12013-10-24 14:10:28 -04001218 mq->policy.set_dirty = mq_set_dirty;
1219 mq->policy.clear_dirty = mq_clear_dirty;
Joe Thornberf2836352013-03-01 22:45:51 +00001220 mq->policy.load_mapping = mq_load_mapping;
1221 mq->policy.walk_mappings = mq_walk_mappings;
1222 mq->policy.remove_mapping = mq_remove_mapping;
Joe Thornber532906a2013-11-08 16:36:17 +00001223 mq->policy.remove_cblock = mq_remove_cblock;
Joe Thornber01911c12013-10-24 14:10:28 -04001224 mq->policy.writeback_work = mq_writeback_work;
Joe Thornberf2836352013-03-01 22:45:51 +00001225 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{
Joe Thornberf2836352013-03-01 22:45:51 +00001236 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);
Joe Thornberf2836352013-03-01 22:45:51 +00001243 mq->cache_size = cache_size;
Joe Thornber633618e2013-11-09 11:12:51 +00001244
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
Joe Thornberf2836352013-03-01 22:45:51 +00001255 mq->tick_protected = 0;
1256 mq->tick = 0;
1257 mq->hit_count = 0;
1258 mq->generation = 0;
Joe Thornber78e03d62013-12-09 12:53:05 +00001259 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;
Joe Thornberf2836352013-03-01 22:45:51 +00001262 mutex_init(&mq->lock);
1263 spin_lock_init(&mq->tick_lock);
Joe Thornberf2836352013-03-01 22:45:51 +00001264
1265 queue_init(&mq->pre_cache);
Joe Thornber01911c12013-10-24 14:10:28 -04001266 queue_init(&mq->cache_clean);
1267 queue_init(&mq->cache_dirty);
1268
Joe Thornberf2836352013-03-01 22:45:51 +00001269 mq->generation_period = max((unsigned) from_cblock(cache_size), 1024U);
1270
Joe Thornberf2836352013-03-01 22:45:51 +00001271 mq->nr_buckets = next_power(from_cblock(cache_size) / 2, 16);
1272 mq->hash_bits = ffs(mq->nr_buckets) - 1;
Heinz Mauelshagen14f398c2014-02-28 12:02:56 -05001273 mq->table = vzalloc(sizeof(*mq->table) * mq->nr_buckets);
Joe Thornberf2836352013-03-01 22:45:51 +00001274 if (!mq->table)
1275 goto bad_alloc_table;
1276
Joe Thornberf2836352013-03-01 22:45:51 +00001277 return &mq->policy;
1278
Joe Thornberf2836352013-03-01 22:45:51 +00001279bad_alloc_table:
Joe Thornber633618e2013-11-09 11:12:51 +00001280 epool_exit(&mq->cache_pool);
1281bad_cache_init:
1282 epool_exit(&mq->pre_cache_pool);
1283bad_pre_cache_init:
Joe Thornberf2836352013-03-01 22:45:51 +00001284 kfree(mq);
1285
1286 return NULL;
1287}
1288
1289/*----------------------------------------------------------------*/
1290
1291static struct dm_cache_policy_type mq_policy_type = {
1292 .name = "mq",
Mike Snitzerf1afb362014-10-30 10:02:01 -04001293 .version = {1, 3, 0},
Joe Thornberf2836352013-03-01 22:45:51 +00001294 .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",
Mike Snitzerf1afb362014-10-30 10:02:01 -04001301 .version = {1, 3, 0},
Joe Thornberf2836352013-03-01 22:45:51 +00001302 .hint_size = 4,
1303 .owner = THIS_MODULE,
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05001304 .create = mq_create,
1305 .real = &mq_policy_type
Joe Thornberf2836352013-03-01 22:45:51 +00001306};
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) {
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001327 DMINFO("version %u.%u.%u loaded",
1328 mq_policy_type.version[0],
1329 mq_policy_type.version[1],
1330 mq_policy_type.version[2]);
Joe Thornberf2836352013-03-01 22:45:51 +00001331 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");