Skip to content

Commit 95903fb

Browse files
NeilBrownbackslashxx
authored andcommitted
BACKPORT: cred: add get_cred_rcu()
torvalds/linux@97d0fb2 upstream Sometimes we want to opportunistically get a ref to a cred in an rcu_read_lock protected section. get_task_cred() does this, and NFS does as similar thing with its own credential structures. To prepare for NFS converting to use 'struct cred' more uniformly, define get_cred_rcu(), and use it in get_task_cred(). Signed-off-by: NeilBrown <[email protected]> Signed-off-by: Anna Schumaker <[email protected]> CONFLICTS: cred: switch to using atomic_long_t - resolve conflict by using `atomic_long_inc_not_zero` instead of `atomic_inc_not_zero` Signed-off-by: backslashxx <[email protected]>
1 parent 35a2491 commit 95903fb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

include/linux/cred.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ static inline const struct cred *get_cred(const struct cred *cred)
252252
return get_new_cred(nonconst_cred);
253253
}
254254

255+
static inline const struct cred *get_cred_rcu(const struct cred *cred)
256+
{
257+
struct cred *nonconst_cred = (struct cred *) cred;
258+
if (!cred)
259+
return NULL;
260+
if (!atomic_long_inc_not_zero(&nonconst_cred->usage))
261+
return NULL;
262+
validate_creds(cred);
263+
return cred;
264+
}
265+
255266
/**
256267
* put_cred - Release a reference to a set of credentials
257268
* @cred: The credentials to release

kernel/cred.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ const struct cred *get_task_cred(struct task_struct *task)
196196
do {
197197
cred = __task_cred((task));
198198
BUG_ON(!cred);
199-
} while (!atomic_long_inc_not_zero(&((struct cred *)cred)->usage));
199+
} while (!get_cred_rcu(cred));
200200

201201
rcu_read_unlock();
202202
return cred;

0 commit comments

Comments
 (0)