Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 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); |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 153 | w_ctx->lba = ADDR_EMPTY; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 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 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 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 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 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 *l2p_upd, |
| 205 | unsigned int to_update) |
| 206 | { |
| 207 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 208 | struct pblk_line *line; |
| 209 | struct pblk_rb_entry *entry; |
| 210 | struct pblk_w_ctx *w_ctx; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 211 | unsigned int user_io = 0, gc_io = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 212 | unsigned int i; |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 213 | int flags; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 214 | |
| 215 | for (i = 0; i < to_update; i++) { |
| 216 | entry = &rb->entries[*l2p_upd]; |
| 217 | w_ctx = &entry->w_ctx; |
| 218 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 219 | flags = READ_ONCE(entry->w_ctx.flags); |
| 220 | if (flags & PBLK_IOTYPE_USER) |
| 221 | user_io++; |
| 222 | else if (flags & PBLK_IOTYPE_GC) |
| 223 | gc_io++; |
| 224 | else |
| 225 | WARN(1, "pblk: unknown IO type\n"); |
| 226 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 227 | pblk_update_map_dev(pblk, w_ctx->lba, w_ctx->ppa, |
| 228 | entry->cacheline); |
| 229 | |
| 230 | line = &pblk->lines[pblk_tgt_ppa_to_line(w_ctx->ppa)]; |
| 231 | kref_put(&line->ref, pblk_line_put); |
| 232 | clean_wctx(w_ctx); |
| 233 | *l2p_upd = (*l2p_upd + 1) & (rb->nr_entries - 1); |
| 234 | } |
| 235 | |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 236 | pblk_rl_out(&pblk->rl, user_io, gc_io); |
| 237 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * When we move the l2p_update pointer, we update the l2p table - lookups will |
| 243 | * point to the physical address instead of to the cacheline in the write buffer |
| 244 | * from this moment on. |
| 245 | */ |
| 246 | static int pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int nr_entries, |
| 247 | unsigned int mem, unsigned int sync) |
| 248 | { |
| 249 | unsigned int space, count; |
| 250 | int ret = 0; |
| 251 | |
| 252 | lockdep_assert_held(&rb->w_lock); |
| 253 | |
| 254 | /* Update l2p only as buffer entries are being overwritten */ |
| 255 | space = pblk_rb_ring_space(rb, mem, rb->l2p_update, rb->nr_entries); |
| 256 | if (space > nr_entries) |
| 257 | goto out; |
| 258 | |
| 259 | count = nr_entries - space; |
| 260 | /* l2p_update used exclusively under rb->w_lock */ |
| 261 | ret = __pblk_rb_update_l2p(rb, &rb->l2p_update, count); |
| 262 | |
| 263 | out: |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Update the l2p entry for all sectors stored on the write buffer. This means |
| 269 | * that all future lookups to the l2p table will point to a device address, not |
| 270 | * to the cacheline in the write buffer. |
| 271 | */ |
| 272 | void pblk_rb_sync_l2p(struct pblk_rb *rb) |
| 273 | { |
| 274 | unsigned int sync; |
| 275 | unsigned int to_update; |
| 276 | |
| 277 | spin_lock(&rb->w_lock); |
| 278 | |
| 279 | /* Protect from reads and writes */ |
| 280 | sync = smp_load_acquire(&rb->sync); |
| 281 | |
| 282 | to_update = pblk_rb_ring_count(sync, rb->l2p_update, rb->nr_entries); |
| 283 | __pblk_rb_update_l2p(rb, &rb->l2p_update, to_update); |
| 284 | |
| 285 | spin_unlock(&rb->w_lock); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Write @nr_entries to ring buffer from @data buffer if there is enough space. |
| 290 | * Typically, 4KB data chunks coming from a bio will be copied to the ring |
| 291 | * buffer, thus the write will fail if not all incoming data can be copied. |
| 292 | * |
| 293 | */ |
| 294 | static void __pblk_rb_write_entry(struct pblk_rb *rb, void *data, |
| 295 | struct pblk_w_ctx w_ctx, |
| 296 | struct pblk_rb_entry *entry) |
| 297 | { |
| 298 | memcpy(entry->data, data, rb->seg_size); |
| 299 | |
| 300 | entry->w_ctx.lba = w_ctx.lba; |
| 301 | entry->w_ctx.ppa = w_ctx.ppa; |
| 302 | } |
| 303 | |
| 304 | void pblk_rb_write_entry_user(struct pblk_rb *rb, void *data, |
| 305 | struct pblk_w_ctx w_ctx, unsigned int ring_pos) |
| 306 | { |
| 307 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 308 | struct pblk_rb_entry *entry; |
| 309 | int flags; |
| 310 | |
| 311 | entry = &rb->entries[ring_pos]; |
| 312 | flags = READ_ONCE(entry->w_ctx.flags); |
| 313 | #ifdef CONFIG_NVM_DEBUG |
| 314 | /* Caller must guarantee that the entry is free */ |
| 315 | BUG_ON(!(flags & PBLK_WRITABLE_ENTRY)); |
| 316 | #endif |
| 317 | |
| 318 | __pblk_rb_write_entry(rb, data, w_ctx, entry); |
| 319 | |
| 320 | pblk_update_map_cache(pblk, w_ctx.lba, entry->cacheline); |
| 321 | flags = w_ctx.flags | PBLK_WRITTEN_DATA; |
| 322 | |
| 323 | /* Release flags on write context. Protect from writes */ |
| 324 | smp_store_release(&entry->w_ctx.flags, flags); |
| 325 | } |
| 326 | |
| 327 | void pblk_rb_write_entry_gc(struct pblk_rb *rb, void *data, |
| 328 | struct pblk_w_ctx w_ctx, struct pblk_line *gc_line, |
| 329 | unsigned int ring_pos) |
| 330 | { |
| 331 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 332 | struct pblk_rb_entry *entry; |
| 333 | int flags; |
| 334 | |
| 335 | entry = &rb->entries[ring_pos]; |
| 336 | flags = READ_ONCE(entry->w_ctx.flags); |
| 337 | #ifdef CONFIG_NVM_DEBUG |
| 338 | /* Caller must guarantee that the entry is free */ |
| 339 | BUG_ON(!(flags & PBLK_WRITABLE_ENTRY)); |
| 340 | #endif |
| 341 | |
| 342 | __pblk_rb_write_entry(rb, data, w_ctx, entry); |
| 343 | |
| 344 | if (!pblk_update_map_gc(pblk, w_ctx.lba, entry->cacheline, gc_line)) |
| 345 | entry->w_ctx.lba = ADDR_EMPTY; |
| 346 | |
| 347 | flags = w_ctx.flags | PBLK_WRITTEN_DATA; |
| 348 | |
| 349 | /* Release flags on write context. Protect from writes */ |
| 350 | smp_store_release(&entry->w_ctx.flags, flags); |
| 351 | } |
| 352 | |
| 353 | static int pblk_rb_sync_point_set(struct pblk_rb *rb, struct bio *bio, |
| 354 | unsigned int pos) |
| 355 | { |
| 356 | struct pblk_rb_entry *entry; |
| 357 | unsigned int subm, sync_point; |
| 358 | int flags; |
| 359 | |
| 360 | subm = READ_ONCE(rb->subm); |
| 361 | |
| 362 | #ifdef CONFIG_NVM_DEBUG |
| 363 | atomic_inc(&rb->inflight_sync_point); |
| 364 | #endif |
| 365 | |
| 366 | if (pos == subm) |
| 367 | return 0; |
| 368 | |
| 369 | sync_point = (pos == 0) ? (rb->nr_entries - 1) : (pos - 1); |
| 370 | entry = &rb->entries[sync_point]; |
| 371 | |
| 372 | flags = READ_ONCE(entry->w_ctx.flags); |
| 373 | flags |= PBLK_FLUSH_ENTRY; |
| 374 | |
| 375 | /* Release flags on context. Protect from writes */ |
| 376 | smp_store_release(&entry->w_ctx.flags, flags); |
| 377 | |
| 378 | /* Protect syncs */ |
| 379 | smp_store_release(&rb->sync_point, sync_point); |
| 380 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 381 | if (!bio) |
| 382 | return 0; |
| 383 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 384 | spin_lock_irq(&rb->s_lock); |
| 385 | bio_list_add(&entry->w_ctx.bios, bio); |
| 386 | spin_unlock_irq(&rb->s_lock); |
| 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | static int __pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries, |
| 392 | unsigned int *pos) |
| 393 | { |
| 394 | unsigned int mem; |
| 395 | unsigned int sync; |
| 396 | |
| 397 | sync = READ_ONCE(rb->sync); |
| 398 | mem = READ_ONCE(rb->mem); |
| 399 | |
| 400 | if (pblk_rb_ring_space(rb, mem, sync, rb->nr_entries) < nr_entries) |
| 401 | return 0; |
| 402 | |
| 403 | if (pblk_rb_update_l2p(rb, nr_entries, mem, sync)) |
| 404 | return 0; |
| 405 | |
| 406 | *pos = mem; |
| 407 | |
| 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | static int pblk_rb_may_write(struct pblk_rb *rb, unsigned int nr_entries, |
| 412 | unsigned int *pos) |
| 413 | { |
| 414 | if (!__pblk_rb_may_write(rb, nr_entries, pos)) |
| 415 | return 0; |
| 416 | |
| 417 | /* Protect from read count */ |
| 418 | smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1)); |
| 419 | return 1; |
| 420 | } |
| 421 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 422 | void pblk_rb_flush(struct pblk_rb *rb) |
| 423 | { |
| 424 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 425 | unsigned int mem = READ_ONCE(rb->mem); |
| 426 | |
| 427 | if (pblk_rb_sync_point_set(rb, NULL, mem)) |
| 428 | return; |
| 429 | |
| 430 | pblk_write_should_kick(pblk); |
| 431 | } |
| 432 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 433 | static int pblk_rb_may_write_flush(struct pblk_rb *rb, unsigned int nr_entries, |
| 434 | unsigned int *pos, struct bio *bio, |
| 435 | int *io_ret) |
| 436 | { |
| 437 | unsigned int mem; |
| 438 | |
| 439 | if (!__pblk_rb_may_write(rb, nr_entries, pos)) |
| 440 | return 0; |
| 441 | |
| 442 | mem = (*pos + nr_entries) & (rb->nr_entries - 1); |
| 443 | *io_ret = NVM_IO_DONE; |
| 444 | |
| 445 | if (bio->bi_opf & REQ_PREFLUSH) { |
| 446 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 447 | |
| 448 | #ifdef CONFIG_NVM_DEBUG |
| 449 | atomic_long_inc(&pblk->nr_flush); |
| 450 | #endif |
| 451 | if (pblk_rb_sync_point_set(&pblk->rwb, bio, mem)) |
| 452 | *io_ret = NVM_IO_OK; |
| 453 | } |
| 454 | |
| 455 | /* Protect from read count */ |
| 456 | smp_store_release(&rb->mem, mem); |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Atomically check that (i) there is space on the write buffer for the |
| 462 | * incoming I/O, and (ii) the current I/O type has enough budget in the write |
| 463 | * buffer (rate-limiter). |
| 464 | */ |
| 465 | int pblk_rb_may_write_user(struct pblk_rb *rb, struct bio *bio, |
| 466 | unsigned int nr_entries, unsigned int *pos) |
| 467 | { |
| 468 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 469 | int io_ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 470 | |
| 471 | spin_lock(&rb->w_lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 472 | io_ret = pblk_rl_user_may_insert(&pblk->rl, nr_entries); |
| 473 | if (io_ret) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 474 | spin_unlock(&rb->w_lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 475 | return io_ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 478 | if (!pblk_rb_may_write_flush(rb, nr_entries, pos, bio, &io_ret)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 479 | spin_unlock(&rb->w_lock); |
| 480 | return NVM_IO_REQUEUE; |
| 481 | } |
| 482 | |
| 483 | pblk_rl_user_in(&pblk->rl, nr_entries); |
| 484 | spin_unlock(&rb->w_lock); |
| 485 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 486 | return io_ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Look at pblk_rb_may_write_user comment |
| 491 | */ |
| 492 | int pblk_rb_may_write_gc(struct pblk_rb *rb, unsigned int nr_entries, |
| 493 | unsigned int *pos) |
| 494 | { |
| 495 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 496 | |
| 497 | spin_lock(&rb->w_lock); |
| 498 | if (!pblk_rl_gc_may_insert(&pblk->rl, nr_entries)) { |
| 499 | spin_unlock(&rb->w_lock); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | if (!pblk_rb_may_write(rb, nr_entries, pos)) { |
| 504 | spin_unlock(&rb->w_lock); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | pblk_rl_gc_in(&pblk->rl, nr_entries); |
| 509 | spin_unlock(&rb->w_lock); |
| 510 | |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * The caller of this function must ensure that the backpointer will not |
| 516 | * overwrite the entries passed on the list. |
| 517 | */ |
| 518 | unsigned int pblk_rb_read_to_bio_list(struct pblk_rb *rb, struct bio *bio, |
| 519 | struct list_head *list, |
| 520 | unsigned int max) |
| 521 | { |
| 522 | struct pblk_rb_entry *entry, *tentry; |
| 523 | struct page *page; |
| 524 | unsigned int read = 0; |
| 525 | int ret; |
| 526 | |
| 527 | list_for_each_entry_safe(entry, tentry, list, index) { |
| 528 | if (read > max) { |
| 529 | pr_err("pblk: too many entries on list\n"); |
| 530 | goto out; |
| 531 | } |
| 532 | |
| 533 | page = virt_to_page(entry->data); |
| 534 | if (!page) { |
| 535 | pr_err("pblk: could not allocate write bio page\n"); |
| 536 | goto out; |
| 537 | } |
| 538 | |
| 539 | ret = bio_add_page(bio, page, rb->seg_size, 0); |
| 540 | if (ret != rb->seg_size) { |
| 541 | pr_err("pblk: could not add page to write bio\n"); |
| 542 | goto out; |
| 543 | } |
| 544 | |
| 545 | list_del(&entry->index); |
| 546 | read++; |
| 547 | } |
| 548 | |
| 549 | out: |
| 550 | return read; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Read available entries on rb and add them to the given bio. To avoid a memory |
| 555 | * copy, a page reference to the write buffer is used to be added to the bio. |
| 556 | * |
| 557 | * This function is used by the write thread to form the write bio that will |
| 558 | * persist data on the write buffer to the media. |
| 559 | */ |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 560 | unsigned int pblk_rb_read_to_bio(struct pblk_rb *rb, struct nvm_rq *rqd, |
| 561 | struct bio *bio, unsigned int pos, |
| 562 | unsigned int nr_entries, unsigned int count) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 563 | { |
| 564 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 565 | struct request_queue *q = pblk->dev->q; |
| 566 | struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 567 | struct pblk_rb_entry *entry; |
| 568 | struct page *page; |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 569 | unsigned int pad = 0, to_read = nr_entries; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 570 | unsigned int i; |
| 571 | int flags; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 572 | |
| 573 | if (count < nr_entries) { |
| 574 | pad = nr_entries - count; |
| 575 | to_read = count; |
| 576 | } |
| 577 | |
| 578 | c_ctx->sentry = pos; |
| 579 | c_ctx->nr_valid = to_read; |
| 580 | c_ctx->nr_padded = pad; |
| 581 | |
| 582 | for (i = 0; i < to_read; i++) { |
| 583 | entry = &rb->entries[pos]; |
| 584 | |
| 585 | /* A write has been allowed into the buffer, but data is still |
| 586 | * being copied to it. It is ok to busy wait. |
| 587 | */ |
| 588 | try: |
| 589 | flags = READ_ONCE(entry->w_ctx.flags); |
Javier González | 1088812 | 2017-06-30 17:56:37 +0200 | [diff] [blame] | 590 | if (!(flags & PBLK_WRITTEN_DATA)) { |
| 591 | io_schedule(); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 592 | goto try; |
Javier González | 1088812 | 2017-06-30 17:56:37 +0200 | [diff] [blame] | 593 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 594 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 595 | page = virt_to_page(entry->data); |
| 596 | if (!page) { |
| 597 | pr_err("pblk: could not allocate write bio page\n"); |
| 598 | flags &= ~PBLK_WRITTEN_DATA; |
| 599 | flags |= PBLK_SUBMITTED_ENTRY; |
| 600 | /* Release flags on context. Protect from writes */ |
| 601 | smp_store_release(&entry->w_ctx.flags, flags); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 602 | return NVM_IO_ERR; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 605 | if (bio_add_pc_page(q, bio, page, rb->seg_size, 0) != |
| 606 | rb->seg_size) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 607 | pr_err("pblk: could not add page to write bio\n"); |
| 608 | flags &= ~PBLK_WRITTEN_DATA; |
| 609 | flags |= PBLK_SUBMITTED_ENTRY; |
| 610 | /* Release flags on context. Protect from writes */ |
| 611 | smp_store_release(&entry->w_ctx.flags, flags); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 612 | return NVM_IO_ERR; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (flags & PBLK_FLUSH_ENTRY) { |
| 616 | unsigned int sync_point; |
| 617 | |
| 618 | sync_point = READ_ONCE(rb->sync_point); |
| 619 | if (sync_point == pos) { |
| 620 | /* Protect syncs */ |
| 621 | smp_store_release(&rb->sync_point, EMPTY_ENTRY); |
| 622 | } |
| 623 | |
| 624 | flags &= ~PBLK_FLUSH_ENTRY; |
| 625 | #ifdef CONFIG_NVM_DEBUG |
| 626 | atomic_dec(&rb->inflight_sync_point); |
| 627 | #endif |
| 628 | } |
| 629 | |
| 630 | flags &= ~PBLK_WRITTEN_DATA; |
| 631 | flags |= PBLK_SUBMITTED_ENTRY; |
| 632 | |
| 633 | /* Release flags on context. Protect from writes */ |
| 634 | smp_store_release(&entry->w_ctx.flags, flags); |
| 635 | |
| 636 | pos = (pos + 1) & (rb->nr_entries - 1); |
| 637 | } |
| 638 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 639 | if (pad) { |
| 640 | if (pblk_bio_add_pages(pblk, bio, GFP_KERNEL, pad)) { |
| 641 | pr_err("pblk: could not pad page in write bio\n"); |
| 642 | return NVM_IO_ERR; |
| 643 | } |
| 644 | } |
| 645 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 646 | #ifdef CONFIG_NVM_DEBUG |
| 647 | atomic_long_add(pad, &((struct pblk *) |
| 648 | (container_of(rb, struct pblk, rwb)))->padded_writes); |
| 649 | #endif |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 650 | |
| 651 | return NVM_IO_OK; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Copy to bio only if the lba matches the one on the given cache entry. |
| 656 | * Otherwise, it means that the entry has been overwritten, and the bio should |
| 657 | * be directed to disk. |
| 658 | */ |
| 659 | int pblk_rb_copy_to_bio(struct pblk_rb *rb, struct bio *bio, sector_t lba, |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 660 | struct ppa_addr ppa, int bio_iter, bool advanced_bio) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 661 | { |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 662 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 663 | struct pblk_rb_entry *entry; |
| 664 | struct pblk_w_ctx *w_ctx; |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 665 | struct ppa_addr l2p_ppa; |
| 666 | u64 pos = pblk_addr_to_cacheline(ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 667 | void *data; |
| 668 | int flags; |
| 669 | int ret = 1; |
| 670 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 671 | |
| 672 | #ifdef CONFIG_NVM_DEBUG |
| 673 | /* Caller must ensure that the access will not cause an overflow */ |
| 674 | BUG_ON(pos >= rb->nr_entries); |
| 675 | #endif |
| 676 | entry = &rb->entries[pos]; |
| 677 | w_ctx = &entry->w_ctx; |
| 678 | flags = READ_ONCE(w_ctx->flags); |
| 679 | |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 680 | spin_lock(&rb->w_lock); |
| 681 | spin_lock(&pblk->trans_lock); |
| 682 | l2p_ppa = pblk_trans_map_get(pblk, lba); |
| 683 | spin_unlock(&pblk->trans_lock); |
| 684 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 685 | /* Check if the entry has been overwritten or is scheduled to be */ |
Javier González | 07698466 | 2017-06-30 17:56:42 +0200 | [diff] [blame] | 686 | if (!pblk_ppa_comp(l2p_ppa, ppa) || w_ctx->lba != lba || |
| 687 | flags & PBLK_WRITABLE_ENTRY) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 688 | ret = 0; |
| 689 | goto out; |
| 690 | } |
| 691 | |
| 692 | /* Only advance the bio if it hasn't been advanced already. If advanced, |
| 693 | * this bio is at least a partial bio (i.e., it has partially been |
| 694 | * filled with data from the cache). If part of the data resides on the |
| 695 | * media, we will read later on |
| 696 | */ |
Javier González | 75cb8e9 | 2017-07-28 15:13:16 +0200 | [diff] [blame] | 697 | if (unlikely(!advanced_bio)) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 698 | bio_advance(bio, bio_iter * PBLK_EXPOSED_PAGE_SIZE); |
| 699 | |
| 700 | data = bio_data(bio); |
| 701 | memcpy(data, entry->data, rb->seg_size); |
| 702 | |
| 703 | out: |
| 704 | spin_unlock(&rb->w_lock); |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos) |
| 709 | { |
| 710 | unsigned int entry = pos & (rb->nr_entries - 1); |
| 711 | |
| 712 | return &rb->entries[entry].w_ctx; |
| 713 | } |
| 714 | |
| 715 | unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags) |
| 716 | __acquires(&rb->s_lock) |
| 717 | { |
| 718 | if (flags) |
| 719 | spin_lock_irqsave(&rb->s_lock, *flags); |
| 720 | else |
| 721 | spin_lock_irq(&rb->s_lock); |
| 722 | |
| 723 | return rb->sync; |
| 724 | } |
| 725 | |
| 726 | void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags) |
| 727 | __releases(&rb->s_lock) |
| 728 | { |
| 729 | lockdep_assert_held(&rb->s_lock); |
| 730 | |
| 731 | if (flags) |
| 732 | spin_unlock_irqrestore(&rb->s_lock, *flags); |
| 733 | else |
| 734 | spin_unlock_irq(&rb->s_lock); |
| 735 | } |
| 736 | |
| 737 | unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries) |
| 738 | { |
| 739 | unsigned int sync; |
| 740 | unsigned int i; |
| 741 | |
| 742 | lockdep_assert_held(&rb->s_lock); |
| 743 | |
| 744 | sync = READ_ONCE(rb->sync); |
| 745 | |
| 746 | for (i = 0; i < nr_entries; i++) |
| 747 | sync = (sync + 1) & (rb->nr_entries - 1); |
| 748 | |
| 749 | /* Protect from counts */ |
| 750 | smp_store_release(&rb->sync, sync); |
| 751 | |
| 752 | return sync; |
| 753 | } |
| 754 | |
| 755 | unsigned int pblk_rb_sync_point_count(struct pblk_rb *rb) |
| 756 | { |
| 757 | unsigned int subm, sync_point; |
| 758 | unsigned int count; |
| 759 | |
| 760 | /* Protect syncs */ |
| 761 | sync_point = smp_load_acquire(&rb->sync_point); |
| 762 | if (sync_point == EMPTY_ENTRY) |
| 763 | return 0; |
| 764 | |
| 765 | subm = READ_ONCE(rb->subm); |
| 766 | |
| 767 | /* The sync point itself counts as a sector to sync */ |
| 768 | count = pblk_rb_ring_count(sync_point, subm, rb->nr_entries) + 1; |
| 769 | |
| 770 | return count; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Scan from the current position of the sync pointer to find the entry that |
| 775 | * corresponds to the given ppa. This is necessary since write requests can be |
| 776 | * completed out of order. The assumption is that the ppa is close to the sync |
| 777 | * pointer thus the search will not take long. |
| 778 | * |
| 779 | * The caller of this function must guarantee that the sync pointer will no |
| 780 | * reach the entry while it is using the metadata associated with it. With this |
| 781 | * assumption in mind, there is no need to take the sync lock. |
| 782 | */ |
| 783 | struct pblk_rb_entry *pblk_rb_sync_scan_entry(struct pblk_rb *rb, |
| 784 | struct ppa_addr *ppa) |
| 785 | { |
| 786 | unsigned int sync, subm, count; |
| 787 | unsigned int i; |
| 788 | |
| 789 | sync = READ_ONCE(rb->sync); |
| 790 | subm = READ_ONCE(rb->subm); |
| 791 | count = pblk_rb_ring_count(subm, sync, rb->nr_entries); |
| 792 | |
| 793 | for (i = 0; i < count; i++) |
| 794 | sync = (sync + 1) & (rb->nr_entries - 1); |
| 795 | |
| 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | int pblk_rb_tear_down_check(struct pblk_rb *rb) |
| 800 | { |
| 801 | struct pblk_rb_entry *entry; |
| 802 | int i; |
| 803 | int ret = 0; |
| 804 | |
| 805 | spin_lock(&rb->w_lock); |
| 806 | spin_lock_irq(&rb->s_lock); |
| 807 | |
| 808 | if ((rb->mem == rb->subm) && (rb->subm == rb->sync) && |
| 809 | (rb->sync == rb->l2p_update) && |
| 810 | (rb->sync_point == EMPTY_ENTRY)) { |
| 811 | goto out; |
| 812 | } |
| 813 | |
| 814 | if (!rb->entries) { |
| 815 | ret = 1; |
| 816 | goto out; |
| 817 | } |
| 818 | |
| 819 | for (i = 0; i < rb->nr_entries; i++) { |
| 820 | entry = &rb->entries[i]; |
| 821 | |
| 822 | if (!entry->data) { |
| 823 | ret = 1; |
| 824 | goto out; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | out: |
| 829 | spin_unlock(&rb->w_lock); |
| 830 | spin_unlock_irq(&rb->s_lock); |
| 831 | |
| 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | unsigned int pblk_rb_wrap_pos(struct pblk_rb *rb, unsigned int pos) |
| 836 | { |
| 837 | return (pos & (rb->nr_entries - 1)); |
| 838 | } |
| 839 | |
| 840 | int pblk_rb_pos_oob(struct pblk_rb *rb, u64 pos) |
| 841 | { |
| 842 | return (pos >= rb->nr_entries); |
| 843 | } |
| 844 | |
| 845 | ssize_t pblk_rb_sysfs(struct pblk_rb *rb, char *buf) |
| 846 | { |
| 847 | struct pblk *pblk = container_of(rb, struct pblk, rwb); |
| 848 | struct pblk_c_ctx *c; |
| 849 | ssize_t offset; |
| 850 | int queued_entries = 0; |
| 851 | |
| 852 | spin_lock_irq(&rb->s_lock); |
| 853 | list_for_each_entry(c, &pblk->compl_list, list) |
| 854 | queued_entries++; |
| 855 | spin_unlock_irq(&rb->s_lock); |
| 856 | |
| 857 | if (rb->sync_point != EMPTY_ENTRY) |
| 858 | offset = scnprintf(buf, PAGE_SIZE, |
| 859 | "%u\t%u\t%u\t%u\t%u\t%u\t%u - %u/%u/%u - %d\n", |
| 860 | rb->nr_entries, |
| 861 | rb->mem, |
| 862 | rb->subm, |
| 863 | rb->sync, |
| 864 | rb->l2p_update, |
| 865 | #ifdef CONFIG_NVM_DEBUG |
| 866 | atomic_read(&rb->inflight_sync_point), |
| 867 | #else |
| 868 | 0, |
| 869 | #endif |
| 870 | rb->sync_point, |
| 871 | pblk_rb_read_count(rb), |
| 872 | pblk_rb_space(rb), |
| 873 | pblk_rb_sync_point_count(rb), |
| 874 | queued_entries); |
| 875 | else |
| 876 | offset = scnprintf(buf, PAGE_SIZE, |
| 877 | "%u\t%u\t%u\t%u\t%u\t%u\tNULL - %u/%u/%u - %d\n", |
| 878 | rb->nr_entries, |
| 879 | rb->mem, |
| 880 | rb->subm, |
| 881 | rb->sync, |
| 882 | rb->l2p_update, |
| 883 | #ifdef CONFIG_NVM_DEBUG |
| 884 | atomic_read(&rb->inflight_sync_point), |
| 885 | #else |
| 886 | 0, |
| 887 | #endif |
| 888 | pblk_rb_read_count(rb), |
| 889 | pblk_rb_space(rb), |
| 890 | pblk_rb_sync_point_count(rb), |
| 891 | queued_entries); |
| 892 | |
| 893 | return offset; |
| 894 | } |