dm-cache-policy-smq.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Red Hat. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-cache-background-tracker.h"
  8. #include "dm-cache-policy-internal.h"
  9. #include "dm-cache-policy.h"
  10. #include "dm.h"
  11. #include <linux/hash.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/math64.h>
  17. #define DM_MSG_PREFIX "cache-policy-smq"
  18. /*----------------------------------------------------------------*/
  19. /*
  20. * Safe division functions that return zero on divide by zero.
  21. */
  22. static unsigned int safe_div(unsigned int n, unsigned int d)
  23. {
  24. return d ? n / d : 0u;
  25. }
  26. static unsigned int safe_mod(unsigned int n, unsigned int d)
  27. {
  28. return d ? n % d : 0u;
  29. }
  30. /*----------------------------------------------------------------*/
  31. struct entry {
  32. unsigned int hash_next:28;
  33. unsigned int prev:28;
  34. unsigned int next:28;
  35. unsigned int level:6;
  36. bool dirty:1;
  37. bool allocated:1;
  38. bool sentinel:1;
  39. bool pending_work:1;
  40. dm_oblock_t oblock;
  41. };
  42. /*----------------------------------------------------------------*/
  43. #define INDEXER_NULL ((1u << 28u) - 1u)
  44. /*
  45. * An entry_space manages a set of entries that we use for the queues.
  46. * The clean and dirty queues share entries, so this object is separate
  47. * from the queue itself.
  48. */
  49. struct entry_space {
  50. struct entry *begin;
  51. struct entry *end;
  52. };
  53. static int space_init(struct entry_space *es, unsigned int nr_entries)
  54. {
  55. if (!nr_entries) {
  56. es->begin = es->end = NULL;
  57. return 0;
  58. }
  59. es->begin = vzalloc(array_size(nr_entries, sizeof(struct entry)));
  60. if (!es->begin)
  61. return -ENOMEM;
  62. es->end = es->begin + nr_entries;
  63. return 0;
  64. }
  65. static void space_exit(struct entry_space *es)
  66. {
  67. vfree(es->begin);
  68. }
  69. static struct entry *__get_entry(struct entry_space *es, unsigned int block)
  70. {
  71. struct entry *e;
  72. e = es->begin + block;
  73. BUG_ON(e >= es->end);
  74. return e;
  75. }
  76. static unsigned int to_index(struct entry_space *es, struct entry *e)
  77. {
  78. BUG_ON(e < es->begin || e >= es->end);
  79. return e - es->begin;
  80. }
  81. static struct entry *to_entry(struct entry_space *es, unsigned int block)
  82. {
  83. if (block == INDEXER_NULL)
  84. return NULL;
  85. return __get_entry(es, block);
  86. }
  87. /*----------------------------------------------------------------*/
  88. struct ilist {
  89. unsigned int nr_elts; /* excluding sentinel entries */
  90. unsigned int head, tail;
  91. };
  92. static void l_init(struct ilist *l)
  93. {
  94. l->nr_elts = 0;
  95. l->head = l->tail = INDEXER_NULL;
  96. }
  97. static struct entry *l_head(struct entry_space *es, struct ilist *l)
  98. {
  99. return to_entry(es, l->head);
  100. }
  101. static struct entry *l_tail(struct entry_space *es, struct ilist *l)
  102. {
  103. return to_entry(es, l->tail);
  104. }
  105. static struct entry *l_next(struct entry_space *es, struct entry *e)
  106. {
  107. return to_entry(es, e->next);
  108. }
  109. static struct entry *l_prev(struct entry_space *es, struct entry *e)
  110. {
  111. return to_entry(es, e->prev);
  112. }
  113. static bool l_empty(struct ilist *l)
  114. {
  115. return l->head == INDEXER_NULL;
  116. }
  117. static void l_add_head(struct entry_space *es, struct ilist *l, struct entry *e)
  118. {
  119. struct entry *head = l_head(es, l);
  120. e->next = l->head;
  121. e->prev = INDEXER_NULL;
  122. if (head)
  123. head->prev = l->head = to_index(es, e);
  124. else
  125. l->head = l->tail = to_index(es, e);
  126. if (!e->sentinel)
  127. l->nr_elts++;
  128. }
  129. static void l_add_tail(struct entry_space *es, struct ilist *l, struct entry *e)
  130. {
  131. struct entry *tail = l_tail(es, l);
  132. e->next = INDEXER_NULL;
  133. e->prev = l->tail;
  134. if (tail)
  135. tail->next = l->tail = to_index(es, e);
  136. else
  137. l->head = l->tail = to_index(es, e);
  138. if (!e->sentinel)
  139. l->nr_elts++;
  140. }
  141. static void l_add_before(struct entry_space *es, struct ilist *l,
  142. struct entry *old, struct entry *e)
  143. {
  144. struct entry *prev = l_prev(es, old);
  145. if (!prev)
  146. l_add_head(es, l, e);
  147. else {
  148. e->prev = old->prev;
  149. e->next = to_index(es, old);
  150. prev->next = old->prev = to_index(es, e);
  151. if (!e->sentinel)
  152. l->nr_elts++;
  153. }
  154. }
  155. static void l_del(struct entry_space *es, struct ilist *l, struct entry *e)
  156. {
  157. struct entry *prev = l_prev(es, e);
  158. struct entry *next = l_next(es, e);
  159. if (prev)
  160. prev->next = e->next;
  161. else
  162. l->head = e->next;
  163. if (next)
  164. next->prev = e->prev;
  165. else
  166. l->tail = e->prev;
  167. if (!e->sentinel)
  168. l->nr_elts--;
  169. }
  170. static struct entry *l_pop_head(struct entry_space *es, struct ilist *l)
  171. {
  172. struct entry *e;
  173. for (e = l_head(es, l); e; e = l_next(es, e))
  174. if (!e->sentinel) {
  175. l_del(es, l, e);
  176. return e;
  177. }
  178. return NULL;
  179. }
  180. static struct entry *l_pop_tail(struct entry_space *es, struct ilist *l)
  181. {
  182. struct entry *e;
  183. for (e = l_tail(es, l); e; e = l_prev(es, e))
  184. if (!e->sentinel) {
  185. l_del(es, l, e);
  186. return e;
  187. }
  188. return NULL;
  189. }
  190. /*----------------------------------------------------------------*/
  191. /*
  192. * The stochastic-multi-queue is a set of lru lists stacked into levels.
  193. * Entries are moved up levels when they are used, which loosely orders the
  194. * most accessed entries in the top levels and least in the bottom. This
  195. * structure is *much* better than a single lru list.
  196. */
  197. #define MAX_LEVELS 64u
  198. struct queue {
  199. struct entry_space *es;
  200. unsigned int nr_elts;
  201. unsigned int nr_levels;
  202. struct ilist qs[MAX_LEVELS];
  203. /*
  204. * We maintain a count of the number of entries we would like in each
  205. * level.
  206. */
  207. unsigned int last_target_nr_elts;
  208. unsigned int nr_top_levels;
  209. unsigned int nr_in_top_levels;
  210. unsigned int target_count[MAX_LEVELS];
  211. };
  212. static void q_init(struct queue *q, struct entry_space *es, unsigned int nr_levels)
  213. {
  214. unsigned int i;
  215. q->es = es;
  216. q->nr_elts = 0;
  217. q->nr_levels = nr_levels;
  218. for (i = 0; i < q->nr_levels; i++) {
  219. l_init(q->qs + i);
  220. q->target_count[i] = 0u;
  221. }
  222. q->last_target_nr_elts = 0u;
  223. q->nr_top_levels = 0u;
  224. q->nr_in_top_levels = 0u;
  225. }
  226. static unsigned int q_size(struct queue *q)
  227. {
  228. return q->nr_elts;
  229. }
  230. /*
  231. * Insert an entry to the back of the given level.
  232. */
  233. static void q_push(struct queue *q, struct entry *e)
  234. {
  235. BUG_ON(e->pending_work);
  236. if (!e->sentinel)
  237. q->nr_elts++;
  238. l_add_tail(q->es, q->qs + e->level, e);
  239. }
  240. static void q_push_front(struct queue *q, struct entry *e)
  241. {
  242. BUG_ON(e->pending_work);
  243. if (!e->sentinel)
  244. q->nr_elts++;
  245. l_add_head(q->es, q->qs + e->level, e);
  246. }
  247. static void q_push_before(struct queue *q, struct entry *old, struct entry *e)
  248. {
  249. BUG_ON(e->pending_work);
  250. if (!e->sentinel)
  251. q->nr_elts++;
  252. l_add_before(q->es, q->qs + e->level, old, e);
  253. }
  254. static void q_del(struct queue *q, struct entry *e)
  255. {
  256. l_del(q->es, q->qs + e->level, e);
  257. if (!e->sentinel)
  258. q->nr_elts--;
  259. }
  260. /*
  261. * Return the oldest entry of the lowest populated level.
  262. */
  263. static struct entry *q_peek(struct queue *q, unsigned int max_level, bool can_cross_sentinel)
  264. {
  265. unsigned int level;
  266. struct entry *e;
  267. max_level = min(max_level, q->nr_levels);
  268. for (level = 0; level < max_level; level++)
  269. for (e = l_head(q->es, q->qs + level); e; e = l_next(q->es, e)) {
  270. if (e->sentinel) {
  271. if (can_cross_sentinel)
  272. continue;
  273. else
  274. break;
  275. }
  276. return e;
  277. }
  278. return NULL;
  279. }
  280. static struct entry *q_pop(struct queue *q)
  281. {
  282. struct entry *e = q_peek(q, q->nr_levels, true);
  283. if (e)
  284. q_del(q, e);
  285. return e;
  286. }
  287. /*
  288. * This function assumes there is a non-sentinel entry to pop. It's only
  289. * used by redistribute, so we know this is true. It also doesn't adjust
  290. * the q->nr_elts count.
  291. */
  292. static struct entry *__redist_pop_from(struct queue *q, unsigned int level)
  293. {
  294. struct entry *e;
  295. for (; level < q->nr_levels; level++)
  296. for (e = l_head(q->es, q->qs + level); e; e = l_next(q->es, e))
  297. if (!e->sentinel) {
  298. l_del(q->es, q->qs + e->level, e);
  299. return e;
  300. }
  301. return NULL;
  302. }
  303. static void q_set_targets_subrange_(struct queue *q, unsigned int nr_elts,
  304. unsigned int lbegin, unsigned int lend)
  305. {
  306. unsigned int level, nr_levels, entries_per_level, remainder;
  307. BUG_ON(lbegin > lend);
  308. BUG_ON(lend > q->nr_levels);
  309. nr_levels = lend - lbegin;
  310. entries_per_level = safe_div(nr_elts, nr_levels);
  311. remainder = safe_mod(nr_elts, nr_levels);
  312. for (level = lbegin; level < lend; level++)
  313. q->target_count[level] =
  314. (level < (lbegin + remainder)) ? entries_per_level + 1u : entries_per_level;
  315. }
  316. /*
  317. * Typically we have fewer elements in the top few levels which allows us
  318. * to adjust the promote threshold nicely.
  319. */
  320. static void q_set_targets(struct queue *q)
  321. {
  322. if (q->last_target_nr_elts == q->nr_elts)
  323. return;
  324. q->last_target_nr_elts = q->nr_elts;
  325. if (q->nr_top_levels > q->nr_levels)
  326. q_set_targets_subrange_(q, q->nr_elts, 0, q->nr_levels);
  327. else {
  328. q_set_targets_subrange_(q, q->nr_in_top_levels,
  329. q->nr_levels - q->nr_top_levels, q->nr_levels);
  330. if (q->nr_in_top_levels < q->nr_elts)
  331. q_set_targets_subrange_(q, q->nr_elts - q->nr_in_top_levels,
  332. 0, q->nr_levels - q->nr_top_levels);
  333. else
  334. q_set_targets_subrange_(q, 0, 0, q->nr_levels - q->nr_top_levels);
  335. }
  336. }
  337. static void q_redistribute(struct queue *q)
  338. {
  339. unsigned int target, level;
  340. struct ilist *l, *l_above;
  341. struct entry *e;
  342. q_set_targets(q);
  343. for (level = 0u; level < q->nr_levels - 1u; level++) {
  344. l = q->qs + level;
  345. target = q->target_count[level];
  346. /*
  347. * Pull down some entries from the level above.
  348. */
  349. while (l->nr_elts < target) {
  350. e = __redist_pop_from(q, level + 1u);
  351. if (!e) {
  352. /* bug in nr_elts */
  353. break;
  354. }
  355. e->level = level;
  356. l_add_tail(q->es, l, e);
  357. }
  358. /*
  359. * Push some entries up.
  360. */
  361. l_above = q->qs + level + 1u;
  362. while (l->nr_elts > target) {
  363. e = l_pop_tail(q->es, l);
  364. if (!e)
  365. /* bug in nr_elts */
  366. break;
  367. e->level = level + 1u;
  368. l_add_tail(q->es, l_above, e);
  369. }
  370. }
  371. }
  372. static void q_requeue(struct queue *q, struct entry *e, unsigned int extra_levels,
  373. struct entry *s1, struct entry *s2)
  374. {
  375. struct entry *de;
  376. unsigned int sentinels_passed = 0;
  377. unsigned int new_level = min(q->nr_levels - 1u, e->level + extra_levels);
  378. /* try and find an entry to swap with */
  379. if (extra_levels && (e->level < q->nr_levels - 1u)) {
  380. for (de = l_head(q->es, q->qs + new_level); de && de->sentinel; de = l_next(q->es, de))
  381. sentinels_passed++;
  382. if (de) {
  383. q_del(q, de);
  384. de->level = e->level;
  385. if (s1) {
  386. switch (sentinels_passed) {
  387. case 0:
  388. q_push_before(q, s1, de);
  389. break;
  390. case 1:
  391. q_push_before(q, s2, de);
  392. break;
  393. default:
  394. q_push(q, de);
  395. }
  396. } else
  397. q_push(q, de);
  398. }
  399. }
  400. q_del(q, e);
  401. e->level = new_level;
  402. q_push(q, e);
  403. }
  404. /*----------------------------------------------------------------*/
  405. #define FP_SHIFT 8
  406. #define SIXTEENTH (1u << (FP_SHIFT - 4u))
  407. #define EIGHTH (1u << (FP_SHIFT - 3u))
  408. struct stats {
  409. unsigned int hit_threshold;
  410. unsigned int hits;
  411. unsigned int misses;
  412. };
  413. enum performance {
  414. Q_POOR,
  415. Q_FAIR,
  416. Q_WELL
  417. };
  418. static void stats_init(struct stats *s, unsigned int nr_levels)
  419. {
  420. s->hit_threshold = (nr_levels * 3u) / 4u;
  421. s->hits = 0u;
  422. s->misses = 0u;
  423. }
  424. static void stats_reset(struct stats *s)
  425. {
  426. s->hits = s->misses = 0u;
  427. }
  428. static void stats_level_accessed(struct stats *s, unsigned int level)
  429. {
  430. if (level >= s->hit_threshold)
  431. s->hits++;
  432. else
  433. s->misses++;
  434. }
  435. static void stats_miss(struct stats *s)
  436. {
  437. s->misses++;
  438. }
  439. /*
  440. * There are times when we don't have any confidence in the hotspot queue.
  441. * Such as when a fresh cache is created and the blocks have been spread
  442. * out across the levels, or if an io load changes. We detect this by
  443. * seeing how often a lookup is in the top levels of the hotspot queue.
  444. */
  445. static enum performance stats_assess(struct stats *s)
  446. {
  447. unsigned int confidence = safe_div(s->hits << FP_SHIFT, s->hits + s->misses);
  448. if (confidence < SIXTEENTH)
  449. return Q_POOR;
  450. else if (confidence < EIGHTH)
  451. return Q_FAIR;
  452. else
  453. return Q_WELL;
  454. }
  455. /*----------------------------------------------------------------*/
  456. struct smq_hash_table {
  457. struct entry_space *es;
  458. unsigned long long hash_bits;
  459. unsigned int *buckets;
  460. };
  461. /*
  462. * All cache entries are stored in a chained hash table. To save space we
  463. * use indexing again, and only store indexes to the next entry.
  464. */
  465. static int h_init(struct smq_hash_table *ht, struct entry_space *es, unsigned int nr_entries)
  466. {
  467. unsigned int i, nr_buckets;
  468. ht->es = es;
  469. nr_buckets = roundup_pow_of_two(max(nr_entries / 4u, 16u));
  470. ht->hash_bits = __ffs(nr_buckets);
  471. ht->buckets = vmalloc(array_size(nr_buckets, sizeof(*ht->buckets)));
  472. if (!ht->buckets)
  473. return -ENOMEM;
  474. for (i = 0; i < nr_buckets; i++)
  475. ht->buckets[i] = INDEXER_NULL;
  476. return 0;
  477. }
  478. static void h_exit(struct smq_hash_table *ht)
  479. {
  480. vfree(ht->buckets);
  481. }
  482. static struct entry *h_head(struct smq_hash_table *ht, unsigned int bucket)
  483. {
  484. return to_entry(ht->es, ht->buckets[bucket]);
  485. }
  486. static struct entry *h_next(struct smq_hash_table *ht, struct entry *e)
  487. {
  488. return to_entry(ht->es, e->hash_next);
  489. }
  490. static void __h_insert(struct smq_hash_table *ht, unsigned int bucket, struct entry *e)
  491. {
  492. e->hash_next = ht->buckets[bucket];
  493. ht->buckets[bucket] = to_index(ht->es, e);
  494. }
  495. static void h_insert(struct smq_hash_table *ht, struct entry *e)
  496. {
  497. unsigned int h = hash_64(from_oblock(e->oblock), ht->hash_bits);
  498. __h_insert(ht, h, e);
  499. }
  500. static struct entry *__h_lookup(struct smq_hash_table *ht, unsigned int h, dm_oblock_t oblock,
  501. struct entry **prev)
  502. {
  503. struct entry *e;
  504. *prev = NULL;
  505. for (e = h_head(ht, h); e; e = h_next(ht, e)) {
  506. if (e->oblock == oblock)
  507. return e;
  508. *prev = e;
  509. }
  510. return NULL;
  511. }
  512. static void __h_unlink(struct smq_hash_table *ht, unsigned int h,
  513. struct entry *e, struct entry *prev)
  514. {
  515. if (prev)
  516. prev->hash_next = e->hash_next;
  517. else
  518. ht->buckets[h] = e->hash_next;
  519. }
  520. /*
  521. * Also moves each entry to the front of the bucket.
  522. */
  523. static struct entry *h_lookup(struct smq_hash_table *ht, dm_oblock_t oblock)
  524. {
  525. struct entry *e, *prev;
  526. unsigned int h = hash_64(from_oblock(oblock), ht->hash_bits);
  527. e = __h_lookup(ht, h, oblock, &prev);
  528. if (e && prev) {
  529. /*
  530. * Move to the front because this entry is likely
  531. * to be hit again.
  532. */
  533. __h_unlink(ht, h, e, prev);
  534. __h_insert(ht, h, e);
  535. }
  536. return e;
  537. }
  538. static void h_remove(struct smq_hash_table *ht, struct entry *e)
  539. {
  540. unsigned int h = hash_64(from_oblock(e->oblock), ht->hash_bits);
  541. struct entry *prev;
  542. /*
  543. * The down side of using a singly linked list is we have to
  544. * iterate the bucket to remove an item.
  545. */
  546. e = __h_lookup(ht, h, e->oblock, &prev);
  547. if (e)
  548. __h_unlink(ht, h, e, prev);
  549. }
  550. /*----------------------------------------------------------------*/
  551. struct entry_alloc {
  552. struct entry_space *es;
  553. unsigned int begin;
  554. unsigned int nr_allocated;
  555. struct ilist free;
  556. };
  557. static void init_allocator(struct entry_alloc *ea, struct entry_space *es,
  558. unsigned int begin, unsigned int end)
  559. {
  560. unsigned int i;
  561. ea->es = es;
  562. ea->nr_allocated = 0u;
  563. ea->begin = begin;
  564. l_init(&ea->free);
  565. for (i = begin; i != end; i++)
  566. l_add_tail(ea->es, &ea->free, __get_entry(ea->es, i));
  567. }
  568. static void init_entry(struct entry *e)
  569. {
  570. /*
  571. * We can't memset because that would clear the hotspot and
  572. * sentinel bits which remain constant.
  573. */
  574. e->hash_next = INDEXER_NULL;
  575. e->next = INDEXER_NULL;
  576. e->prev = INDEXER_NULL;
  577. e->level = 0u;
  578. e->dirty = true; /* FIXME: audit */
  579. e->allocated = true;
  580. e->sentinel = false;
  581. e->pending_work = false;
  582. }
  583. static struct entry *alloc_entry(struct entry_alloc *ea)
  584. {
  585. struct entry *e;
  586. if (l_empty(&ea->free))
  587. return NULL;
  588. e = l_pop_head(ea->es, &ea->free);
  589. init_entry(e);
  590. ea->nr_allocated++;
  591. return e;
  592. }
  593. /*
  594. * This assumes the cblock hasn't already been allocated.
  595. */
  596. static struct entry *alloc_particular_entry(struct entry_alloc *ea, unsigned int i)
  597. {
  598. struct entry *e = __get_entry(ea->es, ea->begin + i);
  599. BUG_ON(e->allocated);
  600. l_del(ea->es, &ea->free, e);
  601. init_entry(e);
  602. ea->nr_allocated++;
  603. return e;
  604. }
  605. static void free_entry(struct entry_alloc *ea, struct entry *e)
  606. {
  607. BUG_ON(!ea->nr_allocated);
  608. BUG_ON(!e->allocated);
  609. ea->nr_allocated--;
  610. e->allocated = false;
  611. l_add_tail(ea->es, &ea->free, e);
  612. }
  613. static bool allocator_empty(struct entry_alloc *ea)
  614. {
  615. return l_empty(&ea->free);
  616. }
  617. static unsigned int get_index(struct entry_alloc *ea, struct entry *e)
  618. {
  619. return to_index(ea->es, e) - ea->begin;
  620. }
  621. static struct entry *get_entry(struct entry_alloc *ea, unsigned int index)
  622. {
  623. return __get_entry(ea->es, ea->begin + index);
  624. }
  625. /*----------------------------------------------------------------*/
  626. #define NR_HOTSPOT_LEVELS 64u
  627. #define NR_CACHE_LEVELS 64u
  628. #define WRITEBACK_PERIOD (10ul * HZ)
  629. #define DEMOTE_PERIOD (60ul * HZ)
  630. #define HOTSPOT_UPDATE_PERIOD (HZ)
  631. #define CACHE_UPDATE_PERIOD (60ul * HZ)
  632. struct smq_policy {
  633. struct dm_cache_policy policy;
  634. /* protects everything */
  635. spinlock_t lock;
  636. dm_cblock_t cache_size;
  637. sector_t cache_block_size;
  638. sector_t hotspot_block_size;
  639. unsigned int nr_hotspot_blocks;
  640. unsigned int cache_blocks_per_hotspot_block;
  641. unsigned int hotspot_level_jump;
  642. struct entry_space es;
  643. struct entry_alloc writeback_sentinel_alloc;
  644. struct entry_alloc demote_sentinel_alloc;
  645. struct entry_alloc hotspot_alloc;
  646. struct entry_alloc cache_alloc;
  647. unsigned long *hotspot_hit_bits;
  648. unsigned long *cache_hit_bits;
  649. /*
  650. * We maintain three queues of entries. The cache proper,
  651. * consisting of a clean and dirty queue, containing the currently
  652. * active mappings. The hotspot queue uses a larger block size to
  653. * track blocks that are being hit frequently and potential
  654. * candidates for promotion to the cache.
  655. */
  656. struct queue hotspot;
  657. struct queue clean;
  658. struct queue dirty;
  659. struct stats hotspot_stats;
  660. struct stats cache_stats;
  661. /*
  662. * Keeps track of time, incremented by the core. We use this to
  663. * avoid attributing multiple hits within the same tick.
  664. */
  665. unsigned int tick;
  666. /*
  667. * The hash tables allows us to quickly find an entry by origin
  668. * block.
  669. */
  670. struct smq_hash_table table;
  671. struct smq_hash_table hotspot_table;
  672. bool current_writeback_sentinels;
  673. unsigned long next_writeback_period;
  674. bool current_demote_sentinels;
  675. unsigned long next_demote_period;
  676. unsigned int write_promote_level;
  677. unsigned int read_promote_level;
  678. unsigned long next_hotspot_period;
  679. unsigned long next_cache_period;
  680. struct background_tracker *bg_work;
  681. bool migrations_allowed:1;
  682. /*
  683. * If this is set the policy will try and clean the whole cache
  684. * even if the device is not idle.
  685. */
  686. bool cleaner:1;
  687. };
  688. /*----------------------------------------------------------------*/
  689. static struct entry *get_sentinel(struct entry_alloc *ea, unsigned int level, bool which)
  690. {
  691. return get_entry(ea, which ? level : NR_CACHE_LEVELS + level);
  692. }
  693. static struct entry *writeback_sentinel(struct smq_policy *mq, unsigned int level)
  694. {
  695. return get_sentinel(&mq->writeback_sentinel_alloc, level, mq->current_writeback_sentinels);
  696. }
  697. static struct entry *demote_sentinel(struct smq_policy *mq, unsigned int level)
  698. {
  699. return get_sentinel(&mq->demote_sentinel_alloc, level, mq->current_demote_sentinels);
  700. }
  701. static void __update_writeback_sentinels(struct smq_policy *mq)
  702. {
  703. unsigned int level;
  704. struct queue *q = &mq->dirty;
  705. struct entry *sentinel;
  706. for (level = 0; level < q->nr_levels; level++) {
  707. sentinel = writeback_sentinel(mq, level);
  708. q_del(q, sentinel);
  709. q_push(q, sentinel);
  710. }
  711. }
  712. static void __update_demote_sentinels(struct smq_policy *mq)
  713. {
  714. unsigned int level;
  715. struct queue *q = &mq->clean;
  716. struct entry *sentinel;
  717. for (level = 0; level < q->nr_levels; level++) {
  718. sentinel = demote_sentinel(mq, level);
  719. q_del(q, sentinel);
  720. q_push(q, sentinel);
  721. }
  722. }
  723. static void update_sentinels(struct smq_policy *mq)
  724. {
  725. if (time_after(jiffies, mq->next_writeback_period)) {
  726. mq->next_writeback_period = jiffies + WRITEBACK_PERIOD;
  727. mq->current_writeback_sentinels = !mq->current_writeback_sentinels;
  728. __update_writeback_sentinels(mq);
  729. }
  730. if (time_after(jiffies, mq->next_demote_period)) {
  731. mq->next_demote_period = jiffies + DEMOTE_PERIOD;
  732. mq->current_demote_sentinels = !mq->current_demote_sentinels;
  733. __update_demote_sentinels(mq);
  734. }
  735. }
  736. static void __sentinels_init(struct smq_policy *mq)
  737. {
  738. unsigned int level;
  739. struct entry *sentinel;
  740. for (level = 0; level < NR_CACHE_LEVELS; level++) {
  741. sentinel = writeback_sentinel(mq, level);
  742. sentinel->level = level;
  743. q_push(&mq->dirty, sentinel);
  744. sentinel = demote_sentinel(mq, level);
  745. sentinel->level = level;
  746. q_push(&mq->clean, sentinel);
  747. }
  748. }
  749. static void sentinels_init(struct smq_policy *mq)
  750. {
  751. mq->next_writeback_period = jiffies + WRITEBACK_PERIOD;
  752. mq->next_demote_period = jiffies + DEMOTE_PERIOD;
  753. mq->current_writeback_sentinels = false;
  754. mq->current_demote_sentinels = false;
  755. __sentinels_init(mq);
  756. mq->current_writeback_sentinels = !mq->current_writeback_sentinels;
  757. mq->current_demote_sentinels = !mq->current_demote_sentinels;
  758. __sentinels_init(mq);
  759. }
  760. /*----------------------------------------------------------------*/
  761. static void del_queue(struct smq_policy *mq, struct entry *e)
  762. {
  763. q_del(e->dirty ? &mq->dirty : &mq->clean, e);
  764. }
  765. static void push_queue(struct smq_policy *mq, struct entry *e)
  766. {
  767. if (e->dirty)
  768. q_push(&mq->dirty, e);
  769. else
  770. q_push(&mq->clean, e);
  771. }
  772. // !h, !q, a -> h, q, a
  773. static void push(struct smq_policy *mq, struct entry *e)
  774. {
  775. h_insert(&mq->table, e);
  776. if (!e->pending_work)
  777. push_queue(mq, e);
  778. }
  779. static void push_queue_front(struct smq_policy *mq, struct entry *e)
  780. {
  781. if (e->dirty)
  782. q_push_front(&mq->dirty, e);
  783. else
  784. q_push_front(&mq->clean, e);
  785. }
  786. static void push_front(struct smq_policy *mq, struct entry *e)
  787. {
  788. h_insert(&mq->table, e);
  789. if (!e->pending_work)
  790. push_queue_front(mq, e);
  791. }
  792. static dm_cblock_t infer_cblock(struct smq_policy *mq, struct entry *e)
  793. {
  794. return to_cblock(get_index(&mq->cache_alloc, e));
  795. }
  796. static void requeue(struct smq_policy *mq, struct entry *e)
  797. {
  798. /*
  799. * Pending work has temporarily been taken out of the queues.
  800. */
  801. if (e->pending_work)
  802. return;
  803. if (!test_and_set_bit(from_cblock(infer_cblock(mq, e)), mq->cache_hit_bits)) {
  804. if (!e->dirty) {
  805. q_requeue(&mq->clean, e, 1u, NULL, NULL);
  806. return;
  807. }
  808. q_requeue(&mq->dirty, e, 1u,
  809. get_sentinel(&mq->writeback_sentinel_alloc, e->level, !mq->current_writeback_sentinels),
  810. get_sentinel(&mq->writeback_sentinel_alloc, e->level, mq->current_writeback_sentinels));
  811. }
  812. }
  813. static unsigned int default_promote_level(struct smq_policy *mq)
  814. {
  815. /*
  816. * The promote level depends on the current performance of the
  817. * cache.
  818. *
  819. * If the cache is performing badly, then we can't afford
  820. * to promote much without causing performance to drop below that
  821. * of the origin device.
  822. *
  823. * If the cache is performing well, then we don't need to promote
  824. * much. If it isn't broken, don't fix it.
  825. *
  826. * If the cache is middling then we promote more.
  827. *
  828. * This scheme reminds me of a graph of entropy vs probability of a
  829. * binary variable.
  830. */
  831. static const unsigned int table[] = {
  832. 1, 1, 1, 2, 4, 6, 7, 8, 7, 6, 4, 4, 3, 3, 2, 2, 1
  833. };
  834. unsigned int hits = mq->cache_stats.hits;
  835. unsigned int misses = mq->cache_stats.misses;
  836. unsigned int index = safe_div(hits << 4u, hits + misses);
  837. return table[index];
  838. }
  839. static void update_promote_levels(struct smq_policy *mq)
  840. {
  841. /*
  842. * If there are unused cache entries then we want to be really
  843. * eager to promote.
  844. */
  845. unsigned int threshold_level = allocator_empty(&mq->cache_alloc) ?
  846. default_promote_level(mq) : (NR_HOTSPOT_LEVELS / 2u);
  847. threshold_level = max(threshold_level, NR_HOTSPOT_LEVELS);
  848. /*
  849. * If the hotspot queue is performing badly then we have little
  850. * confidence that we know which blocks to promote. So we cut down
  851. * the amount of promotions.
  852. */
  853. switch (stats_assess(&mq->hotspot_stats)) {
  854. case Q_POOR:
  855. threshold_level /= 4u;
  856. break;
  857. case Q_FAIR:
  858. threshold_level /= 2u;
  859. break;
  860. case Q_WELL:
  861. break;
  862. }
  863. mq->read_promote_level = NR_HOTSPOT_LEVELS - threshold_level;
  864. mq->write_promote_level = (NR_HOTSPOT_LEVELS - threshold_level);
  865. }
  866. /*
  867. * If the hotspot queue is performing badly, then we try and move entries
  868. * around more quickly.
  869. */
  870. static void update_level_jump(struct smq_policy *mq)
  871. {
  872. switch (stats_assess(&mq->hotspot_stats)) {
  873. case Q_POOR:
  874. mq->hotspot_level_jump = 4u;
  875. break;
  876. case Q_FAIR:
  877. mq->hotspot_level_jump = 2u;
  878. break;
  879. case Q_WELL:
  880. mq->hotspot_level_jump = 1u;
  881. break;
  882. }
  883. }
  884. static void end_hotspot_period(struct smq_policy *mq)
  885. {
  886. clear_bitset(mq->hotspot_hit_bits, mq->nr_hotspot_blocks);
  887. update_promote_levels(mq);
  888. if (time_after(jiffies, mq->next_hotspot_period)) {
  889. update_level_jump(mq);
  890. q_redistribute(&mq->hotspot);
  891. stats_reset(&mq->hotspot_stats);
  892. mq->next_hotspot_period = jiffies + HOTSPOT_UPDATE_PERIOD;
  893. }
  894. }
  895. static void end_cache_period(struct smq_policy *mq)
  896. {
  897. if (time_after(jiffies, mq->next_cache_period)) {
  898. clear_bitset(mq->cache_hit_bits, from_cblock(mq->cache_size));
  899. q_redistribute(&mq->dirty);
  900. q_redistribute(&mq->clean);
  901. stats_reset(&mq->cache_stats);
  902. mq->next_cache_period = jiffies + CACHE_UPDATE_PERIOD;
  903. }
  904. }
  905. /*----------------------------------------------------------------*/
  906. /*
  907. * Targets are given as a percentage.
  908. */
  909. #define CLEAN_TARGET 25u
  910. #define FREE_TARGET 25u
  911. static unsigned int percent_to_target(struct smq_policy *mq, unsigned int p)
  912. {
  913. return from_cblock(mq->cache_size) * p / 100u;
  914. }
  915. static bool clean_target_met(struct smq_policy *mq, bool idle)
  916. {
  917. /*
  918. * Cache entries may not be populated. So we cannot rely on the
  919. * size of the clean queue.
  920. */
  921. if (idle || mq->cleaner) {
  922. /*
  923. * We'd like to clean everything.
  924. */
  925. return q_size(&mq->dirty) == 0u;
  926. }
  927. /*
  928. * If we're busy we don't worry about cleaning at all.
  929. */
  930. return true;
  931. }
  932. static bool free_target_met(struct smq_policy *mq)
  933. {
  934. unsigned int nr_free;
  935. nr_free = from_cblock(mq->cache_size) - mq->cache_alloc.nr_allocated;
  936. return (nr_free + btracker_nr_demotions_queued(mq->bg_work)) >=
  937. percent_to_target(mq, FREE_TARGET);
  938. }
  939. /*----------------------------------------------------------------*/
  940. static void mark_pending(struct smq_policy *mq, struct entry *e)
  941. {
  942. BUG_ON(e->sentinel);
  943. BUG_ON(!e->allocated);
  944. BUG_ON(e->pending_work);
  945. e->pending_work = true;
  946. }
  947. static void clear_pending(struct smq_policy *mq, struct entry *e)
  948. {
  949. BUG_ON(!e->pending_work);
  950. e->pending_work = false;
  951. }
  952. static void queue_writeback(struct smq_policy *mq, bool idle)
  953. {
  954. int r;
  955. struct policy_work work;
  956. struct entry *e;
  957. e = q_peek(&mq->dirty, mq->dirty.nr_levels, idle);
  958. if (e) {
  959. mark_pending(mq, e);
  960. q_del(&mq->dirty, e);
  961. work.op = POLICY_WRITEBACK;
  962. work.oblock = e->oblock;
  963. work.cblock = infer_cblock(mq, e);
  964. r = btracker_queue(mq->bg_work, &work, NULL);
  965. if (r) {
  966. clear_pending(mq, e);
  967. q_push_front(&mq->dirty, e);
  968. }
  969. }
  970. }
  971. static void queue_demotion(struct smq_policy *mq)
  972. {
  973. int r;
  974. struct policy_work work;
  975. struct entry *e;
  976. if (WARN_ON_ONCE(!mq->migrations_allowed))
  977. return;
  978. e = q_peek(&mq->clean, mq->clean.nr_levels / 2, true);
  979. if (!e) {
  980. if (!clean_target_met(mq, true))
  981. queue_writeback(mq, false);
  982. return;
  983. }
  984. mark_pending(mq, e);
  985. q_del(&mq->clean, e);
  986. work.op = POLICY_DEMOTE;
  987. work.oblock = e->oblock;
  988. work.cblock = infer_cblock(mq, e);
  989. r = btracker_queue(mq->bg_work, &work, NULL);
  990. if (r) {
  991. clear_pending(mq, e);
  992. q_push_front(&mq->clean, e);
  993. }
  994. }
  995. static void queue_promotion(struct smq_policy *mq, dm_oblock_t oblock,
  996. struct policy_work **workp)
  997. {
  998. int r;
  999. struct entry *e;
  1000. struct policy_work work;
  1001. if (!mq->migrations_allowed)
  1002. return;
  1003. if (allocator_empty(&mq->cache_alloc)) {
  1004. /*
  1005. * We always claim to be 'idle' to ensure some demotions happen
  1006. * with continuous loads.
  1007. */
  1008. if (!free_target_met(mq))
  1009. queue_demotion(mq);
  1010. return;
  1011. }
  1012. if (btracker_promotion_already_present(mq->bg_work, oblock))
  1013. return;
  1014. /*
  1015. * We allocate the entry now to reserve the cblock. If the
  1016. * background work is aborted we must remember to free it.
  1017. */
  1018. e = alloc_entry(&mq->cache_alloc);
  1019. BUG_ON(!e);
  1020. e->pending_work = true;
  1021. work.op = POLICY_PROMOTE;
  1022. work.oblock = oblock;
  1023. work.cblock = infer_cblock(mq, e);
  1024. r = btracker_queue(mq->bg_work, &work, workp);
  1025. if (r)
  1026. free_entry(&mq->cache_alloc, e);
  1027. }
  1028. /*----------------------------------------------------------------*/
  1029. enum promote_result {
  1030. PROMOTE_NOT,
  1031. PROMOTE_TEMPORARY,
  1032. PROMOTE_PERMANENT
  1033. };
  1034. /*
  1035. * Converts a boolean into a promote result.
  1036. */
  1037. static enum promote_result maybe_promote(bool promote)
  1038. {
  1039. return promote ? PROMOTE_PERMANENT : PROMOTE_NOT;
  1040. }
  1041. static enum promote_result should_promote(struct smq_policy *mq, struct entry *hs_e,
  1042. int data_dir, bool fast_promote)
  1043. {
  1044. if (data_dir == WRITE) {
  1045. if (!allocator_empty(&mq->cache_alloc) && fast_promote)
  1046. return PROMOTE_TEMPORARY;
  1047. return maybe_promote(hs_e->level >= mq->write_promote_level);
  1048. } else
  1049. return maybe_promote(hs_e->level >= mq->read_promote_level);
  1050. }
  1051. static dm_oblock_t to_hblock(struct smq_policy *mq, dm_oblock_t b)
  1052. {
  1053. sector_t r = from_oblock(b);
  1054. (void) sector_div(r, mq->cache_blocks_per_hotspot_block);
  1055. return to_oblock(r);
  1056. }
  1057. static struct entry *update_hotspot_queue(struct smq_policy *mq, dm_oblock_t b)
  1058. {
  1059. unsigned int hi;
  1060. dm_oblock_t hb = to_hblock(mq, b);
  1061. struct entry *e = h_lookup(&mq->hotspot_table, hb);
  1062. if (e) {
  1063. stats_level_accessed(&mq->hotspot_stats, e->level);
  1064. hi = get_index(&mq->hotspot_alloc, e);
  1065. q_requeue(&mq->hotspot, e,
  1066. test_and_set_bit(hi, mq->hotspot_hit_bits) ?
  1067. 0u : mq->hotspot_level_jump,
  1068. NULL, NULL);
  1069. } else {
  1070. stats_miss(&mq->hotspot_stats);
  1071. e = alloc_entry(&mq->hotspot_alloc);
  1072. if (!e) {
  1073. e = q_pop(&mq->hotspot);
  1074. if (e) {
  1075. h_remove(&mq->hotspot_table, e);
  1076. hi = get_index(&mq->hotspot_alloc, e);
  1077. clear_bit(hi, mq->hotspot_hit_bits);
  1078. }
  1079. }
  1080. if (e) {
  1081. e->oblock = hb;
  1082. q_push(&mq->hotspot, e);
  1083. h_insert(&mq->hotspot_table, e);
  1084. }
  1085. }
  1086. return e;
  1087. }
  1088. /*----------------------------------------------------------------*/
  1089. /*
  1090. * Public interface, via the policy struct. See dm-cache-policy.h for a
  1091. * description of these.
  1092. */
  1093. static struct smq_policy *to_smq_policy(struct dm_cache_policy *p)
  1094. {
  1095. return container_of(p, struct smq_policy, policy);
  1096. }
  1097. static void smq_destroy(struct dm_cache_policy *p)
  1098. {
  1099. struct smq_policy *mq = to_smq_policy(p);
  1100. btracker_destroy(mq->bg_work);
  1101. h_exit(&mq->hotspot_table);
  1102. h_exit(&mq->table);
  1103. free_bitset(mq->hotspot_hit_bits);
  1104. free_bitset(mq->cache_hit_bits);
  1105. space_exit(&mq->es);
  1106. kfree(mq);
  1107. }
  1108. /*----------------------------------------------------------------*/
  1109. static int __lookup(struct smq_policy *mq, dm_oblock_t oblock, dm_cblock_t *cblock,
  1110. int data_dir, bool fast_copy,
  1111. struct policy_work **work, bool *background_work)
  1112. {
  1113. struct entry *e, *hs_e;
  1114. enum promote_result pr;
  1115. *background_work = false;
  1116. e = h_lookup(&mq->table, oblock);
  1117. if (e) {
  1118. stats_level_accessed(&mq->cache_stats, e->level);
  1119. requeue(mq, e);
  1120. *cblock = infer_cblock(mq, e);
  1121. return 0;
  1122. } else {
  1123. stats_miss(&mq->cache_stats);
  1124. /*
  1125. * The hotspot queue only gets updated with misses.
  1126. */
  1127. hs_e = update_hotspot_queue(mq, oblock);
  1128. pr = should_promote(mq, hs_e, data_dir, fast_copy);
  1129. if (pr != PROMOTE_NOT) {
  1130. queue_promotion(mq, oblock, work);
  1131. *background_work = true;
  1132. }
  1133. return -ENOENT;
  1134. }
  1135. }
  1136. static int smq_lookup(struct dm_cache_policy *p, dm_oblock_t oblock, dm_cblock_t *cblock,
  1137. int data_dir, bool fast_copy,
  1138. bool *background_work)
  1139. {
  1140. int r;
  1141. unsigned long flags;
  1142. struct smq_policy *mq = to_smq_policy(p);
  1143. spin_lock_irqsave(&mq->lock, flags);
  1144. r = __lookup(mq, oblock, cblock,
  1145. data_dir, fast_copy,
  1146. NULL, background_work);
  1147. spin_unlock_irqrestore(&mq->lock, flags);
  1148. return r;
  1149. }
  1150. static int smq_lookup_with_work(struct dm_cache_policy *p,
  1151. dm_oblock_t oblock, dm_cblock_t *cblock,
  1152. int data_dir, bool fast_copy,
  1153. struct policy_work **work)
  1154. {
  1155. int r;
  1156. bool background_queued;
  1157. unsigned long flags;
  1158. struct smq_policy *mq = to_smq_policy(p);
  1159. spin_lock_irqsave(&mq->lock, flags);
  1160. r = __lookup(mq, oblock, cblock, data_dir, fast_copy, work, &background_queued);
  1161. spin_unlock_irqrestore(&mq->lock, flags);
  1162. return r;
  1163. }
  1164. static int smq_get_background_work(struct dm_cache_policy *p, bool idle,
  1165. struct policy_work **result)
  1166. {
  1167. int r;
  1168. unsigned long flags;
  1169. struct smq_policy *mq = to_smq_policy(p);
  1170. spin_lock_irqsave(&mq->lock, flags);
  1171. r = btracker_issue(mq->bg_work, result);
  1172. if (r == -ENODATA) {
  1173. if (!clean_target_met(mq, idle)) {
  1174. queue_writeback(mq, idle);
  1175. r = btracker_issue(mq->bg_work, result);
  1176. }
  1177. }
  1178. spin_unlock_irqrestore(&mq->lock, flags);
  1179. return r;
  1180. }
  1181. /*
  1182. * We need to clear any pending work flags that have been set, and in the
  1183. * case of promotion free the entry for the destination cblock.
  1184. */
  1185. static void __complete_background_work(struct smq_policy *mq,
  1186. struct policy_work *work,
  1187. bool success)
  1188. {
  1189. struct entry *e = get_entry(&mq->cache_alloc,
  1190. from_cblock(work->cblock));
  1191. switch (work->op) {
  1192. case POLICY_PROMOTE:
  1193. // !h, !q, a
  1194. clear_pending(mq, e);
  1195. if (success) {
  1196. e->oblock = work->oblock;
  1197. e->level = NR_CACHE_LEVELS - 1;
  1198. push(mq, e);
  1199. // h, q, a
  1200. } else {
  1201. free_entry(&mq->cache_alloc, e);
  1202. // !h, !q, !a
  1203. }
  1204. break;
  1205. case POLICY_DEMOTE:
  1206. // h, !q, a
  1207. if (success) {
  1208. h_remove(&mq->table, e);
  1209. free_entry(&mq->cache_alloc, e);
  1210. // !h, !q, !a
  1211. } else {
  1212. clear_pending(mq, e);
  1213. push_queue(mq, e);
  1214. // h, q, a
  1215. }
  1216. break;
  1217. case POLICY_WRITEBACK:
  1218. // h, !q, a
  1219. clear_pending(mq, e);
  1220. push_queue(mq, e);
  1221. // h, q, a
  1222. break;
  1223. }
  1224. btracker_complete(mq->bg_work, work);
  1225. }
  1226. static void smq_complete_background_work(struct dm_cache_policy *p,
  1227. struct policy_work *work,
  1228. bool success)
  1229. {
  1230. unsigned long flags;
  1231. struct smq_policy *mq = to_smq_policy(p);
  1232. spin_lock_irqsave(&mq->lock, flags);
  1233. __complete_background_work(mq, work, success);
  1234. spin_unlock_irqrestore(&mq->lock, flags);
  1235. }
  1236. // in_hash(oblock) -> in_hash(oblock)
  1237. static void __smq_set_clear_dirty(struct smq_policy *mq, dm_cblock_t cblock, bool set)
  1238. {
  1239. struct entry *e = get_entry(&mq->cache_alloc, from_cblock(cblock));
  1240. if (e->pending_work)
  1241. e->dirty = set;
  1242. else {
  1243. del_queue(mq, e);
  1244. e->dirty = set;
  1245. push_queue(mq, e);
  1246. }
  1247. }
  1248. static void smq_set_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
  1249. {
  1250. unsigned long flags;
  1251. struct smq_policy *mq = to_smq_policy(p);
  1252. spin_lock_irqsave(&mq->lock, flags);
  1253. __smq_set_clear_dirty(mq, cblock, true);
  1254. spin_unlock_irqrestore(&mq->lock, flags);
  1255. }
  1256. static void smq_clear_dirty(struct dm_cache_policy *p, dm_cblock_t cblock)
  1257. {
  1258. struct smq_policy *mq = to_smq_policy(p);
  1259. unsigned long flags;
  1260. spin_lock_irqsave(&mq->lock, flags);
  1261. __smq_set_clear_dirty(mq, cblock, false);
  1262. spin_unlock_irqrestore(&mq->lock, flags);
  1263. }
  1264. static unsigned int random_level(dm_cblock_t cblock)
  1265. {
  1266. return hash_32(from_cblock(cblock), 9) & (NR_CACHE_LEVELS - 1);
  1267. }
  1268. static int smq_load_mapping(struct dm_cache_policy *p,
  1269. dm_oblock_t oblock, dm_cblock_t cblock,
  1270. bool dirty, uint32_t hint, bool hint_valid)
  1271. {
  1272. struct smq_policy *mq = to_smq_policy(p);
  1273. struct entry *e;
  1274. e = alloc_particular_entry(&mq->cache_alloc, from_cblock(cblock));
  1275. e->oblock = oblock;
  1276. e->dirty = dirty;
  1277. e->level = hint_valid ? min(hint, NR_CACHE_LEVELS - 1) : random_level(cblock);
  1278. e->pending_work = false;
  1279. /*
  1280. * When we load mappings we push ahead of both sentinels in order to
  1281. * allow demotions and cleaning to occur immediately.
  1282. */
  1283. push_front(mq, e);
  1284. return 0;
  1285. }
  1286. static int smq_invalidate_mapping(struct dm_cache_policy *p, dm_cblock_t cblock)
  1287. {
  1288. struct smq_policy *mq = to_smq_policy(p);
  1289. struct entry *e = get_entry(&mq->cache_alloc, from_cblock(cblock));
  1290. if (!e->allocated)
  1291. return -ENODATA;
  1292. // FIXME: what if this block has pending background work?
  1293. del_queue(mq, e);
  1294. h_remove(&mq->table, e);
  1295. free_entry(&mq->cache_alloc, e);
  1296. return 0;
  1297. }
  1298. static uint32_t smq_get_hint(struct dm_cache_policy *p, dm_cblock_t cblock)
  1299. {
  1300. struct smq_policy *mq = to_smq_policy(p);
  1301. struct entry *e = get_entry(&mq->cache_alloc, from_cblock(cblock));
  1302. if (!e->allocated)
  1303. return 0;
  1304. return e->level;
  1305. }
  1306. static dm_cblock_t smq_residency(struct dm_cache_policy *p)
  1307. {
  1308. dm_cblock_t r;
  1309. unsigned long flags;
  1310. struct smq_policy *mq = to_smq_policy(p);
  1311. spin_lock_irqsave(&mq->lock, flags);
  1312. r = to_cblock(mq->cache_alloc.nr_allocated);
  1313. spin_unlock_irqrestore(&mq->lock, flags);
  1314. return r;
  1315. }
  1316. static void smq_tick(struct dm_cache_policy *p, bool can_block)
  1317. {
  1318. struct smq_policy *mq = to_smq_policy(p);
  1319. unsigned long flags;
  1320. spin_lock_irqsave(&mq->lock, flags);
  1321. mq->tick++;
  1322. update_sentinels(mq);
  1323. end_hotspot_period(mq);
  1324. end_cache_period(mq);
  1325. spin_unlock_irqrestore(&mq->lock, flags);
  1326. }
  1327. static void smq_allow_migrations(struct dm_cache_policy *p, bool allow)
  1328. {
  1329. struct smq_policy *mq = to_smq_policy(p);
  1330. mq->migrations_allowed = allow;
  1331. }
  1332. /*
  1333. * smq has no config values, but the old mq policy did. To avoid breaking
  1334. * software we continue to accept these configurables for the mq policy,
  1335. * but they have no effect.
  1336. */
  1337. static int mq_set_config_value(struct dm_cache_policy *p,
  1338. const char *key, const char *value)
  1339. {
  1340. unsigned long tmp;
  1341. if (kstrtoul(value, 10, &tmp))
  1342. return -EINVAL;
  1343. if (!strcasecmp(key, "random_threshold") ||
  1344. !strcasecmp(key, "sequential_threshold") ||
  1345. !strcasecmp(key, "discard_promote_adjustment") ||
  1346. !strcasecmp(key, "read_promote_adjustment") ||
  1347. !strcasecmp(key, "write_promote_adjustment")) {
  1348. DMWARN("tunable '%s' no longer has any effect, mq policy is now an alias for smq", key);
  1349. return 0;
  1350. }
  1351. return -EINVAL;
  1352. }
  1353. static int mq_emit_config_values(struct dm_cache_policy *p, char *result,
  1354. unsigned int maxlen, ssize_t *sz_ptr)
  1355. {
  1356. ssize_t sz = *sz_ptr;
  1357. DMEMIT("10 random_threshold 0 "
  1358. "sequential_threshold 0 "
  1359. "discard_promote_adjustment 0 "
  1360. "read_promote_adjustment 0 "
  1361. "write_promote_adjustment 0 ");
  1362. *sz_ptr = sz;
  1363. return 0;
  1364. }
  1365. /* Init the policy plugin interface function pointers. */
  1366. static void init_policy_functions(struct smq_policy *mq, bool mimic_mq)
  1367. {
  1368. mq->policy.destroy = smq_destroy;
  1369. mq->policy.lookup = smq_lookup;
  1370. mq->policy.lookup_with_work = smq_lookup_with_work;
  1371. mq->policy.get_background_work = smq_get_background_work;
  1372. mq->policy.complete_background_work = smq_complete_background_work;
  1373. mq->policy.set_dirty = smq_set_dirty;
  1374. mq->policy.clear_dirty = smq_clear_dirty;
  1375. mq->policy.load_mapping = smq_load_mapping;
  1376. mq->policy.invalidate_mapping = smq_invalidate_mapping;
  1377. mq->policy.get_hint = smq_get_hint;
  1378. mq->policy.residency = smq_residency;
  1379. mq->policy.tick = smq_tick;
  1380. mq->policy.allow_migrations = smq_allow_migrations;
  1381. if (mimic_mq) {
  1382. mq->policy.set_config_value = mq_set_config_value;
  1383. mq->policy.emit_config_values = mq_emit_config_values;
  1384. }
  1385. }
  1386. static bool too_many_hotspot_blocks(sector_t origin_size,
  1387. sector_t hotspot_block_size,
  1388. unsigned int nr_hotspot_blocks)
  1389. {
  1390. return (hotspot_block_size * nr_hotspot_blocks) > origin_size;
  1391. }
  1392. static void calc_hotspot_params(sector_t origin_size,
  1393. sector_t cache_block_size,
  1394. unsigned int nr_cache_blocks,
  1395. sector_t *hotspot_block_size,
  1396. unsigned int *nr_hotspot_blocks)
  1397. {
  1398. *hotspot_block_size = cache_block_size * 16u;
  1399. *nr_hotspot_blocks = max(nr_cache_blocks / 4u, 1024u);
  1400. while ((*hotspot_block_size > cache_block_size) &&
  1401. too_many_hotspot_blocks(origin_size, *hotspot_block_size, *nr_hotspot_blocks))
  1402. *hotspot_block_size /= 2u;
  1403. }
  1404. static struct dm_cache_policy *
  1405. __smq_create(dm_cblock_t cache_size, sector_t origin_size, sector_t cache_block_size,
  1406. bool mimic_mq, bool migrations_allowed, bool cleaner)
  1407. {
  1408. unsigned int i;
  1409. unsigned int nr_sentinels_per_queue = 2u * NR_CACHE_LEVELS;
  1410. unsigned int total_sentinels = 2u * nr_sentinels_per_queue;
  1411. struct smq_policy *mq = kzalloc(sizeof(*mq), GFP_KERNEL);
  1412. if (!mq)
  1413. return NULL;
  1414. init_policy_functions(mq, mimic_mq);
  1415. mq->cache_size = cache_size;
  1416. mq->cache_block_size = cache_block_size;
  1417. calc_hotspot_params(origin_size, cache_block_size, from_cblock(cache_size),
  1418. &mq->hotspot_block_size, &mq->nr_hotspot_blocks);
  1419. mq->cache_blocks_per_hotspot_block = div64_u64(mq->hotspot_block_size, mq->cache_block_size);
  1420. mq->hotspot_level_jump = 1u;
  1421. if (space_init(&mq->es, total_sentinels + mq->nr_hotspot_blocks + from_cblock(cache_size))) {
  1422. DMERR("couldn't initialize entry space");
  1423. goto bad_pool_init;
  1424. }
  1425. init_allocator(&mq->writeback_sentinel_alloc, &mq->es, 0, nr_sentinels_per_queue);
  1426. for (i = 0; i < nr_sentinels_per_queue; i++)
  1427. get_entry(&mq->writeback_sentinel_alloc, i)->sentinel = true;
  1428. init_allocator(&mq->demote_sentinel_alloc, &mq->es, nr_sentinels_per_queue, total_sentinels);
  1429. for (i = 0; i < nr_sentinels_per_queue; i++)
  1430. get_entry(&mq->demote_sentinel_alloc, i)->sentinel = true;
  1431. init_allocator(&mq->hotspot_alloc, &mq->es, total_sentinels,
  1432. total_sentinels + mq->nr_hotspot_blocks);
  1433. init_allocator(&mq->cache_alloc, &mq->es,
  1434. total_sentinels + mq->nr_hotspot_blocks,
  1435. total_sentinels + mq->nr_hotspot_blocks + from_cblock(cache_size));
  1436. mq->hotspot_hit_bits = alloc_bitset(mq->nr_hotspot_blocks);
  1437. if (!mq->hotspot_hit_bits) {
  1438. DMERR("couldn't allocate hotspot hit bitset");
  1439. goto bad_hotspot_hit_bits;
  1440. }
  1441. clear_bitset(mq->hotspot_hit_bits, mq->nr_hotspot_blocks);
  1442. if (from_cblock(cache_size)) {
  1443. mq->cache_hit_bits = alloc_bitset(from_cblock(cache_size));
  1444. if (!mq->cache_hit_bits) {
  1445. DMERR("couldn't allocate cache hit bitset");
  1446. goto bad_cache_hit_bits;
  1447. }
  1448. clear_bitset(mq->cache_hit_bits, from_cblock(mq->cache_size));
  1449. } else
  1450. mq->cache_hit_bits = NULL;
  1451. mq->tick = 0;
  1452. spin_lock_init(&mq->lock);
  1453. q_init(&mq->hotspot, &mq->es, NR_HOTSPOT_LEVELS);
  1454. mq->hotspot.nr_top_levels = 8;
  1455. mq->hotspot.nr_in_top_levels = min(mq->nr_hotspot_blocks / NR_HOTSPOT_LEVELS,
  1456. from_cblock(mq->cache_size) / mq->cache_blocks_per_hotspot_block);
  1457. q_init(&mq->clean, &mq->es, NR_CACHE_LEVELS);
  1458. q_init(&mq->dirty, &mq->es, NR_CACHE_LEVELS);
  1459. stats_init(&mq->hotspot_stats, NR_HOTSPOT_LEVELS);
  1460. stats_init(&mq->cache_stats, NR_CACHE_LEVELS);
  1461. if (h_init(&mq->table, &mq->es, from_cblock(cache_size)))
  1462. goto bad_alloc_table;
  1463. if (h_init(&mq->hotspot_table, &mq->es, mq->nr_hotspot_blocks))
  1464. goto bad_alloc_hotspot_table;
  1465. sentinels_init(mq);
  1466. mq->write_promote_level = mq->read_promote_level = NR_HOTSPOT_LEVELS;
  1467. mq->next_hotspot_period = jiffies;
  1468. mq->next_cache_period = jiffies;
  1469. mq->bg_work = btracker_create(4096); /* FIXME: hard coded value */
  1470. if (!mq->bg_work)
  1471. goto bad_btracker;
  1472. mq->migrations_allowed = migrations_allowed;
  1473. mq->cleaner = cleaner;
  1474. return &mq->policy;
  1475. bad_btracker:
  1476. h_exit(&mq->hotspot_table);
  1477. bad_alloc_hotspot_table:
  1478. h_exit(&mq->table);
  1479. bad_alloc_table:
  1480. free_bitset(mq->cache_hit_bits);
  1481. bad_cache_hit_bits:
  1482. free_bitset(mq->hotspot_hit_bits);
  1483. bad_hotspot_hit_bits:
  1484. space_exit(&mq->es);
  1485. bad_pool_init:
  1486. kfree(mq);
  1487. return NULL;
  1488. }
  1489. static struct dm_cache_policy *smq_create(dm_cblock_t cache_size,
  1490. sector_t origin_size,
  1491. sector_t cache_block_size)
  1492. {
  1493. return __smq_create(cache_size, origin_size, cache_block_size,
  1494. false, true, false);
  1495. }
  1496. static struct dm_cache_policy *mq_create(dm_cblock_t cache_size,
  1497. sector_t origin_size,
  1498. sector_t cache_block_size)
  1499. {
  1500. return __smq_create(cache_size, origin_size, cache_block_size,
  1501. true, true, false);
  1502. }
  1503. static struct dm_cache_policy *cleaner_create(dm_cblock_t cache_size,
  1504. sector_t origin_size,
  1505. sector_t cache_block_size)
  1506. {
  1507. return __smq_create(cache_size, origin_size, cache_block_size,
  1508. false, false, true);
  1509. }
  1510. /*----------------------------------------------------------------*/
  1511. static struct dm_cache_policy_type smq_policy_type = {
  1512. .name = "smq",
  1513. .version = {2, 0, 0},
  1514. .hint_size = 4,
  1515. .owner = THIS_MODULE,
  1516. .create = smq_create
  1517. };
  1518. static struct dm_cache_policy_type mq_policy_type = {
  1519. .name = "mq",
  1520. .version = {2, 0, 0},
  1521. .hint_size = 4,
  1522. .owner = THIS_MODULE,
  1523. .create = mq_create,
  1524. };
  1525. static struct dm_cache_policy_type cleaner_policy_type = {
  1526. .name = "cleaner",
  1527. .version = {2, 0, 0},
  1528. .hint_size = 4,
  1529. .owner = THIS_MODULE,
  1530. .create = cleaner_create,
  1531. };
  1532. static struct dm_cache_policy_type default_policy_type = {
  1533. .name = "default",
  1534. .version = {2, 0, 0},
  1535. .hint_size = 4,
  1536. .owner = THIS_MODULE,
  1537. .create = smq_create,
  1538. .real = &smq_policy_type
  1539. };
  1540. static int __init smq_init(void)
  1541. {
  1542. int r;
  1543. r = dm_cache_policy_register(&smq_policy_type);
  1544. if (r) {
  1545. DMERR("register failed %d", r);
  1546. return -ENOMEM;
  1547. }
  1548. r = dm_cache_policy_register(&mq_policy_type);
  1549. if (r) {
  1550. DMERR("register failed (as mq) %d", r);
  1551. goto out_mq;
  1552. }
  1553. r = dm_cache_policy_register(&cleaner_policy_type);
  1554. if (r) {
  1555. DMERR("register failed (as cleaner) %d", r);
  1556. goto out_cleaner;
  1557. }
  1558. r = dm_cache_policy_register(&default_policy_type);
  1559. if (r) {
  1560. DMERR("register failed (as default) %d", r);
  1561. goto out_default;
  1562. }
  1563. return 0;
  1564. out_default:
  1565. dm_cache_policy_unregister(&cleaner_policy_type);
  1566. out_cleaner:
  1567. dm_cache_policy_unregister(&mq_policy_type);
  1568. out_mq:
  1569. dm_cache_policy_unregister(&smq_policy_type);
  1570. return -ENOMEM;
  1571. }
  1572. static void __exit smq_exit(void)
  1573. {
  1574. dm_cache_policy_unregister(&cleaner_policy_type);
  1575. dm_cache_policy_unregister(&smq_policy_type);
  1576. dm_cache_policy_unregister(&mq_policy_type);
  1577. dm_cache_policy_unregister(&default_policy_type);
  1578. }
  1579. module_init(smq_init);
  1580. module_exit(smq_exit);
  1581. MODULE_AUTHOR("Joe Thornber <dm-devel@lists.linux.dev>");
  1582. MODULE_LICENSE("GPL");
  1583. MODULE_DESCRIPTION("smq cache policy");
  1584. MODULE_ALIAS("dm-cache-default");
  1585. MODULE_ALIAS("dm-cache-mq");
  1586. MODULE_ALIAS("dm-cache-cleaner");