Commit 18b4abce authored by Kent Overstreet's avatar Kent Overstreet
Browse files

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: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 3a2d0259
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -107,10 +107,10 @@ static inline struct bch_dev *__bch2_next_dev(struct bch_fs *c, struct bch_dev *

static inline struct bch_dev *bch2_get_next_dev(struct bch_fs *c, struct bch_dev *ca)
{
	rcu_read_lock();
	if (ca)
		percpu_ref_put(&ca->ref);

	rcu_read_lock();
	if ((ca = __bch2_next_dev(c, ca, NULL)))
		percpu_ref_get(&ca->ref);
	rcu_read_unlock();
@@ -132,10 +132,10 @@ static inline struct bch_dev *bch2_get_next_online_dev(struct bch_fs *c,
						       struct bch_dev *ca,
						       unsigned state_mask)
{
	rcu_read_lock();
	if (ca)
		percpu_ref_put(&ca->io_ref);

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