12 #include <linux/module.h>
13 #include <linux/slab.h>
32 static void key_gc_timer_func(
unsigned long);
33 static DEFINE_TIMER(key_gc_timer, key_gc_timer_func, 0, 0);
36 static struct key_type *key_gc_dead_keytype;
38 static unsigned long key_gc_flags;
39 #define KEY_GC_KEY_EXPIRED 0
40 #define KEY_GC_REAP_KEYTYPE 1
41 #define KEY_GC_REAPING_KEYTYPE 2
58 unsigned long expires;
61 kenter(
"%ld", gc_at - now);
66 }
else if (gc_at < key_gc_next_run) {
68 key_gc_next_run = gc_at;
87 static void key_gc_timer_func(
unsigned long data)
97 static int key_gc_wait_bit(
void *
flags)
115 kenter(
"%s", ktype->name);
117 key_gc_dead_keytype =
ktype;
129 key_gc_dead_keytype =
NULL;
144 kenter(
"%x", key_serial(keyring));
146 if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
147 (1 << KEY_FLAG_REVOKED)))
158 for (loop--; loop >= 0; loop--) {
160 if (key_is_dead(key, limit))
182 while (!list_empty(keys)) {
187 kdebug(
"- %u", key->serial);
190 security_key_free(key);
193 if (
test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
194 spin_lock(&key->user->lock);
196 key->user->qnbytes -= key->quotalen;
197 spin_unlock(&key->user->lock);
201 if (
test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
207 if (key->type->destroy)
208 key->type->destroy(key);
210 kfree(key->description);
213 key->magic = KEY_DEBUG_MAGIC_X;
230 #define KEY_GC_REAP_AGAIN 0x01
231 #define KEY_GC_REAPING_LINKS 0x02
232 #define KEY_GC_SET_TIMER 0x04
233 #define KEY_GC_REAPING_DEAD_1 0x10
234 #define KEY_GC_REAPING_DEAD_2 0x20
235 #define KEY_GC_REAPING_DEAD_3 0x40
236 #define KEY_GC_FOUND_DEAD_KEY 0x80
242 kenter(
"[%lx,%x]", key_gc_flags, gc_state);
258 kdebug(
"new pass %x", gc_state);
266 spin_lock(&key_serial_lock);
271 key =
rb_entry(cursor,
struct key, serial_node);
275 goto found_unreferenced_key;
277 if (
unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
278 if (key->type == key_gc_dead_keytype) {
280 set_bit(KEY_FLAG_DEAD, &key->flags);
286 if (gc_state & KEY_GC_SET_TIMER) {
287 if (key->expiry > limit && key->expiry < new_timer) {
288 kdebug(
"will expire %x in %ld",
289 key_serial(key), key->expiry - limit);
290 new_timer = key->expiry;
294 if (
unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
295 if (key->type == key_gc_dead_keytype)
298 if ((gc_state & KEY_GC_REAPING_LINKS) ||
299 unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
304 if (
unlikely(gc_state & KEY_GC_REAPING_DEAD_3))
305 if (key->type == key_gc_dead_keytype)
306 goto destroy_dead_key;
309 if (spin_is_contended(&key_serial_lock) || need_resched())
314 spin_unlock(&key_serial_lock);
319 spin_lock(&key_serial_lock);
320 goto continue_scanning;
329 if (gc_state & KEY_GC_SET_TIMER && new_timer != (
time_t)
LONG_MAX) {
334 if (
unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
335 !list_empty(&graveyard)) {
345 if (!list_empty(&graveyard)) {
347 key_gc_unused_keys(&graveyard);
350 if (
unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
351 KEY_GC_REAPING_DEAD_2))) {
352 if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
357 gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
358 gc_state |= KEY_GC_REAPING_DEAD_3;
364 if (
unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
371 if (gc_state & KEY_GC_REAP_AGAIN)
373 kleave(
" [end %x]", gc_state);
379 found_unreferenced_key:
380 kdebug(
"unrefd key %d", key->serial);
382 spin_unlock(&key_serial_lock);
385 gc_state |= KEY_GC_REAP_AGAIN;
394 spin_unlock(&key_serial_lock);
395 kdebug(
"scan keyring %d", key->serial);
396 key_gc_keyring(key, limit);
403 spin_unlock(&key_serial_lock);
404 kdebug(
"destroy key %d", key->serial);
407 if (key_gc_dead_keytype->destroy)
408 key_gc_dead_keytype->destroy(key);