Skip to content

Commit

Permalink
BACKPORT: cred: add get_cred_rcu()
Browse files Browse the repository at this point in the history
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 <neilb@suse.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>

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 <118538522+backslashxx@users.noreply.github.com>
  • Loading branch information
NeilBrown authored and backslashxx committed Dec 30, 2024
1 parent 230b70f commit 2f7fd3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions include/linux/cred.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ static inline const struct cred *get_cred(const struct cred *cred)
return get_new_cred(nonconst_cred);
}

static inline const struct cred *get_cred_rcu(const struct cred *cred)
{
struct cred *nonconst_cred = (struct cred *) cred;
if (!cred)
return NULL;
if (!atomic_long_inc_not_zero(&nonconst_cred->usage))
return NULL;
validate_creds(cred);
return cred;
}

/**
* put_cred - Release a reference to a set of credentials
* @cred: The credentials to release
Expand Down
2 changes: 1 addition & 1 deletion kernel/cred.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const struct cred *get_task_cred(struct task_struct *task)
do {
cred = __task_cred((task));
BUG_ON(!cred);
} while (!atomic_long_inc_not_zero(&((struct cred *)cred)->usage));
} while (!get_cred_rcu(cred));

rcu_read_unlock();
return cred;
Expand Down

0 comments on commit 2f7fd3a

Please sign in to comment.