Skip to content

Commit e9bdcdb

Browse files
author
Christian Brauner
committed
pid: add pidfd_get_task() helper
The number of system calls making use of pidfds is constantly increasing. Some of those new system calls duplicate the code to turn a pidfd into task_struct it refers to. Give them a simple helper for this. Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Cc: Vlastimil Babka <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: Matthew Bobrowski <[email protected]> Cc: Alexander Duyck <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Jan Kara <[email protected]> Cc: Minchan Kim <[email protected]> Reviewed-by: Matthew Bobrowski <[email protected]> Acked-by: David Hildenbrand <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 64570fb commit e9bdcdb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

include/linux/pid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ struct file;
7878

7979
extern struct pid *pidfd_pid(const struct file *file);
8080
struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags);
81+
struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags);
8182
int pidfd_create(struct pid *pid, unsigned int flags);
8283

8384
static inline struct pid *get_pid(struct pid *pid)

kernel/pid.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,42 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
539539
return pid;
540540
}
541541

542+
/**
543+
* pidfd_get_task() - Get the task associated with a pidfd
544+
*
545+
* @pidfd: pidfd for which to get the task
546+
* @flags: flags associated with this pidfd
547+
*
548+
* Return the task associated with @pidfd. The function takes a reference on
549+
* the returned task. The caller is responsible for releasing that reference.
550+
*
551+
* Currently, the process identified by @pidfd is always a thread-group leader.
552+
* This restriction currently exists for all aspects of pidfds including pidfd
553+
* creation (CLONE_PIDFD cannot be used with CLONE_THREAD) and pidfd polling
554+
* (only supports thread group leaders).
555+
*
556+
* Return: On success, the task_struct associated with the pidfd.
557+
* On error, a negative errno number will be returned.
558+
*/
559+
struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
560+
{
561+
unsigned int f_flags;
562+
struct pid *pid;
563+
struct task_struct *task;
564+
565+
pid = pidfd_get_pid(pidfd, &f_flags);
566+
if (IS_ERR(pid))
567+
return ERR_CAST(pid);
568+
569+
task = get_pid_task(pid, PIDTYPE_TGID);
570+
put_pid(pid);
571+
if (!task)
572+
return ERR_PTR(-ESRCH);
573+
574+
*flags = f_flags;
575+
return task;
576+
}
577+
542578
/**
543579
* pidfd_create() - Create a new pid file descriptor.
544580
*

0 commit comments

Comments
 (0)