From: Roman Gushchin Subject: Re: [PATCH bpf-next 2/3] bpf: add bpffs pretty print for cgroup local storage maps Date: Mon, 10 Dec 2018 22:46:04 +0000 Message-ID: <20181210224600.GB28954@tower.DHCP.thefacebook.com> References: <20181208005315.3500-1-guro@fb.com> <20181208005315.3500-2-guro@fb.com> <60a77f17-6d63-2f73-e0b2-0ca8b5338169@fb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Cc: Roman Gushchin , "netdev@vger.kernel.org" , Kernel Team , "linux-kernel@vger.kernel.org" , "Alexei Starovoitov" , Daniel Borkmann To: Yonghong Song Return-path: In-Reply-To: <60a77f17-6d63-2f73-e0b2-0ca8b5338169@fb.com> Content-Language: en-US Content-ID: <3DBAF662DDA8D34AAF6C834832E54642@namprd15.prod.outlook.com> Sender: linux-kernel-owner@vger.kernel.org List-Id: netdev.vger.kernel.org On Fri, Dec 07, 2018 at 07:37:35PM -0800, Yonghong Song wrote: >=20 >=20 > On 12/7/18 4:53 PM, Roman Gushchin wrote: > > Implement bpffs pretty printing for cgroup local storage maps > > (both shared and per-cpu). > > Output example (captured for tools/testing/selftests/bpf/netcnt_prog.c)= : > >=20 > > Shared: > > $ cat /sys/fs/bpf/map_2 > > # WARNING!! The output is for debug purpose only > > # WARNING!! The output format will change > > {4294968594,1}: {9999,1039896} > >=20 > > Per-cpu: > > $ cat /sys/fs/bpf/map_1 > > # WARNING!! The output is for debug purpose only > > # WARNING!! The output format will change > > {4294968594,1}: { > > cpu0: {0,0,0,0,0} > > cpu1: {0,0,0,0,0} > > cpu2: {1,104,0,0,0} > > cpu3: {0,0,0,0,0} > > } > >=20 > > Signed-off-by: Roman Gushchin > > Cc: Alexei Starovoitov > > Cc: Daniel Borkmann > > --- > > include/linux/btf.h | 10 +++++ > > kernel/bpf/local_storage.c | 90 +++++++++++++++++++++++++++++++++++++= - > > 2 files changed, 99 insertions(+), 1 deletion(-) > >=20 > > diff --git a/include/linux/btf.h b/include/linux/btf.h > > index 8c2199b5d250..ac67bc4cbfd9 100644 > > --- a/include/linux/btf.h > > +++ b/include/linux/btf.h > > @@ -5,6 +5,7 @@ > > #define _LINUX_BTF_H 1 > > =20 > > #include > > +#include > > =20 > > struct btf; > > struct btf_type; > > @@ -63,4 +64,13 @@ static inline const char *btf_name_by_offset(const s= truct btf *btf, > > } > > #endif > > =20 > > +static inline const struct btf_type *btf_orig_type(const struct btf *b= tf, > > + const struct btf_type *t) > > +{ > > + while (t && BTF_INFO_KIND(t->info) =3D=3D BTF_KIND_TYPEDEF) > > + t =3D btf_type_by_id(btf, t->type); > technically, type modifier "const" and "volatile" can apply to member=20 > type as well. But these modifiers really don't make sense here. > Could you add a comment here to mention that they will be treated > as an error since such a programming is not really recommended? Switched over to btf_type_id_size() based on Martin's suggestion. >=20 > > + > > + return t; > > +} > > + > > #endif > > diff --git a/kernel/bpf/local_storage.c b/kernel/bpf/local_storage.c > > index b65017dead44..7b51fe1aba3c 100644 > > --- a/kernel/bpf/local_storage.c > > +++ b/kernel/bpf/local_storage.c > > @@ -1,11 +1,13 @@ > > //SPDX-License-Identifier: GPL-2.0 > > #include > > #include > > +#include > > #include > > #include > > #include > > #include > > #include > > +#include > > =20 > > DEFINE_PER_CPU(struct bpf_cgroup_storage*, bpf_cgroup_storage[MAX_BPF= _CGROUP_STORAGE_TYPE]); > > =20 > > @@ -308,6 +310,91 @@ static int cgroup_storage_delete_elem(struct bpf_m= ap *map, void *key) > > return -EINVAL; > > } > > =20 > > +static int cgroup_storage_check_btf(const struct bpf_map *map, > > + const struct btf *btf, > > + const struct btf_type *key_type, > > + const struct btf_type *value_type) > > +{ > > + const struct btf_type *st, *t; > > + struct btf_member *m; > > + > > + /* Key is expected to be of struct bpf_cgroup_storage_key type, > > + * which is: > > + * struct bpf_cgroup_storage_key { > > + * __u64 cgroup_inode_id; > > + * __u32 attach_type; > > + * }; > > + */ > > + > > + /* > > + * Key_type must be a structure (or a typedef of a structure) with > > + * two members. > > + */ > > + st =3D btf_orig_type(btf, key_type); > > + if (BTF_INFO_KIND(st->info) !=3D BTF_KIND_STRUCT || > > + BTF_INFO_VLEN(st->info) !=3D 2) > > + return -EINVAL; > > + > > + /* > > + * The first field must be a 64 bit integer at 0 offset. > > + */ > > + m =3D (struct btf_member *)(st + 1); > > + t =3D btf_orig_type(btf, btf_type_by_id(btf, m->type)); > > + if (!t || BTF_INFO_KIND(t->info) !=3D BTF_KIND_INT || m->offset || > > + t->size !=3D > > + FIELD_SIZEOF(struct bpf_cgroup_storage_key, cgroup_inode_id)) > > + return -EINVAL; >=20 > We should not use t->size here. The "t->size" is the type size, and the > real number of bits held by the member is BTF_INT_BITS(...) with the=20 > argument of the u32 int value after "t". >=20 > > + > > + /* > > + * The second field must be a 32 bit integer at 0 offset. > > + */ > > + m =3D m + 1; > > + t =3D btf_orig_type(btf, btf_type_by_id(btf, m->type)); > > + if (!t || BTF_INFO_KIND(t->info) !=3D BTF_KIND_INT || > > + m->offset !=3D offsetof(struct bpf_cgroup_storage_key, attach_typ= e) * > > + BITS_PER_BYTE || t->size !=3D > > + FIELD_SIZEOF(struct bpf_cgroup_storage_key, attach_type)) > > + return -EINVAL; >=20 > The same is here. t->size should not be used. > BTF_INT_BITS(...) should be used. Fixed in v2, which I'll send soon. Thank you for the review!