Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp104505ioo; Fri, 20 May 2022 15:17:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOiswG1BSvDrJCdQw88lzhLbobA5ZOww1+w6gr/ALGRl5jhaP6m7G8s4h901rHFc7z3ZSr X-Received: by 2002:a17:907:a40d:b0:6f5:1611:c55e with SMTP id sg13-20020a170907a40d00b006f51611c55emr10531189ejc.479.1653085029915; Fri, 20 May 2022 15:17:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653085029; cv=none; d=google.com; s=arc-20160816; b=o0KfJYpZnMN3kcngOqC6n7Mdak1WF42fsRdqr6B1t0UN1DagcIJ5fyG46vEmFOGU0Z vAaAgJT5UL3j3BWbidqoQ/pRGFPXID7Nj0DTQN6iclrNKLfsDIRiArJT3kVCl4bENUH2 w4kVaQl7mhldzIeS3BMWzlvJFSh7UKBaipsPoacKYsO6XBJ6x0u+z5qHMZHqNxXiFEMv NmTrwbghzO4tTccelulbSCRjqrL3S2lwLEEfZfE//KV23RhDETl1rAgczQ3zJp8xvYEJ 04Qpaubu7Sxb/egcdB5bYmIWNHKzpgUeF85eTcgmOlCUjL0kfwThmVhAQmhbWcWcjLPr Sfgg== 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:mime-version:message-id:date :dkim-signature; bh=ysbiIUs5o2z/FDHftPdhza+ymeMilu85Me2zCte6Iv0=; b=M+xfCJu58CeIymUra3fCCSW+iDDiThxWq99na4OB9YN+vqPHArgRX5aF6Kyv7CsMyu FgBoW29K6JZZqxXW3PODk6OD9bpTa2EXnfpa/jd29PJCmjTQ2UxNyKx6+JjjeQ7b7XyV PRa4pWIqPOwb6awHoC+NIV4+fAEQJhUad2YEsSa1+XRdCwtsmi7qqcH+273INbIFsTF4 8drgAboRNc2CW856nvjAydRB8afVd6dShtRn6ljPtNe+VzcA39pygg1sCdpui6mn9bAW +vEukxLNShvpjNAhydKRAxOFIjJo1DafmpoaikG7SYXN/HavNRocOjgblPY05j6a7Q5k 6tDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="RXxHWd/Y"; 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 ej27-20020a056402369b00b0042b303fea16si2774472edb.50.2022.05.20.15.16.43; Fri, 20 May 2022 15:17:09 -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="RXxHWd/Y"; 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 S1344406AbiETBVm (ORCPT + 99 others); Thu, 19 May 2022 21:21:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344342AbiETBVj (ORCPT ); Thu, 19 May 2022 21:21:39 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8084927CC6 for ; Thu, 19 May 2022 18:21:38 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id cd6-20020a056a00420600b00510a99055e2so3457269pfb.17 for ; Thu, 19 May 2022 18:21:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=ysbiIUs5o2z/FDHftPdhza+ymeMilu85Me2zCte6Iv0=; b=RXxHWd/Y2k75L32XGdjIlD+Qco0V4mN2pXDBsLSot84ETRvW7QLrnjn12Egie/vtkO UyzP9yoAVnSTwdozqLuRPetCkzaknxUbUh/R5YU9r80HXFtDMtHmUCyJrfEOgjwyY9Zu yxpNQVABpbIXtuaBqaBfi/7WOUwjrkLQ58VvJMSHCjUvrSL3bQg0oLa1qk7hrYcUCnwV po5mgzouqPsmVAtbazRDZpYSpzhrOexeN8BcWodNhjxsmEbiznoM+pIdylPrKGtclNuz faxUrnWRmJK9EhT2j1XvnPDPjZRv7PAX2RjM4alvUCqx4o0Zq8xpwSL2LZtMUX4fzF9v WE5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=ysbiIUs5o2z/FDHftPdhza+ymeMilu85Me2zCte6Iv0=; b=TK9xpYv+w8W+jigrvz13ryX3Z93N4wgRNoL8h6G68d/ZXPUZ/qsycKmf8yz+oPFU1S AihqVwmUcyjRVeo2shDKTZBGPfw6XRW9ssvUik7h3Jo+4veASMYkvZyZ6W9r7fRkjVTq QkxpgRg7DOPaMZnqPU2z7uvI3fqZ9kOFGgqxpWXnevBScWTwxQCjlCZRr71M1Onltcr+ Sn4cyBAD4TH0Ub6LoIT6oAmKcMjmfnMFYiKN3XbBaSVyRb5jKtf7bniG5+pQH4fLsVRN 4FjxE0t85UDqIzuSxIwsMpf4n8A5FTA+E26vPdkp7Xo13DJoIemkIYkxjH/RhAHKhX8u fpdQ== X-Gm-Message-State: AOAM533H82y4qm4bifBuYVmVqUe4ELXXcCssCllupcJsC09GUyqLd94P DurGfhhdt1T0aAv8ePuJZR45GXLRnor0Wfzt X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a63:2048:0:b0:3db:7de7:34b4 with SMTP id r8-20020a632048000000b003db7de734b4mr6387103pgm.105.1653009697865; Thu, 19 May 2022 18:21:37 -0700 (PDT) Date: Fri, 20 May 2022 01:21:28 +0000 Message-Id: <20220520012133.1217211-1-yosryahmed@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH bpf-next v1 0/5] bpf: rstat: cgroup hierarchical stats 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 , Roman Gushchin , Michal Hocko Cc: Stanislav Fomichev , David Rientjes , 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 This patch series allows for using bpf to collect hierarchical cgroup stats efficiently by integrating with the rstat framework. The rstat framework provides an efficient way to collect cgroup stats and propagate them through the cgroup hierarchy. * Background on rstat (I am using a subscriber analogy that is not commonly used): The rstat framework maintains a tree of cgroups that have updates and which cpus have updates. A subscriber to the rstat framework maintains their own stats. The framework is used to tell the subscriber when and what to flush, for the most efficient stats propagation. The workflow is as follows: - When a subscriber updates a cgroup on a cpu, it informs the rstat framework by calling cgroup_rstat_updated(cgrp, cpu). - When a subscriber wants to read some stats for a cgroup, it asks the rstat framework to initiate a stats flush (propagation) by calling cgroup_rstat_flush(cgrp). - When the rstat framework initiates a flush, it makes callbacks to subscribers to aggregate stats on cpus that have updates, and propagate updates to their parent. Currently, the main subscribers to the rstat framework are cgroup subsystems (e.g. memory, block). This patch series allow bpf programs to become subscribers as well. Patches in this series are based off two patches in the mailing list: - bpf/btf: also allow kfunc in tracing and syscall programs - btf: Add a new kfunc set which allows to mark a function to be sleepable Both by Benjamin Tissoires, from different versions of his HID patch series (the second patch seems to have been dropped in the last version). Patches in this series are organized as follows: * The first patch adds a hook point, bpf_rstat_flush(), that is called during rstat flushing. This allows bpf fentry programs to attach to it to be called during rstat flushing (effectively registering themselves as rstat flush callbacks). * The second patch adds cgroup_rstat_updated() and cgorup_rstat_flush() kfuncs, to allow bpf stat collectors and readers to communicate with rstat. * The third patch is actually v2 of a previously submitted patch [1] by Hao Luo. We agreed that it fits better as a part of this series. It introduces cgroup_iter programs that can dump stats for cgroups to userspace. v1 - > v2: - Getting the cgroup's reference at the time at attaching, instead of at the time when iterating. (Yonghong) (context [1]) - Remove .init_seq_private and .fini_seq_private callbacks for cgroup_iter. They are not needed now. (Yonghong) * The fourth patch extends bpf selftests cgroup helpers, as necessary for the following patch. * The fifth patch is a selftest that demonstrates the entire workflow. It includes programs that collect, aggregate, and dump per-cgroup stats by fully integrating with the rstat framework. [1]https://lore.kernel.org/lkml/20220225234339.2386398-9-haoluo@google.com/ RFC v2 -> v1: - Instead of introducing a new program type for rstat flushing, add an empty hook point, bpf_rstat_flush(), and use fentry bpf programs to attach to it and flush bpf stats. - Instead of using helpers, use kfuncs for rstat functions. - These changes simplify the patchset greatly, with minimal changes to uapi. RFC v1 -> RFC v2: - Instead of rstat flush programs attach to subsystems, they now attach to rstat (global flushers, not per-subsystem), based on discussions with Tejun. The first patch is entirely rewritten. - Pass cgroup pointers to rstat flushers instead of cgroup ids. This is much more flexibility and less likely to need a uapi update later. - rstat helpers are now only defined if CGROUP_CONFIG. - Most of the code is now only defined if CGROUP_CONFIG and CONFIG_BPF_SYSCALL. - Move rstat helper protos from bpf_base_func_proto() to tracing_prog_func_proto(). - rstat helpers argument (cgroup pointer) is now ARG_PTR_TO_BTF_ID, not ARG_ANYTHING. - Rewrote the selftest to use the cgroup helpers. - Dropped bpf_map_lookup_percpu_elem (already added by Feng). - Dropped patch to support cgroup v1 for cgroup_iter. - Dropped patch to define some cgroup_put() when !CONFIG_CGROUP. The code that calls it is no longer compiled when !CONFIG_CGROUP. Hao Luo (1): bpf: Introduce cgroup iter Yosry Ahmed (4): cgroup: bpf: add a hook for bpf progs to attach to rstat flushing cgroup: bpf: add cgroup_rstat_updated() and cgroup_rstat_flush() kfuncs selftests/bpf: extend cgroup helpers bpf: add a selftest for cgroup hierarchical stats collection include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 6 + kernel/bpf/Makefile | 3 + kernel/bpf/cgroup_iter.c | 148 ++++++++ kernel/cgroup/rstat.c | 40 +++ tools/include/uapi/linux/bpf.h | 6 + tools/testing/selftests/bpf/cgroup_helpers.c | 159 +++++--- tools/testing/selftests/bpf/cgroup_helpers.h | 14 +- .../test_cgroup_hierarchical_stats.c | 339 ++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 + .../selftests/bpf/progs/cgroup_vmscan.c | 221 ++++++++++++ 11 files changed, 899 insertions(+), 46 deletions(-) create mode 100644 kernel/bpf/cgroup_iter.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_cgroup_hierarchical_stats.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_vmscan.c -- 2.36.1.124.g0e6072fb45-goog