diff mbox series

KEYS: Reduce smp_mb() calls in key_put()

Message ID 20250430152554.23646-1-jarkko@kernel.org
State New
Headers show
Series KEYS: Reduce smp_mb() calls in key_put() | expand

Commit Message

Jarkko Sakkinen April 30, 2025, 3:25 p.m. UTC
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(-)

Comments

Jarkko Sakkinen May 3, 2025, 2:39 p.m. UTC | #1
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
Herbert Xu May 3, 2025, 3:02 p.m. UTC | #2
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:
> > 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,
David Howells May 3, 2025, 10:19 p.m. UTC | #3
Jarkko Sakkinen <jarkko@kernel.org> wrote:

> 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.

spin_unlock() is semi-permeable, so stuff after it can leak into the inside of
it up as far as the spin_lock().  With your change, the garbage collector can
no longer guarantee that key_put() will have done with accessing key->user
when it sees KEY_FLAG_FINAL_PUT is set.

So, NAK on this patch, I think.  If you want a second opinion, I'd suggest
waving it in front of Paul McKenney.

Possibly we only need smp_mb() in the IN_QUOTA branch in key_put().

David
Herbert Xu May 4, 2025, 12:35 a.m. UTC | #4
On Sat, May 03, 2025 at 11:19:21PM +0100, David Howells wrote:
>
> Possibly we only need smp_mb() in the IN_QUOTA branch in key_put().

Just change the smp_mb to smp_mb__before_atomic, at least on x86
it just disappears because set_bit is already a serialising operation.

Or even better, reverse the FINAL_PUT bit and call it ALIVE, so
that you can use test_bit_acquire and clear_bit_unlock.

Cheers,
David Howells May 4, 2025, 7:44 a.m. UTC | #5
Herbert Xu <herbert@gondor.apana.org.au> wrote:

> +	key->flags |= KEY_FLAG_DONT_GC_YET;

You need __set_bit() or 1<<N.

Also, don't really like the name, but that's just bikeshedding.  I think I'd
lean more to your initial suggestion of KEY_FLAG_ALIVE.

David
Jarkko Sakkinen May 4, 2025, 4:42 p.m. UTC | #6
On Sat, May 03, 2025 at 11:19:21PM +0100, David Howells wrote:
> Jarkko Sakkinen <jarkko@kernel.org> wrote:
> 
> > 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.
> 
> spin_unlock() is semi-permeable, so stuff after it can leak into the inside of
> it up as far as the spin_lock().  With your change, the garbage collector can
> no longer guarantee that key_put() will have done with accessing key->user
> when it sees KEY_FLAG_FINAL_PUT is set.
> 
> So, NAK on this patch, I think.  If you want a second opinion, I'd suggest
> waving it in front of Paul McKenney.

Fair enough.

If I revisit this in a way or another,  I'll cc to him for comments but
for this I'll buy what you said.

> 
> Possibly we only need smp_mb() in the IN_QUOTA branch in key_put().
> 
> David

Thank you for the comments.

BR, Jarkko
Jarkko Sakkinen May 4, 2025, 4:55 p.m. UTC | #7
On Sat, May 03, 2025 at 11:02:57PM +0800, Herbert Xu wrote:
> 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:
> > > 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.

smp_mb() there makes sure that key->user change don't spill between
key_put() and gc.

GC pairs smp_mb() in key_put() after FINAL_PUT to make sure that also
in its side key->user changes have been walled before moving the key
as part of unrefenced keys.

See also [1]. It cleared this up for me. Here user->lock easily misleads
to overlook the actual synchronization scheme.

> 
> Cheers,
> -- 
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

[1] https://lore.kernel.org/keyrings/1121543.1746310761@warthog.procyon.org.uk/

BR, Jarkko
diff mbox series

Patch

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);
 			schedule_work(&key_gc_work);
 		}
 	}