Message ID | AM6PR03MB5080CD2C2C0EFC01082EC582993D2@AM6PR03MB5080.eurprd03.prod.outlook.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | bpf: Add open-coded style process file iterator and bpf_fget_task() kfunc | expand |
On Tue, Dec 10, 2024 at 02:03:52PM +0000, Juntong Deng wrote: > This patch adds bpf_fget_task() kfunc. > > bpf_fget_task() is used to get a pointer to the struct file > corresponding to the task file descriptor. Note that this function > acquires a reference to struct file. > > Signed-off-by: Juntong Deng <juntong.deng@outlook.com> > --- > fs/bpf_fs_kfuncs.c | 21 +++++++++++++++++++++ > 1 file changed, 21 insertions(+) > > diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c > index 3fe9f59ef867..19a9d45c47f9 100644 > --- a/fs/bpf_fs_kfuncs.c > +++ b/fs/bpf_fs_kfuncs.c > @@ -152,6 +152,26 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, > return bpf_get_dentry_xattr(dentry, name__str, value_p); > } > > +/** > + * bpf_fget_task() - Get a pointer to the struct file corresponding to > + * the task file descriptor > + * > + * Note that this function acquires a reference to struct file. > + * > + * @task: the specified struct task_struct > + * @fd: the file descriptor > + * > + * @returns the corresponding struct file pointer if found, > + * otherwise returns NULL > + */ > +__bpf_kfunc struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) > +{ > + struct file *file; > + > + file = fget_task(task, fd); > + return file; Why the local variable?
On 2024/12/10 14:28, Christian Brauner wrote: > On Tue, Dec 10, 2024 at 02:03:52PM +0000, Juntong Deng wrote: >> This patch adds bpf_fget_task() kfunc. >> >> bpf_fget_task() is used to get a pointer to the struct file >> corresponding to the task file descriptor. Note that this function >> acquires a reference to struct file. >> >> Signed-off-by: Juntong Deng <juntong.deng@outlook.com> >> --- >> fs/bpf_fs_kfuncs.c | 21 +++++++++++++++++++++ >> 1 file changed, 21 insertions(+) >> >> diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c >> index 3fe9f59ef867..19a9d45c47f9 100644 >> --- a/fs/bpf_fs_kfuncs.c >> +++ b/fs/bpf_fs_kfuncs.c >> @@ -152,6 +152,26 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, >> return bpf_get_dentry_xattr(dentry, name__str, value_p); >> } >> >> +/** >> + * bpf_fget_task() - Get a pointer to the struct file corresponding to >> + * the task file descriptor >> + * >> + * Note that this function acquires a reference to struct file. >> + * >> + * @task: the specified struct task_struct >> + * @fd: the file descriptor >> + * >> + * @returns the corresponding struct file pointer if found, >> + * otherwise returns NULL >> + */ >> +__bpf_kfunc struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) >> +{ >> + struct file *file; >> + >> + file = fget_task(task, fd); >> + return file; > > Why the local variable? Thanks for your reply. Uh, I forgot why I wrote it like that. Yes, I agree that it is redundant. I will remove it in the next version.
diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 3fe9f59ef867..19a9d45c47f9 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -152,6 +152,26 @@ __bpf_kfunc int bpf_get_file_xattr(struct file *file, const char *name__str, return bpf_get_dentry_xattr(dentry, name__str, value_p); } +/** + * bpf_fget_task() - Get a pointer to the struct file corresponding to + * the task file descriptor + * + * Note that this function acquires a reference to struct file. + * + * @task: the specified struct task_struct + * @fd: the file descriptor + * + * @returns the corresponding struct file pointer if found, + * otherwise returns NULL + */ +__bpf_kfunc struct file *bpf_fget_task(struct task_struct *task, unsigned int fd) +{ + struct file *file; + + file = fget_task(task, fd); + return file; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_fs_kfunc_set_ids) @@ -161,6 +181,7 @@ BTF_ID_FLAGS(func, bpf_put_file, KF_RELEASE) BTF_ID_FLAGS(func, bpf_path_d_path, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_fget_task, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) BTF_KFUNCS_END(bpf_fs_kfunc_set_ids) static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
This patch adds bpf_fget_task() kfunc. bpf_fget_task() is used to get a pointer to the struct file corresponding to the task file descriptor. Note that this function acquires a reference to struct file. Signed-off-by: Juntong Deng <juntong.deng@outlook.com> --- fs/bpf_fs_kfuncs.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)