Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp243923rwe; Tue, 23 Aug 2022 23:27:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR559RXItTuQCQSNxpsgJ67Slf4f7CQBkxNpleZ8bD8neQVZMAR1WRD+K06soCUcfrzTdmdR X-Received: by 2002:a17:907:7f10:b0:73d:a3c5:1df8 with SMTP id qf16-20020a1709077f1000b0073da3c51df8mr1807801ejc.370.1661322476924; Tue, 23 Aug 2022 23:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661322476; cv=none; d=google.com; s=arc-20160816; b=Pkiyxauu7kqnbR7mvYyl1UAHDkacjtk0LOGf+D0fXzD+lTjCh0VFlbj3zNsCD2ob9z RC6Ol27V700Dzdls4EwjyePZY06VewehGEsspHdBYVeygxTuyT6mMJ6Llg+i3phTLlPS NQVGPgxc9WX4Y37ZT0+d4/j2xoaq0Ce6yo45572qUJf3R5qkzyGG0KrPlaqHAhTt4Jwb 1UmylNTWM/tHxPNAZEjF87l1PKpM8P4P34PVbDjPZ6XFaF7FWCWvcZ67SM/4pxttsFZQ BXVEyCgOzgBo/5UUbCNZ8h96qEEkYuBT3orUCDwlrTANWC2ZQTeQPzdGPBDGXYYc6LmI JCSQ== 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=MJ6AZ1UWd0bT97akas4NpKCnvFJdnaXZc24uDT8zc78=; b=tke3BIQiATHkkfS8bTABBzuTTTwgXien/IH63nZRBXaUs8rYw/SIp+zSNKUfSKvyvt OUeFx+qZxngwrKRqRLKHlwAYA+FJ/JDkg9TXgsgvosXVHP6vJb43VE+gaz7D9W2IpgQl nnPyMkV6rUN9iUkCr/ou80U7obovUE89PYg9glldMtUmWkoGgE0P1PdPpJi2fP/8e+Jd 3Pz7ZFDbBiPlxDYYntAF2dJvmapfKfB8z1K1qpgd0sOIihjL2FxNC2ebksnvRwBkM54e Wq1FHNV2XkOcJnVLPTp1GP0cbPFA2wMoKbxVVjLo/XnOKr1m+qmhzZMvH0CJ1Mvdiblk 0O3A== 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 hs24-20020a1709073e9800b0072b4063eca2si1459524ejc.559.2022.08.23.23.27.30; Tue, 23 Aug 2022 23:27:56 -0700 (PDT) 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 S233556AbiHXGKE (ORCPT + 99 others); Wed, 24 Aug 2022 02:10:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229884AbiHXGKC (ORCPT ); Wed, 24 Aug 2022 02:10:02 -0400 Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CFE121A3; Tue, 23 Aug 2022 23:10:01 -0700 (PDT) Received: by mail-oi1-f170.google.com with SMTP id n124so7593055oih.7; Tue, 23 Aug 2022 23:10:01 -0700 (PDT) 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; bh=MJ6AZ1UWd0bT97akas4NpKCnvFJdnaXZc24uDT8zc78=; b=0S0Nsr74HOZ5Sth0eRvbEcACy6JfuFG91U/SkcWGoympK/Ox2MhJAj0nCkWKdicTml XZTaF1ot6gzNGvTfcCOOEtduyXwf12TQuLOHZszIouD2V4EKKWhdIxN3fmtrRT5ezPF2 m21pLC4J3ytF9pFN6Q3o4/sd2ML1YODrIDYFvLqFgA88HcAW085W+aoNPLEa3iZWAAF4 FHwzck3urNah/zrDq0iDRfY6unjMb0RUob8s1PqyV6O8EnYr2fJ20IwYAg7mmb6662SX MruS+LNZ+I3oX7fDJWUPa/yi149NiQGn5A5fPDOzFczoHUJLNDVVCy0FbGODq1WklVCl QMRw== X-Gm-Message-State: ACgBeo2Bf6Su7R0QANlTS6SPE3NL1F8how70y9FFuviD7aweOA/InkGq 7VmCAsCHF6H91hbrxJOUAAse6oEiZy9x3YzsW49/PrU3 X-Received: by 2002:a05:6808:d46:b0:345:7b42:f987 with SMTP id w6-20020a0568080d4600b003457b42f987mr2312446oik.92.1661321400646; Tue, 23 Aug 2022 23:10:00 -0700 (PDT) MIME-Version: 1.0 References: <20220823210354.1407473-1-namhyung@kernel.org> <6305b7bcbd7a3_6d4fc208d9@john.notmuch> In-Reply-To: <6305b7bcbd7a3_6d4fc208d9@john.notmuch> From: Namhyung Kim Date: Tue, 23 Aug 2022 23:09:49 -0700 Message-ID: Subject: Re: [PATCH bpf-next] bpf: Add bpf_read_raw_record() helper To: John Fastabend Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Steven Rostedt , Peter Zijlstra , Ingo Molnar , bpf , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 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, T_SCC_BODY_TEXT_LINE 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, Aug 23, 2022 at 10:31 PM John Fastabend wrote: > > Namhyung Kim wrote: > > The helper is for BPF programs attached to perf_event in order to read > > event-specific raw data. I followed the convention of the > > bpf_read_branch_records() helper so that it can tell the size of > > record using BPF_F_GET_RAW_RECORD flag. > > > > The use case is to filter perf event samples based on the HW provided > > data which have more detailed information about the sample. > > > > Note that it only reads the first fragment of the raw record. But it > > seems mostly ok since all the existing PMU raw data have only single > > fragment and the multi-fragment records are only for BPF output attached > > to sockets. So unless it's used with such an extreme case, it'd work > > for most of tracing use cases. > > > > Signed-off-by: Namhyung Kim > > --- > > Acked-by: John Fastabend Thanks! > > > I don't know how to test this. As the raw data is available on some > > hardware PMU only (e.g. AMD IBS). I tried a tracepoint event but it was > > rejected by the verifier. Actually it needs a bpf_perf_event_data > > context so that's not an option IIUC. > > not a pmu expert but also no good ideas on my side. > > ... > > > > > +BPF_CALL_4(bpf_read_raw_record, struct bpf_perf_event_data_kern *, ctx, > > + void *, buf, u32, size, u64, flags) > > +{ > > + struct perf_raw_record *raw = ctx->data->raw; > > + struct perf_raw_frag *frag; > > + u32 to_copy; > > + > > + if (unlikely(flags & ~BPF_F_GET_RAW_RECORD_SIZE)) > > + return -EINVAL; > > + > > + if (unlikely(!raw)) > > + return -ENOENT; > > + > > + if (flags & BPF_F_GET_RAW_RECORD_SIZE) > > + return raw->size; > > + > > + if (!buf || (size % sizeof(u32) != 0)) > > + return -EINVAL; > > + > > + frag = &raw->frag; > > + WARN_ON_ONCE(!perf_raw_frag_last(frag)); > > + > > + to_copy = min_t(u32, frag->size, size); > > + memcpy(buf, frag->data, to_copy); > > + > > + return to_copy; > > +} > > + > > +static const struct bpf_func_proto bpf_read_raw_record_proto = { > > + .func = bpf_read_raw_record, > > + .gpl_only = true, > > + .ret_type = RET_INTEGER, > > + .arg1_type = ARG_PTR_TO_CTX, > > + .arg2_type = ARG_PTR_TO_MEM_OR_NULL, > > + .arg3_type = ARG_CONST_SIZE_OR_ZERO, > > + .arg4_type = ARG_ANYTHING, > > +}; > > Patch lgtm but curious why allow the ARG_PTR_TO_MEM_OR_NULL from API > side instead of just ARG_PTR_TO_MEM? Maybe, just to match the > existing perf_event_read()? I acked it as I think matching existing > API is likely good enough reason. It can query the size of raw record using BPF_F_GET_RAW_RECORD_SIZE. In that case it can pass NULL for the buffer (and 0 for the size). Thanks, Namhyung > > > + > > static const struct bpf_func_proto * > > pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) > > { > > @@ -1548,6 +1587,8 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) > > return &bpf_read_branch_records_proto; > > case BPF_FUNC_get_attach_cookie: > > return &bpf_get_attach_cookie_proto_pe; > > + case BPF_FUNC_read_raw_record: > > + return &bpf_read_raw_record_proto; > > default: > > return bpf_tracing_func_proto(func_id, prog); > > } > > -- > > 2.37.2.609.g9ff673ca1a-goog > > > >