All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: Boming Kong <michaelkongboming@gmail.com>
Cc: rust-for-linux@vger.kernel.org, ojeda@kernel.org,
	acourbot@nvidia.com, gary@garyguo.net,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Jens Axboe" <axboe@kernel.dk>,
	nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2] rust: bitfield: implement compile-time bitfield! overlap check
Date: Sat, 5 Dec 2026 12:59:33 -0500	[thread overview]
Message-ID: <axQVXbwbR-U8aMIp@yury> (raw)
In-Reply-To: <20261204034830.117532-1-michaelkongboming@gmail.com>

On Thu, Dec 03, 2026 at 10:48:27PM -0500, Boming Kong wrote:
> This commit implemented compile-time bitfield! overlapping check to avoid
> unintended overlaps between fields within a bitfield! and cause subtle
> driver bug.
> 
> This commit also introduce per-field #[allow_overlap] for user to
> opt-out overlap checks.
> 
> Updated nova-core's MMU ver2.rs PDE and PTE definition to adapt this new
> function.
> 
> As register! builds upon bitfield!, it inherits overlap checking without
> requiring changes to it's own implementation
> 
> Previous discussion: https://lore.kernel.org/rust-for-linux/20261130081359.158313-1-michaelkongboming@gmail.com/
> 
> Signed-off-by: Boming Kong <michaelkongboming@gmail.com>
> ---
> 
> Changes in v2:
> - Enable overlap checking by default.
> - Introduce per-field #[allow_overlap]
> - Update affected nova-core mmu v2 definitions and existing tests.
> - Drop the separate register! patch, as register! now inherits
>   overlap checking from bitfield!.
> 
> Note: overlapping fields without #[allow_overlap] would fail to compile
> as intended, but KUnit cannot reveal this case so I verified it
> manually. Is this acceptable or are there any preferred ways to test
> this scenario?
> 
>  drivers/gpu/nova-core/mm/pagetable/ver2.rs |  2 ++
>  rust/kernel/bitfield.rs                    | 38 ++++++++++++++++++++--
>  2 files changed, 38 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/nova-core/mm/pagetable/ver2.rs b/drivers/gpu/nova-core/mm/pagetable/ver2.rs
> index d7169a0fcf..59f6f40c88 100644
> --- a/drivers/gpu/nova-core/mm/pagetable/ver2.rs
> +++ b/drivers/gpu/nova-core/mm/pagetable/ver2.rs
> @@ -109,6 +109,7 @@ pub(in crate::mm) struct Pte(u64) {
>          /// Atomic operations disabled.
>          7:7     atomic_disable;
>          /// Frame number for system memory.
> +        #[allow_overlap]
>          53:8    frame_number_sys => Pfn;
>          /// Frame number for video memory.
>          32:8    frame_number_vid => Pfn;
> @@ -171,6 +172,7 @@ pub(in crate::mm) struct Pde(u64) {
>          /// Disable Address Translation Services.
>          5:5     no_ats;
>          /// Table frame number for system memory.
> +        #[allow_overlap]
>          53:8    table_frame_sys => Pfn;
>          /// Table frame number for video memory.
>          32:8    table_frame_vid => Pfn;

Unfortunately you ignored my comment in v1. Repeating it here.

This is not overlap, this is embedding, and it should not require any
annotation. If it's overlapping like 

           53:8    table_frame_sys => Pfn;
           32:0    table_frame_vid => Pfn;

then yes, it may need stricter rules.

> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
> index 8d7d781753..fe67cfe9a4 100644
> --- a/rust/kernel/bitfield.rs
> +++ b/rust/kernel/bitfield.rs
> @@ -359,7 +359,7 @@ unsafe impl $crate::mem::AsReprMut for $name {}
>      // Definitions requiring knowledge of individual fields: private and public field accessors,
>      // and `Debug` implementation.
>      (@fields $vis:vis $name:ident $storage:ty {
> -        $($(#[doc = $doc:expr])* $hi:literal:$lo:literal $field:ident
> +        $($(#[$attr:ident $(= $doc:expr)?])* $hi:literal:$lo:literal $field:ident
>              $(?=> $try_into_type:ty)?
>              $(=> $into_type:ty)?
>          ;
> @@ -371,7 +371,7 @@ impl $name {
>          $(
>          $crate::bitfield!(@private_field_accessors $vis $name $storage : $hi:$lo $field);
>          $crate::bitfield!(
> -            @public_field_accessors $(#[doc = $doc])* $vis $name $storage : $hi:$lo $field
> +            @public_field_accessors $($(#[doc = $doc])?)* $vis $name $storage : $hi:$lo $field
>              $(?=> $try_into_type)?
>              $(=> $into_type)?
>          );
> @@ -379,6 +379,9 @@ impl $name {
>          }
>  
>          $crate::bitfield!(@debug $name { $($field;)* });
> +        $crate::bitfield!(@lohi_chk $name $storage {
> +            $($(#[$attr $(= $doc)?])* $field;)*
> +        });
>      };
>  
>      // Private field accessors working with the exact `Bounded` type for the field.
> @@ -540,6 +543,35 @@ const fn [<__with_ $field>](
>          );
>      };
>  
> +    (@allow_overlap doc = $doc:expr) => { false };
> +    (@allow_overlap allow_overlap) => { true };
> +
> +    (@lohi_chk $name:ident $storage:ty {
> +        $($(#[$attr:ident $(= $doc:expr)?])* $field:ident;)*
> +    }) => {
> +        ::kernel::macros::paste!(
> +        const _: () = {
> +            let masks: &[$storage] = &[
> +                $(if false $(|| $crate::bitfield!(@allow_overlap $attr $(= $doc)?))* {
> +                    0
> +                } else {
> +                    $name::[<$field:upper _MASK>]
> +                }),*
> +            ];
> +            let err_msg: &[&str] = &[
> +                $(concat!(stringify!($field), " overlaps a previous field")),*
> +            ];
> +            let mut used: $storage = 0;
> +            let mut i = 0;
> +            while i < masks.len() {
> +                ::core::assert!(used & masks[i] == 0, "{}", err_msg[i]);
> +                used |= masks[i];
> +                i += 1;
> +            }
> +        };
> +        );
> +    };
> +
>      // `Debug` implementation.
>      (@debug $name:ident { $($field:ident;)* }) => {
>          impl ::kernel::fmt::Debug for $name {
> @@ -637,6 +669,7 @@ struct TestU64(u64) {
>          struct TestU16(u16) {
>              15:8      field_15_8;
>              7:4       field_7_4; // Partial overlap with `field_5_4`.
> +            #[allow_overlap]
>              5:4       field_5_4 => Priority;
>              3:1       field_3_1;
>              0:0       field_0;
> @@ -645,6 +678,7 @@ struct TestU16(u16) {
>  
>      bitfield! {
>          struct TestU8(u8) {
> +            #[allow_overlap]
>              7:0       field_7_0; // Full byte overlap.
>              7:4       field_7_4;
>              3:2       field_3_2;
> -- 
> 2.56.1

      parent reply	other threads:[~2026-12-05 17:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  3:48 [PATCH v2] rust: bitfield: implement compile-time bitfield! overlap check Boming Kong
2026-12-04  4:04 ` sashiko-bot
2026-12-05  9:46 ` Shuai Liu
2026-12-05 17:59 ` Yury Norov [this message]

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=axQVXbwbR-U8aMIp@yury \
    --to=ynorov@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.deucher@amd.com \
    --cc=aliceryhl@google.com \
    --cc=axboe@kernel.dk \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kuba@kernel.org \
    --cc=lossin@kernel.org \
    --cc=michaelkongboming@gmail.com \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    --cc=yury.norov@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.