Skip to content

Commit 18b4abc

Browse files
author
Kent Overstreet
committed
bcachefs: Fix lifetime issue in device iterator helpers
bch2_get_next_dev() and bch2_get_next_online_dev() iterate over devices, dropping and taking refs as they go; we can't access the previous device (for ca->dev_idx) after we've dropped our ref to it, unless we take rcu_read_lock() first. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 3a2d025 commit 18b4abc

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

fs/bcachefs/sb-members.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, struct bch_dev *
107107

108108
static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, struct bch_dev *ca)
109109
{
110+
rcu_read_lock();
110111
if (ca)
111112
percpu_ref_put(&ca->ref);
112113

113-
rcu_read_lock();
114114
if ((ca = __bch2_next_dev(c, ca, NULL)))
115115
percpu_ref_get(&ca->ref);
116116
rcu_read_unlock();
@@ -132,10 +132,10 @@ static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
132132
struct bch_dev *ca,
133133
unsigned state_mask)
134134
{
135+
rcu_read_lock();
135136
if (ca)
136137
percpu_ref_put(&ca->io_ref);
137138

138-
rcu_read_lock();
139139
while ((ca = __bch2_next_dev(c, ca, NULL)) &&
140140
(!((1 << ca->mi.state) & state_mask) ||
141141
!percpu_ref_tryget(&ca->io_ref)))

0 commit comments

Comments
 (0)