linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KEYS: Invert FINAL_PUT bit
@ 2025-05-28 12:18 David Howells
  2025-06-11  0:22 ` Paul Moore
  0 siblings, 1 reply; 13+ messages in thread
From: David Howells @ 2025-05-28 12:18 UTC (permalink / raw)
  To: torvalds
  Cc: dhowells, Herbert Xu, Jarkko Sakkinen, keyrings,
	linux-security-module, linux-crypto, linux-integrity,
	linux-kernel

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 <herbert@gondor.apana.org.au>

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.

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>
cc: keyrings@vger.kernel.org
cc: linux-security-module@vger.kernel.org
cc: linux-crypto@vger.kernel.org
cc: linux-integrity@vger.kernel.org
---
 include/linux/key.h |    2 +-
 security/keys/gc.c  |    4 ++--
 security/keys/key.c |    5 +++--
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/include/linux/key.h b/include/linux/key.h
index ba05de8579ec..81b8f05c6898 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -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
diff --git a/security/keys/gc.c b/security/keys/gc.c
index f27223ea4578..748e83818a76 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -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;
 		}
 
diff --git a/security/keys/key.c b/security/keys/key.c
index 7198cd2ac3a3..3bbdde778631 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -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);
 		}
 	}


^ permalink raw reply related	[flat|nested] 13+ messages in thread
* Re: [PATCH] KEYS: Reduce smp_mb() calls in key_put()
  2025-04-30 15:25 ` Jarkko Sakkinen
@ 2025-05-03 14:39 Jarkko Sakkinen
  2025-04-30 15:25 ` Jarkko Sakkinen
  0 siblings, 1 reply; 13+ messages in thread
From: Jarkko Sakkinen @ 2025-05-03 14:39 UTC (permalink / raw)
  To: keyrings
  Cc: David Howells, Lukas Wunner, Ignat Korchagin, Herbert Xu,
	David S. Miller, Peter Huewe, Jason Gunthorpe, Paul Moore,
	James Morris, Serge E. Hallyn, James Bottomley, Mimi Zohar,
	linux-crypto, linux-kernel, linux-integrity,
	linux-security-module

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

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-06-12 10:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-28 12:18 [PATCH] KEYS: Invert FINAL_PUT bit David Howells
2025-06-11  0:22 ` Paul Moore
2025-06-11  9:12   ` Herbert Xu
2025-06-11 15:54     ` Paul Moore
2025-06-11 16:47   ` Jarkko Sakkinen
2025-06-11 17:50   ` Linus Torvalds
2025-06-11 18:45     ` David Howells
2025-06-11 18:59       ` Linus Torvalds
2025-06-11 20:38         ` Al Viro
2025-06-11 21:21           ` Linus Torvalds
2025-06-12 10:32     ` Jarkko Sakkinen
  -- strict thread matches above, loose matches on Subject: below --
2025-05-03 14:39 [PATCH] KEYS: Reduce smp_mb() calls in key_put() Jarkko Sakkinen
2025-04-30 15:25 ` Jarkko Sakkinen
2025-05-03 22:19   ` David Howells
2025-05-04  0:35     ` Herbert Xu
2025-05-04  5:36       ` [PATCH] KEYS: Invert FINAL_PUT bit Herbert Xu
2025-05-04  7:44         ` David Howells

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).