Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4816624ioa; Wed, 27 Apr 2022 11:44:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJziCWZ0vrI3RSaEL5LoObxB7C3BKv7r683TudHXLu+UqEx+r5JTgqCvz8xzWjNc1PuFTkTV X-Received: by 2002:a63:ee0d:0:b0:39d:8637:6706 with SMTP id e13-20020a63ee0d000000b0039d86376706mr25152222pgi.510.1651085095860; Wed, 27 Apr 2022 11:44:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651085095; cv=none; d=google.com; s=arc-20160816; b=K+VwHG2JwQoGU4sFTDe8tMwD91jUuo0b1CPfuAdClBRd8NbieCjQ4VE2VPTrtHB9dH 5sM5EhURuM84lF7kwZlrJqIQ4vI5vdyu23JkyVgH778I9OAHeX3M4Dvc0OCBFG7J3CKP UkQM7unMwkrqfeMz2USbmgbXOt8SXROZp6zWQIMWBG65bCGgF/VRkqEAp3GnT7bxRIL9 PHRXl6uKYY5BPBcNYhFu5LkRQi5WcQGTXQarifc7G7cl5M1pf0iT9Q0hLPja7mR+k6f6 30Tt06bLJQXqhrgwIRkrmy1/HBAO58Z5Lg1We8vCck8/c8ink9SqXoJa75YuwCcpbK+Y rb7Q== 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; bh=Okm5dD/Oc33gIv0yVRc1fgY/+IJnnsOYTp9Pczt09uQ=; b=dTdfiuIF6xMBxiv0s1VBHAKylZ68o9oRy9WEJ0pN5q808oF16RZy4XZyHoMWZPZqfG Lga2GkxH2mwS/b6HnbXDGgW1u/wKo1u+nT/fvDQ3uulFue2YVAwGOx34bFbANSCjfeN4 8YnDg+6NW/YaWDWMKre6oo8qVrnfE9zsESiBnrCr8KZIXNKNW/7ROyl2A8t2+8OLSeBu VFmoZj/2YLfQR7Ki9wb0FM6WibZtz+F8H5zlLj2fGjtee0IZC8GJfYhN4DZyg2d04eRF nTC3j0BNY52Pjc/0j2yoO6pjEMFJuvypw8XtohO9vlmTwsUN5f9619e4NYrXeBRR4zWv e6MQ== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e7-20020a63d947000000b003aaff435397si2244223pgj.86.2022.04.27.11.44.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 11:44:55 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 51AA02BB3B; Wed, 27 Apr 2022 11:15:11 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234438AbiD0SSR (ORCPT + 99 others); Wed, 27 Apr 2022 14:18:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230084AbiD0SSP (ORCPT ); Wed, 27 Apr 2022 14:18:15 -0400 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1706119C02; Wed, 27 Apr 2022 11:15:02 -0700 (PDT) Received: by mail-lf1-f45.google.com with SMTP id w1so4589888lfa.4; Wed, 27 Apr 2022 11:15:02 -0700 (PDT) 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=Okm5dD/Oc33gIv0yVRc1fgY/+IJnnsOYTp9Pczt09uQ=; b=m7OMcPDJcmgUIU3bf+FXbNeg7c+ela3KHvKmg9fBQ1qWlg317zDddDI81aj44pz+AA Vo46wfrGZmi02QXSKnYBrRcSlpNYFNXtM/aJjSjqnZSbkY98ccCW3DM2NgusQzzHZEa6 pClvWGz1NoW0weSFrTpp9FkqrhK6K1RTQDjIye5CgemlFu5kT2O5mRMMfzPhj0BQzCxx //i7QOQzCIG6JsUgMIwpfNftUsNdgMu8h08Lv/h1YfhnAjOLj/PXYiJQHBVFxbDXR0PB igCudI5x//A+QB7l0Y/frG/0reTaHy1cYa4kBboob14QtywZkgQCmEJ8bAZBDKXFl5My 7K9g== X-Gm-Message-State: AOAM530r1axzXZ3julBaRIZza1OHFfDpZrf4A/sQgoeTZxrvmZwAJaun GmtXpSonOGkNW9qPB7fIL46IHgfn4WNCw9lZ0zE= X-Received: by 2002:a05:6512:321c:b0:46b:b7fd:1eca with SMTP id d28-20020a056512321c00b0046bb7fd1ecamr8012902lfe.481.1651083300256; Wed, 27 Apr 2022 11:15:00 -0700 (PDT) MIME-Version: 1.0 References: <20220422150507.222488-1-namhyung@kernel.org> <20220422150507.222488-5-namhyung@kernel.org> In-Reply-To: From: Namhyung Kim Date: Wed, 27 Apr 2022 11:14:49 -0700 Message-ID: Subject: Re: [PATCH 4/4] perf record: Handle argument change in sched_switch To: Andrii Nakryiko 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,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE autolearn=no 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 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? > > 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