Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4887420ioa; Wed, 27 Apr 2022 13:31:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxlg9mU81U8XvVMKxO7MOxu23YiYsqawtRx9tvJvFw26OG+AwZAHMhVJp0iPrPvuOLhgX6 X-Received: by 2002:a63:384e:0:b0:374:ae28:71fc with SMTP id h14-20020a63384e000000b00374ae2871fcmr25218897pgn.159.1651091472014; Wed, 27 Apr 2022 13:31:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651091472; cv=none; d=google.com; s=arc-20160816; b=EFgzZie4HxQ7rSjsLZsbCo4I926J+JRVDGdIYyYoWvnm45AivzfUuH7f7VCnhKKiq2 NzNrCxYdkmB7G9dt8j5StSc+R6bPC+mgHkgAcelZUxzQnaMHuDJh9/QD5a4dU8V0g5bM MFDT+ByXV5X9EwqTJD8HpwjK/TZwKTQUZ21IcZ5EYMcpeTRhnz6ujfvLJcpn10xLR0I7 3qyR/YzlMDnLeUU2EkBDg9y3xq2cPv9dylXWhvINQlWpQQ/ndcSNDtpsIENs2DkK8FaU i3emK6X7QojjahstH1/j3ZFr7QvVDP8Vgp4hXjnSsok969Dn9FyoCutdtJ5efaIOOnH0 5hpw== 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=xPwl3B36r2cAnhKOklI7W5r9ku8U6ZGRu8H06ecsDmA=; b=If1loIHaM2wZbhYRtBt3RB8UG/SEuJmNLmhGyaVtxy2wl2WKQ4rTdeN+pj/SshFoDq TrtDxhyB5HLLLQYPptQq9M/ljzzvCK92mgR6nAgz2mooTZwGPQorAr3xJuDoV500/lHJ +SnR3i/RASCYbGo5N9Y2i6OjoIPY3yJG5sxU79nQfQGugeCJad576Tpby0X+thfgh6N/ 6n4YBwKNOuN9bauKR6Fhg78IcuMWuX75m7FIWG3Ak8i2uJRf/E7/1g+uJdVNwnHcM5w1 YxPy8mBmPQyRRAE/METP40OXPDZzFaMjn9zEcCzQSwy9pbmQ9c1F6rpV4EypxY7cXGTY vcEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BjuIo11T; 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 y15-20020a056a00190f00b0050d26055211si2647041pfi.154.2022.04.27.13.30.37; Wed, 27 Apr 2022 13:31:12 -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=BjuIo11T; 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 S234150AbiD0TbH (ORCPT + 99 others); Wed, 27 Apr 2022 15:31:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233726AbiD0Ta6 (ORCPT ); Wed, 27 Apr 2022 15:30:58 -0400 Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D5EF1A39A; Wed, 27 Apr 2022 12:26:48 -0700 (PDT) Received: by mail-il1-x132.google.com with SMTP id t4so619092ilo.12; Wed, 27 Apr 2022 12:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xPwl3B36r2cAnhKOklI7W5r9ku8U6ZGRu8H06ecsDmA=; b=BjuIo11TFSHGVHxVxumbfBtSf8/SrNJ5SC/Xtue/hPWKa4HvDpapKHpH2L6yrPENpH 2OEaN0xGJdlqoxfc0DVjsqxTiZ8R5bPkyB+qiNr3huDg39Lsetol46LCwuW940laIqwf LbzlCHbPy22J2HKPJ39VYopALdVPToXxpMj5gVW+fjtziRpo1gCbCVVZpWWfWygF+PCG u9tyApFiaaMIlpEsxb6c3637itcFm+maFkB4K24+ToVSepQMG4t8SybAruTA07s54LCI I6sdzHWpmJMyz40EX+iwKNtvW//cG/hteQwpeSn/y5CNx4XJoeB+aq7Hrjf+CcT+d5lN ENWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xPwl3B36r2cAnhKOklI7W5r9ku8U6ZGRu8H06ecsDmA=; b=Cu8VPT2JAFjtCiZiKVboR5MYTqAHrz5WnrpWEEJBPIQUGsbsVYllbc9gGZeRrJCBsa cPENfqMvhi3te2Ulk2x+Emz0LkoB8g3+qvEQSnvb8Tzqpqing4ybbOiCmy9TNPQrqPMr 9LsDMt2YcN/fyBDBGN2KnUHFUuxClzFhL42ZlPsO7rwOytkVheQpsLv0F1PrU7T7q05R 76Br4kdn8TOcinU2OJ9Y/+ATrkk8aorGUoMkF/0uK/rB7Gsg1k7Zfj2JQDuAXAHypafn H7T03ZLHNUWrOLWLV4vAnqA74JrwwdtOSE8de5fnsar9TtribFY3pmfrWuikb1QCCWCq bwHw== X-Gm-Message-State: AOAM533/M00R5d3u7V7EmEftaB+h8R/E/peEF4TVYQVDH1tW/nFkAwxl I4fa8g5caivHU/AiKMKI2byFPmV3yKLE4lQ1wX1/6lib620= X-Received: by 2002:a05:6e02:1ba3:b0:2cc:4158:d3ff with SMTP id n3-20020a056e021ba300b002cc4158d3ffmr11614731ili.98.1651087607619; Wed, 27 Apr 2022 12:26:47 -0700 (PDT) MIME-Version: 1.0 References: <20220422150507.222488-1-namhyung@kernel.org> <20220422150507.222488-5-namhyung@kernel.org> In-Reply-To: From: Andrii Nakryiko Date: Wed, 27 Apr 2022 12:26:36 -0700 Message-ID: Subject: Re: [PATCH 4/4] perf record: Handle argument change in sched_switch To: Namhyung Kim Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Ingo Molnar , Peter Zijlstra , LKML , Andi Kleen , Ian Rogers , Song Liu , Hao Luo , bpf , "linux-perf-use." , Blake Jones 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, Apr 27, 2022 at 11:15 AM Namhyung Kim wrote: > > Hello, > > On Tue, Apr 26, 2022 at 4:55 PM Andrii Nakryiko > wrote: > > > > On Fri, Apr 22, 2022 at 3:49 PM Namhyung Kim wrote: > > > > > > Recently sched_switch tracepoint added a new argument for prev_state, > > > but it's hard to handle the change in a BPF program. Instead, we can > > > check the function prototype in BTF before loading the program. > > > > > > Thus I make two copies of the tracepoint handler and select one based > > > on the BTF info. > > > > > > Signed-off-by: Namhyung Kim > > > --- > > > tools/perf/util/bpf_off_cpu.c | 32 +++++++++++++++ > > > tools/perf/util/bpf_skel/off_cpu.bpf.c | 55 ++++++++++++++++++++------ > > > 2 files changed, 76 insertions(+), 11 deletions(-) > > > > > > > [...] > > > > > > > > +SEC("tp_btf/sched_switch") > > > +int on_switch3(u64 *ctx) > > > +{ > > > + struct task_struct *prev, *next; > > > + int state; > > > + > > > + if (!enabled) > > > + return 0; > > > + > > > + /* > > > + * TP_PROTO(bool preempt, struct task_struct *prev, > > > + * struct task_struct *next) > > > + */ > > > + prev = (struct task_struct *)ctx[1]; > > > + next = (struct task_struct *)ctx[2]; > > > > > > you don't have to have two BPF programs for this, you can use > > read-only variable to make this choice. > > > > On BPF side > > > > const volatile bool has_prev_state = false; > > > > ... > > > > if (has_prev_state) { > > prev = (struct task_struct *)ctx[2]; > > next = (struct task_struct *)ctx[3]; > > } else { > > prev = (struct task_struct *)ctx[1]; > > next = (struct task_struct *)ctx[2]; > > } > > > > > > And from user-space side you do your detection and before skeleton is loaded: > > > > skel->rodata->has_prev_state = > > Nice, thanks for the tip! > > Actually I tried something similar but it was with a variable (in bss) > so the verifier in an old kernel rejected it due to invalid arg access. > > I guess now the const makes the verifier ignore the branch as if > it's dead but the compiler still generates the code, right? yes, exactly > > > > > But I'm still hoping that this prev_state argument can be moved to the > > end ([0]) to make all this unnecessary. > > > > [0] https://lore.kernel.org/lkml/93a20759600c05b6d9e4359a1517c88e06b44834.camel@fb.com/ > > Yeah, that would make life easier. :) > > Thanks, > Namhyung