Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp3807584rwl; Tue, 27 Dec 2022 15:51:31 -0800 (PST) X-Google-Smtp-Source: AMrXdXtsoMBwItLRQJSpiFd54J4VMX9ISXNPu6Hh2u188cLdNoHAXyqV+2827wNi1dcCgbeh/1GY X-Received: by 2002:a17:907:8a24:b0:846:95f9:5b4d with SMTP id sc36-20020a1709078a2400b0084695f95b4dmr21213404ejc.42.1672185091410; Tue, 27 Dec 2022 15:51:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672185091; cv=none; d=google.com; s=arc-20160816; b=EKr2+kz7wBi/P1F+0XGHxUCiwuAdvjQw59pRBHgXLTpxIrGDDql/N8Uh0dIGomtpK8 snorxhZm8nbxSgJSOKwtJHDeTZiPAUb4vhZEyr3LnbRFxNe4sNGSvAY+3QwfdYkovVP9 5a8aBUDD3Os4LYxPIFOvZrNjDZ4B+ZSsxDsUWXdZdWVlT9pRhjlY4YYEHXOdjBCdPLR3 ChMFzOOcqJtmRk/52IDkZaoJAjrnlTMN4h2T5VevXlKERq+ENauWw3gvInS7ez0vslg3 dN6Z/E6uczrnGzmMcBgKzeZpp8M3W3tEjh8LFAkfIPeVMoQZEpoEtNwdBaJeSKul6n+Q tTuQ== 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=9sbupdtVdTz+LltMLzxd/HsPKuNxO8PX5O/NtDk5n+o=; b=dl5bv4lB+6eo8BIInMaROu1/1zA1zd7hcskXiOA6yZcwNcDVE91hwwqwGFtI32rnge h6m/UWejUftkTFwM7y3zK0sajclEXHncvw1wgTzrL1DwOCMjzmGsrFSni4a0OMUUDNiq /Hp9ClNyEygJiOFPZuL9NfJ0pjRJuV9EbnX2gLtqYklmwmIyk7Gv570h6+e7Llh1lgW7 sgmxo9FiR81ifOwg5DUERT0pGrYVm+ceA8alfcThnDihjJfW7ZytMwA62FyeXJX2Jhwo 22LUUJZvDzjcXLJKIAt0y7lXoqM5BJgs5frdMM+wP8ZiVkHBTyBb6/LNuc1+ksp4GZQ5 XsHQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sb31-20020a1709076d9f00b007c4fda74f69si11862935ejc.241.2022.12.27.15.51.14; Tue, 27 Dec 2022 15:51:31 -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; 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 S231221AbiL0XTi (ORCPT + 67 others); Tue, 27 Dec 2022 18:19:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbiL0XTh (ORCPT ); Tue, 27 Dec 2022 18:19:37 -0500 Received: from mail-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EA87108E; Tue, 27 Dec 2022 15:19:36 -0800 (PST) Received: by mail-io1-f52.google.com with SMTP id d123so7594809iof.6; Tue, 27 Dec 2022 15:19:36 -0800 (PST) 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=9sbupdtVdTz+LltMLzxd/HsPKuNxO8PX5O/NtDk5n+o=; b=RZqJxuACpKfyDtKx4S+Ww+OmEkrULVsrzlzQ3eg1j+aqpAA9fU+ElEeTjTy+oG9tvP 9SR7EWBHpO09xh2yli31uSTPJ/tEawbcCBJGXyxriWhofkAVnovgvRL6RiayPmt2FRHb MPn85+AB44UebtMEy/IBtUMfUuNdjBTaNq9b4IG2JLU0mG9GovNY7Hr5XXvPLLFMk1Fo P11N9aZ/0aW2FiSOMn6YdZluN/hiMsV/5+2Fevy5nKzKC/WjeI8bUGd2bNVQvkmLiZl6 e+V7BOPfVxqBUAVj9DDvopi7eKV5maSQ+0553xSteCep62sVQi107ozJaeg2lrrl14im MS9w== X-Gm-Message-State: AFqh2krGaNCUgme9EncJvXPfI6JQiTeLrdehoD4lOf1Md8drwkvYaQZt ldkrOhuDlbMFxc1W/Cy+Vn4fNnL+xmuFkmDu+bQ= X-Received: by 2002:a5e:890a:0:b0:6ec:b1cc:d237 with SMTP id k10-20020a5e890a000000b006ecb1ccd237mr1446959ioj.35.1672183175665; Tue, 27 Dec 2022 15:19:35 -0800 (PST) MIME-Version: 1.0 References: <20221220220144.4016213-1-namhyung@kernel.org> <20221220220144.4016213-2-namhyung@kernel.org> <2d164a5f-2885-2a6e-581a-2673ca0b1b81@iogearbox.net> In-Reply-To: From: Namhyung Kim Date: Tue, 27 Dec 2022 15:19:24 -0800 Message-ID: Subject: Re: [PATCH bpf-next 1/2] bpf/perf: Call perf_prepare_sample() before bpf_prog_run() To: Jiri Olsa Cc: Peter Zijlstra , Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Song Liu , Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Stanislav Fomichev , LKML , bpf@vger.kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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 On Thu, Dec 22, 2022 at 11:53 PM Jiri Olsa wrote: > > On Thu, Dec 22, 2022 at 02:25:49PM -0800, Namhyung Kim wrote: > > On Thu, Dec 22, 2022 at 12:16 PM Peter Zijlstra wrote: > > > > > > On Thu, Dec 22, 2022 at 09:34:42AM -0800, Namhyung Kim wrote: > > > > > > > Sorry about that. Let me rephrase it like below: > > > > > > > > With bpf_cast_to_kern_ctx(), BPF programs attached to a perf event > > > > can access perf sample data directly from the ctx. > > > > > > This is the bpf_prog_run() in bpf_overflow_handler(), right? > > > > Yes. > > > > > > > > > But the perf sample > > > > data is not fully prepared at this point, and some fields can have invalid > > > > uninitialized values. So it needs to call perf_prepare_sample() before > > > > calling the BPF overflow handler. > > > > > > It never was, why is it a problem now? > > > > BPF used to allow selected fields only like period and addr, and they > > are initialized always by perf_sample_data_init(). This is relaxed > > by the bpf_cast_to_kern_ctx() and it can easily access arbitrary > > fields of perf_sample_data now. > > > > The background of this change is to use BPF as a filter for perf > > event samples. The code is there already and returning 0 from > > BPF can drop perf samples. With access to more sample data, > > it'd make more educated decisions. > > > > For example, I got some requests to limit perf samples in a > > selected region of address (code or data). Or it can collect > > samples only if some hardware specific information is set in > > the raw data like in AMD IBS. We can easily extend it to other > > sample info based on users' needs. > > > > > > > > > But just calling perf_prepare_sample() can be costly when the BPF > > > > > > So you potentially call it twice now, how's that useful? > > > > Right. I think we can check data->sample_flags in > > perf_prepare_sample() to minimize the duplicate work. > > It already does it for some fields, but misses others. > > we used to have __PERF_SAMPLE_CALLCHAIN_EARLY to avoid extra perf_callchain, > could we add some flag like __PERF_SAMPLE_INIT_EARLY to avoid double call to > perf_prepare_sample? I think we can check if the filtered_sample_type is 0. But it still needs to update the perf_event_header. I think we need to save the calculated size separately. Thanks, Namhyung