KEYS: Invert FINAL_PUT bit
authorHerbert Xu <herbert@gondor.apana.org.au>
Wed, 28 May 2025 12:18:17 +0000 (13:18 +0100)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 11 Jun 2025 18:57:14 +0000 (11:57 -0700)
Invert the FINAL_PUT bit so that test_bit_acquire and clear_bit_unlock
can be used instead of smp_mb.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/key.h
security/keys/gc.c
security/keys/key.c

index ba05de8579ecc5ca31a428ef963bd4256403f5ea..81b8f05c68985c6f0a5c5bbe0f1fd6661912c618 100644 (file)
@@ -236,7 +236,7 @@ struct key {
 #define KEY_FLAG_ROOT_CAN_INVAL        7       /* set if key can be invalidated by root without permission */
 #define KEY_FLAG_KEEP          8       /* set if key should not be removed */
 #define KEY_FLAG_UID_KEYRING   9       /* set if key is a user or user session keyring */
-#define KEY_FLAG_FINAL_PUT     10      /* set if final put has happened on key */
+#define KEY_FLAG_USER_ALIVE    10      /* set if final put has not happened on key yet */
 
        /* the key type and key description string
         * - the desc is used to match a key against search criteria
index f27223ea4578f1dcee16a68d51357e337e522a0a..748e83818a760425e5324751819ba8e58ab4aca1 100644 (file)
@@ -218,8 +218,8 @@ continue_scanning:
                key = rb_entry(cursor, struct key, serial_node);
                cursor = rb_next(cursor);
 
-               if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) {
-                       smp_mb(); /* Clobber key->user after FINAL_PUT seen. */
+               if (!test_bit_acquire(KEY_FLAG_USER_ALIVE, &key->flags)) {
+                       /* Clobber key->user after final put seen. */
                        goto found_unreferenced_key;
                }
 
index 7198cd2ac3a3a57dd17a34f4d174e54cf2b74558..3bbdde7786318b392af93f570a0ae7322602db88 100644 (file)
@@ -298,6 +298,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
        key->restrict_link = restrict_link;
        key->last_used_at = ktime_get_real_seconds();
 
+       key->flags |= 1 << KEY_FLAG_USER_ALIVE;
        if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
                key->flags |= 1 << KEY_FLAG_IN_QUOTA;
        if (flags & KEY_ALLOC_BUILT_IN)
@@ -658,8 +659,8 @@ void key_put(struct key *key)
                                key->user->qnbytes -= key->quotalen;
                                spin_unlock_irqrestore(&key->user->lock, flags);
                        }
-                       smp_mb(); /* key->user before FINAL_PUT set. */
-                       set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
+                       /* Mark key as safe for GC after key->user done. */
+                       clear_bit_unlock(KEY_FLAG_USER_ALIVE, &key->flags);
                        schedule_work(&key_gc_work);
                }
        }