Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp44164rwe; Wed, 24 Aug 2022 16:34:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR6hehah1pQ+fN/xOn7KAit+XpTh4+Yr2xCIW3e8oN4+j2yF7GfmiRLzUefOa/qjislRg7xW X-Received: by 2002:a63:d047:0:b0:41d:d4e9:e182 with SMTP id s7-20020a63d047000000b0041dd4e9e182mr994802pgi.328.1661384088522; Wed, 24 Aug 2022 16:34:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661384088; cv=none; d=google.com; s=arc-20160816; b=Oi1BzIq3P0RXNBAwe5Mtn3CZIch6uiwc4zvtIj3nqiM+bqjAB2iJER2H47yj2Myglp qwoBvBwLRuCrme/WZ3fT5RV4XpMHYdUKYPuBhCbtkVsTu3eP50pxiVe9IdGmMtRjvOaN OmXJEIGhRdYtrZyfE3470YdYEi4WVWaQxD8+bUz92BndC8RHyLqnRCaJL9agN5QA3uds 72keu5yFIfgquzLVe4Dmd+5qw7ZEky6gqCh2y+B8U85IRSrvaw85EBbCGKtuws2X89d7 uQGfZ4ptQ3Vzi2riPnhukCDK+DSWfiE02tyOtdRdllnoeCitn0oToakgS+HuCY8fMV1s khMw== 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=h0j1tmcryfszvpEO+TtDzb67ntXXnPtNryRzQx2IqTE=; b=nfd+ujI/Br4P1AjunU9ljQ1R3ObKAoDN4m/iwiTH2rjJ/yyd021cbYBDdWV3noIGkE nbeQpZ3GG0mbwBei8tkSlOrrxuztxOYEVR34sSSHU42XK98AJZ8mo23Uze9muIXGJnJf t5Lg7Uzb/GYqHZERw5z3BwsHY0GlnXPllYTpfnEfkukCHmnrAOrCEfRMHcsNbSs5DcgD xqjhBNkDtegL+YTxLrm+cVH7Rc7x8rZTzEI6SsAgj3tu+LOFGSSBssLAalpHDnXr8ZDg KIIXmITc29V3cLFAICCHLxdPJvGQGtLvhD2USu1OmilrkMhPDSS31bb8yG7SpOlLUI4p UeWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pqXoU2+L; 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 l9-20020a170903120900b0017298291826si21589767plh.459.2022.08.24.16.34.35; Wed, 24 Aug 2022 16:34:48 -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=pqXoU2+L; 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 S229555AbiHXXbw (ORCPT + 99 others); Wed, 24 Aug 2022 19:31:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230153AbiHXXbj (ORCPT ); Wed, 24 Aug 2022 19:31:39 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6147985AA8 for ; Wed, 24 Aug 2022 16:31:30 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-3328a211611so312908367b3.5 for ; Wed, 24 Aug 2022 16:31:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=h0j1tmcryfszvpEO+TtDzb67ntXXnPtNryRzQx2IqTE=; b=pqXoU2+LQkTmeTP/rYWY79xTXLQlMwsi5Hq578vTl3lC8cOG2lR1qdoIKE3Wc+r9EL yYOJFB+RUR9dI3J7eIcuxLNwvYVvrwDyN8w5jpXfX1pEofRBqrrJHA9PoldIfG4qe8pK m+IGq1Rr67YLwC9pNdWt/mxzFRadDRA8lebpDItV+lsrtSSiuQmagcDGAW0usuqDmTNd 3hai+OcAN66+I3+/lkJaz1xChm7qMZSCVW/WodUj/7vB2oGnqrO9BBwglLNEB00pGwfY IR7YmOxCW/iuLgTu6g1XMkh+AZUPRUTknV/d4ws9CKAh25wd0NNQjg66GqvKGWZ7EdJG uSQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=h0j1tmcryfszvpEO+TtDzb67ntXXnPtNryRzQx2IqTE=; b=GeV9aJ8IeeR1gluzD3tAMXZoEja7foN07L1BbDXMyqKtJme+QfpWmDqcd8il+H22Gm doWKzGNcnEwA7ZKDucTRrMsDascUkLfdBMy4SpVRDCUO5FaydlwB0aXva4YpkXiPUYfC f+lL/b9duhkvP8gch4kodiraFcTviNjO2ADqIvVpI+Q+0u0cFL6q2Dks9WVqDEauGH32 6vt1nWtgnmvFuShCetrG+vgBmh1V4oPsQ9VA4/HvFk/IpWeNOI3YwCcxO1dgymeTEgzM SfB6Iq2NYqKLlby1x9dqAYry7hrQYVG24t4SZ59UTKyt7Wp2pnAIwaVKW86ayfEMfO7O vy6w== X-Gm-Message-State: ACgBeo0EEA7hEaOdstazLzmcEaBRQJyt9zrg3QHhSANwnywRJyyaRecm pG8B34ByeKXqbvMbcI7X3fxZQHvHkP5j93nuGaDF4EdH7m0kj6OG9PBoTsMhuAfmePjLMDJ2WlQ AryKfncsYPbUekR8QIFDdu3CPsK+cXPxdcvAK0Mg7xii2vYIpux69udeh2Ig2V/97kX6W/A== X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:9854:5a1:b9e2:6545]) (user=haoluo job=sendgmr) by 2002:a25:9d91:0:b0:67b:cd9f:ef88 with SMTP id v17-20020a259d91000000b0067bcd9fef88mr1372574ybp.82.1661383889272; Wed, 24 Aug 2022 16:31:29 -0700 (PDT) Date: Wed, 24 Aug 2022 16:31:15 -0700 In-Reply-To: <20220824233117.1312810-1-haoluo@google.com> Message-Id: <20220824233117.1312810-4-haoluo@google.com> Mime-Version: 1.0 References: <20220824233117.1312810-1-haoluo@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [RESEND PATCH bpf-next v9 3/5] 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 , John Fastabend , Jiri Olsa , Michal Koutny , Roman Gushchin , David Rientjes , Stanislav Fomichev , Shakeel Butt , Yosry Ahmed , Hao Luo 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=unavailable 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 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 feb59380c896..793ecff29038 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -3,6 +3,10 @@ #include +#include +#include +#include + static DEFINE_SPINLOCK(cgroup_rstat_lock); static DEFINE_PER_CPU(raw_spinlock_t, cgroup_rstat_cpu_lock); @@ -141,6 +145,31 @@ 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. + * + * __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 *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, @@ -501,3 +531,21 @@ void cgroup_base_stat_cputime_show(struct seq_file *seq) seq_printf(seq, "core_sched.force_idle_usec %llu\n", forceidle_time); #endif } + +/* 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 = { + .owner = THIS_MODULE, + .set = &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); -- 2.37.1.595.g718a3a8f04-goog