linux-bcachefs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] kmsan splat fixes
@ 2025-03-20 21:32 Kent Overstreet
  2025-03-20 21:32 ` [PATCH 1/5] bcachefs: Disable asm memcpys when kmsan enabled Kent Overstreet
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Kent Overstreet @ 2025-03-20 21:32 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: Kent Overstreet

we should be just about kmsan clean now; not 100%, the kmsan tests are
extremely heavy so I'm not flipping them on in the CI yet.

none of these were actual bugs, but this will make a massive dent in the
syzbot dashboard.

Kent Overstreet (5):
  bcachefs: Disable asm memcpys when kmsan enabled
  bcachefs: Fix kmsan warnings in bch2_extent_crc_pack()
  bcachefs: kmsan asserts
  bcachefs: Fix a KMSAN splat in btree_update_nodes_written()
  bcachefs: move_bucket_key -> __packed

 fs/bcachefs/btree_trans_commit.c    |  1 +
 fs/bcachefs/btree_update.c          |  2 ++
 fs/bcachefs/btree_update.h          |  2 ++
 fs/bcachefs/btree_update_interior.c | 18 +++++++-------
 fs/bcachefs/extents.c               | 38 +++++++++++++++++------------
 fs/bcachefs/move_types.h            |  2 +-
 fs/bcachefs/util.h                  |  4 +--
 7 files changed, 39 insertions(+), 28 deletions(-)

-- 
2.49.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] bcachefs: Disable asm memcpys when kmsan enabled
  2025-03-20 21:32 [PATCH 0/5] kmsan splat fixes Kent Overstreet
@ 2025-03-20 21:32 ` Kent Overstreet
  2025-03-20 21:32 ` [PATCH 2/5] bcachefs: Fix kmsan warnings in bch2_extent_crc_pack() Kent Overstreet
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Kent Overstreet @ 2025-03-20 21:32 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: Kent Overstreet

kmsan doesn't know about inline assembly, obviously; this will close a
ton of syzbot bugs.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/util.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/bcachefs/util.h b/fs/bcachefs/util.h
index d41e133acc4d..7d921fc920a0 100644
--- a/fs/bcachefs/util.h
+++ b/fs/bcachefs/util.h
@@ -431,7 +431,7 @@ static inline void memcpy_u64s_small(void *dst, const void *src,
 static inline void __memcpy_u64s(void *dst, const void *src,
 				 unsigned u64s)
 {
-#ifdef CONFIG_X86_64
+#if defined(CONFIG_X86_64) && !defined(CONFIG_KMSAN)
 	long d0, d1, d2;
 
 	asm volatile("rep ; movsq"
@@ -508,7 +508,7 @@ static inline void __memmove_u64s_up(void *_dst, const void *_src,
 	u64 *dst = (u64 *) _dst + u64s - 1;
 	u64 *src = (u64 *) _src + u64s - 1;
 
-#ifdef CONFIG_X86_64
+#if defined(CONFIG_X86_64) && !defined(CONFIG_KMSAN)
 	long d0, d1, d2;
 
 	asm volatile("std ;\n"
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/5] bcachefs: Fix kmsan warnings in bch2_extent_crc_pack()
  2025-03-20 21:32 [PATCH 0/5] kmsan splat fixes Kent Overstreet
  2025-03-20 21:32 ` [PATCH 1/5] bcachefs: Disable asm memcpys when kmsan enabled Kent Overstreet
@ 2025-03-20 21:32 ` Kent Overstreet
  2025-03-20 21:32 ` [PATCH 3/5] bcachefs: kmsan asserts Kent Overstreet
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Kent Overstreet @ 2025-03-20 21:32 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: Kent Overstreet

We store to all fields, so the kmsan warnings were spurious - but
initializing via stores to bitfields appear to have been giving the
compiler/kmsan trouble, and they're not necessary.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/extents.c | 38 ++++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/fs/bcachefs/extents.c b/fs/bcachefs/extents.c
index 04946d9911f5..ae1a1d917805 100644
--- a/fs/bcachefs/extents.c
+++ b/fs/bcachefs/extents.c
@@ -592,29 +592,35 @@ static void bch2_extent_crc_pack(union bch_extent_crc *dst,
 				 struct bch_extent_crc_unpacked src,
 				 enum bch_extent_entry_type type)
 {
-#define set_common_fields(_dst, _src)					\
-		_dst.type		= 1 << type;			\
-		_dst.csum_type		= _src.csum_type,		\
-		_dst.compression_type	= _src.compression_type,	\
-		_dst._compressed_size	= _src.compressed_size - 1,	\
-		_dst._uncompressed_size	= _src.uncompressed_size - 1,	\
-		_dst.offset		= _src.offset
+#define common_fields(_src)						\
+		.type			= BIT(type),			\
+		.csum_type		= _src.csum_type,		\
+		.compression_type	= _src.compression_type,	\
+		._compressed_size	= _src.compressed_size - 1,	\
+		._uncompressed_size	= _src.uncompressed_size - 1,	\
+		.offset			= _src.offset
 
 	switch (type) {
 	case BCH_EXTENT_ENTRY_crc32:
-		set_common_fields(dst->crc32, src);
-		dst->crc32.csum		= (u32 __force) *((__le32 *) &src.csum.lo);
+		dst->crc32		= (struct bch_extent_crc32) {
+			common_fields(src),
+			.csum		= (u32 __force) *((__le32 *) &src.csum.lo),
+		};
 		break;
 	case BCH_EXTENT_ENTRY_crc64:
-		set_common_fields(dst->crc64, src);
-		dst->crc64.nonce	= src.nonce;
-		dst->crc64.csum_lo	= (u64 __force) src.csum.lo;
-		dst->crc64.csum_hi	= (u64 __force) *((__le16 *) &src.csum.hi);
+		dst->crc64		= (struct bch_extent_crc64) {
+			common_fields(src),
+			.nonce		= src.nonce,
+			.csum_lo	= (u64 __force) src.csum.lo,
+			.csum_hi	= (u64 __force) *((__le16 *) &src.csum.hi),
+		};
 		break;
 	case BCH_EXTENT_ENTRY_crc128:
-		set_common_fields(dst->crc128, src);
-		dst->crc128.nonce	= src.nonce;
-		dst->crc128.csum	= src.csum;
+		dst->crc128		= (struct bch_extent_crc128) {
+			common_fields(src),
+			.nonce		= src.nonce,
+			.csum		= src.csum,
+		};
 		break;
 	default:
 		BUG();
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/5] bcachefs: kmsan asserts
  2025-03-20 21:32 [PATCH 0/5] kmsan splat fixes Kent Overstreet
  2025-03-20 21:32 ` [PATCH 1/5] bcachefs: Disable asm memcpys when kmsan enabled Kent Overstreet
  2025-03-20 21:32 ` [PATCH 2/5] bcachefs: Fix kmsan warnings in bch2_extent_crc_pack() Kent Overstreet
@ 2025-03-20 21:32 ` Kent Overstreet
  2025-03-20 21:32 ` [PATCH 4/5] bcachefs: Fix a KMSAN splat in btree_update_nodes_written() Kent Overstreet
  2025-03-20 21:32 ` [PATCH 5/5] bcachefs: move_bucket_key -> __packed Kent Overstreet
  4 siblings, 0 replies; 6+ messages in thread
From: Kent Overstreet @ 2025-03-20 21:32 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: Kent Overstreet

Catching these early makes them a lot easier to track down.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/btree_trans_commit.c | 1 +
 fs/bcachefs/btree_update.c       | 2 ++
 fs/bcachefs/btree_update.h       | 2 ++
 3 files changed, 5 insertions(+)

diff --git a/fs/bcachefs/btree_trans_commit.c b/fs/bcachefs/btree_trans_commit.c
index d50dc31d0bea..7d7e52ddde02 100644
--- a/fs/bcachefs/btree_trans_commit.c
+++ b/fs/bcachefs/btree_trans_commit.c
@@ -164,6 +164,7 @@ bool bch2_btree_bset_insert_key(struct btree_trans *trans,
 	EBUG_ON(bpos_gt(insert->k.p, b->data->max_key));
 	EBUG_ON(insert->k.u64s > bch2_btree_keys_u64s_remaining(b));
 	EBUG_ON(!b->c.level && !bpos_eq(insert->k.p, path->pos));
+	kmsan_check_memory(insert, bkey_bytes(&insert->k));
 
 	k = bch2_btree_node_iter_peek_all(node_iter, b);
 	if (k && bkey_cmp_left_packed(b, k, &insert->k.p))
diff --git a/fs/bcachefs/btree_update.c b/fs/bcachefs/btree_update.c
index b3e346b5f8d7..bd2eb42edb24 100644
--- a/fs/bcachefs/btree_update.c
+++ b/fs/bcachefs/btree_update.c
@@ -512,6 +512,8 @@ static noinline int bch2_trans_update_get_key_cache(struct btree_trans *trans,
 int __must_check bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
 				   struct bkey_i *k, enum btree_iter_update_trigger_flags flags)
 {
+	kmsan_check_memory(k, bkey_bytes(&k->k));
+
 	btree_path_idx_t path_idx = iter->update_path ?: iter->path;
 	int ret;
 
diff --git a/fs/bcachefs/btree_update.h b/fs/bcachefs/btree_update.h
index 47d8690f01bf..d2e1c04353f6 100644
--- a/fs/bcachefs/btree_update.h
+++ b/fs/bcachefs/btree_update.h
@@ -133,6 +133,8 @@ static inline int __must_check bch2_trans_update_buffered(struct btree_trans *tr
 					    enum btree_id btree,
 					    struct bkey_i *k)
 {
+	kmsan_check_memory(k, bkey_bytes(&k->k));
+
 	if (unlikely(!btree_type_uses_write_buffer(btree))) {
 		int ret = bch2_btree_write_buffer_insert_err(trans, btree, k);
 		dump_stack();
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/5] bcachefs: Fix a KMSAN splat in btree_update_nodes_written()
  2025-03-20 21:32 [PATCH 0/5] kmsan splat fixes Kent Overstreet
                   ` (2 preceding siblings ...)
  2025-03-20 21:32 ` [PATCH 3/5] bcachefs: kmsan asserts Kent Overstreet
@ 2025-03-20 21:32 ` Kent Overstreet
  2025-03-20 21:32 ` [PATCH 5/5] bcachefs: move_bucket_key -> __packed Kent Overstreet
  4 siblings, 0 replies; 6+ messages in thread
From: Kent Overstreet @ 2025-03-20 21:32 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: Kent Overstreet

We may sometimes read from uninitialized memory; we know, and that's ok.

We check if a btree node has been reused before waiting on any
outstanding IO.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/btree_update_interior.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/fs/bcachefs/btree_update_interior.c b/fs/bcachefs/btree_update_interior.c
index d3e0cf01ba37..67f1e3202835 100644
--- a/fs/bcachefs/btree_update_interior.c
+++ b/fs/bcachefs/btree_update_interior.c
@@ -649,6 +649,14 @@ static int btree_update_nodes_written_trans(struct btree_trans *trans,
 	return 0;
 }
 
+/* If the node has been reused, we might be reading uninitialized memory - that's fine: */
+static noinline __no_kmsan_checks bool btree_node_seq_matches(struct btree *b, __le64 seq)
+{
+	struct btree_node *b_data = READ_ONCE(b->data);
+
+	return (b_data ? b_data->keys.seq : 0) == seq;
+}
+
 static void btree_update_nodes_written(struct btree_update *as)
 {
 	struct bch_fs *c = as->c;
@@ -677,17 +685,9 @@ static void btree_update_nodes_written(struct btree_update *as)
 	 * on disk:
 	 */
 	for (i = 0; i < as->nr_old_nodes; i++) {
-		__le64 seq;
-
 		b = as->old_nodes[i];
 
-		bch2_trans_begin(trans);
-		btree_node_lock_nopath_nofail(trans, &b->c, SIX_LOCK_read);
-		seq = b->data ? b->data->keys.seq : 0;
-		six_unlock_read(&b->c.lock);
-		bch2_trans_unlock_long(trans);
-
-		if (seq == as->old_nodes_seq[i])
+		if (btree_node_seq_matches(b, as->old_nodes_seq[i]))
 			wait_on_bit_io(&b->flags, BTREE_NODE_write_in_flight_inner,
 				       TASK_UNINTERRUPTIBLE);
 	}
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 5/5] bcachefs: move_bucket_key -> __packed
  2025-03-20 21:32 [PATCH 0/5] kmsan splat fixes Kent Overstreet
                   ` (3 preceding siblings ...)
  2025-03-20 21:32 ` [PATCH 4/5] bcachefs: Fix a KMSAN splat in btree_update_nodes_written() Kent Overstreet
@ 2025-03-20 21:32 ` Kent Overstreet
  4 siblings, 0 replies; 6+ messages in thread
From: Kent Overstreet @ 2025-03-20 21:32 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: Kent Overstreet

we appear to be tripping over a compiler/kmsan bug with padding fields -
this is an easy workaround.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/move_types.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/bcachefs/move_types.h b/fs/bcachefs/move_types.h
index 82e473ed48d2..31171a5bf0e1 100644
--- a/fs/bcachefs/move_types.h
+++ b/fs/bcachefs/move_types.h
@@ -33,7 +33,7 @@ struct bch_move_stats {
 struct move_bucket_key {
 	struct bpos		bucket;
 	u8			gen;
-};
+} __packed;
 
 struct move_bucket {
 	struct move_bucket_key	k;
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-03-20 21:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-20 21:32 [PATCH 0/5] kmsan splat fixes Kent Overstreet
2025-03-20 21:32 ` [PATCH 1/5] bcachefs: Disable asm memcpys when kmsan enabled Kent Overstreet
2025-03-20 21:32 ` [PATCH 2/5] bcachefs: Fix kmsan warnings in bch2_extent_crc_pack() Kent Overstreet
2025-03-20 21:32 ` [PATCH 3/5] bcachefs: kmsan asserts Kent Overstreet
2025-03-20 21:32 ` [PATCH 4/5] bcachefs: Fix a KMSAN splat in btree_update_nodes_written() Kent Overstreet
2025-03-20 21:32 ` [PATCH 5/5] bcachefs: move_bucket_key -> __packed Kent Overstreet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).