From patchwork Wed Jan 18 06:05:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 44988 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2171549wrn; Tue, 17 Jan 2023 22:23:38 -0800 (PST) X-Google-Smtp-Source: AMrXdXvLMffmZpmnQY7mzC9Yg6F5cFpPqCbaRFb9WJ5UdRlv0kQnpvmVgb0RIMBwZbkXhcK6Hf8R X-Received: by 2002:a17:90a:748c:b0:227:1110:8aa6 with SMTP id p12-20020a17090a748c00b0022711108aa6mr6320125pjk.4.1674023018653; Tue, 17 Jan 2023 22:23:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674023018; cv=none; d=google.com; s=arc-20160816; b=uwNKAKnQvljivd4AkW2+9oAeQeBvnApn+kaUfYb4GO8gshpQwHj0rnuzfprxltmNT1 siMsw4NuYyqLxZc/UwzG1XicQSp2l1Im03CxqbHN/8Asj/D/zxaMRUO2SfyE9kw4zUM4 wFJREA14rsuTd0vkok9nlXD4osYGQFQKPMj8sAapSvaXfxeazkLz5j3JDi9eTeALBE8o PU/zn6b+JHIx0uRsl6bECNMuZKbJW0LtNsJP/vfbgaab+FdnQncAYSDf3vXOWEQSUPrN CwQph61EHd4wfpAQcSPCT4Rh7dMc9/AQGcEitNahygQFR4mvSIobhA8Erds9uy6FYham gA5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=kAqivUnaLGSiiUVP/9yUUwDL5c/NOTjH6M1nhFWggWg=; b=fVMAb/GAJjYsYjrs+gRyi2mU1hdbgOUfTBtvu9yBBCXzC3xq5yQQf4FS50wngaOCwP s9o5PcKUoSpTuJjDFc5ht1SELXn3ORS3YTsDErBmeBjLt9OJ8q4EOgZxmZjmbWLfKy85 tumxKfTh8Zn52kZjeEqJ6IJfTBSqj5btvMUDD5eSp8inRiAKBGrHLFCp40GOwjfqY2S2 9IL355aOx8cUOU3r1aPOa3+RNY6VHwOtqVrfOgD+VQOfNXEInNsIrhuAxwKvjFnP+jlo OnAYllcNO44dLr29NJHmtdYgD01qI7JOZiQv/caCgNtNrVBv5PER9ixnqRlnVeIBfjnJ 12uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ATO5EJCg; 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=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nk3-20020a17090b194300b0022630c5cbc8si1192748pjb.24.2023.01.17.22.23.23; Tue, 17 Jan 2023 22:23:38 -0800 (PST) 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=ATO5EJCg; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229436AbjARGWP (ORCPT + 99 others); Wed, 18 Jan 2023 01:22:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229728AbjARGTT (ORCPT ); Wed, 18 Jan 2023 01:19:19 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CF0E1E5D6; Tue, 17 Jan 2023 22:06:12 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id d3so35849757plr.10; Tue, 17 Jan 2023 22:06:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=kAqivUnaLGSiiUVP/9yUUwDL5c/NOTjH6M1nhFWggWg=; b=ATO5EJCg/oM9Opfk75b9Fk6ftnjKeXw3k6lZVQhZ1oTDVpjgjxnHDrOni75BCwiHAi PkskBMxjj5cDFpulLJwmhq0gSt8UdeM/zaEyZ1VQZ2kDdZkBbTru6NCU5Y9JOY3EPXis YaupmNpHs+OQD7gEzvRQozc0VQTIRPEM6d8HdSSEuiSV6RWEITTvCvm5XZcWw3uSzTpL t9uhO5iBH5f4qHXWYAGhR2NRnEcEpp38sRhdD9M/HMUVSvczlJqD0SQ00J2wY6vce4PM qgksN48Yv/gCXuLPPOmt+vv5OtSablQeA/dm5orZr9KoQZDhXqh4zi8+YoaZNj2xne8s Rwzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=kAqivUnaLGSiiUVP/9yUUwDL5c/NOTjH6M1nhFWggWg=; b=Rjhmd7PlvaSg3gnKPjg32rdiU68IKBwTuAKlIrgz4Fa0eHpUdx0YxxXh6N9mLBESYB hnESnMoJxdgs4B/be0+vAFSYm39scd/8Zlz/4Vaiu6x8xMAUKs3oaX5yDWt7Vmt1oCrZ +AjKGG63OCZ7ZUWJChFBlbeP7xe0F6llbIRqzonTGRPZicBSnwB7V9O1awf6A/9fherF yul/EK84OxkuFKjCf4cl0ZmdsFgs+xwjwQ+eXyMCoET0j1mweHPccWgXA56WmZf93EAa ut72jHfYG8iuVRgv8O6GE4TZa5hHklKpXJACfNHVrtXy+mHqx52szV6kGIX62MQgdGBX 7TWA== X-Gm-Message-State: AFqh2kq1R71tVRAYROT5K6im51ui9zXHaL1IsjWlItJL1qTWlxrKQYqT N0iAwGzlbOXkDyF7JUh2cTwRccFqGS4= X-Received: by 2002:a05:6a20:d2cd:b0:b5:c751:78bb with SMTP id ir13-20020a056a20d2cd00b000b5c75178bbmr6346664pzb.6.1674021966593; Tue, 17 Jan 2023 22:06:06 -0800 (PST) Received: from balhae.hsd1.ca.comcast.net ([2601:647:6780:ff0:3749:9eb3:dfb5:f449]) by smtp.gmail.com with ESMTPSA id b126-20020a62cf84000000b00574e84ed847sm10990180pfg.24.2023.01.17.22.06.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jan 2023 22:06:06 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , Jiri Olsa , LKML , Song Liu , bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org Subject: [PATCH 4/8] perf/core: Add perf_sample_save_brstack() helper Date: Tue, 17 Jan 2023 22:05:55 -0800 Message-Id: <20230118060559.615653-5-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230118060559.615653-1-namhyung@kernel.org> References: <20230118060559.615653-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755340360743805988?= X-GMAIL-MSGID: =?utf-8?q?1755340360743805988?= When it saves the branch stack to the perf sample data, it needs to update the sample flags and the dynamic size. To make sure this, add the perf_sample_save_brstack() helper and convert all call sites. Cc: linuxppc-dev@lists.ozlabs.org Cc: x86@kernel.org Suggested-by: Peter Zijlstra Acked-by: Jiri Olsa Tested-by: Jiri Olsa Signed-off-by: Namhyung Kim Acked-by: Athira Rajeev --- arch/powerpc/perf/core-book3s.c | 3 +- arch/x86/events/amd/core.c | 6 +-- arch/x86/events/intel/core.c | 6 +-- arch/x86/events/intel/ds.c | 9 ++--- include/linux/perf_event.h | 66 ++++++++++++++++++++------------- kernel/events/core.c | 16 +++----- 6 files changed, 53 insertions(+), 53 deletions(-) diff --git a/arch/powerpc/perf/core-book3s.c b/arch/powerpc/perf/core-book3s.c index bf318dd9b709..8c1f7def596e 100644 --- a/arch/powerpc/perf/core-book3s.c +++ b/arch/powerpc/perf/core-book3s.c @@ -2313,8 +2313,7 @@ static void record_and_restart(struct perf_event *event, unsigned long val, struct cpu_hw_events *cpuhw; cpuhw = this_cpu_ptr(&cpu_hw_events); power_pmu_bhrb_read(event, cpuhw); - data.br_stack = &cpuhw->bhrb_stack; - data.sample_flags |= PERF_SAMPLE_BRANCH_STACK; + perf_sample_save_brstack(&data, event, &cpuhw->bhrb_stack); } if (event->attr.sample_type & PERF_SAMPLE_DATA_SRC && diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index d6f3703e4119..463f3eb8bbd7 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -928,10 +928,8 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs) if (!x86_perf_event_set_period(event)) continue; - if (has_branch_stack(event)) { - data.br_stack = &cpuc->lbr_stack; - data.sample_flags |= PERF_SAMPLE_BRANCH_STACK; - } + if (has_branch_stack(event)) + perf_sample_save_brstack(&data, event, &cpuc->lbr_stack); if (perf_event_overflow(event, &data, regs)) x86_pmu_stop(event, 0); diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 29d2d0411caf..14f0a746257d 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3036,10 +3036,8 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) perf_sample_data_init(&data, 0, event->hw.last_period); - if (has_branch_stack(event)) { - data.br_stack = &cpuc->lbr_stack; - data.sample_flags |= PERF_SAMPLE_BRANCH_STACK; - } + if (has_branch_stack(event)) + perf_sample_save_brstack(&data, event, &cpuc->lbr_stack); if (perf_event_overflow(event, &data, regs)) x86_pmu_stop(event, 0); diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index 158cf845fc80..07c8a2cdc3ee 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1720,10 +1720,8 @@ static void setup_pebs_fixed_sample_data(struct perf_event *event, data->sample_flags |= PERF_SAMPLE_TIME; } - if (has_branch_stack(event)) { - data->br_stack = &cpuc->lbr_stack; - data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; - } + if (has_branch_stack(event)) + perf_sample_save_brstack(data, event, &cpuc->lbr_stack); } static void adaptive_pebs_save_regs(struct pt_regs *regs, @@ -1883,8 +1881,7 @@ static void setup_pebs_adaptive_sample_data(struct perf_event *event, if (has_branch_stack(event)) { intel_pmu_store_pebs_lbrs(lbr); - data->br_stack = &cpuc->lbr_stack; - data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; + perf_sample_save_brstack(data, event, &cpuc->lbr_stack); } } diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 569dfac5887f..7db0e9cc2682 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1102,6 +1102,31 @@ extern u64 perf_event_read_value(struct perf_event *event, extern struct perf_callchain_entry *perf_callchain(struct perf_event *event, struct pt_regs *regs); +static inline bool branch_sample_no_flags(const struct perf_event *event) +{ + return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS; +} + +static inline bool branch_sample_no_cycles(const struct perf_event *event) +{ + return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES; +} + +static inline bool branch_sample_type(const struct perf_event *event) +{ + return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE; +} + +static inline bool branch_sample_hw_index(const struct perf_event *event) +{ + return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX; +} + +static inline bool branch_sample_priv(const struct perf_event *event) +{ + return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE; +} + struct perf_sample_data { /* @@ -1210,6 +1235,21 @@ static inline void perf_sample_save_raw_data(struct perf_sample_data *data, data->sample_flags |= PERF_SAMPLE_RAW; } +static inline void perf_sample_save_brstack(struct perf_sample_data *data, + struct perf_event *event, + struct perf_branch_stack *brs) +{ + int size = sizeof(u64); /* nr */ + + if (branch_sample_hw_index(event)) + size += sizeof(u64); + size += brs->nr * sizeof(struct perf_branch_entry); + + data->br_stack = brs; + data->dyn_size += size; + data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; +} + /* * Clear all bitfields in the perf_branch_entry. * The to and from fields are not cleared because they are @@ -1827,30 +1867,4 @@ static inline void perf_lopwr_cb(bool mode) } #endif -#ifdef CONFIG_PERF_EVENTS -static inline bool branch_sample_no_flags(const struct perf_event *event) -{ - return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_FLAGS; -} - -static inline bool branch_sample_no_cycles(const struct perf_event *event) -{ - return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_NO_CYCLES; -} - -static inline bool branch_sample_type(const struct perf_event *event) -{ - return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_TYPE_SAVE; -} - -static inline bool branch_sample_hw_index(const struct perf_event *event) -{ - return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX; -} - -static inline bool branch_sample_priv(const struct perf_event *event) -{ - return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_PRIV_SAVE; -} -#endif /* CONFIG_PERF_EVENTS */ #endif /* _LINUX_PERF_EVENT_H */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 133894ae5e30..0218b6ffaf36 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7317,7 +7317,7 @@ void perf_output_sample(struct perf_output_handle *handle, } if (sample_type & PERF_SAMPLE_BRANCH_STACK) { - if (data->sample_flags & PERF_SAMPLE_BRANCH_STACK) { + if (data->br_stack) { size_t size; size = data->br_stack->nr @@ -7594,16 +7594,10 @@ void perf_prepare_sample(struct perf_event_header *header, data->sample_flags |= PERF_SAMPLE_RAW; } - if (sample_type & PERF_SAMPLE_BRANCH_STACK) { - int size = sizeof(u64); /* nr */ - if (data->sample_flags & PERF_SAMPLE_BRANCH_STACK) { - if (branch_sample_hw_index(event)) - size += sizeof(u64); - - size += data->br_stack->nr - * sizeof(struct perf_branch_entry); - } - data->dyn_size += size; + if (filtered_sample_type & PERF_SAMPLE_BRANCH_STACK) { + data->br_stack = NULL; + data->dyn_size += sizeof(u64); + data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; } if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))