pblk-rb.c 21 KB

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