Received: by 2002:a05:6504:5087:b0:1e5:d399:13c4 with SMTP id i7csp6050ltq; Fri, 30 Sep 2022 16:36:06 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6udFtJGm2FH2KHsCtV59W5Ebd3FTS/zOTnLtQ5SVgHA5K7vwxE8cgRYVJVv1fE35zbzOBS X-Received: by 2002:a17:902:da82:b0:178:38ee:70b with SMTP id j2-20020a170902da8200b0017838ee070bmr11205589plx.85.1664580966564; Fri, 30 Sep 2022 16:36:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664580966; cv=none; d=google.com; s=arc-20160816; b=L00Rma0XaOIq1NbvvRTEei7c1iejd1hQV+fopvPm5M5jtr+ESIsdW801m5ceDt0stg bltr//7q3qw7RFy4s/JV0drbUyQiQHt00JxGPOoAwYnedRkLOewJt0KawxmAq2OpVlLm dYNGD27pTYyfhrLpyDZ+Gf7+TzK2VQYaJq1PbGSLm9f0J8siDyZj3LrhQuvSO4f3G/Ra rnloj3p0eVn+tZsuoqsTLzebP4f33pDUewDGXweKXbSaPPZfmKHiYLO4z7miwL2Tdj/V w4QU/g+MBfQtajWivYRrGwXP+ECV7f+7QOwZMFBEUI7uiyDvXDkdADd+Lj5i94+Ama+O vctA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=rLr4Pui1WgAmc20FBE5q6rikXP7BZ6KUet36h8qjGrQ=; b=Yh4/LlD3+0vZTmwDGYm0jFMRfCcjVZtnpe6ZIFZZsR+VEQ8ThamoPagZ0nTkgbH208 NO+Sae35YYYKNngx6PHahRxjLmNzPNtnmgVtwdLwmZRQTlB6bVcwP5f9s4/REpuffipm JqyexvE7AHji2ZVA5RXeTpyUrHGflwsSOocPyfqCJJmcriVOIMJDzPy6RFl8rXrWVQ9l orgfjGM/v61z0x4YQk7KX/LJdeQG2UMk6fkIWQKQh8TiTPxPkRmnKYG3aIo4RlKRpp1l J39E82wFlcLzn7ieZdI72sdVzD6GJIwIwcmsC+ondO+htJ13HR3+IqyFx73EKDmdC2en 4nYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="j/f7sU21"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q9-20020a17090311c900b0017336c464ecsi2207218plh.407.2022.09.30.16.35.33; Fri, 30 Sep 2022 16:36:06 -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=@gmail.com header.s=20210112 header.b="j/f7sU21"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232763AbiI3Wsz (ORCPT + 99 others); Fri, 30 Sep 2022 18:48:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232421AbiI3Wsw (ORCPT ); Fri, 30 Sep 2022 18:48:52 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9095208540; Fri, 30 Sep 2022 15:48:50 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id a41so7739111edf.4; Fri, 30 Sep 2022 15:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=rLr4Pui1WgAmc20FBE5q6rikXP7BZ6KUet36h8qjGrQ=; b=j/f7sU21OUk8kPvxztuKT5RBnJwNT0hXLpzsQ2xrK8zgdjoKihE425h6cOYq7h8jhD BmxZbSm/gW8kVMpISmmD6SmOhUiguGYs3Byys1w0Zy/P5QefmjDN12gsnBW6Uu9rErAV 2DQ9bm7dg3CJ0lrxl4Ve6NJKftG/Xhn9/KRS3DDC67P7MX3DpgTnenITb5tI0RQ5xZyy oR4A5OHpMMIMgU+H/r23k4CLd2+TsRJJSmrgQYH+3ep+SOIkv2qjR8KU+cAA9kahCs3U gZmgCXIfFNHDAMFICPmTEmNcXOPnX4gUI30gKgEFSVPw/z2AA+hk8rPtCHbY8RqZi+cq 0w/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rLr4Pui1WgAmc20FBE5q6rikXP7BZ6KUet36h8qjGrQ=; b=vJH3malLD4HZ4s9+ziFpMIS7C45I6kUV60JBR2jzwsC69uJz+bxxIDMKGNeekbYMsB vykOxzCgc/ERhChs5uC0kq/BPQJSXGHm5VPrI6YhZc4BOCAWlY5y+pbsh+SPqWQ5y4d3 6IYPqNCeK6Sp40cqKnYa/UAHyWQtrU4QAlgYjqkYD49sAUGjl7bq9V2ARyfV5pPUEZsR DfVrBnMGVJQ7V8Py4qUbztKugcImQAFD5mXALd8lVFuxDHUBpgA2MurDaXXXL1ugpoxH cnnUl3HWXCwrj9RnMVYhK+C7oKmnn2z1nRTwxsKVaCjNc3lv3Fgvq3OxiXFaIQA+f89n uVVw== X-Gm-Message-State: ACrzQf2mpOYKIIVyjLLZEVCWe3knOulgjk6sqqqJ16BxjCLErRBi4qIu Czi6pIaO1jQYcqGoSFybjdVgx65ym2oBpnuNF/s= X-Received: by 2002:a05:6402:518e:b0:452:49bc:179f with SMTP id q14-20020a056402518e00b0045249bc179fmr9659877edd.224.1664578129257; Fri, 30 Sep 2022 15:48:49 -0700 (PDT) MIME-Version: 1.0 References: <20220922041435.709119-1-namhyung@kernel.org> In-Reply-To: <20220922041435.709119-1-namhyung@kernel.org> From: Andrii Nakryiko Date: Fri, 30 Sep 2022 15:48:37 -0700 Message-ID: Subject: Re: [PATCH] perf stat: Support old kernels for bperf cgroup counting To: Namhyung Kim Cc: Tejun Heo , Zefan Li , Johannes Weiner , cgroups@vger.kernel.org, Arnaldo Carvalho de Melo , Jiri Olsa , LKML , linux-perf-users@vger.kernel.org, Song Liu , bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On Wed, Sep 21, 2022 at 9:21 PM Namhyung Kim wrote: > > The recent change in the cgroup will break the backward compatiblity in > the BPF program. It should support both old and new kernels using BPF > CO-RE technique. > > Like the task_struct->__state handling in the offcpu analysis, we can > check the field name in the cgroup struct. > > Signed-off-by: Namhyung Kim > --- > Arnaldo, I think this should go through the cgroup tree since it depends > on the earlier change there. I don't think it'd conflict with other > perf changes but please let me know if you see any trouble, thanks! > > tools/perf/util/bpf_skel/bperf_cgroup.bpf.c | 29 ++++++++++++++++++++- > 1 file changed, 28 insertions(+), 1 deletion(-) > > diff --git a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c > index 488bd398f01d..4fe61043de04 100644 > --- a/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c > +++ b/tools/perf/util/bpf_skel/bperf_cgroup.bpf.c > @@ -43,12 +43,39 @@ struct { > __uint(value_size, sizeof(struct bpf_perf_event_value)); > } cgrp_readings SEC(".maps"); > > +/* new kernel cgroup definition */ > +struct cgroup___new { > + int level; > + struct cgroup *ancestors[]; > +} __attribute__((preserve_access_index)); > + > +/* old kernel cgroup definition */ > +struct cgroup___old { > + int level; > + u64 ancestor_ids[]; > +} __attribute__((preserve_access_index)); > + > const volatile __u32 num_events = 1; > const volatile __u32 num_cpus = 1; > > int enabled = 0; > int use_cgroup_v2 = 0; > > +static inline __u64 get_cgroup_v1_ancestor_id(struct cgroup *cgrp, int level) > +{ > + /* recast pointer to capture new type for compiler */ > + struct cgroup___new *cgrp_new = (void *)cgrp; > + > + if (bpf_core_field_exists(cgrp_new->ancestors)) { > + return BPF_CORE_READ(cgrp_new, ancestors[level], kn, id); have you checked generated BPF code for this ancestors[level] access? I'd expect CO-RE relocation for finding ancestors offset and then just normal + level * 8 arithmetic, but would be nice to confirm. Apart from this, looks good to me: Acked-by: Andrii Nakryiko > + } else { > + /* recast pointer to capture old type for compiler */ > + struct cgroup___old *cgrp_old = (void *)cgrp; > + > + return BPF_CORE_READ(cgrp_old, ancestor_ids[level]); > + } > +} > + > static inline int get_cgroup_v1_idx(__u32 *cgrps, int size) > { > struct task_struct *p = (void *)bpf_get_current_task(); > @@ -70,7 +97,7 @@ static inline int get_cgroup_v1_idx(__u32 *cgrps, int size) > break; > > // convert cgroup-id to a map index > - cgrp_id = BPF_CORE_READ(cgrp, ancestors[i], kn, id); > + cgrp_id = get_cgroup_v1_ancestor_id(cgrp, i); > elem = bpf_map_lookup_elem(&cgrp_idx, &cgrp_id); > if (!elem) > continue; > -- > 2.37.3.968.ga6b4b080e4-goog >