Linux Security Modules development
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Guicai Ma <maguicai@kylinos.cn>
Cc: David Howells <dhowells@redhat.com>,
	keyrings@vger.kernel.org, linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v7 14/14] KEYS: Remove key_type_dead
Date: Mon, 30 Nov 2026 19:12:48 +0200	[thread overview]
Message-ID: <aw2ukMnwtnAn8_Ag@kernel.org> (raw)
In-Reply-To: <20261130090639.219371-1-maguicai@kylinos.cn>

On Mon, Nov 30, 2026 at 04:06:39AM -0500, Guicai Ma wrote:
> On Sun, Nov 29, 2026 at 07:05:43PM +0200, Jarkko Sakkinen wrote:
> >  void unregister_key_type(struct key_type *ktype)
> >  {
> >  	down_write(&key_types_sem);
> > +	WARN_ON_ONCE(atomic_read(&ktype->nr_keys));
> >  	list_del_init(&ktype->link);
> > -	downgrade_write(&key_types_sem);
> > -	key_gc_keytype(ktype);
> >  	pr_notice("Key type %s unregistered\n", ktype->name);
> > -	up_read(&key_types_sem);
> > +	up_write(&key_types_sem);
> >  }
> 
> I think there is a narrow window where this WARN_ON_ONCE() fires
> spuriously when key_gc_unused_keys() from patch 13/14 races with
> unregister_key_type():
> 
> 		rcu_read_lock();
> 		...
> 		module_put(owner);
> 		atomic_dec_return_release(&type->nr_keys);
> 		atomic_dec_return_release(&type->gc_pending);
> 		rcu_read_unlock();
> 
> try_stop_module() proceeds once module_put(owner) drops the refcount
> to zero, and delete_module() then calls mod->exit() ->
> unregister_key_type() before free_module() gets to its
> synchronize_rcu().  rcu_read_lock() keeps struct key_type mapped, but
> it does not stop rmmod from reaching the WARN_ON_ONCE() check before
> the nr_keys decrement runs.
> 
> Swapping the two would close this window but open another:
> key_gc_flush()'s unlocked fast path keys off nr_keys, so a decrement
> landing before module_put() would let a concurrent flush return early
> while the module refcount is still held, and an rmmod right after it
> would get -EWOULDBLOCK.  Waiting out the RCU read side only in the
> slow case should be enough:
> 
> 	if (atomic_read_acquire(&ktype->nr_keys))
> 		synchronize_rcu();
> 
> before taking key_types_sem and checking
> WARN_ON_ONCE(atomic_read(&ktype->nr_keys)).
> 
> Thanks,
> Guicai

Thank you for pointing out the issue. I'll address them after v7.5-rc1 is out.

Br, Jarkko

  reply	other threads:[~2026-11-30 17:12 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 01/14] KEYS: Move key garbage collection to a dedicated workqueue Jarkko Sakkinen
2026-11-30  8:16   ` Guicai Ma
2026-11-29 17:05 ` [PATCH v7 02/14] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
2026-11-30  8:25   ` Guicai Ma
2026-11-29 17:05 ` [PATCH v7 03/14] KEYS: Add key_gc_flush() for draining RCU callbacks Jarkko Sakkinen
2026-11-30  8:42   ` Guicai Ma
2026-11-29 17:05 ` [PATCH v7 04/14] dns_resolver: Move the key type into a separate module Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 06/14] smb: client: Clear idmap/SPNEGO caches when the last session is unloaded Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 07/14] s390/cert_store: Start tracking subsystem keys Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 08/14] rxrpc: Release conn->key when init_connection_security() fails Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 09/14] rxrpc: Move key types to their own module Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 10/14] nvme-keyring: Move the PSK key type into a separate module Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 11/14] platform/cznic: turris-signing-key: Flush key GC on key release Jarkko Sakkinen
2026-11-30 10:14   ` Marek Behún
2026-11-30 14:59     ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 12/14] KEYS: trusted: Unregister key type on module exit in TEE backend Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 13/14] KEYS: Pin the backing kernel module for a key type Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 14/14] KEYS: Remove key_type_dead Jarkko Sakkinen
2026-11-30  9:06   ` Guicai Ma
2026-11-30 17:12     ` Jarkko Sakkinen [this message]
2026-11-29 17:11 ` [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
2026-11-29 19:28   ` Jarkko Sakkinen
2026-11-29 19:35     ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aw2ukMnwtnAn8_Ag@kernel.org \
    --to=jarkko@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=maguicai@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox