From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796058773; b=xUY/wL9ephsPI06oWcaTeS/922rxO0VjrSY940KqQC8JMR8i0+DbwYbuWQ0W2xaJ+WqY br5vMP6x1e//sDkjXnz+/VdT8X0FDMQJ77A1p80v5/UAwAD7/hVoIB+egfONA09mS9frt EMMB5cPE7jUPplKx69qv7inPVhqtj0rIyiJL2L8TpPTgFINqa87lalzHkhUjKCOL2kIcX FpjQMQw81zswUTbb8ekWssnjOY0VBhaSxW7AegQ2A5vzvvDlpyh2AphBJlQX7hi4VwyK/ MXQ7M+NL8cpVGT3wDfS+QiOCENiOaBhyheh4WRrN+xWeTggzY585KMa2QiBEawP2AjQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796058773; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=p3RSE+OftbY4ybMNbeahgW31S1XsIj1J4PqKfP6LNJo=; b=I/C/xsD+CXzt7k3j7sX2kADpzwsDPTAn8i9S4ACZOSQOATAEkHJ+4mcjsFbv/DLPJYbj TNoensxSiMXm01PtokSwhy335Is9GgD3zAin4zCDa8yvBsq9GX3e3wMJfUkXiCR8D2IJs 98HRqstFZON8evfiXaFsZrl85zUT+Ez3zeP3RlwpWZx9TKI/gbAwyS9vrtHQu59c89Eon MUQg48zD+QMu+q7AxAPjNu6J62PZhrl0XRvG+fyFhDuWlnGHa+Vru+K7B2AFUj0q78y+9 IQUVianVXIw6AWF+umj9du2SKOtFH2dbd1UrDXwapoSly0RCUqqoYrgFJ2V7/lVXglA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EvsEFc5k; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 41C70606A320; Mon, 30 Nov 2026 17:12:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=EvsEFc5k Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 479471F0089B; Mon, 30 Nov 2026 17:12:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796058772; bh=p3RSE+OftbY4ybMNbeahgW31S1XsIj1J4PqKfP6LNJo=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=EvsEFc5k4GY0qihurUo9fbIneUrlQFXYNq/ql2UaWHDRYkYZxJHiyEN0YDR0eBCO0 ccZsMAZ+FLflKiN8QnCyUzmi97TjAJzBKzPd0h4qTHzIovnt48vwx/Fc1cyVg/Z3a8 +gYdpVggW+zpdZtT6cwj/7+CfCnpMp1msI8qQuld9n/3Fhc/fd6QsvWhHxF+3l601C T938APb2ViCVmycAlfDExlhqMDy6qgx1XYg4KCVwec8DGNPhg/jG6eR76jwJs6TatA mmGyS5hCyJDeOXMZfi5vAiyDrwsG8zHjdINjsC82FHVJgTpM2gkGPiNBNwqLRfJu1T DXzAzVML7pOxQ== Date: Mon, 30 Nov 2026 19:12:48 +0200 From: Jarkko Sakkinen To: Guicai Ma Cc: David Howells , 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 Message-ID: References: <20261129170543.2915157-1-jarkko@kernel.org> <20261129170543.2915157-15-jarkko@kernel.org> <20261130090639.219371-1-maguicai@kylinos.cn> X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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