From nobody Wed Apr 8 09:43:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9F43C25B08 for ; Sat, 20 Aug 2022 11:33:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345264AbiHTLd1 (ORCPT ); Sat, 20 Aug 2022 07:33:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344899AbiHTLdS (ORCPT ); Sat, 20 Aug 2022 07:33:18 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60AF310B6; Sat, 20 Aug 2022 04:33:12 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.54]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4M8xJb3PrsznTfD; Sat, 20 Aug 2022 19:30:55 +0800 (CST) Received: from dggpemm500019.china.huawei.com (7.185.36.180) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 20 Aug 2022 19:33:10 +0800 Received: from k04.huawei.com (10.67.174.115) by dggpemm500019.china.huawei.com (7.185.36.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 20 Aug 2022 19:33:10 +0800 From: Pu Lehui To: , CC: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Quentin Monnet , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , "Jean-Philippe Brucker" , Stanislav Fomichev , "Hao Luo" , Jiri Olsa , Pu Lehui Subject: [PATCH bpf-next 1/2] bpf, cgroup: Fix attach flags being assigned to effective progs Date: Sat, 20 Aug 2022 20:02:33 +0800 Message-ID: <20220820120234.2121044-2-pulehui@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220820120234.2121044-1-pulehui@huawei.com> References: <20220820120234.2121044-1-pulehui@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.67.174.115] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500019.china.huawei.com (7.185.36.180) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Attach flags is only valid for attached progs of this layer cgroup, but not for effective progs. We know that the attached progs is at the beginning of the effective progs array, so we can just populate the elements in front of the prog_attach_flags array. Signed-off-by: Pu Lehui --- kernel/bpf/cgroup.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 59b7eb60d5b4..9adf72e99907 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -1091,11 +1091,14 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, = const union bpf_attr *attr, } =20 if (prog_attach_flags) { + int progs_cnt =3D prog_list_length(&cgrp->bpf.progs[atype]); flags =3D cgrp->bpf.flags[atype]; =20 - for (i =3D 0; i < cnt; i++) + /* attach flags only for attached progs, but not effective progs */ + for (i =3D 0; i < progs_cnt; i++) if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags))) return -EFAULT; + prog_attach_flags +=3D cnt; } =20 --=20 2.25.1 From nobody Wed Apr 8 09:43:00 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2B59C32774 for ; Sat, 20 Aug 2022 11:33:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345309AbiHTLdc (ORCPT ); Sat, 20 Aug 2022 07:33:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345157AbiHTLdS (ORCPT ); Sat, 20 Aug 2022 07:33:18 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 694E010C0; Sat, 20 Aug 2022 04:33:12 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4M8xHZ1y3YzlWLb; Sat, 20 Aug 2022 19:30:02 +0800 (CST) Received: from dggpemm500019.china.huawei.com (7.185.36.180) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 20 Aug 2022 19:33:10 +0800 Received: from k04.huawei.com (10.67.174.115) by dggpemm500019.china.huawei.com (7.185.36.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 20 Aug 2022 19:33:10 +0800 From: Pu Lehui To: , CC: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Quentin Monnet , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , "Jean-Philippe Brucker" , Stanislav Fomichev , "Hao Luo" , Jiri Olsa , Pu Lehui Subject: [PATCH bpf-next 2/2] bpftool: Fix cgroup attach flags being assigned to effective progs Date: Sat, 20 Aug 2022 20:02:34 +0800 Message-ID: <20220820120234.2121044-3-pulehui@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220820120234.2121044-1-pulehui@huawei.com> References: <20220820120234.2121044-1-pulehui@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.67.174.115] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm500019.china.huawei.com (7.185.36.180) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When root-cgroup attach multi progs and sub-cgroup attach a override prog, bpftool will display incorrectly for the attach flags of the sub-cgroup=E2=80=99s effective progs: $ bpftool c t /sys/fs/cgroup effective CgroupPath ID AttachType AttachFlags Name /sys/fs/cgroup 6 sysctl multi sysctl_tcp_mem 13 sysctl multi sysctl_tcp_mem /sys/fs/cgroup/cg1 20 sysctl override sysctl_tcp_mem 6 sysctl override sysctl_tcp_mem 13 sysctl override sysctl_tcp_mem Attach flags is only valid for attached progs of this layer cgroup, but not for effective progs. Since prog_attach_flags array is already bypass the effective progs, so we can just use it. Signed-off-by: Pu Lehui --- tools/bpf/bpftool/cgroup.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/bpf/bpftool/cgroup.c b/tools/bpf/bpftool/cgroup.c index cced668fb2a3..fa3eef0ff860 100644 --- a/tools/bpf/bpftool/cgroup.c +++ b/tools/bpf/bpftool/cgroup.c @@ -219,11 +219,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum= bpf_attach_type type, return 0; =20 for (iter =3D 0; iter < p.prog_cnt; iter++) { - __u32 attach_flags; - - attach_flags =3D prog_attach_flags[iter] ?: p.attach_flags; - - switch (attach_flags) { + switch (prog_attach_flags[iter]) { case BPF_F_ALLOW_MULTI: attach_flags_str =3D "multi"; break; @@ -234,7 +230,8 @@ static int show_attached_bpf_progs(int cgroup_fd, enum = bpf_attach_type type, attach_flags_str =3D ""; break; default: - snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags); + snprintf(buf, sizeof(buf), "unknown(%x)", + prog_attach_flags[iter]); attach_flags_str =3D buf; } =20 --=20 2.25.1