From patchwork Fri Jul 22 02:13:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 12925895 X-Patchwork-Delegate: bpf@iogearbox.net 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 C8E18C433EF for ; Fri, 22 Jul 2022 02:13:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234114AbiGVCNl (ORCPT ); Thu, 21 Jul 2022 22:13:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234039AbiGVCNc (ORCPT ); Thu, 21 Jul 2022 22:13:32 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F08A797A1A for ; Thu, 21 Jul 2022 19:13:26 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id iq18-20020a17090afb5200b001ef966d1febso1626102pjb.5 for ; Thu, 21 Jul 2022 19:13:26 -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=tLU3GevE7EMq9svQBvmyRhnadL4Y+2gn2LprvT/4YYk=; b=WJ8665xaHjsgIdkrbSgYU3Vo7MtIiyG2UO4Za0C9TzTlUp7Aq7oAziFEULIp7hjp+Z DJ+t4zTWp0z5UXInnWyoxHHqxnN8yPduXx5pW9ds2FjqUnTEcbABw+LGgxrrAPMcN1eV z+MojCuD3/i7a0HJVN8Bq4eSfU8qu0zTBHPD3oCbCfVpVf65C+/xi6wlqxj1cEmBCdk3 tMnyUSeHBrO2pQI90BwTyb4CUoCH8hvytQixa4psc0lLVTfdmU+qRCYYysor8bdLZjb7 np/mK2MgbacYiWCOJiW3UOBBI/V/N4FwTd4baweLMkL+N9MOykrVLgcFTaV0XFvuapzF 6dcw== 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=tLU3GevE7EMq9svQBvmyRhnadL4Y+2gn2LprvT/4YYk=; b=pIsxKT7sUy/U+etEG8eE5DHXpsnWY7hBSjsJ07rRRsz21dwnL998/3wfktM4peTtu4 Jtixy5T27LSLXuOg7+qfG3addHU6V4Qur+fFwqKh/rq9I6J7I+ASmGtc25r07Plq3CCr dh2t0pZYFL97Ch895GXOg4bGSYrdojg8s1qYxE0lV5dgZNT95m7HaYZ54JQ4KercxFbq Luu5hpitsP+NS6Hotwt358jtCvaaY4wsnM1NY9bYtcxXNloxMMdwm3eUSCGPI1H/0RcW LKUzNXQPfS3kCiP26lcVdLtYEBnlA5Wk8QmJjZMkl7EOqBznrkJ/VejfUuX+/cp4VLMS RVDQ== X-Gm-Message-State: AJIora8xdE2HXcMOQ/jEVyFdFtJPAPOjEyDI3A1tI+j5kd3gvKrsuvFK lIFYuU5KBatzHd9eVZ6kw9dud5QUL5moXzG5 X-Google-Smtp-Source: AGRyM1vDH6ZHqKhC4Dn/p1vre782Xqt/F67hZF/QuRKkjVTK5IJ5MXiMaBF1V3RKD0YfTdT5LWpuKzr2fAZnSakt X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a62:b60d:0:b0:528:99aa:da09 with SMTP id j13-20020a62b60d000000b0052899aada09mr1137449pff.86.1658456005918; Thu, 21 Jul 2022 19:13:25 -0700 (PDT) Date: Fri, 22 Jul 2022 02:13:11 +0000 In-Reply-To: <20220722021313.3150035-1-yosryahmed@google.com> Message-Id: <20220722021313.3150035-7-yosryahmed@google.com> Mime-Version: 1.0 References: <20220722021313.3150035-1-yosryahmed@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH bpf-next v4 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 , Hao Luo , Tejun Heo , Zefan Li Cc: Johannes Weiner , Shuah Khan , Michal Hocko , KP Singh , Benjamin Tissoires , John Fastabend , " =?utf-8?q?Michal_Koutn=C3=BD?= " , 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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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 | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index 24b5c2ab5598..0f87c31eecc2 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,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 +198,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 +500,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);