Skip to content

Commit 0586942

Browse files
hlitzaxboe
authored andcommitted
lightnvm: pblk: fix race condition on GC
This patch fixes a race condition where a write is mapped to the last sectors of a line. The write is synced to the device but the L2P is not updated yet. When the line is garbage collected before the L2P update is performed, the sectors are ignored by the GC logic and the line is freed before all sectors are moved. When the L2P is finally updated, it contains a mapping to a freed line, subsequent reads of the corresponding LBAs fail. This patch introduces a per line counter specifying the number of sectors that are synced to the device but have not been updated in the L2P. Lines with a counter of greater than zero will not be selected for GC. Signed-off-by: Heiner Litz <hlitz@ucsc.edu> Reviewed-by: Hans Holmberg <hans.holmberg@cnexlabs.com> Reviewed-by: Javier González <javier@javigon.com> Signed-off-by: Matias Bjørling <mb@lightnvm.io> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b4cdc42 commit 0586942

6 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎drivers/lightnvm/pblk-core.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
12781278
spin_unlock(&line->lock);
12791279

12801280
kref_init(&line->ref);
1281+
atomic_set(&line->sec_to_update, 0);
12811282

12821283
return 0;
12831284
}

‎drivers/lightnvm/pblk-gc.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,22 @@ static struct pblk_line *pblk_gc_get_victim_line(struct pblk *pblk,
365365
struct list_head *group_list)
366366
{
367367
struct pblk_line *line, *victim;
368-
int line_vsc, victim_vsc;
368+
unsigned int line_vsc = ~0x0L, victim_vsc = ~0x0L;
369369

370370
victim = list_first_entry(group_list, struct pblk_line, list);
371+
371372
list_for_each_entry(line, group_list, list) {
372-
line_vsc = le32_to_cpu(*line->vsc);
373-
victim_vsc = le32_to_cpu(*victim->vsc);
374-
if (line_vsc < victim_vsc)
373+
if (!atomic_read(&line->sec_to_update))
374+
line_vsc = le32_to_cpu(*line->vsc);
375+
if (line_vsc < victim_vsc) {
375376
victim = line;
377+
victim_vsc = le32_to_cpu(*victim->vsc);
378+
}
376379
}
377380

381+
if (victim_vsc == ~0x0)
382+
return NULL;
383+
378384
return victim;
379385
}
380386

@@ -448,13 +454,13 @@ static void pblk_gc_run(struct pblk *pblk)
448454

449455
do {
450456
spin_lock(&l_mg->gc_lock);
451-
if (list_empty(group_list)) {
457+
458+
line = pblk_gc_get_victim_line(pblk, group_list);
459+
if (!line) {
452460
spin_unlock(&l_mg->gc_lock);
453461
break;
454462
}
455463

456-
line = pblk_gc_get_victim_line(pblk, group_list);
457-
458464
spin_lock(&line->lock);
459465
WARN_ON(line->state != PBLK_LINESTATE_CLOSED);
460466
line->state = PBLK_LINESTATE_GC;

‎drivers/lightnvm/pblk-map.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static int pblk_map_page_data(struct pblk *pblk, unsigned int sentry,
7373
*/
7474
if (i < valid_secs) {
7575
kref_get(&line->ref);
76+
atomic_inc(&line->sec_to_update);
7677
w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i);
7778
w_ctx->ppa = ppa_list[i];
7879
meta->lba = cpu_to_le64(w_ctx->lba);

‎drivers/lightnvm/pblk-rb.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ static int __pblk_rb_update_l2p(struct pblk_rb *rb, unsigned int to_update)
260260
entry->cacheline);
261261

262262
line = pblk_ppa_to_line(pblk, w_ctx->ppa);
263+
atomic_dec(&line->sec_to_update);
263264
kref_put(&line->ref, pblk_line_put);
264265
clean_wctx(w_ctx);
265266
rb->l2p_update = pblk_rb_ptr_wrap(rb, rb->l2p_update, 1);

‎drivers/lightnvm/pblk-write.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ static void pblk_prepare_resubmit(struct pblk *pblk, unsigned int sentry,
177177
* re-map these entries
178178
*/
179179
line = pblk_ppa_to_line(pblk, w_ctx->ppa);
180+
atomic_dec(&line->sec_to_update);
180181
kref_put(&line->ref, pblk_line_put);
181182
}
182183
spin_unlock(&pblk->trans_lock);

‎drivers/lightnvm/pblk.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ struct pblk_line {
487487
__le32 *vsc; /* Valid sector count in line */
488488

489489
struct kref ref; /* Write buffer L2P references */
490+
atomic_t sec_to_update; /* Outstanding L2P updates to ppa */
490491

491492
struct pblk_w_err_gc *w_err_gc; /* Write error gc recovery metadata */
492493

0 commit comments

Comments
 (0)