From patchwork Wed Sep 14 07:24:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= X-Patchwork-Id: 9330619 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2142260231 for ; Wed, 14 Sep 2016 07:31:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1562229A62 for ; Wed, 14 Sep 2016 07:31:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 09D5B29A64; Wed, 14 Sep 2016 07:31:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4784129A63 for ; Wed, 14 Sep 2016 07:31:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761117AbcINHar (ORCPT ); Wed, 14 Sep 2016 03:30:47 -0400 Received: from smtp-sh.infomaniak.ch ([128.65.195.4]:39319 "EHLO smtp-sh.infomaniak.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760289AbcINHZ4 (ORCPT ); Wed, 14 Sep 2016 03:25:56 -0400 Received: from smtp6.infomaniak.ch (smtp6.infomaniak.ch [83.166.132.19]) by smtp-sh.infomaniak.ch (8.14.5/8.14.5) with ESMTP id u8E7P3AB011067 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 14 Sep 2016 09:25:03 +0200 Received: from localhost (ns3096276.ip-94-23-54.eu [94.23.54.103]) (authenticated bits=0) by smtp6.infomaniak.ch (8.14.5/8.14.5) with ESMTP id u8E7P3DT008024; Wed, 14 Sep 2016 09:25:03 +0200 From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Alexei Starovoitov , Andy Lutomirski , Arnd Bergmann , Casey Schaufler , Daniel Borkmann , Daniel Mack , David Drysdale , "David S . Miller" , Elena Reshetova , "Eric W . Biederman" , James Morris , Kees Cook , Paul Moore , Sargun Dhillon , "Serge E . Hallyn" , Tejun Heo , Will Drewry , kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, linux-security-module@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org Subject: [RFC v3 13/22] bpf/cgroup: Replace struct bpf_prog with union bpf_object Date: Wed, 14 Sep 2016 09:24:06 +0200 Message-Id: <20160914072415.26021-14-mic@digikod.net> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20160914072415.26021-1-mic@digikod.net> References: <20160914072415.26021-1-mic@digikod.net> MIME-Version: 1.0 X-Antivirus: Dr.Web (R) for Unix mail servers drweb plugin ver.6.0.2.8 X-Antivirus-Code: 0x100000 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP This allows CONFIG_CGROUP_BPF to manage different type of pointers instead of only eBPF programs. This will be useful for the next commits to support Landlock with cgroups. Signed-off-by: Mickaël Salaün Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Daniel Mack Cc: David S. Miller Cc: Tejun Heo --- include/linux/bpf-cgroup.h | 8 ++++++-- kernel/bpf/cgroup.c | 44 +++++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index fc076de74ab9..2234042d7f61 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -14,14 +14,18 @@ struct sk_buff; extern struct static_key_false cgroup_bpf_enabled_key; #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) +union bpf_object { + struct bpf_prog *prog; +}; + struct cgroup_bpf { /* * Store two sets of bpf_prog pointers, one for programs that are * pinned directly to this cgroup, and one for those that are effective * when this cgroup is accessed. */ - struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE]; - struct bpf_prog *effective[MAX_BPF_ATTACH_TYPE]; + union bpf_object pinned[MAX_BPF_ATTACH_TYPE]; + union bpf_object effective[MAX_BPF_ATTACH_TYPE]; }; void cgroup_bpf_put(struct cgroup *cgrp); diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 21d168c3ad35..782878ec4f2d 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -20,18 +20,18 @@ DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key); EXPORT_SYMBOL(cgroup_bpf_enabled_key); /** - * cgroup_bpf_put() - put references of all bpf programs + * cgroup_bpf_put() - put references of all bpf objects * @cgrp: the cgroup to modify */ void cgroup_bpf_put(struct cgroup *cgrp) { unsigned int type; - for (type = 0; type < ARRAY_SIZE(cgrp->bpf.prog); type++) { - struct bpf_prog *prog = cgrp->bpf.prog[type]; + for (type = 0; type < ARRAY_SIZE(cgrp->bpf.pinned); type++) { + union bpf_object pinned = cgrp->bpf.pinned[type]; - if (prog) { - bpf_prog_put(prog); + if (pinned.prog) { + bpf_prog_put(pinned.prog); static_branch_dec(&cgroup_bpf_enabled_key); } } @@ -47,11 +47,12 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent) unsigned int type; for (type = 0; type < ARRAY_SIZE(cgrp->bpf.effective); type++) { - struct bpf_prog *e; + union bpf_object e; - e = rcu_dereference_protected(parent->bpf.effective[type], - lockdep_is_held(&cgroup_mutex)); - rcu_assign_pointer(cgrp->bpf.effective[type], e); + e.prog = rcu_dereference_protected( + parent->bpf.effective[type].prog, + lockdep_is_held(&cgroup_mutex)); + rcu_assign_pointer(cgrp->bpf.effective[type].prog, e.prog); } } @@ -87,32 +88,33 @@ void __cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog, enum bpf_attach_type type) { - struct bpf_prog *old_prog, *effective; + union bpf_object obj, old_pinned, effective; struct cgroup_subsys_state *pos; - old_prog = xchg(cgrp->bpf.prog + type, prog); + obj.prog = prog; + old_pinned = xchg(cgrp->bpf.pinned + type, obj); - effective = (!prog && parent) ? - rcu_dereference_protected(parent->bpf.effective[type], + effective.prog = (!obj.prog && parent) ? + rcu_dereference_protected(parent->bpf.effective[type].prog, lockdep_is_held(&cgroup_mutex)) : - prog; + obj.prog; css_for_each_descendant_pre(pos, &cgrp->self) { struct cgroup *desc = container_of(pos, struct cgroup, self); /* skip the subtree if the descendant has its own program */ - if (desc->bpf.prog[type] && desc != cgrp) + if (desc->bpf.pinned[type].prog && desc != cgrp) pos = css_rightmost_descendant(pos); else - rcu_assign_pointer(desc->bpf.effective[type], - effective); + rcu_assign_pointer(desc->bpf.effective[type].prog, + effective.prog); } - if (prog) + if (obj.prog) static_branch_inc(&cgroup_bpf_enabled_key); - if (old_prog) { - bpf_prog_put(old_prog); + if (old_pinned.prog) { + bpf_prog_put(old_pinned.prog); static_branch_dec(&cgroup_bpf_enabled_key); } } @@ -151,7 +153,7 @@ int __cgroup_bpf_run_filter(struct sock *sk, rcu_read_lock(); - prog = rcu_dereference(cgrp->bpf.effective[type]); + prog = rcu_dereference(cgrp->bpf.effective[type].prog); if (prog) { unsigned int offset = skb->data - skb_mac_header(skb);