aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiguel Ojeda <ojeda@kernel.org>2026-06-09 04:18:30 +0200
committerMiguel Ojeda <ojeda@kernel.org>2026-06-09 04:25:04 +0200
commit98cc68794c003bc76fc9da2e8a075e947eee5921 (patch)
tree3d8521950d137a9ab38233e1aab74ccbb46b6940
parent759da9e0fa79223138ec8c18b9075cd7dace6070 (diff)
parent506bb8742ea52da13f9f281c5cb2b603ac1931e7 (diff)
Merge patch series "`zerocopy` support"
Introduce support for `zerocopy` [1][2]: Fast, safe, compile error. Pick two. Zerocopy makes zero-cost memory manipulation effortless. We write `unsafe` so you don't have to. It essentially provides derivable traits (e.g. `FromBytes`) and macros (e.g. `transmute!`) for safely converting between byte sequences and other types. Having such support allows us to remove some `unsafe` code. It is among the most downloaded Rust crates (top #50 recent, top #100 all-time downloads; according to crates.io), and it is also used by the Rust compiler itself. The series starts with a few preparation commits, then the `zerocopy` and `zerocopy-derive` crates are added. Finally, an example patch using it is on top, removing one `unsafe impl`. I had to adapt the crates slightly (just +2/-3 lines), but both patches could potentially be provided upstream eventually. Please see the commits for details. In total, it is about ~39k lines added, ~32k without counting `benches/` which are just for documentation purposes. See the cover letter for `syn` for some more details about depending on third-party crates in commit 54e3eae85562 ("Merge patch series "`syn` support""). The codegen of an isolated example function similar to the patch on top is essentially identical. It also turns out that (for that particular case) `zerocopy`'s version, even under `debug-assertions` enabled, has no remaining panics, unlike a few in the current code (because the compiler can prove the remaining `ub_checks` statically). So their "fast, safe" does indeed check out -- at least in that case. P.S. This version of `zerocopy` has already the unstable `Ptr{,Inner}` types -- to play with them, please use: make ... KRUSTFLAGS=--cfg=zerocopy_unstable_ptr Link: https://github.com/google/zerocopy [1] Link: https://docs.rs/zerocopy [2] Link: https://patch.msgid.link/20260608141439.182634-1-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
-rw-r--r--Makefile2
-rw-r--r--drivers/gpu/nova-core/firmware.rs5
-rw-r--r--drivers/gpu/nova-core/vbios.rs6
-rw-r--r--rust/Makefile96
-rw-r--r--rust/kernel/prelude.rs6
-rw-r--r--rust/zerocopy-derive/README.md14
-rw-r--r--rust/zerocopy-derive/derive/from_bytes.rs192
-rw-r--r--rust/zerocopy-derive/derive/into_bytes.rs164
-rw-r--r--rust/zerocopy-derive/derive/known_layout.rs350
-rw-r--r--rust/zerocopy-derive/derive/mod.rs132
-rw-r--r--rust/zerocopy-derive/derive/try_from_bytes.rs765
-rw-r--r--rust/zerocopy-derive/derive/unaligned.rs80
-rw-r--r--rust/zerocopy-derive/lib.rs146
-rw-r--r--rust/zerocopy-derive/repr.rs851
-rw-r--r--rust/zerocopy-derive/util.rs845
-rw-r--r--rust/zerocopy/README.md14
-rw-r--r--rust/zerocopy/benches/as_bytes_dynamic_size.rs9
-rw-r--r--rust/zerocopy/benches/as_bytes_dynamic_size.x86-645
-rw-r--r--rust/zerocopy/benches/as_bytes_dynamic_size.x86-64.mca47
-rw-r--r--rust/zerocopy/benches/as_bytes_static_size.rs9
-rw-r--r--rust/zerocopy/benches/as_bytes_static_size.x86-644
-rw-r--r--rust/zerocopy/benches/as_bytes_static_size.x86-64.mca45
-rw-r--r--rust/zerocopy/benches/extend_vec_zeroed.rs9
-rw-r--r--rust/zerocopy/benches/extend_vec_zeroed.x86-6460
-rw-r--r--rust/zerocopy/benches/extend_vec_zeroed.x86-64.mca147
-rw-r--r--rust/zerocopy/benches/formats/coco_dynamic_padding.rs24
-rw-r--r--rust/zerocopy/benches/formats/coco_dynamic_size.rs27
-rw-r--r--rust/zerocopy/benches/formats/coco_static_size.rs27
-rw-r--r--rust/zerocopy/benches/insert_vec_zeroed.rs13
-rw-r--r--rust/zerocopy/benches/insert_vec_zeroed.x86-6479
-rw-r--r--rust/zerocopy/benches/insert_vec_zeroed.x86-64.mca183
-rw-r--r--rust/zerocopy/benches/new_box_zeroed.rs9
-rw-r--r--rust/zerocopy/benches/new_box_zeroed.x86-647
-rw-r--r--rust/zerocopy/benches/new_box_zeroed.x86-64.mca51
-rw-r--r--rust/zerocopy/benches/new_box_zeroed_with_elems_dynamic_padding.rs11
-rw-r--r--rust/zerocopy/benches/new_box_zeroed_with_elems_dynamic_padding.x86-6424
-rw-r--r--rust/zerocopy/benches/new_box_zeroed_with_elems_dynamic_padding.x86-64.mca81
-rw-r--r--rust/zerocopy/benches/new_box_zeroed_with_elems_dynamic_size.rs9
-rw-r--r--rust/zerocopy/benches/new_box_zeroed_with_elems_dynamic_size.x86-6422
-rw-r--r--rust/zerocopy/benches/new_box_zeroed_with_elems_dynamic_size.x86-64.mca77
-rw-r--r--rust/zerocopy/benches/new_vec_zeroed.rs9
-rw-r--r--rust/zerocopy/benches/new_vec_zeroed.x86-6440
-rw-r--r--rust/zerocopy/benches/new_vec_zeroed.x86-64.mca113
-rw-r--r--rust/zerocopy/benches/new_zeroed.rs9
-rw-r--r--rust/zerocopy/benches/new_zeroed.x86-643
-rw-r--r--rust/zerocopy/benches/new_zeroed.x86-64.mca43
-rw-r--r--rust/zerocopy/benches/read_from_bytes.rs7
-rw-r--r--rust/zerocopy/benches/read_from_bytes.x86-6415
-rw-r--r--rust/zerocopy/benches/read_from_bytes.x86-64.mca65
-rw-r--r--rust/zerocopy/benches/read_from_prefix.rs10
-rw-r--r--rust/zerocopy/benches/read_from_prefix.x86-6414
-rw-r--r--rust/zerocopy/benches/read_from_prefix.x86-64.mca63
-rw-r--r--rust/zerocopy/benches/read_from_suffix.rs10
-rw-r--r--rust/zerocopy/benches/read_from_suffix.x86-6415
-rw-r--r--rust/zerocopy/benches/read_from_suffix.x86-64.mca65
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_dynamic_padding.rs7
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_dynamic_padding.x86-6422
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_dynamic_padding.x86-64.mca77
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_dynamic_size.rs7
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_dynamic_size.x86-6420
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_dynamic_size.x86-64.mca75
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_static_size.rs7
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_static_size.x86-648
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_static_size.x86-64.mca53
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_with_elems_dynamic_padding.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_with_elems_dynamic_padding.x86-6419
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_with_elems_dynamic_padding.x86-64.mca71
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_with_elems_dynamic_size.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_with_elems_dynamic_size.x86-6416
-rw-r--r--rust/zerocopy/benches/ref_from_bytes_with_elems_dynamic_size.x86-64.mca65
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_dynamic_padding.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_dynamic_padding.x86-6422
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_dynamic_padding.x86-64.mca77
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_dynamic_size.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_dynamic_size.x86-6417
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_dynamic_size.x86-64.mca67
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_static_size.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_static_size.x86-648
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_static_size.x86-64.mca53
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_with_elems_dynamic_padding.rs13
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_with_elems_dynamic_padding.x86-6426
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_with_elems_dynamic_padding.x86-64.mca85
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_with_elems_dynamic_size.rs13
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_with_elems_dynamic_size.x86-6422
-rw-r--r--rust/zerocopy/benches/ref_from_prefix_with_elems_dynamic_size.x86-64.mca77
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_dynamic_padding.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_dynamic_padding.x86-6423
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_dynamic_padding.x86-64.mca79
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_dynamic_size.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_dynamic_size.x86-6413
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_dynamic_size.x86-64.mca63
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_static_size.rs10
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_static_size.x86-6413
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_static_size.x86-64.mca61
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_with_elems_dynamic_padding.rs13
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_with_elems_dynamic_padding.x86-6427
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_with_elems_dynamic_padding.x86-64.mca85
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_with_elems_dynamic_size.rs13
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_with_elems_dynamic_size.x86-6423
-rw-r--r--rust/zerocopy/benches/ref_from_suffix_with_elems_dynamic_size.x86-64.mca77
-rw-r--r--rust/zerocopy/benches/split_at_dynamic_padding.rs12
-rw-r--r--rust/zerocopy/benches/split_at_dynamic_padding.x86-6412
-rw-r--r--rust/zerocopy/benches/split_at_dynamic_padding.x86-64.mca59
-rw-r--r--rust/zerocopy/benches/split_at_dynamic_size.rs12
-rw-r--r--rust/zerocopy/benches/split_at_dynamic_size.x86-6412
-rw-r--r--rust/zerocopy/benches/split_at_dynamic_size.x86-64.mca59
-rw-r--r--rust/zerocopy/benches/split_at_unchecked_dynamic_padding.rs12
-rw-r--r--rust/zerocopy/benches/split_at_unchecked_dynamic_padding.x86-646
-rw-r--r--rust/zerocopy/benches/split_at_unchecked_dynamic_padding.x86-64.mca49
-rw-r--r--rust/zerocopy/benches/split_at_unchecked_dynamic_size.rs12
-rw-r--r--rust/zerocopy/benches/split_at_unchecked_dynamic_size.x86-646
-rw-r--r--rust/zerocopy/benches/split_at_unchecked_dynamic_size.x86-64.mca49
-rw-r--r--rust/zerocopy/benches/split_via_immutable_dynamic_padding.rs11
-rw-r--r--rust/zerocopy/benches/split_via_immutable_dynamic_padding.x86-6414
-rw-r--r--rust/zerocopy/benches/split_via_immutable_dynamic_padding.x86-64.mca65
-rw-r--r--rust/zerocopy/benches/split_via_immutable_dynamic_size.rs11
-rw-r--r--rust/zerocopy/benches/split_via_immutable_dynamic_size.x86-6413
-rw-r--r--rust/zerocopy/benches/split_via_immutable_dynamic_size.x86-64.mca63
-rw-r--r--rust/zerocopy/benches/split_via_runtime_check_dynamic_padding.rs11
-rw-r--r--rust/zerocopy/benches/split_via_runtime_check_dynamic_padding.x86-6422
-rw-r--r--rust/zerocopy/benches/split_via_runtime_check_dynamic_padding.x86-64.mca79