xref: /OK3568_Linux_fs/kernel/security/keys/gc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* Key garbage collector
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2009-2011 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun  * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include <linux/security.h>
10*4882a593Smuzhiyun #include <keys/keyring-type.h>
11*4882a593Smuzhiyun #include "internal.h"
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun  * Delay between key revocation/expiry in seconds
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun unsigned key_gc_delay = 5 * 60;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * Reaper for unused keys.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun static void key_garbage_collector(struct work_struct *work);
22*4882a593Smuzhiyun DECLARE_WORK(key_gc_work, key_garbage_collector);
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Reaper for links from keyrings to dead keys.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun static void key_gc_timer_func(struct timer_list *);
28*4882a593Smuzhiyun static DEFINE_TIMER(key_gc_timer, key_gc_timer_func);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static time64_t key_gc_next_run = TIME64_MAX;
31*4882a593Smuzhiyun static struct key_type *key_gc_dead_keytype;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static unsigned long key_gc_flags;
34*4882a593Smuzhiyun #define KEY_GC_KEY_EXPIRED	0	/* A key expired and needs unlinking */
35*4882a593Smuzhiyun #define KEY_GC_REAP_KEYTYPE	1	/* A keytype is being unregistered */
36*4882a593Smuzhiyun #define KEY_GC_REAPING_KEYTYPE	2	/* Cleared when keytype reaped */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun  * Any key whose type gets unregistered will be re-typed to this if it can't be
41*4882a593Smuzhiyun  * immediately unlinked.
42*4882a593Smuzhiyun  */
43*4882a593Smuzhiyun struct key_type key_type_dead = {
44*4882a593Smuzhiyun 	.name = ".dead",
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * Schedule a garbage collection run.
49*4882a593Smuzhiyun  * - time precision isn't particularly important
50*4882a593Smuzhiyun  */
key_schedule_gc(time64_t gc_at)51*4882a593Smuzhiyun void key_schedule_gc(time64_t gc_at)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	unsigned long expires;
54*4882a593Smuzhiyun 	time64_t now = ktime_get_real_seconds();
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	kenter("%lld", gc_at - now);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
59*4882a593Smuzhiyun 		kdebug("IMMEDIATE");
60*4882a593Smuzhiyun 		schedule_work(&key_gc_work);
61*4882a593Smuzhiyun 	} else if (gc_at < key_gc_next_run) {
62*4882a593Smuzhiyun 		kdebug("DEFERRED");
63*4882a593Smuzhiyun 		key_gc_next_run = gc_at;
64*4882a593Smuzhiyun 		expires = jiffies + (gc_at - now) * HZ;
65*4882a593Smuzhiyun 		mod_timer(&key_gc_timer, expires);
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * Schedule a dead links collection run.
71*4882a593Smuzhiyun  */
key_schedule_gc_links(void)72*4882a593Smuzhiyun void key_schedule_gc_links(void)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
75*4882a593Smuzhiyun 	schedule_work(&key_gc_work);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * Some key's cleanup time was met after it expired, so we need to get the
80*4882a593Smuzhiyun  * reaper to go through a cycle finding expired keys.
81*4882a593Smuzhiyun  */
key_gc_timer_func(struct timer_list * unused)82*4882a593Smuzhiyun static void key_gc_timer_func(struct timer_list *unused)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	kenter("");
85*4882a593Smuzhiyun 	key_gc_next_run = TIME64_MAX;
86*4882a593Smuzhiyun 	key_schedule_gc_links();
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun  * Reap keys of dead type.
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * We use three flags to make sure we see three complete cycles of the garbage
93*4882a593Smuzhiyun  * collector: the first to mark keys of that type as being dead, the second to
94*4882a593Smuzhiyun  * collect dead links and the third to clean up the dead keys.  We have to be
95*4882a593Smuzhiyun  * careful as there may already be a cycle in progress.
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * The caller must be holding key_types_sem.
98*4882a593Smuzhiyun  */
key_gc_keytype(struct key_type * ktype)99*4882a593Smuzhiyun void key_gc_keytype(struct key_type *ktype)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	kenter("%s", ktype->name);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	key_gc_dead_keytype = ktype;
104*4882a593Smuzhiyun 	set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
105*4882a593Smuzhiyun 	smp_mb();
106*4882a593Smuzhiyun 	set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	kdebug("schedule");
109*4882a593Smuzhiyun 	schedule_work(&key_gc_work);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	kdebug("sleep");
112*4882a593Smuzhiyun 	wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
113*4882a593Smuzhiyun 		    TASK_UNINTERRUPTIBLE);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	key_gc_dead_keytype = NULL;
116*4882a593Smuzhiyun 	kleave("");
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun  * Garbage collect a list of unreferenced, detached keys
121*4882a593Smuzhiyun  */
key_gc_unused_keys(struct list_head * keys)122*4882a593Smuzhiyun static noinline void key_gc_unused_keys(struct list_head *keys)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	while (!list_empty(keys)) {
125*4882a593Smuzhiyun 		struct key *key =
126*4882a593Smuzhiyun 			list_entry(keys->next, struct key, graveyard_link);
127*4882a593Smuzhiyun 		short state = key->state;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 		list_del(&key->graveyard_link);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		kdebug("- %u", key->serial);
132*4882a593Smuzhiyun 		key_check(key);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun #ifdef CONFIG_KEY_NOTIFICATIONS
135*4882a593Smuzhiyun 		remove_watch_list(key->watchers, key->serial);
136*4882a593Smuzhiyun 		key->watchers = NULL;
137*4882a593Smuzhiyun #endif
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		/* Throw away the key data if the key is instantiated */
140*4882a593Smuzhiyun 		if (state == KEY_IS_POSITIVE && key->type->destroy)
141*4882a593Smuzhiyun 			key->type->destroy(key);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		security_key_free(key);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		/* deal with the user's key tracking and quota */
146*4882a593Smuzhiyun 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
147*4882a593Smuzhiyun 			spin_lock(&key->user->lock);
148*4882a593Smuzhiyun 			key->user->qnkeys--;
149*4882a593Smuzhiyun 			key->user->qnbytes -= key->quotalen;
150*4882a593Smuzhiyun 			spin_unlock(&key->user->lock);
151*4882a593Smuzhiyun 		}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 		atomic_dec(&key->user->nkeys);
154*4882a593Smuzhiyun 		if (state != KEY_IS_UNINSTANTIATED)
155*4882a593Smuzhiyun 			atomic_dec(&key->user->nikeys);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		key_user_put(key->user);
158*4882a593Smuzhiyun 		key_put_tag(key->domain_tag);
159*4882a593Smuzhiyun 		kfree(key->description);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		memzero_explicit(key, sizeof(*key));
162*4882a593Smuzhiyun 		kmem_cache_free(key_jar, key);
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun /*
167*4882a593Smuzhiyun  * Garbage collector for unused keys.
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * This is done in process context so that we don't have to disable interrupts
170*4882a593Smuzhiyun  * all over the place.  key_put() schedules this rather than trying to do the
171*4882a593Smuzhiyun  * cleanup itself, which means key_put() doesn't have to sleep.
172*4882a593Smuzhiyun  */
key_garbage_collector(struct work_struct * work)173*4882a593Smuzhiyun static void key_garbage_collector(struct work_struct *work)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	static LIST_HEAD(graveyard);
176*4882a593Smuzhiyun 	static u8 gc_state;		/* Internal persistent state */
177*4882a593Smuzhiyun #define KEY_GC_REAP_AGAIN	0x01	/* - Need another cycle */
178*4882a593Smuzhiyun #define KEY_GC_REAPING_LINKS	0x02	/* - We need to reap links */
179*4882a593Smuzhiyun #define KEY_GC_SET_TIMER	0x04	/* - We need to restart the timer */
180*4882a593Smuzhiyun #define KEY_GC_REAPING_DEAD_1	0x10	/* - We need to mark dead keys */
181*4882a593Smuzhiyun #define KEY_GC_REAPING_DEAD_2	0x20	/* - We need to reap dead key links */
182*4882a593Smuzhiyun #define KEY_GC_REAPING_DEAD_3	0x40	/* - We need to reap dead keys */
183*4882a593Smuzhiyun #define KEY_GC_FOUND_DEAD_KEY	0x80	/* - We found at least one dead key */
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	struct rb_node *cursor;
186*4882a593Smuzhiyun 	struct key *key;
187*4882a593Smuzhiyun 	time64_t new_timer, limit;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	kenter("[%lx,%x]", key_gc_flags, gc_state);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	limit = ktime_get_real_seconds();
192*4882a593Smuzhiyun 	if (limit > key_gc_delay)
193*4882a593Smuzhiyun 		limit -= key_gc_delay;
194*4882a593Smuzhiyun 	else
195*4882a593Smuzhiyun 		limit = key_gc_delay;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	/* Work out what we're going to be doing in this pass */
198*4882a593Smuzhiyun 	gc_state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
199*4882a593Smuzhiyun 	gc_state <<= 1;
200*4882a593Smuzhiyun 	if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
201*4882a593Smuzhiyun 		gc_state |= KEY_GC_REAPING_LINKS | KEY_GC_SET_TIMER;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
204*4882a593Smuzhiyun 		gc_state |= KEY_GC_REAPING_DEAD_1;
205*4882a593Smuzhiyun 	kdebug("new pass %x", gc_state);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	new_timer = TIME64_MAX;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* As only this function is permitted to remove things from the key
210*4882a593Smuzhiyun 	 * serial tree, if cursor is non-NULL then it will always point to a
211*4882a593Smuzhiyun 	 * valid node in the tree - even if lock got dropped.
212*4882a593Smuzhiyun 	 */
213*4882a593Smuzhiyun 	spin_lock(&key_serial_lock);
214*4882a593Smuzhiyun 	cursor = rb_first(&key_serial_tree);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun continue_scanning:
217*4882a593Smuzhiyun 	while (cursor) {
218*4882a593Smuzhiyun 		key = rb_entry(cursor, struct key, serial_node);
219*4882a593Smuzhiyun 		cursor = rb_next(cursor);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		if (refcount_read(&key->usage) == 0)
222*4882a593Smuzhiyun 			goto found_unreferenced_key;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
225*4882a593Smuzhiyun 			if (key->type == key_gc_dead_keytype) {
226*4882a593Smuzhiyun 				gc_state |= KEY_GC_FOUND_DEAD_KEY;
227*4882a593Smuzhiyun 				set_bit(KEY_FLAG_DEAD, &key->flags);
228*4882a593Smuzhiyun 				key->perm = 0;
229*4882a593Smuzhiyun 				goto skip_dead_key;
230*4882a593Smuzhiyun 			} else if (key->type == &key_type_keyring &&
231*4882a593Smuzhiyun 				   key->restrict_link) {
232*4882a593Smuzhiyun 				goto found_restricted_keyring;
233*4882a593Smuzhiyun 			}
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 		if (gc_state & KEY_GC_SET_TIMER) {
237*4882a593Smuzhiyun 			if (key->expiry > limit && key->expiry < new_timer) {
238*4882a593Smuzhiyun 				kdebug("will expire %x in %lld",
239*4882a593Smuzhiyun 				       key_serial(key), key->expiry - limit);
240*4882a593Smuzhiyun 				new_timer = key->expiry;
241*4882a593Smuzhiyun 			}
242*4882a593Smuzhiyun 		}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
245*4882a593Smuzhiyun 			if (key->type == key_gc_dead_keytype)
246*4882a593Smuzhiyun 				gc_state |= KEY_GC_FOUND_DEAD_KEY;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 		if ((gc_state & KEY_GC_REAPING_LINKS) ||
249*4882a593Smuzhiyun 		    unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
250*4882a593Smuzhiyun 			if (key->type == &key_type_keyring)
251*4882a593Smuzhiyun 				goto found_keyring;
252*4882a593Smuzhiyun 		}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 		if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3))
255*4882a593Smuzhiyun 			if (key->type == key_gc_dead_keytype)
256*4882a593Smuzhiyun 				goto destroy_dead_key;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	skip_dead_key:
259*4882a593Smuzhiyun 		if (spin_is_contended(&key_serial_lock) || need_resched())
260*4882a593Smuzhiyun 			goto contended;
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun contended:
264*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun maybe_resched:
267*4882a593Smuzhiyun 	if (cursor) {
268*4882a593Smuzhiyun 		cond_resched();
269*4882a593Smuzhiyun 		spin_lock(&key_serial_lock);
270*4882a593Smuzhiyun 		goto continue_scanning;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	/* We've completed the pass.  Set the timer if we need to and queue a
274*4882a593Smuzhiyun 	 * new cycle if necessary.  We keep executing cycles until we find one
275*4882a593Smuzhiyun 	 * where we didn't reap any keys.
276*4882a593Smuzhiyun 	 */
277*4882a593Smuzhiyun 	kdebug("pass complete");
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	if (gc_state & KEY_GC_SET_TIMER && new_timer != (time64_t)TIME64_MAX) {
280*4882a593Smuzhiyun 		new_timer += key_gc_delay;
281*4882a593Smuzhiyun 		key_schedule_gc(new_timer);
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
285*4882a593Smuzhiyun 	    !list_empty(&graveyard)) {
286*4882a593Smuzhiyun 		/* Make sure that all pending keyring payload destructions are
287*4882a593Smuzhiyun 		 * fulfilled and that people aren't now looking at dead or
288*4882a593Smuzhiyun 		 * dying keys that they don't have a reference upon or a link
289*4882a593Smuzhiyun 		 * to.
290*4882a593Smuzhiyun 		 */
291*4882a593Smuzhiyun 		kdebug("gc sync");
292*4882a593Smuzhiyun 		synchronize_rcu();
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (!list_empty(&graveyard)) {
296*4882a593Smuzhiyun 		kdebug("gc keys");
297*4882a593Smuzhiyun 		key_gc_unused_keys(&graveyard);
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	if (unlikely(gc_state & (KEY_GC_REAPING_DEAD_1 |
301*4882a593Smuzhiyun 				 KEY_GC_REAPING_DEAD_2))) {
302*4882a593Smuzhiyun 		if (!(gc_state & KEY_GC_FOUND_DEAD_KEY)) {
303*4882a593Smuzhiyun 			/* No remaining dead keys: short circuit the remaining
304*4882a593Smuzhiyun 			 * keytype reap cycles.
305*4882a593Smuzhiyun 			 */
306*4882a593Smuzhiyun 			kdebug("dead short");
307*4882a593Smuzhiyun 			gc_state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
308*4882a593Smuzhiyun 			gc_state |= KEY_GC_REAPING_DEAD_3;
309*4882a593Smuzhiyun 		} else {
310*4882a593Smuzhiyun 			gc_state |= KEY_GC_REAP_AGAIN;
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
315*4882a593Smuzhiyun 		kdebug("dead wake");
316*4882a593Smuzhiyun 		smp_mb();
317*4882a593Smuzhiyun 		clear_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
318*4882a593Smuzhiyun 		wake_up_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE);
319*4882a593Smuzhiyun 	}
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (gc_state & KEY_GC_REAP_AGAIN)
322*4882a593Smuzhiyun 		schedule_work(&key_gc_work);
323*4882a593Smuzhiyun 	kleave(" [end %x]", gc_state);
324*4882a593Smuzhiyun 	return;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	/* We found an unreferenced key - once we've removed it from the tree,
327*4882a593Smuzhiyun 	 * we can safely drop the lock.
328*4882a593Smuzhiyun 	 */
329*4882a593Smuzhiyun found_unreferenced_key:
330*4882a593Smuzhiyun 	kdebug("unrefd key %d", key->serial);
331*4882a593Smuzhiyun 	rb_erase(&key->serial_node, &key_serial_tree);
332*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	list_add_tail(&key->graveyard_link, &graveyard);
335*4882a593Smuzhiyun 	gc_state |= KEY_GC_REAP_AGAIN;
336*4882a593Smuzhiyun 	goto maybe_resched;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	/* We found a restricted keyring and need to update the restriction if
339*4882a593Smuzhiyun 	 * it is associated with the dead key type.
340*4882a593Smuzhiyun 	 */
341*4882a593Smuzhiyun found_restricted_keyring:
342*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
343*4882a593Smuzhiyun 	keyring_restriction_gc(key, key_gc_dead_keytype);
344*4882a593Smuzhiyun 	goto maybe_resched;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	/* We found a keyring and we need to check the payload for links to
347*4882a593Smuzhiyun 	 * dead or expired keys.  We don't flag another reap immediately as we
348*4882a593Smuzhiyun 	 * have to wait for the old payload to be destroyed by RCU before we
349*4882a593Smuzhiyun 	 * can reap the keys to which it refers.
350*4882a593Smuzhiyun 	 */
351*4882a593Smuzhiyun found_keyring:
352*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
353*4882a593Smuzhiyun 	keyring_gc(key, limit);
354*4882a593Smuzhiyun 	goto maybe_resched;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* We found a dead key that is still referenced.  Reset its type and
357*4882a593Smuzhiyun 	 * destroy its payload with its semaphore held.
358*4882a593Smuzhiyun 	 */
359*4882a593Smuzhiyun destroy_dead_key:
360*4882a593Smuzhiyun 	spin_unlock(&key_serial_lock);
361*4882a593Smuzhiyun 	kdebug("destroy key %d", key->serial);
362*4882a593Smuzhiyun 	down_write(&key->sem);
363*4882a593Smuzhiyun 	key->type = &key_type_dead;
364*4882a593Smuzhiyun 	if (key_gc_dead_keytype->destroy)
365*4882a593Smuzhiyun 		key_gc_dead_keytype->destroy(key);
366*4882a593Smuzhiyun 	memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
367*4882a593Smuzhiyun 	up_write(&key->sem);
368*4882a593Smuzhiyun 	goto maybe_resched;
369*4882a593Smuzhiyun }
370