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