From patchwork Wed Jul 7 19:46:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363983 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C86CCC07E95 for ; Wed, 7 Jul 2021 19:46:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF61F61CC9 for ; Wed, 7 Jul 2021 19:46:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232683AbhGGTtN (ORCPT ); Wed, 7 Jul 2021 15:49:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:37273 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231721AbhGGTtN (ORCPT ); Wed, 7 Jul 2021 15:49:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QgBr4LV9SR6/Qelufj3zindVEajUhYS2+l3zjx7rHSU=; b=LnVMXQFH5pgY3ojaN0hM8HLv970Zpb82XHV+zT1gXHIwUuO8Om/4c/NiWUKOlBAIQ6MLmf iVUh+YdUKT6dQeQjT+pIZJzqBvafPFR0JnAaqUtcQo1iI0J5D25R5u1nNnOk7DWQ8Y0+Gv mMwfn45MpGDr3HJ7Y1Qc/OD8v9MLjVE= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-597-8vWHPAKUNH2IornOAQ5OUg-1; Wed, 07 Jul 2021 15:46:31 -0400 X-MC-Unique: 8vWHPAKUNH2IornOAQ5OUg-1 Received: by mail-wr1-f69.google.com with SMTP id m10-20020a5d64aa0000b029013370949d6bso1140522wrp.1 for ; Wed, 07 Jul 2021 12:46:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QgBr4LV9SR6/Qelufj3zindVEajUhYS2+l3zjx7rHSU=; b=n7U/K5goNn81SQCd1nYvbVLaYDs60oUegSOu/XWhwzOebLhhYLCdc8qlGPOvMp+Wqh MIxdiMZNkBud6/ScsvIkAU3dqlkMntlElMR3T2QrB4yi91jj0m5S8/hFNTvYuueFPlCR g0b+djPmd2a/Z1fX5ou7i/znzLq4u9fHnQCCfG3vUKEUZqC1hCiDTjH5/dHfjwa/3Y59 vu4FaiQd6tTflriGHvjfxns7gHXMtNkbc7AGkDcM+AyP/SFo5vHSgMdJlHSI00Wd3yxr LigTu0VQ4s9mGYzXciV652FoG4P1rZhgj4TBjl5JW+6mspJfzKsaKqmUmMVhTr0BxgmC u5pw== X-Gm-Message-State: AOAM531LCg4xQJRMEujmcAlq6wzHf9oYrTbVu3VKe6/NqUWZ7Jzl3Gme 1Vp/XbYrlWAdhlPS75x+MsdF0xd9wT+oyNr9RGb21u40UP+hPMYL9BcfT+w07K4RoknvGSn1mPm 1zbBJQHfOdL00 X-Received: by 2002:adf:9084:: with SMTP id i4mr30099342wri.23.1625687190056; Wed, 07 Jul 2021 12:46:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLO79EMn3rV783LaMxncMKtw60ZojKfQRrfALZnttyQBWpER13DmAyWOA56j8/XujiSgM20A== X-Received: by 2002:adf:9084:: with SMTP id i4mr30099335wri.23.1625687189932; Wed, 07 Jul 2021 12:46:29 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id b7sm17543795wri.96.2021.07.07.12.46.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:29 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 1/7] bpf, x86: Store caller's ip in trampoline stack Date: Wed, 7 Jul 2021 21:46:13 +0200 Message-Id: <20210707194619.151676-2-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Storing caller's ip in trampoline's stack. Trampoline programs can reach the IP in (ctx - 8) address, so there's no change in program's arguments interface. The IP address is takes from [fp + 8], which is return address from the initial 'call fentry' call to trampoline. This IP address will be returned via bpf_get_func_ip helper helper, which is added in following patches. Signed-off-by: Jiri Olsa --- arch/x86/net/bpf_jit_comp.c | 19 +++++++++++++++++++ include/linux/bpf.h | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index e835164189f1..c320b3ce7b58 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1951,6 +1951,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i if (flags & BPF_TRAMP_F_CALL_ORIG) stack_size += 8; /* room for return value of orig_call */ + if (flags & BPF_TRAMP_F_IP_ARG) + stack_size += 8; /* room for IP address argument */ + if (flags & BPF_TRAMP_F_SKIP_FRAME) /* skip patched call instruction and point orig_call to actual * body of the kernel function. @@ -1964,6 +1967,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i EMIT4(0x48, 0x83, 0xEC, stack_size); /* sub rsp, stack_size */ EMIT1(0x53); /* push rbx */ + if (flags & BPF_TRAMP_F_IP_ARG) { + /* Store IP address of the traced function: + * mov rax, QWORD PTR [rbp + 8] + * sub rax, X86_PATCH_SIZE + * mov QWORD PTR [rbp - stack_size], rax + */ + emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, 8); + EMIT4(0x48, 0x83, 0xe8, X86_PATCH_SIZE); + emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -stack_size); + + /* Continue with stack_size for regs storage, stack will + * be correctly restored with 'leave' instruction. + */ + stack_size -= 8; + } + save_regs(m, &prog, nr_args, stack_size); if (flags & BPF_TRAMP_F_CALL_ORIG) { diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f309fc1509f2..6b3da9bc3d16 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -554,6 +554,11 @@ struct btf_func_model { */ #define BPF_TRAMP_F_SKIP_FRAME BIT(2) +/* Store IP address of the caller on the trampoline stack, + * so it's available for trampoline's programs. + */ +#define BPF_TRAMP_F_IP_ARG BIT(3) + /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 */ From patchwork Wed Jul 7 19:46:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363985 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63203C07E95 for ; Wed, 7 Jul 2021 19:46:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4A4AC61CBE for ; Wed, 7 Jul 2021 19:46:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232598AbhGGTtV (ORCPT ); Wed, 7 Jul 2021 15:49:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:56261 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230002AbhGGTtU (ORCPT ); Wed, 7 Jul 2021 15:49:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jm5cd0zOZ7Itm+FjlMPQhsBe4LoJlMQjbASHrTs/ggA=; b=R5qCsHGOgoTEfrP06iT+d+Pv2GQ60eL0ruZHqI2/HhZ7m5ZStGgaB2xiF4Rx+CDf+QrV7W Q3zWb2I1Hb1ddXTjYMgDzcQZn0aO4YPTxsrTQ/apCb1B6y1hmhg7YwNb4UwqRci/YklimZ bbrrxV3ChfM/6H7BMDIkxmVsrXIMx9I= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-573-Ws8H4-7xNUyYQ4vWjKMYPg-1; Wed, 07 Jul 2021 15:46:38 -0400 X-MC-Unique: Ws8H4-7xNUyYQ4vWjKMYPg-1 Received: by mail-wm1-f72.google.com with SMTP id n37-20020a05600c3ba5b02901fe49ba3bd0so1412893wms.1 for ; Wed, 07 Jul 2021 12:46:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Jm5cd0zOZ7Itm+FjlMPQhsBe4LoJlMQjbASHrTs/ggA=; b=ob31Dp4Sr50FeK9DyZIm/HDPX3W4xLduZX/3ggfoEtBxrtvnrm7Z8uImzE3dTleNOn EluqAD6s2VpkXW0iiyY5OkkX6jGV3vhBb8vDnTt9wyBTny5Sc4tkv/uf9NesG0Un2tvs 6DY9Iggvk1jhUvh0UyUNWCaA2L3BuA2xor0OyQCmKzoGgdjCVhZQIBamMfPpCU2XwUCr Rx9BoGqd1Px506/RW1L5iupTXYHnwHMG6IUIaFxqqZi7S4Wx/Z14tLqOpgklSgG8Y397 y58xGOPJzCbrghqen8T/8mTttYrKzxvkZJti+opmaIgc37iT2JONn+MA47Zsafbd+a7j /+4w== X-Gm-Message-State: AOAM532+W0a7t1rqrXLhkXAEdC3I+Lif2rFzOh3cIGGZPOUY1euvkh7F EK1Z8BuFZAiicAWLDMCSWURb9q1tuwWYF6t8WbH+J78NaxIUhX2AOCz4HxsxXHJdntvBqcIe75i j8YUmin5LmGfW X-Received: by 2002:a5d:5271:: with SMTP id l17mr5906661wrc.239.1625687197789; Wed, 07 Jul 2021 12:46:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBe+9LG+vQmU7WuM+qgcXfsAzLMlWz9Xa0cUggl+MXKe4W6WQVS7iSBwX1Zs5rJPTANSyCSQ== X-Received: by 2002:a5d:5271:: with SMTP id l17mr5906648wrc.239.1625687197613; Wed, 07 Jul 2021 12:46:37 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id l1sm6980416wme.11.2021.07.07.12.46.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:37 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 2/7] bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip Date: Wed, 7 Jul 2021 21:46:14 +0200 Message-Id: <20210707194619.151676-3-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Enabling BPF_TRAMP_F_IP_ARG for trampolines that actually need it. The BPF_TRAMP_F_IP_ARG adds extra 3 instructions to trampoline code and is used only by programs with bpf_get_func_ip helper, which is added in following patch and sets call_get_func_ip bit. This patch ensures that BPF_TRAMP_F_IP_ARG flag is used only for trampolines that have programs with call_get_func_ip set. Signed-off-by: Jiri Olsa --- include/linux/filter.h | 3 ++- kernel/bpf/trampoline.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index 472f97074da0..ba36989f711a 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -559,7 +559,8 @@ struct bpf_prog { kprobe_override:1, /* Do we override a kprobe? */ has_callchain_buf:1, /* callchain buffer allocated? */ 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_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ + call_get_func_ip:1; /* Do we call get_func_ip() */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ u32 len; /* Number of filter blocks */ diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 28a3630c48ee..b2535acfe9db 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -172,7 +172,7 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) } static struct bpf_tramp_progs * -bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total) +bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) { const struct bpf_prog_aux *aux; struct bpf_tramp_progs *tprogs; @@ -189,8 +189,10 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total) *total += tr->progs_cnt[kind]; progs = tprogs[kind].progs; - hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) + hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) { + *ip_arg |= aux->prog->call_get_func_ip; *progs++ = aux->prog; + } } return tprogs; } @@ -333,9 +335,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) struct bpf_tramp_image *im; struct bpf_tramp_progs *tprogs; u32 flags = BPF_TRAMP_F_RESTORE_REGS; + bool ip_arg = false; int err, total; - tprogs = bpf_trampoline_get_progs(tr, &total); + tprogs = bpf_trampoline_get_progs(tr, &total, &ip_arg); if (IS_ERR(tprogs)) return PTR_ERR(tprogs); @@ -357,6 +360,9 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs) flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; + if (ip_arg) + flags |= BPF_TRAMP_F_IP_ARG; + err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE, &tr->func.model, flags, tprogs, tr->func.addr); From patchwork Wed Jul 7 19:46:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363987 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 407E4C07E95 for ; Wed, 7 Jul 2021 19:46:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29B1F61CBE for ; Wed, 7 Jul 2021 19:46:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232700AbhGGTt3 (ORCPT ); Wed, 7 Jul 2021 15:49:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50958 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232672AbhGGTt2 (ORCPT ); Wed, 7 Jul 2021 15:49:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FQtsSphgnYl9qFWBMJGlJNrhM3rcnyrmEFUQ3XZzsHM=; b=hPv4Y+Db+TnTVyPRR8Sfm5bPkHrkxRFVZA+AyurMktUNEu8fDaNN37gl3fi3kHzYUwwD5x 8PsRZrh9sshw3G8m+y6G4G3GlG0zjohhEHX1M4U7fgh6SJiJjz0YGLGwUuWO7tL0sgb7Fd 8YhtPjHLFbSJFlNRO5VtQOqV1BcXQro= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-486-G2LEB9ffN-WgmJ1Sds1WgQ-1; Wed, 07 Jul 2021 15:46:46 -0400 X-MC-Unique: G2LEB9ffN-WgmJ1Sds1WgQ-1 Received: by mail-wm1-f71.google.com with SMTP id z4-20020a1ce2040000b02901ee8d8e151eso2890410wmg.1 for ; Wed, 07 Jul 2021 12:46:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FQtsSphgnYl9qFWBMJGlJNrhM3rcnyrmEFUQ3XZzsHM=; b=ufBbP0jhoeKHViVZv5MPq2ojtSz8mBKA1lrWklTJoZisqkuZP2poBAY5l3J5/zAoU2 49kOxj+NiJFvKZxN2OtmF8YNekKW8zIWf8HHdupdEw6LDE5fneHyXHYDpnQePCNKRrsN +dhWAVOzLVhDreC4kllh2NZjS1TX+ct7sh3Q8LO4G/lVaXLhUYffJwqa6MBU/oHTi4ii B0LDtv3QxQ/vXRZ9RXj+UQu7FnlROgFwcHz58+UfkLmztYmhcZkBRGXBB3OscWsYBorc 5e8DR+HTxsvZSVxqcYLllatT08V3Zc6xBOKnPR8GSV2KZjFIRR/ShU2LhfOxU7peB22x TWJg== X-Gm-Message-State: AOAM5304QnuJGzVc0bm8dd69wokFEUrMAjyk8rupBrd55ja5l9EMJ0pR LqdUd6pavWRTOnj23C+ML/xAW6E9TBFnzPucKTfZdwtBCXaNB3ShBYr7tvBU8EVrME1X5gNyGx/ aoF3jD6Hj9Pvx X-Received: by 2002:a05:600c:4b98:: with SMTP id e24mr29173952wmp.61.1625687205432; Wed, 07 Jul 2021 12:46:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysmUi2r9/ejoClvXbNGkVGIne0VRSRhD66jvlDPHpaqc/LLRXHwdCN6MdaDStGbnsXXyPaTw== X-Received: by 2002:a05:600c:4b98:: with SMTP id e24mr29173919wmp.61.1625687205174; Wed, 07 Jul 2021 12:46:45 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id b9sm25293403wrh.81.2021.07.07.12.46.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:44 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel test robot , Dan Carpenter , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 3/7] bpf: Add bpf_get_func_ip helper for tracing programs Date: Wed, 7 Jul 2021 21:46:15 +0200 Message-Id: <20210707194619.151676-4-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding bpf_get_func_ip helper for BPF_PROG_TYPE_TRACING programs, specifically for all trampoline attach types. The trampoline's caller IP address is stored in (ctx - 8) address. so there's no reason to actually call the helper, but rather fixup the call instruction and return [ctx - 8] value directly (suggested by Alexei). [fixed has_get_func_ip wrong return type] Reported-by: kernel test robot Reported-by: Dan Carpenter Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 7 +++++ kernel/bpf/verifier.c | 53 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 15 ++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++ 4 files changed, 82 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index bf9252c7381e..83e87ffdbb6e 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4780,6 +4780,12 @@ union bpf_attr { * Execute close syscall for given FD. * Return * A syscall result. + * + * u64 bpf_get_func_ip(void *ctx) + * Description + * Get address of the traced function (for tracing programs). + * Return + * Address of the traced function. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4951,6 +4957,7 @@ union bpf_attr { FN(sys_bpf), \ FN(btf_find_by_name_kind), \ FN(sys_close), \ + FN(get_func_ip), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index be38bb930bf1..f975a3aa9368 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5149,6 +5149,11 @@ static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); } +static bool allow_get_func_ip_tracing(struct bpf_verifier_env *env) +{ + return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); +} + static int check_map_func_compatibility(struct bpf_verifier_env *env, struct bpf_map *map, int func_id) { @@ -5955,6 +5960,32 @@ static int check_bpf_snprintf_call(struct bpf_verifier_env *env, return err; } +static int has_get_func_ip(struct bpf_verifier_env *env) +{ + enum bpf_attach_type eatype = env->prog->expected_attach_type; + enum bpf_prog_type type = resolve_prog_type(env->prog); + int func_id = BPF_FUNC_get_func_ip; + + if (type == BPF_PROG_TYPE_TRACING) { + if (eatype != BPF_TRACE_FENTRY && eatype != BPF_TRACE_FEXIT && + eatype != BPF_MODIFY_RETURN) { + verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", + func_id_name(func_id), func_id); + return -ENOTSUPP; + } + if (!allow_get_func_ip_tracing(env)) { + verbose(env, "func %s#%d for tracing programs supported only for JITed x86_64\n", + func_id_name(func_id), func_id); + return -ENOTSUPP; + } + return 0; + } + + verbose(env, "func %s#%d not supported for program type %d\n", + func_id_name(func_id), func_id, type); + return -ENOTSUPP; +} + static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, int *insn_idx_p) { @@ -6225,6 +6256,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) env->prog->call_get_stack = true; + if (func_id == BPF_FUNC_get_func_ip) { + if (has_get_func_ip(env)) + return -ENOTSUPP; + env->prog->call_get_func_ip = true; + } + if (changes_data) clear_all_pkt_pointers(env); return 0; @@ -12369,6 +12406,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env) { struct bpf_prog *prog = env->prog; bool expect_blinding = bpf_jit_blinding_enabled(prog); + enum bpf_prog_type prog_type = resolve_prog_type(prog); struct bpf_insn *insn = prog->insnsi; const struct bpf_func_proto *fn; const int insn_cnt = prog->len; @@ -12702,6 +12740,21 @@ static int do_misc_fixups(struct bpf_verifier_env *env) continue; } + /* Implement bpf_get_func_ip inline. */ + if (prog_type == BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_get_func_ip) { + /* Load IP address from ctx - 8 */ + insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); + if (!new_prog) + return -ENOMEM; + + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; + continue; + } + patch_call_imm: fn = env->ops->get_func_proto(insn->imm, env->prog); /* all functions that have prototype and verifier allowed diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 64bd2d84367f..9edd3b1a00ad 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -948,6 +948,19 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .arg5_type = ARG_ANYTHING, }; +BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) +{ + /* Stub, the helper call is inlined in the program. */ + return 0; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { + .func = bpf_get_func_ip_tracing, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, +}; + const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1058,6 +1071,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_for_each_map_elem_proto; case BPF_FUNC_snprintf: return &bpf_snprintf_proto; + case BPF_FUNC_get_func_ip: + return &bpf_get_func_ip_proto_tracing; default: return NULL; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index bf9252c7381e..83e87ffdbb6e 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4780,6 +4780,12 @@ union bpf_attr { * Execute close syscall for given FD. * Return * A syscall result. + * + * u64 bpf_get_func_ip(void *ctx) + * Description + * Get address of the traced function (for tracing programs). + * Return + * Address of the traced function. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4951,6 +4957,7 @@ union bpf_attr { FN(sys_bpf), \ FN(btf_find_by_name_kind), \ FN(sys_close), \ + FN(get_func_ip), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper From patchwork Wed Jul 7 19:46:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363989 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0058C07E95 for ; Wed, 7 Jul 2021 19:46:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8910E61CB0 for ; Wed, 7 Jul 2021 19:46:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232357AbhGGTti (ORCPT ); Wed, 7 Jul 2021 15:49:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:26449 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232772AbhGGTtg (ORCPT ); Wed, 7 Jul 2021 15:49:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687215; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XyCTtNsvJM23bTJlhCXcXFu90hTwq9G3m+Uc2FcCDhw=; b=Bh2OhR4RGO5qS0TiPtEs/I9B2eoNjXVT75srB3ukIvyjB7wLxZTyiS9TrZz/2ErMVwhmDY XpZedWmxWGggckrgr1aJ3klx0rVwnDBd2HNWQh26d+mYB1s3dFKvPFTSquAycUR7xElVte HJspj/C65LqPbI2lZWRTN/1ewsoQD1Y= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-557-3z-_NYNEO-KUeaT_H2vf0Q-1; Wed, 07 Jul 2021 15:46:54 -0400 X-MC-Unique: 3z-_NYNEO-KUeaT_H2vf0Q-1 Received: by mail-wr1-f69.google.com with SMTP id m10-20020a5d64aa0000b029013370949d6bso1140657wrp.1 for ; Wed, 07 Jul 2021 12:46:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XyCTtNsvJM23bTJlhCXcXFu90hTwq9G3m+Uc2FcCDhw=; b=AnlN5HLKaIKhJLbODd2nt1ZKWyioMZvcOJdeCh9KuSPl3l0C94BfOVA33D4rz4CN/X ZxjW094KhKEN3Vh583WQAcVZcpNooGl8eOLJnhhXRA04laDPpo7XZi7S1fqw+dFicBfX wDyxPCvhyVVuBr29QBLz7hIn+x+ndoYl00NvFetTuGVj/zTzLrANNH0jmrXRFOChc3LE QuSX+Jt2a3YGx9MPYzlmJWzXlDppCDA7dNBL1YC6G+3NGxLIcYX7gCPSxDlr+gTIhJIC 22N4rUyMFWtX6xb1yzLlfQlFTwOON3Aq7mgYRzV5Q+Hrd05j/fE8RArveNvz7AZMOAtf 9XRg== X-Gm-Message-State: AOAM530I52Rg9YNtJi1Fk5xfDrCWnAbUoCHotxyOfbTbrG+/xK/KVn2Z N1174ZJg/IYO+0/ZKkVDTh18dbDZywjJHpJ09+2UCsdXKXZ8G+REVe8iMG6GuDeOEpKfL/LTO/W tXLgGhUOjpIRl X-Received: by 2002:a05:600c:1c8f:: with SMTP id k15mr28858768wms.91.1625687212842; Wed, 07 Jul 2021 12:46:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwu7rf02t2v0dKPwUDOi8KjZXmcJxl0X3f27O0ei2he77A+R1yn8AOCE2VQNGl53nogDYsQeA== X-Received: by 2002:a05:600c:1c8f:: with SMTP id k15mr28858743wms.91.1625687212688; Wed, 07 Jul 2021 12:46:52 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id t9sm21265548wrq.92.2021.07.07.12.46.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:52 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 4/7] bpf: Add bpf_get_func_ip helper for kprobe programs Date: Wed, 7 Jul 2021 21:46:16 +0200 Message-Id: <20210707194619.151676-5-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding bpf_get_func_ip helper for BPF_PROG_TYPE_KPROBE programs, so it's now possible to call bpf_get_func_ip from both kprobe and kretprobe programs. Taking the caller's address from 'struct kprobe::addr', which is defined for both kprobe and kretprobe. Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 2 +- kernel/bpf/verifier.c | 2 ++ kernel/trace/bpf_trace.c | 17 +++++++++++++++++ tools/include/uapi/linux/bpf.h | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 83e87ffdbb6e..4894f99a1993 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4783,7 +4783,7 @@ union bpf_attr { * * u64 bpf_get_func_ip(void *ctx) * Description - * Get address of the traced function (for tracing programs). + * Get address of the traced function (for tracing and kprobe programs). * Return * Address of the traced function. */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f975a3aa9368..79eb9d81a198 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5979,6 +5979,8 @@ static int has_get_func_ip(struct bpf_verifier_env *env) return -ENOTSUPP; } return 0; + } else if (type == BPF_PROG_TYPE_KPROBE) { + return 0; } verbose(env, "func %s#%d not supported for program type %d\n", diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 9edd3b1a00ad..55acf56b0c3a 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -961,6 +962,20 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs) +{ + struct kprobe *kp = kprobe_running(); + + return kp ? (u64) kp->addr : 0; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { + .func = bpf_get_func_ip_kprobe, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, +}; + const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1092,6 +1107,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_override_return: return &bpf_override_return_proto; #endif + case BPF_FUNC_get_func_ip: + return &bpf_get_func_ip_proto_kprobe; default: return bpf_tracing_func_proto(func_id, prog); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 83e87ffdbb6e..4894f99a1993 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4783,7 +4783,7 @@ union bpf_attr { * * u64 bpf_get_func_ip(void *ctx) * Description - * Get address of the traced function (for tracing programs). + * Get address of the traced function (for tracing and kprobe programs). * Return * Address of the traced function. */ From patchwork Wed Jul 7 19:46:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363991 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8B3EC07E95 for ; Wed, 7 Jul 2021 19:47:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B9D7361CC1 for ; Wed, 7 Jul 2021 19:47:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232955AbhGGTtp (ORCPT ); Wed, 7 Jul 2021 15:49:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:31679 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232660AbhGGTtn (ORCPT ); Wed, 7 Jul 2021 15:49:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yu/yssvroh5wN9vZzKT4DxZ0/5KDe3VlIF6FttuA20I=; b=c/UsD7c46hVY3hZPqRFocV2h5QGvwTEXMMG9AeR5Zv0hk886gxKoB6kwtEjmzUpHCdkHEc QtFzL/sNbH0crvN7+uwX4jQASc1falXGl4QKM1srVHBztrkNsPbuXfFBR0Uh/fHsMx48H8 x9REzrT4zeF3b3xLhTPw1KfxLmE+zzc= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-423-b5BOhn4kNBC6euHr4okRmA-1; Wed, 07 Jul 2021 15:47:01 -0400 X-MC-Unique: b5BOhn4kNBC6euHr4okRmA-1 Received: by mail-wm1-f71.google.com with SMTP id j38-20020a05600c1c26b02901dbf7d18ff8so2871302wms.8 for ; Wed, 07 Jul 2021 12:47:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Yu/yssvroh5wN9vZzKT4DxZ0/5KDe3VlIF6FttuA20I=; b=iOWwLhTiUAs9NU3qoAV+JKgGDxgAfRT71lMCgpowWF3FOGU1R+gTMBNGsVlxrt4nr7 8D2AR5aPs2gqXz8kG6WMpM+UsUQCBoXzTKOZEdzfHxjX26mg9xtAfW8WsupCNqiPvlzy mojBxksnAjlrqNqN1QXCt6f1jVfKO/o06Aq1HsQXWgAor8OoPsRycTlOu/keq/1tXzlW fJLC8kJqqZj+939BRXcSt2pTvj09+jNYMtBeQ3mpqumVYcIZJrbQT3Deaf+U1iOd+llq UN/NTdrCabEGv5JCNfuFkSPo+mCC4Mz14yr13Hc9ygDkBP8QaefRLKDSf9zCCxA5t9ut t7xw== X-Gm-Message-State: AOAM531kzmpMSM1BspbWUrowVWXjmAZg+Z95mZrAu/BMicKsw1BKTiKH OhR5XZpOMIN0xhP/UA1tt8RiCPnU59N8cdefCtECIwkO/Dujnl1C4pCr7Z6klskcMEeAOH/G0Pf WO8mnk/x1XxIu X-Received: by 2002:a7b:c0c6:: with SMTP id s6mr800187wmh.176.1625687220104; Wed, 07 Jul 2021 12:47:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4ko4VbSlhXPsLn2IGSoVpTJFVa9bwyIz+SEM80cwEG3J9oL6KvVet35WTjUpZbLWaKDSmkA== X-Received: by 2002:a7b:c0c6:: with SMTP id s6mr800164wmh.176.1625687219924; Wed, 07 Jul 2021 12:46:59 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id c16sm19416281wmr.2.2021.07.07.12.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:59 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 5/7] selftests/bpf: Add test for bpf_get_func_ip helper Date: Wed, 7 Jul 2021 21:46:17 +0200 Message-Id: <20210707194619.151676-6-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding test for bpf_get_func_ip helper for fentry, fexit, kprobe, kretprobe and fmod_ret programs. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/get_func_ip_test.c | 42 +++++++++++++ .../selftests/bpf/progs/get_func_ip_test.c | 62 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c create mode 100644 tools/testing/selftests/bpf/progs/get_func_ip_test.c diff --git a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c new file mode 100644 index 000000000000..06d34f566bbb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "get_func_ip_test.skel.h" + +void test_get_func_ip_test(void) +{ + struct get_func_ip_test *skel = NULL; + __u32 duration = 0, retval; + int err, prog_fd, i; + __u64 *result; + + skel = get_func_ip_test__open_and_load(); + if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open_and_load")) + goto cleanup; + + err = get_func_ip_test__attach(skel); + if (!ASSERT_OK(err, "get_func_ip_test__attach")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.test1); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + prog_fd = bpf_program__fd(skel->progs.fmod_ret_test); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + + ASSERT_OK(err, "test_run"); + + result = (__u64 *)skel->bss; + for (i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) { + if (!ASSERT_EQ(result[i], 1, "fentry_result")) + break; + } + + get_func_ip_test__detach(skel); + +cleanup: + get_func_ip_test__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c new file mode 100644 index 000000000000..8ca54390d2b1 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +extern const void bpf_fentry_test1 __ksym; +extern const void bpf_fentry_test2 __ksym; +extern const void bpf_fentry_test3 __ksym; +extern const void bpf_fentry_test4 __ksym; +extern const void bpf_modify_return_test __ksym; + +__u64 test1_result = 0; +SEC("fentry/bpf_fentry_test1") +int BPF_PROG(test1, int a) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test1_result = (const void *) addr == &bpf_fentry_test1; + return 0; +} + +__u64 test2_result = 0; +SEC("fexit/bpf_fentry_test2") +int BPF_PROG(test2, int a) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test2_result = (const void *) addr == &bpf_fentry_test2; + return 0; +} + +__u64 test3_result = 0; +SEC("kprobe/bpf_fentry_test3") +int test3(struct pt_regs *ctx) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test3_result = (const void *) addr == &bpf_fentry_test3; + return 0; +} + +__u64 test4_result = 0; +SEC("kretprobe/bpf_fentry_test4") +int BPF_KRETPROBE(test4) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test4_result = (const void *) addr == &bpf_fentry_test4; + return 0; +} + +__u64 test5_result = 0; +SEC("fmod_ret/bpf_modify_return_test") +int BPF_PROG(fmod_ret_test, int a, int *b, int ret) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test5_result = (const void *) addr == &bpf_modify_return_test; + return ret; +} From patchwork Wed Jul 7 19:46:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363993 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E88B3C07E95 for ; Wed, 7 Jul 2021 19:47:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D190361CB0 for ; Wed, 7 Jul 2021 19:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232766AbhGGTtw (ORCPT ); Wed, 7 Jul 2021 15:49:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:58442 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233086AbhGGTtu (ORCPT ); Wed, 7 Jul 2021 15:49:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N6Lkp9H+PeXEJohL6vruWVGim8bb8jO4Rintdh3j+00=; b=Z2PpmaxhScqQGLUL6IQM8yOFVU3NPEuV995gMNMgerMFQ5RxYOj6c8BzPSppHwGWZoXL8Y KCs6rS2PJRAoYenBJwSM1W0H5ufXBtRW4ApCpUIHaJkwYCf+hUcWvI7laRm6n5CuPFZzVM D7x9N9UN+jkwdkUzQ+q7fzJbKQSkLts= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-475-kYbJR8WSMeq_rgPzqooUpg-1; Wed, 07 Jul 2021 15:47:08 -0400 X-MC-Unique: kYbJR8WSMeq_rgPzqooUpg-1 Received: by mail-wm1-f71.google.com with SMTP id l3-20020a05600c1d03b029021076e2b2f6so2547590wms.4 for ; Wed, 07 Jul 2021 12:47:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N6Lkp9H+PeXEJohL6vruWVGim8bb8jO4Rintdh3j+00=; b=LzEjqrw36Ri/a25Pk2weuv279ov7YsAIuyak+jbm035lK5REMEetzIigb0xRmMxLgo 9tYGXLHGME7PT+vxRn8QaiNHRNcpm+J3yOYIw7xhHwQbR5eQno3nEE+DrxZXhNdf3wAn qGAeuVtZErgx3OnPIisaaqsg7LWyhzCxCD3kpvz3HtiOAE+KvgHJbKOZkpzvJHM+C8TK Rup1Dd2X5m4zpkTOEfasNb9fUhHjv7rHIECKcypB0/f9F3Tg4fn0Ogy1Buk3PFHa3xRj vn0wEJ1Xa8xJa+lfg2udcgpprDw/1ZJZFf5xmALfO5A4fGU3niCCJEm5D0+mu08Ml6Hv 8wNg== X-Gm-Message-State: AOAM533e5R9VAQC6etT7fDuo85IWHmyeIWOt/Ja6BJjfxYrN8sAg9B1w JmhSk0xLNmDeXnMbfQrt8DA5rvXeh4oTkOhn3GTDdt7Dka3yg7hwlL/k0hfsuFeeLX4Tp380dAR oMQgb2nrinuEZ X-Received: by 2002:a1c:4302:: with SMTP id q2mr792617wma.37.1625687227560; Wed, 07 Jul 2021 12:47:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwx6Hij6OUVN0HPvFYTpi+Y4VHcPBqilpZnkPQP9PAJQEeBMChoVHxNTGuD6GH5ZZ0UJXu2rA== X-Received: by 2002:a1c:4302:: with SMTP id q2mr792598wma.37.1625687227390; Wed, 07 Jul 2021 12:47:07 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id l20sm20208357wmq.3.2021.07.07.12.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:47:07 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 6/7] libbpf: allow specification of "kprobe/function+offset" Date: Wed, 7 Jul 2021 21:46:18 +0200 Message-Id: <20210707194619.151676-7-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alan Maguire kprobes can be placed on most instructions in a function, not just entry, and ftrace and bpftrace support the function+offset notification for probe placement. Adding parsing of func_name into func+offset to bpf_program__attach_kprobe() allows the user to specify SEC("kprobe/bpf_fentry_test5+0x6") ...for example, and the offset can be passed to perf_event_open_probe() to support kprobe attachment. Signed-off-by: Alan Maguire Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1e04ce724240..60c9e3e77684 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10309,11 +10309,25 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, const char *func_name) { char errmsg[STRERR_BUFSIZE]; + char func[BPF_OBJ_NAME_LEN]; + unsigned long offset = 0; struct bpf_link *link; - int pfd, err; + int pfd, err, n; + + n = sscanf(func_name, "%[a-zA-Z0-9_.]+%lx", func, &offset); + if (n < 1) { + err = -EINVAL; + pr_warn("kprobe name is invalid: %s\n", func_name); + return libbpf_err_ptr(err); + } + if (retprobe && offset != 0) { + err = -EINVAL; + pr_warn("kretprobes do not support offset specification\n"); + return libbpf_err_ptr(err); + } - pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name, - 0 /* offset */, -1 /* pid */); + pfd = perf_event_open_probe(false /* uprobe */, retprobe, func, + offset, -1 /* pid */); if (pfd < 0) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, retprobe ? "kretprobe" : "kprobe", func_name, From patchwork Wed Jul 7 19:46:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12363995 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A811EC07E9C for ; Wed, 7 Jul 2021 19:47:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9118661CB0 for ; Wed, 7 Jul 2021 19:47:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233082AbhGGTt7 (ORCPT ); Wed, 7 Jul 2021 15:49:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24772 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233089AbhGGTt6 (ORCPT ); Wed, 7 Jul 2021 15:49:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zbTjAalRT8G7ucBl7KamxhzUVx0mh7mTfA4EZ32X+1U=; b=gwodM7Q5OGzlScxodxH/mBqHiQwVRtah9sz55ovP9qsFTX/Q4eqaLpl6919aB+qLPyjOTz 3cTQxNm6GL32zOc37v6UnJQKsJ5rkll646CMpbXH88Qxc0kgEZ1bSNTQLq/gPVQpCkLsRk 7W0aC0RA8ngkSSwXwWqCv8YMoVlPBU0= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-359-DkZ0Vn3INq-KBLP6aJeRxA-1; Wed, 07 Jul 2021 15:47:16 -0400 X-MC-Unique: DkZ0Vn3INq-KBLP6aJeRxA-1 Received: by mail-wr1-f70.google.com with SMTP id l12-20020a5d410c0000b029012b4f055c9bso1133264wrp.4 for ; Wed, 07 Jul 2021 12:47:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zbTjAalRT8G7ucBl7KamxhzUVx0mh7mTfA4EZ32X+1U=; b=Pw2z0z8eFQ8A5llz7HxoD3tDCogizXjvdbye6+8Lf5HL5x2EzLOk5iDhZAbvMU5zfi eYMwSAH8x1d26w2JimvZbiWVA69fOWS/OOl2DUZnifsksyo97cXa2qEEWOa/pJ81Wvu6 6cz8jr5NMs0svLfvu+66uLT4Y+4xLCqqlaRZLfbKDncuEhHRl26lVH4ZVQa7WVaT0OS1 2upCovi5LJ60A5vuYdApS7YYaZJKc9vxJzIWZ4w+SkDvc8XuyjyDOe9ZaElDaoZ42YPa X77cnCkFgrCtgpcrqbjao4AzkCffEekHwS0D9UQeM+xER82///cIUMRlgEjeBDdToyha ZT0Q== X-Gm-Message-State: AOAM532mxQA3jV/z+IuE3Sg/pIvwJhusk5NOFELaqrgQLeWEL/C9z1Kk zJrfGZWMidFZ0M/Fh1UbyDgtc9mDiPb+/o4vrgJWr0fX580Ykp7xC9i3CW0ZBg9MPB1NJQusR8m Qt8SjDoifCnig X-Received: by 2002:a5d:4d4b:: with SMTP id a11mr5812966wru.325.1625687235395; Wed, 07 Jul 2021 12:47:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCeEyHGUskiHM337qfooE0f3Ro/cCDcwbEMlSi+dsBwOUNqQw0kh8Z0/fopTxAKcSeFTYUDw== X-Received: by 2002:a5d:4d4b:: with SMTP id a11mr5812949wru.325.1625687235220; Wed, 07 Jul 2021 12:47:15 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id l16sm7619853wmj.47.2021.07.07.12.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:47:14 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 7/7] selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe Date: Wed, 7 Jul 2021 21:46:19 +0200 Message-Id: <20210707194619.151676-8-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding test for bpf_get_func_ip in kprobe+ofset probe. Because of the offset value it's arch specific, adding it only for x86_64 architecture. Signed-off-by: Jiri Olsa --- .../testing/selftests/bpf/progs/get_func_ip_test.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c index 8ca54390d2b1..e8a9428a0ea3 100644 --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c @@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym; extern const void bpf_fentry_test3 __ksym; extern const void bpf_fentry_test4 __ksym; extern const void bpf_modify_return_test __ksym; +extern const void bpf_fentry_test6 __ksym; __u64 test1_result = 0; SEC("fentry/bpf_fentry_test1") @@ -60,3 +61,15 @@ int BPF_PROG(fmod_ret_test, int a, int *b, int ret) test5_result = (const void *) addr == &bpf_modify_return_test; return ret; } + +#ifdef __x86_64__ +__u64 test6_result = 0; +SEC("kprobe/bpf_fentry_test6+0x5") +int test6(struct pt_regs *ctx) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test6_result = (const void *) addr == &bpf_fentry_test6 + 5; + return 0; +} +#endif