blob: a9a25de5b0119462e3998e4cd58965b6d4e70bc9 [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
29static unsigned long *alloc_bitset(unsigned nr_entries)
30{
31 size_t s = sizeof(unsigned long) * dm_div_up(nr_entries, BITS_PER_LONG);
32 return vzalloc(s);
33}
34
35static void free_bitset(unsigned long *bits)
36{
37 vfree(bits);
38}
39
40/*----------------------------------------------------------------*/
41
42/*
43 * Large, sequential ios are probably better left on the origin device since
44 * spindles tend to have good bandwidth.
45 *
46 * The io_tracker tries to spot when the io is in one of these sequential
47 * modes.
48 *
49 * Two thresholds to switch between random and sequential io mode are defaulting
50 * as follows and can be adjusted via the constructor and message interfaces.
51 */
52#define RANDOM_THRESHOLD_DEFAULT 4
53#define SEQUENTIAL_THRESHOLD_DEFAULT 512
54
55enum io_pattern {
56 PATTERN_SEQUENTIAL,
57 PATTERN_RANDOM
58};
59
60struct io_tracker {
61 enum io_pattern pattern;
62
63 unsigned nr_seq_samples;
64 unsigned nr_rand_samples;
65 unsigned thresholds[2];
66
67 dm_oblock_t last_end_oblock;
68};
69
70static void iot_init(struct io_tracker *t,
71 int sequential_threshold, int random_threshold)
72{
73 t->pattern = PATTERN_RANDOM;
74 t->nr_seq_samples = 0;
75 t->nr_rand_samples = 0;
76 t->last_end_oblock = 0;
77 t->thresholds[PATTERN_RANDOM] = random_threshold;
78 t->thresholds[PATTERN_SEQUENTIAL] = sequential_threshold;
79}
80
81static enum io_pattern iot_pattern(struct io_tracker *t)
82{
83 return t->pattern;
84}
85
86static void iot_update_stats(struct io_tracker *t, struct bio *bio)
87{
88 if (bio->bi_sector == from_oblock(t->last_end_oblock) + 1)
89 t->nr_seq_samples++;
90 else {
91 /*
92 * Just one non-sequential IO is enough to reset the
93 * counters.
94 */
95 if (t->nr_seq_samples) {
96 t->nr_seq_samples = 0;
97 t->nr_rand_samples = 0;
98 }
99
100 t->nr_rand_samples++;
101 }
102
103 t->last_end_oblock = to_oblock(bio->bi_sector + bio_sectors(bio) - 1);
104}
105
106static void iot_check_for_pattern_switch(struct io_tracker *t)
107{
108 switch (t->pattern) {
109 case PATTERN_SEQUENTIAL:
110 if (t->nr_rand_samples >= t->thresholds[PATTERN_RANDOM]) {
111 t->pattern = PATTERN_RANDOM;
112 t->nr_seq_samples = t->nr_rand_samples = 0;
113 }
114 break;
115
116 case PATTERN_RANDOM:
117 if (t->nr_seq_samples >= t->thresholds[PATTERN_SEQUENTIAL]) {
118 t->pattern = PATTERN_SEQUENTIAL;
119 t->nr_seq_samples = t->nr_rand_samples = 0;
120 }
121 break;
122 }
123}
124
125static void iot_examine_bio(struct io_tracker *t, struct bio *bio)
126{
127 iot_update_stats(t, bio);
128 iot_check_for_pattern_switch(t);
129}
130
131/*----------------------------------------------------------------*/
132
133
134/*
135 * This queue is divided up into different levels. Allowing us to push
136 * entries to the back of any of the levels. Think of it as a partially
137 * sorted queue.
138 */
139#define NR_QUEUE_LEVELS 16u
140
141struct queue {
142 struct list_head qs[NR_QUEUE_LEVELS];
143};
144
145static void queue_init(struct queue *q)
146{
147 unsigned i;
148
149 for (i = 0; i < NR_QUEUE_LEVELS; i++)
150 INIT_LIST_HEAD(q->qs + i);
151}
152
153/*
154 * Insert an entry to the back of the given level.
155 */
156static void queue_push(struct queue *q, unsigned level, struct list_head *elt)
157{
158 list_add_tail(elt, q->qs + level);
159}
160
161static void queue_remove(struct list_head *elt)
162{
163 list_del(elt);
164}
165
166/*
167 * Shifts all regions down one level. This has no effect on the order of
168 * the queue.
169 */
170static void queue_shift_down(struct queue *q)
171{
172 unsigned level;
173
174 for (level = 1; level < NR_QUEUE_LEVELS; level++)
175 list_splice_init(q->qs + level, q->qs + level - 1);
176}
177
178/*
179 * Gives us the oldest entry of the lowest popoulated level. If the first
180 * level is emptied then we shift down one level.
181 */
182static struct list_head *queue_pop(struct queue *q)
183{
184 unsigned level;
185 struct list_head *r;
186
187 for (level = 0; level < NR_QUEUE_LEVELS; level++)
188 if (!list_empty(q->qs + level)) {
189 r = q->qs[level].next;
190 list_del(r);
191
192 /* have we just emptied the bottom level? */
193 if (level == 0 && list_empty(q->qs))
194 queue_shift_down(q);
195
196 return r;
197 }
198
199 return NULL;
200}
201
202static struct list_head *list_pop(struct list_head *lh)
203{
204 struct list_head *r = lh->next;
205
206 BUG_ON(!r);
207 list_del_init(r);
208
209 return r;
210}
211
212/*----------------------------------------------------------------*/
213
214/*
215 * Describes a cache entry. Used in both the cache and the pre_cache.
216 */
217struct entry {
218 struct hlist_node hlist;
219 struct list_head list;
220 dm_oblock_t oblock;
221 dm_cblock_t cblock; /* valid iff in_cache */
222
223 /*
224 * FIXME: pack these better
225 */
226 bool in_cache:1;
227 unsigned hit_count;
228 unsigned generation;
229 unsigned tick;
230};
231
232struct mq_policy {
233 struct dm_cache_policy policy;
234
235 /* protects everything */
236 struct mutex lock;
237 dm_cblock_t cache_size;
238 struct io_tracker tracker;
239
240 /*
241 * We maintain two queues of entries. The cache proper contains
242 * the currently active mappings. Whereas the pre_cache tracks
243 * blocks that are being hit frequently and potential candidates
244 * for promotion to the cache.
245 */
246 struct queue pre_cache;
247 struct queue cache;
248
249 /*
250 * Keeps track of time, incremented by the core. We use this to
251 * avoid attributing multiple hits within the same tick.
252 *
253 * Access to tick_protected should be done with the spin lock held.
254 * It's copied to tick at the start of the map function (within the
255 * mutex).
256 */
257 spinlock_t tick_lock;
258 unsigned tick_protected;
259 unsigned tick;
260
261 /*
262 * A count of the number of times the map function has been called
263 * and found an entry in the pre_cache or cache. Currently used to
264 * calculate the generation.
265 */
266 unsigned hit_count;
267
268 /*
269 * A generation is a longish period that is used to trigger some
270 * book keeping effects. eg, decrementing hit counts on entries.
271 * This is needed to allow the cache to evolve as io patterns
272 * change.
273 */
274 unsigned generation;
275 unsigned generation_period; /* in lookups (will probably change) */
276
277 /*
278 * Entries in the pre_cache whose hit count passes the promotion
279 * threshold move to the cache proper. Working out the correct
280 * value for the promotion_threshold is crucial to this policy.
281 */
282 unsigned promote_threshold;
283
284 /*
285 * We need cache_size entries for the cache, and choose to have
286 * cache_size entries for the pre_cache too. One motivation for
287 * using the same size is to make the hit counts directly
288 * comparable between pre_cache and cache.
289 */
290 unsigned nr_entries;
291 unsigned nr_entries_allocated;
292 struct list_head free;
293
294 /*
295 * Cache blocks may be unallocated. We store this info in a
296 * bitset.
297 */
298 unsigned long *allocation_bitset;
299 unsigned nr_cblocks_allocated;
300 unsigned find_free_nr_words;
301 unsigned find_free_last_word;
302
303 /*
304 * The hash table allows us to quickly find an entry by origin
305 * block. Both pre_cache and cache entries are in here.
306 */
307 unsigned nr_buckets;
308 dm_block_t hash_bits;
309 struct hlist_head *table;
310};
311
312/*----------------------------------------------------------------*/
313/* Free/alloc mq cache entry structures. */
Joe Thornber0184b442013-10-24 14:10:28 -0400314static void concat_queue(struct list_head *lh, struct queue *q)
Joe Thornberf2836352013-03-01 22:45:51 +0000315{
316 unsigned level;
317
318 for (level = 0; level < NR_QUEUE_LEVELS; level++)
319 list_splice(q->qs + level, lh);
320}
321
322static void free_entries(struct mq_policy *mq)
323{
324 struct entry *e, *tmp;
325
Joe Thornber0184b442013-10-24 14:10:28 -0400326 concat_queue(&mq->free, &mq->pre_cache);
327 concat_queue(&mq->free, &mq->cache);
Joe Thornberf2836352013-03-01 22:45:51 +0000328
329 list_for_each_entry_safe(e, tmp, &mq->free, list)
330 kmem_cache_free(mq_entry_cache, e);
331}
332
333static int alloc_entries(struct mq_policy *mq, unsigned elts)
334{
335 unsigned u = mq->nr_entries;
336
337 INIT_LIST_HEAD(&mq->free);
338 mq->nr_entries_allocated = 0;
339
340 while (u--) {
341 struct entry *e = kmem_cache_zalloc(mq_entry_cache, GFP_KERNEL);
342
343 if (!e) {
344 free_entries(mq);
345 return -ENOMEM;
346 }
347
348
349 list_add(&e->list, &mq->free);
350 }
351
352 return 0;
353}
354
355/*----------------------------------------------------------------*/
356
357/*
358 * Simple hash table implementation. Should replace with the standard hash
359 * table that's making its way upstream.
360 */
361static void hash_insert(struct mq_policy *mq, struct entry *e)
362{
363 unsigned h = hash_64(from_oblock(e->oblock), mq->hash_bits);
364
365 hlist_add_head(&e->hlist, mq->table + h);
366}
367
368static struct entry *hash_lookup(struct mq_policy *mq, dm_oblock_t oblock)
369{
370 unsigned h = hash_64(from_oblock(oblock), mq->hash_bits);
371 struct hlist_head *bucket = mq->table + h;
372 struct entry *e;
373
374 hlist_for_each_entry(e, bucket, hlist)
375 if (e->oblock == oblock) {
376 hlist_del(&e->hlist);
377 hlist_add_head(&e->hlist, bucket);
378 return e;
379 }
380
381 return NULL;
382}
383
384static void hash_remove(struct entry *e)
385{
386 hlist_del(&e->hlist);
387}
388
389/*----------------------------------------------------------------*/
390
391/*
392 * Allocates a new entry structure. The memory is allocated in one lump,
393 * so we just handing it out here. Returns NULL if all entries have
394 * already been allocated. Cannot fail otherwise.
395 */
396static struct entry *alloc_entry(struct mq_policy *mq)
397{
398 struct entry *e;
399
400 if (mq->nr_entries_allocated >= mq->nr_entries) {
401 BUG_ON(!list_empty(&mq->free));
402 return NULL;
403 }
404
405 e = list_entry(list_pop(&mq->free), struct entry, list);
406 INIT_LIST_HEAD(&e->list);
407 INIT_HLIST_NODE(&e->hlist);
408
409 mq->nr_entries_allocated++;
410 return e;
411}
412
413/*----------------------------------------------------------------*/
414
415/*
416 * Mark cache blocks allocated or not in the bitset.
417 */
418static void alloc_cblock(struct mq_policy *mq, dm_cblock_t cblock)
419{
420 BUG_ON(from_cblock(cblock) > from_cblock(mq->cache_size));
421 BUG_ON(test_bit(from_cblock(cblock), mq->allocation_bitset));
422
423 set_bit(from_cblock(cblock), mq->allocation_bitset);
424 mq->nr_cblocks_allocated++;
425}
426
427static void free_cblock(struct mq_policy *mq, dm_cblock_t cblock)
428{
429 BUG_ON(from_cblock(cblock) > from_cblock(mq->cache_size));
430 BUG_ON(!test_bit(from_cblock(cblock), mq->allocation_bitset));
431
432 clear_bit(from_cblock(cblock), mq->allocation_bitset);
433 mq->nr_cblocks_allocated--;
434}
435
436static bool any_free_cblocks(struct mq_policy *mq)
437{
438 return mq->nr_cblocks_allocated < from_cblock(mq->cache_size);
439}
440
441/*
442 * Fills result out with a cache block that isn't in use, or return
443 * -ENOSPC. This does _not_ mark the cblock as allocated, the caller is
444 * reponsible for that.
445 */
446static int __find_free_cblock(struct mq_policy *mq, unsigned begin, unsigned end,
447 dm_cblock_t *result, unsigned *last_word)
448{
449 int r = -ENOSPC;
450 unsigned w;
451
452 for (w = begin; w < end; w++) {
453 /*
454 * ffz is undefined if no zero exists
455 */
456 if (mq->allocation_bitset[w] != ~0UL) {
457 *last_word = w;
458 *result = to_cblock((w * BITS_PER_LONG) + ffz(mq->allocation_bitset[w]));
459 if (from_cblock(*result) < from_cblock(mq->cache_size))
460 r = 0;
461
462 break;
463 }
464 }
465
466 return r;
467}
468
469static int find_free_cblock(struct mq_policy *mq, dm_cblock_t *result)
470{
471 int r;
472
473 if (!any_free_cblocks(mq))
474 return -ENOSPC;
475
476 r = __find_free_cblock(mq, mq->find_free_last_word, mq->find_free_nr_words, result, &mq->find_free_last_word);
477 if (r == -ENOSPC && mq->find_free_last_word)
478 r = __find_free_cblock(mq, 0, mq->find_free_last_word, result, &mq->find_free_last_word);
479
480 return r;
481}
482
483/*----------------------------------------------------------------*/
484
485/*
486 * Now we get to the meat of the policy. This section deals with deciding
487 * when to to add entries to the pre_cache and cache, and move between
488 * them.
489 */
490
491/*
492 * The queue level is based on the log2 of the hit count.
493 */
494static unsigned queue_level(struct entry *e)
495{
496 return min((unsigned) ilog2(e->hit_count), NR_QUEUE_LEVELS - 1u);
497}
498
499/*
500 * Inserts the entry into the pre_cache or the cache. Ensures the cache
501 * block is marked as allocated if necc. Inserts into the hash table. Sets the
502 * tick which records when the entry was last moved about.
503 */
504static void push(struct mq_policy *mq, struct entry *e)
505{
506 e->tick = mq->tick;
507 hash_insert(mq, e);
508
509 if (e->in_cache) {
510 alloc_cblock(mq, e->cblock);
511 queue_push(&mq->cache, queue_level(e), &e->list);
512 } else
513 queue_push(&mq->pre_cache, queue_level(e), &e->list);
514}
515
516/*
517 * Removes an entry from pre_cache or cache. Removes from the hash table.
518 * Frees off the cache block if necc.
519 */
520static void del(struct mq_policy *mq, struct entry *e)
521{
522 queue_remove(&e->list);
523 hash_remove(e);
524 if (e->in_cache)
525 free_cblock(mq, e->cblock);
526}
527
528/*
529 * Like del, except it removes the first entry in the queue (ie. the least
530 * recently used).
531 */
532static struct entry *pop(struct mq_policy *mq, struct queue *q)
533{
Joe Thornber0184b442013-10-24 14:10:28 -0400534 struct entry *e;
535 struct list_head *h = queue_pop(q);
Joe Thornberf2836352013-03-01 22:45:51 +0000536
Joe Thornber0184b442013-10-24 14:10:28 -0400537 if (!h)
538 return NULL;
Joe Thornberf2836352013-03-01 22:45:51 +0000539
Joe Thornber0184b442013-10-24 14:10:28 -0400540 e = container_of(h, struct entry, list);
541 hash_remove(e);
542 if (e->in_cache)
543 free_cblock(mq, e->cblock);
Joe Thornberf2836352013-03-01 22:45:51 +0000544
545 return e;
546}
547
548/*
549 * Has this entry already been updated?
550 */
551static bool updated_this_tick(struct mq_policy *mq, struct entry *e)
552{
553 return mq->tick == e->tick;
554}
555
556/*
557 * The promotion threshold is adjusted every generation. As are the counts
558 * of the entries.
559 *
560 * At the moment the threshold is taken by averaging the hit counts of some
561 * of the entries in the cache (the first 20 entries of the first level).
562 *
563 * We can be much cleverer than this though. For example, each promotion
564 * could bump up the threshold helping to prevent churn. Much more to do
565 * here.
566 */
567
568#define MAX_TO_AVERAGE 20
569
570static void check_generation(struct mq_policy *mq)
571{
572 unsigned total = 0, nr = 0, count = 0, level;
573 struct list_head *head;
574 struct entry *e;
575
576 if ((mq->hit_count >= mq->generation_period) &&
577 (mq->nr_cblocks_allocated == from_cblock(mq->cache_size))) {
578
579 mq->hit_count = 0;
580 mq->generation++;
581
582 for (level = 0; level < NR_QUEUE_LEVELS && count < MAX_TO_AVERAGE; level++) {
583 head = mq->cache.qs + level;
584 list_for_each_entry(e, head, list) {
585 nr++;
586 total += e->hit_count;
587
588 if (++count >= MAX_TO_AVERAGE)
589 break;
590 }
591 }
592
593 mq->promote_threshold = nr ? total / nr : 1;
594 if (mq->promote_threshold * nr < total)
595 mq->promote_threshold++;
596 }
597}
598
599/*
600 * Whenever we use an entry we bump up it's hit counter, and push it to the
601 * back to it's current level.
602 */
603static void requeue_and_update_tick(struct mq_policy *mq, struct entry *e)
604{
605 if (updated_this_tick(mq, e))
606 return;
607
608 e->hit_count++;
609 mq->hit_count++;
610 check_generation(mq);
611
612 /* generation adjustment, to stop the counts increasing forever. */
613 /* FIXME: divide? */
614 /* e->hit_count -= min(e->hit_count - 1, mq->generation - e->generation); */
615 e->generation = mq->generation;
616
617 del(mq, e);
618 push(mq, e);
619}
620
621/*
622 * Demote the least recently used entry from the cache to the pre_cache.
623 * Returns the new cache entry to use, and the old origin block it was
624 * mapped to.
625 *
626 * We drop the hit count on the demoted entry back to 1 to stop it bouncing
627 * straight back into the cache if it's subsequently hit. There are
628 * various options here, and more experimentation would be good:
629 *
630 * - just forget about the demoted entry completely (ie. don't insert it
631 into the pre_cache).
632 * - divide the hit count rather that setting to some hard coded value.
633 * - set the hit count to a hard coded value other than 1, eg, is it better
634 * if it goes in at level 2?
635 */
636static dm_cblock_t demote_cblock(struct mq_policy *mq, dm_oblock_t *oblock)
637{
638 dm_cblock_t result;
639 struct entry *demoted = pop(mq, &mq->cache);
640
641 BUG_ON(!demoted);
642 result = demoted->cblock;
643 *oblock = demoted->oblock;
644 demoted->in_cache = false;
645 demoted->hit_count = 1;
646 push(mq, demoted);
647
648 return result;
649}
650
651/*
652 * We modify the basic promotion_threshold depending on the specific io.
653 *
654 * If the origin block has been discarded then there's no cost to copy it
655 * to the cache.
656 *
657 * We bias towards reads, since they can be demoted at no cost if they
658 * haven't been dirtied.
659 */
660#define DISCARDED_PROMOTE_THRESHOLD 1
661#define READ_PROMOTE_THRESHOLD 4
662#define WRITE_PROMOTE_THRESHOLD 8
663
664static unsigned adjusted_promote_threshold(struct mq_policy *mq,
665 bool discarded_oblock, int data_dir)
666{
667 if (discarded_oblock && any_free_cblocks(mq) && data_dir == WRITE)
668 /*
669 * We don't need to do any copying at all, so give this a
670 * very low threshold. In practice this only triggers
671 * during initial population after a format.
672 */
673 return DISCARDED_PROMOTE_THRESHOLD;
674
675 return data_dir == READ ?
676 (mq->promote_threshold + READ_PROMOTE_THRESHOLD) :
677 (mq->promote_threshold + WRITE_PROMOTE_THRESHOLD);
678}
679
680static bool should_promote(struct mq_policy *mq, struct entry *e,
681 bool discarded_oblock, int data_dir)
682{
683 return e->hit_count >=
684 adjusted_promote_threshold(mq, discarded_oblock, data_dir);
685}
686
687static int cache_entry_found(struct mq_policy *mq,
688 struct entry *e,
689 struct policy_result *result)
690{
691 requeue_and_update_tick(mq, e);
692
693 if (e->in_cache) {
694 result->op = POLICY_HIT;
695 result->cblock = e->cblock;
696 }
697
698 return 0;
699}
700
701/*
Joe Thornber0184b442013-10-24 14:10:28 -0400702 * Moves an entry from the pre_cache to the cache. The main work is
Joe Thornberf2836352013-03-01 22:45:51 +0000703 * finding which cache block to use.
704 */
705static int pre_cache_to_cache(struct mq_policy *mq, struct entry *e,
706 struct policy_result *result)
707{
708 dm_cblock_t cblock;
709
710 if (find_free_cblock(mq, &cblock) == -ENOSPC) {
711 result->op = POLICY_REPLACE;
712 cblock = demote_cblock(mq, &result->old_oblock);
713 } else
714 result->op = POLICY_NEW;
715
716 result->cblock = e->cblock = cblock;
717
718 del(mq, e);
719 e->in_cache = true;
720 push(mq, e);
721
722 return 0;
723}
724
725static int pre_cache_entry_found(struct mq_policy *mq, struct entry *e,
726 bool can_migrate, bool discarded_oblock,
727 int data_dir, struct policy_result *result)
728{
729 int r = 0;
730 bool updated = updated_this_tick(mq, e);
731
732 requeue_and_update_tick(mq, e);
733
734 if ((!discarded_oblock && updated) ||
735 !should_promote(mq, e, discarded_oblock, data_dir))
736 result->op = POLICY_MISS;
737 else if (!can_migrate)
738 r = -EWOULDBLOCK;
739 else
740 r = pre_cache_to_cache(mq, e, result);
741
742 return r;
743}
744
745static void insert_in_pre_cache(struct mq_policy *mq,
746 dm_oblock_t oblock)
747{
748 struct entry *e = alloc_entry(mq);
749
750 if (!e)
751 /*
752 * There's no spare entry structure, so we grab the least
753 * used one from the pre_cache.
754 */
755 e = pop(mq, &mq->pre_cache);
756
757 if (unlikely(!e)) {
758 DMWARN("couldn't pop from pre cache");
759 return;
760 }
761
762 e->in_cache = false;
763 e->oblock = oblock;
764 e->hit_count = 1;
765 e->generation = mq->generation;
766 push(mq, e);
767}
768
769static void insert_in_cache(struct mq_policy *mq, dm_oblock_t oblock,
770 struct policy_result *result)
771{
772 struct entry *e;
773 dm_cblock_t cblock;
774
775 if (find_free_cblock(mq, &cblock) == -ENOSPC) {
776 result->op = POLICY_MISS;
777 insert_in_pre_cache(mq, oblock);
778 return;
779 }
780
781 e = alloc_entry(mq);
782 if (unlikely(!e)) {
783 result->op = POLICY_MISS;
784 return;
785 }
786
787 e->oblock = oblock;
788 e->cblock = cblock;
789 e->in_cache = true;
790 e->hit_count = 1;
791 e->generation = mq->generation;
792 push(mq, e);
793
794 result->op = POLICY_NEW;
795 result->cblock = e->cblock;
796}
797
798static int no_entry_found(struct mq_policy *mq, dm_oblock_t oblock,
799 bool can_migrate, bool discarded_oblock,
800 int data_dir, struct policy_result *result)
801{
802 if (adjusted_promote_threshold(mq, discarded_oblock, data_dir) == 1) {
803 if (can_migrate)
804 insert_in_cache(mq, oblock, result);
805 else
806 return -EWOULDBLOCK;
807 } else {
808 insert_in_pre_cache(mq, oblock);
809 result->op = POLICY_MISS;
810 }
811
812 return 0;
813}
814
815/*
816 * Looks the oblock up in the hash table, then decides whether to put in
817 * pre_cache, or cache etc.
818 */
819static int map(struct mq_policy *mq, dm_oblock_t oblock,
820 bool can_migrate, bool discarded_oblock,
821 int data_dir, struct policy_result *result)
822{
823 int r = 0;
824 struct entry *e = hash_lookup(mq, oblock);
825
826 if (e && e->in_cache)
827 r = cache_entry_found(mq, e, result);
828 else if (iot_pattern(&mq->tracker) == PATTERN_SEQUENTIAL)
829 result->op = POLICY_MISS;
830 else if (e)
831 r = pre_cache_entry_found(mq, e, can_migrate, discarded_oblock,
832 data_dir, result);
833 else
834 r = no_entry_found(mq, oblock, can_migrate, discarded_oblock,
835 data_dir, result);
836
837 if (r == -EWOULDBLOCK)
838 result->op = POLICY_MISS;
839
840 return r;
841}
842
843/*----------------------------------------------------------------*/
844
845/*
846 * Public interface, via the policy struct. See dm-cache-policy.h for a
847 * description of these.
848 */
849
850static struct mq_policy *to_mq_policy(struct dm_cache_policy *p)
851{
852 return container_of(p, struct mq_policy, policy);
853}
854
855static void mq_destroy(struct dm_cache_policy *p)
856{
857 struct mq_policy *mq = to_mq_policy(p);
858
859 free_bitset(mq->allocation_bitset);
860 kfree(mq->table);
861 free_entries(mq);
862 kfree(mq);
863}
864
865static void copy_tick(struct mq_policy *mq)
866{
867 unsigned long flags;
868
869 spin_lock_irqsave(&mq->tick_lock, flags);
870 mq->tick = mq->tick_protected;
871 spin_unlock_irqrestore(&mq->tick_lock, flags);
872}
873
874static int mq_map(struct dm_cache_policy *p, dm_oblock_t oblock,
875 bool can_block, bool can_migrate, bool discarded_oblock,
876 struct bio *bio, struct policy_result *result)
877{
878 int r;
879 struct mq_policy *mq = to_mq_policy(p);
880
881 result->op = POLICY_MISS;
882
883 if (can_block)
884 mutex_lock(&mq->lock);
885 else if (!mutex_trylock(&mq->lock))
886 return -EWOULDBLOCK;
887
888 copy_tick(mq);
889
890 iot_examine_bio(&mq->tracker, bio);
891 r = map(mq, oblock, can_migrate, discarded_oblock,
892 bio_data_dir(bio), result);
893
894 mutex_unlock(&mq->lock);
895
896 return r;
897}
898
899static int mq_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock)
900{
901 int r;
902 struct mq_policy *mq = to_mq_policy(p);
903 struct entry *e;
904
905 if (!mutex_trylock(&mq->lock))
906 return -EWOULDBLOCK;
907
908 e = hash_lookup(mq, oblock);
909 if (e && e->in_cache) {
910 *cblock = e->cblock;
911 r = 0;
912 } else
913 r = -ENOENT;
914
915 mutex_unlock(&mq->lock);
916
917 return r;
918}
919
920static int mq_load_mapping(struct dm_cache_policy *p,
921 dm_oblock_t oblock, dm_cblock_t cblock,
922 uint32_t hint, bool hint_valid)
923{
924 struct mq_policy *mq = to_mq_policy(p);
925 struct entry *e;
926
927 e = alloc_entry(mq);
928 if (!e)
929 return -ENOMEM;
930
931 e->cblock = cblock;
932 e->oblock = oblock;
933 e->in_cache = true;
934 e->hit_count = hint_valid ? hint : 1;
935 e->generation = mq->generation;
936 push(mq, e);
937
938 return 0;
939}
940
941static int mq_walk_mappings(struct dm_cache_policy *p, policy_walk_fn fn,
942 void *context)
943{
944 struct mq_policy *mq = to_mq_policy(p);
945 int r = 0;
946 struct entry *e;
947 unsigned level;
948
949 mutex_lock(&mq->lock);
950
951 for (level = 0; level < NR_QUEUE_LEVELS; level++)
952 list_for_each_entry(e, &mq->cache.qs[level], list) {
953 r = fn(context, e->cblock, e->oblock, e->hit_count);
954 if (r)
955 goto out;
956 }
957
958out:
959 mutex_unlock(&mq->lock);
960
961 return r;
962}
963
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +0200964static void mq_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
Joe Thornberf2836352013-03-01 22:45:51 +0000965{
Geert Uytterhoevenb936bf82013-07-26 09:57:31 +0200966 struct mq_policy *mq = to_mq_policy(p);
967 struct entry *e;
968
969 mutex_lock(&mq->lock);
970
971 e = hash_lookup(mq, oblock);
Joe Thornberf2836352013-03-01 22:45:51 +0000972
973 BUG_ON(!e || !e->in_cache);
974
975 del(mq, e);
976 e->in_cache = false;
977 push(mq, e);
Joe Thornberf2836352013-03-01 22:45:51 +0000978
Joe Thornberf2836352013-03-01 22:45:51 +0000979 mutex_unlock(&mq->lock);
980}
981
982static void force_mapping(struct mq_policy *mq,
983 dm_oblock_t current_oblock, dm_oblock_t new_oblock)
984{
985 struct entry *e = hash_lookup(mq, current_oblock);
986
987 BUG_ON(!e || !e->in_cache);
988
989 del(mq, e);
990 e->oblock = new_oblock;
991 push(mq, e);
992}
993
994static void mq_force_mapping(struct dm_cache_policy *p,
995 dm_oblock_t current_oblock, dm_oblock_t new_oblock)
996{
997 struct mq_policy *mq = to_mq_policy(p);
998
999 mutex_lock(&mq->lock);
1000 force_mapping(mq, current_oblock, new_oblock);
1001 mutex_unlock(&mq->lock);
1002}
1003
1004static dm_cblock_t mq_residency(struct dm_cache_policy *p)
1005{
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001006 dm_cblock_t r;
Joe Thornberf2836352013-03-01 22:45:51 +00001007 struct mq_policy *mq = to_mq_policy(p);
1008
Joe Thornber99ba2ae2013-10-21 11:44:57 +01001009 mutex_lock(&mq->lock);
1010 r = to_cblock(mq->nr_cblocks_allocated);
1011 mutex_unlock(&mq->lock);
1012
1013 return r;
Joe Thornberf2836352013-03-01 22:45:51 +00001014}
1015
1016static void mq_tick(struct dm_cache_policy *p)
1017{
1018 struct mq_policy *mq = to_mq_policy(p);
1019 unsigned long flags;
1020
1021 spin_lock_irqsave(&mq->tick_lock, flags);
1022 mq->tick_protected++;
1023 spin_unlock_irqrestore(&mq->tick_lock, flags);
1024}
1025
1026static int mq_set_config_value(struct dm_cache_policy *p,
1027 const char *key, const char *value)
1028{
1029 struct mq_policy *mq = to_mq_policy(p);
1030 enum io_pattern pattern;
1031 unsigned long tmp;
1032
1033 if (!strcasecmp(key, "random_threshold"))
1034 pattern = PATTERN_RANDOM;
1035 else if (!strcasecmp(key, "sequential_threshold"))
1036 pattern = PATTERN_SEQUENTIAL;
1037 else
1038 return -EINVAL;
1039
1040 if (kstrtoul(value, 10, &tmp))
1041 return -EINVAL;
1042
1043 mq->tracker.thresholds[pattern] = tmp;
1044
1045 return 0;
1046}
1047
1048static int mq_emit_config_values(struct dm_cache_policy *p, char *result, unsigned maxlen)
1049{
1050 ssize_t sz = 0;
1051 struct mq_policy *mq = to_mq_policy(p);
1052
1053 DMEMIT("4 random_threshold %u sequential_threshold %u",
1054 mq->tracker.thresholds[PATTERN_RANDOM],
1055 mq->tracker.thresholds[PATTERN_SEQUENTIAL]);
1056
1057 return 0;
1058}
1059
1060/* Init the policy plugin interface function pointers. */
1061static void init_policy_functions(struct mq_policy *mq)
1062{
1063 mq->policy.destroy = mq_destroy;
1064 mq->policy.map = mq_map;
1065 mq->policy.lookup = mq_lookup;
1066 mq->policy.load_mapping = mq_load_mapping;
1067 mq->policy.walk_mappings = mq_walk_mappings;
1068 mq->policy.remove_mapping = mq_remove_mapping;
1069 mq->policy.writeback_work = NULL;
1070 mq->policy.force_mapping = mq_force_mapping;
1071 mq->policy.residency = mq_residency;
1072 mq->policy.tick = mq_tick;
1073 mq->policy.emit_config_values = mq_emit_config_values;
1074 mq->policy.set_config_value = mq_set_config_value;
1075}
1076
1077static struct dm_cache_policy *mq_create(dm_cblock_t cache_size,
1078 sector_t origin_size,
1079 sector_t cache_block_size)
1080{
1081 int r;
1082 struct mq_policy *mq = kzalloc(sizeof(*mq), GFP_KERNEL);
1083
1084 if (!mq)
1085 return NULL;
1086
1087 init_policy_functions(mq);
1088 iot_init(&mq->tracker, SEQUENTIAL_THRESHOLD_DEFAULT, RANDOM_THRESHOLD_DEFAULT);
1089
1090 mq->cache_size = cache_size;
1091 mq->tick_protected = 0;
1092 mq->tick = 0;
1093 mq->hit_count = 0;
1094 mq->generation = 0;
1095 mq->promote_threshold = 0;
1096 mutex_init(&mq->lock);
1097 spin_lock_init(&mq->tick_lock);
1098 mq->find_free_nr_words = dm_div_up(from_cblock(mq->cache_size), BITS_PER_LONG);
1099 mq->find_free_last_word = 0;
1100
1101 queue_init(&mq->pre_cache);
1102 queue_init(&mq->cache);
1103 mq->generation_period = max((unsigned) from_cblock(cache_size), 1024U);
1104
1105 mq->nr_entries = 2 * from_cblock(cache_size);
1106 r = alloc_entries(mq, mq->nr_entries);
1107 if (r)
1108 goto bad_cache_alloc;
1109
1110 mq->nr_entries_allocated = 0;
1111 mq->nr_cblocks_allocated = 0;
1112
1113 mq->nr_buckets = next_power(from_cblock(cache_size) / 2, 16);
1114 mq->hash_bits = ffs(mq->nr_buckets) - 1;
1115 mq->table = kzalloc(sizeof(*mq->table) * mq->nr_buckets, GFP_KERNEL);
1116 if (!mq->table)
1117 goto bad_alloc_table;
1118
1119 mq->allocation_bitset = alloc_bitset(from_cblock(cache_size));
1120 if (!mq->allocation_bitset)
1121 goto bad_alloc_bitset;
1122
1123 return &mq->policy;
1124
1125bad_alloc_bitset:
1126 kfree(mq->table);
1127bad_alloc_table:
1128 free_entries(mq);
1129bad_cache_alloc:
1130 kfree(mq);
1131
1132 return NULL;
1133}
1134
1135/*----------------------------------------------------------------*/
1136
1137static struct dm_cache_policy_type mq_policy_type = {
1138 .name = "mq",
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001139 .version = {1, 0, 0},
Joe Thornberf2836352013-03-01 22:45:51 +00001140 .hint_size = 4,
1141 .owner = THIS_MODULE,
1142 .create = mq_create
1143};
1144
1145static struct dm_cache_policy_type default_policy_type = {
1146 .name = "default",
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001147 .version = {1, 0, 0},
Joe Thornberf2836352013-03-01 22:45:51 +00001148 .hint_size = 4,
1149 .owner = THIS_MODULE,
1150 .create = mq_create
1151};
1152
1153static int __init mq_init(void)
1154{
1155 int r;
1156
1157 mq_entry_cache = kmem_cache_create("dm_mq_policy_cache_entry",
1158 sizeof(struct entry),
1159 __alignof__(struct entry),
1160 0, NULL);
1161 if (!mq_entry_cache)
1162 goto bad;
1163
1164 r = dm_cache_policy_register(&mq_policy_type);
1165 if (r) {
1166 DMERR("register failed %d", r);
1167 goto bad_register_mq;
1168 }
1169
1170 r = dm_cache_policy_register(&default_policy_type);
1171 if (!r) {
Mike Snitzer4e7f5062013-03-20 17:21:27 +00001172 DMINFO("version %u.%u.%u loaded",
1173 mq_policy_type.version[0],
1174 mq_policy_type.version[1],
1175 mq_policy_type.version[2]);
Joe Thornberf2836352013-03-01 22:45:51 +00001176 return 0;
1177 }
1178
1179 DMERR("register failed (as default) %d", r);
1180
1181 dm_cache_policy_unregister(&mq_policy_type);
1182bad_register_mq:
1183 kmem_cache_destroy(mq_entry_cache);
1184bad:
1185 return -ENOMEM;
1186}
1187
1188static void __exit mq_exit(void)
1189{
1190 dm_cache_policy_unregister(&mq_policy_type);
1191 dm_cache_policy_unregister(&default_policy_type);
1192
1193 kmem_cache_destroy(mq_entry_cache);
1194}
1195
1196module_init(mq_init);
1197module_exit(mq_exit);
1198
1199MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
1200MODULE_LICENSE("GPL");
1201MODULE_DESCRIPTION("mq cache policy");
1202
1203MODULE_ALIAS("dm-cache-default");