Re: [PATCH v2 01/34] compiler_types: Move lock checking attributes to compiler-capability-analysis.h

Marco Elver
2025-03-05T09:13:55Z

On Wed, Mar 05, 2025 at 11:36AM +0300, Dan Carpenter wrote:
> On Tue, Mar 04, 2025 at 10:21:00AM +0100, Marco Elver wrote:
> > +#ifndef _LINUX_COMPILER_CAPABILITY_ANALYSIS_H
> > +#define _LINUX_COMPILER_CAPABILITY_ANALYSIS_H
> > +
> > +#ifdef __CHECKER__
> > +
> > +/* Sparse context/lock checking support. */
> > +# define __must_hold(x) __attribute__((context(x,1,1)))
> > +# define __acquires(x) __attribute__((context(x,0,1)))
> > +# define __cond_acquires(x) __attribute__((context(x,0,-1)))
> > +# define __releases(x) __attribute__((context(x,1,0)))
> > +# define __acquire(x) __context__(x,1)
> > +# define __release(x) __context__(x,-1)
> > +# define __cond_lock(x, c) ((c) ? ({ __acquire(x); 1; }) : 0)
> > +
>
> The other thing you might want to annotate is ww_mutex_destroy().

We can add an annotation to check the lock is not held:

diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h
index 63978cb36a98..549d75aee76a 100644
--- a/include/linux/ww_mutex.h
+++ b/include/linux/ww_mutex.h
@@ -372,6 +372,7 @@ extern int __must_check ww_mutex_trylock(struct ww_mutex *lock,
* this function is called.
*/
static inline void ww_mutex_destroy(struct ww_mutex *lock)
+ __must_not_hold(lock)
{
#ifndef CONFIG_PREEMPT_RT
mutex_destroy(&lock->base);
diff --git a/lib/test_capability-analysis.c b/lib/test_capability-analysis.c
index 13e7732c38a2..1a466b362373 100644
--- a/lib/test_capability-analysis.c
+++ b/lib/test_capability-analysis.c
@@ -516,6 +516,8 @@ static void __used test_ww_mutex_lock_noctx(struct test_ww_mutex_data *d)
ww_mutex_lock_slow(&d->mtx, NULL);
d->counter++;
ww_mutex_unlock(&d->mtx);
+
+ ww_mutex_destroy(&d->mtx);
}

static void __used test_ww_mutex_lock_ctx(struct test_ww_mutex_data *d)
@@ -545,4 +547,6 @@ static void __used test_ww_mutex_lock_ctx(struct test_ww_mutex_data *d)

ww_acquire_done(&ctx);
ww_acquire_fini(&ctx);
+
+ ww_mutex_destroy(&d->mtx);
}

Probably a fixup for the ww_mutex patch:
https://lore.kernel.org/all/20250304092417.2873893-21-elver@google.com/
Or extra patch depending on when/if Peter decides to take the series.

> I'm happy about the new __guarded_by annotation.

Thanks!

-- Marco