]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/lightnvm/pblk-rb.c
lightnvm: pblk: add SPDX license tag
[linux.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 /*
173  * Buffer count is calculated with respect to the submission entry signaling the
174  * entries that are available to send to the media
175  */
176 unsigned int pblk_rb_read_count(struct pblk_rb *rb)
177 {
178         unsigned int mem = READ_ONCE(rb->mem);
179         unsigned int subm = READ_ONCE(rb->subm);
180
181         return pblk_rb_ring_count(mem, subm, rb->nr_entries);
182 }
183
184 unsigned int pblk_rb_sync_count(struct pblk_rb *rb)
185 {
186         unsigned int mem = READ_ONCE(rb->mem);
187         unsigned int sync = READ_ONCE(rb->sync);
188
189         return pblk_rb_ring_count(mem, sync, rb->nr_entries);
190 }
191
192 unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries)
193 {
194         unsigned int subm;
195
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));
200
201         return subm;
202 }
203
204 static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int to_update)
205 {
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;
213
214         for (i = 0; i < to_update; i++) {
215                 entry = &rb->entries[rb->l2p_update];
216                 w_ctx = &entry->w_ctx;
217
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");
225
226                 pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa,
227                                                         entry->cacheline);
228
229                 line = pblk_ppa_to_line(pblk, 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);
233         }
234
235         pblk_rl_out(&pblk->rl, user_io, gc_io);
236
237         return 0;
238 }
239
240 /*
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.
244  */
245 static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries,
246                               unsigned int mem, unsigned int sync)
247 {
248         unsigned int space, count;
249         int ret = 0;
250
251         lockdep_assert_held(&rb->w_lock);
252
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;
257
258         count = nr_entries - space;
259         /* l2p_update used exclusively under rb->w_lock */
260         ret = __pblk_rb_update_l2p(rb, count);
261
262 out:
263         return ret;
264 }
265
266 /*
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.
270  */
271 void pblk_rb_sync_l2p(struct pblk_rb *rb)
272 {
273         unsigned int sync;
274         unsigned int to_update;
275
276         spin_lock(&rb->w_lock);
277
278         /* Protect from reads and writes */
279         sync = smp_load_acquire(&rb->sync);
280
281         to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries);
282         __pblk_rb_update_l2p(rb, to_update);
283
284         spin_unlock(&rb->w_lock);
285 }
286
287 /*
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.
291  *
292  */
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)
296 {
297         memcpy(entry->data, data, rb->seg_size);
298
299         entry->w_ctx.lba = w_ctx.lba;
300         entry->w_ctx.ppa = w_ctx.ppa;
301 }
302
303 void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data,
304                               struct pblk_w_ctx w_ctx, unsigned int ring_pos)
305 {
306         struct pblk *pblk = container_of(rb, struct pblk, rwb);
307         struct pblk_rb_entry *entry;
308         int flags;
309
310         entry = &rb->entries[ring_pos];
311         flags = READ_ONCE(entry->w_ctx.flags);
312 #ifdef CONFIG_NVM_PBLK_DEBUG
313         /* Caller must guarantee that the entry is free */
314         BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
315 #endif
316
317         __pblk_rb_write_entry(rb, data, w_ctx, entry);
318
319         pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline);
320         flags = w_ctx.flags | PBLK_WRITTEN_DATA;
321
322         /* Release flags on write context. Protect from writes */
323         smp_store_release(&entry->w_ctx.flags, flags);
324 }
325
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)
329 {
330         struct pblk *pblk = container_of(rb, struct pblk, rwb);
331         struct pblk_rb_entry *entry;
332         int flags;
333
334         entry = &rb->entries[ring_pos];
335         flags = READ_ONCE(entry->w_ctx.flags);
336 #ifdef CONFIG_NVM_PBLK_DEBUG
337         /* Caller must guarantee that the entry is free */
338         BUG_ON(!(flags & PBLK_WRITABLE_ENTRY));
339 #endif
340
341         __pblk_rb_write_entry(rb, data, w_ctx, entry);
342
343         if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, line, paddr))
344                 entry->w_ctx.lba = ADDR_EMPTY;
345
346         flags = w_ctx.flags | PBLK_WRITTEN_DATA;
347
348         /* Release flags on write context. Protect from writes */
349         smp_store_release(&entry->w_ctx.flags, flags);
350 }
351
352 static int pblk_rb_flush_point_set(struct pblk_rb *rb, struct bio *bio,
353                                    unsigned int pos)
354 {
355         struct pblk_rb_entry *entry;
356         unsigned int sync, flush_point;
357
358         pblk_rb_sync_init(rb, NULL);
359         sync = READ_ONCE(rb->sync);
360
361         if (pos == sync) {
362                 pblk_rb_sync_end(rb, NULL);
363                 return 0;
364         }
365
366 #ifdef CONFIG_NVM_PBLK_DEBUG
367         atomic_inc(&rb->inflight_flush_point);
368 #endif
369
370         flush_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1);
371         entry = &rb->entries[flush_point];
372
373         /* Protect flush points */
374         smp_store_release(&rb->flush_point, flush_point);
375
376         if (bio)
377                 bio_list_add(&entry->w_ctx.bios, bio);
378
379         pblk_rb_sync_end(rb, NULL);
380
381         return bio ? 1 : 0;
382 }
383
384 static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
385                                unsigned int *pos)
386 {
387         unsigned int mem;
388         unsigned int sync;
389
390         sync = READ_ONCE(rb->sync);
391         mem = READ_ONCE(rb->mem);
392
393         if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries)
394                 return 0;
395
396         if (pblk_rb_update_l2p(rb, nr_entries, mem, sync))
397                 return 0;
398
399         *pos = mem;
400
401         return 1;
402 }
403
404 static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries,
405                              unsigned int *pos)
406 {
407         if (!__pblk_rb_may_write(rb, nr_entries, pos))
408                 return 0;
409
410         /* Protect from read count */
411         smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1));
412         return 1;
413 }
414
415 void pblk_rb_flush(struct pblk_rb *rb)
416 {
417         struct pblk *pblk = container_of(rb, struct pblk, rwb);
418         unsigned int mem = READ_ONCE(rb->mem);
419
420         if (pblk_rb_flush_point_set(rb, NULL, mem))
421                 return;
422
423         pblk_write_kick(pblk);
424 }
425
426 static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries,
427                                    unsigned int *pos, struct bio *bio,
428                                    int *io_ret)
429 {
430         unsigned int mem;
431
432         if (!__pblk_rb_may_write(rb, nr_entries, pos))
433                 return 0;
434
435         mem = (*pos + nr_entries) & (rb->nr_entries - 1);
436         *io_ret = NVM_IO_DONE;
437
438         if (bio->bi_opf & REQ_PREFLUSH) {
439                 struct pblk *pblk = container_of(rb, struct pblk, rwb);
440
441                 atomic64_inc(&pblk->nr_flush);
442                 if (pblk_rb_flush_point_set(&pblk->rwb, bio, mem))
443                         *io_ret = NVM_IO_OK;
444         }
445
446         /* Protect from read count */
447         smp_store_release(&rb->mem, mem);
448
449         return 1;
450 }
451
452 /*
453  * Atomically check that (i) there is space on the write buffer for the
454  * incoming I/O, and (ii) the current I/O type has enough budget in the write
455  * buffer (rate-limiter).
456  */
457 int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio,
458                            unsigned int nr_entries, unsigned int *pos)
459 {
460         struct pblk *pblk = container_of(rb, struct pblk, rwb);
461         int io_ret;
462
463         spin_lock(&rb->w_lock);
464         io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries);
465         if (io_ret) {
466                 spin_unlock(&rb->w_lock);
467                 return io_ret;
468         }
469
470         if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) {
471                 spin_unlock(&rb->w_lock);
472                 return NVM_IO_REQUEUE;
473         }
474
475         pblk_rl_user_in(&pblk->rl, nr_entries);
476         spin_unlock(&rb->w_lock);
477
478         return io_ret;
479 }
480
481 /*
482  * Look at pblk_rb_may_write_user comment
483  */
484 int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries,
485                          unsigned int *pos)
486 {
487         struct pblk *pblk = container_of(rb, struct pblk, rwb);
488
489         spin_lock(&rb->w_lock);
490         if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) {
491                 spin_unlock(&rb->w_lock);
492                 return 0;
493         }
494
495         if (!pblk_rb_may_write(rb, nr_entries, pos)) {
496                 spin_unlock(&rb->w_lock);
497                 return 0;
498         }
499
500         pblk_rl_gc_in(&pblk->rl, nr_entries);
501         spin_unlock(&rb->w_lock);
502
503         return 1;
504 }
505
506 /*
507  * Read available entries on rb and add them to the given bio. To avoid a memory
508  * copy, a page reference to the write buffer is used to be added to the bio.
509  *
510  * This function is used by the write thread to form the write bio that will
511  * persist data on the write buffer to the media.
512  */
513 unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd,
514                                  unsigned int pos, unsigned int nr_entries,
515                                  unsigned int count)
516 {
517         struct pblk *pblk = container_of(rb, struct pblk, rwb);
518         struct request_queue *q = pblk->dev->q;
519         struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
520         struct bio *bio = rqd->bio;
521         struct pblk_rb_entry *entry;
522         struct page *page;
523         unsigned int pad = 0, to_read = nr_entries;
524         unsigned int i;
525         int flags;
526
527         if (count < nr_entries) {
528                 pad = nr_entries - count;
529                 to_read = count;
530         }
531
532         c_ctx->sentry = pos;
533         c_ctx->nr_valid = to_read;
534         c_ctx->nr_padded = pad;
535
536         for (i = 0; i < to_read; i++) {
537                 entry = &rb->entries[pos];
538
539                 /* A write has been allowed into the buffer, but data is still
540                  * being copied to it. It is ok to busy wait.
541                  */
542 try:
543                 flags = READ_ONCE(entry->w_ctx.flags);
544                 if (!(flags & PBLK_WRITTEN_DATA)) {
545                         io_schedule();
546                         goto try;
547                 }
548
549                 page = virt_to_page(entry->data);
550                 if (!page) {
551                         pblk_err(pblk, "could not allocate write bio page\n");
552                         flags &= ~PBLK_WRITTEN_DATA;
553                         flags |= PBLK_SUBMITTED_ENTRY;
554                         /* Release flags on context. Protect from writes */
555                         smp_store_release(&entry->w_ctx.flags, flags);
556                         return NVM_IO_ERR;
557                 }
558
559                 if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) !=
560                                                                 rb->seg_size) {
561                         pblk_err(pblk, "could not add page to write bio\n");
562                         flags &= ~PBLK_WRITTEN_DATA;
563                         flags |= PBLK_SUBMITTED_ENTRY;
564                         /* Release flags on context. Protect from writes */
565                         smp_store_release(&entry->w_ctx.flags, flags);
566                         return NVM_IO_ERR;
567                 }
568
569                 flags &= ~PBLK_WRITTEN_DATA;
570                 flags |= PBLK_SUBMITTED_ENTRY;
571
572                 /* Release flags on context. Protect from writes */
573                 smp_store_release(&entry->w_ctx.flags, flags);
574
575                 pos = (pos + 1) & (rb->nr_entries - 1);
576         }
577
578         if (pad) {
579                 if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) {
580                         pblk_err(pblk, "could not pad page in write bio\n");
581                         return NVM_IO_ERR;
582                 }
583
584                 if (pad < pblk->min_write_pgs)
585                         atomic64_inc(&pblk->pad_dist[pad - 1]);
586                 else
587                         pblk_warn(pblk, "padding more than min. sectors\n");
588
589                 atomic64_add(pad, &pblk->pad_wa);
590         }
591
592 #ifdef CONFIG_NVM_PBLK_DEBUG
593         atomic_long_add(pad, &pblk->padded_writes);
594 #endif
595
596         return NVM_IO_OK;
597 }
598
599 /*
600  * Copy to bio only if the lba matches the one on the given cache entry.
601  * Otherwise, it means that the entry has been overwritten, and the bio should
602  * be directed to disk.
603  */
604 int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba,
605                         struct ppa_addr ppa, int bio_iter, bool advanced_bio)
606 {
607         struct pblk *pblk = container_of(rb, struct pblk, rwb);
608         struct pblk_rb_entry *entry;
609         struct pblk_w_ctx *w_ctx;
610         struct ppa_addr l2p_ppa;
611         u64 pos = pblk_addr_to_cacheline(ppa);
612         void *data;
613         int flags;
614         int ret = 1;
615
616
617 #ifdef CONFIG_NVM_PBLK_DEBUG
618         /* Caller must ensure that the access will not cause an overflow */
619         BUG_ON(pos >= rb->nr_entries);
620 #endif
621         entry = &rb->entries[pos];
622         w_ctx = &entry->w_ctx;
623         flags = READ_ONCE(w_ctx->flags);
624
625         spin_lock(&rb->w_lock);
626         spin_lock(&pblk->trans_lock);
627         l2p_ppa = pblk_trans_map_get(pblk, lba);
628         spin_unlock(&pblk->trans_lock);
629
630         /* Check if the entry has been overwritten or is scheduled to be */
631         if (!pblk_ppa_comp(l2p_ppa, ppa) || w_ctx->lba != lba ||
632                                                 flags & PBLK_WRITABLE_ENTRY) {
633                 ret = 0;
634                 goto out;
635         }
636
637         /* Only advance the bio if it hasn't been advanced already. If advanced,
638          * this bio is at least a partial bio (i.e., it has partially been
639          * filled with data from the cache). If part of the data resides on the
640          * media, we will read later on
641          */
642         if (unlikely(!advanced_bio))
643                 bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE);
644
645         data = bio_data(bio);
646         memcpy(data, entry->data, rb->seg_size);
647
648 out:
649         spin_unlock(&rb->w_lock);
650         return ret;
651 }
652
653 struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos)
654 {
655         unsigned int entry = pos & (rb->nr_entries - 1);
656
657         return &rb->entries[entry].w_ctx;
658 }
659
660 unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags)
661         __acquires(&rb->s_lock)
662 {
663         if (flags)
664                 spin_lock_irqsave(&rb->s_lock, *flags);
665         else
666                 spin_lock_irq(&rb->s_lock);
667
668         return rb->sync;
669 }
670
671 void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags)
672         __releases(&rb->s_lock)
673 {
674         lockdep_assert_held(&rb->s_lock);
675
676         if (flags)
677                 spin_unlock_irqrestore(&rb->s_lock, *flags);
678         else
679                 spin_unlock_irq(&rb->s_lock);
680 }
681
682 unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries)
683 {
684         unsigned int sync, flush_point;
685         lockdep_assert_held(&rb->s_lock);
686
687         sync = READ_ONCE(rb->sync);
688         flush_point = READ_ONCE(rb->flush_point);
689
690         if (flush_point != EMPTY_ENTRY) {
691                 unsigned int secs_to_flush;
692
693                 secs_to_flush = pblk_rb_ring_count(flush_point, sync,
694                                         rb->nr_entries);
695                 if (secs_to_flush < nr_entries) {
696                         /* Protect flush points */
697                         smp_store_release(&rb->flush_point, EMPTY_ENTRY);
698                 }
699         }
700
701         sync = (sync + nr_entries) & (rb->nr_entries - 1);
702
703         /* Protect from counts */
704         smp_store_release(&rb->sync, sync);
705
706         return sync;
707 }
708
709 /* Calculate how many sectors to submit up to the current flush point. */
710 unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb)
711 {
712         unsigned int subm, sync, flush_point;
713         unsigned int submitted, to_flush;
714
715         /* Protect flush points */
716         flush_point = smp_load_acquire(&rb->flush_point);
717         if (flush_point == EMPTY_ENTRY)
718                 return 0;
719
720         /* Protect syncs */
721         sync = smp_load_acquire(&rb->sync);
722
723         subm = READ_ONCE(rb->subm);
724         submitted = pblk_rb_ring_count(subm, sync, rb->nr_entries);
725
726         /* The sync point itself counts as a sector to sync */
727         to_flush = pblk_rb_ring_count(flush_point, sync, rb->nr_entries) + 1;
728
729         return (submitted < to_flush) ? (to_flush - submitted) : 0;
730 }
731
732 /*
733  * Scan from the current position of the sync pointer to find the entry that
734  * corresponds to the given ppa. This is necessary since write requests can be
735  * completed out of order. The assumption is that the ppa is close to the sync
736  * pointer thus the search will not take long.
737  *
738  * The caller of this function must guarantee that the sync pointer will no
739  * reach the entry while it is using the metadata associated with it. With this
740  * assumption in mind, there is no need to take the sync lock.
741  */
742 struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb,
743                                               struct ppa_addr *ppa)
744 {
745         unsigned int sync, subm, count;
746         unsigned int i;
747
748         sync = READ_ONCE(rb->sync);
749         subm = READ_ONCE(rb->subm);
750         count = pblk_rb_ring_count(subm, sync, rb->nr_entries);
751
752         for (i = 0; i < count; i++)
753                 sync = (sync + 1) & (rb->nr_entries - 1);
754
755         return NULL;
756 }
757
758 int pblk_rb_tear_down_check(struct pblk_rb *rb)
759 {
760         struct pblk_rb_entry *entry;
761         int i;
762         int ret = 0;
763
764         spin_lock(&rb->w_lock);
765         spin_lock_irq(&rb->s_lock);
766
767         if ((rb->mem == rb->subm) && (rb->subm == rb->sync) &&
768                                 (rb->sync == rb->l2p_update) &&
769                                 (rb->flush_point == EMPTY_ENTRY)) {
770                 goto out;
771         }
772
773         if (!rb->entries) {
774                 ret = 1;
775                 goto out;
776         }
777
778         for (i = 0; i < rb->nr_entries; i++) {
779                 entry = &rb->entries[i];
780
781                 if (!entry->data) {
782                         ret = 1;
783                         goto out;
784                 }
785         }
786
787 out:
788         spin_unlock(&rb->w_lock);
789         spin_unlock_irq(&rb->s_lock);
790
791         return ret;
792 }
793
794 unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos)
795 {
796         return (pos & (rb->nr_entries - 1));
797 }
798
799 int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos)
800 {
801         return (pos >= rb->nr_entries);
802 }
803
804 ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf)
805 {
806         struct pblk *pblk = container_of(rb, struct pblk, rwb);
807         struct pblk_c_ctx *c;
808         ssize_t offset;
809         int queued_entries = 0;
810
811         spin_lock_irq(&rb->s_lock);
812         list_for_each_entry(c, &pblk->compl_list, list)
813                 queued_entries++;
814         spin_unlock_irq(&rb->s_lock);
815
816         if (rb->flush_point != EMPTY_ENTRY)
817                 offset = scnprintf(buf, PAGE_SIZE,
818                         "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n",
819                         rb->nr_entries,
820                         rb->mem,
821                         rb->subm,
822                         rb->sync,
823                         rb->l2p_update,
824 #ifdef CONFIG_NVM_PBLK_DEBUG
825                         atomic_read(&rb->inflight_flush_point),
826 #else
827                         0,
828 #endif
829                         rb->flush_point,
830                         pblk_rb_read_count(rb),
831                         pblk_rb_space(rb),
832                         pblk_rb_flush_point_count(rb),
833                         queued_entries);
834         else
835                 offset = scnprintf(buf, PAGE_SIZE,
836                         "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n",
837                         rb->nr_entries,
838                         rb->mem,
839                         rb->subm,
840                         rb->sync,
841                         rb->l2p_update,
842 #ifdef CONFIG_NVM_PBLK_DEBUG
843                         atomic_read(&rb->inflight_flush_point),
844 #else
845                         0,
846 #endif
847                         pblk_rb_read_count(rb),
848                         pblk_rb_space(rb),
849                         pblk_rb_flush_point_count(rb),
850                         queued_entries);
851
852         return offset;
853 }