staticunsignedlong key_gc_flags; #define KEY_GC_KEY_EXPIRED 0/* A key expired and needs unlinking */ #define KEY_GC_REAP_KEYTYPE 1/* A keytype is being unregistered */ #define KEY_GC_REAPING_KEYTYPE 2/* Cleared when keytype reaped */
/* *Garbagecollectorforunusedkeys. * *Thisisdoneinprocesscontextsothatwedon'thavetodisableinterrupts *allovertheplace.key_put()schedulesthisratherthantryingtodothe *cleanupitself,whichmeanskey_put()doesn'thavetosleep.
*/ staticvoid key_garbage_collector(struct work_struct *work)
{ static LIST_HEAD(graveyard); static u8 gc_state; /* Internal persistent state */ #define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */ #define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */ #define KEY_GC_REAPING_DEAD_1 0x10 /* - We need to mark dead keys */ #define KEY_GC_REAPING_DEAD_2 0x20 /* - We need to reap dead key links */ #define KEY_GC_REAPING_DEAD_3 0x40 /* - We need to reap dead keys */ #define KEY_GC_FOUND_DEAD_KEY 0x80 /* - We found at least one dead key */
/* Work out what we're going to be doing in this pass */
gc_state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
gc_state <<= 1; if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
gc_state |= KEY_GC_REAPING_LINKS;
if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
gc_state |= KEY_GC_REAPING_DEAD_1;
kdebug("new pass %x", gc_state);
new_timer = TIME64_MAX;
/* As only this function is permitted to remove things from the key *serialtree,ifcursorisnon-NULLthenitwillalwayspointtoa *validnodeinthetree-eveniflockgotdropped.
*/
spin_lock(&key_serial_lock);
cursor = rb_first(&key_serial_tree);
expiry = key->expiry; if (expiry != TIME64_MAX) { if (!(key->type->flags & KEY_TYPE_INSTANT_REAP))
expiry += key_gc_delay; if (expiry > limit && expiry < new_timer) {
kdebug("will expire %x in %lld",
key_serial(key), key->expiry - limit);
new_timer = key->expiry;
}
}
if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) if (key->type == key_gc_dead_keytype)
gc_state |= KEY_GC_FOUND_DEAD_KEY;
if ((gc_state & KEY_GC_REAPING_LINKS) ||
unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) { if (key->type == &key_type_keyring) goto found_keyring;
}
if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) if (key->type == key_gc_dead_keytype) goto destroy_dead_key;
skip_dead_key: if (spin_is_contended(&key_serial_lock) || need_resched()) goto contended;
}
contended:
spin_unlock(&key_serial_lock);
maybe_resched: if (cursor) {
cond_resched();
spin_lock(&key_serial_lock); goto continue_scanning;
}
/* We've completed the pass. Set the timer if we need to and queue a *newcycleifnecessary.Wekeepexecutingcyclesuntilwefindone *wherewedidn'treapanykeys.
*/
kdebug("pass complete");
if (new_timer != TIME64_MAX) {
new_timer += key_gc_delay;
key_schedule_gc(new_timer);
}
if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
!list_empty(&graveyard)) { /* Make sure that all pending keyring payload destructions are *fulfilledandthatpeoplearen'tnowlookingatdeador *dyingkeysthattheydon'thaveareferenceuponoralink *to.
*/
kdebug("gc sync");
synchronize_rcu();
}
if (!list_empty(&graveyard)) {
kdebug("gc keys");
key_gc_unused_keys(&graveyard);
}
if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
KEY_GC_REAPING_DEAD_2))) { if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) { /* No remaining dead keys: short circuit the remaining *keytypereapcycles.
*/
kdebug("dead short");
gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
gc_state |= KEY_GC_REAPING_DEAD_3;
} else {
gc_state |= KEY_GC_REAP_AGAIN;
}
}
if (gc_state & KEY_GC_REAP_AGAIN)
schedule_work(&key_gc_work);
kleave(" [end %x]", gc_state); return;
/* We found an unreferenced key - once we've removed it from the tree, *wecansafelydropthelock.
*/
found_unreferenced_key:
kdebug("unrefd key %d", key->serial);
rb_erase(&key->serial_node, &key_serial_tree);
spin_unlock(&key_serial_lock);
/* We found a restricted keyring and need to update the restriction if *itisassociatedwiththedeadkeytype.
*/
found_restricted_keyring:
spin_unlock(&key_serial_lock);
keyring_restriction_gc(key, key_gc_dead_keytype); goto maybe_resched;
/* We found a keyring and we need to check the payload for links to *deadorexpiredkeys.Wedon'tflaganotherreapimmediatelyaswe *havetowaitfortheoldpayloadtobedestroyedbyRCUbeforewe *canreapthekeystowhichitrefers.
*/
found_keyring:
spin_unlock(&key_serial_lock);
keyring_gc(key, limit); goto maybe_resched;
/* We found a dead key that is still referenced. Reset its type and *destroyitspayloadwithitssemaphoreheld.
*/
destroy_dead_key:
spin_unlock(&key_serial_lock);
kdebug("destroy key %d", key->serial);
down_write(&key->sem);
key->type = &key_type_dead; if (key_gc_dead_keytype->destroy)
key_gc_dead_keytype->destroy(key);
memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
up_write(&key->sem); goto maybe_resched;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.