All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: ojeda@kernel.org, linux-modules@vger.kernel.org,
	gary@garyguo.net, "Petr Pavlu" <petr.pavlu@suse.com>
Subject: Re: [PATCH RFC 08/11] rust: add support for dynamic debug
Date: Sun, 11 Oct 2026 13:08:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-167246@kernel.org> (raw)
In-Reply-To: <20261011-rust-dyndbg-v1-8-832e4039b71f@weissschuh.net>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] printk: dynamic debug formatting prefixes are silently ignored in Rust
- [Low] rust: data race due to mixed atomic/non-atomic access on C bitfield flags
--

commit 62390fa941837d8feaa389e3a6ea73a463056133
Author: Thomas Weißschuh <linux@weissschuh.net>

rust: add support for dynamic debug

This commit adds the Rust infrastructure to integrate with the dynamic debug
framework. It introduces a descriptor wrapper and macros to wire up the
conditional debug calls, with static keys and custom classes planned for
the future.

> diff --git a/rust/kernel/dynamic_debug.rs b/rust/kernel/dynamic_debug.rs
> new file mode 100644
> index 0000000000000..2fdc691eaac94
> --- /dev/null
> +++ b/rust/kernel/dynamic_debug.rs

[ ... ]

> +    #[inline(always)]
> +    pub unsafe fn branch_and_stack<F: FnOnce()>(desc: *mut Descriptor<'static>, func: F) {
> +        // TODO: Use static keys.
> +
> +        let flags_ptr = project!(mut desc, .0.flags);
> +        // SAFETY: As per the function safety requirement desc is valid, and properly aligned,
> +        // so flags_ptr is so, too. Updates from C are atomic.
> +        let flags = unsafe { atomic::atomic_load(flags_ptr, atomic::ordering::Relaxed) };
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: Low]
Does this atomic load in Rust race with non-atomic bitfield updates in C?

In the C core, the flags field is defined as an unsigned int bitfield
sharing a 32-bit memory location with lineno and class_id:

include/linux/dynamic_debug.h:struct _ddebug {
    ...
    unsigned int flags:8;
    ...
}

The C code modifies flags using non-atomic, unprotected read-modify-write
operations under a mutex:

lib/dynamic_debug.c:ddebug_change() {
    ...
    dp->flags = newflags;
    ...
}

Mixing atomic loads in Rust with non-atomic bitfield updates in C results
in a data race. While practically benign as word tearing on 32-bit aligned
words is rare, the assumption in the comments that C updates are atomic or
that flags is an independent byte appears factually incorrect.

> +
> +        if flags & flags::PRINT != 0 {
> +            func();
                 ^^^^^^

[Severity: Medium]
Does this parameterless closure execution prevent formatting prefixes?

In C, dynamic debug formats optional prefixes such as module name,
function name, line number, or thread ID via dynamic_emit_prefix()
using the _ddebug descriptor.

Because func() does not receive the descriptor, the underlying closure
will invoke printk directly without retrieving the formatted prefix. As a
result, all dynamic debug formatting flags (+p, +m, +f, +l, +t) configured
via debugfs appear to be silently ignored for Rust logs, degrading expected
kernel observability features.

[ ... ]

> +macro_rules! dynamic_func_call_cls {
> +    ($cls:expr, $fmt:expr, $func:expr) => {{
> +        const default_enabled: bool = cfg!(debug_assertions);
> +
> +        let descriptor = $crate::dynamic_debug_metadata_cls!(default_enabled, $cls, $fmt);
> +
> +        // SAFETY: descriptor has static lifetime, so it is always valid.
> +        unsafe {
> +            $crate::dynamic_debug::Descriptor::branch_and_stack(descriptor, $func);
                                                                               ^^^^^

[Severity: Medium]
Will this macro need to pass the descriptor into the func closure so
that the underlying logging macro can use it for dynamic prefix formatting?

> +        }
> +    }};
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261011-rust-dyndbg-v1-0-832e4039b71f@weissschuh.net?part=8

  reply	other threads:[~2026-10-11 13:08 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-11 12:52 [PATCH RFC 00/11] rust: add support for dynamic debug Thomas Weißschuh
2026-10-11 12:52 ` [PATCH RFC 01/11] rust: print: use CStr for __LOG_PREFIX Thomas Weißschuh
2026-10-11 13:01   ` Gary Guo
2026-10-11 14:43     ` Thomas Weißschuh
2026-10-11 12:52 ` [PATCH RFC 02/11] rust: sync: atomic: Add Atomic<u{8,16}> Thomas Weißschuh
2026-10-11 13:00   ` sashiko-bot
2026-10-11 13:04   ` Gary Guo
2026-10-11 12:52 ` [PATCH RFC 03/11] rust: num: Add Bounded::USABLE_BITS Thomas Weißschuh
2026-10-11 12:52 ` [PATCH RFC 04/11] samples: rust_print: add debug messages Thomas Weißschuh
2026-10-11 13:53   ` Miguel Ojeda
2026-10-11 14:48     ` Thomas Weißschuh
2026-10-11 12:52 ` [PATCH RFC 05/11] dyndbg: use u32 for struct _ddebug bitfields Thomas Weißschuh
2026-10-11 12:52 ` [PATCH RFC 06/11] dyndbg: make _ddebug::flags its own field Thomas Weißschuh
2026-10-11 13:23   ` Gary Guo
2026-10-11 14:47     ` Thomas Weißschuh
2026-10-11 15:44       ` Gary Guo
2026-10-11 12:52 ` [PATCH RFC 07/11] dyndbg: use READ_ONCE()/WRITE_ONCE() to access descriptor flags Thomas Weißschuh
2026-10-11 13:16   ` Gary Guo
2026-10-11 12:52 ` [PATCH RFC 08/11] rust: add support for dynamic debug Thomas Weißschuh
2026-10-11 13:08   ` sashiko-bot [this message]
2026-10-11 12:52 ` [PATCH RFC 09/11] rust: print: enable dynamic debug for pr_debug!() Thomas Weißschuh
2026-10-11 13:01   ` sashiko-bot
2026-10-11 12:52 ` [PATCH RFC 10/11] rust: device: remove logging methods Thomas Weißschuh
2026-10-11 13:01   ` sashiko-bot
2026-10-11 12:52 ` [PATCH RFC 11/11] rust: device: enable dynamic debug for dev_dbg!() Thomas Weißschuh
2026-10-11 13:01   ` sashiko-bot

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=sashiko-outbox-167246@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-modules@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=ojeda@kernel.org \
    --cc=petr.pavlu@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.