pblk-rb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * Copyright (C) 2016 CNEX Labs
  3. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  4. *
  5. * Based upon the circular ringbuffer.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * pblk-rb.c - pblk's write buffer
  17. */
  18. #include <linux/circ_buf.h>
  19. #include "pblk.h"
  20. static DECLARE_RWSEM(pblk_rb_lock);
  21. void pblk_rb_data_free(struct pblk_rb *rb)
  22. {
  23. struct pblk_rb_pages *p, *t;
  24. down_write(&pblk_rb_lock);
  25. list_for_each_entry_safe(p, t, &rb->pages, list) {
  26. free_pages((unsigned long)page_address(p->pages), p->order);
  27. list_del(&p->list);
  28. kfree(p);
  29. }
  30. up_write(&pblk_rb_lock);
  31. }
  32. /*
  33. * Initialize ring buffer. The data and metadata buffers must be previously
  34. * allocated and their size must be a power of two
  35. * (Documentation/circular-buffers.txt)
  36. */
  37. int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
  38. unsigned int power_size, unsigned int power_seg_sz)
  39. {
  40. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  41. unsigned int init_entry = 0;
  42. unsigned int alloc_order = power_size;
  43. unsigned int max_order = MAX_ORDER - 1;
  44. unsigned int order, iter;
  45. down_write(&pblk_rb_lock);
  46. rb->entries = rb_entry_base;
  47. rb->seg_size = (1 << power_seg_sz);
  48. rb->nr_entries = (1 << power_size);
  49. rb->mem = rb->subm = rb->sync = rb->l2p_update = 0;
  50. rb->flush_point = EMPTY_ENTRY;
  51. spin_lock_init(&rb->w_lock);
  52. spin_lock_init(&rb->s_lock);
  53. INIT_LIST_HEAD(&rb->pages);
  54. if (alloc_order >= max_order) {
  55. order = max_order;
  56. iter = (1 << (alloc_order - max_order));
  57. } else {
  58. order = alloc_order;
  59. iter = 1;
  60. }
  61. do {
  62. struct pblk_rb_entry *entry;
  63. struct pblk_rb_pages *page_set;
  64. void *kaddr;
  65. unsigned long set_size;
  66. int i;
  67. page_set = kmalloc(sizeof(struct pblk_rb_pages), GFP_KERNEL);
  68. if (!page_set) {
  69. up_write(&pblk_rb_lock);
  70. return -ENOMEM;
  71. }
  72. page_set->order = order;
  73. page_set->pages = alloc_pages(GFP_KERNEL, order);
  74. if (!page_set->pages) {
  75. kfree(page_set);
  76. pblk_rb_data_free(rb);
  77. up_write(&pblk_rb_lock);
  78. return -ENOMEM;
  79. }
  80. kaddr = page_address(page_set->pages);
  81. entry = &rb->entries[init_entry];
  82. entry->data = kaddr;
  83. entry->cacheline = pblk_cacheline_to_addr(init_entry++);
  84. entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
  85. set_size = (1 << order);
  86. for (i = 1; i < set_size; i++) {
  87. entry = &rb->entries[init_entry];
  88. entry->cacheline = pblk_cacheline_to_addr(init_entry++);
  89. entry->data = kaddr + (i * rb->seg_size);
  90. entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
  91. bio_list_init(&entry->w_ctx.bios);
  92. }
  93. list_add_tail(&page_set->list, &rb->pages);
  94. iter--;
  95. } while (iter > 0);
  96. up_write(&pblk_rb_lock);
  97. #ifdef CONFIG_NVM_DEBUG
  98. atomic_set(&rb->inflight_flush_point, 0);
  99. #endif
  100. /*
  101. * Initialize rate-limiter, which controls access to the write buffer
  102. * but user and GC I/O
  103. */
  104. pblk_rl_init(&pblk->rl, rb->nr_entries);
  105. return 0;
  106. }
  107. /*
  108. * pblk_rb_calculate_size -- calculate the size of the write buffer
  109. */
  110. unsigned int pblk_rb_calculate_size(unsigned int nr_entries)
  111. {
  112. /* Alloc a write buffer that can at least fit 128 entries */
  113. return (1 << max(get_count_order(nr_entries), 7));
  114. }
  115. void *pblk_rb_entries_ref(struct pblk_rb *rb)
  116. {
  117. return rb->entries;
  118. }
  119. static void clean_wctx(struct pblk_w_ctx *w_ctx)
  120. {
  121. int flags;
  122. try:
  123. flags = READ_ONCE(w_ctx->flags);
  124. if (!(flags & PBLK_SUBMITTED_ENTRY))
  125. goto try;
  126. /* Release flags on context. Protect from writes and reads */
  127. smp_store_release(&w_ctx->flags, PBLK_WRITABLE_ENTRY);
  128. pblk_ppa_set_empty(&w_ctx->ppa);
  129. w_ctx->lba = ADDR_EMPTY;
  130. }
  131. #define pblk_rb_ring_count(head, tail, size) CIRC_CNT(head, tail, size)
  132. #define pblk_rb_ring_space(rb, head, tail, size) \
  133. (CIRC_SPACE(head, tail, size))
  134. /*
  135. * Buffer space is calculated with respect to the back pointer signaling
  136. * synchronized entries to the media.
  137. */
  138. static unsigned int pblk_rb_space(struct pblk_rb *rb)
  139. {
  140. unsigned int mem = READ_ONCE(rb->mem);
  141. unsigned int sync = READ_ONCE(rb->sync);
  142. return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries);
  143. }
  144. /*
  145. * Buffer count is calculated with respect to the submission entry signaling the
  146. * entries that are available to send to the media
  147. */
  148. unsigned int pblk_rb_read_count(struct pblk_rb *rb)
  149. {
  150. unsigned int mem = READ_ONCE(rb->mem);
  151. unsigned int subm = READ_ONCE(rb->subm);
  152. return pblk_rb_ring_count(mem, subm, rb->nr_entries);
  153. }
  154. unsigned int pblk_rb_sync_count(struct pblk_rb *rb)
  155. {
  156. unsigned int mem = READ_ONCE(rb->mem);
  157. unsigned int sync = READ_ONCE(rb->sync);
  158. return pblk_rb_ring_count(mem, sync, rb->nr_entries);
  159. }
  160. unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries)
  161. {
  162. unsigned int subm;
  163. subm = READ_ONCE(rb->subm);
  164. /* Commit read means updating submission pointer */
  165. smp_store_release(&rb->subm,
  166. (subm + nr_entries) & (rb->nr_entries - 1));
  167. return subm;
  168. }
  169. static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int to_update)
  170. {
  171. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  172. struct pblk_line *line;
  173. struct pblk_rb_entry *entry;
  174. struct pblk_w_ctx *w_ctx;
  175. unsigned int user_io = 0, gc_io = 0;
  176. unsigned int i;
  177. int flags;
  178. for (i = 0; i < to_update; i++) {
  179. entry = &rb->entries[rb->l2p_update];
  180. w_ctx = &entry->w_ctx;
  181. flags = READ_ONCE(entry->w_ctx.flags);
  182. if (flags & PBLK_IOTYPE_USER)
  183. user_io++;
  184. else if (flags & PBLK_IOTYPE_GC)
  185. gc_io++;
  186. else
  187. WARN(1, "pblk: unknown IO type\n");
  188. pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa,
  189. entry->cacheline);
  190. line = &pblk->lines[pblk_ppa_to_line(w_ctx->ppa)];
  191. kref_put(&line->ref, pblk_line_put);
  192. clean_wctx(w_ctx);
  193. rb->l2p_update = (rb->l2p_update + 1) & (rb->nr_entries - 1);
  194. }
  195. pblk_rl_out(&pblk->rl, user_io, gc_io);
  196. return 0;
  197. }
  198. /*
  199. * When we move the l2p_update pointer, we update the l2p table - lookups will
  200. * point to the physical address instead of to the cacheline in the write buffer
  201. * from this moment on.
  202. */
  203. static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries,
  204. unsigned int mem, unsigned int sync)
  205. {
  206. unsigned int space, count;
  207. int ret = 0;
  208. lockdep_assert_held(&rb->w_lock);
  209. /* Update l2p only as buffer entries are being overwritten */
  210. space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries);
  211. if (space > nr_entries)
  212. goto out;
  213. count = nr_entries - space;
  214. /* l2p_update used exclusively under rb->w_lock */
  215. ret = __pblk_rb_update_l2p(rb, count);
  216. out:
  217. return ret;
  218. }
  219. /*
  220. * Update the l2p entry for all sectors stored on the write buffer. This means
  221. * that all future lookups to the l2p table will point to a device address, not
  222. * to the cacheline in the write buffer.
  223. */
  224. void pblk_rb_sync_l2p(struct pblk_rb *rb)
  225. {
  226. unsigned int sync;
  227. unsigned int to_update;
  228. spin_lock(&rb->w_lock);
  229. /* Protect from reads and writes */
  230. sync = smp_load_acquire(&rb->sync);
  231. to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries);
  232. __pblk_rb_update_l2p(rb, to_update);
  233. spin_unlock(&rb->w_lock);
  234. }
  235. /*
  236. * Write @nr_entries to ring buffer from @data buffer if there is enough space.
  237. * Typically, 4KB data chunks coming from a bio will be copied to the ring
  238. * buffer, thus the write will fail if not all incoming data can be copied.
  239. *
  240. */
  241. static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data,
  242. struct pblk_w_ctx w_ctx,
  243. struct pblk_rb_entry *entry)
  244. {
  245. memcpy(entry->data, data, rb->seg_size);
  246. entry->w_ctx.lba = w_ctx.lba;
  247. entry->w_ctx.ppa = w_ctx.ppa;
  248. }
  249. void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
  250. struct pblk_w_ctx w_ctx, unsigned int ring_pos)
  251. {
  252. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  253. struct pblk_rb_entry *entry;
  254. int flags;
  255. entry = &rb->entries[ring_pos];
  256. flags = READ_ONCE(entry->w_ctx.flags);
  257. #ifdef CONFIG_NVM_DEBUG
  258. /* Caller must guarantee that the entry is free */
  259. BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
  260. #endif
  261. __pblk_rb_write_entry(rb, data, w_ctx, entry);
  262. pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline);
  263. flags = w_ctx.flags | PBLK_WRITTEN_DATA;
  264. /* Release flags on write context. Protect from writes */
  265. smp_store_release(&entry->w_ctx.flags, flags);
  266. }
  267. void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
  268. struct pblk_w_ctx w_ctx, struct pblk_line *line,
  269. u64 paddr, unsigned int ring_pos)
  270. {
  271. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  272. struct pblk_rb_entry *entry;
  273. int flags;
  274. entry = &rb->entries[ring_pos];
  275. flags = READ_ONCE(entry->w_ctx.flags);
  276. #ifdef CONFIG_NVM_DEBUG
  277. /* Caller must guarantee that the entry is free */
  278. BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
  279. #endif
  280. __pblk_rb_write_entry(rb, data, w_ctx, entry);
  281. if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, line, paddr))
  282. entry->w_ctx.lba = ADDR_EMPTY;
  283. flags = w_ctx.flags | PBLK_WRITTEN_DATA;
  284. /* Release flags on write context. Protect from writes */
  285. smp_store_release(&entry->w_ctx.flags, flags);
  286. }
  287. static int pblk_rb_flush_point_set(struct pblk_rb *rb, struct bio *bio,
  288. unsigned int pos)
  289. {
  290. struct pblk_rb_entry *entry;
  291. unsigned int sync, flush_point;
  292. sync = READ_ONCE(rb->sync);
  293. if (pos == sync)
  294. return 0;
  295. #ifdef CONFIG_NVM_DEBUG
  296. atomic_inc(&rb->inflight_flush_point);
  297. #endif
  298. flush_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1);
  299. entry = &rb->entries[flush_point];
  300. pblk_rb_sync_init(rb, NULL);
  301. /* Protect flush points */
  302. smp_store_release(&rb->flush_point, flush_point);
  303. if (bio)
  304. bio_list_add(&entry->w_ctx.bios, bio);
  305. pblk_rb_sync_end(rb, NULL);
  306. return bio ? 1 : 0;
  307. }
  308. static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
  309. unsigned int *pos)
  310. {
  311. unsigned int mem;
  312. unsigned int sync;
  313. sync = READ_ONCE(rb->sync);
  314. mem = READ_ONCE(rb->mem);
  315. if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries)
  316. return 0;
  317. if (pblk_rb_update_l2p(rb, nr_entries, mem, sync))
  318. return 0;
  319. *pos = mem;
  320. return 1;
  321. }
  322. static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
  323. unsigned int *pos)
  324. {
  325. if (!__pblk_rb_may_write(rb, nr_entries, pos))
  326. return 0;
  327. /* Protect from read count */
  328. smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1));
  329. return 1;
  330. }
  331. void pblk_rb_flush(struct pblk_rb *rb)
  332. {
  333. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  334. unsigned int mem = READ_ONCE(rb->mem);
  335. if (pblk_rb_flush_point_set(rb, NULL, mem))
  336. return;
  337. pblk_write_should_kick(pblk);
  338. }
  339. static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries,
  340. unsigned int *pos, struct bio *bio,
  341. int *io_ret)
  342. {
  343. unsigned int mem;
  344. if (!__pblk_rb_may_write(rb, nr_entries, pos))
  345. return 0;
  346. mem = (*pos + nr_entries) & (rb->nr_entries - 1);
  347. *io_ret = NVM_IO_DONE;
  348. if (bio->bi_opf & REQ_PREFLUSH) {
  349. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  350. #ifdef CONFIG_NVM_DEBUG
  351. atomic_long_inc(&pblk->nr_flush);
  352. #endif
  353. if (pblk_rb_flush_point_set(&pblk->rwb, bio, mem))
  354. *io_ret = NVM_IO_OK;
  355. }
  356. /* Protect from read count */
  357. smp_store_release(&rb->mem, mem);
  358. return 1;
  359. }
  360. /*
  361. * Atomically check that (i) there is space on the write buffer for the
  362. * incoming I/O, and (ii) the current I/O type has enough budget in the write
  363. * buffer (rate-limiter).
  364. */
  365. int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
  366. unsigned int nr_entries, unsigned int *pos)
  367. {
  368. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  369. int io_ret;
  370. spin_lock(&rb->w_lock);
  371. io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries);
  372. if (io_ret) {
  373. spin_unlock(&rb->w_lock);
  374. return io_ret;
  375. }
  376. if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) {
  377. spin_unlock(&rb->w_lock);
  378. return NVM_IO_REQUEUE;
  379. }
  380. pblk_rl_user_in(&pblk->rl, nr_entries);
  381. spin_unlock(&rb->w_lock);
  382. return io_ret;
  383. }
  384. /*
  385. * Look at pblk_rb_may_write_user comment
  386. */
  387. int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
  388. unsigned int *pos)
  389. {
  390. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  391. spin_lock(&rb->w_lock);
  392. if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) {
  393. spin_unlock(&rb->w_lock);
  394. return 0;
  395. }
  396. if (!pblk_rb_may_write(rb, nr_entries, pos)) {
  397. spin_unlock(&rb->w_lock);
  398. return 0;
  399. }
  400. pblk_rl_gc_in(&pblk->rl, nr_entries);
  401. spin_unlock(&rb->w_lock);
  402. return 1;
  403. }
  404. /*
  405. * The caller of this function must ensure that the backpointer will not
  406. * overwrite the entries passed on the list.
  407. */
  408. unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
  409. struct list_head *list,
  410. unsigned int max)
  411. {
  412. struct pblk_rb_entry *entry, *tentry;
  413. struct page *page;
  414. unsigned int read = 0;
  415. int ret;
  416. list_for_each_entry_safe(entry, tentry, list, index) {
  417. if (read > max) {
  418. pr_err("pblk: too many entries on list\n");
  419. goto out;
  420. }
  421. page = virt_to_page(entry->data);
  422. if (!page) {
  423. pr_err("pblk: could not allocate write bio page\n");
  424. goto out;
  425. }
  426. ret = bio_add_page(bio, page, rb->seg_size, 0);
  427. if (ret != rb->seg_size) {
  428. pr_err("pblk: could not add page to write bio\n");
  429. goto out;
  430. }
  431. list_del(&entry->index);
  432. read++;
  433. }
  434. out:
  435. return read;
  436. }
  437. /*
  438. * Read available entries on rb and add them to the given bio. To avoid a memory
  439. * copy, a page reference to the write buffer is used to be added to the bio.
  440. *
  441. * This function is used by the write thread to form the write bio that will
  442. * persist data on the write buffer to the media.
  443. */
  444. unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
  445. unsigned int pos, unsigned int nr_entries,
  446. unsigned int count)
  447. {
  448. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  449. struct request_queue *q = pblk->dev->q;
  450. struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
  451. struct bio *bio = rqd->bio;
  452. struct pblk_rb_entry *entry;
  453. struct page *page;
  454. unsigned int pad = 0, to_read = nr_entries;
  455. unsigned int i;
  456. int flags;
  457. if (count < nr_entries) {
  458. pad = nr_entries - count;
  459. to_read = count;
  460. }
  461. c_ctx->sentry = pos;
  462. c_ctx->nr_valid = to_read;
  463. c_ctx->nr_padded = pad;
  464. for (i = 0; i < to_read; i++) {
  465. entry = &rb->entries[pos];
  466. /* A write has been allowed into the buffer, but data is still
  467. * being copied to it. It is ok to busy wait.
  468. */
  469. try:
  470. flags = READ_ONCE(entry->w_ctx.flags);
  471. if (!(flags & PBLK_WRITTEN_DATA)) {
  472. io_schedule();
  473. goto try;
  474. }
  475. page = virt_to_page(entry->data);
  476. if (!page) {
  477. pr_err("pblk: could not allocate write bio page\n");
  478. flags &= ~PBLK_WRITTEN_DATA;
  479. flags |= PBLK_SUBMITTED_ENTRY;
  480. /* Release flags on context. Protect from writes */
  481. smp_store_release(&entry->w_ctx.flags, flags);
  482. return NVM_IO_ERR;
  483. }
  484. if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) !=
  485. rb->seg_size) {
  486. pr_err("pblk: could not add page to write bio\n");
  487. flags &= ~PBLK_WRITTEN_DATA;
  488. flags |= PBLK_SUBMITTED_ENTRY;
  489. /* Release flags on context. Protect from writes */
  490. smp_store_release(&entry->w_ctx.flags, flags);
  491. return NVM_IO_ERR;
  492. }
  493. flags &= ~PBLK_WRITTEN_DATA;
  494. flags |= PBLK_SUBMITTED_ENTRY;
  495. /* Release flags on context. Protect from writes */
  496. smp_store_release(&entry->w_ctx.flags, flags);
  497. pos = (pos + 1) & (rb->nr_entries - 1);
  498. }
  499. if (pad) {
  500. if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) {
  501. pr_err("pblk: could not pad page in write bio\n");
  502. return NVM_IO_ERR;
  503. }
  504. }
  505. #ifdef CONFIG_NVM_DEBUG
  506. atomic_long_add(pad, &((struct pblk *)
  507. (container_of(rb, struct pblk, rwb)))->padded_writes);
  508. #endif
  509. return NVM_IO_OK;
  510. }
  511. /*
  512. * Copy to bio only if the lba matches the one on the given cache entry.
  513. * Otherwise, it means that the entry has been overwritten, and the bio should
  514. * be directed to disk.
  515. */
  516. int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
  517. struct ppa_addr ppa, int bio_iter, bool advanced_bio)
  518. {
  519. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  520. struct pblk_rb_entry *entry;
  521. struct pblk_w_ctx *w_ctx;
  522. struct ppa_addr l2p_ppa;
  523. u64 pos = pblk_addr_to_cacheline(ppa);
  524. void *data;
  525. int flags;
  526. int ret = 1;
  527. #ifdef CONFIG_NVM_DEBUG
  528. /* Caller must ensure that the access will not cause an overflow */
  529. BUG_ON(pos >= rb->nr_entries);
  530. #endif
  531. entry = &rb->entries[pos];
  532. w_ctx = &entry->w_ctx;
  533. flags = READ_ONCE(w_ctx->flags);
  534. spin_lock(&rb->w_lock);
  535. spin_lock(&pblk->trans_lock);
  536. l2p_ppa = pblk_trans_map_get(pblk, lba);
  537. spin_unlock(&pblk->trans_lock);
  538. /* Check if the entry has been overwritten or is scheduled to be */
  539. if (!pblk_ppa_comp(l2p_ppa, ppa) || w_ctx->lba != lba ||
  540. flags & PBLK_WRITABLE_ENTRY) {
  541. ret = 0;
  542. goto out;
  543. }
  544. /* Only advance the bio if it hasn't been advanced already. If advanced,
  545. * this bio is at least a partial bio (i.e., it has partially been
  546. * filled with data from the cache). If part of the data resides on the
  547. * media, we will read later on
  548. */
  549. if (unlikely(!advanced_bio))
  550. bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE);
  551. data = bio_data(bio);
  552. memcpy(data, entry->data, rb->seg_size);
  553. out:
  554. spin_unlock(&rb->w_lock);
  555. return ret;
  556. }
  557. struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos)
  558. {
  559. unsigned int entry = pos & (rb->nr_entries - 1);
  560. return &rb->entries[entry].w_ctx;
  561. }
  562. unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags)
  563. __acquires(&rb->s_lock)
  564. {
  565. if (flags)
  566. spin_lock_irqsave(&rb->s_lock, *flags);
  567. else
  568. spin_lock_irq(&rb->s_lock);
  569. return rb->sync;
  570. }
  571. void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags)
  572. __releases(&rb->s_lock)
  573. {
  574. lockdep_assert_held(&rb->s_lock);
  575. if (flags)
  576. spin_unlock_irqrestore(&rb->s_lock, *flags);
  577. else
  578. spin_unlock_irq(&rb->s_lock);
  579. }
  580. unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries)
  581. {
  582. unsigned int sync, flush_point;
  583. lockdep_assert_held(&rb->s_lock);
  584. sync = READ_ONCE(rb->sync);
  585. flush_point = READ_ONCE(rb->flush_point);
  586. if (flush_point != EMPTY_ENTRY) {
  587. unsigned int secs_to_flush;
  588. secs_to_flush = pblk_rb_ring_count(flush_point, sync,
  589. rb->nr_entries);
  590. if (secs_to_flush < nr_entries) {
  591. /* Protect flush points */
  592. smp_store_release(&rb->flush_point, EMPTY_ENTRY);
  593. }
  594. }
  595. sync = (sync + nr_entries) & (rb->nr_entries - 1);
  596. /* Protect from counts */
  597. smp_store_release(&rb->sync, sync);
  598. return sync;
  599. }
  600. /* Calculate how many sectors to submit up to the current flush point. */
  601. unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb)
  602. {
  603. unsigned int subm, sync, flush_point;
  604. unsigned int submitted, to_flush;
  605. /* Protect flush points */
  606. flush_point = smp_load_acquire(&rb->flush_point);
  607. if (flush_point == EMPTY_ENTRY)
  608. return 0;
  609. /* Protect syncs */
  610. sync = smp_load_acquire(&rb->sync);
  611. subm = READ_ONCE(rb->subm);
  612. submitted = pblk_rb_ring_count(subm, sync, rb->nr_entries);
  613. /* The sync point itself counts as a sector to sync */
  614. to_flush = pblk_rb_ring_count(flush_point, sync, rb->nr_entries) + 1;
  615. return (submitted < to_flush) ? (to_flush - submitted) : 0;
  616. }
  617. /*
  618. * Scan from the current position of the sync pointer to find the entry that
  619. * corresponds to the given ppa. This is necessary since write requests can be
  620. * completed out of order. The assumption is that the ppa is close to the sync
  621. * pointer thus the search will not take long.
  622. *
  623. * The caller of this function must guarantee that the sync pointer will no
  624. * reach the entry while it is using the metadata associated with it. With this
  625. * assumption in mind, there is no need to take the sync lock.
  626. */
  627. struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
  628. struct ppa_addr *ppa)
  629. {
  630. unsigned int sync, subm, count;
  631. unsigned int i;
  632. sync = READ_ONCE(rb->sync);
  633. subm = READ_ONCE(rb->subm);
  634. count = pblk_rb_ring_count(subm, sync, rb->nr_entries);
  635. for (i = 0; i < count; i++)
  636. sync = (sync + 1) & (rb->nr_entries - 1);
  637. return NULL;
  638. }
  639. int pblk_rb_tear_down_check(struct pblk_rb *rb)
  640. {
  641. struct pblk_rb_entry *entry;
  642. int i;
  643. int ret = 0;
  644. spin_lock(&rb->w_lock);
  645. spin_lock_irq(&rb->s_lock);
  646. if ((rb->mem == rb->subm) && (rb->subm == rb->sync) &&
  647. (rb->sync == rb->l2p_update) &&
  648. (rb->flush_point == EMPTY_ENTRY)) {
  649. goto out;
  650. }
  651. if (!rb->entries) {
  652. ret = 1;
  653. goto out;
  654. }
  655. for (i = 0; i < rb->nr_entries; i++) {
  656. entry = &rb->entries[i];
  657. if (!entry->data) {
  658. ret = 1;
  659. goto out;
  660. }
  661. }
  662. out:
  663. spin_unlock(&rb->w_lock);
  664. spin_unlock_irq(&rb->s_lock);
  665. return ret;
  666. }
  667. unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos)
  668. {
  669. return (pos & (rb->nr_entries - 1));
  670. }
  671. int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos)
  672. {
  673. return (pos >= rb->nr_entries);
  674. }
  675. ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf)
  676. {
  677. struct pblk *pblk = container_of(rb, struct pblk, rwb);
  678. struct pblk_c_ctx *c;
  679. ssize_t offset;
  680. int queued_entries = 0;
  681. spin_lock_irq(&rb->s_lock);
  682. list_for_each_entry(c, &pblk->compl_list, list)
  683. queued_entries++;
  684. spin_unlock_irq(&rb->s_lock);
  685. if (rb->flush_point != EMPTY_ENTRY)
  686. offset = scnprintf(buf, PAGE_SIZE,
  687. "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n",
  688. rb->nr_entries,
  689. rb->mem,
  690. rb->subm,
  691. rb->sync,
  692. rb->l2p_update,
  693. #ifdef CONFIG_NVM_DEBUG
  694. atomic_read(&rb->inflight_flush_point),
  695. #else
  696. 0,
  697. #endif
  698. rb->flush_point,
  699. pblk_rb_read_count(rb),
  700. pblk_rb_space(rb),
  701. pblk_rb_flush_point_count(rb),
  702. queued_entries);
  703. else
  704. offset = scnprintf(buf, PAGE_SIZE,
  705. "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n",
  706. rb->nr_entries,
  707. rb->mem,
  708. rb->subm,
  709. rb->sync,
  710. rb->l2p_update,
  711. #ifdef CONFIG_NVM_DEBUG
  712. atomic_read(&rb->inflight_flush_point),
  713. #else
  714. 0,
  715. #endif
  716. pblk_rb_read_count(rb),
  717. pblk_rb_space(rb),
  718. pblk_rb_flush_point_count(rb),
  719. queued_entries);
  720. return offset;
  721. }