All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Markus Probst" <markus.probst@posteo.de>
Cc: linux-leds@vger.kernel.org, linux-pci@vger.kernel.org,
	lee@kernel.org, ojeda@kernel.org, gary@garyguo.net
Subject: Re: [PATCH v29 3/4] rust: leds: Add multicolor classdev abstractions
Date: Thu, 03 Dec 2026 22:00:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-248718@kernel.org> (raw)
In-Reply-To: <20261203-rust_leds-v29-3-dc8a8ccb96d7@posteo.de>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] rust: leds: unsynchronized led_mc_calc_color_components() in
  callbacks
- [Low] rust: leds: broken rustdoc links when LEDS_CLASS_MULTICOLOR=m
- [Low] rust: leds: missing INVARIANT comments and #[inline] in
  multicolor.rs
--

commit 1e49a309518af64831c1082f7d1d80c038a985c7
Author: Markus Probst <markus.probst@posteo.de>

rust: leds: Add multicolor classdev abstractions

This commit adds Rust abstractions for multicolor LED class devices
(struct led_classdev_mc and struct mc_subled), including the MultiColor
mode, MultiColorDevice, MultiColorSubLed, MultiColorSubLedBuilder, and
DeviceBuilder::build_multicolor().

> diff --git a/rust/kernel/led.rs b/rust/kernel/led.rs
> index f0c64821334ba..56ffc5bc4933a 100644
> --- a/rust/kernel/led.rs
> +++ b/rust/kernel/led.rs
> @@ -32,8 +32,17 @@
>      types::Opaque, //
>  };
>  
> +#[cfg(CONFIG_LEDS_CLASS_MULTICOLOR = "y")]
> +mod multicolor;
>  mod normal;
>  
> +#[cfg(CONFIG_LEDS_CLASS_MULTICOLOR = "y")]
> +pub use multicolor::{
> +    MultiColor,
> +    MultiColorDevice,
> +    MultiColorSubLed,
> +    MultiColorSubLedBuilder, //
> +};
>  pub use normal::{
>      Device,
>      Normal, //
> @@ -235,7 +244,24 @@ pub enum Color {
>      Violet = bindings::LED_COLOR_ID_VIOLET,
>      Yellow = bindings::LED_COLOR_ID_YELLOW,
>      Ir = bindings::LED_COLOR_ID_IR,
> +    #[cfg_attr(
> +        CONFIG_LEDS_CLASS_MULTICOLOR,
> +        doc = "Use this color for a [`MultiColor`] led."
> +    )]
> +    #[cfg_attr(
> +        not(CONFIG_LEDS_CLASS_MULTICOLOR),
> +        doc = "Use this color for a `MultiColor` led."
> +    )]
> +    /// If the led supports RGB, use [`Color::Rgb`] instead.
>      Multi = bindings::LED_COLOR_ID_MULTI,
> +    #[cfg_attr(
> +        CONFIG_LEDS_CLASS_MULTICOLOR,
> +        doc = "Use this color for a [`MultiColor`] led with rgb support."
> +    )]
> +    #[cfg_attr(
> +        not(CONFIG_LEDS_CLASS_MULTICOLOR),
> +        doc = "Use this color for a `MultiColor` led with rgb support."
> +    )]
>      Rgb = bindings::LED_COLOR_ID_RGB,
>      Purple = bindings::LED_COLOR_ID_PURPLE,
>      Orange = bindings::LED_COLOR_ID_ORANGE,
> @@ -268,7 +294,14 @@ fn try_from(value: u32) -> core::result::Result<Self, Self::Error> {
>  ///
>  /// Each led mode has its own led class device type with different capabilities.
>  ///
> -/// See [`Normal`].
> +#[cfg_attr(
> +    CONFIG_LEDS_CLASS_MULTICOLOR,
> +    doc = "See [`Normal`] and [`MultiColor`]."
> +)]
> +#[cfg_attr(
> +    not(CONFIG_LEDS_CLASS_MULTICOLOR),
> +    doc = "See [`Normal`] and `MultiColor`."
> +)]
>  pub trait Mode: private::Sealed {
>      /// The class device for the led mode.
>      type Device<'bound, T: LedOps<Mode = Self> + 'bound>: Deref<Target = T>;

[Severity: Low]
Will these cfg_attr doc attributes break make rustdoc when
CONFIG_LEDS_CLASS=y and CONFIG_LEDS_CLASS_MULTICOLOR=m?

LEDS_CLASS_MULTICOLOR is tristate, and conf_write_rustccfg() emits the bare
CONFIG_LEDS_CLASS_MULTICOLOR cfg for both y and m.  When it is set to m,
mod multicolor and MultiColor are not compiled because they are gated on
CONFIG_LEDS_CLASS_MULTICOLOR = "y", so the intra-doc links to MultiColor on
Color::Multi, Color::Rgb, and Mode become unresolved and fail rustdoc under
-Dwarnings.

Checking CONFIG_LEDS_CLASS_MULTICOLOR = "y" in these cfg_attr conditions
would match the module gate.

> diff --git a/rust/kernel/led/multicolor.rs b/rust/kernel/led/multicolor.rs
> new file mode 100644
> index 0000000000000..492a132579d6c
> --- /dev/null
> +++ b/rust/kernel/led/multicolor.rs

[ ... ]

> +    /// Build the sub led descriptor.
> +    #[inline]
> +    pub const fn build(self) -> MultiColorSubLed {
> +        MultiColorSubLed(Opaque::new(bindings::mc_subled {
> +            brightness: 0,
> +            channel: self.channel,
> +            color_index: self.color as u32,
> +            intensity: self.initial_intensity,
> +            max_intensity: if let Some(max_intensity) = self.max_intensity {
> +                max_intensity.get()
> +            } else {
> +                0
> +            },
> +        }))
> +    }

[ ... ]

> +impl Clone for MultiColorSubLed {
> +    fn clone(&self) -> Self {
> +        Self(Opaque::new(bindings::mc_subled {
> +            color_index: self.color() as u32,
> +            brightness: self.brightness(),
> +            intensity: self.intensity(),
> +            max_intensity: self.max_intensity().map(NonZero::get).unwrap_or_default(),
> +            channel: self.channel(),
> +        }))
> +    }
> +}

[ ... ]

> +impl<'bound, T: LedOps + 'bound> Deref for MultiColorDevice<'bound, T> {
> +    type Target = T;
> +
> +    fn deref(&self) -> &Self::Target {
> +        &self.ops
> +    }
> +}
> +
> +// SAFETY: A `led::MultiColorDevice` can be unregistered from any thread.
> +unsafe impl<'bound, T: 'bound + Send> Send for MultiColorDevice<'bound, T> {}
> +
> +// SAFETY: `led::MultiColorDevice` can be shared among threads because all methods of `led::Device`
> +// are thread safe.
> +unsafe impl<'bound, T: 'bound + Sync> Sync for MultiColorDevice<'bound, T> {}

[Severity: Low]
MultiColorSubLed documents an Invariants section, so
MultiColorSubLedBuilder::build() and MultiColorSubLed::clone() should carry
INVARIANT comments explaining why the invariants hold.

In addition, MultiColorSubLed::clone() and MultiColorDevice::deref() are
small kernel-crate methods missing #[inline], and the Sync safety comment on
MultiColorDevice refers to led::Device instead of led::MultiColorDevice.

[ ... ]

> +    unsafe extern "C" fn brightness_set_callback(
> +        led_cdev: *mut bindings::led_classdev,
> +        brightness: u32,
> +    ) {
> +        // SAFETY: The function's contract guarantees that `led_cdev` is a valid pointer to a
> +        // `led_classdev` embedded within a `led::MultiColorDevice`.
> +        let classdev = unsafe { MultiColorDevice::<T>::from_raw(led_cdev) };
> +
> +        // SAFETY: `classdev.classdev.get()` is guaranteed to be a pointer to a valid
> +        // `led_classdev_mc`.
> +        unsafe { bindings::led_mc_calc_color_components(classdev.classdev.get(), brightness) };
> +
> +        let _ = classdev.brightness_set(brightness);
> +    }
> +
> +    /// # Safety
> +    /// `led_cdev` must be a valid pointer to a `led_classdev` embedded within a
> +    /// `led::MultiColorDevice`.
> +    /// This function is called on setting the brightness of a led immediately.
> +    unsafe extern "C" fn brightness_set_blocking_callback(
> +        led_cdev: *mut bindings::led_classdev,
> +        brightness: u32,
> +    ) -> i32 {
> +        from_result(|| {
> +            // SAFETY: The function's contract guarantees that `led_cdev` is a valid pointer to a
> +            // `led_classdev` embedded within a `led::MultiColorDevice`.
> +            let classdev = unsafe { MultiColorDevice::<T>::from_raw(led_cdev) };
> +
> +            // SAFETY: `classdev.classdev.get()` is guaranteed to be a pointer to a valid
> +            // `led_classdev_mc`.
> +            unsafe { bindings::led_mc_calc_color_components(classdev.classdev.get(), brightness) };
> +
> +            classdev.brightness_set(brightness)?;
> +            Ok(0)
> +        })
> +    }

[Severity: Medium]
Can calling led_mc_calc_color_components() here race with concurrent
brightness updates?

led_mc_calc_color_components() updates subled_info[i].brightness in-place
with plain non-atomic stores and no lock.  The LED core does not serialize
brightness callbacks under one lock: for non-blocking LEDs,
led_timer_function() in softirq context can run concurrently with sysfs
brightness_store(), and for blocking LEDs, led_set_brightness_sync() can
race with set_brightness_delayed() on leds_wq:

CPU0                                CPU1
brightness_store()                  <softirq>
  brightness_set_callback(255)      led_timer_function()
    led_mc_calc_color_components()    brightness_set_callback(0)
    classdev.brightness_set(255)        led_mc_calc_color_components()
      subleds()[0].brightness()           subled_info[i].brightness = 0
      subleds()[1].brightness()  <-- data race / torn channel values

Because Adapter calls led_mc_calc_color_components() before entering
LedOps::brightness_set(), a driver cannot hold its own lock across the
calculation and hardware update, resulting in a data race and torn
per-channel brightness values programmed into the LED hardware.

Exposing a method on MultiColorDevice so drivers call
led_mc_calc_color_components() under their own lock would avoid the race,
let devices with hardware global brightness skip it, and allow blink_set()
to compute channel brightness when needed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-rust_leds-v29-0-dc8a8ccb96d7@posteo.de?part=3

  reply	other threads:[~2026-12-03 22:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 21:38 [PATCH v29 0/4] rust: leds: Add led classdev abstractions Markus Probst
2026-12-03 21:38 ` [PATCH v29 1/4] rust: leds: Add basic " Markus Probst
2026-12-03 22:02   ` sashiko-bot
2026-12-03 21:38 ` [PATCH v29 2/4] rust: leds: Add Mode trait Markus Probst
2026-12-03 21:38 ` [PATCH v29 3/4] rust: leds: Add multicolor classdev abstractions Markus Probst
2026-12-03 22:00   ` sashiko-bot [this message]
2026-12-03 21:38 ` [PATCH v29 4/4] MAINTAINERS: rust: leds: Add rust abstraction entry Markus Probst
2026-12-03 22:02 ` [PATCH v29 0/4] rust: leds: Add led classdev abstractions 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-248718@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gary@garyguo.net \
    --cc=lee@kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --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.