From patchwork Tue Dec 20 22:01:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 35198 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp3204936wrn; Tue, 20 Dec 2022 14:07:20 -0800 (PST) X-Google-Smtp-Source: AA0mqf7SzZ+ENXdjyNmhik33HZHR5re8DWPEShDfmjofFZDPogbq9qkzb1Jsv2YvqBVKBgHGjKCo X-Received: by 2002:a17:907:c71b:b0:7c1:99a7:e10b with SMTP id ty27-20020a170907c71b00b007c199a7e10bmr28741985ejc.49.1671574040730; Tue, 20 Dec 2022 14:07:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671574040; cv=none; d=google.com; s=arc-20160816; b=mXsSKf//fP/VYVp/v0GwjU1Vohdix6U7cNnHOGPhNLhXGzd93hwc9Xi29N/YtSzG+S UhCqDEiVgc1ZzbJLKv64qfu7e5BYqm90Dns36Ln4STig2Tg1GRWaL8MBEQ9ci+u+mmgo My8bZD48zy1AGBLMP1Mt7vq2OlPu+zh4WthwoyTa6iF89r4PC7N9QhWDWIjcLnRowal2 py1SD6kTBa/qDx18n0pFcOleVvdzObfGsJTJZI176j481krw8qjHgemqFcqLaGXuNsXB XuHzzqoMp2ntOsh9oBeIfxP5sW4rmMuq/gpStRFNqvBioPBGS/dhMFpx9UijN0B9Fc18 nUyQ== 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=ppwXM72OEq9TzN8BvxQmanyYhSITXro6/homdwIj+C0=; b=CnNOWSuQBpSGDZZFGTRAHKlWeys2KZqKGOs1Z1ePuzCk92mWTJ0ENZPe2ohE0JdNMz +XwiiqQwfD+i3Nt3CGUhevHzCo7Md354c5ZlnQxNqqmPzF0mBQmBPPjTSzYT9nqU2NxT s6PtCEPOOBv3nc6UZKjOerWFQUCj24S5iA22cRR88q9xnPbGAbURwAVFnmhvmvcryzac u2u0N+qd1CsijSosby5Tg5F2V6hZhuU2VE4cgpbUG85I/27X0RrNM51Alvp99s7Ri2i9 SzKxUoQv+NmnvVdRUenbwwWXM0Lnqn4f6nTgv4tpnO1E0ZhEshM8awo9LdYVjxDozo0T YcBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="f/EP8r2v"; 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 m17-20020a056402511100b0046c2a42d72asi404460edd.404.2022.12.20.14.06.57; Tue, 20 Dec 2022 14:07:20 -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="f/EP8r2v"; 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 S233820AbiLTWB4 (ORCPT + 99 others); Tue, 20 Dec 2022 17:01:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233966AbiLTWBv (ORCPT ); Tue, 20 Dec 2022 17:01:51 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3517B265B; Tue, 20 Dec 2022 14:01:50 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id m4so13663481pls.4; Tue, 20 Dec 2022 14:01:50 -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=ppwXM72OEq9TzN8BvxQmanyYhSITXro6/homdwIj+C0=; b=f/EP8r2v4/F6MbyeG0xeTvhslCd25jrvRfGoS7cg2lo+PWx+YYhY0AlP4M6cFhMUDE U2Yok77E4GT7Mmgx+JJFi7piivQ8nVOpNkki4HNpRqdLMTACehta4z7x7PTWkHhcfUlw qe9of5Pyl8p5YSvxOeKePlcccajs9KVCZtP6YQeoUCOYmpp4+9d6HrxREWMa58F6oba7 1YKX65pHEblXmYCILOIWtz+tpgbY3wXt6Z4MZDwTLjXN6cVzipSQWTp5MNVbn8l6zgyK UVjjWjBdfayNi+j1r5DaAQg2lr0bDM/yyS7mtqOABTtyMh7Bnzo3eOlg8acwe68nTCP2 ypzw== 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=ppwXM72OEq9TzN8BvxQmanyYhSITXro6/homdwIj+C0=; b=jAEwQtQn+OTvdtBgyPyH/049QSuMF5zXDGGAeOgT2SKObvtDMJSqxC+2nmoz0LNR9k IBk9wr3BI52wC1KLE6gedWs8a65RBcV8HmT8SaOa+IViIH3Y8NZF+eR/FEhMqVOXkZUI ycg9Tt3YRJsQfqz1umXdEuW5jTAaYfHZ1H3s1ZFLmQgJe3sESL24J6AolNQEDwohXUwO vigj8nLKVjzO4GndnqfYApk+MuETkgADZJKeQ5y5Vy1f6bwvB7pwMknbbpMhZkl3+u/Q m0YpGdIf4hoENIU5gMXSw/xswfRdspb4ZwfLsuFbkQADFPPH/wAkFL4tNbVFTjzUkszz dSsQ== X-Gm-Message-State: AFqh2kr31wHKj6JnwYNRRmGhgwh4ziNN+Ox2yeDF9KZ9Vh+gXwFFs4ht jZbwOWKdc4MAJca+A43SzAI= X-Received: by 2002:a17:90b:1997:b0:223:f12b:9881 with SMTP id mv23-20020a17090b199700b00223f12b9881mr2908677pjb.36.1671573709704; Tue, 20 Dec 2022 14:01:49 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:68c4:de54:581a:ee2]) by smtp.gmail.com with ESMTPSA id 2-20020a17090a174200b00217090ece49sm14374pjm.31.2022.12.20.14.01.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Dec 2022 14:01:49 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Jiri Olsa , Peter Zijlstra Cc: Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Stanislav Fomichev , LKML , bpf@vger.kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo Subject: [PATCH bpf-next 1/2] bpf/perf: Call perf_prepare_sample() before bpf_prog_run() Date: Tue, 20 Dec 2022 14:01:43 -0800 Message-Id: <20221220220144.4016213-2-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20221220220144.4016213-1-namhyung@kernel.org> References: <20221220220144.4016213-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 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?1752772421628283290?= X-GMAIL-MSGID: =?utf-8?q?1752772421628283290?= When the BPF program calls bpf_cast_to_kern_ctx(), it assumes the program will access perf sample data directly and call perf_prepare_sample() to make sure the sample data is populated. Signed-off-by: Namhyung Kim --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 1 + kernel/events/core.c | 3 +++ 3 files changed, 5 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5fec2d1be6d7..6bd4c21a6dd4 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1341,6 +1341,7 @@ struct bpf_prog { enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */ call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ call_get_func_ip:1, /* Do we call get_func_ip() */ + call_cast_kctx:1, /* Do we call bpf_cast_to_kern_ctx() */ tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index faa358b3d5d7..23a9dc187292 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -9236,6 +9236,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0].type = PTR_TO_BTF_ID | PTR_TRUSTED; regs[BPF_REG_0].btf = desc_btf; regs[BPF_REG_0].btf_id = meta.ret_btf_id; + env->prog->call_cast_kctx = 1; } else if (meta.func_id == special_kfunc_list[KF_bpf_rdonly_cast]) { ret_t = btf_type_by_id(desc_btf, meta.arg_constant.value); if (!ret_t || !btf_type_is_struct(ret_t)) { diff --git a/kernel/events/core.c b/kernel/events/core.c index e47914ac8732..a654a0cb6842 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10332,6 +10332,7 @@ static void bpf_overflow_handler(struct perf_event *event, .event = event, }; struct bpf_prog *prog; + struct perf_event_header dummy; int ret = 0; ctx.regs = perf_arch_bpf_user_pt_regs(regs); @@ -10346,6 +10347,8 @@ static void bpf_overflow_handler(struct perf_event *event, data->callchain = perf_callchain(event, regs); data->sample_flags |= PERF_SAMPLE_CALLCHAIN; } + if (prog->call_cast_kctx) + perf_prepare_sample(&dummy, data, event, regs); ret = bpf_prog_run(prog, &ctx); } From patchwork Tue Dec 20 22:01:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 35199 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp3205023wrn; Tue, 20 Dec 2022 14:07:31 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Qmzii81TyORKq+PdlzZtBCnxGQuQNu1VrGwji7MopC7GbfFtgRDxde7RedFxFUOUyOxsA X-Received: by 2002:a17:906:c1ca:b0:78d:f457:1062 with SMTP id bw10-20020a170906c1ca00b0078df4571062mr31441956ejb.31.1671574051258; Tue, 20 Dec 2022 14:07:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671574051; cv=none; d=google.com; s=arc-20160816; b=LgZtP/j97zaTqAJPNkI06D1WI0enfoQFgu07Bw58gQa0zI4rA/XhCxPELK7KZffV7Z e6oc4He4bWuSbiXoJ+rKrqj0IRZUb2bDx4otfJcf5c3vgAoP5sbQOvPbmYfeTzqqJAic ylMA6m37gBIkGHXFy6x1SLwboLSMjlsjjWCQr1vLN3VZjfWBklN2s+khAFbrnngZbBg7 o1Lpaty6G7sP4pbtBVX1S18oRzMBdI/j3o5qaEVW8MCpgWlHjSXwdx10mDyf9h5WQPpT Uksqg4d0I1NDop2ZBHZHh49CeX8XTc5DQtLonWlQLpLLItkpJzCDOUnHtI0FrVJMKn+p V+mw== 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=Een9ADhaz5ehEAwvU7HmNyImJ88Ez5hMroHk5J56uIY=; b=AzEECwzQdpNDAJc/ekAAoXbhvJTmgv/lREIDlsnXRyDCtSFwCQeB6DDPsgJPp+dbUp WOdCOLTpFqrbJ/2eV9UIukBMtEBCEFVV2ecq03kpx3kvWilWVmEbeAjgPMj3oUSUbg1/ gxU5nIOzWm3Q0HyhnnPiqe7Ug48fXeSD1m2vK7T3BqKFBxV4iLeg4+3yfctXVQjkvXVb 6WflWYedrBwsWZuhZjH2u3fuMoCohx8yO3tjbAa8LJaEszpf6SewGAOCsgU3H+djkL1w DlEffUnspPxjfa85gKqF+PdEGyten+CbL7bAZsl+Mf/FG9puA72fx4AOQPYd1F2fh/m6 xecQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SYMe3153; 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 ds22-20020a170907725600b00780af308970si14278533ejc.185.2022.12.20.14.07.08; Tue, 20 Dec 2022 14:07: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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SYMe3153; 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 S234027AbiLTWCE (ORCPT + 99 others); Tue, 20 Dec 2022 17:02:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234195AbiLTWBx (ORCPT ); Tue, 20 Dec 2022 17:01:53 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCD4A265B; Tue, 20 Dec 2022 14:01:51 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id o12so13802201pjo.4; Tue, 20 Dec 2022 14:01:51 -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=Een9ADhaz5ehEAwvU7HmNyImJ88Ez5hMroHk5J56uIY=; b=SYMe3153Rgqa44SBJ483+8SEvUSJddnUW99SDn7Cb7pisiecKZ4W9gurVO9hgUr85U xWVIaidt/JbnlDs+ZA+sEB+RmzsUVxqHG5Bkxr5AczT73lAmR6y9BTa2QCambxcvjt9N ifU4qYhtoyoQYg6OgVNToRz7WshXgdBhqs0hDtHbQSY7ALJ1ViRQXukWfMO9uL/UxXIo izzbQ5JUfhgnciKx9aawh7HZsvmWErSipBZNEhxjWsgUVBuwSfnD92MEaqgap4ZbT0C+ PcjfLOUpLXVcUrDC9Oex6c4EGRq1ME2Mf5ftHlZD2gYc0lIrRepJJCpygbVjCj/4bwFr S47g== 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=Een9ADhaz5ehEAwvU7HmNyImJ88Ez5hMroHk5J56uIY=; b=uPNyJI62gC73Mfjb1wbL4epExfc6cgZbFAN/p8M7SSSckokukXPlIRCCG+H2jjPy3S PRhz+BU13NpG8DMBfys8nSJ0mdjcHJ+9CCTI9mGOsMbLtvBvVSYpnQ3FE/Fh3pEMrCUW 7A4GwW5NVlYwAD1VUXTr6bYiVHQt6QoFbaAf6/REJBfBtj8ET5EtVJYpPOM4Yzvwd7+t m8lEAAD8Wo6GPrrFsUE9zIWs4ZOGCa1bPnDgynYkhpKw13xHxuhFVeErU8app4GWjpJ/ JPYj4ugEwyrTutmwvJ07/99c8e2fn2p/ad3a4F+G6jYwHxccbbWQvIQMEMpC7IU/QKvw l9mA== X-Gm-Message-State: ANoB5pn8BknGcIcWXjU5eBhgJrLPZRzfQLarlACLLdSDcOrWbJGXBUEq DEt8YVgN7mFl1GGGsvclCUo= X-Received: by 2002:a17:90a:db04:b0:219:b936:6bd7 with SMTP id g4-20020a17090adb0400b00219b9366bd7mr49496981pjv.19.1671573711192; Tue, 20 Dec 2022 14:01:51 -0800 (PST) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:68c4:de54:581a:ee2]) by smtp.gmail.com with ESMTPSA id 2-20020a17090a174200b00217090ece49sm14374pjm.31.2022.12.20.14.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Dec 2022 14:01:50 -0800 (PST) Sender: Namhyung Kim From: Namhyung Kim To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Jiri Olsa , Peter Zijlstra Cc: Martin KaFai Lau , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Stanislav Fomichev , LKML , bpf@vger.kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo Subject: [PATCH bpf-next 2/2] selftests/bpf: Add perf_event_read_sample test cases Date: Tue, 20 Dec 2022 14:01:44 -0800 Message-Id: <20221220220144.4016213-3-namhyung@kernel.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20221220220144.4016213-1-namhyung@kernel.org> References: <20221220220144.4016213-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 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?1752772431993311965?= X-GMAIL-MSGID: =?utf-8?q?1752772431993311965?= It checks the perf event sample access with bpf_cast_to_kern_ctx(). It should access sample data only event->attr.sample_type allows. Other fields might not be initialized. $ ./vmtest.sh ./test_progs -t perf_event_read_sample ... #135/1 perf_event_read_sample/perf_event_read_sample_ok:OK #135/2 perf_event_read_sample/perf_event_read_sample_invalid:OK #135 perf_event_read_sample:OK Signed-off-by: Namhyung Kim --- .../selftests/bpf/prog_tests/perf_sample.c | 167 ++++++++++++++++++ .../selftests/bpf/progs/test_perf_sample.c | 33 ++++ 2 files changed, 200 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_sample.c create mode 100644 tools/testing/selftests/bpf/progs/test_perf_sample.c diff --git a/tools/testing/selftests/bpf/prog_tests/perf_sample.c b/tools/testing/selftests/bpf/prog_tests/perf_sample.c new file mode 100644 index 000000000000..dc1e88711e23 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/perf_sample.c @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include +#include "test_perf_sample.skel.h" + +#ifndef noinline +#define noinline __attribute__((noinline)) +#endif + +/* treat user-stack data as invalid (for testing only) */ +#define PERF_SAMPLE_INVALID PERF_SAMPLE_STACK_USER + +#define PERF_MMAP_SIZE 8192 +#define DATA_MMAP_SIZE 4096 + +static int perf_fd = -1; +static void *perf_ringbuf; +static struct test_perf_sample *skel; + +static int open_perf_event(u64 sample_flags) +{ + struct perf_event_attr attr = { + .type = PERF_TYPE_SOFTWARE, + .config = PERF_COUNT_SW_PAGE_FAULTS, + .sample_type = sample_flags, + .sample_period = 1, + .disabled = 1, + .size = sizeof(attr), + }; + int fd; + void *ptr; + + fd = syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0); + if (!ASSERT_GT(fd, 0, "perf_event_open")) + return -1; + + ptr = mmap(NULL, PERF_MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (!ASSERT_NEQ(ptr, MAP_FAILED, "mmap")) { + close(fd); + return -1; + } + + perf_fd = fd; + perf_ringbuf = ptr; + + return 0; +} + +static void close_perf_event(void) +{ + if (perf_fd == -1) + return; + + munmap(perf_ringbuf, PERF_MMAP_SIZE); + close(perf_fd); + + perf_fd = -1; + perf_ringbuf = NULL; +} + +static noinline void *trigger_perf_event(void) +{ + int *buf = mmap(NULL, DATA_MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); + + if (!ASSERT_NEQ(buf, MAP_FAILED, "mmap")) + return NULL; + + ioctl(perf_fd, PERF_EVENT_IOC_ENABLE); + + /* it should generate a page fault which triggers the perf_event */ + *buf = 1; + + ioctl(perf_fd, PERF_EVENT_IOC_DISABLE); + + munmap(buf, DATA_MMAP_SIZE); + + /* return the map address to check the sample addr */ + return buf; +} + +/* check if the perf ringbuf has a sample data */ +static int check_perf_event(void) +{ + struct perf_event_mmap_page *page = perf_ringbuf; + struct perf_event_header *hdr; + + if (page->data_head == page->data_tail) + return 0; + + hdr = perf_ringbuf + page->data_offset; + + if (hdr->type != PERF_RECORD_SAMPLE) + return 0; + + return 1; +} + +static void setup_perf_sample_bpf_skel(void) +{ + struct bpf_link *link; + + skel = test_perf_sample__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_perf_sample_open_and_load")) + return; + + link = bpf_program__attach_perf_event(skel->progs.perf_sample_filter, perf_fd); + if (!ASSERT_OK_PTR(link, "bpf_program__attach_perf_event")) + return; +} + +static void clean_perf_sample_bpf_skel(void) +{ + test_perf_sample__detach(skel); + test_perf_sample__destroy(skel); +} + +static void test_perf_event_read_sample_ok(void) +{ + u64 flags = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR; + uintptr_t map_addr; + + if (open_perf_event(flags) < 0) + return; + setup_perf_sample_bpf_skel(); + map_addr = (uintptr_t)trigger_perf_event(); + + ASSERT_EQ(check_perf_event(), 1, "number of sample"); + ASSERT_EQ(skel->bss->sample_addr, map_addr, "sample addr"); + ASSERT_EQ((int)skel->bss->sample_pid, getpid(), "sample pid"); + /* just assume the IP in (trigger_perf_event, +4096) */ + ASSERT_GT(skel->bss->sample_ip, (uintptr_t)trigger_perf_event, "sample ip"); + ASSERT_LT(skel->bss->sample_ip, (uintptr_t)trigger_perf_event + 4096, "sample ip"); + + clean_perf_sample_bpf_skel(); + close_perf_event(); +} + +static void test_perf_event_read_sample_invalid(void) +{ + u64 flags = PERF_SAMPLE_INVALID; + + if (open_perf_event(flags) < 0) + return; + setup_perf_sample_bpf_skel(); + trigger_perf_event(); + + ASSERT_EQ(check_perf_event(), 0, "number of sample"); + + clean_perf_sample_bpf_skel(); + close_perf_event(); +} + +void test_perf_event_read_sample(void) +{ + if (test__start_subtest("perf_event_read_sample_ok")) + test_perf_event_read_sample_ok(); + if (test__start_subtest("perf_event_read_sample_invalid")) + test_perf_event_read_sample_invalid(); +} diff --git a/tools/testing/selftests/bpf/progs/test_perf_sample.c b/tools/testing/selftests/bpf/progs/test_perf_sample.c new file mode 100644 index 000000000000..b1f498d447b9 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_perf_sample.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2022 Google + +#include "vmlinux.h" +#include + +unsigned long long sample_ip; +unsigned long long sample_pid; +unsigned long long sample_addr; + +void *bpf_cast_to_kern_ctx(void *) __ksym; + +#define SAMPLE_FLAGS (PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_ADDR) + +SEC("perf_event") +int perf_sample_filter(void *ctx) +{ + struct bpf_perf_event_data_kern *kctx; + + kctx = bpf_cast_to_kern_ctx(ctx); + + if ((kctx->event->attr.sample_type & SAMPLE_FLAGS) != SAMPLE_FLAGS) + return 0; + + sample_ip = kctx->data->ip; + sample_pid = kctx->data->tid_entry.pid; + sample_addr = kctx->data->addr; + + /* generate sample data */ + return 1; +} + +char _license[] SEC("license") = "GPL";