From patchwork Thu Jan 12 21:40:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13099480 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADF33C61DB3 for ; Thu, 12 Jan 2023 21:48:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240711AbjALVsb (ORCPT ); Thu, 12 Jan 2023 16:48:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240730AbjALVrg (ORCPT ); Thu, 12 Jan 2023 16:47:36 -0500 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D18686DBA4; Thu, 12 Jan 2023 13:40:23 -0800 (PST) Received: by mail-pg1-x532.google.com with SMTP id h192so13691860pgc.7; Thu, 12 Jan 2023 13:40:23 -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=l3GEYOgHwVgvJBmBnAdUL8BdmY/9n1zanIfz8U25Xac=; b=Gb11zOzI9XeCl+/zCSz8IqtHBx1WB/Pd3fb3E2R32TTjj2OD1cwX9X8V6TSptQQv4i MaFNBKnV3reJc2C/aH3UjumX3hFZ0Y+WpC9LSBGH4NS9RDx2XCa0C0kpe98OHCg08b+/ vLC6CfHccT4mUKZHJicRNjOpCGAbGjIQ25ywc2nSHkbHLq1kWI2hN08wxujg6BURrs/9 e1H0brQCK9Enl3zWbMiwOWn9y0wJw21x/i7Obphk7dBJDd1ss8j4b0ewmTwLr0qXH2ok kdFo2Kw81t/lH4lGnlX9pNWssTvKD88pq4ExF1dQmxEjfktEhXm6pIh0VJ30507tC08b UpYg== 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=l3GEYOgHwVgvJBmBnAdUL8BdmY/9n1zanIfz8U25Xac=; b=bArBiHr6srPrm8CNsUgDFDCyq4aOETUOkiiZC0WUT5vgFJXLsJkalil9r+CTLbYY4a e1gH79HYO1PW257VpREAa0hAL+yoh3rRRjN73DlO1DXERa8+q1KsJsRWkw9Im+w9/bTl 7UgZdWuMJgPKOuhc5M51nKclANnJiiQ9z4xvw3UvSqoIhPXG92ZV4NpPiQLqnGLIp2fl WAUMNdASvvEWvhRYygEFeVOcyCZbMferFSb9zn/hT/W502/zauLaSHxTJvjiBc3ESVN3 i9PrlA6r72C5I26p4dGhwG4tBVru0hC36t+MRYud0jiarXCia+79sncB5cM1kvvIqGVk MSPw== X-Gm-Message-State: AFqh2koxHwOpdW9oD3R6SSrOya05GDWwL/U/Y9c2AxQGTGVtRbRAofHy 3Kk/jEX1yKPNuwaJycg/TDA= X-Google-Smtp-Source: AMrXdXubGB5Pd7aXclXke3/QKfLbDxQrtVVvLizKeXonP8BECBbNKliNCfkDMiH+agQTZxzwWIIevw== X-Received: by 2002:a62:cd4e:0:b0:58a:d606:4258 with SMTP id o75-20020a62cd4e000000b0058ad6064258mr11338748pfg.10.1673559623278; Thu, 12 Jan 2023 13:40:23 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:b71e:23d4:aa6a:47c0]) by smtp.gmail.com with ESMTPSA id q20-20020aa79834000000b0058134d2df41sm11344605pfl.146.2023.01.12.13.40.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 13:40:22 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Peter Zijlstra Cc: Ingo Molnar , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , Jiri Olsa , LKML , Song Liu , linux-s390@vger.kernel.org, x86@kernel.org, bpf@vger.kernel.org Subject: [PATCH 3/8] perf/core: Add perf_sample_save_raw_data() helper Date: Thu, 12 Jan 2023 13:40:10 -0800 Message-Id: <20230112214015.1014857-4-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230112214015.1014857-1-namhyung@kernel.org> References: <20230112214015.1014857-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net When it saves the raw_data 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_raw_data() helper and convert all call sites. Cc: linux-s390@vger.kernel.org Cc: x86@kernel.org Cc: bpf@vger.kernel.org Suggested-by: Peter Zijlstra Signed-off-by: Namhyung Kim --- arch/s390/kernel/perf_cpum_cf.c | 4 +--- arch/s390/kernel/perf_pai_crypto.c | 4 +--- arch/s390/kernel/perf_pai_ext.c | 4 +--- arch/x86/events/amd/ibs.c | 3 +-- include/linux/perf_event.h | 33 +++++++++++++++++++++++++----- kernel/events/core.c | 31 +++++----------------------- kernel/trace/bpf_trace.c | 6 ++---- 7 files changed, 39 insertions(+), 46 deletions(-) diff --git a/arch/s390/kernel/perf_cpum_cf.c b/arch/s390/kernel/perf_cpum_cf.c index f043a7ff220b..aa38649c7c27 100644 --- a/arch/s390/kernel/perf_cpum_cf.c +++ b/arch/s390/kernel/perf_cpum_cf.c @@ -662,9 +662,7 @@ static int cfdiag_push_sample(struct perf_event *event, if (event->attr.sample_type & PERF_SAMPLE_RAW) { raw.frag.size = cpuhw->usedss; raw.frag.data = cpuhw->stop; - raw.size = raw.frag.size; - data.raw = &raw; - data.sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(&data, &raw); } overflow = perf_event_overflow(event, &data, ®s); diff --git a/arch/s390/kernel/perf_pai_crypto.c b/arch/s390/kernel/perf_pai_crypto.c index 985e243a2ed8..a7b339c4fd7c 100644 --- a/arch/s390/kernel/perf_pai_crypto.c +++ b/arch/s390/kernel/perf_pai_crypto.c @@ -362,9 +362,7 @@ static int paicrypt_push_sample(void) if (event->attr.sample_type & PERF_SAMPLE_RAW) { raw.frag.size = rawsize; raw.frag.data = cpump->save; - raw.size = raw.frag.size; - data.raw = &raw; - data.sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(&data, &raw); } overflow = perf_event_overflow(event, &data, ®s); diff --git a/arch/s390/kernel/perf_pai_ext.c b/arch/s390/kernel/perf_pai_ext.c index 1138f57baae3..555597222bad 100644 --- a/arch/s390/kernel/perf_pai_ext.c +++ b/arch/s390/kernel/perf_pai_ext.c @@ -451,9 +451,7 @@ static int paiext_push_sample(void) if (event->attr.sample_type & PERF_SAMPLE_RAW) { raw.frag.size = rawsize; raw.frag.data = cpump->save; - raw.size = raw.frag.size; - data.raw = &raw; - data.sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(&data, &raw); } overflow = perf_event_overflow(event, &data, ®s); diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c index 417c80bd3274..64582954b5f6 100644 --- a/arch/x86/events/amd/ibs.c +++ b/arch/x86/events/amd/ibs.c @@ -1110,8 +1110,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs) .data = ibs_data.data, }, }; - data.raw = &raw; - data.sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(&data, &raw); } if (perf_ibs == &perf_ibs_op) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index a9419608402b..569dfac5887f 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -95,6 +95,11 @@ struct perf_raw_record { u32 size; }; +static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag) +{ + return frag->pad < sizeof(u64); +} + /* * branch stack layout: * nr: number of taken branches stored in entries[] @@ -1182,6 +1187,29 @@ static inline void perf_sample_save_callchain(struct perf_sample_data *data, data->sample_flags |= PERF_SAMPLE_CALLCHAIN; } +static inline void perf_sample_save_raw_data(struct perf_sample_data *data, + struct perf_raw_record *raw) +{ + struct perf_raw_frag *frag = &raw->frag; + u32 sum = 0; + int size; + + do { + sum += frag->size; + if (perf_raw_frag_last(frag)) + break; + frag = frag->next; + } while (1); + + size = round_up(sum + sizeof(u32), sizeof(u64)); + raw->size = size - sizeof(u32); + frag->pad = raw->size - sum; + + data->raw = raw; + data->dyn_size += size; + data->sample_flags |= PERF_SAMPLE_RAW; +} + /* * Clear all bitfields in the perf_branch_entry. * The to and from fields are not cleared because they are @@ -1690,11 +1718,6 @@ extern void perf_restore_debug_store(void); static inline void perf_restore_debug_store(void) { } #endif -static __always_inline bool perf_raw_frag_last(const struct perf_raw_frag *frag) -{ - return frag->pad < sizeof(u64); -} - #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x)) struct perf_pmu_events_attr { diff --git a/kernel/events/core.c b/kernel/events/core.c index 0fba98b9cd65..133894ae5e30 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7588,30 +7588,10 @@ void perf_prepare_sample(struct perf_event_header *header, if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN) perf_sample_save_callchain(data, event, regs); - if (sample_type & PERF_SAMPLE_RAW) { - struct perf_raw_record *raw = data->raw; - int size; - - if (raw && (data->sample_flags & PERF_SAMPLE_RAW)) { - struct perf_raw_frag *frag = &raw->frag; - u32 sum = 0; - - do { - sum += frag->size; - if (perf_raw_frag_last(frag)) - break; - frag = frag->next; - } while (1); - - size = round_up(sum + sizeof(u32), sizeof(u64)); - raw->size = size - sizeof(u32); - frag->pad = raw->size - sum; - } else { - size = sizeof(u64); - data->raw = NULL; - } - - data->dyn_size += size; + if (filtered_sample_type & PERF_SAMPLE_RAW) { + data->raw = NULL; + data->dyn_size += sizeof(u64); + data->sample_flags |= PERF_SAMPLE_RAW; } if (sample_type & PERF_SAMPLE_BRANCH_STACK) { @@ -10127,8 +10107,7 @@ void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, }; perf_sample_data_init(&data, 0, 0); - data.raw = &raw; - data.sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(&data, &raw); perf_trace_buf_update(record, event_type); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 3bbd3f0c810c..ad37608afc35 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -687,8 +687,7 @@ BPF_CALL_5(bpf_perf_event_output, struct pt_regs *, regs, struct bpf_map *, map, } perf_sample_data_init(sd, 0, 0); - sd->raw = &raw; - sd->sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(sd, &raw); err = __bpf_perf_event_output(regs, map, flags, sd); @@ -746,8 +745,7 @@ u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size, perf_fetch_caller_regs(regs); perf_sample_data_init(sd, 0, 0); - sd->raw = &raw; - sd->sample_flags |= PERF_SAMPLE_RAW; + perf_sample_save_raw_data(sd, &raw); ret = __bpf_perf_event_output(regs, map, flags, sd); out: From patchwork Thu Jan 12 21:40:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13099481 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A224BC54EBE for ; Thu, 12 Jan 2023 21:48:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232397AbjALVsv (ORCPT ); Thu, 12 Jan 2023 16:48:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240782AbjALVrh (ORCPT ); Thu, 12 Jan 2023 16:47:37 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D223434756; Thu, 12 Jan 2023 13:40:29 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id a184so14781571pfa.9; Thu, 12 Jan 2023 13:40:29 -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=hw1JZT4caRKOkdMu9I0bZSzXZiCQYqiKX0Yt62R8CcM=; b=RcFJQebsL1oo93IC8w4Oxta1bemGsbrJW5pMa303hyHmzRNvU7QBOBjeX85vkrLEMI Sk8rvxoUTPBaDeQKT680TOM72ra1sjElMwvEJgEJg0Ixa1MHLja3gUAB5dX2sqdGdS+B 2Ftg662VuXT/wTPCOj17BJlqy3JKqPcihaJF9+HMrbcRO0NM+nDcEmtBjx/IFreBBKcV hEYBr7jIEVgn3SgMffwhKIVJvoRsqwkc4pY6hlH3ftDgyMONnAebid7/JZlnztEwWDRL MZtAQtFWAI5VqoXoV9cSAyctDSmahTV6wIziVVoFz8qE/VCP3fHp+HqVMDhDeyg/RYwW YUJA== 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=hw1JZT4caRKOkdMu9I0bZSzXZiCQYqiKX0Yt62R8CcM=; b=xZZErkY3MCSEchY1+UfF4tsyDHjS/ACCM/gc7dyESV00VoHFtHow9BV1NIDHJ4hY8R wuePVcJpNEtaEt2NxTW690K6XTba6a/War6jjz83vKpu9OgSd3H10L8Xa5eIV0J/rQ1Y r8UH8uBvw8RFoOmucELfMnDyvaPqiXfvu7fyNFWE1Mne85ENP/N59LWGnLYJjPuTRof/ d/KJTbIDG+wkzs19C4LC2MypVhFldICGf9jfDr09YpcsfY5o7lXCjDg672YIBWsPvYMC ADOdl8pITXyaKTbm15IU1YsrCLbimL0sAwlSu2mO6pfal0tOb1dTCwGhH+PtHpv13X1i azfg== X-Gm-Message-State: AFqh2kpq7KwigFDN92GKJ8ZYmFn20s9PNMV8JM6IVtJNGKlmIzZyjKoH G8t7U4V+paN23YwCzzpAel8= X-Google-Smtp-Source: AMrXdXumgzPYKmHuVCA/C0WD1hyzao8lVSTgn1LDJ9sa/DSxpejlrQSYdKclEkw1PD+HtkBrLGNIAQ== X-Received: by 2002:a62:f211:0:b0:581:68ea:50 with SMTP id m17-20020a62f211000000b0058168ea0050mr56865082pfh.26.1673559629520; Thu, 12 Jan 2023 13:40:29 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:b71e:23d4:aa6a:47c0]) by smtp.gmail.com with ESMTPSA id q20-20020aa79834000000b0058134d2df41sm11344605pfl.146.2023.01.12.13.40.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 13:40:29 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Peter Zijlstra Cc: Ingo Molnar , Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , Jiri Olsa , LKML , Song Liu , bpf@vger.kernel.org Subject: [PATCH 8/8] perf/core: Call perf_prepare_sample() before running BPF Date: Thu, 12 Jan 2023 13:40:15 -0800 Message-Id: <20230112214015.1014857-9-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230112214015.1014857-1-namhyung@kernel.org> References: <20230112214015.1014857-1-namhyung@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org As BPF can access sample data, it needs to populate the data. Also remove the logic to get the callchain specifically as it's covered by the perf_prepare_sample() now. Cc: Song Liu Cc: bpf@vger.kernel.org Signed-off-by: Namhyung Kim Acked-by: Song Liu --- kernel/events/core.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 5c4f3fa3d2b7..af8365fb639a 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10363,13 +10363,7 @@ static void bpf_overflow_handler(struct perf_event *event, rcu_read_lock(); prog = READ_ONCE(event->prog); if (prog) { - if (prog->call_get_stack && - (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) && - !(data->sample_flags & PERF_SAMPLE_CALLCHAIN)) { - data->callchain = perf_callchain(event, regs); - data->sample_flags |= PERF_SAMPLE_CALLCHAIN; - } - + perf_prepare_sample(data, event, regs); ret = bpf_prog_run(prog, &ctx); } rcu_read_unlock();