Skip to contentSkip to conversation

KEYS: Invert FINAL_PUT bit

PATCH In review David Howells 6 participants 11 messages May 28, 2025 – Jun 12, 2025

Subsystems (4)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 KEYS: Invert FINAL_PUT bit

Hi Linus,

Could you apply this, please? There shouldn't be any functional change, rather it's a switch to using combined bit-barrier ops and lesser barriers. A better way to do this might be to provide set_bit_release(), but the end result would be much the same.

Thanks,
David
---
From: Herbert Xu <[email protected]>

KEYS: Invert FINAL_PUT bit

Invert the FINAL_PUT bit so that test_bit_acquire and clear_bit_unlock can be used instead of smp_mb.

---
 include/linux/key.h |    2 +-
 security/keys/gc.c  |    4 ++--
 security/keys/key.c |    5 +++--
 3 files changed, 6 insertions(+), 5 deletions(-)
include/linux/key.h +1 -1
index ba05de8579ec..81b8f05c6898 100644
@@ -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
security/keys/gc.c +2 -2
index f27223ea4578..748e83818a76 100644
@@ -218,8 +218,8 @@ static void key_garbage_collector(struct work_struct *work)
 		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;
 		}
 
security/keys/key.c +3 -2
index 7198cd2ac3a3..3bbdde778631 100644
@@ -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);
 		}
 	}
maintainer
#1 lore raw
Quoted 25 lines · David Howells

Hi Linus,

Could you apply this, please? There shouldn't be any functional change, rather it's a switch to using combined bit-barrier ops and lesser barriers. A better way to do this might be to provide set_bit_release(), but the end result would be much the same.

Thanks,
David
---
From: Herbert Xu <[email protected]>

KEYS: Invert FINAL_PUT bit

Invert the FINAL_PUT bit so that test_bit_acquire and clear_bit_unlock can be used instead of smp_mb.

Quoted 5 lines
---
 include/linux/key.h |    2 +-
 security/keys/gc.c  |    4 ++--
 security/keys/key.c |    5 +++--
 3 files changed, 6 insertions(+), 5 deletions(-)

It doesn't look like this has made its way to Linus. David or Jarkko, do one of you want to pick this up into a tree and send this to Linus properly?

Quoted 53 lines
include/linux/key.h +1 -1
index ba05de8579ec..81b8f05c6898 100644
@@ -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
security/keys/gc.c +2 -2
index f27223ea4578..748e83818a76 100644
@@ -218,8 +218,8 @@ static void key_garbage_collector(struct work_struct *work)
                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;
                }
 
security/keys/key.c +3 -2
index 7198cd2ac3a3..3bbdde778631 100644
@@ -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);
                }
        }
Signature · 1 line
paul-moore.com
maintainer
#2 lore raw
Quoted 4 lines · Paul Moore

It doesn't look like this has made its way to Linus. David or Jarkko, do one of you want to pick this up into a tree and send this to Linus properly?

I can pick it up for the next merge window.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#3 lore raw
Quoted 7 lines · Herbert Xu
Quoted 4 lines · Paul Moore

It doesn't look like this has made its way to Linus. David or Jarkko, do one of you want to pick this up into a tree and send this to Linus properly?

I can pick it up for the next merge window.

Great, thanks Herbert.

Signature · 1 line
paul-moore.com
maintainer
#4 lore raw
In reply to Paul Moore
Quoted 32 lines · Paul Moore
Quoted 25 lines · David Howells

Hi Linus,

Could you apply this, please? There shouldn't be any functional change, rather it's a switch to using combined bit-barrier ops and lesser barriers. A better way to do this might be to provide set_bit_release(), but the end result would be much the same.

Thanks,
David
---
From: Herbert Xu <[email protected]>

KEYS: Invert FINAL_PUT bit

Invert the FINAL_PUT bit so that test_bit_acquire and clear_bit_unlock can be used instead of smp_mb.

Quoted 5 lines
---
 include/linux/key.h |    2 +-
 security/keys/gc.c  |    4 ++--
 security/keys/key.c |    5 +++--
 3 files changed, 6 insertions(+), 5 deletions(-)
Quoted 3 lines

It doesn't look like this has made its way to Linus. David or Jarkko, do one of you want to pick this up into a tree and send this to Linus properly?

I'm open for anything but need comment from David at first. It is up to him as he carries the torch ATM for this one :-)

BR, Jarkko

#5 lore raw
In reply to Paul Moore
Quoted 2 lines · Paul Moore

It doesn't look like this has made its way to Linus.

Bah. It "made it" in the sense that sure, it's in my inbox.

But particularly during the the early merge window I end up heavily limiting my emails to pull requests. And then it ended up composted at the bottom of my endless pile of emails.

I guess I can still take it if people just say "do it".

Linus

author maintainer
#6 lore raw
Quoted 1 line · Linus Torvalds

I guess I can still take it if people just say "do it".

Do you want a signed tag and git pull for it?

David

#7 lore raw
Quoted 2 lines · David Howells

Do you want a signed tag and git pull for it?

Particularly during the merge window that makes sense just to make it trigger my usual "git pull" pattern, but now that I'm more aware of it I can just take the patch directly.

Anyway - done just to get this behind us. But for next time, just do it as a signed tag pull request, _particularly_ during the merge window when most other emails get much lower priority.

Linus

#8 lore raw
Quoted 11 lines · Linus Torvalds
Quoted 2 lines · David Howells

Do you want a signed tag and git pull for it?

Particularly during the merge window that makes sense just to make it trigger my usual "git pull" pattern, but now that I'm more aware of it I can just take the patch directly.

Anyway - done just to get this behind us. But for next time, just do it as a signed tag pull request, _particularly_ during the merge window when most other emails get much lower priority.

Speaking of the stuff fallen through the cracks - could you take another look at https://lore.kernel.org/all/20250602041118.GA2675383@ZenIV/?

maintainer
#10 lore raw
In reply to Linus Torvalds
Quoted 6 lines · Linus Torvalds
Quoted 2 lines · Paul Moore

It doesn't look like this has made its way to Linus.

Bah. It "made it" in the sense that sure, it's in my inbox.

Quoted 7 lines

But particularly during the the early merge window I end up heavily limiting my emails to pull requests. And then it ended up composted at the bottom of my endless pile of emails.

I guess I can still take it if people just say "do it".

Linus

+1 for picking it.

BR, Jarkko