75
* Schedule a dead links collection run.
77
void key_schedule_gc_links(void)
79
set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
80
queue_work(system_nrt_wq, &key_gc_work);
75
84
* Some key's cleanup time was met after it expired, so we need to get the
76
85
* reaper to go through a cycle finding expired keys.
131
139
static void key_gc_keyring(struct key *keyring, time_t limit)
133
141
struct keyring_list *klist;
137
144
kenter("%x", key_serial(keyring));
139
if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
146
if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) |
147
(1 << KEY_FLAG_REVOKED)))
142
150
/* scan the keyring looking for dead keys */
146
154
goto unlock_dont_gc;
148
for (loop = klist->nkeys - 1; loop >= 0; loop--) {
149
key = klist->keys[loop];
150
if (test_bit(KEY_FLAG_DEAD, &key->flags) ||
151
(key->expiry > 0 && key->expiry <= limit))
158
for (loop--; loop >= 0; loop--) {
159
struct key *key = rcu_dereference(klist->keys[loop]);
160
if (key_is_dead(key, limit))
169
* Garbage collect an unreferenced, detached key
178
* Garbage collect a list of unreferenced, detached keys
171
static noinline void key_gc_unused_key(struct key *key)
180
static noinline void key_gc_unused_keys(struct list_head *keys)
175
security_key_free(key);
177
/* deal with the user's key tracking and quota */
178
if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
179
spin_lock(&key->user->lock);
181
key->user->qnbytes -= key->quotalen;
182
spin_unlock(&key->user->lock);
185
atomic_dec(&key->user->nkeys);
186
if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
187
atomic_dec(&key->user->nikeys);
189
key_user_put(key->user);
191
/* now throw away the key memory */
192
if (key->type->destroy)
193
key->type->destroy(key);
195
kfree(key->description);
182
while (!list_empty(keys)) {
184
list_entry(keys->next, struct key, graveyard_link);
185
list_del(&key->graveyard_link);
187
kdebug("- %u", key->serial);
190
security_key_free(key);
192
/* deal with the user's key tracking and quota */
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);
200
atomic_dec(&key->user->nkeys);
201
if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
202
atomic_dec(&key->user->nikeys);
204
key_user_put(key->user);
206
/* now throw away the key memory */
207
if (key->type->destroy)
208
key->type->destroy(key);
210
kfree(key->description);
197
212
#ifdef KEY_DEBUGGING
198
key->magic = KEY_DEBUG_MAGIC_X;
213
key->magic = KEY_DEBUG_MAGIC_X;
200
kmem_cache_free(key_jar, key);
215
kmem_cache_free(key_jar, key);
210
226
static void key_garbage_collector(struct work_struct *work)
228
static LIST_HEAD(graveyard);
212
229
static u8 gc_state; /* Internal persistent state */
213
230
#define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
214
231
#define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */
314
331
key_schedule_gc(new_timer);
317
if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
318
/* Make sure everyone revalidates their keys if we marked a
319
* bunch as being dead and make sure all keyring ex-payloads
334
if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
335
!list_empty(&graveyard)) {
336
/* Make sure that all pending keyring payload destructions are
337
* fulfilled and that people aren't now looking at dead or
338
* dying keys that they don't have a reference upon or a link
323
342
synchronize_rcu();
345
if (!list_empty(&graveyard)) {
347
key_gc_unused_keys(&graveyard);
326
350
if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
327
351
KEY_GC_REAPING_DEAD_2))) {
328
352
if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
357
381
rb_erase(&key->serial_node, &key_serial_tree);
358
382
spin_unlock(&key_serial_lock);
360
key_gc_unused_key(key);
384
list_add_tail(&key->graveyard_link, &graveyard);
361
385
gc_state |= KEY_GC_REAP_AGAIN;
362
386
goto maybe_resched;