Re: [PATCH v2 02/14] cleanup: Disable context analysis for __free() helpers

From: Marco Elver

Date: Wed Nov 11 2026 - 04:32:49 EST


On Mon, 9 Nov 2026 at 20:14, Bart Van Assche <bvanassche@xxxxxxx> wrote:
>
> The __free_##_name() helper functions defined by DEFINE_FREE() call the
> free expression that has been passed to DEFINE_FREE(). If that
> expression releases a lock, e.g. because it is a call to an unlock
> function, the Clang thread-safety analyzer complains because it cannot
> know that the variable that is passed to the helper function holds a
> lock. Hence, disable context analysis for the __free() helper
> functions.
>
> Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>

An example where this fails?

I think this is ok; but knowing the cases where this is needed will
tell me if we need to make this work properly or just disabling
analysis here is ok.

> ---
> include/linux/cleanup.h | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
> index 1fb8058b897d..f6afe80781a4 100644
> --- a/include/linux/cleanup.h
> +++ b/include/linux/cleanup.h
> @@ -207,8 +207,10 @@
> * Without the NULL test it turns into a mess and the compiler can't help us.
> */
>
> -#define DEFINE_FREE(_name, _type, _free) \
> - static __always_inline void __free_##_name(void *p) { _type _T = *(_type *)p; _free; }
> +#define DEFINE_FREE(_name, _type, _free) \
> + static __always_inline void __free_##_name(void *p) \
> + __context_unsafe(/* _free may release a lock */) \
> + { _type _T = *(_type *)p; _free; }
>
> #define __free(_name) __cleanup(__free_##_name)
>