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