Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp759277rwj; Thu, 22 Dec 2022 14:36:53 -0800 (PST) X-Google-Smtp-Source: AMrXdXsegn87M5yde1nNsRa4SAyfa3f1E9x7PBvxkxGl6pAFIooWEBdALpeVml4RwTbpLVp0RnXB X-Received: by 2002:a50:ef11:0:b0:46a:d5ee:7664 with SMTP id m17-20020a50ef11000000b0046ad5ee7664mr6200464eds.6.1671748613759; Thu, 22 Dec 2022 14:36:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671748613; cv=none; d=google.com; s=arc-20160816; b=M+0VS16BMChqLoqicRcbx6JvvE2eo1zzXJxqBZIctbeD/4Dk0ai185U7jOXUeVwL2p 3LEqposET+/fKnSqni9LuMn2enuAcntWjhDJNqVK5Am8CBmB/tTTmFYpHLvtlTR5A9XU EY4h7bCQ2Rvw3uxCJAXskrVB5lUby13p/mtKYGe7JUm5x6a0QQdA00b2KOzBrPb7RGW+ J64OJgv9/61CSrRZJT2OrA7nYPULajNGVb9bqyHH2X2kMo4kPSe9LbRvFW+JYCXqefFO M4WFXLtWo7/geWLcCcU4aSFUgtGjE1gKfvjkgDYyB3KVarz/YTMViTAZGNnO0TygP+K4 tvrg== 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=AkWzqdI4Ao/+eVXLfPWUg98sZ0/cE4H62K67j4Goqj8=; b=t8zeXPF8dbdgTkdEgM2Fnw4ZZqBnD9GSZZjZQSyj/9ffxoe9A5CfJdgVP56r1QbYIr bp/uAIEpgqmHefiT7C/3avp+dRf/jcce2g4RLKPzZuCHqK739fSoUIhWEHEPPDXS+TtF QE4DnZE5FJ3NUH8YOzPE4g28SfXLDA2yQznFs8KrEmMQjds1iYAa9f3lpGiTYX8Icuov QgicjaYXd4wlRFfMTzWozbDeeF201oIfF6UVd5mKIV6ChVvdAF5+uBXa6DfMhVlUvtGe tR5DqlWxt5bzVRwMK1JEkhzWeotXsMkFlwe7lBXVoLFo3xpXvs/Buhf/DJcgR5vLbMO8 hKbA== 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 m12-20020a056402430c00b0046b6da777ecsi1605737edc.451.2022.12.22.14.36.37; Thu, 22 Dec 2022 14:36:53 -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 S235753AbiLVW0E (ORCPT + 68 others); Thu, 22 Dec 2022 17:26:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235746AbiLVW0C (ORCPT ); Thu, 22 Dec 2022 17:26:02 -0500 Received: from mail-vs1-f52.google.com (mail-vs1-f52.google.com [209.85.217.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 316E01AF0F; Thu, 22 Dec 2022 14:26:02 -0800 (PST) Received: by mail-vs1-f52.google.com with SMTP id b189so3019455vsc.10; Thu, 22 Dec 2022 14:26:02 -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=AkWzqdI4Ao/+eVXLfPWUg98sZ0/cE4H62K67j4Goqj8=; b=iEF2E45WYSiHPz6GVI7qBefO4C0uOy5YXfmuIzCRND8a4TYbPscY0QrjhTXrDQpXdo 5NMJRXg1Xhp93ucEtlN9ErDxGPNvisjFXYvR70WCuY+QxjmqqRXssDJquVaGr822iBnB vouQsxC/7JvWnUeSacxZRw68LtNzS07hWK12aXj33pibEb+Fhrso/4glRKnrjDZ4+2j9 D3Fn/4Q4wVcvdnBcFF9+FgjUMOhKB7bMOunPd9LTmQPqDZU+WaciZU4fEQH/Th/I7KxT g8RHJYr8nNd9Sp1bR+bLuzNrEdrK5AQn5oBxkTNbAb3yOVPGcVcdM0/VeH9MBlt3xlZZ UMLw== X-Gm-Message-State: AFqh2kp+Aw5Glv9oHS1kkYNOmfSuoAA7Wb3HUPEWcDAcrzLlaEpZbMka YJ+8W1ve6wljwSp8EDfraqVoQsb00YgRJGP6ar8= X-Received: by 2002:a67:eace:0:b0:3b1:30fb:e106 with SMTP id s14-20020a67eace000000b003b130fbe106mr705520vso.55.1671747961063; Thu, 22 Dec 2022 14:26:01 -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: Thu, 22 Dec 2022 14:25:49 -0800 Message-ID: Subject: Re: [PATCH bpf-next 1/2] bpf/perf: Call perf_prepare_sample() before bpf_prog_run() To: Peter Zijlstra Cc: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Song Liu , Jiri Olsa , 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 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. Thanks, Namhyung