Rust for Linux List
 help / color / mirror / Atom feed
From: Boqun Feng <boqun@kernel.org>
To: Markus Probst <markus.probst@posteo.de>
Cc: "Lee Jones" <lee@kernel.org>, "Pavel Machek" <pavel@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Dave Ertman" <david.m.ertman@intel.com>,
	"Leon Romanovsky" <leon@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczy´nski" <kwilczynski@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Ira Weiny" <iweiny@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-leds@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v26 3/4] rust: leds: Add multicolor classdev abstractions
Date: Fri, 9 Oct 2026 13:08:52 -0700	[thread overview]
Message-ID: <aslJ1ISONL9ojP-3@tardis.local> (raw)
In-Reply-To: <7174e1f95dcbeb8802d9e9c9715afa32784f0c20.camel@posteo.de>

On Fri, Oct 09, 2026 at 07:59:02PM +0000, Markus Probst wrote:
[..]
> > > > > +/// The multicolor sub led info representation.
> > > > > +///
> > > > > +/// This structure represents the Rust abstraction for a C `struct mc_subled`.
> > > > > +#[repr(C)]
> > > > > +#[derive(Debug)]
> > > > > +#[non_exhaustive]
> > > > > +pub struct MultiColorSubLed {
> > > > > +    /// The color of the sub led
> > > > > +    pub color: Color,
> > > > > +    brightness: UnsafeCell<u32>,
> > > > > +    intensity: UnsafeCell<u32>,
> > > > 
> > > > These should be `Atomic<u32>`, or am I missing something here? Using
> > > > `Atomic<u32>` should resolve sashiko's comment on this patch.
> > > Snippet of the `Atomic::from_ptr` rustdoc:
> > > 
> > > "
> > > For the duration of 'a, other accesses to *ptr must not cause data
> > > races (defined by LKMM) against atomic operations on the returned
> > > reference. Note that if all other accesses are atomic, then this safety
> > > requirement is trivially fulfilled.
> > > "
> > > 
> > > This safety requirement is likely not met if I see this correctly,
> > > because the led subsystem does not use atomic accesses.
> > > 
> > 
> > Then the C side has a data race that needs some fix (or they use
> > READ_ONCE() or WRITE_ONCE() which are *atomic* to avoid the data race).
> They don't use READ_ONCE or WRITE_ONCE.
> 
> Writes to "intensity" can happen at anytime by `multi_intensity_store`.
> It does lock the `led_access` mutex on write. It is not locked on read
> and `grep "READ_ONCE" -r drivers/leds/` has no matches in drivers
> either.
> 

I wonder whether KCSAN will report an issue of this (w/o
CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC).

> Writes to "brightness" are on the C-side handled by the driver by
> calling `led_mc_calc_color_components`. This rust abstraction always
> calles it in `brightness_set_callback`. So on the C-side, this at least
> is less of an issue, as writes and reads are controlled by the C
> driver.
> 

Thank you for taking a look into this.

> > 
> > The general rule is: if C side has a data race, they should fix it, if C
> > side doesn't care ("the compiler should not data race on this code"),
> > then the Rust side treat it as atomic operations. This is the only way
> > to better code regarding data races.
> It probably should use WRITE_ONCE and READ_ONCE, but it also shouldn't
> create any issues if its not used. There is no load tearing on a 32-bit
> integer and memory ordering is not required. Not sure if its worth the

I think some people would disagree with you on "no load tearing"
(because data race = UB = anything can happen), but..

> trouble changing every existing multicolor led driver.
> 

I agree it's probably not worth doing this at the moment.

> > 
> > > Ofc, this function won't be used, but I think given that the same
> > > struct is also accessed by the C-side, it should also apply here.
> > > 
> > > Like Sashiko suggests, "core::ptr::read_volatile()" might be a better
> > > option to prevent certain compiler optimizations.
> > > 
> > 
> > No, please don't over-use read_volatile(). The reason that READ_ONCE()
> > and WRITE_ONCE() are safe to use for synchronization is because
> > semantics-wise they are atomic on certain types (if aligned), and the
> > them being volatile is just an implementation detail.
> Ok.
> 
> I will need to make .get_mut() const for this.
> 

Sounds good to me.

Regards,
Boqun

> > 
> > Regards,
> > Boqun
> 
> Thanks
> - Markus Probst
> 
> > 
> > > Thanks
> > > - Markus Probst
> > > 
> > > > 
> > > > Regards,
> > > > Boqun
> > > > 
> > > > > +    /// The maximum supported intensity value.
> > > > > +    ///
> > > > > +    /// If None the maximum intensity equals to [`LedOps::MAX_BRIGHTNESS`].
> > > > > +    pub max_intensity: Option<NonZero<u32>>,
> > > > > +    /// Arbitrary data for the driver to store.
> > > > > +    pub channel: u32,
> > > > > +}
> > > > > +
> > > > [...]
> > 



  reply	other threads:[~2026-10-09 20:08 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-30 13:05 [PATCH v26 0/4] rust: leds: Add led classdev abstractions Markus Probst
2026-09-30 13:05 ` [PATCH v26 1/4] rust: leds: Add basic " Markus Probst
2026-09-30 13:05 ` [PATCH v26 2/4] rust: leds: Add Mode trait Markus Probst
2026-09-30 13:05 ` [PATCH v26 3/4] rust: leds: Add multicolor classdev abstractions Markus Probst
2026-10-09 18:52   ` Boqun Feng
2026-10-09 19:18     ` Markus Probst
2026-10-09 19:34       ` Boqun Feng
2026-10-09 19:59         ` Markus Probst
2026-10-09 20:08           ` Boqun Feng [this message]
2026-10-09 22:11             ` leds: KCSAN report Markus Probst
2026-09-30 13:05 ` [PATCH v26 4/4] MAINTAINERS: rust: leds: Add rust abstraction entry Markus Probst
2026-10-08 11:32 ` [PATCH v26 0/4] rust: leds: Add led classdev abstractions Markus Probst

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=aslJ1ISONL9ojP-3@tardis.local \
    --to=boqun@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=david.m.ertman@intel.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=iweiny@kernel.org \
    --cc=kwilczynski@kernel.org \
    --cc=lee@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=pavel@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox