Linux 4.16.11
[linux/fpc-iii.git] / drivers / lightnvm / pblk-rb.c
blobec8fc314646ba5ffcf04855a81935f5185f9f4bf
1 /*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Based upon the circular ringbuffer.
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.
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.
16 * pblk-rb.c - pblk's write buffer
19 #include <linux/circ_buf.h>
21 #include "pblk.h"
23 static DECLARE_RWSEM(pblk_rb_lock);
25 void pblk_rb_data_free(struct pblk_rb *rb)
27 struct pblk_rb_pages *p, *t;
29 down_write(&pblk_rb_lock);
30 list_for_each_entry_safe(p, t, &rb->pages, list) {
31 free_pages((unsigned long)page_address(p->pages), p->order);
32 list_del(&p->list);
33 kfree(p);
35 up_write(&pblk_rb_lock);
39 * Initialize ring buffer. The data and metadata buffers must be previously
40 * allocated and their size must be a power of two
41 * (Documentation/circular-buffers.txt)
43 int pblk_rb_init(struct pblk_rb *rb, struct pblk_rb_entry *rb_entry_base,
44 unsigned int power_size, unsigned int power_seg_sz)
46 struct pblk *pblk = container_of(rb, struct pblk, rwb);
47 unsigned int init_entry = 0;
48 unsigned int alloc_order = power_size;
49 unsigned int max_order = MAX_ORDER - 1;
50 unsigned int order, iter;
52 down_write(&pblk_rb_lock);
53 rb->entries = rb_entry_base;
54 rb->seg_size = (1 << power_seg_sz);
55 rb->nr_entries = (1 << power_size);
56 rb->mem = rb->subm = rb->sync = rb->l2p_update = 0;
57 rb->flush_point = EMPTY_ENTRY;
59 spin_lock_init(&rb->w_lock);
60 spin_lock_init(&rb->s_lock);
62 INIT_LIST_HEAD(&rb->pages);
64 if (alloc_order >= max_order) {
65 order = max_order;
66 iter = (1 << (alloc_order - max_order));
67 } else {
68 order = alloc_order;
69 iter = 1;
72 do {
73 struct pblk_rb_entry *entry;
74 struct pblk_rb_pages *page_set;
75 void *kaddr;
76 unsigned long set_size;
77 int i;
79 page_set = kmalloc(sizeof(struct pblk_rb_pages), GFP_KERNEL);
80 if (!page_set) {
81 up_write(&pblk_rb_lock);
82 return -ENOMEM;
85 page_set->order = order;
86 page_set->pages = alloc_pages(GFP_KERNEL, order);
87 if (!page_set->pages) {
88 kfree(page_set);
89 pblk_rb_data_free(rb);
90 up_write(&pblk_rb_lock);
91 return -ENOMEM;
93 kaddr = page_address(page_set->pages);
95 entry = &rb->entries[init_entry];
96 entry->data = kaddr;
97 entry->cacheline = pblk_cacheline_to_addr(init_entry++);
98 entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
100 set_size = (1 << order);
101 for (i = 1; i < set_size; i++) {
102 entry = &rb->entries[init_entry];
103 entry->cacheline = pblk_cacheline_to_addr(init_entry++);
104 entry->data = kaddr + (i * rb->seg_size);
105 entry->w_ctx.flags = PBLK_WRITABLE_ENTRY;
106 bio_list_init(&entry->w_ctx.bios);
109 list_add_tail(&page_set->list, &rb->pages);
110 iter--;
111 } while (iter > 0);
112 up_write(&pblk_rb_lock);
114 #ifdef CONFIG_NVM_DEBUG
115 atomic_set(&rb->inflight_flush_point, 0);
116 #endif
119 * Initialize rate-limiter, which controls access to the write buffer
120 * but user and GC I/O
122 pblk_rl_init(&pblk->rl, rb->nr_entries);
124 return 0;
128 * pblk_rb_calculate_size -- calculate the size of the write buffer
130 unsigned int pblk_rb_calculate_size(unsigned int nr_entries)
132 /* Alloc a write buffer that can at least fit 128 entries */
133 return (1 << max(get_count_order(nr_entries), 7));
136 void *pblk_rb_entries_ref(struct pblk_rb *rb)
138 return rb->entries;
141 static void clean_wctx(struct pblk_w_ctx *w_ctx)
143 int flags;
145 try:
146 flags = READ_ONCE(w_ctx->flags);
147 if (!(flags & PBLK_SUBMITTED_ENTRY))
148 goto try;
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;
156 #define pblk_rb_ring_count(head, tail, size) CIRC_CNT(head, tail, size)
157 #define pblk_rb_ring_space(rb, head, tail, size) \
158 (CIRC_SPACE(head, tail, size))
161 * Buffer space is calculated with respect to the back pointer signaling
162 * synchronized entries to the media.
164 static unsigned int pblk_rb_space(struct pblk_rb *rb)
166 unsigned int mem = READ_ONCE(rb->mem);
167 unsigned int sync = READ_ONCE(rb->sync);
169 return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries);
173 * Buffer count is calculated with respect to the submission entry signaling the
174 * entries that are available to send to the media
176 unsigned int pblk_rb_read_count(struct pblk_rb *rb)
178 unsigned int mem = READ_ONCE(rb->mem);
179 unsigned int subm = READ_ONCE(rb->subm);
181 return pblk_rb_ring_count(mem, subm, rb->nr_entries);
184 unsigned int pblk_rb_sync_count(struct pblk_rb *rb)
186 unsigned int mem = READ_ONCE(rb->mem);
187 unsigned int sync = READ_ONCE(rb->sync);
189 return pblk_rb_ring_count(mem, sync, rb->nr_entries);
192 unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries)
194 unsigned int subm;
196 subm = READ_ONCE(rb->subm);
197 /* Commit read means updating submission pointer */
198 smp_store_release(&rb->subm,
199 (subm + nr_entries) & (rb->nr_entries - 1));
201 return subm;
204 static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int to_update)
206 struct pblk *pblk = container_of(rb, struct pblk, rwb);
207 struct pblk_line *line;
208 struct pblk_rb_entry *entry;
209 struct pblk_w_ctx *w_ctx;
210 unsigned int user_io = 0, gc_io = 0;
211 unsigned int i;
212 int flags;
214 for (i = 0; i < to_update; i++) {
215 entry = &rb->entries[rb->l2p_update];
216 w_ctx = &entry->w_ctx;
218 flags = READ_ONCE(entry->w_ctx.flags);
219 if (flags & PBLK_IOTYPE_USER)
220 user_io++;
221 else if (flags & PBLK_IOTYPE_GC)
222 gc_io++;
223 else
224 WARN(1, "pblk: unknown IO type\n");
226 pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa,
227 entry->cacheline);
229 line = &pblk->lines[pblk_ppa_to_line(w_ctx->ppa)];
230 kref_put(&line->ref, pblk_line_put);
231 clean_wctx(w_ctx);
232 rb->l2p_update = (rb->l2p_update + 1) & (rb->nr_entries - 1);
235 pblk_rl_out(&pblk->rl, user_io, gc_io);
237 return 0;
241 * When we move the l2p_update pointer, we update the l2p table - lookups will
242 * point to the physical address instead of to the cacheline in the write buffer
243 * from this moment on.
245 static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries,
246 unsigned int mem, unsigned int sync)
248 unsigned int space, count;
249 int ret = 0;
251 lockdep_assert_held(&rb->w_lock);
253 /* Update l2p only as buffer entries are being overwritten */
254 space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries);
255 if (space > nr_entries)
256 goto out;
258 count = nr_entries - space;
259 /* l2p_update used exclusively under rb->w_lock */
260 ret = __pblk_rb_update_l2p(rb, count);
262 out:
263 return ret;
267 * Update the l2p entry for all sectors stored on the write buffer. This means
268 * that all future lookups to the l2p table will point to a device address, not
269 * to the cacheline in the write buffer.
271 void pblk_rb_sync_l2p(struct pblk_rb *rb)
273 unsigned int sync;
274 unsigned int to_update;
276 spin_lock(&rb->w_lock);
278 /* Protect from reads and writes */
279 sync = smp_load_acquire(&rb->sync);
281 to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries);
282 __pblk_rb_update_l2p(rb, to_update);
284 spin_unlock(&rb->w_lock);
288 * Write @nr_entries to ring buffer from @data buffer if there is enough space.
289 * Typically, 4KB data chunks coming from a bio will be copied to the ring
290 * buffer, thus the write will fail if not all incoming data can be copied.
293 static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data,
294 struct pblk_w_ctx w_ctx,
295 struct pblk_rb_entry *entry)
297 memcpy(entry->data, data, rb->seg_size);
299 entry->w_ctx.lba = w_ctx.lba;
300 entry->w_ctx.ppa = w_ctx.ppa;
303 void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
304 struct pblk_w_ctx w_ctx, unsigned int ring_pos)
306 struct pblk *pblk = container_of(rb, struct pblk, rwb);
307 struct pblk_rb_entry *entry;
308 int flags;
310 entry = &rb->entries[ring_pos];
311 flags = READ_ONCE(entry->w_ctx.flags);
312 #ifdef CONFIG_NVM_DEBUG
313 /* Caller must guarantee that the entry is free */
314 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
315 #endif
317 __pblk_rb_write_entry(rb, data, w_ctx, entry);
319 pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline);
320 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
322 /* Release flags on write context. Protect from writes */
323 smp_store_release(&entry->w_ctx.flags, flags);
326 void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data,
327 struct pblk_w_ctx w_ctx, struct pblk_line *line,
328 u64 paddr, unsigned int ring_pos)
330 struct pblk *pblk = container_of(rb, struct pblk, rwb);
331 struct pblk_rb_entry *entry;
332 int flags;
334 entry = &rb->entries[ring_pos];
335 flags = READ_ONCE(entry->w_ctx.flags);
336 #ifdef CONFIG_NVM_DEBUG
337 /* Caller must guarantee that the entry is free */
338 BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
339 #endif
341 __pblk_rb_write_entry(rb, data, w_ctx, entry);
343 if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, line, paddr))
344 entry->w_ctx.lba = ADDR_EMPTY;
346 flags = w_ctx.flags | PBLK_WRITTEN_DATA;
348 /* Release flags on write context. Protect from writes */
349 smp_store_release(&entry->w_ctx.flags, flags);
352 static int pblk_rb_flush_point_set(struct pblk_rb *rb, struct bio *bio,
353 unsigned int pos)
355 struct pblk_rb_entry *entry;
356 unsigned int sync, flush_point;
358 sync = READ_ONCE(rb->sync);
360 if (pos == sync)
361 return 0;
363 #ifdef CONFIG_NVM_DEBUG
364 atomic_inc(&rb->inflight_flush_point);
365 #endif
367 flush_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1);
368 entry = &rb->entries[flush_point];
370 pblk_rb_sync_init(rb, NULL);
372 /* Protect flush points */
373 smp_store_release(&rb->flush_point, flush_point);
375 if (bio)
376 bio_list_add(&entry->w_ctx.bios, bio);
378 pblk_rb_sync_end(rb, NULL);
380 return bio ? 1 : 0;
383 static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
384 unsigned int *pos)
386 unsigned int mem;
387 unsigned int sync;
389 sync = READ_ONCE(rb->sync);
390 mem = READ_ONCE(rb->mem);
392 if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries)
393 return 0;
395 if (pblk_rb_update_l2p(rb, nr_entries, mem, sync))
396 return 0;
398 *pos = mem;
400 return 1;
403 static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
404 unsigned int *pos)
406 if (!__pblk_rb_may_write(rb, nr_entries, pos))
407 return 0;
409 /* Protect from read count */
410 smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1));
411 return 1;
414 void pblk_rb_flush(struct pblk_rb *rb)
416 struct pblk *pblk = container_of(rb, struct pblk, rwb);
417 unsigned int mem = READ_ONCE(rb->mem);
419 if (pblk_rb_flush_point_set(rb, NULL, mem))
420 return;
422 pblk_write_should_kick(pblk);
425 static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries,
426 unsigned int *pos, struct bio *bio,
427 int *io_ret)
429 unsigned int mem;
431 if (!__pblk_rb_may_write(rb, nr_entries, pos))
432 return 0;
434 mem = (*pos + nr_entries) & (rb->nr_entries - 1);
435 *io_ret = NVM_IO_DONE;
437 if (bio->bi_opf & REQ_PREFLUSH) {
438 struct pblk *pblk = container_of(rb, struct pblk, rwb);
440 #ifdef CONFIG_NVM_DEBUG
441 atomic_long_inc(&pblk->nr_flush);
442 #endif
443 if (pblk_rb_flush_point_set(&pblk->rwb, bio, mem))
444 *io_ret = NVM_IO_OK;
447 /* Protect from read count */
448 smp_store_release(&rb->mem, mem);
450 return 1;
454 * Atomically check that (i) there is space on the write buffer for the
455 * incoming I/O, and (ii) the current I/O type has enough budget in the write
456 * buffer (rate-limiter).
458 int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
459 unsigned int nr_entries, unsigned int *pos)
461 struct pblk *pblk = container_of(rb, struct pblk, rwb);
462 int io_ret;
464 spin_lock(&rb->w_lock);
465 io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries);
466 if (io_ret) {
467 spin_unlock(&rb->w_lock);
468 return io_ret;
471 if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) {
472 spin_unlock(&rb->w_lock);
473 return NVM_IO_REQUEUE;
476 pblk_rl_user_in(&pblk->rl, nr_entries);
477 spin_unlock(&rb->w_lock);
479 return io_ret;
483 * Look at pblk_rb_may_write_user comment
485 int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
486 unsigned int *pos)
488 struct pblk *pblk = container_of(rb, struct pblk, rwb);
490 spin_lock(&rb->w_lock);
491 if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) {
492 spin_unlock(&rb->w_lock);
493 return 0;
496 if (!pblk_rb_may_write(rb, nr_entries, pos)) {
497 spin_unlock(&rb->w_lock);
498 return 0;
501 pblk_rl_gc_in(&pblk->rl, nr_entries);
502 spin_unlock(&rb->w_lock);
504 return 1;
508 * The caller of this function must ensure that the backpointer will not
509 * overwrite the entries passed on the list.
511 unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio,
512 struct list_head *list,
513 unsigned int max)
515 struct pblk_rb_entry *entry, *tentry;
516 struct page *page;
517 unsigned int read = 0;
518 int ret;
520 list_for_each_entry_safe(entry, tentry, list, index) {
521 if (read > max) {
522 pr_err("pblk: too many entries on list\n");
523 goto out;
526 page = virt_to_page(entry->data);
527 if (!page) {
528 pr_err("pblk: could not allocate write bio page\n");
529 goto out;
532 ret = bio_add_page(bio, page, rb->seg_size, 0);
533 if (ret != rb->seg_size) {
534 pr_err("pblk: could not add page to write bio\n");
535 goto out;
538 list_del(&entry->index);
539 read++;
542 out:
543 return read;
547 * Read available entries on rb and add them to the given bio. To avoid a memory
548 * copy, a page reference to the write buffer is used to be added to the bio.
550 * This function is used by the write thread to form the write bio that will
551 * persist data on the write buffer to the media.
553 unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
554 unsigned int pos, unsigned int nr_entries,
555 unsigned int count)
557 struct pblk *pblk = container_of(rb, struct pblk, rwb);
558 struct request_queue *q = pblk->dev->q;
559 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
560 struct bio *bio = rqd->bio;
561 struct pblk_rb_entry *entry;
562 struct page *page;
563 unsigned int pad = 0, to_read = nr_entries;
564 unsigned int i;
565 int flags;
567 if (count < nr_entries) {
568 pad = nr_entries - count;
569 to_read = count;
572 c_ctx->sentry = pos;
573 c_ctx->nr_valid = to_read;
574 c_ctx->nr_padded = pad;
576 for (i = 0; i < to_read; i++) {
577 entry = &rb->entries[pos];
579 /* A write has been allowed into the buffer, but data is still
580 * being copied to it. It is ok to busy wait.
582 try:
583 flags = READ_ONCE(entry->w_ctx.flags);
584 if (!(flags & PBLK_WRITTEN_DATA)) {
585 io_schedule();
586 goto try;
589 page = virt_to_page(entry->data);
590 if (!page) {
591 pr_err("pblk: could not allocate write bio page\n");
592 flags &= ~PBLK_WRITTEN_DATA;
593 flags |= PBLK_SUBMITTED_ENTRY;
594 /* Release flags on context. Protect from writes */
595 smp_store_release(&entry->w_ctx.flags, flags);
596 return NVM_IO_ERR;
599 if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) !=
600 rb->seg_size) {
601 pr_err("pblk: could not add page to write bio\n");
602 flags &= ~PBLK_WRITTEN_DATA;
603 flags |= PBLK_SUBMITTED_ENTRY;
604 /* Release flags on context. Protect from writes */
605 smp_store_release(&entry->w_ctx.flags, flags);
606 return NVM_IO_ERR;
609 flags &= ~PBLK_WRITTEN_DATA;
610 flags |= PBLK_SUBMITTED_ENTRY;
612 /* Release flags on context. Protect from writes */
613 smp_store_release(&entry->w_ctx.flags, flags);
615 pos = (pos + 1) & (rb->nr_entries - 1);
618 if (pad) {
619 if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) {
620 pr_err("pblk: could not pad page in write bio\n");
621 return NVM_IO_ERR;
625 #ifdef CONFIG_NVM_DEBUG
626 atomic_long_add(pad, &((struct pblk *)
627 (container_of(rb, struct pblk, rwb)))->padded_writes);
628 #endif
630 return NVM_IO_OK;
634 * Copy to bio only if the lba matches the one on the given cache entry.
635 * Otherwise, it means that the entry has been overwritten, and the bio should
636 * be directed to disk.
638 int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
639 struct ppa_addr ppa, int bio_iter, bool advanced_bio)
641 struct pblk *pblk = container_of(rb, struct pblk, rwb);
642 struct pblk_rb_entry *entry;
643 struct pblk_w_ctx *w_ctx;
644 struct ppa_addr l2p_ppa;
645 u64 pos = pblk_addr_to_cacheline(ppa);
646 void *data;
647 int flags;
648 int ret = 1;
651 #ifdef CONFIG_NVM_DEBUG
652 /* Caller must ensure that the access will not cause an overflow */
653 BUG_ON(pos >= rb->nr_entries);
654 #endif
655 entry = &rb->entries[pos];
656 w_ctx = &entry->w_ctx;
657 flags = READ_ONCE(w_ctx->flags);
659 spin_lock(&rb->w_lock);
660 spin_lock(&pblk->trans_lock);
661 l2p_ppa = pblk_trans_map_get(pblk, lba);
662 spin_unlock(&pblk->trans_lock);
664 /* Check if the entry has been overwritten or is scheduled to be */
665 if (!pblk_ppa_comp(l2p_ppa, ppa) || w_ctx->lba != lba ||
666 flags & PBLK_WRITABLE_ENTRY) {
667 ret = 0;
668 goto out;
671 /* Only advance the bio if it hasn't been advanced already. If advanced,
672 * this bio is at least a partial bio (i.e., it has partially been
673 * filled with data from the cache). If part of the data resides on the
674 * media, we will read later on
676 if (unlikely(!advanced_bio))
677 bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE);
679 data = bio_data(bio);
680 memcpy(data, entry->data, rb->seg_size);
682 out:
683 spin_unlock(&rb->w_lock);
684 return ret;
687 struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos)
689 unsigned int entry = pos & (rb->nr_entries - 1);
691 return &rb->entries[entry].w_ctx;
694 unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags)
695 __acquires(&rb->s_lock)
697 if (flags)
698 spin_lock_irqsave(&rb->s_lock, *flags);
699 else
700 spin_lock_irq(&rb->s_lock);
702 return rb->sync;
705 void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags)
706 __releases(&rb->s_lock)
708 lockdep_assert_held(&rb->s_lock);
710 if (flags)
711 spin_unlock_irqrestore(&rb->s_lock, *flags);
712 else
713 spin_unlock_irq(&rb->s_lock);
716 unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries)
718 unsigned int sync, flush_point;
719 lockdep_assert_held(&rb->s_lock);
721 sync = READ_ONCE(rb->sync);
722 flush_point = READ_ONCE(rb->flush_point);
724 if (flush_point != EMPTY_ENTRY) {
725 unsigned int secs_to_flush;
727 secs_to_flush = pblk_rb_ring_count(flush_point, sync,
728 rb->nr_entries);
729 if (secs_to_flush < nr_entries) {
730 /* Protect flush points */
731 smp_store_release(&rb->flush_point, EMPTY_ENTRY);
735 sync = (sync + nr_entries) & (rb->nr_entries - 1);
737 /* Protect from counts */
738 smp_store_release(&rb->sync, sync);
740 return sync;
743 /* Calculate how many sectors to submit up to the current flush point. */
744 unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb)
746 unsigned int subm, sync, flush_point;
747 unsigned int submitted, to_flush;
749 /* Protect flush points */
750 flush_point = smp_load_acquire(&rb->flush_point);
751 if (flush_point == EMPTY_ENTRY)
752 return 0;
754 /* Protect syncs */
755 sync = smp_load_acquire(&rb->sync);
757 subm = READ_ONCE(rb->subm);
758 submitted = pblk_rb_ring_count(subm, sync, rb->nr_entries);
760 /* The sync point itself counts as a sector to sync */
761 to_flush = pblk_rb_ring_count(flush_point, sync, rb->nr_entries) + 1;
763 return (submitted < to_flush) ? (to_flush - submitted) : 0;
767 * Scan from the current position of the sync pointer to find the entry that
768 * corresponds to the given ppa. This is necessary since write requests can be
769 * completed out of order. The assumption is that the ppa is close to the sync
770 * pointer thus the search will not take long.
772 * The caller of this function must guarantee that the sync pointer will no
773 * reach the entry while it is using the metadata associated with it. With this
774 * assumption in mind, there is no need to take the sync lock.
776 struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
777 struct ppa_addr *ppa)
779 unsigned int sync, subm, count;
780 unsigned int i;
782 sync = READ_ONCE(rb->sync);
783 subm = READ_ONCE(rb->subm);
784 count = pblk_rb_ring_count(subm, sync, rb->nr_entries);
786 for (i = 0; i < count; i++)
787 sync = (sync + 1) & (rb->nr_entries - 1);
789 return NULL;
792 int pblk_rb_tear_down_check(struct pblk_rb *rb)
794 struct pblk_rb_entry *entry;
795 int i;
796 int ret = 0;
798 spin_lock(&rb->w_lock);
799 spin_lock_irq(&rb->s_lock);
801 if ((rb->mem == rb->subm) && (rb->subm == rb->sync) &&
802 (rb->sync == rb->l2p_update) &&
803 (rb->flush_point == EMPTY_ENTRY)) {
804 goto out;
807 if (!rb->entries) {
808 ret = 1;
809 goto out;
812 for (i = 0; i < rb->nr_entries; i++) {
813 entry = &rb->entries[i];
815 if (!entry->data) {
816 ret = 1;
817 goto out;
821 out:
822 spin_unlock(&rb->w_lock);
823 spin_unlock_irq(&rb->s_lock);
825 return ret;
828 unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos)
830 return (pos & (rb->nr_entries - 1));
833 int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos)
835 return (pos >= rb->nr_entries);
838 ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf)
840 struct pblk *pblk = container_of(rb, struct pblk, rwb);
841 struct pblk_c_ctx *c;
842 ssize_t offset;
843 int queued_entries = 0;
845 spin_lock_irq(&rb->s_lock);
846 list_for_each_entry(c, &pblk->compl_list, list)
847 queued_entries++;
848 spin_unlock_irq(&rb->s_lock);
850 if (rb->flush_point != EMPTY_ENTRY)
851 offset = scnprintf(buf, PAGE_SIZE,
852 "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n",
853 rb->nr_entries,
854 rb->mem,
855 rb->subm,
856 rb->sync,
857 rb->l2p_update,
858 #ifdef CONFIG_NVM_DEBUG
859 atomic_read(&rb->inflight_flush_point),
860 #else
862 #endif
863 rb->flush_point,
864 pblk_rb_read_count(rb),
865 pblk_rb_space(rb),
866 pblk_rb_flush_point_count(rb),
867 queued_entries);
868 else
869 offset = scnprintf(buf, PAGE_SIZE,
870 "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n",
871 rb->nr_entries,
872 rb->mem,
873 rb->subm,
874 rb->sync,
875 rb->l2p_update,
876 #ifdef CONFIG_NVM_DEBUG
877 atomic_read(&rb->inflight_flush_point),
878 #else
880 #endif
881 pblk_rb_read_count(rb),
882 pblk_rb_space(rb),
883 pblk_rb_flush_point_count(rb),
884 queued_entries);
886 return offset;