Skip to content

Commit

Permalink
bpf: In bpf_task_fd_query use fget_task
Browse files Browse the repository at this point in the history
Use the helper fget_task to simplify bpf_task_fd_query.

As well as simplifying the code this removes one unnecessary increment of
struct files_struct.  This unnecessary increment of files_struct.count can
result in exec unnecessarily unsharing files_struct and breaking posix
locks, and it can result in fget_light having to fallback to fget reducing
performance.

This simplification comes from the observation that none of the
callers of get_files_struct actually need to call get_files_struct
that was made when discussing[1] exec and posix file locks.

[1] https://lkml.kernel.org/r/[email protected]
Suggested-by: Oleg Nesterov <[email protected]>
v1: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Eric W. Biederman <[email protected]>
  • Loading branch information
ebiederm committed Dec 10, 2020
1 parent f43c283 commit b48845a
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions kernel/bpf/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -3878,7 +3878,6 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
pid_t pid = attr->task_fd_query.pid;
u32 fd = attr->task_fd_query.fd;
const struct perf_event *event;
struct files_struct *files;
struct task_struct *task;
struct file *file;
int err;
Expand All @@ -3896,23 +3895,11 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
if (!task)
return -ENOENT;

files = get_files_struct(task);
put_task_struct(task);
if (!files)
return -ENOENT;

err = 0;
spin_lock(&files->file_lock);
file = fcheck_files(files, fd);
file = fget_task(task, fd);
put_task_struct(task);
if (!file)
err = -EBADF;
else
get_file(file);
spin_unlock(&files->file_lock);
put_files_struct(files);

if (err)
goto out;
return -EBADF;

if (file->f_op == &bpf_link_fops) {
struct bpf_link *link = file->private_data;
Expand Down Expand Up @@ -3952,7 +3939,6 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
err = -ENOTSUPP;
put_file:
fput(file);
out:
return err;
}

Expand Down

0 comments on commit b48845a

Please sign in to comment.