From: sashiko-bot@kernel.org
To: "Jarkko Sakkinen" <jarkko@kernel.org>,
"David Howells" <dhowells@redhat.com>
Cc: "Eric Biggers" <ebiggers@kernel.org>,
"Mimi Zohar" <zohar@linux.ibm.com>,
"Roberto Sassu" <roberto.sassu@huawei.com>,
"Dmitry Kasatkin" <dmitry.kasatkin@gmail.com>,
"Eric Snowberg" <eric.snowberg@oracle.com>,
"Paul Moore" <paul@paul-moore.com>,
"James Morris" <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
"open list:SECURITY SUBSYSTEM"
<linux-security-module@vger.kernel.org>,
"open list:KEYS/KEYRINGS" <keyrings@vger.kernel.org>
Subject: Re: [RFC PATCH 4/4] KEYS: key_garbage_collector(): process in chunks
Date: Mon, 02 Nov 2026 12:32:57 +0000 [thread overview]
Message-ID: <sashiko-outbox-193421@kernel.org> (raw)
In-Reply-To: <20261102121518.1197453-5-jarkko@kernel.org>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] keys: non-atomic flag clearing in key_garbage_collector() drops link reaping
--
commit 9857f1d86bdced127ae471468f5b25b61aa04d0f
Author: Jarkko Sakkinen <jarkko@kernel.org>
KEYS: key_garbage_collector(): process in chunks
This commit updates key_garbage_collector() to scan the serial tree and
destroy unused keys in chunks of 64 items, tracking in-flight state across
workqueue continuations to prevent workqueue lockups.
Link: https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd-test.git/tree/package/keygcbench
> diff --git a/security/keys/gc.c b/security/keys/gc.c
> index 876c30c019ad4..fc1e5e39b828c 100644
> --- a/security/keys/gc.c
> +++ b/security/keys/gc.c
[ ... ]
> @@ -31,6 +42,23 @@ static DEFINE_TIMER(key_gc_timer, key_gc_timer_func);
> static time64_t key_gc_next_run = TIME64_MAX;
> static unsigned long key_gc_flags;
> #define KEY_GC_KEY_EXPIRED 0 /* A key expired and needs unlinking */
> +#define KEY_GC_REAP_REQUESTED 1 /* Another complete pass was requested */
> +
> +/*
> + * Schedule a new GC pass or re-schedule an ongoing GC pass.
> +*/
> +void key_schedule_gc_work(bool expired)
> +{
> + unsigned long flags = BIT(KEY_GC_REAP_REQUESTED);
> +
> + if (expired)
> + flags |= BIT(KEY_GC_KEY_EXPIRED);
> +
> + /* Publish all request flags together, after the caller's updates. */
> + set_mask_bits(&key_gc_flags, 0, flags);
> + schedule_work(&key_gc_work.work);
> +}
[ ... ]
> @@ -137,35 +159,45 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
> static void key_garbage_collector(struct work_struct *work)
> {
> static LIST_HEAD(graveyard);
> - u8 gc_state = 0;
> + typeof(key_gc_work) *gc = &key_gc_work;
> #define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
> #define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */
> +#define KEY_GC_DESTROYING 0x04 /* - We need to destroy keys */
>
> - struct rb_node *cursor;
> + unsigned int chunk = KEY_GC_CHUNK_SIZE;
> struct key *key;
> - time64_t new_timer, limit, expiry;
> + time64_t expiry;
>
> - kenter("[%lx]", key_gc_flags);
> + kenter("[%lx,%x]", key_gc_flags, gc->state);
>
> - limit = ktime_get_real_seconds();
> + if (gc->state & KEY_GC_DESTROYING)
> + goto destroy_keys;
> + if (gc->cursor) {
> + spin_lock(&key_serial_lock);
> + goto continue_scanning;
> + }
>
> + test_and_clear_bit(KEY_GC_REAP_REQUESTED, &key_gc_flags);
> + gc->start = ktime_get_real_seconds();
> + gc->state = 0;
> if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
> - gc_state |= KEY_GC_REAPING_LINKS;
> - kdebug("new pass %x", gc_state);
> + gc->state |= KEY_GC_REAPING_LINKS;
[Severity: Medium]
Can clearing KEY_GC_REAP_REQUESTED and KEY_GC_KEY_EXPIRED in two separate
operations on opposite sides of ktime_get_real_seconds() cause a link-reaping
request to be dropped?
In key_schedule_gc_work(true), called from key_gc_timer_func(),
key_schedule_gc(), key_invalidate(), and key_remove_domain(), both
KEY_GC_REAP_REQUESTED and KEY_GC_KEY_EXPIRED are published together via
set_mask_bits().
However, if another CPU calls key_schedule_gc_work(true) after
key_garbage_collector() executes:
test_and_clear_bit(KEY_GC_REAP_REQUESTED, &key_gc_flags);
gc->start = ktime_get_real_seconds();
(for example, right after the clock ticks to a new second, or after a new key
is invalidated or a domain is removed) and before:
if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
then the current pass consumes KEY_GC_KEY_EXPIRED while leaving
KEY_GC_REAP_REQUESTED set.
The current pass will run keyring_gc(key, gc->start) with the older gc->start
timestamp (or after already having scanned past a keyring if this were not
the start of the pass, or before the new expiry second is reached in
key_is_dead()), and when the follow-up pass triggered by
KEY_GC_REAP_REQUESTED runs, KEY_GC_KEY_EXPIRED is already clear so
KEY_GC_REAPING_LINKS will not be set.
Should key_gc_flags be consumed atomically with xchg(&key_gc_flags, 0) before
sampling gc->start = ktime_get_real_seconds()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261102121518.1197453-1-jarkko@kernel.org?part=4
next prev parent reply other threads:[~2026-11-02 12:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261102121518.1197453-1-jarkko@kernel.org>
2026-11-02 12:15 ` [RFC PATCH 1/4] KEYS: trusted: Disable sysfs bind attributes for TEE driver Jarkko Sakkinen
2026-11-02 12:15 ` [RFC PATCH 2/4] KEYS: Pin the backing module for struct key_type Jarkko Sakkinen
2026-11-02 12:22 ` Jarkko Sakkinen
2026-11-02 12:32 ` sashiko-bot
2026-11-02 12:41 ` Jarkko Sakkinen
2026-11-02 12:15 ` [RFC PATCH 3/4] KEYS: Remove key_type_dead Jarkko Sakkinen
2026-11-02 12:21 ` Jarkko Sakkinen
2026-11-02 12:35 ` sashiko-bot
2026-11-02 12:15 ` [RFC PATCH 4/4] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
2026-11-02 12:31 ` Jarkko Sakkinen
2026-11-02 12:32 ` sashiko-bot [this message]
2026-11-02 13:34 ` Jarkko Sakkinen
2026-11-02 12:19 ` [RFC PATCH 0/4] KEYS: sanitize the garbage collector behavior 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=sashiko-outbox-193421@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dhowells@redhat.com \
--cc=dmitry.kasatkin@gmail.com \
--cc=ebiggers@kernel.org \
--cc=eric.snowberg@oracle.com \
--cc=jarkko@kernel.org \
--cc=jmorris@namei.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=roberto.sassu@huawei.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=serge@hallyn.com \
--cc=zohar@linux.ibm.com \
/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