Re: [PATCH] KEYS: Reduce smp_mb() calls in key_put()

Jarkko Sakkinen
2025-05-03T14:39:21Z

On Wed, Apr 30, 2025 at 06:25:53PM +0300, Jarkko Sakkinen wrote:
> Rely only on the memory ordering of spin_unlock() when setting
> KEY_FLAG_FINAL_PUT under key->user->lock in key_put().
>
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> ---
> security/keys/key.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/security/keys/key.c b/security/keys/key.c
> index 7198cd2ac3a3..aecbd624612d 100644
> --- a/security/keys/key.c
> +++ b/security/keys/key.c
> @@ -656,10 +656,12 @@ void key_put(struct key *key)
> spin_lock_irqsave(&key->user->lock, flags);
> key->user->qnkeys--;
> key->user->qnbytes -= key->quotalen;
> + set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
> spin_unlock_irqrestore(&key->user->lock, flags);
> + } else {
> + set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
> + smp_mb(); /* key->user before FINAL_PUT set. */
> }
> - smp_mb(); /* key->user before FINAL_PUT set. */
> - set_bit(KEY_FLAG_FINAL_PUT, &key->flags);

Oops, my bad (order swap), sorry. Should have been:

spin_unlock_irqrestore(&key->user->lock, flags);
} else {
smp_mb(); /* key->user before FINAL_PUT set. */
}
set_bit(KEY_FLAG_FINAL_PUT, &key->flags);

Should spin_lock()/unlock() be good enough or what good does smp_mb() do
in that branch? Just checking if I'm missing something before sending
fixed version.

BR, Jarkko