Skip to content

Commit 488ef35

Browse files
herbertxtorvalds
authored andcommitted
KEYS: Invert FINAL_PUT bit
Invert the FINAL_PUT bit so that test_bit_acquire and clear_bit_unlock can be used instead of smp_mb. Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: David Howells <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent aef17cb commit 488ef35

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

include/linux/key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct key {
236236
#define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */
237237
#define KEY_FLAG_KEEP 8 /* set if key should not be removed */
238238
#define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */
239-
#define KEY_FLAG_FINAL_PUT 10 /* set if final put has happened on key */
239+
#define KEY_FLAG_USER_ALIVE 10 /* set if final put has not happened on key yet */
240240

241241
/* the key type and key description string
242242
* - the desc is used to match a key against search criteria

security/keys/gc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ static void key_garbage_collector(struct work_struct *work)
218218
key = rb_entry(cursor, struct key, serial_node);
219219
cursor = rb_next(cursor);
220220

221-
if (test_bit(KEY_FLAG_FINAL_PUT, &key->flags)) {
222-
smp_mb(); /* Clobber key->user after FINAL_PUT seen. */
221+
if (!test_bit_acquire(KEY_FLAG_USER_ALIVE, &key->flags)) {
222+
/* Clobber key->user after final put seen. */
223223
goto found_unreferenced_key;
224224
}
225225

security/keys/key.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
298298
key->restrict_link = restrict_link;
299299
key->last_used_at = ktime_get_real_seconds();
300300

301+
key->flags |= 1 << KEY_FLAG_USER_ALIVE;
301302
if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
302303
key->flags |= 1 << KEY_FLAG_IN_QUOTA;
303304
if (flags & KEY_ALLOC_BUILT_IN)
@@ -658,8 +659,8 @@ void key_put(struct key *key)
658659
key->user->qnbytes -= key->quotalen;
659660
spin_unlock_irqrestore(&key->user->lock, flags);
660661
}
661-
smp_mb(); /* key->user before FINAL_PUT set. */
662-
set_bit(KEY_FLAG_FINAL_PUT, &key->flags);
662+
/* Mark key as safe for GC after key->user done. */
663+
clear_bit_unlock(KEY_FLAG_USER_ALIVE, &key->flags);
663664
schedule_work(&key_gc_work);
664665
}
665666
}

0 commit comments

Comments
 (0)