From: Ankit Kapoor <ankitkap@google.com>
To: Coly Li <colyli@fnnas.com>, Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org,
Ankit Kapoor <ankitkap@google.com>
Subject: [PATCH 1/1] bcache: fix stale data race between read cache miss and bypass write
Date: Thu, 21 May 2026 16:39:25 +0000 [thread overview]
Message-ID: <20260521163925.178264-2-ankitkap@google.com> (raw)
In-Reply-To: <20260521163925.178264-1-ankitkap@google.com>
A race condition exists between a read cache miss and a bypass write
due to either congestion or sequential bypass, that causes stale data
to be cached when the read cache miss runs concurrently with a bypass
write targeting the same sectors. If the read cache miss fetches data
from the backing device before the write to the backing device,
stale data populates the cache.
The root cause is that bcache currently executes btree key
invalidation in parallel with (or prior to) writing the actual data
payload to the backing device. Under this sequence, a concurrent
read path can register a cache miss and insert a placeholder key.
If the write's btree key invalidation completes before the read finishes
fetching old data from the backing device, the read's subsequent
key replacement will not detect a collision, allowing stale data
to persist in the cache.
Fix this by deferring the btree key invalidation until after the
backing device write completes successfully. Enforcing this
sequential execution ensures that a stale read is always detected
and invalidated.
Signed-off-by: Ankit Kapoor <ankitkap@google.com>
---
drivers/md/bcache/request.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index af345dc6fde1..ef2cf55df3bb 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -978,6 +978,14 @@ static CLOSURE_CALLBACK(cached_dev_write_complete)
cached_dev_bio_complete(&cl->work);
}
+static CLOSURE_CALLBACK(backing_device_bypass_write_complete)
+{
+ closure_type(s, struct search, cl);
+
+ closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
+ continue_at(cl, cached_dev_write_complete, NULL);
+}
+
static void cached_dev_write(struct cached_dev *dc, struct search *s)
{
struct closure *cl = &s->cl;
@@ -1058,6 +1066,11 @@ static void cached_dev_write(struct cached_dev *dc, struct search *s)
}
insert_data:
+ if (s->iop.bypass) {
+ continue_at(cl, backing_device_bypass_write_complete, NULL);
+ return;
+ }
+
closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
continue_at(cl, cached_dev_write_complete, NULL);
}
--
2.54.0.669.g59709faab0-goog
next prev parent reply other threads:[~2026-05-21 16:39 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-21 16:39 [PATCH 0/1] bcache: fix stale data race between read cache miss and bypass write Ankit Kapoor
2026-05-21 16:39 ` Ankit Kapoor [this message]
2026-05-25 13:41 ` [PATCH 1/1] " Coly Li
2026-05-27 13:41 ` Ankit Kapoor
2026-05-27 15:27 ` Coly Li
2026-05-24 16:12 ` [PATCH 0/1] " Coly Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260521163925.178264-2-ankitkap@google.com \
--to=ankitkap@google.com \
--cc=colyli@fnnas.com \
--cc=kent.overstreet@linux.dev \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox