Re: [RFC PATCH 2/3] rust: device add support for dynamic debug to pr_debug!

From: Alice Ryhl
Date: Thu Jun 12 2025 - 03:20:11 EST


On Thu, Jun 12, 2025 at 6:02 AM Andrew Ballance
<andrewjballance@xxxxxxxxx> wrote:
>
> On 6/11/25 4:38 PM, Alice Ryhl wrote:
> > Please define a STATIC_KEY_INIT_FALSE constant in
> > rust/kernel/jump_label.rs and refer to it. You can use mem::zeroed()
> > in the definition of the constant.
>
> Will do for the v2.
>
> > No, we can't use mutable references like this. In Rust, the real
> > meaning of &mut is exclusive, not mutable. (And the real meaning of &
> > is shared.) We don't have exclusive access to the DEBUG_INFO static
> > here - the access is shared, so we must use &_ references instead of
> > &mut _ references here.
> >
> > Note that by using Opaque, it's possible to mutate the value even if
> > it's behind a &_ reference.
> > #[repr(transparent)]
> > pub struct _Ddebug {
> > pub inner: Opaque<bindings::_ddebug>,
> > }
> > and then you can do DEBUG_INFO.inner.get() to obtain a mutable raw
> > pointer to the contents.
> >
>
> Unfortunately, static_branch_unlikely does not work with keys contained
> within a Opaque, because it uses offset_of and sym. I can create a macro
> specifically for dealing with Opaque::<type_that_contains_a_static_key>
> because I imagine that many uses of static branch will end up wrapped
> in an Opaque.

Ah, fair enough. In that case, you can also do this:

$crate::print::dynamic_debug::dynamic_pr_debug(
&raw mut DEBUG_INFO,
format_args!($($f)*)
);

using &raw mut creates a raw pointer directly without creating a
reference, so that also solves my concern.

Alice