[PATCH RFC 0/2] rust: sync: create lock class using `#[track_caller]`

From: Gary Guo

Date: Fri Jul 03 2026 - 10:17:03 EST


Currently we're adding more and more macros so that lock classes can be
created. The only purpose served by these macros are to create names and
static lock classes so they can be tracked by lockdep.

I've come up with an approach that uses `Location::caller` and Rust's
`#[track_caller]` feature for this purpose instead. `Location::caller()`
will return a `&'static Location<'static>` that lives in `.rodata`. As
only addresses matter for static objects, the address of the `Location`
themselves could be used for lock classes. The `Location` is of 2 * usize +
8 bytes, so they could host 2 lock class keys, so `DelayedWork` which needs
two lock classes can also use this mechanism.

The `Location::caller` could also be used to provide the name; currently,
`optional_name` just uses `file_name:line_number` as the name, and this
information is available in `Location`. However, `Location` encodes them
using file name as a C string plus two `u32`'s for line and column number,
so it cannot provide a single name. I came up with an alternative method,
which is to use a special string, which lockdep can recognize and delegate
back to Rust to print it.

Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
---
Gary Guo (2):
rust: sync: introduce a way to create lock class from caller
lockdep: delegate Rust lock class printing to Rust code

kernel/locking/lockdep.c | 7 ++++-
kernel/locking/lockdep_internals.h | 3 ++
rust/kernel/sync.rs | 61 +++++++++++++++++++++++++++++++++++---
rust/kernel/sync/lock.rs | 21 +++++++++++--
rust/kernel/sync/lock/mutex.rs | 11 ++++---
rust/kernel/sync/lock/spinlock.rs | 11 ++++---
6 files changed, 95 insertions(+), 19 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260702-rust_lockdep-6220af6a6ba9

Best regards,
--
Gary Guo <gary@xxxxxxxxxxx>