* [PATCH v4 0/3] KEYS: Replace assoc_array with rhashtable
@ 2026-10-29 23:24 Jarkko Sakkinen
2026-10-29 23:24 ` [PATCH v4 1/3] KEYS: Introduce keyring_is_empty() Jarkko Sakkinen
0 siblings, 1 reply; 2+ messages in thread
From: Jarkko Sakkinen @ 2026-10-29 23:24 UTC (permalink / raw)
To: David Howells
Cc: keyrings, Eric Biggers, Jarkko Sakkinen, Lukas Wunner,
Ignat Korchagin, Herbert Xu, David S. Miller,
open list:ASYMMETRIC KEYS, open list,
open list:BPF [MISC]:Keyword:(?:b|_)bpf(?:b|_)
The keyring subsystem depends on assoc_array for searching keys by their
description, type and domain. It is the sole user of this data structure,
which has during the age of LLMs transformed into a maintenance nightmare.
assoc_array needs to be replaced with a data structure meeting the
following criteria:
1. Must be suitable for a sparse key-value store.
2. Must have multiple in-kernel users.
3. Would be nice if it had dedicated maintainers.
rhashtable is likely the best pick from the available options as it is
designed for a sparse key space, has dedicated maintainers, and multiple
in-kernel users.
CHANGES
=======
v4:
- Eric Biggers: Correct the kerneldoc summary for keyring_is_empty().
- Eric Biggers: Truncate keyring_index_key hash to u32 and drop index_id.
- Eric Biggers, David Howells: Use a proper struct instead of a two-element
array to pass the edits.
- Eric Biggers: __keyring_link(): Call key_put() in the rollback of
rhashtable_replace_fast().
- Eric Biggers: Retract from using __free(kfree) in keyring_get_index().
- Eric Biggers: Reuse keyring_link_key() in keyring_obj_cmpfn().
- Eric Biggers: Document why keyring_index_free() uses queue_rcu_work().
- Eric Biggers: Fix the commit message for "KEYS: Replace assoc_array with
rhashtable commit message".
v3:
- Since David is away for the next week I decided to address the
low hanging fruit that I reported myself.
- Unified and sanitized function and variable naming and removed
unnecessary asymmetry e.g., by renaming keyring_remove_all_links
as keyring_index_free, 'links' as 'index' and '_edit' as 'edit'.
- Flattened the implementation by refactoring out some useless
wrappers and other constructs.
- Addressed continuation after triggered WARN_ON() on at the three
sites that I've described in my email response to the previous
patch set version.
- Reworked keyring_read() so that it triggers the scheduler between
the iterations.
- Dropped "Assisted-by: LLM" tag because checkpatch.pl told me so.
v2:
- Tried to address all the issues pointed out by David and Eric. Tell me
if something was ignored, as that would be an unintentional mishap.
- Updated and trimmed the source code comments.
- For assoc_array_edit I decided to go with two-element pointer array,
which shifts a bit what David suggested.
- Dropped is_keyring flag. It was so obvious call so I mention it here
just in case because too obvious maxes out my self-doubt ;-)
- Used cleanup.h primitives where I possibly could especially in order
to make fallible initializations more bug-resistant.
- The already-ack'd bug fixes are included but will be dropped as soon
as I've sent them over to Linus.
VERSIONS
========
- v3: https://lore.kernel.org/keyrings/20261025001105.1824178-1-jarkko@kernel.org/
- v2: https://lore.kernel.org/keyrings/20261023120953.1206778-1-jarkko@kernel.org/
- v1: https://lore.kernel.org/keyrings/20261016215326.4110064-1-jarkko@kernel.org/
Jarkko Sakkinen (3):
KEYS: Introduce keyring_is_empty()
KEYS: Replace assoc_array with rhashtable
lib: Remove assoc_array
Documentation/core-api/assoc_array.rst | 564 ------
Documentation/core-api/index.rst | 1 -
.../zh_CN/core-api/assoc_array.rst | 473 -----
.../translations/zh_CN/core-api/index.rst | 1 -
drivers/md/dm-verity-verify-sig.c | 2 +-
fs/verity/signature.c | 2 +-
include/keys/keyring-type.h | 1 -
include/linux/assoc_array.h | 88 -
include/linux/assoc_array_priv.h | 178 --
include/linux/key-type.h | 1 -
include/linux/key.h | 29 +-
kernel/bpf/keys.c | 2 +-
lib/Kconfig | 14 -
lib/Makefile | 1 -
lib/assoc_array.c | 1724 -----------------
security/keys/Kconfig | 1 -
security/keys/internal.h | 14 +-
security/keys/key.c | 22 +-
security/keys/keyring.c | 977 ++++------
security/keys/request_key.c | 8 +-
.../dm-verity/test-dm-verity-keyring.sh | 2 +-
21 files changed, 462 insertions(+), 3643 deletions(-)
delete mode 100644 Documentation/core-api/assoc_array.rst
delete mode 100644 Documentation/translations/zh_CN/core-api/assoc_array.rst
delete mode 100644 include/linux/assoc_array.h
delete mode 100644 include/linux/assoc_array_priv.h
delete mode 100644 lib/assoc_array.c
--
2.47.3
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH v4 1/3] KEYS: Introduce keyring_is_empty()
2026-10-29 23:24 [PATCH v4 0/3] KEYS: Replace assoc_array with rhashtable Jarkko Sakkinen
@ 2026-10-29 23:24 ` Jarkko Sakkinen
0 siblings, 0 replies; 2+ messages in thread
From: Jarkko Sakkinen @ 2026-10-29 23:24 UTC (permalink / raw)
To: David Howells
Cc: keyrings, Eric Biggers, Jarkko Sakkinen, Lukas Wunner,
Ignat Korchagin, Herbert Xu, David S. Miller, Alasdair Kergon,
Mike Snitzer, Mikulas Patocka, Benjamin Marzinski,
Theodore Y. Ts'o, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Paul Moore, James Morris,
Serge E. Hallyn, Shuah Khan, Christian Brauner,
open list:ASYMMETRIC KEYS, open list,
open list:DEVICE-MAPPER (LVM),
open list:FSVERITY: READ-ONLY FILE-BASED AUTHENTICITY PRO...,
open list:BPF [GENERAL] (Safe Dynamic Programs and Tools),
open list:SECURITY SUBSYSTEM, open list:KERNEL SELFTEST FRAMEWORK
Introduce keyring_is_empty() in order to decouple for BPF, dm-verity and
fs-verity how keyring internally manages the keys.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
drivers/md/dm-verity-verify-sig.c | 2 +-
fs/verity/signature.c | 2 +-
include/linux/key.h | 1 +
kernel/bpf/keys.c | 2 +-
security/keys/keyring.c | 10 ++++++++++
.../selftests/dm-verity/test-dm-verity-keyring.sh | 2 +-
6 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/drivers/md/dm-verity-verify-sig.c b/drivers/md/dm-verity-verify-sig.c
index aadcf5e4a47c..cad21e9144ed 100644
--- a/drivers/md/dm-verity-verify-sig.c
+++ b/drivers/md/dm-verity-verify-sig.c
@@ -153,7 +153,7 @@ int verity_verify_root_hash(const void *root_hash, size_t root_hash_len,
if (ret != -ENOKEY && ret != -EKEYREJECTED)
return ret;
- if (dm_verity_keyring->keys.nr_leaves_on_tree &&
+ if (!keyring_is_empty(dm_verity_keyring) &&
dm_verity_keyring->restrict_link)
ret = verify_pkcs7_signature(root_hash, root_hash_len,
sig_data, sig_len,
diff --git a/fs/verity/signature.c b/fs/verity/signature.c
index 0302a4e506ec..890d33b9d812 100644
--- a/fs/verity/signature.c
+++ b/fs/verity/signature.c
@@ -67,7 +67,7 @@ int fsverity_verify_signature(const struct fsverity_info *vi,
return 0;
}
- if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
+ if (keyring_is_empty(fsverity_keyring)) {
/*
* The ".fs-verity" keyring is empty, due to builtin signatures
* being supported by the kernel but not actually being used.
diff --git a/include/linux/key.h b/include/linux/key.h
index c4486e938a06..20ca43f5eab1 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -439,6 +439,7 @@ extern int restrict_link_reject(struct key *keyring,
struct key *restriction_key);
extern int keyring_clear(struct key *keyring);
+bool keyring_is_empty(const struct key *keyring);
extern key_ref_t keyring_search(key_ref_t keyring,
struct key_type *type,
diff --git a/kernel/bpf/keys.c b/kernel/bpf/keys.c
index bffc356839ac..24e0416abc99 100644
--- a/kernel/bpf/keys.c
+++ b/kernel/bpf/keys.c
@@ -29,7 +29,7 @@ struct bpf_key *bpf_lookup_keyring(void)
if (!bpf_keyring)
return NULL;
- if (!READ_ONCE(bpf_keyring->keys.nr_leaves_on_tree) ||
+ if (keyring_is_empty(bpf_keyring) ||
!READ_ONCE(bpf_keyring->restrict_link))
return NULL;
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 5281da9d5bf7..ac48d6204915 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -443,6 +443,16 @@ static void keyring_destroy(struct key *keyring)
assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops);
}
+/**
+ * keyring_is_empty - Check whether a keyring is empty
+ * @keyring: The keyring to check.
+ */
+bool keyring_is_empty(const struct key *keyring)
+{
+ return READ_ONCE(keyring->keys.nr_leaves_on_tree) == 0;
+}
+EXPORT_SYMBOL(keyring_is_empty);
+
/*
* Describe a keyring for /proc.
*/
diff --git a/tools/testing/selftests/dm-verity/test-dm-verity-keyring.sh b/tools/testing/selftests/dm-verity/test-dm-verity-keyring.sh
index 1f9601ef22f8..792449d356cf 100755
--- a/tools/testing/selftests/dm-verity/test-dm-verity-keyring.sh
+++ b/tools/testing/selftests/dm-verity/test-dm-verity-keyring.sh
@@ -746,7 +746,7 @@ test_keyring_inactive_when_empty() {
log_info "TEST: Verify dm-verity keyring is inactive when sealed empty"
# When keyring_unsealed=0, the keyring is sealed immediately while empty
- # This means it should NOT be used for verification (nr_leaves_on_tree=0)
+ # An empty keyring should NOT be used for verification.
log_info "Keyring state (should be empty and sealed):"
list_keyring_keys
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-29 23:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-29 23:24 [PATCH v4 0/3] KEYS: Replace assoc_array with rhashtable Jarkko Sakkinen
2026-10-29 23:24 ` [PATCH v4 1/3] KEYS: Introduce keyring_is_empty() Jarkko Sakkinen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox