dm-cache-policy-mq.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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,
  572. struct policy_locker *locker, dm_oblock_t *oblock)
  573. {
  574. struct entry *demoted = peek(&mq->cache_clean);
  575. if (!demoted)
  576. /*
  577. * We could get a block from mq->cache_dirty, but that
  578. * would add extra latency to the triggering bio as it
  579. * waits for the writeback. Better to not promote this
  580. * time and hope there's a clean block next time this block
  581. * is hit.
  582. */
  583. return -ENOSPC;
  584. if (locker->fn(locker, demoted->oblock))
  585. /*
  586. * We couldn't lock the demoted block.
  587. */
  588. return -EBUSY;
  589. del(mq, demoted);
  590. *oblock = demoted->oblock;
  591. free_entry(&mq->cache_pool, demoted);
  592. /*
  593. * We used to put the demoted block into the pre-cache, but I think
  594. * it's simpler to just let it work it's way up from zero again.
  595. * Stops blocks flickering in and out of the cache.
  596. */
  597. return 0;
  598. }
  599. /*
  600. * Entries in the pre_cache whose hit count passes the promotion
  601. * threshold move to the cache proper. Working out the correct
  602. * value for the promotion_threshold is crucial to this policy.
  603. */
  604. static unsigned promote_threshold(struct mq_policy *mq)
  605. {
  606. struct entry *e;
  607. if (any_free_cblocks(mq))
  608. return 0;
  609. e = peek(&mq->cache_clean);
  610. if (e)
  611. return e->hit_count;
  612. e = peek(&mq->cache_dirty);
  613. if (e)
  614. return e->hit_count + DISCOURAGE_DEMOTING_DIRTY_THRESHOLD;
  615. /* This should never happen */
  616. return 0;
  617. }
  618. /*
  619. * We modify the basic promotion_threshold depending on the specific io.
  620. *
  621. * If the origin block has been discarded then there's no cost to copy it
  622. * to the cache.
  623. *
  624. * We bias towards reads, since they can be demoted at no cost if they
  625. * haven't been dirtied.
  626. */
  627. static unsigned adjusted_promote_threshold(struct mq_policy *mq,
  628. bool discarded_oblock, int data_dir)
  629. {
  630. if (data_dir == READ)
  631. return promote_threshold(mq) + mq->read_promote_adjustment;
  632. if (discarded_oblock && (any_free_cblocks(mq) || any_clean_cblocks(mq))) {
  633. /*
  634. * We don't need to do any copying at all, so give this a
  635. * very low threshold.
  636. */
  637. return mq->discard_promote_adjustment;
  638. }
  639. return promote_threshold(mq) + mq->write_promote_adjustment;
  640. }
  641. static bool should_promote(struct mq_policy *mq, struct entry *e,
  642. bool discarded_oblock, int data_dir)
  643. {
  644. return e->hit_count >=
  645. adjusted_promote_threshold(mq, discarded_oblock, data_dir);
  646. }
  647. static int cache_entry_found(struct mq_policy *mq,
  648. struct entry *e,
  649. struct policy_result *result)
  650. {
  651. requeue(mq, e);
  652. if (in_cache(mq, e)) {
  653. result->op = POLICY_HIT;
  654. result->cblock = infer_cblock(&mq->cache_pool, e);
  655. }
  656. return 0;
  657. }
  658. /*
  659. * Moves an entry from the pre_cache to the cache. The main work is
  660. * finding which cache block to use.
  661. */
  662. static int pre_cache_to_cache(struct mq_policy *mq, struct entry *e,
  663. struct policy_locker *locker,
  664. struct policy_result *result)
  665. {
  666. int r;
  667. struct entry *new_e;
  668. /* Ensure there's a free cblock in the cache */
  669. if (epool_empty(&mq->cache_pool)) {
  670. result->op = POLICY_REPLACE;
  671. r = demote_cblock(mq, locker, &result->old_oblock);
  672. if (r) {
  673. result->op = POLICY_MISS;
  674. return 0;
  675. }
  676. } else
  677. result->op = POLICY_NEW;
  678. new_e = alloc_entry(&mq->cache_pool);
  679. BUG_ON(!new_e);
  680. new_e->oblock = e->oblock;
  681. new_e->dirty = false;
  682. new_e->hit_count = e->hit_count;
  683. del(mq, e);
  684. free_entry(&mq->pre_cache_pool, e);
  685. push(mq, new_e);
  686. result->cblock = infer_cblock(&mq->cache_pool, new_e);
  687. return 0;
  688. }
  689. static int pre_cache_entry_found(struct mq_policy *mq, struct entry *e,
  690. bool can_migrate, bool discarded_oblock,
  691. int data_dir, struct policy_locker *locker,
  692. struct policy_result *result)
  693. {
  694. int r = 0;
  695. if (!should_promote(mq, e, discarded_oblock, data_dir)) {
  696. requeue(mq, e);
  697. result->op = POLICY_MISS;
  698. } else if (!can_migrate)
  699. r = -EWOULDBLOCK;
  700. else {
  701. requeue(mq, e);
  702. r = pre_cache_to_cache(mq, e, locker, result);
  703. }
  704. return r;
  705. }
  706. static void insert_in_pre_cache(struct mq_policy *mq,
  707. dm_oblock_t oblock)
  708. {
  709. struct entry *e = alloc_entry(&mq->pre_cache_pool);
  710. if (!e)
  711. /*
  712. * There's no spare entry structure, so we grab the least
  713. * used one from the pre_cache.
  714. */
  715. e = pop(mq, &mq->pre_cache);
  716. if (unlikely(!e)) {
  717. DMWARN("couldn't pop from pre cache");
  718. return;
  719. }
  720. e->dirty = false;
  721. e->oblock = oblock;
  722. e->hit_count = 1;
  723. push(mq, e);
  724. }
  725. static void insert_in_cache(struct mq_policy *mq, dm_oblock_t oblock,
  726. struct policy_locker *locker,
  727. struct policy_result *result)
  728. {
  729. int r;
  730. struct entry *e;
  731. if (epool_empty(&mq->cache_pool)) {
  732. result->op = POLICY_REPLACE;
  733. r = demote_cblock(mq, locker, &result->old_oblock);
  734. if (unlikely(r)) {
  735. result->op = POLICY_MISS;
  736. insert_in_pre_cache(mq, oblock);
  737. return;
  738. }
  739. /*
  740. * This will always succeed, since we've just demoted.
  741. */
  742. e = alloc_entry(&mq->cache_pool);
  743. BUG_ON(!e);
  744. } else {
  745. e = alloc_entry(&mq->cache_pool);
  746. result->op = POLICY_NEW;
  747. }
  748. e->oblock = oblock;
  749. e->dirty = false;
  750. e->hit_count = 1;
  751. push(mq, e);
  752. result->cblock = infer_cblock(&mq->cache_pool, e);
  753. }
  754. static int no_entry_found(struct mq_policy *mq, dm_oblock_t oblock,
  755. bool can_migrate, bool discarded_oblock,
  756. int data_dir, struct policy_locker *locker,
  757. struct policy_result *result)
  758. {
  759. if (adjusted_promote_threshold(mq, discarded_oblock, data_dir) <= 1) {
  760. if (can_migrate)
  761. insert_in_cache(mq, oblock, locker, result);
  762. else
  763. return -EWOULDBLOCK;
  764. } else {
  765. insert_in_pre_cache(mq, oblock);
  766. result->op = POLICY_MISS;
  767. }
  768. return 0;
  769. }
  770. /*
  771. * Looks the oblock up in the hash table, then decides whether to put in
  772. * pre_cache, or cache etc.
  773. */
  774. static int map(struct mq_policy *mq, dm_oblock_t oblock,
  775. bool can_migrate, bool discarded_oblock,
  776. int data_dir, struct policy_locker *locker,
  777. struct policy_result *result)
  778. {
  779. int r = 0;
  780. struct entry *e = hash_lookup(mq, oblock);
  781. if (e && in_cache(mq, e))
  782. r = cache_entry_found(mq, e, result);
  783. else if (mq->tracker.thresholds[PATTERN_SEQUENTIAL] &&
  784. iot_pattern(&mq->tracker) == PATTERN_SEQUENTIAL)
  785. result->op = POLICY_MISS;
  786. else if (e)
  787. r = pre_cache_entry_found(mq, e, can_migrate, discarded_oblock,
  788. data_dir, locker, result);
  789. else
  790. r = no_entry_found(mq, oblock, can_migrate, discarded_oblock,
  791. data_dir, locker, result);
  792. if (r == -EWOULDBLOCK)
  793. result->op = POLICY_MISS;
  794. return r;
  795. }
  796. /*----------------------------------------------------------------*/
  797. /*
  798. * Public interface, via the policy struct. See dm-cache-policy.h for a
  799. * description of these.
  800. */
  801. static struct mq_policy *to_mq_policy(struct dm_cache_policy *p)
  802. {
  803. return container_of(p, struct mq_policy, policy);
  804. }
  805. static void mq_destroy(struct dm_cache_policy *p)
  806. {
  807. struct mq_policy *mq = to_mq_policy(p);
  808. vfree(mq->table);
  809. epool_exit(&mq->cache_pool);
  810. epool_exit(&mq->pre_cache_pool);
  811. kfree(mq);
  812. }
  813. static void update_pre_cache_hits(struct list_head *h, void *context)
  814. {
  815. struct entry *e = container_of(h, struct entry, list);
  816. e->hit_count++;
  817. }
  818. static void update_cache_hits(struct list_head *h, void *context)
  819. {
  820. struct mq_policy *mq = context;
  821. struct entry *e = container_of(h, struct entry, list);
  822. e->hit_count++;
  823. mq->hit_count++;
  824. }
  825. static void copy_tick(struct mq_policy *mq)
  826. {
  827. unsigned long flags, tick;
  828. spin_lock_irqsave(&mq->tick_lock, flags);
  829. tick = mq->tick_protected;
  830. if (tick != mq->tick) {
  831. queue_iterate_tick(&mq->pre_cache, update_pre_cache_hits, mq);
  832. queue_iterate_tick(&mq->cache_dirty, update_cache_hits, mq);
  833. queue_iterate_tick(&mq->cache_clean, update_cache_hits, mq);
  834. mq->tick = tick;
  835. }
  836. queue_tick(&mq->pre_cache);
  837. queue_tick(&mq->cache_dirty);
  838. queue_tick(&mq->cache_clean);
  839. queue_update_writeback_sentinels(&mq->cache_dirty);
  840. spin_unlock_irqrestore(&mq->tick_lock, flags);
  841. }
  842. static int mq_map(struct dm_cache_policy *p, dm_oblock_t oblock,
  843. bool can_block, bool can_migrate, bool discarded_oblock,
  844. struct bio *bio, struct policy_locker *locker,
  845. struct policy_result *result)
  846. {
  847. int r;
  848. struct mq_policy *mq = to_mq_policy(p);
  849. result->op = POLICY_MISS;
  850. if (can_block)
  851. mutex_lock(&mq->lock);
  852. else if (!mutex_trylock(&mq->lock))
  853. return -EWOULDBLOCK;
  854. copy_tick(mq);
  855. iot_examine_bio(&mq->tracker, bio);
  856. r = map(mq, oblock, can_migrate, discarded_oblock,
  857. bio_data_dir(bio), locker, result);
  858. mutex_unlock(&mq->lock);
  859. return r;
  860. }
  861. static int mq_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock)
  862. {
  863. int r;
  864. struct mq_policy *mq = to_mq_policy(p);
  865. struct entry *e;
  866. if (!mutex_trylock(&mq->lock))
  867. return -EWOULDBLOCK;
  868. e = hash_lookup(mq, oblock);
  869. if (e && in_cache(mq, e)) {
  870. *cblock = infer_cblock(&mq->cache_pool, e);
  871. r = 0;
  872. } else
  873. r = -ENOENT;
  874. mutex_unlock(&mq->lock);
  875. return r;
  876. }
  877. static void __mq_set_clear_dirty(struct mq_policy *mq, dm_oblock_t oblock, bool set)
  878. {
  879. struct entry *e;
  880. e = hash_lookup(mq, oblock);
  881. BUG_ON(!e || !in_cache(mq, e));
  882. del(mq, e);
  883. e->dirty = set;
  884. push(mq, e);
  885. }
  886. static void mq_set_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
  887. {
  888. struct mq_policy *mq = to_mq_policy(p);
  889. mutex_lock(&mq->lock);
  890. __mq_set_clear_dirty(mq, oblock, true);
  891. mutex_unlock(&mq->lock);
  892. }
  893. static void mq_clear_dirty(struct dm_cache_policy *p, dm_oblock_t oblock)
  894. {
  895. struct mq_policy *mq = to_mq_policy(p);
  896. mutex_lock(&mq->lock);
  897. __mq_set_clear_dirty(mq, oblock, false);
  898. mutex_unlock(&mq->lock);
  899. }
  900. static int mq_load_mapping(struct dm_cache_policy *p,
  901. dm_oblock_t oblock, dm_cblock_t cblock,
  902. uint32_t hint, bool hint_valid)
  903. {
  904. struct mq_policy *mq = to_mq_policy(p);
  905. struct entry *e;
  906. e = alloc_particular_entry(&mq->cache_pool, cblock);
  907. e->oblock = oblock;
  908. e->dirty = false; /* this gets corrected in a minute */
  909. e->hit_count = hint_valid ? hint : 1;
  910. push(mq, e);
  911. return 0;
  912. }
  913. static int mq_save_hints(struct mq_policy *mq, struct queue *q,
  914. policy_walk_fn fn, void *context)
  915. {
  916. int r;
  917. unsigned level;
  918. struct list_head *h;
  919. struct entry *e;
  920. for (level = 0; level < NR_QUEUE_LEVELS; level++)
  921. list_for_each(h, q->qs + level) {
  922. if (is_sentinel(q, h))
  923. continue;
  924. e = container_of(h, struct entry, list);
  925. r = fn(context, infer_cblock(&mq->cache_pool, e),
  926. e->oblock, e->hit_count);
  927. if (r)
  928. return r;
  929. }
  930. return 0;
  931. }
  932. static int mq_walk_mappings(struct dm_cache_policy *p, policy_walk_fn fn,
  933. void *context)
  934. {
  935. struct mq_policy *mq = to_mq_policy(p);
  936. int r = 0;
  937. mutex_lock(&mq->lock);
  938. r = mq_save_hints(mq, &mq->cache_clean, fn, context);
  939. if (!r)
  940. r = mq_save_hints(mq, &mq->cache_dirty, fn, context);
  941. mutex_unlock(&mq->lock);
  942. return r;
  943. }
  944. static void __remove_mapping(struct mq_policy *mq, dm_oblock_t oblock)
  945. {
  946. struct entry *e;
  947. e = hash_lookup(mq, oblock);
  948. BUG_ON(!e || !in_cache(mq, e));
  949. del(mq, e);
  950. free_entry(&mq->cache_pool, e);
  951. }
  952. static void mq_remove_mapping(struct dm_cache_policy *p, dm_oblock_t oblock)
  953. {
  954. struct mq_policy *mq = to_mq_policy(p);
  955. mutex_lock(&mq->lock);
  956. __remove_mapping(mq, oblock);
  957. mutex_unlock(&mq->lock);
  958. }
  959. static int __remove_cblock(struct mq_policy *mq, dm_cblock_t cblock)
  960. {
  961. struct entry *e = epool_find(&mq->cache_pool, cblock);
  962. if (!e)
  963. return -ENODATA;
  964. del(mq, e);
  965. free_entry(&mq->cache_pool, e);
  966. return 0;
  967. }
  968. static int mq_remove_cblock(struct dm_cache_policy *p, dm_cblock_t cblock)
  969. {
  970. int r;
  971. struct mq_policy *mq = to_mq_policy(p);
  972. mutex_lock(&mq->lock);
  973. r = __remove_cblock(mq, cblock);
  974. mutex_unlock(&mq->lock);
  975. return r;
  976. }
  977. #define CLEAN_TARGET_PERCENTAGE 25
  978. static bool clean_target_met(struct mq_policy *mq)
  979. {
  980. /*
  981. * Cache entries may not be populated. So we're cannot rely on the
  982. * size of the clean queue.
  983. */
  984. unsigned nr_clean = from_cblock(mq->cache_size) - queue_size(&mq->cache_dirty);
  985. unsigned target = from_cblock(mq->cache_size) * CLEAN_TARGET_PERCENTAGE / 100;
  986. return nr_clean >= target;
  987. }
  988. static int __mq_writeback_work(struct mq_policy *mq, dm_oblock_t *oblock,
  989. dm_cblock_t *cblock)
  990. {
  991. struct entry *e = pop_old(mq, &mq->cache_dirty);
  992. if (!e && !clean_target_met(mq))
  993. e = pop(mq, &mq->cache_dirty);
  994. if (!e)
  995. return -ENODATA;
  996. *oblock = e->oblock;
  997. *cblock = infer_cblock(&mq->cache_pool, e);
  998. e->dirty = false;
  999. push(mq, e);
  1000. return 0;
  1001. }
  1002. static int mq_writeback_work(struct dm_cache_policy *p, dm_oblock_t *oblock,
  1003. dm_cblock_t *cblock, bool critical_only)
  1004. {
  1005. int r;
  1006. struct mq_policy *mq = to_mq_policy(p);
  1007. mutex_lock(&mq->lock);
  1008. r = __mq_writeback_work(mq, oblock, cblock);
  1009. mutex_unlock(&mq->lock);
  1010. return r;
  1011. }
  1012. static void __force_mapping(struct mq_policy *mq,
  1013. dm_oblock_t current_oblock, dm_oblock_t new_oblock)
  1014. {
  1015. struct entry *e = hash_lookup(mq, current_oblock);
  1016. if (e && in_cache(mq, e)) {
  1017. del(mq, e);
  1018. e->oblock = new_oblock;
  1019. e->dirty = true;
  1020. push(mq, e);
  1021. }
  1022. }
  1023. static void mq_force_mapping(struct dm_cache_policy *p,
  1024. dm_oblock_t current_oblock, dm_oblock_t new_oblock)
  1025. {
  1026. struct mq_policy *mq = to_mq_policy(p);
  1027. mutex_lock(&mq->lock);
  1028. __force_mapping(mq, current_oblock, new_oblock);
  1029. mutex_unlock(&mq->lock);
  1030. }
  1031. static dm_cblock_t mq_residency(struct dm_cache_policy *p)
  1032. {
  1033. dm_cblock_t r;
  1034. struct mq_policy *mq = to_mq_policy(p);
  1035. mutex_lock(&mq->lock);
  1036. r = to_cblock(mq->cache_pool.nr_allocated);
  1037. mutex_unlock(&mq->lock);
  1038. return r;
  1039. }
  1040. static void mq_tick(struct dm_cache_policy *p, bool can_block)
  1041. {
  1042. struct mq_policy *mq = to_mq_policy(p);
  1043. unsigned long flags;
  1044. spin_lock_irqsave(&mq->tick_lock, flags);
  1045. mq->tick_protected++;
  1046. spin_unlock_irqrestore(&mq->tick_lock, flags);
  1047. if (can_block) {
  1048. mutex_lock(&mq->lock);
  1049. copy_tick(mq);
  1050. mutex_unlock(&mq->lock);
  1051. }
  1052. }
  1053. static int mq_set_config_value(struct dm_cache_policy *p,
  1054. const char *key, const char *value)
  1055. {
  1056. struct mq_policy *mq = to_mq_policy(p);
  1057. unsigned long tmp;
  1058. if (kstrtoul(value, 10, &tmp))
  1059. return -EINVAL;
  1060. if (!strcasecmp(key, "random_threshold")) {
  1061. mq->tracker.thresholds[PATTERN_RANDOM] = tmp;
  1062. } else if (!strcasecmp(key, "sequential_threshold")) {
  1063. mq->tracker.thresholds[PATTERN_SEQUENTIAL] = tmp;
  1064. } else if (!strcasecmp(key, "discard_promote_adjustment"))
  1065. mq->discard_promote_adjustment = tmp;
  1066. else if (!strcasecmp(key, "read_promote_adjustment"))
  1067. mq->read_promote_adjustment = tmp;
  1068. else if (!strcasecmp(key, "write_promote_adjustment"))
  1069. mq->write_promote_adjustment = tmp;
  1070. else
  1071. return -EINVAL;
  1072. return 0;
  1073. }
  1074. static int mq_emit_config_values(struct dm_cache_policy *p, char *result,
  1075. unsigned maxlen, ssize_t *sz_ptr)
  1076. {
  1077. ssize_t sz = *sz_ptr;
  1078. struct mq_policy *mq = to_mq_policy(p);
  1079. DMEMIT("10 random_threshold %u "
  1080. "sequential_threshold %u "
  1081. "discard_promote_adjustment %u "
  1082. "read_promote_adjustment %u "
  1083. "write_promote_adjustment %u ",
  1084. mq->tracker.thresholds[PATTERN_RANDOM],
  1085. mq->tracker.thresholds[PATTERN_SEQUENTIAL],
  1086. mq->discard_promote_adjustment,
  1087. mq->read_promote_adjustment,
  1088. mq->write_promote_adjustment);
  1089. *sz_ptr = sz;
  1090. return 0;
  1091. }
  1092. /* Init the policy plugin interface function pointers. */
  1093. static void init_policy_functions(struct mq_policy *mq)
  1094. {
  1095. mq->policy.destroy = mq_destroy;
  1096. mq->policy.map = mq_map;
  1097. mq->policy.lookup = mq_lookup;
  1098. mq->policy.set_dirty = mq_set_dirty;
  1099. mq->policy.clear_dirty = mq_clear_dirty;
  1100. mq->policy.load_mapping = mq_load_mapping;
  1101. mq->policy.walk_mappings = mq_walk_mappings;
  1102. mq->policy.remove_mapping = mq_remove_mapping;
  1103. mq->policy.remove_cblock = mq_remove_cblock;
  1104. mq->policy.writeback_work = mq_writeback_work;
  1105. mq->policy.force_mapping = mq_force_mapping;
  1106. mq->policy.residency = mq_residency;
  1107. mq->policy.tick = mq_tick;
  1108. mq->policy.emit_config_values = mq_emit_config_values;
  1109. mq->policy.set_config_value = mq_set_config_value;
  1110. }
  1111. static struct dm_cache_policy *mq_create(dm_cblock_t cache_size,
  1112. sector_t origin_size,
  1113. sector_t cache_block_size)
  1114. {
  1115. struct mq_policy *mq = kzalloc(sizeof(*mq), GFP_KERNEL);
  1116. if (!mq)
  1117. return NULL;
  1118. init_policy_functions(mq);
  1119. iot_init(&mq->tracker, SEQUENTIAL_THRESHOLD_DEFAULT, RANDOM_THRESHOLD_DEFAULT);
  1120. mq->cache_size = cache_size;
  1121. if (epool_init(&mq->pre_cache_pool, from_cblock(cache_size))) {
  1122. DMERR("couldn't initialize pool of pre-cache entries");
  1123. goto bad_pre_cache_init;
  1124. }
  1125. if (epool_init(&mq->cache_pool, from_cblock(cache_size))) {
  1126. DMERR("couldn't initialize pool of cache entries");
  1127. goto bad_cache_init;
  1128. }
  1129. mq->tick_protected = 0;
  1130. mq->tick = 0;
  1131. mq->hit_count = 0;
  1132. mq->generation = 0;
  1133. mq->discard_promote_adjustment = DEFAULT_DISCARD_PROMOTE_ADJUSTMENT;
  1134. mq->read_promote_adjustment = DEFAULT_READ_PROMOTE_ADJUSTMENT;
  1135. mq->write_promote_adjustment = DEFAULT_WRITE_PROMOTE_ADJUSTMENT;
  1136. mutex_init(&mq->lock);
  1137. spin_lock_init(&mq->tick_lock);
  1138. queue_init(&mq->pre_cache);
  1139. queue_init(&mq->cache_clean);
  1140. queue_init(&mq->cache_dirty);
  1141. mq->generation_period = max((unsigned) from_cblock(cache_size), 1024U);
  1142. mq->nr_buckets = next_power(from_cblock(cache_size) / 2, 16);
  1143. mq->hash_bits = __ffs(mq->nr_buckets);
  1144. mq->table = vzalloc(sizeof(*mq->table) * mq->nr_buckets);
  1145. if (!mq->table)
  1146. goto bad_alloc_table;
  1147. return &mq->policy;
  1148. bad_alloc_table:
  1149. epool_exit(&mq->cache_pool);
  1150. bad_cache_init:
  1151. epool_exit(&mq->pre_cache_pool);
  1152. bad_pre_cache_init:
  1153. kfree(mq);
  1154. return NULL;
  1155. }
  1156. /*----------------------------------------------------------------*/
  1157. static struct dm_cache_policy_type mq_policy_type = {
  1158. .name = "mq",
  1159. .version = {1, 4, 0},
  1160. .hint_size = 4,
  1161. .owner = THIS_MODULE,
  1162. .create = mq_create
  1163. };
  1164. static int __init mq_init(void)
  1165. {
  1166. int r;
  1167. mq_entry_cache = kmem_cache_create("dm_mq_policy_cache_entry",
  1168. sizeof(struct entry),
  1169. __alignof__(struct entry),
  1170. 0, NULL);
  1171. if (!mq_entry_cache)
  1172. return -ENOMEM;
  1173. r = dm_cache_policy_register(&mq_policy_type);
  1174. if (r) {
  1175. DMERR("register failed %d", r);
  1176. kmem_cache_destroy(mq_entry_cache);
  1177. return -ENOMEM;
  1178. }
  1179. return 0;
  1180. }
  1181. static void __exit mq_exit(void)
  1182. {
  1183. dm_cache_policy_unregister(&mq_policy_type);
  1184. kmem_cache_destroy(mq_entry_cache);
  1185. }
  1186. module_init(mq_init);
  1187. module_exit(mq_exit);
  1188. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1189. MODULE_LICENSE("GPL");
  1190. MODULE_DESCRIPTION("mq cache policy");