From nobody Sun Apr 12 05:50:50 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 72638C00144 for ; Mon, 1 Aug 2022 17:55:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234265AbiHARzW (ORCPT ); Mon, 1 Aug 2022 13:55:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234132AbiHARzA (ORCPT ); Mon, 1 Aug 2022 13:55:00 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22A803C163 for ; Mon, 1 Aug 2022 10:54:50 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id a11-20020a5b090b000000b00672acbe13c1so9120534ybq.2 for ; Mon, 01 Aug 2022 10:54:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=TT0h45HvgWsHdbEBDmT0Xh/QzjlzP1cphHtwakCGIcs=; b=MPoa2ahkqftJtCCQwamXONh3imuj1fc11gLvHmErlPHEQVftxYVmYXKbIQVEPEzMrN d/96nYD7171+3GsL2PBIaeo2uPqqTjv3DAYS2+YmniPaPGFLc6m3jew3jDiwE4KYbqt3 v9ntQucK1EtArTYje84l9fk2F5KX300+2c/5+QF+7IvL0a8HZQi32ZwjAPNdnD6c0GH5 xtGZ0Cv2jVnb2n1dRXjBW7zoi/13/M/jawWbKX7vC5qDybY4Ni2KQ5VpSdTbXZO1jcaA RBb3PBGrhlpvVIQeSBzU/4yP8E1DMv7dgyMV699T5nYTpvfYNoyZXakulYwRlQdAyX6f uG8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=TT0h45HvgWsHdbEBDmT0Xh/QzjlzP1cphHtwakCGIcs=; b=7p97hfN2HmN5FSKseD0GXobEkKI3cLMWuks+5G9sU75uatL7WR7MssFXaZoxgejzFl lxC4uzZsNC8OLtNDOzkIbGkFFGYEz8SpBWm3QwAMtE+lltZUsuA4+w3vIkoWHam5MuyF CESJusY3EKEp8kx1lYQKOhmxp0KkmMgg++ehQKLLOmjg78cy+4L+txwmI4VNNsCJzZ6a HBcAzXP9FanbyuCFkxILXMrmheBD74VtRoWTcfsMMcz74xTXownJ9Ps9oThnB/QMo2HQ fijL3YLvDAfBPcFxyg/egSFIz8u+N+mXjBJeUl/txEBZ5Bcth4+ELcjufaSyRBP/pEZ5 f4Ww== X-Gm-Message-State: ACgBeo32HTu0wnD6vqq6SKZBltC39iHrN+LQgTOGlr8iF8i62DcnB35I 3r2orMW5hnFfm7inixNRqDai4621VFJwMTxRJGOPS1panzODeiEqsaGuuvBljNuRFTTCWDvMJSk D8YMgxl8wYisC7Pp9z3ASvKAzPHCszzGwrV4erLpMdt3EEVpensQ7OA6HnhxMorjDc4Xo2Q== X-Google-Smtp-Source: AA6agR7/Ia2Te0RU2F8G9qmVle6rQ/lQr5q4ydpTOUxduyezVB7kODRc+/A3KwJuO4yCSSQHbQ4KaNfzjkA= X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:7c9:7b32:e73f:6716]) (user=haoluo job=sendgmr) by 2002:a25:2103:0:b0:66f:2c4f:7de8 with SMTP id h3-20020a252103000000b0066f2c4f7de8mr11858019ybh.306.1659376489186; Mon, 01 Aug 2022 10:54:49 -0700 (PDT) Date: Mon, 1 Aug 2022 10:54:05 -0700 In-Reply-To: <20220801175407.2647869-1-haoluo@google.com> Message-Id: <20220801175407.2647869-7-haoluo@google.com> Mime-Version: 1.0 References: <20220801175407.2647869-1-haoluo@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH bpf-next v6 6/8] cgroup: bpf: enable bpf programs to integrate with rstat From: Hao Luo To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, cgroups@vger.kernel.org, netdev@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Tejun Heo , Zefan Li , KP Singh , Johannes Weiner , Michal Hocko , Benjamin Tissoires , John Fastabend , Michal Koutny , Roman Gushchin , David Rientjes , Stanislav Fomichev , Shakeel Butt , Yosry Ahmed , Hao Luo Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Yosry Ahmed From: Yosry Ahmed Enable bpf programs to make use of rstat to collect cgroup hierarchical stats efficiently: - Add cgroup_rstat_updated() kfunc, for bpf progs that collect stats. - Add cgroup_rstat_flush() sleepable kfunc, for bpf progs that read stats. - Add an empty bpf_rstat_flush() hook that is called during rstat flushing, for bpf progs that flush stats to attach to. Attaching a bpf prog to this hook effectively registers it as a flush callback. Signed-off-by: Yosry Ahmed Acked-by: Tejun Heo Signed-off-by: Hao Luo --- kernel/cgroup/rstat.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index 24b5c2ab5598..3289f6e0d306 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -3,6 +3,10 @@ =20 #include =20 +#include +#include +#include + static DEFINE_SPINLOCK(cgroup_rstat_lock); static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); =20 @@ -141,6 +145,31 @@ static struct cgroup *cgroup_rstat_cpu_pop_updated(str= uct cgroup *pos, return pos; } =20 +/* + * A hook for bpf stat collectors to attach to and flush their stats. + * Together with providing bpf kfuncs for cgroup_rstat_updated() and + * cgroup_rstat_flush(), this enables a complete workflow where bpf progs = that + * collect cgroup stats can integrate with rstat for efficient flushing. + * + * A static noinline declaration here could cause the compiler to optimize= away + * the function. A global noinline declaration will keep the definition, b= ut may + * optimize away the callsite. Therefore, __weak is needed to ensure that = the + * call is still emitted, by telling the compiler that we don't know what = the + * function might eventually be. + * + * __diag_* below are needed to dismiss the missing prototype warning. + */ +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "kfuncs which will be used in BPF programs"); + +__weak noinline void bpf_rstat_flush(struct cgroup *cgrp, + struct cgroup *parent, int cpu) +{ +} + +__diag_pop(); + /* see cgroup_rstat_flush() */ static void cgroup_rstat_flush_locked(struct cgroup *cgrp, bool may_sleep) __releases(&cgroup_rstat_lock) __acquires(&cgroup_rstat_lock) @@ -168,6 +197,7 @@ static void cgroup_rstat_flush_locked(struct cgroup *cg= rp, bool may_sleep) struct cgroup_subsys_state *css; =20 cgroup_base_stat_flush(pos, cpu); + bpf_rstat_flush(pos, cgroup_parent(pos), cpu); =20 rcu_read_lock(); list_for_each_entry_rcu(css, &pos->rstat_css_list, @@ -469,3 +499,21 @@ void cgroup_base_stat_cputime_show(struct seq_file *se= q) "system_usec %llu\n", usage, utime, stime); } + +/* Add bpf kfuncs for cgroup_rstat_updated() and cgroup_rstat_flush() */ +BTF_SET8_START(bpf_rstat_kfunc_ids) +BTF_ID_FLAGS(func, cgroup_rstat_updated) +BTF_ID_FLAGS(func, cgroup_rstat_flush, KF_SLEEPABLE) +BTF_SET8_END(bpf_rstat_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_rstat_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &bpf_rstat_kfunc_ids, +}; + +static int __init bpf_rstat_kfunc_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, + &bpf_rstat_kfunc_set); +} +late_initcall(bpf_rstat_kfunc_init); --=20 2.37.1.455.g008518b4e5-goog