dm-cache-policy-mq.c 30 KB

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