]>
Commit | Line | Data |
---|---|---|
a4bd217b JG |
1 | /* |
2 | * Copyright (C) 2016 CNEX Labs | |
3 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> | |
4 | * | |
5 | * Based upon the circular ringbuffer. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * pblk-rb.c - pblk's write buffer | |
17 | */ | |
18 | ||
19 | #include <linux/circ_buf.h> | |
20 | ||
21 | #include "pblk.h" | |
22 | ||
23 | static DECLARE_RWSEM(pblk_rb_lock); | |
24 | ||
25 | void pblk_rb_data_free(struct pblk_rb *rb) | |
26 | { | |
27 | struct pblk_rb_pages *p, *t; | |
28 | ||
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); | |
34 | } | |
35 | up_write(&pblk_rb_lock); | |
36 | } | |
37 | ||
38 | /* | |
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) | |
42 | */ | |
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) | |
45 | { | |
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; | |
51 | ||
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->sync_point = EMPTY_ENTRY; | |
58 | ||
59 | spin_lock_init(&rb->w_lock); | |
60 | spin_lock_init(&rb->s_lock); | |
61 | ||
62 | INIT_LIST_HEAD(&rb->pages); | |
63 | ||
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; | |
70 | } | |
71 | ||
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; | |
78 | ||
79 | page_set = kmalloc(sizeof(struct pblk_rb_pages), GFP_KERNEL); | |
80 | if (!page_set) { | |
81 | up_write(&pblk_rb_lock); | |
82 | return -ENOMEM; | |
83 | } | |
84 | ||
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; | |
92 | } | |
93 | kaddr = page_address(page_set->pages); | |
94 | ||
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; | |
99 | ||
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); | |
107 | } | |
108 | ||
109 | list_add_tail(&page_set->list, &rb->pages); | |
110 | iter--; | |
111 | } while (iter > 0); | |
112 | up_write(&pblk_rb_lock); | |
113 | ||
114 | #ifdef CONFIG_NVM_DEBUG | |
115 | atomic_set(&rb->inflight_sync_point, 0); | |
116 | #endif | |
117 | ||
118 | /* | |
119 | * Initialize rate-limiter, which controls access to the write buffer | |
120 | * but user and GC I/O | |
121 | */ | |
122 | pblk_rl_init(&pblk->rl, rb->nr_entries); | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | /* | |
128 | * pblk_rb_calculate_size -- calculate the size of the write buffer | |
129 | */ | |
130 | unsigned int pblk_rb_calculate_size(unsigned int nr_entries) | |
131 | { | |
132 | /* Alloc a write buffer that can at least fit 128 entries */ | |
133 | return (1 << max(get_count_order(nr_entries), 7)); | |
134 | } | |
135 | ||
136 | void *pblk_rb_entries_ref(struct pblk_rb *rb) | |
137 | { | |
138 | return rb->entries; | |
139 | } | |
140 | ||
141 | static void clean_wctx(struct pblk_w_ctx *w_ctx) | |
142 | { | |
143 | int flags; | |
144 | ||
145 | try: | |
146 | flags = READ_ONCE(w_ctx->flags); | |
147 | if (!(flags & PBLK_SUBMITTED_ENTRY)) | |
148 | goto try; | |
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 | } | |
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 | /* | |
160 | * Buffer space is calculated with respect to the back pointer signaling | |
161 | * synchronized entries to the media. | |
162 | */ | |
163 | static unsigned int pblk_rb_space(struct pblk_rb *rb) | |
164 | { | |
165 | unsigned int mem = READ_ONCE(rb->mem); | |
166 | unsigned int sync = READ_ONCE(rb->sync); | |
167 | ||
168 | return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries); | |
169 | } | |
170 | ||
171 | /* | |
172 | * Buffer count is calculated with respect to the submission entry signaling the | |
173 | * entries that are available to send to the media | |
174 | */ | |
175 | unsigned int pblk_rb_read_count(struct pblk_rb *rb) | |
176 | { | |
177 | unsigned int mem = READ_ONCE(rb->mem); | |
178 | unsigned int subm = READ_ONCE(rb->subm); | |
179 | ||
180 | return pblk_rb_ring_count(mem, subm, rb->nr_entries); | |
181 | } | |
182 | ||
183 | unsigned int pblk_rb_read_commit(struct pblk_rb *rb, unsigned int nr_entries) | |
184 | { | |
185 | unsigned int subm; | |
186 | ||
187 | subm = READ_ONCE(rb->subm); | |
188 | /* Commit read means updating submission pointer */ | |
189 | smp_store_release(&rb->subm, | |
190 | (subm + nr_entries) & (rb->nr_entries - 1)); | |
191 | ||
192 | return subm; | |
193 | } | |
194 | ||
195 | static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int *l2p_upd, | |
196 | unsigned int to_update) | |
197 | { | |
198 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
199 | struct pblk_line *line; | |
200 | struct pblk_rb_entry *entry; | |
201 | struct pblk_w_ctx *w_ctx; | |
b20ba1bc | 202 | unsigned int user_io = 0, gc_io = 0; |
a4bd217b | 203 | unsigned int i; |
b20ba1bc | 204 | int flags; |
a4bd217b JG |
205 | |
206 | for (i = 0; i < to_update; i++) { | |
207 | entry = &rb->entries[*l2p_upd]; | |
208 | w_ctx = &entry->w_ctx; | |
209 | ||
b20ba1bc JG |
210 | flags = READ_ONCE(entry->w_ctx.flags); |
211 | if (flags & PBLK_IOTYPE_USER) | |
212 | user_io++; | |
213 | else if (flags & PBLK_IOTYPE_GC) | |
214 | gc_io++; | |
215 | else | |
216 | WARN(1, "pblk: unknown IO type\n"); | |
217 | ||
a4bd217b JG |
218 | pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa, |
219 | entry->cacheline); | |
220 | ||
221 | line = &pblk->lines[pblk_tgt_ppa_to_line(w_ctx->ppa)]; | |
222 | kref_put(&line->ref, pblk_line_put); | |
223 | clean_wctx(w_ctx); | |
224 | *l2p_upd = (*l2p_upd + 1) & (rb->nr_entries - 1); | |
225 | } | |
226 | ||
b20ba1bc JG |
227 | pblk_rl_out(&pblk->rl, user_io, gc_io); |
228 | ||
a4bd217b JG |
229 | return 0; |
230 | } | |
231 | ||
232 | /* | |
233 | * When we move the l2p_update pointer, we update the l2p table - lookups will | |
234 | * point to the physical address instead of to the cacheline in the write buffer | |
235 | * from this moment on. | |
236 | */ | |
237 | static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries, | |
238 | unsigned int mem, unsigned int sync) | |
239 | { | |
240 | unsigned int space, count; | |
241 | int ret = 0; | |
242 | ||
243 | lockdep_assert_held(&rb->w_lock); | |
244 | ||
245 | /* Update l2p only as buffer entries are being overwritten */ | |
246 | space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries); | |
247 | if (space > nr_entries) | |
248 | goto out; | |
249 | ||
250 | count = nr_entries - space; | |
251 | /* l2p_update used exclusively under rb->w_lock */ | |
252 | ret = __pblk_rb_update_l2p(rb, &rb->l2p_update, count); | |
253 | ||
254 | out: | |
255 | return ret; | |
256 | } | |
257 | ||
258 | /* | |
259 | * Update the l2p entry for all sectors stored on the write buffer. This means | |
260 | * that all future lookups to the l2p table will point to a device address, not | |
261 | * to the cacheline in the write buffer. | |
262 | */ | |
263 | void pblk_rb_sync_l2p(struct pblk_rb *rb) | |
264 | { | |
265 | unsigned int sync; | |
266 | unsigned int to_update; | |
267 | ||
268 | spin_lock(&rb->w_lock); | |
269 | ||
270 | /* Protect from reads and writes */ | |
271 | sync = smp_load_acquire(&rb->sync); | |
272 | ||
273 | to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries); | |
274 | __pblk_rb_update_l2p(rb, &rb->l2p_update, to_update); | |
275 | ||
276 | spin_unlock(&rb->w_lock); | |
277 | } | |
278 | ||
279 | /* | |
280 | * Write @nr_entries to ring buffer from @data buffer if there is enough space. | |
281 | * Typically, 4KB data chunks coming from a bio will be copied to the ring | |
282 | * buffer, thus the write will fail if not all incoming data can be copied. | |
283 | * | |
284 | */ | |
285 | static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data, | |
286 | struct pblk_w_ctx w_ctx, | |
287 | struct pblk_rb_entry *entry) | |
288 | { | |
289 | memcpy(entry->data, data, rb->seg_size); | |
290 | ||
291 | entry->w_ctx.lba = w_ctx.lba; | |
292 | entry->w_ctx.ppa = w_ctx.ppa; | |
293 | } | |
294 | ||
295 | void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data, | |
296 | struct pblk_w_ctx w_ctx, unsigned int ring_pos) | |
297 | { | |
298 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
299 | struct pblk_rb_entry *entry; | |
300 | int flags; | |
301 | ||
302 | entry = &rb->entries[ring_pos]; | |
303 | flags = READ_ONCE(entry->w_ctx.flags); | |
304 | #ifdef CONFIG_NVM_DEBUG | |
305 | /* Caller must guarantee that the entry is free */ | |
306 | BUG_ON(!(flags & PBLK_WRITABLE_ENTRY)); | |
307 | #endif | |
308 | ||
309 | __pblk_rb_write_entry(rb, data, w_ctx, entry); | |
310 | ||
311 | pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline); | |
312 | flags = w_ctx.flags | PBLK_WRITTEN_DATA; | |
313 | ||
314 | /* Release flags on write context. Protect from writes */ | |
315 | smp_store_release(&entry->w_ctx.flags, flags); | |
316 | } | |
317 | ||
318 | void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data, | |
319 | struct pblk_w_ctx w_ctx, struct pblk_line *gc_line, | |
320 | unsigned int ring_pos) | |
321 | { | |
322 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
323 | struct pblk_rb_entry *entry; | |
324 | int flags; | |
325 | ||
326 | entry = &rb->entries[ring_pos]; | |
327 | flags = READ_ONCE(entry->w_ctx.flags); | |
328 | #ifdef CONFIG_NVM_DEBUG | |
329 | /* Caller must guarantee that the entry is free */ | |
330 | BUG_ON(!(flags & PBLK_WRITABLE_ENTRY)); | |
331 | #endif | |
332 | ||
333 | __pblk_rb_write_entry(rb, data, w_ctx, entry); | |
334 | ||
335 | if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, gc_line)) | |
336 | entry->w_ctx.lba = ADDR_EMPTY; | |
337 | ||
338 | flags = w_ctx.flags | PBLK_WRITTEN_DATA; | |
339 | ||
340 | /* Release flags on write context. Protect from writes */ | |
341 | smp_store_release(&entry->w_ctx.flags, flags); | |
342 | } | |
343 | ||
344 | static int pblk_rb_sync_point_set(struct pblk_rb *rb, struct bio *bio, | |
345 | unsigned int pos) | |
346 | { | |
347 | struct pblk_rb_entry *entry; | |
348 | unsigned int subm, sync_point; | |
349 | int flags; | |
350 | ||
351 | subm = READ_ONCE(rb->subm); | |
352 | ||
353 | #ifdef CONFIG_NVM_DEBUG | |
354 | atomic_inc(&rb->inflight_sync_point); | |
355 | #endif | |
356 | ||
357 | if (pos == subm) | |
358 | return 0; | |
359 | ||
360 | sync_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1); | |
361 | entry = &rb->entries[sync_point]; | |
362 | ||
363 | flags = READ_ONCE(entry->w_ctx.flags); | |
364 | flags |= PBLK_FLUSH_ENTRY; | |
365 | ||
366 | /* Release flags on context. Protect from writes */ | |
367 | smp_store_release(&entry->w_ctx.flags, flags); | |
368 | ||
369 | /* Protect syncs */ | |
370 | smp_store_release(&rb->sync_point, sync_point); | |
371 | ||
588726d3 JG |
372 | if (!bio) |
373 | return 0; | |
374 | ||
a4bd217b JG |
375 | spin_lock_irq(&rb->s_lock); |
376 | bio_list_add(&entry->w_ctx.bios, bio); | |
377 | spin_unlock_irq(&rb->s_lock); | |
378 | ||
379 | return 1; | |
380 | } | |
381 | ||
382 | static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries, | |
383 | unsigned int *pos) | |
384 | { | |
385 | unsigned int mem; | |
386 | unsigned int sync; | |
387 | ||
388 | sync = READ_ONCE(rb->sync); | |
389 | mem = READ_ONCE(rb->mem); | |
390 | ||
391 | if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries) | |
392 | return 0; | |
393 | ||
394 | if (pblk_rb_update_l2p(rb, nr_entries, mem, sync)) | |
395 | return 0; | |
396 | ||
397 | *pos = mem; | |
398 | ||
399 | return 1; | |
400 | } | |
401 | ||
402 | static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries, | |
403 | unsigned int *pos) | |
404 | { | |
405 | if (!__pblk_rb_may_write(rb, nr_entries, pos)) | |
406 | return 0; | |
407 | ||
408 | /* Protect from read count */ | |
409 | smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1)); | |
410 | return 1; | |
411 | } | |
412 | ||
588726d3 JG |
413 | void pblk_rb_flush(struct pblk_rb *rb) |
414 | { | |
415 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
416 | unsigned int mem = READ_ONCE(rb->mem); | |
417 | ||
418 | if (pblk_rb_sync_point_set(rb, NULL, mem)) | |
419 | return; | |
420 | ||
421 | pblk_write_should_kick(pblk); | |
422 | } | |
423 | ||
a4bd217b JG |
424 | static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries, |
425 | unsigned int *pos, struct bio *bio, | |
426 | int *io_ret) | |
427 | { | |
428 | unsigned int mem; | |
429 | ||
430 | if (!__pblk_rb_may_write(rb, nr_entries, pos)) | |
431 | return 0; | |
432 | ||
433 | mem = (*pos + nr_entries) & (rb->nr_entries - 1); | |
434 | *io_ret = NVM_IO_DONE; | |
435 | ||
436 | if (bio->bi_opf & REQ_PREFLUSH) { | |
437 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
438 | ||
439 | #ifdef CONFIG_NVM_DEBUG | |
440 | atomic_long_inc(&pblk->nr_flush); | |
441 | #endif | |
442 | if (pblk_rb_sync_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 | return 1; | |
449 | } | |
450 | ||
451 | /* | |
452 | * Atomically check that (i) there is space on the write buffer for the | |
453 | * incoming I/O, and (ii) the current I/O type has enough budget in the write | |
454 | * buffer (rate-limiter). | |
455 | */ | |
456 | int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio, | |
457 | unsigned int nr_entries, unsigned int *pos) | |
458 | { | |
459 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
588726d3 | 460 | int io_ret; |
a4bd217b JG |
461 | |
462 | spin_lock(&rb->w_lock); | |
588726d3 JG |
463 | io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries); |
464 | if (io_ret) { | |
a4bd217b | 465 | spin_unlock(&rb->w_lock); |
588726d3 | 466 | return io_ret; |
a4bd217b JG |
467 | } |
468 | ||
588726d3 | 469 | if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) { |
a4bd217b JG |
470 | spin_unlock(&rb->w_lock); |
471 | return NVM_IO_REQUEUE; | |
472 | } | |
473 | ||
474 | pblk_rl_user_in(&pblk->rl, nr_entries); | |
475 | spin_unlock(&rb->w_lock); | |
476 | ||
588726d3 | 477 | return io_ret; |
a4bd217b JG |
478 | } |
479 | ||
480 | /* | |
481 | * Look at pblk_rb_may_write_user comment | |
482 | */ | |
483 | int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries, | |
484 | unsigned int *pos) | |
485 | { | |
486 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
487 | ||
488 | spin_lock(&rb->w_lock); | |
489 | if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) { | |
490 | spin_unlock(&rb->w_lock); | |
491 | return 0; | |
492 | } | |
493 | ||
494 | if (!pblk_rb_may_write(rb, nr_entries, pos)) { | |
495 | spin_unlock(&rb->w_lock); | |
496 | return 0; | |
497 | } | |
498 | ||
499 | pblk_rl_gc_in(&pblk->rl, nr_entries); | |
500 | spin_unlock(&rb->w_lock); | |
501 | ||
502 | return 1; | |
503 | } | |
504 | ||
505 | /* | |
506 | * The caller of this function must ensure that the backpointer will not | |
507 | * overwrite the entries passed on the list. | |
508 | */ | |
509 | unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio, | |
510 | struct list_head *list, | |
511 | unsigned int max) | |
512 | { | |
513 | struct pblk_rb_entry *entry, *tentry; | |
514 | struct page *page; | |
515 | unsigned int read = 0; | |
516 | int ret; | |
517 | ||
518 | list_for_each_entry_safe(entry, tentry, list, index) { | |
519 | if (read > max) { | |
520 | pr_err("pblk: too many entries on list\n"); | |
521 | goto out; | |
522 | } | |
523 | ||
524 | page = virt_to_page(entry->data); | |
525 | if (!page) { | |
526 | pr_err("pblk: could not allocate write bio page\n"); | |
527 | goto out; | |
528 | } | |
529 | ||
530 | ret = bio_add_page(bio, page, rb->seg_size, 0); | |
531 | if (ret != rb->seg_size) { | |
532 | pr_err("pblk: could not add page to write bio\n"); | |
533 | goto out; | |
534 | } | |
535 | ||
536 | list_del(&entry->index); | |
537 | read++; | |
538 | } | |
539 | ||
540 | out: | |
541 | return read; | |
542 | } | |
543 | ||
544 | /* | |
545 | * Read available entries on rb and add them to the given bio. To avoid a memory | |
546 | * copy, a page reference to the write buffer is used to be added to the bio. | |
547 | * | |
548 | * This function is used by the write thread to form the write bio that will | |
549 | * persist data on the write buffer to the media. | |
550 | */ | |
d624f371 JG |
551 | unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd, |
552 | struct bio *bio, unsigned int pos, | |
553 | unsigned int nr_entries, unsigned int count) | |
a4bd217b JG |
554 | { |
555 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
d624f371 JG |
556 | struct request_queue *q = pblk->dev->q; |
557 | struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd); | |
a4bd217b JG |
558 | struct pblk_rb_entry *entry; |
559 | struct page *page; | |
d624f371 | 560 | unsigned int pad = 0, to_read = nr_entries; |
a4bd217b JG |
561 | unsigned int i; |
562 | int flags; | |
a4bd217b JG |
563 | |
564 | if (count < nr_entries) { | |
565 | pad = nr_entries - count; | |
566 | to_read = count; | |
567 | } | |
568 | ||
569 | c_ctx->sentry = pos; | |
570 | c_ctx->nr_valid = to_read; | |
571 | c_ctx->nr_padded = pad; | |
572 | ||
573 | for (i = 0; i < to_read; i++) { | |
574 | entry = &rb->entries[pos]; | |
575 | ||
576 | /* A write has been allowed into the buffer, but data is still | |
577 | * being copied to it. It is ok to busy wait. | |
578 | */ | |
579 | try: | |
580 | flags = READ_ONCE(entry->w_ctx.flags); | |
10888129 JG |
581 | if (!(flags & PBLK_WRITTEN_DATA)) { |
582 | io_schedule(); | |
a4bd217b | 583 | goto try; |
10888129 | 584 | } |
a4bd217b | 585 | |
a4bd217b JG |
586 | page = virt_to_page(entry->data); |
587 | if (!page) { | |
588 | pr_err("pblk: could not allocate write bio page\n"); | |
589 | flags &= ~PBLK_WRITTEN_DATA; | |
590 | flags |= PBLK_SUBMITTED_ENTRY; | |
591 | /* Release flags on context. Protect from writes */ | |
592 | smp_store_release(&entry->w_ctx.flags, flags); | |
d624f371 | 593 | return NVM_IO_ERR; |
a4bd217b JG |
594 | } |
595 | ||
d624f371 JG |
596 | if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) != |
597 | rb->seg_size) { | |
a4bd217b JG |
598 | pr_err("pblk: could not add page to write bio\n"); |
599 | flags &= ~PBLK_WRITTEN_DATA; | |
600 | flags |= PBLK_SUBMITTED_ENTRY; | |
601 | /* Release flags on context. Protect from writes */ | |
602 | smp_store_release(&entry->w_ctx.flags, flags); | |
d624f371 | 603 | return NVM_IO_ERR; |
a4bd217b JG |
604 | } |
605 | ||
606 | if (flags & PBLK_FLUSH_ENTRY) { | |
607 | unsigned int sync_point; | |
608 | ||
609 | sync_point = READ_ONCE(rb->sync_point); | |
610 | if (sync_point == pos) { | |
611 | /* Protect syncs */ | |
612 | smp_store_release(&rb->sync_point, EMPTY_ENTRY); | |
613 | } | |
614 | ||
615 | flags &= ~PBLK_FLUSH_ENTRY; | |
616 | #ifdef CONFIG_NVM_DEBUG | |
617 | atomic_dec(&rb->inflight_sync_point); | |
618 | #endif | |
619 | } | |
620 | ||
621 | flags &= ~PBLK_WRITTEN_DATA; | |
622 | flags |= PBLK_SUBMITTED_ENTRY; | |
623 | ||
624 | /* Release flags on context. Protect from writes */ | |
625 | smp_store_release(&entry->w_ctx.flags, flags); | |
626 | ||
627 | pos = (pos + 1) & (rb->nr_entries - 1); | |
628 | } | |
629 | ||
d624f371 JG |
630 | if (pad) { |
631 | if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) { | |
632 | pr_err("pblk: could not pad page in write bio\n"); | |
633 | return NVM_IO_ERR; | |
634 | } | |
635 | } | |
636 | ||
a4bd217b JG |
637 | #ifdef CONFIG_NVM_DEBUG |
638 | atomic_long_add(pad, &((struct pblk *) | |
639 | (container_of(rb, struct pblk, rwb)))->padded_writes); | |
640 | #endif | |
d624f371 JG |
641 | |
642 | return NVM_IO_OK; | |
a4bd217b JG |
643 | } |
644 | ||
645 | /* | |
646 | * Copy to bio only if the lba matches the one on the given cache entry. | |
647 | * Otherwise, it means that the entry has been overwritten, and the bio should | |
648 | * be directed to disk. | |
649 | */ | |
650 | int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba, | |
651 | u64 pos, int bio_iter) | |
652 | { | |
653 | struct pblk_rb_entry *entry; | |
654 | struct pblk_w_ctx *w_ctx; | |
655 | void *data; | |
656 | int flags; | |
657 | int ret = 1; | |
658 | ||
659 | spin_lock(&rb->w_lock); | |
660 | ||
661 | #ifdef CONFIG_NVM_DEBUG | |
662 | /* Caller must ensure that the access will not cause an overflow */ | |
663 | BUG_ON(pos >= rb->nr_entries); | |
664 | #endif | |
665 | entry = &rb->entries[pos]; | |
666 | w_ctx = &entry->w_ctx; | |
667 | flags = READ_ONCE(w_ctx->flags); | |
668 | ||
669 | /* Check if the entry has been overwritten or is scheduled to be */ | |
670 | if (w_ctx->lba != lba || flags & PBLK_WRITABLE_ENTRY) { | |
671 | ret = 0; | |
672 | goto out; | |
673 | } | |
674 | ||
675 | /* Only advance the bio if it hasn't been advanced already. If advanced, | |
676 | * this bio is at least a partial bio (i.e., it has partially been | |
677 | * filled with data from the cache). If part of the data resides on the | |
678 | * media, we will read later on | |
679 | */ | |
680 | if (unlikely(!bio->bi_iter.bi_idx)) | |
681 | bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE); | |
682 | ||
683 | data = bio_data(bio); | |
684 | memcpy(data, entry->data, rb->seg_size); | |
685 | ||
686 | out: | |
687 | spin_unlock(&rb->w_lock); | |
688 | return ret; | |
689 | } | |
690 | ||
691 | struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos) | |
692 | { | |
693 | unsigned int entry = pos & (rb->nr_entries - 1); | |
694 | ||
695 | return &rb->entries[entry].w_ctx; | |
696 | } | |
697 | ||
698 | unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags) | |
699 | __acquires(&rb->s_lock) | |
700 | { | |
701 | if (flags) | |
702 | spin_lock_irqsave(&rb->s_lock, *flags); | |
703 | else | |
704 | spin_lock_irq(&rb->s_lock); | |
705 | ||
706 | return rb->sync; | |
707 | } | |
708 | ||
709 | void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags) | |
710 | __releases(&rb->s_lock) | |
711 | { | |
712 | lockdep_assert_held(&rb->s_lock); | |
713 | ||
714 | if (flags) | |
715 | spin_unlock_irqrestore(&rb->s_lock, *flags); | |
716 | else | |
717 | spin_unlock_irq(&rb->s_lock); | |
718 | } | |
719 | ||
720 | unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries) | |
721 | { | |
722 | unsigned int sync; | |
723 | unsigned int i; | |
724 | ||
725 | lockdep_assert_held(&rb->s_lock); | |
726 | ||
727 | sync = READ_ONCE(rb->sync); | |
728 | ||
729 | for (i = 0; i < nr_entries; i++) | |
730 | sync = (sync + 1) & (rb->nr_entries - 1); | |
731 | ||
732 | /* Protect from counts */ | |
733 | smp_store_release(&rb->sync, sync); | |
734 | ||
735 | return sync; | |
736 | } | |
737 | ||
738 | unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb) | |
739 | { | |
740 | unsigned int subm, sync_point; | |
741 | unsigned int count; | |
742 | ||
743 | /* Protect syncs */ | |
744 | sync_point = smp_load_acquire(&rb->sync_point); | |
745 | if (sync_point == EMPTY_ENTRY) | |
746 | return 0; | |
747 | ||
748 | subm = READ_ONCE(rb->subm); | |
749 | ||
750 | /* The sync point itself counts as a sector to sync */ | |
751 | count = pblk_rb_ring_count(sync_point, subm, rb->nr_entries) + 1; | |
752 | ||
753 | return count; | |
754 | } | |
755 | ||
756 | /* | |
757 | * Scan from the current position of the sync pointer to find the entry that | |
758 | * corresponds to the given ppa. This is necessary since write requests can be | |
759 | * completed out of order. The assumption is that the ppa is close to the sync | |
760 | * pointer thus the search will not take long. | |
761 | * | |
762 | * The caller of this function must guarantee that the sync pointer will no | |
763 | * reach the entry while it is using the metadata associated with it. With this | |
764 | * assumption in mind, there is no need to take the sync lock. | |
765 | */ | |
766 | struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb, | |
767 | struct ppa_addr *ppa) | |
768 | { | |
769 | unsigned int sync, subm, count; | |
770 | unsigned int i; | |
771 | ||
772 | sync = READ_ONCE(rb->sync); | |
773 | subm = READ_ONCE(rb->subm); | |
774 | count = pblk_rb_ring_count(subm, sync, rb->nr_entries); | |
775 | ||
776 | for (i = 0; i < count; i++) | |
777 | sync = (sync + 1) & (rb->nr_entries - 1); | |
778 | ||
779 | return NULL; | |
780 | } | |
781 | ||
782 | int pblk_rb_tear_down_check(struct pblk_rb *rb) | |
783 | { | |
784 | struct pblk_rb_entry *entry; | |
785 | int i; | |
786 | int ret = 0; | |
787 | ||
788 | spin_lock(&rb->w_lock); | |
789 | spin_lock_irq(&rb->s_lock); | |
790 | ||
791 | if ((rb->mem == rb->subm) && (rb->subm == rb->sync) && | |
792 | (rb->sync == rb->l2p_update) && | |
793 | (rb->sync_point == EMPTY_ENTRY)) { | |
794 | goto out; | |
795 | } | |
796 | ||
797 | if (!rb->entries) { | |
798 | ret = 1; | |
799 | goto out; | |
800 | } | |
801 | ||
802 | for (i = 0; i < rb->nr_entries; i++) { | |
803 | entry = &rb->entries[i]; | |
804 | ||
805 | if (!entry->data) { | |
806 | ret = 1; | |
807 | goto out; | |
808 | } | |
809 | } | |
810 | ||
811 | out: | |
812 | spin_unlock(&rb->w_lock); | |
813 | spin_unlock_irq(&rb->s_lock); | |
814 | ||
815 | return ret; | |
816 | } | |
817 | ||
818 | unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos) | |
819 | { | |
820 | return (pos & (rb->nr_entries - 1)); | |
821 | } | |
822 | ||
823 | int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos) | |
824 | { | |
825 | return (pos >= rb->nr_entries); | |
826 | } | |
827 | ||
828 | ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf) | |
829 | { | |
830 | struct pblk *pblk = container_of(rb, struct pblk, rwb); | |
831 | struct pblk_c_ctx *c; | |
832 | ssize_t offset; | |
833 | int queued_entries = 0; | |
834 | ||
835 | spin_lock_irq(&rb->s_lock); | |
836 | list_for_each_entry(c, &pblk->compl_list, list) | |
837 | queued_entries++; | |
838 | spin_unlock_irq(&rb->s_lock); | |
839 | ||
840 | if (rb->sync_point != EMPTY_ENTRY) | |
841 | offset = scnprintf(buf, PAGE_SIZE, | |
842 | "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n", | |
843 | rb->nr_entries, | |
844 | rb->mem, | |
845 | rb->subm, | |
846 | rb->sync, | |
847 | rb->l2p_update, | |
848 | #ifdef CONFIG_NVM_DEBUG | |
849 | atomic_read(&rb->inflight_sync_point), | |
850 | #else | |
851 | 0, | |
852 | #endif | |
853 | rb->sync_point, | |
854 | pblk_rb_read_count(rb), | |
855 | pblk_rb_space(rb), | |
856 | pblk_rb_sync_point_count(rb), | |
857 | queued_entries); | |
858 | else | |
859 | offset = scnprintf(buf, PAGE_SIZE, | |
860 | "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n", | |
861 | rb->nr_entries, | |
862 | rb->mem, | |
863 | rb->subm, | |
864 | rb->sync, | |
865 | rb->l2p_update, | |
866 | #ifdef CONFIG_NVM_DEBUG | |
867 | atomic_read(&rb->inflight_sync_point), | |
868 | #else | |
869 | 0, | |
870 | #endif | |
871 | pblk_rb_read_count(rb), | |
872 | pblk_rb_space(rb), | |
873 | pblk_rb_sync_point_count(rb), | |
874 | queued_entries); | |
875 | ||
876 | return offset; | |
877 | } |