blob: 6bfb39411fa91798e0dfb9d0267df8aac71a9e4c [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
Joe Thornber3e45c912015-02-20 13:49:45 +0000127#define NR_SENTINELS NR_QUEUE_LEVELS * 3
Joe Thornberf2836352013-03-01 22:45:51 +0000128
129struct queue {
Joe Thornber75da39b2015-02-20 12:58:03 +0000130 unsigned nr_elts;
Joe Thornberf2836352013-03-01 22:45:51 +0000131 struct list_head qs[NR_QUEUE_LEVELS];
Joe Thornber3e45c912015-02-20 13:49:45 +0000132 struct list_head sentinels[NR_SENTINELS];
Joe Thornberf2836352013-03-01 22:45:51 +0000133};
134
135static void queue_init(struct queue *q)
136{
137 unsigned i;
138
Joe Thornber75da39b2015-02-20 12:58:03 +0000139 q->nr_elts = 0;
Joe Thornber3e45c912015-02-20 13:49:45 +0000140 for (i = 0; i < NR_QUEUE_LEVELS; i++) {
Joe Thornberf2836352013-03-01 22:45:51 +0000141 INIT_LIST_HEAD(q->qs + i);
Joe Thornber3e45c912015-02-20 13:49:45 +0000142 INIT_LIST_HEAD(q->sentinels + i);
143 }
Joe Thornberf2836352013-03-01 22:45:51 +0000144}
145
Joe Thornberc86c3072013-10-24 14:10:28 -0400146static bool queue_empty(struct queue *q)
147{
Joe Thornber75da39b2015-02-20 12:58:03 +0000148 return q->nr_elts == 0;
Joe Thornberc86c3072013-10-24 14:10:28 -0400149}
150
151/*
Joe Thornberf2836352013-03-01 22:45:51 +0000152 * Insert an entry to the back of the given level.
153 */
154static void queue_push(struct queue *q, unsigned level, struct list_head *elt)
155{
Joe Thornber75da39b2015-02-20 12:58:03 +0000156 q->nr_elts++;
Joe Thornberf2836352013-03-01 22:45:51 +0000157 list_add_tail(elt, q->qs + level);
158}
159
Joe Thornber75da39b2015-02-20 12:58:03 +0000160static void queue_remove(struct queue *q, struct list_head *elt)
Joe Thornberf2836352013-03-01 22:45:51 +0000161{
Joe Thornber75da39b2015-02-20 12:58:03 +0000162 q->nr_elts--;
Joe Thornberf2836352013-03-01 22:45:51 +0000163 list_del(elt);
164}
165
Joe Thornber3e45c912015-02-20 13:49:45 +0000166static bool is_sentinel(struct queue *q, struct list_head *h)
167{
168 return (h >= q->sentinels) && (h < (q->sentinels + NR_SENTINELS));
169}
170
Joe Thornberf2836352013-03-01 22:45:51 +0000171/*
Joe Thornberf2836352013-03-01 22:45:51 +0000172 * Gives us the oldest entry of the lowest popoulated level. If the first
173 * level is emptied then we shift down one level.
174 */
Joe Thornberb155aa02014-10-22 14:30:58 +0100175static struct list_head *queue_peek(struct queue *q)
Joe Thornberf2836352013-03-01 22:45:51 +0000176{
177 unsigned level;
Joe Thornber3e45c912015-02-20 13:49:45 +0000178 struct list_head *h;
Joe Thornberf2836352013-03-01 22:45:51 +0000179
180 for (level = 0; level < NR_QUEUE_LEVELS; level++)
Joe Thornber3e45c912015-02-20 13:49:45 +0000181 list_for_each(h, q->qs + level)
182 if (!is_sentinel(q, h))
183 return h;
Joe Thornberf2836352013-03-01 22:45:51 +0000184
185 return NULL;
186}
187
Joe Thornberb155aa02014-10-22 14:30:58 +0100188static struct list_head *queue_pop(struct queue *q)
189{
190 struct list_head *r = queue_peek(q);
191
192 if (r) {
Joe Thornber75da39b2015-02-20 12:58:03 +0000193 q->nr_elts--;
Joe Thornberb155aa02014-10-22 14:30:58 +0100194 list_del(r);
Joe Thornberb155aa02014-10-22 14:30:58 +0100195 }
196
197 return r;
198}
199
Joe Thornberf2836352013-03-01 22:45:51 +0000200static struct list_head *list_pop(struct list_head *lh)
201{
202 struct list_head *r = lh->next;
203
204 BUG_ON(!r);
205 list_del_init(r);
206
207 return r;
208}
209
Joe Thornber3e45c912015-02-20 13:49:45 +0000210/*
211 * Sometimes we want to iterate through entries that have been pushed since
212 * a certain event. We use sentinel entries on the queues to delimit these
213 * 'tick' events.
214 */
215static void queue_tick(struct queue *q)
216{
217 unsigned i;
218
219 for (i = 0; i < NR_QUEUE_LEVELS; i++) {
220 list_del(q->sentinels + i);
221 list_add_tail(q->sentinels + i, q->qs + i);
222 }
223}
224
225typedef void (*iter_fn)(struct list_head *, void *);
226static void queue_iterate_tick(struct queue *q, iter_fn fn, void *context)
227{
228 unsigned i;
229 struct list_head *h;
230
231 for (i = 0; i < NR_QUEUE_LEVELS; i++) {
232 list_for_each_prev(h, q->qs + i) {
233 if (is_sentinel(q, h))
234 break;
235
236 fn(h, context);
237 }
238 }
239}
240
Joe Thornberf2836352013-03-01 22:45:51 +0000241/*----------------------------------------------------------------*/
242
243/*
244 * Describes a cache entry. Used in both the cache and the pre_cache.
245 */
246struct entry {
247 struct hlist_node hlist;
248 struct list_head list;
249 dm_oblock_t oblock;
Joe Thornberf2836352013-03-01 22:45:51 +0000250
251 /*
252 * FIXME: pack these better
253 */
Joe Thornber01911c12013-10-24 14:10:28 -0400254 bool dirty:1;
Joe Thornberf2836352013-03-01 22:45:51 +0000255 unsigned hit_count;
Joe Thornberf2836352013-03-01 22:45:51 +0000256};
257
Joe Thornber633618e2013-11-09 11:12:51 +0000258/*
259 * Rather than storing the cblock in an entry, we allocate all entries in
260 * an array, and infer the cblock from the entry position.
261 *
262 * Free entries are linked together into a list.
263 */
264struct entry_pool {
265 struct entry *entries, *entries_end;
266 struct list_head free;
267 unsigned nr_allocated;
268};
269
270static int epool_init(struct entry_pool *ep, unsigned nr_entries)
271{
272 unsigned i;
273
274 ep->entries = vzalloc(sizeof(struct entry) * nr_entries);
275 if (!ep->entries)
276 return -ENOMEM;
277
278 ep->entries_end = ep->entries + nr_entries;
279
280 INIT_LIST_HEAD(&ep->free);
281 for (i = 0; i < nr_entries; i++)
282 list_add(&ep->entries[i].list, &ep->free);
283
284 ep->nr_allocated = 0;
285
286 return 0;
287}
288
289static void epool_exit(struct entry_pool *ep)
290{
291 vfree(ep->entries);
292}
293
294static struct entry *alloc_entry(struct entry_pool *ep)
295{
296 struct entry *e;
297
298 if (list_empty(&ep->free))
299 return NULL;
300
301 e = list_entry(list_pop(&ep->free), struct entry, list);
302 INIT_LIST_HEAD(&e->list);
303 INIT_HLIST_NODE(&e->hlist);
304 ep->nr_allocated++;
305
306 return e;
307}
308
309/*
310 * This assumes the cblock hasn't already been allocated.
311 */
312static struct entry *alloc_particular_entry(struct entry_pool *ep, dm_cblock_t cblock)
313{
314 struct entry *e = ep->entries + from_cblock(cblock);
Joe Thornber633618e2013-11-09 11:12:51 +0000315
Wei Yongjunb8158052013-11-18 13:32:43 -0500316 list_del_init(&e->list);
Joe Thornber633618e2013-11-09 11:12:51 +0000317 INIT_HLIST_NODE(&e->hlist);
318 ep->nr_allocated++;
319
320 return e;
321}
322
323static void free_entry(struct entry_pool *ep, struct entry *e)
324{
325 BUG_ON(!ep->nr_allocated);
326 ep->nr_allocated--;
327 INIT_HLIST_NODE(&e->hlist);
328 list_add(&e->list, &ep->free);
329}
330
Joe Thornber532906a2013-11-08 16:36:17 +0000331/*
332 * Returns NULL if the entry is free.
333 */
334static struct entry *epool_find(struct entry_pool *ep, dm_cblock_t cblock)
335{
336 struct entry *e = ep->entries + from_cblock(cblock);
Mike Snitzer7b6b2bc2013-11-12 12:17:43 -0500337 return !hlist_unhashed(&e->hlist) ? e : NULL;
Joe Thornber532906a2013-11-08 16:36:17 +0000338}
339
Joe Thornber633618e2013-11-09 11:12:51 +0000340static bool epool_empty(struct entry_pool *ep)
341{
342 return list_empty(&ep->free);
343}
344
345static bool in_pool(struct entry_pool *ep, struct entry *e)
346{
347 return e >= ep->entries && e < ep->entries_end;
348}
349
350static dm_cblock_t infer_cblock(struct entry_pool *ep, struct entry *e)
351{
352 return to_cblock(e - ep->entries);
353}
354
355/*----------------------------------------------------------------*/
356
Joe Thornberf2836352013-03-01 22:45:51 +0000357struct mq_policy {
358 struct dm_cache_policy policy;
359
360 /* protects everything */
361 struct mutex lock;
362 dm_cblock_t cache_size;
363 struct io_tracker tracker;
364
365 /*
Joe Thornber633618e2013-11-09 11:12:51 +0000366 * Entries come from two pools, one of pre-cache entries, and one
367 * for the cache proper.
368 */
369 struct entry_pool pre_cache_pool;
370 struct entry_pool cache_pool;
371
372 /*
Joe Thornber01911c12013-10-24 14:10:28 -0400373 * We maintain three queues of entries. The cache proper,
374 * consisting of a clean and dirty queue, contains the currently
375 * active mappings. Whereas the pre_cache tracks blocks that
376 * are being hit frequently and potential candidates for promotion
377 * to the cache.
Joe Thornberf2836352013-03-01 22:45:51 +0000378 */
379 struct queue pre_cache;
Joe Thornber01911c12013-10-24 14:10:28 -0400380 struct queue cache_clean;
381 struct queue cache_dirty;
Joe Thornberf2836352013-03-01 22:45:51 +0000382
383 /*
384 * Keeps track of time, incremented by the core. We use this to
385 * avoid attributing multiple hits within the same tick.
386 *
387 * Access to tick_protected should be done with the spin lock held.
388 * It's copied to tick at the start of the map function (within the
389 * mutex).
390 */
391 spinlock_t tick_lock;
392 unsigned tick_protected;
393 unsigned tick;
394
395 /*
396 * A count of the number of times the map function has been called
397 * and found an entry in the pre_cache or cache. Currently used to
398 * calculate the generation.
399 */
400 unsigned hit_count;
401
402 /*
403 * A generation is a longish period that is used to trigger some
404 * book keeping effects. eg, decrementing hit counts on entries.
405 * This is needed to allow the cache to evolve as io patterns
406 * change.
407 */
408 unsigned generation;
409 unsigned generation_period; /* in lookups (will probably change) */
410
Joe Thornber78e03d62013-12-09 12:53:05 +0000411 unsigned discard_promote_adjustment;
412 unsigned read_promote_adjustment;
413 unsigned write_promote_adjustment;
414
Joe Thornberf2836352013-03-01 22:45:51 +0000415 /*
Joe Thornberf2836352013-03-01 22:45:51 +0000416 * The hash table allows us to quickly find an entry by origin
417 * block. Both pre_cache and cache entries are in here.
418 */
419 unsigned nr_buckets;
420 dm_block_t hash_bits;
421 struct hlist_head *table;
422};
423
Joe Thornber78e03d62013-12-09 12:53:05 +0000424#define DEFAULT_DISCARD_PROMOTE_ADJUSTMENT 1
425#define DEFAULT_READ_PROMOTE_ADJUSTMENT 4
426#define DEFAULT_WRITE_PROMOTE_ADJUSTMENT 8
Joe Thornberb155aa02014-10-22 14:30:58 +0100427#define DISCOURAGE_DEMOTING_DIRTY_THRESHOLD 128
Joe Thornber78e03d62013-12-09 12:53:05 +0000428
Joe Thornberf2836352013-03-01 22:45:51 +0000429/*----------------------------------------------------------------*/
Joe Thornberf2836352013-03-01 22:45:51 +0000430
431/*
432 * Simple hash table implementation. Should replace with the standard hash
433 * table that's making its way upstream.
434 */
435static void hash_insert(struct mq_policy *mq, struct entry *e)
436{
437 unsigned h = hash_64(from_oblock(e->oblock), mq->hash_bits);
438
439 hlist_add_head(&e->hlist, mq->table + h);
440}
441
442static struct entry *hash_lookup(struct mq_policy *mq, dm_oblock_t oblock)
443{
444 unsigned h = hash_64(from_oblock(oblock), mq->hash_bits);
445 struct hlist_head *bucket = mq->table + h;
446 struct entry *e;
447
448 hlist_for_each_entry(e, bucket, hlist)
449 if (e->oblock == oblock) {
450 hlist_del(&e->hlist);
451 hlist_add_head(&e->hlist, bucket);
452 return e;
453 }
454
455 return NULL;
456}
457
458static void hash_remove(struct entry *e)
459{
460 hlist_del(&e->hlist);
461}
462
463/*----------------------------------------------------------------*/
464
Joe Thornberf2836352013-03-01 22:45:51 +0000465static bool any_free_cblocks(struct mq_policy *mq)
466{
Joe Thornber633618e2013-11-09 11:12:51 +0000467 return !epool_empty(&mq->cache_pool);
Joe Thornberf2836352013-03-01 22:45:51 +0000468}
469
Joe Thornberc86c3072013-10-24 14:10:28 -0400470static bool any_clean_cblocks(struct mq_policy *mq)
471{
472 return !queue_empty(&mq->cache_clean);
473}
474
Joe Thornberf2836352013-03-01 22:45:51 +0000475/*----------------------------------------------------------------*/
476
477/*
478 * Now we get to the meat of the policy. This section deals with deciding
479 * when to to add entries to the pre_cache and cache, and move between
480 * them.
481 */
482
483/*
484 * The queue level is based on the log2 of the hit count.
485 */
486static unsigned queue_level(struct entry *e)
487{
488 return min((unsigned) ilog2(e->hit_count), NR_QUEUE_LEVELS - 1u);
489}
490
Joe Thornber633618e2013-11-09 11:12:51 +0000491static bool in_cache(struct mq_policy *mq, struct entry *e)
492{
493 return in_pool(&mq->cache_pool, e);
494}
495
Joe Thornberf2836352013-03-01 22:45:51 +0000496/*
497 * Inserts the entry into the pre_cache or the cache. Ensures the cache
Joe Thornber633618e2013-11-09 11:12:51 +0000498 * block is marked as allocated if necc. Inserts into the hash table.
499 * Sets the tick which records when the entry was last moved about.
Joe Thornberf2836352013-03-01 22:45:51 +0000500 */
501static void push(struct mq_policy *mq, struct entry *e)
502{
Joe Thornberf2836352013-03-01 22:45:51 +0000503 hash_insert(mq, e);
504
Joe Thornber633618e2013-11-09 11:12:51 +0000505 if (in_cache(mq, e))
Joe Thornber01911c12013-10-24 14:10:28 -0400506 queue_push(e->dirty ? &mq->cache_dirty : &mq->cache_clean,
507 queue_level(e), &e->list);
Joe Thornber633618e2013-11-09 11:12:51 +0000508 else
Joe Thornberf2836352013-03-01 22:45:51 +0000509 queue_push(&mq->pre_cache, queue_level(e), &e->list);
510}
511
512/*
513 * Removes an entry from pre_cache or cache. Removes from the hash table.
Joe Thornberf2836352013-03-01 22:45:51 +0000514 */
515static void del(struct mq_policy *mq, struct entry *e)
516{
Joe Thornber75da39b2015-02-20 12:58:03 +0000517 if (in_cache(mq, e))
518 queue_remove(e->dirty ? &mq->cache_dirty : &mq->cache_clean, &e->list);
519 else
520 queue_remove(&mq->pre_cache, &e->list);
521
Joe Thornberf2836352013-03-01 22:45:51 +0000522 hash_remove(e);
Joe Thornberf2836352013-03-01 22:45:51 +0000523}
524
525/*
526 * Like del, except it removes the first entry in the queue (ie. the least
527 * recently used).
528 */
529static struct entry *pop(struct mq_policy *mq, struct queue *q)
530{
Joe Thornber0184b442013-10-24 14:10:28 -0400531 struct entry *e;
532 struct list_head *h = queue_pop(q);
Joe Thornberf2836352013-03-01 22:45:51 +0000533
Joe Thornber0184b442013-10-24 14:10:28 -0400534 if (!h)
535 return NULL;
Joe Thornberf2836352013-03-01 22:45:51 +0000536
Joe Thornber0184b442013-10-24 14:10:28 -0400537 e = container_of(h, struct entry, list);
538 hash_remove(e);
Joe Thornberf2836352013-03-01 22:45:51 +0000539
540 return e;
541}
542
Joe Thornberb155aa02014-10-22 14:30:58 +0100543static struct entry *peek(struct queue *q)
544{
545 struct list_head *h = queue_peek(q);
546 return h ? container_of(h, struct entry, list) : NULL;
547}
548
Joe Thornberf2836352013-03-01 22:45:51 +0000549/*
Joe Thornberf2836352013-03-01 22:45:51 +0000550 * The promotion threshold is adjusted every generation. As are the counts
551 * of the entries.
552 *
553 * At the moment the threshold is taken by averaging the hit counts of some
Joe Thornber01911c12013-10-24 14:10:28 -0400554 * of the entries in the cache (the first 20 entries across all levels in
555 * ascending order, giving preference to the clean entries at each level).
Joe Thornberf2836352013-03-01 22:45:51 +0000556 *
557 * We can be much cleverer than this though. For example, each promotion
558 * could bump up the threshold helping to prevent churn. Much more to do
559 * here.
560 */
561
562#define MAX_TO_AVERAGE 20
563
564static void check_generation(struct mq_policy *mq)
565{
566 unsigned total = 0, nr = 0, count = 0, level;
567 struct list_head *head;
568 struct entry *e;
569
Joe Thornber633618e2013-11-09 11:12:51 +0000570 if ((mq->hit_count >= mq->generation_period) && (epool_empty(&mq->cache_pool))) {
Joe Thornberf2836352013-03-01 22:45:51 +0000571 mq->hit_count = 0;
572 mq->generation++;
573
574 for (level = 0; level < NR_QUEUE_LEVELS && count < MAX_TO_AVERAGE; level++) {
Joe Thornber01911c12013-10-24 14:10:28 -0400575 head = mq->cache_clean.qs + level;
576 list_for_each_entry(e, head, list) {
577 nr++;
578 total += e->hit_count;
579
580 if (++count >= MAX_TO_AVERAGE)
581 break;
582 }
583
584 head = mq->cache_dirty.qs + level;
Joe Thornberf2836352013-03-01 22:45:51 +0000585 list_for_each_entry(e, head, list) {
586 nr++;
587 total += e->hit_count;
588
589 if (++count >= MAX_TO_AVERAGE)
590 break;
591 }
592 }
Joe Thornberf2836352013-03-01 22:45:51 +0000593 }
594}
595
596/*
597 * Whenever we use an entry we bump up it's hit counter, and push it to the
598 * back to it's current level.
599 */
Joe Thornber3e45c912015-02-20 13:49:45 +0000600static void requeue(struct mq_policy *mq, struct entry *e)
Joe Thornberf2836352013-03-01 22:45:51 +0000601{
Joe Thornberf2836352013-03-01 22:45:51 +0000602 check_generation(mq);
Joe Thornberf2836352013-03-01 22:45:51 +0000603 del(mq, e);
604 push(mq, e);
605}
606
607/*
608 * Demote the least recently used entry from the cache to the pre_cache.
609 * Returns the new cache entry to use, and the old origin block it was
610 * mapped to.
611 *
612 * We drop the hit count on the demoted entry back to 1 to stop it bouncing
613 * straight back into the cache if it's subsequently hit. There are
614 * various options here, and more experimentation would be good:
615 *
616 * - just forget about the demoted entry completely (ie. don't insert it
617 into the pre_cache).
618 * - divide the hit count rather that setting to some hard coded value.
619 * - set the hit count to a hard coded value other than 1, eg, is it better
620 * if it goes in at level 2?
621 */
Joe Thornber633618e2013-11-09 11:12:51 +0000622static int demote_cblock(struct mq_policy *mq, dm_oblock_t *oblock)
Joe Thornberf2836352013-03-01 22:45:51 +0000623{
Joe Thornber01911c12013-10-24 14:10:28 -0400624 struct entry *demoted = pop(mq, &mq->cache_clean);
Joe Thornberf2836352013-03-01 22:45:51 +0000625
Joe Thornber01911c12013-10-24 14:10:28 -0400626 if (!demoted)
627 /*
628 * We could get a block from mq->cache_dirty, but that
629 * would add extra latency to the triggering bio as it
630 * waits for the writeback. Better to not promote this
631 * time and hope there's a clean block next time this block
632 * is hit.
633 */
634 return -ENOSPC;
635
Joe Thornberf2836352013-03-01 22:45:51 +0000636 *oblock = demoted->oblock;
Joe Thornber633618e2013-11-09 11:12:51 +0000637 free_entry(&mq->cache_pool, demoted);
638
639 /*
640 * We used to put the demoted block into the pre-cache, but I think
641 * it's simpler to just let it work it's way up from zero again.
642 * Stops blocks flickering in and out of the cache.
643 */
Joe Thornberf2836352013-03-01 22:45:51 +0000644
Joe Thornber01911c12013-10-24 14:10:28 -0400645 return 0;
Joe Thornberf2836352013-03-01 22:45:51 +0000646}
647
648/*
Joe Thornberb155aa02014-10-22 14:30:58 +0100649 * Entries in the pre_cache whose hit count passes the promotion
650 * threshold move to the cache proper. Working out the correct
651 * value for the promotion_threshold is crucial to this policy.
652 */
653static unsigned promote_threshold(struct mq_policy *mq)
654{
655 struct entry *e;
656
657 if (any_free_cblocks(mq))
658 return 0;
659
660 e = peek(&mq->cache_clean);
661 if (e)
662 return e->hit_count;
663
664 e = peek(&mq->cache_dirty);
665 if (e)
666 return e->hit_count + DISCOURAGE_DEMOTING_DIRTY_THRESHOLD;
667
668 /* This should never happen */
669 return 0;
670}
671
672/*
Joe Thornberf2836352013-03-01 22:45:51 +0000673 * We modify the basic promotion_threshold depending on the specific io.
674 *
675 * If the origin block has been discarded then there's no cost to copy it
676 * to the cache.
677 *
678 * We bias towards reads, since they can be demoted at no cost if they
679 * haven't been dirtied.
680 */
Joe Thornberf2836352013-03-01 22:45:51 +0000681static unsigned adjusted_promote_threshold(struct mq_policy *mq,
682 bool discarded_oblock, int data_dir)
683{
Joe Thornberc86c3072013-10-24 14:10:28 -0400684 if (data_dir == READ)
Joe Thornberb155aa02014-10-22 14:30:58 +0100685 return promote_threshold(mq) + mq->read_promote_adjustment;
Joe Thornberc86c3072013-10-24 14:10:28 -0400686
687 if (discarded_oblock && (any_free_cblocks(mq) || any_clean_cblocks(mq))) {
Joe Thornberf2836352013-03-01 22:45:51 +0000688 /*
689 * We don't need to do any copying at all, so give this a
Joe Thornberc86c3072013-10-24 14:10:28 -0400690 * very low threshold.
Joe Thornberf2836352013-03-01 22:45:51 +0000691 */
Joe Thornber78e03d62013-12-09 12:53:05 +0000692 return mq->discard_promote_adjustment;
Joe Thornberc86c3072013-10-24 14:10:28 -0400693 }
Joe Thornberf2836352013-03-01 22:45:51 +0000694
Joe Thornberb155aa02014-10-22 14:30:58 +0100695 return promote_threshold(mq) + mq->write_promote_adjustment;
Joe Thornberf2836352013-03-01 22:45:51 +0000696}
697
698static bool should_promote(struct mq_policy *mq, struct entry *e,
699 bool discarded_oblock, int data_dir)
700{
701 return e->hit_count >=
702 adjusted_promote_threshold(mq, discarded_oblock, data_dir);
703}
704
705static int cache_entry_found(struct mq_policy *mq,
706 struct entry *e,
707 struct policy_result *result)
708{
Joe Thornber3e45c912015-02-20 13:49:45 +0000709 requeue(mq, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000710
Joe Thornber633618e2013-11-09 11:12:51 +0000711 if (in_cache(mq, e)) {
Joe Thornberf2836352013-03-01 22:45:51 +0000712 result->op = POLICY_HIT;
Joe Thornber633618e2013-11-09 11:12:51 +0000713 result->cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000714 }
715
716 return 0;
717}
718
719/*
Joe Thornber0184b442013-10-24 14:10:28 -0400720 * Moves an entry from the pre_cache to the cache. The main work is
Joe Thornberf2836352013-03-01 22:45:51 +0000721 * finding which cache block to use.
722 */
723static int pre_cache_to_cache(struct mq_policy *mq, struct entry *e,
724 struct policy_result *result)
725{
Joe Thornber01911c12013-10-24 14:10:28 -0400726 int r;
Joe Thornber633618e2013-11-09 11:12:51 +0000727 struct entry *new_e;
Joe Thornberf2836352013-03-01 22:45:51 +0000728
Joe Thornber633618e2013-11-09 11:12:51 +0000729 /* Ensure there's a free cblock in the cache */
730 if (epool_empty(&mq->cache_pool)) {
Joe Thornberf2836352013-03-01 22:45:51 +0000731 result->op = POLICY_REPLACE;
Joe Thornber633618e2013-11-09 11:12:51 +0000732 r = demote_cblock(mq, &result->old_oblock);
Joe Thornber01911c12013-10-24 14:10:28 -0400733 if (r) {
734 result->op = POLICY_MISS;
735 return 0;
736 }
Joe Thornberf2836352013-03-01 22:45:51 +0000737 } else
738 result->op = POLICY_NEW;
739
Joe Thornber633618e2013-11-09 11:12:51 +0000740 new_e = alloc_entry(&mq->cache_pool);
741 BUG_ON(!new_e);
742
743 new_e->oblock = e->oblock;
744 new_e->dirty = false;
745 new_e->hit_count = e->hit_count;
Joe Thornberf2836352013-03-01 22:45:51 +0000746
747 del(mq, e);
Joe Thornber633618e2013-11-09 11:12:51 +0000748 free_entry(&mq->pre_cache_pool, e);
749 push(mq, new_e);
750
751 result->cblock = infer_cblock(&mq->cache_pool, new_e);
Joe Thornberf2836352013-03-01 22:45:51 +0000752
753 return 0;
754}
755
756static int pre_cache_entry_found(struct mq_policy *mq, struct entry *e,
757 bool can_migrate, bool discarded_oblock,
758 int data_dir, struct policy_result *result)
759{
760 int r = 0;
Joe Thornberf2836352013-03-01 22:45:51 +0000761
Joe Thornber3e45c912015-02-20 13:49:45 +0000762 if (!should_promote(mq, e, discarded_oblock, data_dir)) {
763 requeue(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 {
Joe Thornber3e45c912015-02-20 13:49:45 +0000770 requeue(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;
Joe Thornber633618e2013-11-09 11:12:51 +0000797 push(mq, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000798}
799
800static void insert_in_cache(struct mq_policy *mq, dm_oblock_t oblock,
801 struct policy_result *result)
802{
Joe Thornberc86c3072013-10-24 14:10:28 -0400803 int r;
Joe Thornberf2836352013-03-01 22:45:51 +0000804 struct entry *e;
Joe Thornberf2836352013-03-01 22:45:51 +0000805
Joe Thornber633618e2013-11-09 11:12:51 +0000806 if (epool_empty(&mq->cache_pool)) {
807 result->op = POLICY_REPLACE;
808 r = demote_cblock(mq, &result->old_oblock);
Joe Thornberc86c3072013-10-24 14:10:28 -0400809 if (unlikely(r)) {
810 result->op = POLICY_MISS;
811 insert_in_pre_cache(mq, oblock);
812 return;
813 }
Joe Thornberf2836352013-03-01 22:45:51 +0000814
Joe Thornberc86c3072013-10-24 14:10:28 -0400815 /*
816 * This will always succeed, since we've just demoted.
817 */
Joe Thornber633618e2013-11-09 11:12:51 +0000818 e = alloc_entry(&mq->cache_pool);
819 BUG_ON(!e);
Joe Thornberc86c3072013-10-24 14:10:28 -0400820
821 } else {
Joe Thornber633618e2013-11-09 11:12:51 +0000822 e = alloc_entry(&mq->cache_pool);
Joe Thornberc86c3072013-10-24 14:10:28 -0400823 result->op = POLICY_NEW;
Joe Thornberf2836352013-03-01 22:45:51 +0000824 }
825
826 e->oblock = oblock;
Joe Thornber01911c12013-10-24 14:10:28 -0400827 e->dirty = false;
Joe Thornberf2836352013-03-01 22:45:51 +0000828 e->hit_count = 1;
Joe Thornberf2836352013-03-01 22:45:51 +0000829 push(mq, e);
830
Joe Thornber633618e2013-11-09 11:12:51 +0000831 result->cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000832}
833
834static int no_entry_found(struct mq_policy *mq, dm_oblock_t oblock,
835 bool can_migrate, bool discarded_oblock,
836 int data_dir, struct policy_result *result)
837{
Joe Thornber78e03d62013-12-09 12:53:05 +0000838 if (adjusted_promote_threshold(mq, discarded_oblock, data_dir) <= 1) {
Joe Thornberf2836352013-03-01 22:45:51 +0000839 if (can_migrate)
840 insert_in_cache(mq, oblock, result);
841 else
842 return -EWOULDBLOCK;
843 } else {
844 insert_in_pre_cache(mq, oblock);
845 result->op = POLICY_MISS;
846 }
847
848 return 0;
849}
850
851/*
852 * Looks the oblock up in the hash table, then decides whether to put in
853 * pre_cache, or cache etc.
854 */
855static int map(struct mq_policy *mq, dm_oblock_t oblock,
856 bool can_migrate, bool discarded_oblock,
857 int data_dir, struct policy_result *result)
858{
859 int r = 0;
860 struct entry *e = hash_lookup(mq, oblock);
861
Joe Thornber633618e2013-11-09 11:12:51 +0000862 if (e && in_cache(mq, e))
Joe Thornberf2836352013-03-01 22:45:51 +0000863 r = cache_entry_found(mq, e, result);
Joe Thornber633618e2013-11-09 11:12:51 +0000864
Mike Snitzerf1afb362014-10-30 10:02:01 -0400865 else if (mq->tracker.thresholds[PATTERN_SEQUENTIAL] &&
866 iot_pattern(&mq->tracker) == PATTERN_SEQUENTIAL)
Joe Thornberf2836352013-03-01 22:45:51 +0000867 result->op = POLICY_MISS;
Joe Thornber633618e2013-11-09 11:12:51 +0000868
Joe Thornberf2836352013-03-01 22:45:51 +0000869 else if (e)
870 r = pre_cache_entry_found(mq, e, can_migrate, discarded_oblock,
871 data_dir, result);
Joe Thornber633618e2013-11-09 11:12:51 +0000872
Joe Thornberf2836352013-03-01 22:45:51 +0000873 else
874 r = no_entry_found(mq, oblock, can_migrate, discarded_oblock,
875 data_dir, result);
876
877 if (r == -EWOULDBLOCK)
878 result->op = POLICY_MISS;
879
880 return r;
881}
882
883/*----------------------------------------------------------------*/
884
885/*
886 * Public interface, via the policy struct. See dm-cache-policy.h for a
887 * description of these.
888 */
889
890static struct mq_policy *to_mq_policy(struct dm_cache_policy *p)
891{
892 return container_of(p, struct mq_policy, policy);
893}
894
895static void mq_destroy(struct dm_cache_policy *p)
896{
897 struct mq_policy *mq = to_mq_policy(p);
898
Heinz Mauelshagen14f398c2014-02-28 12:02:56 -0500899 vfree(mq->table);
Joe Thornber633618e2013-11-09 11:12:51 +0000900 epool_exit(&mq->cache_pool);
901 epool_exit(&mq->pre_cache_pool);
Joe Thornberf2836352013-03-01 22:45:51 +0000902 kfree(mq);
903}
904
Joe Thornber3e45c912015-02-20 13:49:45 +0000905static void update_pre_cache_hits(struct list_head *h, void *context)
906{
907 struct entry *e = container_of(h, struct entry, list);
908 e->hit_count++;
909}
910
911static void update_cache_hits(struct list_head *h, void *context)
912{
913 struct mq_policy *mq = context;
914 struct entry *e = container_of(h, struct entry, list);
915 e->hit_count++;
916 mq->hit_count++;
917}
918
Joe Thornberf2836352013-03-01 22:45:51 +0000919static void copy_tick(struct mq_policy *mq)
920{
Joe Thornber3e45c912015-02-20 13:49:45 +0000921 unsigned long flags, tick;
Joe Thornberf2836352013-03-01 22:45:51 +0000922
923 spin_lock_irqsave(&mq->tick_lock, flags);
Joe Thornber3e45c912015-02-20 13:49:45 +0000924 tick = mq->tick_protected;
925 if (tick != mq->tick) {
926 queue_iterate_tick(&mq->pre_cache, update_pre_cache_hits, mq);
927 queue_iterate_tick(&mq->cache_dirty, update_cache_hits, mq);
928 queue_iterate_tick(&mq->cache_clean, update_cache_hits, mq);
929 mq->tick = tick;
930 }
931
932 queue_tick(&mq->pre_cache);
933 queue_tick(&mq->cache_dirty);
934 queue_tick(&mq->cache_clean);
Joe Thornberf2836352013-03-01 22:45:51 +0000935 spin_unlock_irqrestore(&mq->tick_lock, flags);
936}
937
938static int mq_map(struct dm_cache_policy *p, dm_oblock_t oblock,
939 bool can_block, bool can_migrate, bool discarded_oblock,
940 struct bio *bio, struct policy_result *result)
941{
942 int r;
943 struct mq_policy *mq = to_mq_policy(p);
944
945 result->op = POLICY_MISS;
946
947 if (can_block)
948 mutex_lock(&mq->lock);
949 else if (!mutex_trylock(&mq->lock))
950 return -EWOULDBLOCK;
951
952 copy_tick(mq);
953
954 iot_examine_bio(&mq->tracker, bio);
955 r = map(mq, oblock, can_migrate, discarded_oblock,
956 bio_data_dir(bio), result);
957
958 mutex_unlock(&mq->lock);
959
960 return r;
961}
962
963static int mq_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock)
964{
965 int r;
966 struct mq_policy *mq = to_mq_policy(p);
967 struct entry *e;
968
969 if (!mutex_trylock(&mq->lock))
970 return -EWOULDBLOCK;
971
972 e = hash_lookup(mq, oblock);
Joe Thornber633618e2013-11-09 11:12:51 +0000973 if (e && in_cache(mq, e)) {
974 *cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000975 r = 0;
976 } else
977 r = -ENOENT;
978
979 mutex_unlock(&mq->lock);
980
981 return r;
982}
983
Joe Thornber633618e2013-11-09 11:12:51 +0000984static void __mq_set_clear_dirty(struct mq_policy *mq, dm_oblock_t oblock, bool set)
Joe Thornber01911c12013-10-24 14:10:28 -0400985{
Joe Thornber01911c12013-10-24 14:10:28 -0400986 struct entry *e;
987
Joe Thornber01911c12013-10-24 14:10:28 -0400988 e = hash_lookup(mq, oblock);
Joe Thornber633618e2013-11-09 11:12:51 +0000989 BUG_ON(!e || !in_cache(mq, e));
Joe Thornber01911c12013-10-24 14:10:28 -0400990
Joe Thornber633618e2013-11-09 11:12:51 +0000991 del(mq, e);
992 e->dirty = set;
993 push(mq, e);
Joe Thornber01911c12013-10-24 14:10:28 -0400994}
995
996static void mq_set_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
997{
Joe Thornber633618e2013-11-09 11:12:51 +0000998 struct mq_policy *mq = to_mq_policy(p);
999
1000 mutex_lock(&mq->lock);
1001 __mq_set_clear_dirty(mq, oblock, true);
1002 mutex_unlock(&mq->lock);
Joe Thornber01911c12013-10-24 14:10:28 -04001003}
1004
1005static void mq_clear_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
1006{
Joe Thornber633618e2013-11-09 11:12:51 +00001007 struct mq_policy *mq = to_mq_policy(p);
1008
1009 mutex_lock(&mq->lock);
1010 __mq_set_clear_dirty(mq, oblock, false);
1011 mutex_unlock(&mq->lock);
Joe Thornber01911c12013-10-24 14:10:28 -04001012}
1013
Joe Thornberf2836352013-03-01 22:45:51 +00001014static int mq_load_mapping(struct dm_cache_policy *p,
1015 dm_oblock_t oblock, dm_cblock_t cblock,
1016 uint32_t hint, bool hint_valid)
1017{
1018 struct mq_policy *mq = to_mq_policy(p);
1019 struct entry *e;
1020
Joe Thornber633618e2013-11-09 11:12:51 +00001021 e = alloc_particular_entry(&mq->cache_pool, cblock);
Joe Thornberf2836352013-03-01 22:45:51 +00001022 e->oblock = oblock;
Joe Thornber01911c12013-10-24 14:10:28 -04001023 e->dirty = false; /* this gets corrected in a minute */
Joe Thornberf2836352013-03-01 22:45:51 +00001024 e->hit_count = hint_valid ? hint : 1;
Joe Thornberf2836352013-03-01 22:45:51 +00001025 push(mq, e);
1026
1027 return 0;
1028}
1029
Joe Thornber633618e2013-11-09 11:12:51 +00001030static int mq_save_hints(struct mq_policy *mq, struct queue *q,
1031 policy_walk_fn fn, void *context)
1032{
1033 int r;
1034 unsigned level;
Joe Thornber3e45c912015-02-20 13:49:45 +00001035 struct list_head *h;
Joe Thornber633618e2013-11-09 11:12:51 +00001036 struct entry *e;
1037
1038 for (level = 0; level < NR_QUEUE_LEVELS; level++)
Joe Thornber3e45c912015-02-20 13:49:45 +00001039 list_for_each(h, q->qs + level) {
1040 if (is_sentinel(q, h))
1041 continue;
1042
1043 e = container_of(h, struct entry, list);
Joe Thornber633618e2013-11-09 11:12:51 +00001044 r = fn(context, infer_cblock(&mq->cache_pool, e),
1045 e->oblock, e->hit_count);
1046 if (r)
1047 return r;
1048 }
1049
1050 return 0;
1051}
1052
Joe Thornberf2836352013-03-01 22:45:51 +00001053static int mq_walk_mappings(struct dm_cache_policy *p, policy_walk_fn fn,
1054 void *context)
1055{
1056 struct mq_policy *mq = to_mq_policy(p);
1057 int r = 0;
Joe Thornberf2836352013-03-01 22:45:51 +00001058
1059 mutex_lock(&mq->lock);
1060
Joe Thornber633618e2013-11-09 11:12:51 +00001061 r = mq_save_hints(mq, &mq->cache_clean, fn, context);
1062 if (!r)
1063 r = mq_save_hints(mq, &mq->cache_dirty, fn, context);
Joe Thornber01911c12013-10-24 14:10:28 -04001064
Joe Thornberf2836352013-03-01 22:45:51 +00001065 mutex_unlock(&mq->lock);
1066
1067 return r;
1068}
1069
Joe Thornber633618e2013-11-09 11:12:51 +00001070static void __remove_mapping(struct mq_policy *mq, dm_oblock_t oblock)
1071{
1072 struct entry *e;
1073
1074 e = hash_lookup(mq, oblock);
1075 BUG_ON(!e || !in_cache(mq, e));
1076
1077 del(mq, e);
1078 free_entry(&mq->cache_pool, e);
1079}
1080
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +02001081static void mq_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
Joe Thornberf2836352013-03-01 22:45:51 +00001082{
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +02001083 struct mq_policy *mq = to_mq_policy(p);
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +02001084
1085 mutex_lock(&mq->lock);
Joe Thornber633618e2013-11-09 11:12:51 +00001086 __remove_mapping(mq, oblock);
Joe Thornberf2836352013-03-01 22:45:51 +00001087 mutex_unlock(&mq->lock);
1088}
1089
Joe Thornber532906a2013-11-08 16:36:17 +00001090static int __remove_cblock(struct mq_policy *mq, dm_cblock_t cblock)
1091{
1092 struct entry *e = epool_find(&mq->cache_pool, cblock);
1093
1094 if (!e)
1095 return -ENODATA;
1096
1097 del(mq, e);
1098 free_entry(&mq->cache_pool, e);
1099
1100 return 0;
1101}
1102
1103static int mq_remove_cblock(struct dm_cache_policy *p, dm_cblock_t cblock)
1104{
1105 int r;
1106 struct mq_policy *mq = to_mq_policy(p);
1107
1108 mutex_lock(&mq->lock);
1109 r = __remove_cblock(mq, cblock);
1110 mutex_unlock(&mq->lock);
1111
1112 return r;
1113}
1114
Joe Thornber01911c12013-10-24 14:10:28 -04001115static int __mq_writeback_work(struct mq_policy *mq, dm_oblock_t *oblock,
1116 dm_cblock_t *cblock)
1117{
1118 struct entry *e = pop(mq, &mq->cache_dirty);
1119
1120 if (!e)
1121 return -ENODATA;
1122
1123 *oblock = e->oblock;
Joe Thornber633618e2013-11-09 11:12:51 +00001124 *cblock = infer_cblock(&mq->cache_pool, e);
Joe Thornber01911c12013-10-24 14:10:28 -04001125 e->dirty = false;
1126 push(mq, e);
1127
1128 return 0;
1129}
1130
1131static int mq_writeback_work(struct dm_cache_policy *p, dm_oblock_t *oblock,
1132 dm_cblock_t *cblock)
1133{
1134 int r;
1135 struct mq_policy *mq = to_mq_policy(p);
1136
1137 mutex_lock(&mq->lock);
1138 r = __mq_writeback_work(mq, oblock, cblock);
1139 mutex_unlock(&mq->lock);
1140
1141 return r;
1142}
1143
Joe Thornber633618e2013-11-09 11:12:51 +00001144static void __force_mapping(struct mq_policy *mq,
1145 dm_oblock_t current_oblock, dm_oblock_t new_oblock)
Joe Thornberf2836352013-03-01 22:45:51 +00001146{
1147 struct entry *e = hash_lookup(mq, current_oblock);
1148
Joe Thornber633618e2013-11-09 11:12:51 +00001149 if (e && in_cache(mq, e)) {
1150 del(mq, e);
1151 e->oblock = new_oblock;
1152 e->dirty = true;
1153 push(mq, e);
1154 }
Joe Thornberf2836352013-03-01 22:45:51 +00001155}
1156
1157static void mq_force_mapping(struct dm_cache_policy *p,
1158 dm_oblock_t current_oblock, dm_oblock_t new_oblock)
1159{
1160 struct mq_policy *mq = to_mq_policy(p);
1161
1162 mutex_lock(&mq->lock);
Joe Thornber633618e2013-11-09 11:12:51 +00001163 __force_mapping(mq, current_oblock, new_oblock);
Joe Thornberf2836352013-03-01 22:45:51 +00001164 mutex_unlock(&mq->lock);
1165}
1166
1167static dm_cblock_t mq_residency(struct dm_cache_policy *p)
1168{
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001169 dm_cblock_t r;
Joe Thornberf2836352013-03-01 22:45:51 +00001170 struct mq_policy *mq = to_mq_policy(p);
1171
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001172 mutex_lock(&mq->lock);
Joe Thornber633618e2013-11-09 11:12:51 +00001173 r = to_cblock(mq->cache_pool.nr_allocated);
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001174 mutex_unlock(&mq->lock);
1175
1176 return r;
Joe Thornberf2836352013-03-01 22:45:51 +00001177}
1178
1179static void mq_tick(struct dm_cache_policy *p)
1180{
1181 struct mq_policy *mq = to_mq_policy(p);
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&mq->tick_lock, flags);
1185 mq->tick_protected++;
1186 spin_unlock_irqrestore(&mq->tick_lock, flags);
1187}
1188
1189static int mq_set_config_value(struct dm_cache_policy *p,
1190 const char *key, const char *value)
1191{
1192 struct mq_policy *mq = to_mq_policy(p);
Joe Thornberf2836352013-03-01 22:45:51 +00001193 unsigned long tmp;
1194
Joe Thornberf2836352013-03-01 22:45:51 +00001195 if (kstrtoul(value, 10, &tmp))
1196 return -EINVAL;
1197
Joe Thornber78e03d62013-12-09 12:53:05 +00001198 if (!strcasecmp(key, "random_threshold")) {
1199 mq->tracker.thresholds[PATTERN_RANDOM] = tmp;
1200
1201 } else if (!strcasecmp(key, "sequential_threshold")) {
1202 mq->tracker.thresholds[PATTERN_SEQUENTIAL] = tmp;
1203
1204 } else if (!strcasecmp(key, "discard_promote_adjustment"))
1205 mq->discard_promote_adjustment = tmp;
1206
1207 else if (!strcasecmp(key, "read_promote_adjustment"))
1208 mq->read_promote_adjustment = tmp;
1209
1210 else if (!strcasecmp(key, "write_promote_adjustment"))
1211 mq->write_promote_adjustment = tmp;
1212
1213 else
1214 return -EINVAL;
Joe Thornberf2836352013-03-01 22:45:51 +00001215
1216 return 0;
1217}
1218
1219static int mq_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen)
1220{
1221 ssize_t sz = 0;
1222 struct mq_policy *mq = to_mq_policy(p);
1223
Joe Thornber78e03d62013-12-09 12:53:05 +00001224 DMEMIT("10 random_threshold %u "
1225 "sequential_threshold %u "
1226 "discard_promote_adjustment %u "
1227 "read_promote_adjustment %u "
1228 "write_promote_adjustment %u",
Joe Thornberf2836352013-03-01 22:45:51 +00001229 mq->tracker.thresholds[PATTERN_RANDOM],
Joe Thornber78e03d62013-12-09 12:53:05 +00001230 mq->tracker.thresholds[PATTERN_SEQUENTIAL],
1231 mq->discard_promote_adjustment,
1232 mq->read_promote_adjustment,
1233 mq->write_promote_adjustment);
Joe Thornberf2836352013-03-01 22:45:51 +00001234
1235 return 0;
1236}
1237
1238/* Init the policy plugin interface function pointers. */
1239static void init_policy_functions(struct mq_policy *mq)
1240{
1241 mq->policy.destroy = mq_destroy;
1242 mq->policy.map = mq_map;
1243 mq->policy.lookup = mq_lookup;
Joe Thornber01911c12013-10-24 14:10:28 -04001244 mq->policy.set_dirty = mq_set_dirty;
1245 mq->policy.clear_dirty = mq_clear_dirty;
Joe Thornberf2836352013-03-01 22:45:51 +00001246 mq->policy.load_mapping = mq_load_mapping;
1247 mq->policy.walk_mappings = mq_walk_mappings;
1248 mq->policy.remove_mapping = mq_remove_mapping;
Joe Thornber532906a2013-11-08 16:36:17 +00001249 mq->policy.remove_cblock = mq_remove_cblock;
Joe Thornber01911c12013-10-24 14:10:28 -04001250 mq->policy.writeback_work = mq_writeback_work;
Joe Thornberf2836352013-03-01 22:45:51 +00001251 mq->policy.force_mapping = mq_force_mapping;
1252 mq->policy.residency = mq_residency;
1253 mq->policy.tick = mq_tick;
1254 mq->policy.emit_config_values = mq_emit_config_values;
1255 mq->policy.set_config_value = mq_set_config_value;
1256}
1257
1258static struct dm_cache_policy *mq_create(dm_cblock_t cache_size,
1259 sector_t origin_size,
1260 sector_t cache_block_size)
1261{
Joe Thornberf2836352013-03-01 22:45:51 +00001262 struct mq_policy *mq = kzalloc(sizeof(*mq), GFP_KERNEL);
1263
1264 if (!mq)
1265 return NULL;
1266
1267 init_policy_functions(mq);
1268 iot_init(&mq->tracker, SEQUENTIAL_THRESHOLD_DEFAULT, RANDOM_THRESHOLD_DEFAULT);
Joe Thornberf2836352013-03-01 22:45:51 +00001269 mq->cache_size = cache_size;
Joe Thornber633618e2013-11-09 11:12:51 +00001270
1271 if (epool_init(&mq->pre_cache_pool, from_cblock(cache_size))) {
1272 DMERR("couldn't initialize pool of pre-cache entries");
1273 goto bad_pre_cache_init;
1274 }
1275
1276 if (epool_init(&mq->cache_pool, from_cblock(cache_size))) {
1277 DMERR("couldn't initialize pool of cache entries");
1278 goto bad_cache_init;
1279 }
1280
Joe Thornberf2836352013-03-01 22:45:51 +00001281 mq->tick_protected = 0;
1282 mq->tick = 0;
1283 mq->hit_count = 0;
1284 mq->generation = 0;
Joe Thornber78e03d62013-12-09 12:53:05 +00001285 mq->discard_promote_adjustment = DEFAULT_DISCARD_PROMOTE_ADJUSTMENT;
1286 mq->read_promote_adjustment = DEFAULT_READ_PROMOTE_ADJUSTMENT;
1287 mq->write_promote_adjustment = DEFAULT_WRITE_PROMOTE_ADJUSTMENT;
Joe Thornberf2836352013-03-01 22:45:51 +00001288 mutex_init(&mq->lock);
1289 spin_lock_init(&mq->tick_lock);
Joe Thornberf2836352013-03-01 22:45:51 +00001290
1291 queue_init(&mq->pre_cache);
Joe Thornber01911c12013-10-24 14:10:28 -04001292 queue_init(&mq->cache_clean);
1293 queue_init(&mq->cache_dirty);
1294
Joe Thornberf2836352013-03-01 22:45:51 +00001295 mq->generation_period = max((unsigned) from_cblock(cache_size), 1024U);
1296
Joe Thornberf2836352013-03-01 22:45:51 +00001297 mq->nr_buckets = next_power(from_cblock(cache_size) / 2, 16);
1298 mq->hash_bits = ffs(mq->nr_buckets) - 1;
Heinz Mauelshagen14f398c2014-02-28 12:02:56 -05001299 mq->table = vzalloc(sizeof(*mq->table) * mq->nr_buckets);
Joe Thornberf2836352013-03-01 22:45:51 +00001300 if (!mq->table)
1301 goto bad_alloc_table;
1302
Joe Thornberf2836352013-03-01 22:45:51 +00001303 return &mq->policy;
1304
Joe Thornberf2836352013-03-01 22:45:51 +00001305bad_alloc_table:
Joe Thornber633618e2013-11-09 11:12:51 +00001306 epool_exit(&mq->cache_pool);
1307bad_cache_init:
1308 epool_exit(&mq->pre_cache_pool);
1309bad_pre_cache_init:
Joe Thornberf2836352013-03-01 22:45:51 +00001310 kfree(mq);
1311
1312 return NULL;
1313}
1314
1315/*----------------------------------------------------------------*/
1316
1317static struct dm_cache_policy_type mq_policy_type = {
1318 .name = "mq",
Mike Snitzerf1afb362014-10-30 10:02:01 -04001319 .version = {1, 3, 0},
Joe Thornberf2836352013-03-01 22:45:51 +00001320 .hint_size = 4,
1321 .owner = THIS_MODULE,
1322 .create = mq_create
1323};
1324
1325static struct dm_cache_policy_type default_policy_type = {
1326 .name = "default",
Mike Snitzerf1afb362014-10-30 10:02:01 -04001327 .version = {1, 3, 0},
Joe Thornberf2836352013-03-01 22:45:51 +00001328 .hint_size = 4,
1329 .owner = THIS_MODULE,
Mike Snitzer2e68c4e2014-01-15 21:06:55 -05001330 .create = mq_create,
1331 .real = &mq_policy_type
Joe Thornberf2836352013-03-01 22:45:51 +00001332};
1333
1334static int __init mq_init(void)
1335{
1336 int r;
1337
1338 mq_entry_cache = kmem_cache_create("dm_mq_policy_cache_entry",
1339 sizeof(struct entry),
1340 __alignof__(struct entry),
1341 0, NULL);
1342 if (!mq_entry_cache)
1343 goto bad;
1344
1345 r = dm_cache_policy_register(&mq_policy_type);
1346 if (r) {
1347 DMERR("register failed %d", r);
1348 goto bad_register_mq;
1349 }
1350
1351 r = dm_cache_policy_register(&default_policy_type);
1352 if (!r) {
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001353 DMINFO("version %u.%u.%u loaded",
1354 mq_policy_type.version[0],
1355 mq_policy_type.version[1],
1356 mq_policy_type.version[2]);
Joe Thornberf2836352013-03-01 22:45:51 +00001357 return 0;
1358 }
1359
1360 DMERR("register failed (as default) %d", r);
1361
1362 dm_cache_policy_unregister(&mq_policy_type);
1363bad_register_mq:
1364 kmem_cache_destroy(mq_entry_cache);
1365bad:
1366 return -ENOMEM;
1367}
1368
1369static void __exit mq_exit(void)
1370{
1371 dm_cache_policy_unregister(&mq_policy_type);
1372 dm_cache_policy_unregister(&default_policy_type);
1373
1374 kmem_cache_destroy(mq_entry_cache);
1375}
1376
1377module_init(mq_init);
1378module_exit(mq_exit);
1379
1380MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1381MODULE_LICENSE("GPL");
1382MODULE_DESCRIPTION("mq cache policy");
1383
1384MODULE_ALIAS("dm-cache-default");