diff mbox series

[2/7] udp: seq_file: Remove bpf_seq_afinfo from udp_iter_state

Message ID 20230418153148.2231644-3-aditi.ghag@isovalent.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Add socket destroy capability | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
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: 130 this patch: 130
netdev/cc_maintainers warning 6 maintainers not CCed: pabeni@redhat.com dsahern@kernel.org willemdebruijn.kernel@gmail.com kuba@kernel.org netdev@vger.kernel.org davem@davemloft.net
netdev/build_clang success Errors and warnings before: 22 this patch: 22
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: 189 this patch: 189
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 69 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-36 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for set-matrix

Commit Message

Aditi Ghag April 18, 2023, 3:31 p.m. UTC
This is a preparatory commit to remove the field. The field was
previously shared between proc fs and BPF UDP socket iterators. As the
follow-up commits will decouple the implementation for the iterators,
remove the field. As for BPF socket iterator, filtering of sockets is
exepected to be done in BPF programs.

Suggested-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Aditi Ghag <aditi.ghag@isovalent.com>
---
 include/net/udp.h |  1 -
 net/ipv4/udp.c    | 34 ++++------------------------------
 2 files changed, 4 insertions(+), 31 deletions(-)

Comments

Martin KaFai Lau April 24, 2023, 12:18 a.m. UTC | #1
On 4/18/23 8:31 AM, Aditi Ghag wrote:
> This is a preparatory commit to remove the field. The field was
> previously shared between proc fs and BPF UDP socket iterators. As the
> follow-up commits will decouple the implementation for the iterators,
> remove the field. As for BPF socket iterator, filtering of sockets is
> exepected to be done in BPF programs.
> 
> Suggested-by: Martin KaFai Lau <martin.lau@kernel.org>
> Signed-off-by: Aditi Ghag <aditi.ghag@isovalent.com>
> ---
>   include/net/udp.h |  1 -
>   net/ipv4/udp.c    | 34 ++++------------------------------
>   2 files changed, 4 insertions(+), 31 deletions(-)
> 
> diff --git a/include/net/udp.h b/include/net/udp.h
> index de4b528522bb..5cad44318d71 100644
> --- a/include/net/udp.h
> +++ b/include/net/udp.h
> @@ -437,7 +437,6 @@ struct udp_seq_afinfo {
>   struct udp_iter_state {
>   	struct seq_net_private  p;
>   	int			bucket;
> -	struct udp_seq_afinfo	*bpf_seq_afinfo;
>   };
>   
>   void *udp_seq_start(struct seq_file *seq, loff_t *pos);
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index c605d171eb2d..3c9eeee28678 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -2997,10 +2997,7 @@ static struct sock *udp_get_first(struct seq_file *seq, int start)
>   	struct udp_table *udptable;
>   	struct sock *sk;
>   
> -	if (state->bpf_seq_afinfo)
> -		afinfo = state->bpf_seq_afinfo;
> -	else
> -		afinfo = pde_data(file_inode(seq->file));
> +	afinfo = pde_data(file_inode(seq->file));

I can see how this change will work after patch 4. However, this patch alone 
cannot work independently as is. The udp bpf iter still uses the 
udp_get_{first,next} and udp_seq_stop() up-to this patch.

First, patch 3 refactoring should be done before patch 2 here. The removal of 
'struct udp_seq_afinfo *bpf_seq_afinfo' in patch 2 should be done when all the 
necessary refactoring is in-place first.

Also, this afinfo is passed to udp_get_table_afinfo(). How about renaming 
udp_get_table_afinfo() to udp_get_table_seq() and having it take the "seq" as 
the arg instead. This probably will deserve another refactoring patch before 
finally removing bpf_seq_afinfo. Something like this (un-compiled code):

static struct udp_table *udp_get_table_seq(struct seq_file *seq,
                                            struct net *net)
{
  	const struct udp_seq_afinfo *afinfo;

         if (st->bpf_seq_afinfo)
                 return net->ipv4.udp_table;

	afinfo = pde_data(file_inode(seq->file));
         return afinfo->udp_table ? : net->ipv4.udp_table;
}

Of course, when the later patch finally removes the bpf_seq_afinfo, the 'if 
(st->bpf_seq_afinfo)' test should be replaced with the 'if (seq->op == 
&bpf_iter_udp_seq_ops)' test.

That will also make the afinfo dance in bpf_iter_udp_batch() in patch 4 goes away.

>   
>   	udptable = udp_get_table_afinfo(afinfo, net);
>   
> @@ -3033,10 +3030,7 @@ static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
>   	struct udp_seq_afinfo *afinfo;
>   	struct udp_table *udptable;
>   
> -	if (state->bpf_seq_afinfo)
> -		afinfo = state->bpf_seq_afinfo;
> -	else
> -		afinfo = pde_data(file_inode(seq->file));
> +	afinfo = pde_data(file_inode(seq->file));
>   
>   	do {
>   		sk = sk_next(sk);
> @@ -3094,10 +3088,7 @@ void udp_seq_stop(struct seq_file *seq, void *v)
>   	struct udp_seq_afinfo *afinfo;
>   	struct udp_table *udptable;
>   
> -	if (state->bpf_seq_afinfo)
> -		afinfo = state->bpf_seq_afinfo;
> -	else
> -		afinfo = pde_data(file_inode(seq->file));
> +	afinfo = pde_data(file_inode(seq->file));
>   
>   	udptable = udp_get_table_afinfo(afinfo, seq_file_net(seq));
>   
> @@ -3415,28 +3406,11 @@ DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta,
>   
>   static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux)
>   {
> -	struct udp_iter_state *st = priv_data;
> -	struct udp_seq_afinfo *afinfo;
> -	int ret;
> -
> -	afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN);
> -	if (!afinfo)
> -		return -ENOMEM;
> -
> -	afinfo->family = AF_UNSPEC;
> -	afinfo->udp_table = NULL;
> -	st->bpf_seq_afinfo = afinfo;
> -	ret = bpf_iter_init_seq_net(priv_data, aux);
> -	if (ret)
> -		kfree(afinfo);
> -	return ret;
> +	return bpf_iter_init_seq_net(priv_data, aux);

Nice simplification with the bpf_seq_afinfo cleanup.

>   }
>   
>   static void bpf_iter_fini_udp(void *priv_data)
>   {
> -	struct udp_iter_state *st = priv_data;
> -
> -	kfree(st->bpf_seq_afinfo);
>   	bpf_iter_fini_seq_net(priv_data);
>   }
>
Aditi Ghag May 1, 2023, 10:39 p.m. UTC | #2
> On Apr 23, 2023, at 5:18 PM, Martin KaFai Lau <martin.lau@linux.dev> wrote:
> 
> On 4/18/23 8:31 AM, Aditi Ghag wrote:
>> This is a preparatory commit to remove the field. The field was
>> previously shared between proc fs and BPF UDP socket iterators. As the
>> follow-up commits will decouple the implementation for the iterators,
>> remove the field. As for BPF socket iterator, filtering of sockets is
>> exepected to be done in BPF programs.
>> Suggested-by: Martin KaFai Lau <martin.lau@kernel.org>
>> Signed-off-by: Aditi Ghag <aditi.ghag@isovalent.com>
>> ---
>>  include/net/udp.h |  1 -
>>  net/ipv4/udp.c    | 34 ++++------------------------------
>>  2 files changed, 4 insertions(+), 31 deletions(-)
>> diff --git a/include/net/udp.h b/include/net/udp.h
>> index de4b528522bb..5cad44318d71 100644
>> --- a/include/net/udp.h
>> +++ b/include/net/udp.h
>> @@ -437,7 +437,6 @@ struct udp_seq_afinfo {
>>  struct udp_iter_state {
>>  	struct seq_net_private  p;
>>  	int			bucket;
>> -	struct udp_seq_afinfo	*bpf_seq_afinfo;
>>  };
>>    void *udp_seq_start(struct seq_file *seq, loff_t *pos);
>> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
>> index c605d171eb2d..3c9eeee28678 100644
>> --- a/net/ipv4/udp.c
>> +++ b/net/ipv4/udp.c
>> @@ -2997,10 +2997,7 @@ static struct sock *udp_get_first(struct seq_file *seq, int start)
>>  	struct udp_table *udptable;
>>  	struct sock *sk;
>>  -	if (state->bpf_seq_afinfo)
>> -		afinfo = state->bpf_seq_afinfo;
>> -	else
>> -		afinfo = pde_data(file_inode(seq->file));
>> +	afinfo = pde_data(file_inode(seq->file));
> 
> I can see how this change will work after patch 4. However, this patch alone cannot work independently as is. The udp bpf iter still uses the udp_get_{first,next} and udp_seq_stop() up-to this patch.
> 
> First, patch 3 refactoring should be done before patch 2 here. The removal of 'struct udp_seq_afinfo *bpf_seq_afinfo' in patch 2 should be done when all the necessary refactoring is in-place first.
> 
> Also, this afinfo is passed to udp_get_table_afinfo(). How about renaming udp_get_table_afinfo() to udp_get_table_seq() and having it take the "seq" as the arg instead. This probably will deserve another refactoring patch before finally removing bpf_seq_afinfo. Something like this (un-compiled code):
> 
> static struct udp_table *udp_get_table_seq(struct seq_file *seq,
>                                           struct net *net)
> {
> 	const struct udp_seq_afinfo *afinfo;
> 
>        if (st->bpf_seq_afinfo)
>                return net->ipv4.udp_table;
> 
> 	afinfo = pde_data(file_inode(seq->file));
>        return afinfo->udp_table ? : net->ipv4.udp_table;
> }
> 
> Of course, when the later patch finally removes the bpf_seq_afinfo, the 'if (st->bpf_seq_afinfo)' test should be replaced with the 'if (seq->op == &bpf_iter_udp_seq_ops)' test.
> 
> That will also make the afinfo dance in bpf_iter_udp_batch() in patch 4 goes away.

Sweet! I suppose it was worth resolving a few conflicts while creating the new preparatory patch, especially since the refactoring simplified unnecessary setting of afinfo in bpf_iter_udp_batch(). The additional minor change that was needed was to forward declare bpf_iter_udp_seq_ops. And of course, the if (seq->op == &bpf_iter_udp_seq_ops) check needed to be wrapped in the CONFIG_BPF_SYSCALL ifdef.


> 
>>    	udptable = udp_get_table_afinfo(afinfo, net);
>>  @@ -3033,10 +3030,7 @@ static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
>>  	struct udp_seq_afinfo *afinfo;
>>  	struct udp_table *udptable;
>>  -	if (state->bpf_seq_afinfo)
>> -		afinfo = state->bpf_seq_afinfo;
>> -	else
>> -		afinfo = pde_data(file_inode(seq->file));
>> +	afinfo = pde_data(file_inode(seq->file));
>>    	do {
>>  		sk = sk_next(sk);
>> @@ -3094,10 +3088,7 @@ void udp_seq_stop(struct seq_file *seq, void *v)
>>  	struct udp_seq_afinfo *afinfo;
>>  	struct udp_table *udptable;
>>  -	if (state->bpf_seq_afinfo)
>> -		afinfo = state->bpf_seq_afinfo;
>> -	else
>> -		afinfo = pde_data(file_inode(seq->file));
>> +	afinfo = pde_data(file_inode(seq->file));
>>    	udptable = udp_get_table_afinfo(afinfo, seq_file_net(seq));
>>  @@ -3415,28 +3406,11 @@ DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta,
>>    static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux)
>>  {
>> -	struct udp_iter_state *st = priv_data;
>> -	struct udp_seq_afinfo *afinfo;
>> -	int ret;
>> -
>> -	afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN);
>> -	if (!afinfo)
>> -		return -ENOMEM;
>> -
>> -	afinfo->family = AF_UNSPEC;
>> -	afinfo->udp_table = NULL;
>> -	st->bpf_seq_afinfo = afinfo;
>> -	ret = bpf_iter_init_seq_net(priv_data, aux);
>> -	if (ret)
>> -		kfree(afinfo);
>> -	return ret;
>> +	return bpf_iter_init_seq_net(priv_data, aux);
> 
> Nice simplification with the bpf_seq_afinfo cleanup.
> 
>>  }
>>    static void bpf_iter_fini_udp(void *priv_data)
>>  {
>> -	struct udp_iter_state *st = priv_data;
>> -
>> -	kfree(st->bpf_seq_afinfo);
>>  	bpf_iter_fini_seq_net(priv_data);
>>  }
diff mbox series

Patch

diff --git a/include/net/udp.h b/include/net/udp.h
index de4b528522bb..5cad44318d71 100644
--- a/include/net/udp.h
+++ b/include/net/udp.h
@@ -437,7 +437,6 @@  struct udp_seq_afinfo {
 struct udp_iter_state {
 	struct seq_net_private  p;
 	int			bucket;
-	struct udp_seq_afinfo	*bpf_seq_afinfo;
 };
 
 void *udp_seq_start(struct seq_file *seq, loff_t *pos);
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index c605d171eb2d..3c9eeee28678 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2997,10 +2997,7 @@  static struct sock *udp_get_first(struct seq_file *seq, int start)
 	struct udp_table *udptable;
 	struct sock *sk;
 
-	if (state->bpf_seq_afinfo)
-		afinfo = state->bpf_seq_afinfo;
-	else
-		afinfo = pde_data(file_inode(seq->file));
+	afinfo = pde_data(file_inode(seq->file));
 
 	udptable = udp_get_table_afinfo(afinfo, net);
 
@@ -3033,10 +3030,7 @@  static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
 	struct udp_seq_afinfo *afinfo;
 	struct udp_table *udptable;
 
-	if (state->bpf_seq_afinfo)
-		afinfo = state->bpf_seq_afinfo;
-	else
-		afinfo = pde_data(file_inode(seq->file));
+	afinfo = pde_data(file_inode(seq->file));
 
 	do {
 		sk = sk_next(sk);
@@ -3094,10 +3088,7 @@  void udp_seq_stop(struct seq_file *seq, void *v)
 	struct udp_seq_afinfo *afinfo;
 	struct udp_table *udptable;
 
-	if (state->bpf_seq_afinfo)
-		afinfo = state->bpf_seq_afinfo;
-	else
-		afinfo = pde_data(file_inode(seq->file));
+	afinfo = pde_data(file_inode(seq->file));
 
 	udptable = udp_get_table_afinfo(afinfo, seq_file_net(seq));
 
@@ -3415,28 +3406,11 @@  DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta,
 
 static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux)
 {
-	struct udp_iter_state *st = priv_data;
-	struct udp_seq_afinfo *afinfo;
-	int ret;
-
-	afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN);
-	if (!afinfo)
-		return -ENOMEM;
-
-	afinfo->family = AF_UNSPEC;
-	afinfo->udp_table = NULL;
-	st->bpf_seq_afinfo = afinfo;
-	ret = bpf_iter_init_seq_net(priv_data, aux);
-	if (ret)
-		kfree(afinfo);
-	return ret;
+	return bpf_iter_init_seq_net(priv_data, aux);
 }
 
 static void bpf_iter_fini_udp(void *priv_data)
 {
-	struct udp_iter_state *st = priv_data;
-
-	kfree(st->bpf_seq_afinfo);
 	bpf_iter_fini_seq_net(priv_data);
 }