dm-cache-policy-mq.c 28 KB

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