From patchwork Wed Jul 14 09:43:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477296 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=-19.4 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 0109DC07E9C for ; Wed, 14 Jul 2021 09:44:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D46DB613AA for ; Wed, 14 Jul 2021 09:44:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238927AbhGNJrG (ORCPT ); Wed, 14 Jul 2021 05:47:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:25362 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238923AbhGNJrF (ORCPT ); Wed, 14 Jul 2021 05:47:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255853; 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=js7ItpLeBG6341HxQnnXIxzAVMeXbOzDGxLGSYjQe08=; b=EoxHcs2fIOcmbgqNkbxEmAh9O9c9MeZDQCIubqV4lj7hEqH+amDcdDl5HlzOzSH1RGAQaL eKySBWaVDu6L+4h0Ur9G9R1b6L8BMt8eza1VLABQx/XWkUpQvwjSTlqrXJefwwquvodsDt SQwOEkP6weX+Fzdo8ORkDFCrh6iebDg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-418-OYkSWKfBMyWLiFGlTPex3Q-1; Wed, 14 Jul 2021 05:44:12 -0400 X-MC-Unique: OYkSWKfBMyWLiFGlTPex3Q-1 Received: by mail-wr1-f71.google.com with SMTP id r18-20020adff1120000b029013e2b4ee624so1239126wro.1 for ; Wed, 14 Jul 2021 02:44:12 -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=js7ItpLeBG6341HxQnnXIxzAVMeXbOzDGxLGSYjQe08=; b=oeS0AJ3PteOhOjjaA4+PZpN02W8/JHCMVDGCmFPK7R9NkfXVNF2oVYNctL2IPmwedO 6iZMA4+fm1FFRH+0mEpi8FOnRWa/Ia3DPLoKj/i6jHvSBsv6iGicu82R9ubY3pWIpgaw kqvap9T7VCCRVEd4UW2oksx0VlBkCiWnrfXAvDHrzf+ulGNGy/ECXCW4f3IcrxvJcaF2 fKlNoQrQ5mze2bRSC6Vaf2eakybK/alx05hyXgSqSMNxjyGIyPwIWcOUu4gAbflTiKUT XkYEPDtifm+0NkqqUTNWr23Uf2ZXuTM6XkDsdNehNI1EmqeXE48/WmIkVUuJVgWr3M+X 5W8w== X-Gm-Message-State: AOAM530gvYOMpiqeyW+vGG6AxpwRS3dczN0xt1dQah43C3pUQBnAis+q 8ACZmlMnONKUrQ6t37MqdZBA8FxzuPZYMUy2hxYXKzG/eQPlRS8jts696A9ZSP41Vo7KTGhOQOA YNV3MEr7VFMns4IVs X-Received: by 2002:a05:6000:551:: with SMTP id b17mr11631514wrf.32.1626255851343; Wed, 14 Jul 2021 02:44:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz9wxWDgLR+XvYeobeH3N2tpea63Gzr0Is8aqeGRFYHharxAcPTxU0MECVMqK95yblgf8g04w== X-Received: by 2002:a05:6000:551:: with SMTP id b17mr11631497wrf.32.1626255851148; Wed, 14 Jul 2021 02:44:11 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id g15sm1502900wmh.44.2021.07.14.02.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44:10 -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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 1/8] bpf, x86: Store caller's ip in trampoline stack Date: Wed, 14 Jul 2021 11:43:53 +0200 Message-Id: <20210714094400.396467-2-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 4afbff308ca3..1ebb7690af91 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 14 09:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477896 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=-19.4 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 1522BC11F66 for ; Wed, 14 Jul 2021 09:44:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2E79613B6 for ; Wed, 14 Jul 2021 09:44:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238945AbhGNJrN (ORCPT ); Wed, 14 Jul 2021 05:47:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:37861 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238930AbhGNJrM (ORCPT ); Wed, 14 Jul 2021 05:47:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255861; 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=Qwn8KMxfTojYxcZ63zQvWSgXlx01/5vJSB0TcI4zFpKhKonLKD2ArcH4mXN2ZJztJcKiHc lDd05+aZMOJ4lEX0mw3gsbJpKMuIx3NIs0MjQqzF9TKEnMKdg6IE/J4ovvnJGgQNr56jo5 s4jbqqUvtvjdGBaiSdJmuj/q/lJKMqg= 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-170-JL0KtywNNZu21jF702OJXg-1; Wed, 14 Jul 2021 05:44:20 -0400 X-MC-Unique: JL0KtywNNZu21jF702OJXg-1 Received: by mail-wr1-f69.google.com with SMTP id d9-20020adffbc90000b029011a3b249b10so1212893wrs.3 for ; Wed, 14 Jul 2021 02:44:20 -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=SKiq95TIRnajMaXd2VT6TVczK1KwRmG59fcovdEpWqzdYiozFmg/VCq6h4R5jwxtC+ 45nq4oKIel1oJXYJQyAW8KN/KBhptRfYNKwJjaCyq+sg+YGlqgEp82bchHeyBD1i1QxD RvJQ7BsCvexU6rQnw/dMGSwxhXZ1EHUlbAHQqbpXbKqqItINXOLmdALvx/WjyNsCO/CP iSYpy9zQtUBOK/97qScm3ifcXm9EuQMeDs4/x68Ru4QGKZp2sgcKBBBMX7/SBCOkWe0u 72AT7GsZBabcfmwy3rM5juz+G4irp8Facnm7J+KhP+5O4bUqrFLnxjl/Dh4JVpOWh7Dq fUqg== X-Gm-Message-State: AOAM533tEl/277K5PqrMBdtQklr7Ir7t5KJ02UbFXrY7t53MU8azQ0n0 D3hnnUiyBSc2+VN5ExW5hwsDw1/dyy0cw0RCZff2qsSZgsoJCx5aqaLNvzuFd6+OrdQPtwIZP/8 JYbLq4vKXJGaYXYEl X-Received: by 2002:a1c:f206:: with SMTP id s6mr9850235wmc.102.1626255859148; Wed, 14 Jul 2021 02:44:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyF2knkhR2JGKJXdyuYnd2E9BYmdfbnj8CH969zHq91hrlXkVOGyBKLQFuJ08BZMZSrW+0TjQ== X-Received: by 2002:a1c:f206:: with SMTP id s6mr9850211wmc.102.1626255858921; Wed, 14 Jul 2021 02:44:18 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id g3sm1939223wru.95.2021.07.14.02.44.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44:18 -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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 2/8] bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip Date: Wed, 14 Jul 2021 11:43:54 +0200 Message-Id: <20210714094400.396467-3-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 14 09:43:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477295 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=-19.4 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, URIBL_BLOCKED, 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 5E8FDC11F66 for ; Wed, 14 Jul 2021 09:44:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48352601FF for ; Wed, 14 Jul 2021 09:44:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238916AbhGNJrW (ORCPT ); Wed, 14 Jul 2021 05:47:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26848 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238840AbhGNJrV (ORCPT ); Wed, 14 Jul 2021 05:47:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255870; 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=Pq7O3qLWv1NvU3TCdeEA06tLOllu2YQ+xJvUQBt7sDQ=; b=WX/7lBQAkmJc30Ow3+WgGQR+3f1R3fiml4GLoW8/YqrdTFqJMRuHMhNW2rtZT0mJJCGmm2 EpdWP8pEyUoQxxnD4ZaFBHzTn1c7Z/Gaq5St2PafN7IGdcaDA5aqkSaytPEcVjz5HY7q2T 8rvlpKfnzHNS0tmovOfYTn9scK+nBLg= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-602-VBAaEF_HNj2a-MI739vNDw-1; Wed, 14 Jul 2021 05:44:29 -0400 X-MC-Unique: VBAaEF_HNj2a-MI739vNDw-1 Received: by mail-wm1-f69.google.com with SMTP id m40-20020a05600c3b28b02901f42375a73fso567515wms.5 for ; Wed, 14 Jul 2021 02:44:29 -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=Pq7O3qLWv1NvU3TCdeEA06tLOllu2YQ+xJvUQBt7sDQ=; b=oEw2SyTkrNWCLUBuzNrvYDXpU5wLQuHCT/qdOQEVz2eVh+Fuwm8Vf5gcR66B3mYPoc waesMzN8M0f8SHbIDHU7Lrqw4xcTyQ/K2eL52i+KD0ihgQmBJCg6Kudnna/AkbjhIoNz G5zi7xHrAxf2Lb1FqUYIYAYCSxbGP1LeMI5LUqJl3zGW+jd6Q75TOl2wynXTvsZAHJIJ les4CGXh5dc9R/SqB1tm+duGtsq5zJl8yxgnWFWEiG/h1vpTSnxy9wwzYTS2aQY1qxhv NUOZN5CEGHXSUn28vCTQCCUUEikLNHOPt1cGHvsAyhJtqsL/UVcwRPKLeTLs1w25XNFk /Jiw== X-Gm-Message-State: AOAM5306HvpWByMnz9CNoJ/JlTcmojo2zNNr/F0bIs9Vq9Ek48RjC98T oyqEwCjVK1NzwjHyTPAqldVZBsfjeAMQd5mftQ4UYhcNa5ursuRu/qcx8ZW6EeY0LsAnW1nG7Gd JqM2iLzFqw4ViovPj X-Received: by 2002:adf:8061:: with SMTP id 88mr11646712wrk.233.1626255868078; Wed, 14 Jul 2021 02:44:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxeG4DFtzNV4k4TT3JKbi+8jlcLFEkDDmoKnXC4YNGqchGBzeXmFlL25HZ/PKSnoVcVpOGN/g== X-Received: by 2002:adf:8061:: with SMTP id 88mr11646678wrk.233.1626255867907; Wed, 14 Jul 2021 02:44:27 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id v30sm2083621wrv.85.2021.07.14.02.44.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44:27 -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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 3/8] bpf: Add bpf_get_func_ip helper for tracing programs Date: Wed, 14 Jul 2021 11:43:55 +0200 Message-Id: <20210714094400.396467-4-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 | 43 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 15 ++++++++++++ tools/include/uapi/linux/bpf.h | 7 ++++++ 4 files changed, 72 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index b46a383e8db7..31dd386b64ec 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4777,6 +4777,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), \ @@ -4948,6 +4954,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..d27aa23fb572 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5955,6 +5955,27 @@ static int check_bpf_snprintf_call(struct bpf_verifier_env *env, return err; } +static int check_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; + } + 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 +6246,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 (check_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 +12396,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 +12730,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..022cbe42ac57 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) +{ + /* This helper call is inlined by verifier. */ + return ((u64 *)ctx)[-1]; +} + +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 14 09:43:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477895 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=-19.4 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, URIBL_BLOCKED, 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 D3DC9C07E9A for ; Wed, 14 Jul 2021 09:44:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BD9ED613B7 for ; Wed, 14 Jul 2021 09:44:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238945AbhGNJrb (ORCPT ); Wed, 14 Jul 2021 05:47:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50894 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238189AbhGNJra (ORCPT ); Wed, 14 Jul 2021 05:47:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255878; 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=onwZSv1Gm5ECjGOwSW4DJSwBbehKx10mVnDmGzm070k=; b=dNCYxOlQwyS1i4/kfoWPSVyUFhPNcTEQwGShe8PgvYfegfUmHl8OWAqrpDDlS1qm1+dmuc Y+1QgMGpvebI6Com4Iu6ko/hi9VFYicBdhRwbp4PPOAbzFdnFbDhLie6u5VbnKG8X7S3FG 1TWx7hnvOnk8pTU5lzIGl+A4MnV2BaA= 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-65-YquG9wKQOTCUc3RdTxx7-A-1; Wed, 14 Jul 2021 05:44:37 -0400 X-MC-Unique: YquG9wKQOTCUc3RdTxx7-A-1 Received: by mail-wr1-f69.google.com with SMTP id y15-20020a5d614f0000b029013cd60e9baaso1217187wrt.7 for ; Wed, 14 Jul 2021 02:44:37 -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=onwZSv1Gm5ECjGOwSW4DJSwBbehKx10mVnDmGzm070k=; b=n0VsWxapJ9ex194Vd/PGLV5psahF8lnq6LsvQdqKVUtO3bMl4tLEq85xelJAf7r2Rh K7okEC5z1VfG2qyqjLuwic+Ft2g8AGkYU32P+LpQjXzoiQ1fJBffYkQx0h/l/P6s6CVX cr8rS8HEd3PvsfsQ8OgQounIhoA4W3Tc4A/bZimMjoNIwmsG+qZWV/yB3Yq1DOS/c+5c BxnOIcD5V0MrIYdSLgyX6Q6n2mfTD+8TarxAYHI35pe251kZr4PW0WIAYcDp+KzQZfFn sYDUwb5M3RsJybu1vb9PC3ILxRUr07fo2LVW6+qSIgsIaVZ9WVrJvbC/yixBZ6OJCwOE 0AYg== X-Gm-Message-State: AOAM532ebXJUWDlsaVZaQXX4uUN0rcw6GnqvP5s9yTucn6vDoFaDw9J1 uQwNEnLDUiGacNCM0sm+wdRJUXadZCyyzJ9PSeQpvhjehA/RiglWRKxgWX/RcSYnIm0n8uceEtM +vaKYu1vHlqH9Mboe X-Received: by 2002:a7b:ca43:: with SMTP id m3mr10071498wml.74.1626255876117; Wed, 14 Jul 2021 02:44:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZLzYFJUVgvmfR7kLuWngd5KlZkneeiRF9AalFugBgeOOC7+/dgr0yJmi1hXaj4rNp29qerQ== X-Received: by 2002:a7b:ca43:: with SMTP id m3mr10071480wml.74.1626255875882; Wed, 14 Jul 2021 02:44:35 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id g7sm4438007wmq.22.2021.07.14.02.44.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44:35 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel test robot , Masami Hiramatsu , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCHv4 bpf-next 4/8] bpf: Add bpf_get_func_ip helper for kprobe programs Date: Wed, 14 Jul 2021 11:43:56 +0200 Message-Id: <20210714094400.396467-5-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. [removed duplicate include] Reported-by: kernel test robot Reviewed-by: Masami Hiramatsu Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 2 +- kernel/bpf/verifier.c | 2 ++ kernel/trace/bpf_trace.c | 16 ++++++++++++++++ tools/include/uapi/linux/bpf.h | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 31dd386b64ec..3ea5874f603b 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4780,7 +4780,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 d27aa23fb572..9998ffc00bbd 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5969,6 +5969,8 @@ static int check_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 022cbe42ac57..8af385fd5419 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -961,6 +961,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 +1106,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 14 09:43:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477294 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=-19.4 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 6E5B8C07E9A for ; Wed, 14 Jul 2021 09:44:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 59099613B9 for ; Wed, 14 Jul 2021 09:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238958AbhGNJri (ORCPT ); Wed, 14 Jul 2021 05:47:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32582 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238869AbhGNJrh (ORCPT ); Wed, 14 Jul 2021 05:47:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255886; 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=at0v+SiznMJ5rgxjJyNt+4zpti6KLDNE/wwHxHtwGZM=; b=aZAkyhOfN0u/NYi8A5rH9ZdPXzTcfBcG0R8gzR7etiiGoxa0ifkcQH20yvnNQupU1R2zbP GAfOb85cGmTf8UvvCZG/x+/iY7dpSEe1WxTR3wKQyLLxzKSENx8cNb6fLHq3gsf4g3zxiQ 9/cCiD3jUDOQ9oWHcUJoNgYXG/OcqAQ= 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-581-MSudTRR8PDugwhI-wFQdNw-1; Wed, 14 Jul 2021 05:44:45 -0400 X-MC-Unique: MSudTRR8PDugwhI-wFQdNw-1 Received: by mail-wr1-f69.google.com with SMTP id t8-20020a05600001c8b029013e2027cf9aso1198979wrx.9 for ; Wed, 14 Jul 2021 02:44:44 -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=at0v+SiznMJ5rgxjJyNt+4zpti6KLDNE/wwHxHtwGZM=; b=O3e6jlMQh8tcQTalWMP457nWYXbJGXmFZ5rPeCzUP/k9Icf14gqiZGaiocSc/F+jJx ft17IaPPGrHom+awXwjPkzYjmIuJNMeSYDjg1Ue0JzhNHu5eSUs3GoPDpqBVUY3vbHgu 0wRwIJLKYnfu3M+UW2DE3AqYntUBVN7YkHnassX07nXHrD/B3xYfCjDVoWEQvUjfbDLD S++i9f0k0PSzlc0Q443z1rikVvC+TcKq5PLambiW//dFQqOQosoHSzsnRiJmZN8HzJ3H ptfjW8IzzG0PUA3ePhFixTR8P72sfP1qugsAG8ynr/I8ywWP/aW6W56UHe3XZX7XhQtM l4Pg== X-Gm-Message-State: AOAM533H9837pznMZ3S6mPTEqogG9Om+pegouELoilPn4CrN4ZK6dUTi jKldJBZVs4fHpXc1/ASZIXO32ohhEwZxG7krFVfctohdps1xbM4n4uy+mD0RsHeIq1KoBYB3rFq XedD3e0OiqNOKGb98 X-Received: by 2002:a5d:48ce:: with SMTP id p14mr11988514wrs.170.1626255884073; Wed, 14 Jul 2021 02:44:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6N/hErFIrqsMzdSYW/RV4Olk1siG+GOQcYgq66khqzMit1bnG7E/QCI5/JqG0eik2ujfURA== X-Received: by 2002:a5d:48ce:: with SMTP id p14mr11988487wrs.170.1626255883950; Wed, 14 Jul 2021 02:44:43 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id f82sm4654191wmf.25.2021.07.14.02.44.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44:43 -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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 5/8] selftests/bpf: Add test for bpf_get_func_ip helper Date: Wed, 14 Jul 2021 11:43:57 +0200 Message-Id: <20210714094400.396467-6-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 | 39 ++++++++++++ .../selftests/bpf/progs/get_func_ip_test.c | 62 +++++++++++++++++++ 2 files changed, 101 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..8bb18a8d31a0 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c @@ -0,0 +1,39 @@ +// 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; + + skel = get_func_ip_test__open_and_load(); + if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open_and_load")) + return; + + 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.test5); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + + ASSERT_OK(err, "test_run"); + + ASSERT_EQ(skel->bss->test1_result, 1, "test1_result"); + ASSERT_EQ(skel->bss->test2_result, 1, "test2_result"); + ASSERT_EQ(skel->bss->test3_result, 1, "test3_result"); + ASSERT_EQ(skel->bss->test4_result, 1, "test4_result"); + ASSERT_EQ(skel->bss->test5_result, 1, "test5_result"); + +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..ba3e107b52dd --- /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(test5, 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 14 09:43:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477894 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=-19.4 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 7A2F1C07E9C for ; Wed, 14 Jul 2021 09:44:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 62067613C3 for ; Wed, 14 Jul 2021 09:44:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238970AbhGNJrq (ORCPT ); Wed, 14 Jul 2021 05:47:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:51600 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238906AbhGNJrq (ORCPT ); Wed, 14 Jul 2021 05:47:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255894; 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=gS2TUzSMuzLBnMpE+3kD0qi4Lo6QsgvFke4iFwk++og=; b=QZXZz3j58JpAMQE8T8mWZn6v8mF8ZlkEtKSwBsC9lUiOCiWfLZFwq6CQDfC/ieHqp48GXa 8p2I9/MRvT7l4U/FNk8HlnNtCSOzP5L5nymoTGBcQXLsrYVLnPUKEt28+VSA1KN4tKpeuf Jk2YwVN7WpN6mLFvFKXhs8KqZZOVIzM= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-79-hUzKVF_rOv29SWAWcIK98A-1; Wed, 14 Jul 2021 05:44:53 -0400 X-MC-Unique: hUzKVF_rOv29SWAWcIK98A-1 Received: by mail-wm1-f70.google.com with SMTP id j141-20020a1c23930000b0290212502cb19aso574417wmj.0 for ; Wed, 14 Jul 2021 02:44: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=gS2TUzSMuzLBnMpE+3kD0qi4Lo6QsgvFke4iFwk++og=; b=Ja0D8neR6Vt2em3q5zUzfyeaebjDRTQLj0HlfoWgVBUR9TgBgUMLFIwDE3Uynj/3QS TuvltIaX5oD9PBkadal+uctOdqPP6Ks1foTh+cn1LVd/sW8FAF91Sp4d9hhBfhybnz8u LCooLyc5d4iO2ciugCh5kzDKFzxD8QnO1QRzCIazaCDoGYS1i3tvhzsxONBAHS0gJ7fE 9kIzML4789pOKJuGVRJ/D2P/YTzZ7RsmhMTbamLae4xsTCYNZPgso4EPkGKforj9+neD iv0Yt83fkP7Dej7nljEzQx2AC3FuiTpH+DdGNjmuDEnimZ+eFONmsQszL5PGmUDv1jKv NiTA== X-Gm-Message-State: AOAM531jKM57nyVT6SiT6sz0JrhgTjM6620CuCyIlP5WYGMYw3T7kXxH Rr3sy7aXrsTF5eh5VJrT1G4hs6DybZ3hhhrlIgSgtvVnOezzRI5KJyWw5zbxUMlbfNbWvgDa0fi /sOoV3GcjZ0FYsPJS X-Received: by 2002:a5d:58d6:: with SMTP id o22mr12007028wrf.307.1626255892109; Wed, 14 Jul 2021 02:44:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzopcNaGLLt7E6TkmR1jeyOlECEvn3WL1uHMzljuZ/xIarhd2afZNQp9cQ7vRgN7IuLJj4GGQ== X-Received: by 2002:a5d:58d6:: with SMTP id o22mr12007013wrf.307.1626255891909; Wed, 14 Jul 2021 02:44:51 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id a64sm1511979wme.8.2021.07.14.02.44.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44:51 -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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 6/8] libbpf: Add bpf_program__attach_kprobe_opts function Date: Wed, 14 Jul 2021 11:43:58 +0200 Message-Id: <20210714094400.396467-7-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding bpf_program__attach_kprobe_opts that does the same as bpf_program__attach_kprobe, but takes opts argument. Currently opts struct holds just retprobe bool, but we will add new field in following patch. The function is not exported, so there's no need to add size to the struct bpf_program_attach_kprobe_opts for now. Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 88b99401040c..d93a6f9408d1 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, return pfd; } -struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, - bool retprobe, - const char *func_name) +struct bpf_program_attach_kprobe_opts { + bool retprobe; +}; + +static struct bpf_link* +bpf_program__attach_kprobe_opts(struct bpf_program *prog, + const char *func_name, + struct bpf_program_attach_kprobe_opts *opts) { char errmsg[STRERR_BUFSIZE]; struct bpf_link *link; int pfd, err; - pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name, + pfd = perf_event_open_probe(false /* uprobe */, opts->retprobe, func_name, 0 /* 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, + prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); return libbpf_err_ptr(pfd); } @@ -10367,23 +10372,34 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, if (err) { close(pfd); pr_warn("prog '%s': failed to attach to %s '%s': %s\n", - prog->name, retprobe ? "kretprobe" : "kprobe", func_name, + prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); return libbpf_err_ptr(err); } return link; } +struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, + bool retprobe, + const char *func_name) +{ + struct bpf_program_attach_kprobe_opts opts = { + .retprobe = retprobe, + }; + + return bpf_program__attach_kprobe_opts(prog, func_name, &opts); +} + static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, struct bpf_program *prog) { + struct bpf_program_attach_kprobe_opts opts; const char *func_name; - bool retprobe; func_name = prog->sec_name + sec->len; - retprobe = strcmp(sec->sec, "kretprobe/") == 0; + opts.retprobe = strcmp(sec->sec, "kretprobe/") == 0; - return bpf_program__attach_kprobe(prog, retprobe, func_name); + return bpf_program__attach_kprobe_opts(prog, func_name, &opts); } struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, From patchwork Wed Jul 14 09:43:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477293 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=-19.4 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 40ABAC11F66 for ; Wed, 14 Jul 2021 09:45:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DEBC613C5 for ; Wed, 14 Jul 2021 09:45:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238973AbhGNJry (ORCPT ); Wed, 14 Jul 2021 05:47:54 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:46966 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238894AbhGNJry (ORCPT ); Wed, 14 Jul 2021 05:47:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255902; 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=FDtmaDB4kqS4wOcww/6mdOWXrjUXzfE502+dL3peCCs=; b=Cea+fuGCc+pXw6u+5p1PMr/XMhYLmUEznsomEIdA5Ulr5Ld90fvAula9l6ENFls2ifzD2t dpuSvwUQ1D30NMz1PjSzMPYu9NdVAFkmT2yA2qqKU6Ozosqx4KjGtzOS3xqXSE1LoCJwT1 n+zQgiL8XfsNK095olNnPKOTsy7xSHk= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-125-67kW1tjlMLKJNfKbwt7Anw-1; Wed, 14 Jul 2021 05:45:01 -0400 X-MC-Unique: 67kW1tjlMLKJNfKbwt7Anw-1 Received: by mail-wr1-f72.google.com with SMTP id 32-20020adf82a30000b029013b21c75294so1203954wrc.14 for ; Wed, 14 Jul 2021 02:45:01 -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=FDtmaDB4kqS4wOcww/6mdOWXrjUXzfE502+dL3peCCs=; b=C9qQYH06rRWpj4EG+NdhMhE/1EEVKOKCZKsIY0EuMP7pKCZ7IXFM2pjWQdtUp5zw1a Tpeq3gmXhIUV6BJeii5akKmpgGEWBPWPVXj9BQfd9OY7QhvDaY2r3Nq2R7KyT+tZ0GVl +pv3m0m+5EExR1dJmVRHiMPI2v7O6JinK8LvTPkaKthVF+e/OHQp4sVpUnI5GhgunNfP 0nr4gde1NHbwNYYIcYML5fyNpBc/3oiCYY+whVol/dH/PvtYLIH8wDsudGFXfOYD99k/ 3psoqzQmRftwQfWXCw5CwbW1H+X4URCaDIXRs0kYqHnA65tUhtwJHBkxjlr5jYCUpfbS Ekcg== X-Gm-Message-State: AOAM532FzxsuBJbC3FRQkMYeGEwO9ofVrplCauhi6BB+g+hXErZlL7bA iEG1Y3IDvD5P/h2fpnm9wHxM2mn5WKLSqcyCKOscgQeWCRxpjYloDe9Pn4noZPO9t2BS//1jBTS m89cjYVO6rg2uhsXs X-Received: by 2002:a5d:4c87:: with SMTP id z7mr11733054wrs.405.1626255900310; Wed, 14 Jul 2021 02:45:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsX2PRzSqZC/fziGlaaGD8QqV4nNw/LBU9k/sV45C+e6beEAn1vHoY0xIAGZK32lnnmXF69w== X-Received: by 2002:a5d:4c87:: with SMTP id z7mr11733030wrs.405.1626255900114; Wed, 14 Jul 2021 02:45:00 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id c12sm2108345wrr.90.2021.07.14.02.44.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:44: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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 7/8] libbpf: Allow specification of "kprobe/function+offset" Date: Wed, 14 Jul 2021 11:43:59 +0200 Message-Id: <20210714094400.396467-8-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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. [jolsa: changed original code to use bpf_program__attach_kprobe_opts and use dynamic allocation in sscanf] Signed-off-by: Alan Maguire Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d93a6f9408d1..abe6d4842bb0 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10348,6 +10348,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, struct bpf_program_attach_kprobe_opts { bool retprobe; + unsigned long offset; }; static struct bpf_link* @@ -10360,7 +10361,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog, int pfd, err; pfd = perf_event_open_probe(false /* uprobe */, opts->retprobe, func_name, - 0 /* offset */, -1 /* pid */); + opts->offset, -1 /* pid */); if (pfd < 0) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name, @@ -10394,12 +10395,31 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, struct bpf_program *prog) { struct bpf_program_attach_kprobe_opts opts; + unsigned long offset = 0; + struct bpf_link *link; const char *func_name; + char *func; + int n, err; func_name = prog->sec_name + sec->len; opts.retprobe = strcmp(sec->sec, "kretprobe/") == 0; - return bpf_program__attach_kprobe_opts(prog, func_name, &opts); + n = sscanf(func_name, "%m[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 (opts.retprobe && offset != 0) { + err = -EINVAL; + pr_warn("kretprobes do not support offset specification\n"); + return libbpf_err_ptr(err); + } + + opts.offset = offset; + link = bpf_program__attach_kprobe_opts(prog, func, &opts); + free(func); + return link; } struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, From patchwork Wed Jul 14 09:44:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 477893 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=-19.4 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 9EF29C11F66 for ; Wed, 14 Jul 2021 09:45:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81E7B613C3 for ; Wed, 14 Jul 2021 09:45:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238970AbhGNJsD (ORCPT ); Wed, 14 Jul 2021 05:48:03 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:42765 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238894AbhGNJsC (ORCPT ); Wed, 14 Jul 2021 05:48:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255910; 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=ZN3a1tvndTSdC7YsooBDBFTgb7MR0oz3akD0iS1avfE=; b=UHjzfvzWl9ExW2XTfoR19g13E/VIPrL74CEMuD2dzXUzOXt7++XBxF0Ih2du1U5AWaaeA/ NEbf79NzWklqzGNtjcKLDcCYoLiV0IX3bmH+WIdFupObhD1fGUxL1aVIkqbDPW5BI+Si80 cgkd0Lk4TjnqZoZ+oTsCwPmLevc7RbY= 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-56-bVejZXFKOOK69lmW0tZzpQ-1; Wed, 14 Jul 2021 05:45:09 -0400 X-MC-Unique: bVejZXFKOOK69lmW0tZzpQ-1 Received: by mail-wr1-f69.google.com with SMTP id u13-20020a5d6dad0000b029012e76845945so1205942wrs.11 for ; Wed, 14 Jul 2021 02:45:09 -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=ZN3a1tvndTSdC7YsooBDBFTgb7MR0oz3akD0iS1avfE=; b=jKTusLNWEx8yQWX81TrGpl6TUdk/LeVjVyaSuplgUif1FUpaLRnajZXkAU+u3B4mGE F6KBxgfwLtTiBoxMsrHLLU9qjRNwVNHG7iTHOKwHK5YUczUFwdDym9otkM2P5WCuszJV /K/DGDz69gk/xheGSI7W2CGq/tY4OqxUlLT9VBpjqer8muohS37yOvk28EI0D9cFMdN/ 6iJ2QWL+akJH7+o1T2FU4fJYfPzB5MYB5uIjXjSUAp/1mUeP0fLFc13G37LnpJ93+5Qf vnFB7qQMSbx0y7qUxxRP6KjBtUDZ0PYBaPechbNQAx0r9bqTknnIASyZYFmb0l3uDa8i hcEg== X-Gm-Message-State: AOAM533SGsdr/RjhGGVDYLeZAy5KSBzuOMwHr1BrRwDrBb0XPeSNQLYp k2voAVE4WuGNU0Ez/BjOZEzu5hsHdwlCy2CnxnOSus1GU8/b9aIBso0MlWsRo/jCCblz3EpFy57 AOi3EVGzRf8bbuEAj X-Received: by 2002:a5d:64aa:: with SMTP id m10mr11766430wrp.351.1626255908343; Wed, 14 Jul 2021 02:45:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLVyxjqH+zKP2iLuw7jWabuqEbAAePGdeqkySMndl6Fvplu9NljgNilCAW5cz7H5MlpEdX0w== X-Received: by 2002:a5d:64aa:: with SMTP id m10mr11766409wrp.351.1626255908218; Wed, 14 Jul 2021 02:45:08 -0700 (PDT) Received: from krava.redhat.com ([5.171.203.6]) by smtp.gmail.com with ESMTPSA id o18sm1953328wrx.21.2021.07.14.02.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jul 2021 02:45: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 , Masami Hiramatsu , Alan Maguire Subject: [PATCHv4 bpf-next 8/8] selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe Date: Wed, 14 Jul 2021 11:44:00 +0200 Message-Id: <20210714094400.396467-9-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210714094400.396467-1-jolsa@kernel.org> References: <20210714094400.396467-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding test for bpf_get_func_ip in kprobe+ofset probe. Because of the offset value it's arch specific, enabling the new test only for x86_64 architecture. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/get_func_ip_test.c | 18 ++++++++++++++++-- .../selftests/bpf/progs/get_func_ip_test.c | 11 +++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) 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 index 8bb18a8d31a0..088b3653610d 100644 --- a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c @@ -8,10 +8,21 @@ void test_get_func_ip_test(void) __u32 duration = 0, retval; int err, prog_fd; - skel = get_func_ip_test__open_and_load(); - if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open_and_load")) + skel = get_func_ip_test__open(); + if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open")) return; + /* test6 is x86_64 specifc because of the instruction + * offset, disabling it for all other archs + */ +#ifndef __x86_64__ + bpf_program__set_autoload(skel->progs.test6, false); +#endif + + err = get_func_ip_test__load(skel); + if (!ASSERT_OK(err, "get_func_ip_test__load")) + goto cleanup; + err = get_func_ip_test__attach(skel); if (!ASSERT_OK(err, "get_func_ip_test__attach")) goto cleanup; @@ -33,6 +44,9 @@ void test_get_func_ip_test(void) ASSERT_EQ(skel->bss->test3_result, 1, "test3_result"); ASSERT_EQ(skel->bss->test4_result, 1, "test4_result"); ASSERT_EQ(skel->bss->test5_result, 1, "test5_result"); +#ifdef __x86_64__ + ASSERT_EQ(skel->bss->test6_result, 1, "test6_result"); +#endif 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 index ba3e107b52dd..acd587b6e859 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,13 @@ int BPF_PROG(test5, int a, int *b, int ret) test5_result = (const void *) addr == &bpf_modify_return_test; return ret; } + +__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; +}