Linux Documentation
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Jarkko Sakkinen <jarkko@kernel.org>
Cc: David Howells <dhowells@redhat.com>,
	bpf@vger.kernel.org, dm-devel@lists.linux.dev,
	fsverity@lists.linux.dev, keyrings@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	Alasdair Kergon <agk@redhat.com>,
	Alexei Starovoitov <ast@kernel.org>, Alex Shi <alexs@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Benjamin Marzinski <bmarzins@redhat.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Dongliang Mu <dzm91@hust.edu.cn>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	James Morris <jmorris@namei.org>, Jiri Olsa <jolsa@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Mike Snitzer <snitzer@kernel.org>,
	Mikulas Patocka <mpatocka@redhat.com>,
	Paul Moore <paul@paul-moore.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Song Liu <song@kernel.org>, "Theodore Y. Ts'o" <tytso@mit.edu>,
	Yanteng Si <si.yanteng@linux.dev>,
	Yonghong Song <yonghong.song@linux.dev>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH v5 2/3] KEYS: Replace assoc_array with rhashtable
Date: Fri, 6 Nov 2026 19:25:51 -0800	[thread overview]
Message-ID: <20261107032551.GA1990@sol> (raw)
In-Reply-To: <20261105031401.956609-3-jarkko@kernel.org>

On Thu, Nov 05, 2026 at 05:14:00AM +0200, Jarkko Sakkinen wrote:
> The keyrings subsystem is the sole user of assoc_array. It has over 1700
> lines of code, no test suite, and a long history of bugs, with LLMs now
> finding more.
> 
> This unfortunate outcome has evolved over time by large from the shortage
> of in-kernel callers, time bandwidth and 3rd party contributors.
> 
> What the keyrings needs is just a generic key-value store, which performs
> well with sparse key sets.
> 
> Replace struct assoc_array in struct key with struct keyring_index:
> 
> - keyring_index: holds an rhashtable for direct lookups, an RCU-protected
>   list of keyring_link entries for stable walks, the active link count, and
>   an rcu_work item for deferred teardown.
> - keyring_link: links a key into a keyring's hash table and list, caching
>   its 32-bit SipHash digest rehashed with jhash by rhashtable.
> 
> Correspondingly, struct assoc_array_edit is replaced by struct keyring_edit
> for delivering transient state across link operations.
> 
> Other behavioral changes:
> 
> - KEYCTL_READ and sibling-keyring searches follow list order instead of
>   hash order: keyrings are prepended, other keys are appended, and
>   replacing a matching key retains its position.
> - key_unlink(), keyring_clear(), keyring_revoke(), and keyring_gc() no
>   longer allocate memory.
> - keyring_index_free() uses queue_rcu_work() instead of call_rcu() because
>   rhashtable_destroy() may sleep.
> 
> Suggested-by: Eric Biggers <ebiggers@kernel.org>
> Co-developed-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

Reviewed-by: Eric Biggers <ebiggers@kernel.org>

But one comment below.

> +		do {
> +			ret = rhashtable_insert_fast(&links->ht, &link->node,
> +						     keyring_link_params);
> +			if (ret == -EBUSY)
> +				fsleep(100);
> +		} while (ret == -EBUSY);
> +		if (ret < 0) {
> +			key_payload_reserve(keyring,
> +					    keyring->datalen - KEYQUOTA_LINK_BYTES);
> +			goto error;
> +		}

I don't think the retry loop is actually sufficient or desirable here
(though it would be very rare for it to be reached).  It gets reached
when there are too many elements in one rhashtable chain.  However,
we're already pre-hashing with SipHash, and the rhashtable level hash is
passed only the 32-bit SipHash.  Suppose we reach this case because the
SipHash values collided: then rhashtable's rehashing would never reduce
the chain length, and the -EBUSY would last forever.

I think the right solution is to just set '.insecure_elasticity = true'
in the rhashtable_params, to disable the chain length checks.  Those
checks make no sense when we're using SipHash anyway.

Then the retry loop would not be needed.

(And even for rhashtable users who aren't using SipHash, like
fs/verity/, hard-failing insertions if the hash chains gets too long
seems wrong.  I mean, imagine if the kernel started refusing to do file
operations if one of the hash chains in the VFS hash tables happened to
get too long...  But I guess rhashtable was designed for towards
networking users who are able to just drop the data without much issue.
Plus it predates SipHash existing.)

- Eric

  parent reply	other threads:[~2026-11-07  3:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05  3:13 [PATCH v5 0/3] KEYS: Replace assoc_array with rhashtable Jarkko Sakkinen
2026-11-05  3:13 ` [PATCH v5 1/3] KEYS: Introduce keyring_is_empty() Jarkko Sakkinen
2026-11-05  3:14 ` [PATCH v5 2/3] KEYS: Replace assoc_array with rhashtable Jarkko Sakkinen
2026-11-06  8:21   ` Hannes Reinecke
2026-11-06  9:05     ` Eric Biggers
2026-11-06 13:02       ` Hannes Reinecke
2026-11-06 19:05         ` Jarkko Sakkinen
2026-11-06 19:19           ` Eric Biggers
2026-11-06 19:30             ` Jarkko Sakkinen
2026-11-06 19:41               ` Jarkko Sakkinen
2026-11-06 15:08     ` Jarkko Sakkinen
2026-11-07  3:25   ` Eric Biggers [this message]
2026-11-07 12:44     ` Jarkko Sakkinen
2026-11-05  3:14 ` [PATCH v5 3/3] lib: Remove assoc_array 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=20261107032551.GA1990@sol \
    --to=ebiggers@kernel.org \
    --cc=agk@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexs@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bmarzins@redhat.com \
    --cc=bpf@vger.kernel.org \
    --cc=corbet@lwn.net \
    --cc=daniel@iogearbox.net \
    --cc=dhowells@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=dzm91@hust.edu.cn \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=fsverity@lists.linux.dev \
    --cc=ihor.solodrai@linux.dev \
    --cc=jarkko@kernel.org \
    --cc=jmorris@namei.org \
    --cc=jolsa@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mpatocka@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=rdunlap@infradead.org \
    --cc=serge@hallyn.com \
    --cc=si.yanteng@linux.dev \
    --cc=skhan@linuxfoundation.org \
    --cc=snitzer@kernel.org \
    --cc=song@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=tytso@mit.edu \
    --cc=yonghong.song@linux.dev \
    /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