Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp1694691iol; Fri, 10 Jun 2022 12:50:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwn3xBgHE+Q1rUvSr3raZ4pzkmU3LMSYsSoRqgF3AL4O4sheOO2yrwy2VXedhwDvir6glek X-Received: by 2002:a17:907:9483:b0:6ff:b1:467a with SMTP id dm3-20020a170907948300b006ff00b1467amr42315031ejc.683.1654890606633; Fri, 10 Jun 2022 12:50:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654890606; cv=none; d=google.com; s=arc-20160816; b=MXruezQMs0UfS3xQY3+HCN97SKww9eonG2GOZx5diQ6ViNU1VypXlCYiXLe5xOtNU+ EAGK2jAaE7NIo1gPxrU0tX4bKTc4RekFEfXV7RWmQssIfF2yyFJZd97JTDvv2c3jkYCX MDWCVuSen5Lt7024LO27sOfUcl7ariTZKeNngRyOXflbj79kxx9JCUoPBE7pPmjFcjck 1h/gsrCPtKabwGD049X9aslskjLz3Ci4zYS2Fmdt1AZM+cr4D98ACW/KgG14x1emAgdb 2vZYxCvlCElBCFzXw/sKpf7mB7oei7gsAMh2sBva6JREJFqFOgRcJ7eexaE6JnEkqh8q 0M6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=XL2ObbHh5bI7PkeXJYGc2UizZ2yMx0505kd5+6ZTGek=; b=gvB9C60YhDmN+Qvw8knz0bigba7mNPPp9M5SsVva1pevEsnOx7JF4Nja7zSH6UKksI Fvog0ltG4u5s8OAKJ1DIwwf7uaa7ttUkOH8NpfdsAJJ1ECo3LyR5H7xSwRNpeEp7qoCV 8h6GXQ2yaLAVG3GaHSSctt6JBLvchcCdGaSi5zHcITe3BlruuZXAo0F0rpDaQ4V7JLDy 7vFyfZDA75N+93mv5nt/rpSGVRgvkPcxnVtrbcMdIF/PtNTvXXlvZj5FtgzcLbF9Kv4a NHjcVHkwqNeC3HQMWPTjiqlo2RthqhBVOuQoNiU9jZTsuxwb5w9mTmy5E91euBTVMTcp CLsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=V3FC87To; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r6-20020a50c006000000b0042dd14fb5d9si152522edb.125.2022.06.10.12.49.40; Fri, 10 Jun 2022 12:50:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=V3FC87To; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348604AbiFJTpf (ORCPT + 99 others); Fri, 10 Jun 2022 15:45:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350397AbiFJTpG (ORCPT ); Fri, 10 Jun 2022 15:45:06 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8343483B2 for ; Fri, 10 Jun 2022 12:44:52 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id b9-20020a656689000000b003f672946300so21093pgw.16 for ; Fri, 10 Jun 2022 12:44:52 -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=XL2ObbHh5bI7PkeXJYGc2UizZ2yMx0505kd5+6ZTGek=; b=V3FC87ToUbSYHf9bcAaSuAZkXLsAVlExIxHjjiSdh7QMh02pWjjd6E4B6KhNiA1WpZ wQs+tK4vP63QqacshGi7SJTaZU1yq6orG3ioQrH/ECMcW5DnQHBLM/tgT3J3gC1Y3tUE NIVL9e156rrxhaF7d/TohQFAqUrJ8xhXkl+Nx51wagFPyZSTYL1LiNofjdmqEiS9AwcH Wp/hJmcWK/LD0/hoTFDDIDdKENwKOsBPT5tZKRr14jzUN++hNlPv5gxli9eX7ki33559 PI8x7eKkaXF/raklqyIIGN2PTc5dXzYJm6hj5I6kJAJjavpnoqjMrgMM50C+/2x8XIk+ fEWg== 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=XL2ObbHh5bI7PkeXJYGc2UizZ2yMx0505kd5+6ZTGek=; b=D9sw+Bzkq7G3vTEgGuneCtW1mcK93BPp1lViCg656O1Q5yRATLtDm8dtVAARjufICV VWN2RQAGPLwO5RvgUHHPRAUfaSHjKLf+z1SDLW7EdzkNLzLEYR/ruxw5Iij3BVH0NETd iLfIk1PedAsTd+Sovf7cBKw8205vw4CgocezIgfdsIbnwOQKy1dZ07LL8ha0WUTJMocy fGLzNid191bymSqle206CUks01sIbUvIfZCl3J/S6ZRsQCzt/A3rRBKv53pz/46x/8mm dw47kqXEWrDSoh11RQD7Gq120fWjvccIkWrcL9+je8x/u+0pbOhCCIIsZhqAgsvop+AU BSdw== X-Gm-Message-State: AOAM533Zv9R69xiZB0aOMh/YNlSQ2tEr6UnNoZzhvjTHR8QsWWf+FVwO whFugLdiyma0cAzrFGe4SX7V2wwMxV1cMUp9 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a63:181:0:b0:3fe:149a:35df with SMTP id 123-20020a630181000000b003fe149a35dfmr16815341pgb.158.1654890292074; Fri, 10 Jun 2022 12:44:52 -0700 (PDT) Date: Fri, 10 Jun 2022 19:44:33 +0000 In-Reply-To: <20220610194435.2268290-1-yosryahmed@google.com> Message-Id: <20220610194435.2268290-7-yosryahmed@google.com> Mime-Version: 1.0 References: <20220610194435.2268290-1-yosryahmed@google.com> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH bpf-next v2 6/8] cgroup: bpf: enable bpf programs to integrate with rstat From: Yosry Ahmed To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Tejun Heo , Zefan Li , Johannes Weiner , Shuah Khan , Michal Hocko Cc: Roman Gushchin , David Rientjes , Stanislav Fomichev , Greg Thelen , Shakeel Butt , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, cgroups@vger.kernel.org, Yosry Ahmed Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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() 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 --- kernel/cgroup/rstat.c | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index 24b5c2ab55983..94140bd0d02a4 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -3,6 +3,11 @@ #include +#include +#include +#include + + static DEFINE_SPINLOCK(cgroup_rstat_lock); static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); @@ -141,6 +146,23 @@ static struct cgroup *cgroup_rstat_cpu_pop_updated(struct cgroup *pos, return pos; } +/* + * 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, but 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. + */ +__weak noinline void bpf_rstat_flush(struct cgroup *cgrp, + struct cgroup *parent, int cpu) +{ +} + /* 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 +190,7 @@ static void cgroup_rstat_flush_locked(struct cgroup *cgrp, bool may_sleep) struct cgroup_subsys_state *css; cgroup_base_stat_flush(pos, cpu); + bpf_rstat_flush(pos, cgroup_parent(pos), cpu); rcu_read_lock(); list_for_each_entry_rcu(css, &pos->rstat_css_list, @@ -469,3 +492,26 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq) "system_usec %llu\n", usage, utime, stime); } + +/* Add bpf kfuncs for cgroup_rstat_updated() and cgroup_rstat_flush() */ +BTF_SET_START(bpf_rstat_check_kfunc_ids) +BTF_ID(func, cgroup_rstat_updated) +BTF_ID(func, cgroup_rstat_flush) +BTF_SET_END(bpf_rstat_check_kfunc_ids) + +BTF_SET_START(bpf_rstat_sleepable_kfunc_ids) +BTF_ID(func, cgroup_rstat_flush) +BTF_SET_END(bpf_rstat_sleepable_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_rstat_kfunc_set = { + .owner = THIS_MODULE, + .check_set = &bpf_rstat_check_kfunc_ids, + .sleepable_set = &bpf_rstat_sleepable_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); -- 2.36.1.476.g0c4daa206d-goog