Thread (16 messages) 16 messages, 4 authors, 2025-05-12

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

From: Herbert Xu <herbert@gondor.apana.org.au>
Date: 2025-05-03 15:03:51
Also in: keyrings, linux-crypto, linux-integrity, lkml

On Sat, May 03, 2025 at 05:39:16PM +0300, Jarkko Sakkinen wrote:
On Wed, Apr 30, 2025 at 06:25:53PM +0300, Jarkko Sakkinen wrote:
quoted
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. */
You can use smp_mb__before_atomic here as it is equivalent to
smp_mb in this situation.
 			}
			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.
I don't think spin_unlock alone is enough to replace an smp_mb.
A spin_lock + spin_unlock would be enough though.

However, looking at the bigger picture this smp_mb looks bogus.
What exactly is it protecting against?

The race condition that this is supposed to fix should have been
dealt with by the set_bit/test_bit of FINAL_PUT alone.  I don't
see any point in having this smb_mb at all.

Cheers,
-- 
Email: Herbert Xu [off-list ref]
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help