Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp2170014rwb; Fri, 5 Aug 2022 14:53:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR5lqaYZmECGMQzLUXAs7ayKRKHhy/7Yd8X8Vxgq5bqdN/io9poY4BPhgBNRy7z6wyaHBAyw X-Received: by 2002:a17:907:272a:b0:72b:8cd9:9ddd with SMTP id d10-20020a170907272a00b0072b8cd99dddmr6449551ejl.299.1659736415641; Fri, 05 Aug 2022 14:53:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659736415; cv=none; d=google.com; s=arc-20160816; b=a/ZbFOGqxvyLEH6lG6QaRZlq/OSMW4giVFz1Rq9fU1vUSg2sichOyc9Vtj2neOhkUy AgSxCzxJ/5NjCFZ3kpNlZ+l6U5khXYvn6PTDJ1LArfjpQmQftxxY2Qi9r2aESWsxf4DU LiKbPMNFsrFCPemSLdc0heVnnHPS85jTK0KRFQWoSgIlK+Xh0oLsbbRgtOp4uynKSrUO mDZWpBywG1B/EX1OpD6feBIqUc5SLEKO0L2Gr1bIJIdRDpym9dgR2wm3dLzkX3fD1/aQ cUT81moY9AesRob2yh7WFNd5lfb0sIQxxdWn0egfF2x9n/y1BBAu2Mx+LgwQ2v8M7fVQ vk4g== 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=C+Iy4ZCmB+ISavp/KSQWytbBtP5lX7pymE33Bes2CKQ=; b=rMgTlFlZgSTX6J6L1+66XhHu606BrjT179wGWgy/VzqwZveKQsvLzk1cMH/eQmxua6 CS7Y0wZllseq+3P56wWzfEb9NnV9UTpp1Dtg6qTmHgBRH17OvYzsVza7n+87cjw696dq 9k+MEb4OooISwbCBjH9JhZKdqxDaYu4JITldzruwFZojPBj279ugER9T6CLDNJaihOfc smMcQXm2hGRJqRNv5sRwr9C0dwBA+ZA20d1yJP8+XKHvrkZkGypxBwJJ3VVF0PbSHpdd FS5XZbc5OqWJ/47IV6l8OpoCHn3VtUNztiCjGLxm5KJvDnW6KaMKFoEdgOshsrcZwpWj 7OLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=eWABbD0w; 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 qq17-20020a17090720d100b007305bbe840esi2584893ejb.765.2022.08.05.14.53.09; Fri, 05 Aug 2022 14:53:35 -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=eWABbD0w; 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 S241428AbiHEVuB (ORCPT + 99 others); Fri, 5 Aug 2022 17:50:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241474AbiHEVtU (ORCPT ); Fri, 5 Aug 2022 17:49:20 -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 876BF7C196 for ; Fri, 5 Aug 2022 14:49:08 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-324989683fdso31185147b3.12 for ; Fri, 05 Aug 2022 14:49:08 -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=C+Iy4ZCmB+ISavp/KSQWytbBtP5lX7pymE33Bes2CKQ=; b=eWABbD0wEBZOZlr5Zxc+y9xa0WYiJXB933g0IcK7klW7bckEWUQPNrAZdSP/zvSvLW /qqJwW4ApuiEKDYMyCizboSkhJO/G8E5H34PUto5S7x3QiRM7LeykuPS717hUiNMlw3F 8r6tys6QW4axOrirrPb6gD9/hfGMIs8j71MmyXNV20cWpt0tECW0xZ1Mbuu0et56o4AT FCHHVTjnNtJv5MgzzJ19ooCuZ+gP/OCrTza6AOW27h+awBfK1IN/zpIEkHbGIasb9shA aJFaEGmjdqAUIFol8na6n0wGw8Hv+jJuYmOq4+c9qkUFUz4H9Z/Fsc55cr/6Gzk6peRQ V1fA== 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=C+Iy4ZCmB+ISavp/KSQWytbBtP5lX7pymE33Bes2CKQ=; b=dyQUOXUoaTDookvP7EWWaUthcaUdQoBkuWt8c2vl9Ur2JnhqqhhhqeaUKH+n3ZZi9R Sp4l7lQdghj/7sC3gg2K4it2hZQepYI39mDQudoQqIhhKerRKtsH9tTevxf16JIp8eAN Hwzz2udnJlpb893LShY0o4nK8BiEj2sgBmAIi+qsNxoHJImSat469A+QeFW/cbQjFXan WNcimmDIKgOjlWBcXrDLqzX1EJ2mKpkGKexObatDhyQ7B7JambFQH7vJTykSpNGJ9v3I +qnvDIbkSdCLeTsOOKVpFEyiWY6VcdgbDR5R/oGIJVi0LWiVoPfKsQy8DxiC9BG8PROe 6wrg== X-Gm-Message-State: ACgBeo1lGFy2vtglIDV4N+NDg1bpVewxf0hjyuY02MQXSDoJh1VBeNQa nC7sdlg6lu2/uTyIUHY0pmNjIul0ZcU8twy/2/qqzj4EI9otl4s5014RxVpB7lB8XV30cSlbGqL WKALbStddqmAqZnvqnl5jT+uLgpv48DUdOLiCGVwy9DHMcBJb+w4/MMJZHsUXqe/SGJjvEg== X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:4f27:97db:8644:dc82]) (user=haoluo job=sendgmr) by 2002:a25:b9d0:0:b0:67a:6b9a:ef84 with SMTP id y16-20020a25b9d0000000b0067a6b9aef84mr6860219ybj.282.1659736147449; Fri, 05 Aug 2022 14:49:07 -0700 (PDT) Date: Fri, 5 Aug 2022 14:48:19 -0700 In-Reply-To: <20220805214821.1058337-1-haoluo@google.com> Message-Id: <20220805214821.1058337-7-haoluo@google.com> Mime-Version: 1.0 References: <20220805214821.1058337-1-haoluo@google.com> X-Mailer: git-send-email 2.37.1.559.g78731f0fdb-goog Subject: [PATCH bpf-next v7 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 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 24b5c2ab5598..3289f6e0d306 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, @@ -469,3 +499,21 @@ 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_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.559.g78731f0fdb-goog