Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lightnvm: pblk: encapsulate rb pointer operations

pblk's read/write buffer is always a power-of-2, thus wrapping up the
buffer can be done with a bit mask. Since this is an implementation
detail internal to the write buffer, make a helper that hides pointer
increment + wrap, and allows to transparently relax this assumption in
the future.

Signed-off-by: Javier González <javier@cnexlabs.com>
Signed-off-by: Matias Bjørling <mb@lightnvm.io>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Javier González and committed by
Jens Axboe
40b8657d dde4aac2

+17 -13
+13 -8
drivers/lightnvm/pblk-rb.c
··· 169 169 return pblk_rb_ring_space(rb, mem, sync, rb->nr_entries); 170 170 } 171 171 172 + unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p, 173 + unsigned int nr_entries) 174 + { 175 + return (p + nr_entries) & (rb->nr_entries - 1); 176 + } 177 + 172 178 /* 173 179 * Buffer count is calculated with respect to the submission entry signaling the 174 180 * entries that are available to send to the media ··· 201 195 202 196 subm = READ_ONCE(rb->subm); 203 197 /* Commit read means updating submission pointer */ 204 - smp_store_release(&rb->subm, 205 - (subm + nr_entries) & (rb->nr_entries - 1)); 198 + smp_store_release(&rb->subm, pblk_rb_ptr_wrap(rb, subm, nr_entries)); 206 199 207 200 return subm; 208 201 } ··· 234 229 line = pblk_ppa_to_line(pblk, w_ctx->ppa); 235 230 kref_put(&line->ref, pblk_line_put); 236 231 clean_wctx(w_ctx); 237 - rb->l2p_update = (rb->l2p_update + 1) & (rb->nr_entries - 1); 232 + rb->l2p_update = pblk_rb_ptr_wrap(rb, rb->l2p_update, 1); 238 233 } 239 234 240 235 pblk_rl_out(&pblk->rl, user_io, gc_io); ··· 413 408 return 0; 414 409 415 410 /* Protect from read count */ 416 - smp_store_release(&rb->mem, (*pos + nr_entries) & (rb->nr_entries - 1)); 411 + smp_store_release(&rb->mem, pblk_rb_ptr_wrap(rb, *pos, nr_entries)); 417 412 return 1; 418 413 } 419 414 ··· 437 432 if (!__pblk_rb_may_write(rb, nr_entries, pos)) 438 433 return 0; 439 434 440 - mem = (*pos + nr_entries) & (rb->nr_entries - 1); 435 + mem = pblk_rb_ptr_wrap(rb, *pos, nr_entries); 441 436 *io_ret = NVM_IO_DONE; 442 437 443 438 if (bio->bi_opf & REQ_PREFLUSH) { ··· 577 572 /* Release flags on context. Protect from writes */ 578 573 smp_store_release(&entry->w_ctx.flags, flags); 579 574 580 - pos = (pos + 1) & (rb->nr_entries - 1); 575 + pos = pblk_rb_ptr_wrap(rb, pos, 1); 581 576 } 582 577 583 578 if (pad) { ··· 657 652 658 653 struct pblk_w_ctx *pblk_rb_w_ctx(struct pblk_rb *rb, unsigned int pos) 659 654 { 660 - unsigned int entry = pos & (rb->nr_entries - 1); 655 + unsigned int entry = pblk_rb_ptr_wrap(rb, pos, 0); 661 656 662 657 return &rb->entries[entry].w_ctx; 663 658 } ··· 703 698 } 704 699 } 705 700 706 - sync = (sync + nr_entries) & (rb->nr_entries - 1); 701 + sync = pblk_rb_ptr_wrap(rb, sync, nr_entries); 707 702 708 703 /* Protect from counts */ 709 704 smp_store_release(&rb->sync, sync);
+2 -5
drivers/lightnvm/pblk-write.c
··· 140 140 struct pblk_w_ctx *w_ctx; 141 141 struct ppa_addr ppa_l2p; 142 142 int flags; 143 - unsigned int pos, i; 143 + unsigned int i; 144 144 145 145 spin_lock(&pblk->trans_lock); 146 - pos = sentry; 147 146 for (i = 0; i < nr_entries; i++) { 148 - entry = &rb->entries[pos]; 147 + entry = &rb->entries[pblk_rb_ptr_wrap(rb, sentry, i)]; 149 148 w_ctx = &entry->w_ctx; 150 149 151 150 /* Check if the lba has been overwritten */ ··· 163 164 */ 164 165 line = pblk_ppa_to_line(pblk, w_ctx->ppa); 165 166 kref_put(&line->ref, pblk_line_put); 166 - 167 - pos = (pos + 1) & (rb->nr_entries - 1); 168 167 } 169 168 spin_unlock(&pblk->trans_lock); 170 169 }
+2
drivers/lightnvm/pblk.h
··· 760 760 761 761 unsigned int pblk_rb_sync_init(struct pblk_rb *rb, unsigned long *flags); 762 762 unsigned int pblk_rb_sync_advance(struct pblk_rb *rb, unsigned int nr_entries); 763 + unsigned int pblk_rb_ptr_wrap(struct pblk_rb *rb, unsigned int p, 764 + unsigned int nr_entries); 763 765 void pblk_rb_sync_end(struct pblk_rb *rb, unsigned long *flags); 764 766 unsigned int pblk_rb_flush_point_count(struct pblk_rb *rb); 765 767