mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: David Howells <dhowells@redhat.com>
Cc: keyrings@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Eric Biggers <ebiggers@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Paul Moore <paul@paul-moore.com>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	open list <linux-kernel@vger.kernel.org>,
	"open list:SECURITY SUBSYSTEM"
	<linux-security-module@vger.kernel.org>
Subject: Re: [PATCH v2 5/6] KEYS: Replace assoc_array with rhashtable
Date: Sat, 24 Oct 2026 17:28:35 +0300	[thread overview]
Message-ID: <atzAk-1IhxTN5nk8@kernel.org> (raw)
In-Reply-To: <20261023120953.1206778-6-jarkko@kernel.org>

On Fri, Oct 23, 2026 at 03:09:25PM +0300, Jarkko Sakkinen wrote:
> @@ -516,8 +507,7 @@ static long keyring_read(const struct key *keyring,
>  		ctx.buffer = (key_serial_t *)buffer;
>  		ctx.buflen = buflen;
>  		ctx.count = 0;
> -		ret = assoc_array_iterate(&keyring->keys,
> -					  keyring_read_iterator, &ctx);
> +		ret = keyring_iterate(keyring, keyring_read_iterator, &ctx);
>  		if (ret < 0) {
>  			kleave(" = %ld [iterate]", ret);
>  			return ret;
> @@ -525,7 +515,7 @@ static long keyring_read(const struct key *keyring,
>  	}
>  
>  	/* Return the size of the buffer needed */
> -	ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
> +	ret = keyring_nr_links(keyring) * sizeof(key_serial_t);
>  	if (ret <= buflen)
>  		kleave("= %ld [ok]", ret);
>  	else

I changed this a bit because I wanted to bake cond_resched() between the
iterations. I'd be interested getting feedback about the change below.

I.e. in my working branch I have this laid out as follows:

-struct keyring_read_iterator_context {
-	size_t			buflen;
-	size_t			count;
-	key_serial_t		*buffer;
-};
-
-static int keyring_read_iterator(const void *object, void *data)
-{
-	struct keyring_read_iterator_context *ctx = data;
-	const struct key *key = keyring_ptr_to_key(object);
-
-	kenter("{%s,%d},,{%zu/%zu}",
-	       key->type->name, key->serial, ctx->count, ctx->buflen);
-
-	if (ctx->count >= ctx->buflen)
-		return 1;
-
-	*ctx->buffer++ = key->serial;
-	ctx->count += sizeof(key->serial);
-	return 0;
-}
-
 /*
  * Read a list of key IDs from the keyring's contents in binary form
  *
@@ -503,7 +473,10 @@ static int keyring_read_iterator(const void *object, void *data)
 static long keyring_read(const struct key *keyring,
 			 char *buffer, size_t buflen)
 {
-	struct keyring_read_iterator_context ctx;
+	struct keyring_index *links = keyring_index_locked(keyring);
+	struct keyring_link *link;
+	key_serial_t *serials = (key_serial_t *)buffer;
+	size_t count = 0;
 	long ret;

 	kenter("{%d},,%zu", key_serial(keyring), buflen);
@@ -512,20 +485,18 @@ static long keyring_read(const struct key *keyring,
 		return -EINVAL;

 	/* Copy as many key IDs as fit into the buffer */
-	if (buffer && buflen) {
-		ctx.buffer = (key_serial_t *)buffer;
-		ctx.buflen = buflen;
-		ctx.count = 0;
-		ret = assoc_array_iterate(&keyring->keys,
-					  keyring_read_iterator, &ctx);
-		if (ret < 0) {
-			kleave(" = %ld [iterate]", ret);
-			return ret;
+	if (buffer && buflen && links) {
+		list_for_each_entry(link, &links->list, list) {
+			if (count == buflen / sizeof(*serials))
+				break;
+
+			serials[count++] = keyring_link_key(link)->serial;
+			cond_resched();
 		}
 	}

 	/* Return the size of the buffer needed */
-	ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t);
+	ret = links ? links->nr * sizeof(key_serial_t) : 0;
 	if (ret <= buflen)
 		kleave("= %ld [ok]", ret);
 	else

What do you think about this? I'm worried about blind spot mainly
because this feels too obvious.

Br, Jarkko

  parent reply	other threads:[~2026-10-24 14:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23 12:09 [PATCH v2 0/6] KEYS: From assoc_array to rhashtable migration Jarkko Sakkinen
2026-10-23 12:09 ` [PATCH v2 1/6] KEYS: Expand key->{datalen,quotalen} to unsigned int Jarkko Sakkinen
2026-10-23 12:09 ` [PATCH v2 2/6] KEYS: Reject descriptions larger than KEY_MAX_DESC_SIZE Jarkko Sakkinen
2026-10-23 12:09 ` [PATCH v2 3/6] KEYS: Verify the key identity in __key_unlink_begin Jarkko Sakkinen
2026-10-23 12:09 ` [PATCH v2 4/6] KEYS: Introduce keyring_is_empty() Jarkko Sakkinen
2026-10-23 12:09 ` [PATCH v2 5/6] KEYS: Replace assoc_array with rhashtable Jarkko Sakkinen
2026-10-23 13:22   ` Jarkko Sakkinen
2026-10-24  4:58   ` Jarkko Sakkinen
2026-10-24 14:28   ` Jarkko Sakkinen [this message]
2026-10-24 16:26     ` Jarkko Sakkinen
2026-10-23 12:09 ` [PATCH v2 6/6] lib: Remove assoc_array Jarkko Sakkinen
2026-10-23 12:36 ` [PATCH v2 0/6] KEYS: From assoc_array to rhashtable migration 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=atzAk-1IhxTN5nk8@kernel.org \
    --to=jarkko@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=dhowells@redhat.com \
    --cc=ebiggers@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=serge@hallyn.com \
    --cc=torvalds@linux-foundation.org \
    /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

all inboxes | Powered by JetHome®