dm-cache-policy-mq.c 33 KB

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