Skip to content

Commit 0b50b73

Browse files
author
Kent Overstreet
committed
bcachefs: Fix refcounting in discard path
bch_dev->io_ref does not protect against the filesystem going away; bch_fs->writes does. Thus the filesystem write ref needs to be the last ref we release. Reported-by: [email protected] Signed-off-by: Kent Overstreet <[email protected]>
1 parent 8ed823b commit 0b50b73

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

fs/bcachefs/alloc_background.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,26 +1874,26 @@ static void bch2_do_discards_work(struct work_struct *work)
18741874
trace_discard_buckets(c, s.seen, s.open, s.need_journal_commit, s.discarded,
18751875
bch2_err_str(ret));
18761876

1877-
bch2_write_ref_put(c, BCH_WRITE_REF_discard);
18781877
percpu_ref_put(&ca->io_ref);
1878+
bch2_write_ref_put(c, BCH_WRITE_REF_discard);
18791879
}
18801880

18811881
void bch2_dev_do_discards(struct bch_dev *ca)
18821882
{
18831883
struct bch_fs *c = ca->fs;
18841884

1885-
if (!bch2_dev_get_ioref(c, ca->dev_idx, WRITE))
1885+
if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_discard))
18861886
return;
18871887

1888-
if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_discard))
1889-
goto put_ioref;
1888+
if (!bch2_dev_get_ioref(c, ca->dev_idx, WRITE))
1889+
goto put_write_ref;
18901890

18911891
if (queue_work(c->write_ref_wq, &ca->discard_work))
18921892
return;
18931893

1894-
bch2_write_ref_put(c, BCH_WRITE_REF_discard);
1895-
put_ioref:
18961894
percpu_ref_put(&ca->io_ref);
1895+
put_write_ref:
1896+
bch2_write_ref_put(c, BCH_WRITE_REF_discard);
18971897
}
18981898

18991899
void bch2_do_discards(struct bch_fs *c)

0 commit comments

Comments
 (0)