[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <aIFM-qEnD4pzdtMs@tardis-2.local>
Date: Wed, 23 Jul 2025 13:58:34 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Benno Lossin <lossin@...nel.org>
Cc: Alice Ryhl <aliceryhl@...gle.com>, Miguel Ojeda <ojeda@...nel.org>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/2] rust: sync: refactor static_lock_class!() macro
On Wed, Jul 23, 2025 at 10:41:18PM +0200, Benno Lossin wrote:
[...]
> >> >> > is in a static segment it uses different behavior?
> >> >> >
> >> >> > Because from the safety requirements on this function, I could just do
> >> >> > this:
> >> >> >
> >> >> > // SAFETY: we leak the box below, so the destructor never runs.
> >> >> > let class = KBox::new(unsafe { LockClassKey::new_static() });
> >> >> > let class = Pin::static_ref(KBox::leak(class));
> >> >> > let lock = SpinLock::new(42, c_str!("test"), class);
> >> >
> >> > This will trigger a runtime error because `class` is not static, but
> >> > technically, it won't trigger UB, at least lockdep should be able to
> >> > handle this case.
> >>
> >> Could you go into more details? What is the "technically it won't
> >> trigger UB" part about?
> >>
> >
> > If a dynamic key is not registered, lockdep will simply just skip the
> > initialization of locks, report an error and disable itself entirely. So
> > it won't cause UB.
>
> So the code above would trigger lockdep to disable itself?
>
Yes.
> And how does it detect that the class isn't registered? By checking for
> the address in the hash list?
Right, in is_dynamice_key(), the hash list is scanned to see the key has
been registered.
> Otherwise it would be UB, right? Could there be a hash collision that
> induces UB?
>
I don't think a hash collision will cause an UB, because the checking
only uses the address of the key, so even the key is uninitialized, it's
fine.
Regards,
Boqun
> ---
> Cheers,
> Benno
Powered by blists - more mailing lists