Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp130506rwe; Tue, 23 Aug 2022 20:16:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR4pcUmIzTS4sUy/4zDNGT2yZCFq+0KuTWOGpyGwrjK8zohEddpfXxf3krqPPAO6bfjmbXcs X-Received: by 2002:aa7:8430:0:b0:536:5173:a2c6 with SMTP id q16-20020aa78430000000b005365173a2c6mr17930261pfn.4.1661310959910; Tue, 23 Aug 2022 20:15:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661310959; cv=none; d=google.com; s=arc-20160816; b=ek85FsIs8jBbbN+MyA5dTVbouM0NJ1ZavWE79fMYJlYWgC2NDWOao+zKek97coELPs MKr6lE763OIQKgn9U+eBnYMvQn9NhAhhrqN80y1YssKx6yZfM2nh6yTKg0abkoYPRSoh bPqnXmrS+qCOWLEJABeet1/GRGuI/T5c+HKS6KR5/uZ12kwjRdlwOO+HxY0qyL9zWi4U 82OBj5hr+QPSv1SNnb3FRJJNxoRnuBrF3gRydEJBqxd/MlDLguxapU8t2bQPUqQwI8vb nKckxiJtkyuPi4mUPrxyZAoRkXtbsubILO9Ilwnx6Qcs6rLusvYs9+iBFRqgHGDnUNEj rcfQ== 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=uOKgnUMeTaJ0OG1/9zQcrToGBIezaHCLMpKtu10WwTUbqSswBSHQSeo8xCMyKRJIA5 1K46uUx7LgfooSpFFmX/e/YSpdlOgZ745buZJUieWqlHynEcIzOoG1sKqDoniCyYOYOS VxF2QsyFfIgnsFPsFFIvWR312Y2HcbE62KoexgZfIrAstO4LN757wJ3ufv2MVxzwzM7x 0EuN++/1w5DmYLEw1iGBY8ts4/uRHczkSfmlkrdpvLTCZQK0IYohIgFo/3Zc7n6iFV3u G0kNpyR3Zc2pNJIQMefrEjRdbvuac5v6T9ZsjDt/Gh+UAObzIK5sa6BcUdPMUXJHVYol TWLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nZpScWeB; 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 r22-20020a170902be1600b00172cde9be63si2409483pls.127.2022.08.23.20.15.48; Tue, 23 Aug 2022 20:15:59 -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=nZpScWeB; 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 S234138AbiHXDCE (ORCPT + 99 others); Tue, 23 Aug 2022 23:02:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232735AbiHXDBp (ORCPT ); Tue, 23 Aug 2022 23:01:45 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62A257DF5B for ; Tue, 23 Aug 2022 20:01:11 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-337ed9110c2so227350437b3.15 for ; Tue, 23 Aug 2022 20:01:11 -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=nZpScWeBj5Y7oVtwP7UXuKRE97XwsaQ9gMJ3OYVHSEjml+RkUW7kdC2BC03LA1Pr10 UT0m2RfXsKATgs7cqw7BawdfyYXbN3iPevPeTNGjop9F7VN3VsPFOhQLNHgoyH88028a 5vMyWQqjcS9QIAanZMKJBNJXfy3kNYnjO+nepaMAPwPSafWQQKX49b6KG3lG7mcjlq0d emOJOlYctXQRTcCwqJaZNCDryE0EMINQsbMYiajZFy36myU9wznhEAzVZcuX+8N64o2y bYuZYWPXYeX9SlsWcKauGRyibb5E3L+GcAdb0T/0QcafLuVPZiK5tdDQz2Iqpx1Jvmbx g1gg== 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=6/AnvJRtMchcvKQ8eg9CkXNYFBD8uc59x52ytIEqPsiM6l4PM/1ZWGVDLSzHDh4ivh nrMA91bNmXK8MsXF2FmnAe9Ul10hNc/ABBOk1qrrwn/4W6zkqDcrcNzUOcF4617EDeym zaiCUnoVGDf1lroP0HbHubjNHd+KgZPPNc7jZKnDzd7WVVVgFF7dITI/jhvazKXrY5wY kpEg0yt8LAi8WaTvlQfrGGyiIgBjT85f2Bk1CEN2eUlipT5TrUyOU48c6P5MA3D/aGe1 TMQiRproKPoNE+H/DTqJRKre+eAM6pQRrtUC45p8CIjUFh7CLAzIEIudxSFPagzVB3OP rNEw== X-Gm-Message-State: ACgBeo2cJFW3W9WkkbhVuFt4tJ4SF9HIn59QxTvv/NfpweWjGqjbbRJJ 6s8H87GwxQ6vLuoJPCeTsymgfE2oot7avpsKwdHO7awuRvCcd2e8hLtr+RzbRuzwMcZnGC0+Q/s iqhrr4WuN9M9r5T0qodhZK0jLAZ6Q2AybFXTuT3aVteuJ6CNStnjrA5frF8IKt8pzSaCsNQ== X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:87e3:473b:a297:1247]) (user=haoluo job=sendgmr) by 2002:a0d:f282:0:b0:329:7da1:90e8 with SMTP id b124-20020a0df282000000b003297da190e8mr29139161ywf.519.1661310070509; Tue, 23 Aug 2022 20:01:10 -0700 (PDT) Date: Tue, 23 Aug 2022 20:00:29 -0700 In-Reply-To: <20220824030031.1013441-1-haoluo@google.com> Message-Id: <20220824030031.1013441-4-haoluo@google.com> Mime-Version: 1.0 References: <20220824030031.1013441-1-haoluo@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [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