diff mbox series

[bpf-next,1/2] bpf: enable the "open" operator on a pinned path of a struct_osp link.

Message ID 20240417002513.1534535-2-thinker.li@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Update a struct_ops link through a pinned path | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7516 this patch: 7516
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 9 maintainers not CCed: yonghong.song@linux.dev john.fastabend@gmail.com netdev@vger.kernel.org sdf@google.com kpsingh@kernel.org jolsa@kernel.org daniel@iogearbox.net haoluo@google.com eddyz87@gmail.com
netdev/build_clang success Errors and warnings before: 1228 this patch: 1228
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 7898 this patch: 7898
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 62 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 6 this patch: 6
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc

Commit Message

Kui-Feng Lee April 17, 2024, 12:25 a.m. UTC
Add the "open" operator for the inodes of BPF links to allow applications
to obtain a file descriptor of a struct_ops link from a pinned path.

Applications have the ability to update a struct_ops link with another
struct_ops map. However, they were unable to open pinned paths of the links
with this patch. This implies that updating a link through its pinned paths
was not feasible.

This patch adds the "open" operator to bpf_link_ops and uses bpf_link_ops
as the i_fop for inodes of struct_ops links. "open" will be called to open
the pinned path represented by an inode. Additionally, bpf_link_ops will be
used as the f->f_ops of the opened "file" to provide operators for the
"file".

Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
 include/linux/bpf.h         |  4 ++++
 kernel/bpf/bpf_struct_ops.c | 10 ++++++++++
 kernel/bpf/inode.c          | 11 ++++++++---
 kernel/bpf/syscall.c        | 14 +++++++++++++-
 4 files changed, 35 insertions(+), 4 deletions(-)

Comments

kernel test robot April 17, 2024, 11:19 p.m. UTC | #1
Hi Kui-Feng,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Kui-Feng-Lee/bpf-enable-the-open-operator-on-a-pinned-path-of-a-struct_osp-link/20240417-082736
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20240417002513.1534535-2-thinker.li%40gmail.com
patch subject: [PATCH bpf-next 1/2] bpf: enable the "open" operator on a pinned path of a struct_osp link.
config: riscv-defconfig (https://download.01.org/0day-ci/archive/20240418/202404180747.NXWUtZTG-lkp@intel.com/config)
compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project 7089c359a3845323f6f30c44a47dd901f2edfe63)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240418/202404180747.NXWUtZTG-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202404180747.NXWUtZTG-lkp@intel.com/

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: bpffs_struct_ops_link_open
   >>> referenced by syscall.c
   >>>               kernel/bpf/syscall.o:(bpf_link_open) in archive vmlinux.a
kernel test robot April 18, 2024, 6:13 a.m. UTC | #2
Hi Kui-Feng,

kernel test robot noticed the following build errors:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Kui-Feng-Lee/bpf-enable-the-open-operator-on-a-pinned-path-of-a-struct_osp-link/20240417-082736
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20240417002513.1534535-2-thinker.li%40gmail.com
patch subject: [PATCH bpf-next 1/2] bpf: enable the "open" operator on a pinned path of a struct_osp link.
config: arm-aspeed_g5_defconfig (https://download.01.org/0day-ci/archive/20240418/202404181413.1uMDy1xi-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240418/202404181413.1uMDy1xi-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202404181413.1uMDy1xi-lkp@intel.com/

All errors (new ones prefixed by >>):

   arm-linux-gnueabi-ld: kernel/bpf/syscall.o: in function `bpf_link_open':
>> kernel/bpf/syscall.c:3117:(.text+0xb70): undefined reference to `bpffs_struct_ops_link_open'


vim +3117 kernel/bpf/syscall.c

  3110	
  3111	/* Support opening pinned links */
  3112	static int bpf_link_open(struct inode *inode, struct file *filp)
  3113	{
  3114		struct bpf_link *link = inode->i_private;
  3115	
  3116		if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
> 3117			return bpffs_struct_ops_link_open(inode, filp);
  3118	
  3119		return -EOPNOTSUPP;
  3120	}
  3121
Martin KaFai Lau April 20, 2024, 12:05 a.m. UTC | #3
On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
> +{
> +	struct bpf_struct_ops_link *link = inode->i_private;
> +
> +	/* Paired with bpf_link_put_direct() in bpf_link_release(). */
> +	bpf_link_inc(&link->link);
> +	filp->private_data = link;
> +	return 0;
> +}
> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
> index af5d2ffadd70..b020d761ab0a 100644
> --- a/kernel/bpf/inode.c
> +++ b/kernel/bpf/inode.c
> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
>   
>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
>   {
> +	const struct file_operations *fops;
>   	struct bpf_link *link = arg;
>   
> -	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
> -			     bpf_link_is_iter(link) ?
> -			     &bpf_iter_fops : &bpffs_obj_fops);
> +	if (bpf_link_is_iter(link))
> +		fops = &bpf_iter_fops;
> +	else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)

Open a pinned link and then update should not be specific to struct_ops link. 
e.g. should be useful to the cgroup link also?

Andrii, wdyt about supporting other link types also?

> +		fops = &bpf_link_fops;
> +	else
> +		fops = &bpffs_obj_fops;
> +	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops, fops);
>   }
>   
>   static struct dentry *
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index 7d392ec83655..f66bc6215faa 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3108,7 +3108,19 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
>   }
>   #endif
>   
> -static const struct file_operations bpf_link_fops = {
> +/* Support opening pinned links */
> +static int bpf_link_open(struct inode *inode, struct file *filp)
> +{
> +	struct bpf_link *link = inode->i_private;
> +
> +	if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
> +		return bpffs_struct_ops_link_open(inode, filp);
> +
> +	return -EOPNOTSUPP;
> +}
> +
> +const struct file_operations bpf_link_fops = {
> +	.open = bpf_link_open,
>   #ifdef CONFIG_PROC_FS
>   	.show_fdinfo	= bpf_link_show_fdinfo,
>   #endif
Kui-Feng Lee April 22, 2024, 5:12 p.m. UTC | #4
On 4/19/24 17:05, Martin KaFai Lau wrote:
> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
>> +{
>> +    struct bpf_struct_ops_link *link = inode->i_private;
>> +
>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
>> +    bpf_link_inc(&link->link);
>> +    filp->private_data = link;
>> +    return 0;
>> +}
>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
>> index af5d2ffadd70..b020d761ab0a 100644
>> --- a/kernel/bpf/inode.c
>> +++ b/kernel/bpf/inode.c
>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, 
>> umode_t mode, void *arg)
>>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
>>   {
>> +    const struct file_operations *fops;
>>       struct bpf_link *link = arg;
>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
>> -                 bpf_link_is_iter(link) ?
>> -                 &bpf_iter_fops : &bpffs_obj_fops);
>> +    if (bpf_link_is_iter(link))
>> +        fops = &bpf_iter_fops;
>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
> 
> Open a pinned link and then update should not be specific to struct_ops 
> link. e.g. should be useful to the cgroup link also?

It could be. Here, I played safe in case it creates any unwanted side
effect for links of unknown types.

> 
> Andrii, wdyt about supporting other link types also?
> 
>> +        fops = &bpf_link_fops;
>> +    else
>> +        fops = &bpffs_obj_fops;
>> +    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops, fops);
>>   }
>>   static struct dentry *
>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>> index 7d392ec83655..f66bc6215faa 100644
>> --- a/kernel/bpf/syscall.c
>> +++ b/kernel/bpf/syscall.c
>> @@ -3108,7 +3108,19 @@ static void bpf_link_show_fdinfo(struct 
>> seq_file *m, struct file *filp)
>>   }
>>   #endif
>> -static const struct file_operations bpf_link_fops = {
>> +/* Support opening pinned links */
>> +static int bpf_link_open(struct inode *inode, struct file *filp)
>> +{
>> +    struct bpf_link *link = inode->i_private;
>> +
>> +    if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>> +        return bpffs_struct_ops_link_open(inode, filp);
>> +
>> +    return -EOPNOTSUPP;
>> +}
>> +
>> +const struct file_operations bpf_link_fops = {
>> +    .open = bpf_link_open,
>>   #ifdef CONFIG_PROC_FS
>>       .show_fdinfo    = bpf_link_show_fdinfo,
>>   #endif
>
Kui-Feng Lee April 22, 2024, 5:30 p.m. UTC | #5
On 4/22/24 10:12, Kui-Feng Lee wrote:
> 
> 
> On 4/19/24 17:05, Martin KaFai Lau wrote:
>> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
>>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
>>> +{
>>> +    struct bpf_struct_ops_link *link = inode->i_private;
>>> +
>>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
>>> +    bpf_link_inc(&link->link);
>>> +    filp->private_data = link;
>>> +    return 0;
>>> +}
>>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
>>> index af5d2ffadd70..b020d761ab0a 100644
>>> --- a/kernel/bpf/inode.c
>>> +++ b/kernel/bpf/inode.c
>>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, 
>>> umode_t mode, void *arg)
>>>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
>>>   {
>>> +    const struct file_operations *fops;
>>>       struct bpf_link *link = arg;
>>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
>>> -                 bpf_link_is_iter(link) ?
>>> -                 &bpf_iter_fops : &bpffs_obj_fops);
>>> +    if (bpf_link_is_iter(link))
>>> +        fops = &bpf_iter_fops;
>>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>>
>> Open a pinned link and then update should not be specific to 
>> struct_ops link. e.g. should be useful to the cgroup link also?
> 
> It could be. Here, I played safe in case it creates any unwanted side
> effect for links of unknown types.

By the way, may I put it in a follow up patch if we want cgroup links?

> 
>>
>> Andrii, wdyt about supporting other link types also?
>>
>>> +        fops = &bpf_link_fops;
>>> +    else
>>> +        fops = &bpffs_obj_fops;
>>> +    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops, fops);
>>>   }
>>>   static struct dentry *
>>> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>>> index 7d392ec83655..f66bc6215faa 100644
>>> --- a/kernel/bpf/syscall.c
>>> +++ b/kernel/bpf/syscall.c
>>> @@ -3108,7 +3108,19 @@ static void bpf_link_show_fdinfo(struct 
>>> seq_file *m, struct file *filp)
>>>   }
>>>   #endif
>>> -static const struct file_operations bpf_link_fops = {
>>> +/* Support opening pinned links */
>>> +static int bpf_link_open(struct inode *inode, struct file *filp)
>>> +{
>>> +    struct bpf_link *link = inode->i_private;
>>> +
>>> +    if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>>> +        return bpffs_struct_ops_link_open(inode, filp);
>>> +
>>> +    return -EOPNOTSUPP;
>>> +}
>>> +
>>> +const struct file_operations bpf_link_fops = {
>>> +    .open = bpf_link_open,
>>>   #ifdef CONFIG_PROC_FS
>>>       .show_fdinfo    = bpf_link_show_fdinfo,
>>>   #endif
>>
Martin KaFai Lau April 22, 2024, 11:43 p.m. UTC | #6
On 4/22/24 10:30 AM, Kui-Feng Lee wrote:
> 
> 
> On 4/22/24 10:12, Kui-Feng Lee wrote:
>>
>>
>> On 4/19/24 17:05, Martin KaFai Lau wrote:
>>> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
>>>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
>>>> +{
>>>> +    struct bpf_struct_ops_link *link = inode->i_private;
>>>> +
>>>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
>>>> +    bpf_link_inc(&link->link);
>>>> +    filp->private_data = link;
>>>> +    return 0;
>>>> +}
>>>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
>>>> index af5d2ffadd70..b020d761ab0a 100644
>>>> --- a/kernel/bpf/inode.c
>>>> +++ b/kernel/bpf/inode.c
>>>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, umode_t 
>>>> mode, void *arg)
>>>>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
>>>>   {
>>>> +    const struct file_operations *fops;
>>>>       struct bpf_link *link = arg;
>>>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
>>>> -                 bpf_link_is_iter(link) ?
>>>> -                 &bpf_iter_fops : &bpffs_obj_fops);
>>>> +    if (bpf_link_is_iter(link))
>>>> +        fops = &bpf_iter_fops;
>>>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>>>
>>> Open a pinned link and then update should not be specific to struct_ops link. 
>>> e.g. should be useful to the cgroup link also?
>>
>> It could be. Here, I played safe in case it creates any unwanted side
>> effect for links of unknown types.
> 
> By the way, may I put it in a follow up patch if we want cgroup links?

This does not feel right. It is not struct_ops specific.

Before we dive in further, there is BPF_OBJ_GET which can get a fd of a pinned 
bpf obj (prog, map, and link). Take a look at bpf_link__open() in libbpf. Does 
it work for the use case that needs to update the link?
Kui-Feng Lee April 23, 2024, 5:16 p.m. UTC | #7
On 4/22/24 16:43, Martin KaFai Lau wrote:
> On 4/22/24 10:30 AM, Kui-Feng Lee wrote:
>>
>>
>> On 4/22/24 10:12, Kui-Feng Lee wrote:
>>>
>>>
>>> On 4/19/24 17:05, Martin KaFai Lau wrote:
>>>> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
>>>>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file 
>>>>> *filp)
>>>>> +{
>>>>> +    struct bpf_struct_ops_link *link = inode->i_private;
>>>>> +
>>>>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
>>>>> +    bpf_link_inc(&link->link);
>>>>> +    filp->private_data = link;
>>>>> +    return 0;
>>>>> +}
>>>>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
>>>>> index af5d2ffadd70..b020d761ab0a 100644
>>>>> --- a/kernel/bpf/inode.c
>>>>> +++ b/kernel/bpf/inode.c
>>>>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, 
>>>>> umode_t mode, void *arg)
>>>>>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void 
>>>>> *arg)
>>>>>   {
>>>>> +    const struct file_operations *fops;
>>>>>       struct bpf_link *link = arg;
>>>>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
>>>>> -                 bpf_link_is_iter(link) ?
>>>>> -                 &bpf_iter_fops : &bpffs_obj_fops);
>>>>> +    if (bpf_link_is_iter(link))
>>>>> +        fops = &bpf_iter_fops;
>>>>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>>>>
>>>> Open a pinned link and then update should not be specific to 
>>>> struct_ops link. e.g. should be useful to the cgroup link also?
>>>
>>> It could be. Here, I played safe in case it creates any unwanted side
>>> effect for links of unknown types.
>>
>> By the way, may I put it in a follow up patch if we want cgroup links?
> 
> This does not feel right. It is not struct_ops specific.
> 
> Before we dive in further, there is BPF_OBJ_GET which can get a fd of a 
> pinned bpf obj (prog, map, and link). Take a look at bpf_link__open() in 
> libbpf. Does it work for the use case that needs to update the link?
> 
It should work.
So, this patch is not necessary. However, it is still a nice and
intuitive feature. WDYT?
Martin KaFai Lau April 24, 2024, 12:29 a.m. UTC | #8
On 4/23/24 10:16 AM, Kui-Feng Lee wrote:
> 
> 
> On 4/22/24 16:43, Martin KaFai Lau wrote:
>> On 4/22/24 10:30 AM, Kui-Feng Lee wrote:
>>>
>>>
>>> On 4/22/24 10:12, Kui-Feng Lee wrote:
>>>>
>>>>
>>>> On 4/19/24 17:05, Martin KaFai Lau wrote:
>>>>> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
>>>>>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
>>>>>> +{
>>>>>> +    struct bpf_struct_ops_link *link = inode->i_private;
>>>>>> +
>>>>>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
>>>>>> +    bpf_link_inc(&link->link);
>>>>>> +    filp->private_data = link;
>>>>>> +    return 0;
>>>>>> +}
>>>>>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
>>>>>> index af5d2ffadd70..b020d761ab0a 100644
>>>>>> --- a/kernel/bpf/inode.c
>>>>>> +++ b/kernel/bpf/inode.c
>>>>>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, umode_t 
>>>>>> mode, void *arg)
>>>>>>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
>>>>>>   {
>>>>>> +    const struct file_operations *fops;
>>>>>>       struct bpf_link *link = arg;
>>>>>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
>>>>>> -                 bpf_link_is_iter(link) ?
>>>>>> -                 &bpf_iter_fops : &bpffs_obj_fops);
>>>>>> +    if (bpf_link_is_iter(link))
>>>>>> +        fops = &bpf_iter_fops;
>>>>>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>>>>>
>>>>> Open a pinned link and then update should not be specific to struct_ops 
>>>>> link. e.g. should be useful to the cgroup link also?
>>>>
>>>> It could be. Here, I played safe in case it creates any unwanted side
>>>> effect for links of unknown types.
>>>
>>> By the way, may I put it in a follow up patch if we want cgroup links?
>>
>> This does not feel right. It is not struct_ops specific.
>>
>> Before we dive in further, there is BPF_OBJ_GET which can get a fd of a pinned 
>> bpf obj (prog, map, and link). Take a look at bpf_link__open() in libbpf. Does 
>> it work for the use case that needs to update the link?
>>
> It should work.
> So, this patch is not necessary. However, it is still a nice and
> intuitive feature. WDYT?

There is already BPF_OBJ_GET which works for all major bpf obj types (prog, map, 
and link). Having open only works for link and only works for one link type 
(struct_ops) is not very convincing.

Beside, I am not sure how the file flags (e.g. rdonly...etc) should be handled. 
I don't know enough in this area, so I will defer to others to comment in 
general the usefulness and the approach.
Andrii Nakryiko April 25, 2024, 12:17 a.m. UTC | #9
On Tue, Apr 23, 2024 at 5:29 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>
> On 4/23/24 10:16 AM, Kui-Feng Lee wrote:
> >
> >
> > On 4/22/24 16:43, Martin KaFai Lau wrote:
> >> On 4/22/24 10:30 AM, Kui-Feng Lee wrote:
> >>>
> >>>
> >>> On 4/22/24 10:12, Kui-Feng Lee wrote:
> >>>>
> >>>>
> >>>> On 4/19/24 17:05, Martin KaFai Lau wrote:
> >>>>> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
> >>>>>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
> >>>>>> +{
> >>>>>> +    struct bpf_struct_ops_link *link = inode->i_private;
> >>>>>> +
> >>>>>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
> >>>>>> +    bpf_link_inc(&link->link);
> >>>>>> +    filp->private_data = link;
> >>>>>> +    return 0;
> >>>>>> +}
> >>>>>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
> >>>>>> index af5d2ffadd70..b020d761ab0a 100644
> >>>>>> --- a/kernel/bpf/inode.c
> >>>>>> +++ b/kernel/bpf/inode.c
> >>>>>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, umode_t
> >>>>>> mode, void *arg)
> >>>>>>   static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
> >>>>>>   {
> >>>>>> +    const struct file_operations *fops;
> >>>>>>       struct bpf_link *link = arg;
> >>>>>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
> >>>>>> -                 bpf_link_is_iter(link) ?
> >>>>>> -                 &bpf_iter_fops : &bpffs_obj_fops);
> >>>>>> +    if (bpf_link_is_iter(link))
> >>>>>> +        fops = &bpf_iter_fops;
> >>>>>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
> >>>>>
> >>>>> Open a pinned link and then update should not be specific to struct_ops
> >>>>> link. e.g. should be useful to the cgroup link also?
> >>>>
> >>>> It could be. Here, I played safe in case it creates any unwanted side
> >>>> effect for links of unknown types.
> >>>
> >>> By the way, may I put it in a follow up patch if we want cgroup links?
> >>
> >> This does not feel right. It is not struct_ops specific.
> >>
> >> Before we dive in further, there is BPF_OBJ_GET which can get a fd of a pinned
> >> bpf obj (prog, map, and link). Take a look at bpf_link__open() in libbpf. Does
> >> it work for the use case that needs to update the link?
> >>
> > It should work.
> > So, this patch is not necessary. However, it is still a nice and
> > intuitive feature. WDYT?
>
> There is already BPF_OBJ_GET which works for all major bpf obj types (prog, map,
> and link). Having open only works for link and only works for one link type
> (struct_ops) is not very convincing.
>
> Beside, I am not sure how the file flags (e.g. rdonly...etc) should be handled.
> I don't know enough in this area, so I will defer to others to comment in
> general the usefulness and the approach.
>
>

Didn't see this discussion before replying on the other patch. But I
agree with Martin, we already use the BPF_OBJ_GET method, and we don't
support directly open()'ing progs/maps, so I don't think we should do
this for links either.

pw-bot: cr
Kui-Feng Lee April 25, 2024, 5:11 p.m. UTC | #10
On 4/24/24 17:17, Andrii Nakryiko wrote:
> On Tue, Apr 23, 2024 at 5:29 PM Martin KaFai Lau <martin.lau@linux.dev> wrote:
>>
>> On 4/23/24 10:16 AM, Kui-Feng Lee wrote:
>>>
>>>
>>> On 4/22/24 16:43, Martin KaFai Lau wrote:
>>>> On 4/22/24 10:30 AM, Kui-Feng Lee wrote:
>>>>>
>>>>>
>>>>> On 4/22/24 10:12, Kui-Feng Lee wrote:
>>>>>>
>>>>>>
>>>>>> On 4/19/24 17:05, Martin KaFai Lau wrote:
>>>>>>> On 4/16/24 5:25 PM, Kui-Feng Lee wrote:
>>>>>>>> +int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
>>>>>>>> +{
>>>>>>>> +    struct bpf_struct_ops_link *link = inode->i_private;
>>>>>>>> +
>>>>>>>> +    /* Paired with bpf_link_put_direct() in bpf_link_release(). */
>>>>>>>> +    bpf_link_inc(&link->link);
>>>>>>>> +    filp->private_data = link;
>>>>>>>> +    return 0;
>>>>>>>> +}
>>>>>>>> diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
>>>>>>>> index af5d2ffadd70..b020d761ab0a 100644
>>>>>>>> --- a/kernel/bpf/inode.c
>>>>>>>> +++ b/kernel/bpf/inode.c
>>>>>>>> @@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, umode_t
>>>>>>>> mode, void *arg)
>>>>>>>>    static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
>>>>>>>>    {
>>>>>>>> +    const struct file_operations *fops;
>>>>>>>>        struct bpf_link *link = arg;
>>>>>>>> -    return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
>>>>>>>> -                 bpf_link_is_iter(link) ?
>>>>>>>> -                 &bpf_iter_fops : &bpffs_obj_fops);
>>>>>>>> +    if (bpf_link_is_iter(link))
>>>>>>>> +        fops = &bpf_iter_fops;
>>>>>>>> +    else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
>>>>>>>
>>>>>>> Open a pinned link and then update should not be specific to struct_ops
>>>>>>> link. e.g. should be useful to the cgroup link also?
>>>>>>
>>>>>> It could be. Here, I played safe in case it creates any unwanted side
>>>>>> effect for links of unknown types.
>>>>>
>>>>> By the way, may I put it in a follow up patch if we want cgroup links?
>>>>
>>>> This does not feel right. It is not struct_ops specific.
>>>>
>>>> Before we dive in further, there is BPF_OBJ_GET which can get a fd of a pinned
>>>> bpf obj (prog, map, and link). Take a look at bpf_link__open() in libbpf. Does
>>>> it work for the use case that needs to update the link?
>>>>
>>> It should work.
>>> So, this patch is not necessary. However, it is still a nice and
>>> intuitive feature. WDYT?
>>
>> There is already BPF_OBJ_GET which works for all major bpf obj types (prog, map,
>> and link). Having open only works for link and only works for one link type
>> (struct_ops) is not very convincing.
>>
>> Beside, I am not sure how the file flags (e.g. rdonly...etc) should be handled.
>> I don't know enough in this area, so I will defer to others to comment in
>> general the usefulness and the approach.
>>
>>
> 
> Didn't see this discussion before replying on the other patch. But I
> agree with Martin, we already use the BPF_OBJ_GET method, and we don't
> support directly open()'ing progs/maps, so I don't think we should do
> this for links either.
> 
> pw-bot: cr

Understand! Let's drop this patch.
diff mbox series

Patch

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 5034c1b4ded7..23b831ec9b71 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2160,6 +2160,10 @@  extern const struct super_operations bpf_super_ops;
 extern const struct file_operations bpf_map_fops;
 extern const struct file_operations bpf_prog_fops;
 extern const struct file_operations bpf_iter_fops;
+extern const struct file_operations bpf_link_fops;
+
+/* Required by bpf_link_open() */
+int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp);
 
 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
 	extern const struct bpf_prog_ops _name ## _prog_ops; \
diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
index 86c7884abaf8..8be4f755a182 100644
--- a/kernel/bpf/bpf_struct_ops.c
+++ b/kernel/bpf/bpf_struct_ops.c
@@ -1198,3 +1198,13 @@  void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map
 
 	info->btf_vmlinux_id = btf_obj_id(st_map->btf);
 }
+
+int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
+{
+	struct bpf_struct_ops_link *link = inode->i_private;
+
+	/* Paired with bpf_link_put_direct() in bpf_link_release(). */
+	bpf_link_inc(&link->link);
+	filp->private_data = link;
+	return 0;
+}
diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
index af5d2ffadd70..b020d761ab0a 100644
--- a/kernel/bpf/inode.c
+++ b/kernel/bpf/inode.c
@@ -360,11 +360,16 @@  static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
 
 static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
 {
+	const struct file_operations *fops;
 	struct bpf_link *link = arg;
 
-	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
-			     bpf_link_is_iter(link) ?
-			     &bpf_iter_fops : &bpffs_obj_fops);
+	if (bpf_link_is_iter(link))
+		fops = &bpf_iter_fops;
+	else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
+		fops = &bpf_link_fops;
+	else
+		fops = &bpffs_obj_fops;
+	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops, fops);
 }
 
 static struct dentry *
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 7d392ec83655..f66bc6215faa 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3108,7 +3108,19 @@  static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
 }
 #endif
 
-static const struct file_operations bpf_link_fops = {
+/* Support opening pinned links */
+static int bpf_link_open(struct inode *inode, struct file *filp)
+{
+	struct bpf_link *link = inode->i_private;
+
+	if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
+		return bpffs_struct_ops_link_open(inode, filp);
+
+	return -EOPNOTSUPP;
+}
+
+const struct file_operations bpf_link_fops = {
+	.open = bpf_link_open,
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo	= bpf_link_show_fdinfo,
 #endif