Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: tcp: seq_file: Remove bpf_seq_afinfo from tcp_iter_state

A following patch will create a separate struct to store extra
bpf_iter state and it will embed the existing tcp_iter_state like this:
struct bpf_tcp_iter_state {
struct tcp_iter_state state;
/* More bpf_iter specific states here ... */
}

As a prep work, this patch removes the
"struct tcp_seq_afinfo *bpf_seq_afinfo" where its purpose is
to tell if it is iterating from bpf_iter instead of proc fs.
Currently, if "*bpf_seq_afinfo" is not NULL, it is iterating from
bpf_iter. The kernel should not filter by the addr family and
leave this filtering decision to the bpf prog.

Instead of adding a "*bpf_seq_afinfo" pointer, this patch uses the
"seq->op == &bpf_iter_tcp_seq_ops" test to tell if it is iterating
from the bpf iter.

The bpf_iter_(init|fini)_tcp() is left here to prepare for
the change of a following patch.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Acked-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210701200554.1034982-1-kafai@fb.com

authored by

Martin KaFai Lau and committed by
Andrii Nakryiko
62001372 ad2d6137

+5 -21
-1
include/net/tcp.h
··· 1959 1959 struct seq_net_private p; 1960 1960 enum tcp_seq_states state; 1961 1961 struct sock *syn_wait_sk; 1962 - struct tcp_seq_afinfo *bpf_seq_afinfo; 1963 1962 int bucket, offset, sbucket, num; 1964 1963 loff_t last_pos; 1965 1964 };
+5 -20
net/ipv4/tcp_ipv4.c
··· 2735 2735 #endif 2736 2736 static unsigned short seq_file_family(const struct seq_file *seq) 2737 2737 { 2738 - const struct tcp_iter_state *st = seq->private; 2739 - const struct tcp_seq_afinfo *afinfo = st->bpf_seq_afinfo; 2738 + const struct tcp_seq_afinfo *afinfo; 2740 2739 2740 + #ifdef CONFIG_BPF_SYSCALL 2741 2741 /* Iterated from bpf_iter. Let the bpf prog to filter instead. */ 2742 - if (afinfo) 2742 + if (seq->op == &bpf_iter_tcp_seq_ops) 2743 2743 return AF_UNSPEC; 2744 + #endif 2744 2745 2745 2746 /* Iterated from proc fs */ 2746 2747 afinfo = PDE_DATA(file_inode(seq->file)); ··· 2999 2998 3000 2999 static int bpf_iter_init_tcp(void *priv_data, struct bpf_iter_aux_info *aux) 3001 3000 { 3002 - struct tcp_iter_state *st = priv_data; 3003 - struct tcp_seq_afinfo *afinfo; 3004 - int ret; 3005 - 3006 - afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN); 3007 - if (!afinfo) 3008 - return -ENOMEM; 3009 - 3010 - afinfo->family = AF_UNSPEC; 3011 - st->bpf_seq_afinfo = afinfo; 3012 - ret = bpf_iter_init_seq_net(priv_data, aux); 3013 - if (ret) 3014 - kfree(afinfo); 3015 - return ret; 3001 + return bpf_iter_init_seq_net(priv_data, aux); 3016 3002 } 3017 3003 3018 3004 static void bpf_iter_fini_tcp(void *priv_data) 3019 3005 { 3020 - struct tcp_iter_state *st = priv_data; 3021 - 3022 - kfree(st->bpf_seq_afinfo); 3023 3006 bpf_iter_fini_seq_net(priv_data); 3024 3007 } 3025 3008