Skip to contentSkip to conversation

bcachefs: omit alignment attribute on big endian struct bkey

PATCH Applied Thomas Bertschinger 2 participants 2 messages Feb 16, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bcachefs: omit alignment attribute on big endian struct bkey

This is needed for building Rust bindings on big endian architectures like s390x. Currently this is only done in userspace, but it might happen in-kernel in the future. When creating a Rust binding for struct bkey, the "packed" attribute is needed to get a type with the correct member offsets in the big endian case. However, rustc does not allow types to have both a "packed" and "align" attribute. Thus, in order to get a Rust type compatible with the C type, we must omit the "aligned" attribute in C.

This does not affect the struct's size or member offsets, only its toplevel alignment, which should be an acceptable impact.

The little endian version can have the "align" attribute because the "packed" attr is redundant, and rust-bindgen will omit the "packed" attr when an "align" attr is present and it can do so without changing a type's layout

---
v2: add comment explaining why this is needed

 fs/bcachefs/bcachefs_format.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/bcachefs/bcachefs_format.h +11 -1
index 1bb24aa73528..00060bc6128f 100644
@@ -222,7 +222,17 @@ struct bkey {
 
 	__u8		pad[1];
 #endif
-} __packed __aligned(8);
+} __packed
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+/*
+ * The big-endian version of bkey can't be compiled by rustc with the "aligned"
+ * attr since it doesn't allow types to have both "packed" and "aligned" attrs.
+ * So for Rust compatibility, don't include this. It can be included in the LE
+ * version because the "packed" attr is redundant in that case.
+ */
+__aligned(8)
+#endif
+;
 
 struct bkey_packed {
 	__u64		_data[0];
Signature · 1 line
2.43.0
#1 lore raw
Quoted 12 lines · Thomas Bertschinger

This is needed for building Rust bindings on big endian architectures like s390x. Currently this is only done in userspace, but it might happen in-kernel in the future. When creating a Rust binding for struct bkey, the "packed" attribute is needed to get a type with the correct member offsets in the big endian case. However, rustc does not allow types to have both a "packed" and "align" attribute. Thus, in order to get a Rust type compatible with the C type, we must omit the "aligned" attribute in C.

This does not affect the struct's size or member offsets, only its toplevel alignment, which should be an acceptable impact.

Quoted 6 lines

The little endian version can have the "align" attribute because the "packed" attr is redundant, and rust-bindgen will omit the "packed" attr when an "align" attr is present and it can do so without changing a type's layout

Thanks, applied