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: 12376455 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=-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 A790EC07E9A for ; Wed, 14 Jul 2021 09:44:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8CD67613A9 for ; Wed, 14 Jul 2021 09:44:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238876AbhGNJrF (ORCPT ); Wed, 14 Jul 2021 05:47:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:47006 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238927AbhGNJrF (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-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-111-w4i8xMJMNUu0CqN57NwOHQ-1; Wed, 14 Jul 2021 05:44:12 -0400 X-MC-Unique: w4i8xMJMNUu0CqN57NwOHQ-1 Received: by mail-wr1-f70.google.com with SMTP id r11-20020a5d52cb0000b02901309f5e7298so1248124wrv.0 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=XAYiOPcv7ZSEkUyz5HFIoCg+kq5v46k5Sbt289wzwciHV7SDt0eAwp7of8P60EDG26 mjqKX0xThADr5AxxgPFIaN1LxLQfQcvgngcZ2fq1g20udiB6p/Gg5YvBE14PR49u+QvZ gnVe8b3kk+8gRXMI2uoM9Sub7U+aYsOYQnxiGx5fPW9yWBkOUKmad1CHAMPxG6+eN0LY wXs/VndjlSa/XoMpgQ/a3AEBtJx16MEzcGRiqapKsPer0ca2EGlpHhNd8EjMlAPhwn74 w9i4vg/7fEQLUsCjYbYJSYkPifjnXBgHnbkLU/7Uq6iw//NchN3pz/540BqDGc3VaN4W xZ8g== X-Gm-Message-State: AOAM531yOQJP9jXwUdaF8jWc7tDxg17o9zQkmm32OCuE9l+Z1VjxqAyy RmeJJ20c3qH+qQp5lFpOAe9Y4r9v4dj2T83FmiyyiJ8WVELVltJmIKqX4DSYlq4H0OxgNs40vj8 3Yc9htse/6t7a X-Received: by 2002:a05:6000:551:: with SMTP id b17mr11631525wrf.32.1626255851364; 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: 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 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: 12376457 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=-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 D9E13C07E9A for ; Wed, 14 Jul 2021 09:44:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE50861026 for ; Wed, 14 Jul 2021 09:44:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238933AbhGNJrN (ORCPT ); Wed, 14 Jul 2021 05:47:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:56528 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238923AbhGNJrM (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-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-170-zmWo5Lp3N7-XS9qDOK273w-1; Wed, 14 Jul 2021 05:44:20 -0400 X-MC-Unique: zmWo5Lp3N7-XS9qDOK273w-1 Received: by mail-wr1-f71.google.com with SMTP id z6-20020a5d4c860000b029013a10564614so1210328wrs.15 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=rhJ+ERyB+6cFMdakZj2wmgITSZJEIlljq+Fbnymahy99yWb7QWAjQb06/lr1KHSvxO AdRZjn5xHUjVa/gkrjPmwMfUIvqlBDkIcbQAS2F/B0mN+iDFOb4f2DZ92P52lEEBPyvu 1e9YMwtTemiW3Vwh1tEq7hmZZBfq9TxZ9aBYMmG8ILNNE4Yw38c7vChGBPY4KZdea4C1 eC6tf2/geZO3eOmtAvdf4amYfhyxJzNXS2nV+u2bLuk2Lc0IHpFIPp1q94/Gagxrw38T XYrGUMT80/5O077pNnAMoosTxzyqGNEbexfiGkTbaoV5efSYJcdWhPlvrdcnqyPpURc8 JxJw== X-Gm-Message-State: AOAM532q/mXdJXohcT+VvyA6eBOo893vpPmNLZdCsU6WC9KR+sjPLxZR TsV9EiM3JSB7Amc/CXzvQ6DZ+mhBROxB0abznssY/iI7y2juaN2S9ZbhBZz9gjbOSQXpuWKXPeq /i/f+Urc0Fl+f X-Received: by 2002:a1c:f206:: with SMTP id s6mr9850233wmc.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: 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 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: 12376459 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=-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=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 557AFC07E9A 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 39C6C613A9 for ; Wed, 14 Jul 2021 09:44:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238959AbhGNJrW (ORCPT ); Wed, 14 Jul 2021 05:47:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:37042 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238916AbhGNJrW (ORCPT ); Wed, 14 Jul 2021 05:47:22 -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-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-185-kT7IJDWTOa2l-twkMcqtNA-1; Wed, 14 Jul 2021 05:44:29 -0400 X-MC-Unique: kT7IJDWTOa2l-twkMcqtNA-1 Received: by mail-wm1-f72.google.com with SMTP id p6-20020a05600c3586b029020b46124c2eso1773613wmq.1 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=nZJnA/4Idvzq7yQI8yrYNW1XXqgpUedi+Yug8ijuHUdNZuXdmdnKJnoSx/E0rZnVqP IQpVdFgkXakUTbmikg/e0ZHmHJnAADHbHDXYhwTbCysBwfbZ7T0PkqGJU8a4S23F9jhv 9TtXESZ8dGfObXKgb/hRalVfgYLGo3NKAWHBd0fvgnObG3tlx/uRhtKz6DPe4XdxMqqx BJXMoQEM7rrtxbdqbQrQxiJNQRJS13Znb7vYR0whLWh1fdXoOYQeBo8S5kt6q1ql5ojE QBjlrcvbzm4Q1y9Pcn4lRpQmfsG2YHpKxVVJfz9HN38neLeUzWNDHu6uUD2roaGJ5o0Z +pnw== X-Gm-Message-State: AOAM531m5SvQz/3jCol2sZKYIuR5uZepCIMOTBc6V7+Xwa6ylw2+dLI7 ani0qUSBHdT3kGbJnKTB+cbacM5HBj+EOpD/AtX+s4xmHX931m12ExpVIMIXWANRDxMySUHhLFm /l71JEGeCkS02 X-Received: by 2002:adf:8061:: with SMTP id 88mr11646707wrk.233.1626255868077; 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: 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 | 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: 12376461 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=-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=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 61668C11F66 for ; Wed, 14 Jul 2021 09:44:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E8C0613B7 for ; Wed, 14 Jul 2021 09:44:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238966AbhGNJrf (ORCPT ); Wed, 14 Jul 2021 05:47:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32087 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238189AbhGNJrc (ORCPT ); Wed, 14 Jul 2021 05:47:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626255880; 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=Cx70hRaH0mv95wXQLj7Il+5zHfT7lVCBqXyCRT0IhQI9Q09azg2UOuvq5m9yhL0Xy2FWZc uzzwJcsut2f/eFQAXGjBOC1vTWl3yAK0kXlkWTPiAHUot8D1TWQXc7I68uja3oSSlQwBeK 80vQBw2QpwnEBB88GKhlU+z0s6zndPU= 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-57-aZtmLjkbNv65TB8X2_JnlQ-1; Wed, 14 Jul 2021 05:44:37 -0400 X-MC-Unique: aZtmLjkbNv65TB8X2_JnlQ-1 Received: by mail-wr1-f69.google.com with SMTP id 32-20020adf82a30000b029013b21c75294so1203287wrc.14 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=KztxMKxCLIagvWC717iCxHhp+uqoR15D68kQ1y/NX8IiHPy+gPKhrqHFZ8cig6xdwZ t84YbsIihEGSdBNDoPS0aFGqdM7Os34aP6DzLZA0b4AE0n66estQSzQDJO9qa+/DIoNO 2a0hNBbGH8dsv+qCGO1RPVbrgc67zy1WYFkhohe4LHhax1waUbGGKywW9NAFQueMTFX3 +TiSfopcBM49uL1x5plmyjAF4sSvHb9eVSytqf0JegsRnoMLdU81lnFA6aKAIGdiHvuV AwJLCdFK26Pe6Q5zQDnTh7Y6/NQaoKBZhpgv9kQEU/1CZ6VDeHiQxkYPzDtreFKkKMhH SBqw== X-Gm-Message-State: AOAM531KjXFmHT/lDMCQeHIf2qd5PO/OqwZsP2s8TqNjAGp7kFTYb1sK cP1D8PrmyQGvx8+iAGIsBCpseU+rc2devuKG1k6MWmuK5/EZa/qH1oenqjpxVVKyxPo/CQoy6HE Zm9QEY28/Mw82 X-Received: by 2002:a7b:ca43:: with SMTP id m3mr10071497wml.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: 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. [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: 12376463 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=-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 7D8EDC11F66 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 66C42613B7 for ; Wed, 14 Jul 2021 09:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238962AbhGNJri (ORCPT ); Wed, 14 Jul 2021 05:47:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35799 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238958AbhGNJri (ORCPT ); Wed, 14 Jul 2021 05:47:38 -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-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-382-4n95ckyIN0afyZY_5TlsYg-1; Wed, 14 Jul 2021 05:44:45 -0400 X-MC-Unique: 4n95ckyIN0afyZY_5TlsYg-1 Received: by mail-wr1-f70.google.com with SMTP id g9-20020adff3c90000b0290140a25efc6dso1232637wrp.5 for ; Wed, 14 Jul 2021 02:44:45 -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=GV65t6D/OEViY6kdioyC5bgN26qR/zBZJOa0/6DVZDSTd2v3MEvcxDAooQ9zW7ixyD ZzjqeAPzov5AGugzgrt6LK7P9CtRwiZsj1+HqxWPxnufXGZFq44WSG6PCAa5p3tAYvYy blVTnBr5Zs5FcoPeum5rDy5ZAOj98hb4/jZL7i0fSBFLDjfbAFDkeyn0HLsTkrPJm+Fv th1KDxEZiEVVge7Y33nk+kZwVi2JuLZS/ebOvvZfAcydVFb4wVduDPRbvr4s2YOtytvv UgpDz8DOFDZlxl5FbkK0t9VoxE8KzIQrevAVsT0p69LIZTiUokyqJfYLSSRtGzbGkB8v dHjA== X-Gm-Message-State: AOAM530jW6BgfElrV977dHyAsQqCfXUpXsvamw+iK0PIt1/R3gDs5vrw CgQjj6Ak0IE3Rpc7RAEtDRIGgcILUgPkdmc8uvOeoPMrKWncJTgheveXA+NdiOgSktbATZymZpR gE1AWJjks5PN7 X-Received: by 2002:a5d:48ce:: with SMTP id p14mr11988508wrs.170.1626255884072; 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: 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 | 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: 12376465 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=-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 0D2B0C07E9A 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 EC3F0613C1 for ; Wed, 14 Jul 2021 09:44:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238965AbhGNJrq (ORCPT ); Wed, 14 Jul 2021 05:47:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:55917 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238964AbhGNJrp (ORCPT ); Wed, 14 Jul 2021 05:47:45 -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-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-86-fPEVnAf7PKae79kSdG22YA-1; Wed, 14 Jul 2021 05:44:53 -0400 X-MC-Unique: fPEVnAf7PKae79kSdG22YA-1 Received: by mail-wm1-f69.google.com with SMTP id b26-20020a7bc25a0000b0290218757e2783so567322wmj.7 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=fcTGjLZNFjOJmql4N6XeVjK7xbHV7qhCUt5jilclfZJaxpJWYHp9g9PLUH2ApOXHL5 SrA8RYyyBUYKUS8VSp6A+Iv5B2cP82LnJUicucBshb+6pYWwMRwi75CmQopSZyNrAaEE gD4iAaKtBxGDhqeMI8SLUlq7jpHZUrwdAwxdAYjVe4Ic7PhsisvtNA1PhIfzeJQRCH8Z fIvsR9Gl080yS9Qvo+ZBUoaircWwZ4JdMMT5F5JZ7d5LR5nSIJAGEO10/HPKsI5wgYUp 8e14nOWf9PDhiB3KTIdPHj8aOCG3GG+ovvQEmuwrGUGjYHiQXk4k4WWGra6FO/KA/hqF 0vGA== X-Gm-Message-State: AOAM5319s/imxWugCH0b5nxpRBDOk+/OGzWeScFkZV7/0FqURX24YQ5r 0rn9H3WlsyBkKA65QiyFswL/w/TzRMUTa/5lDT2FffodEVifTfL2gqAeqa7HVJDXiU2ae5BFAcm OBFx8OPG714b9 X-Received: by 2002:a5d:58d6:: with SMTP id o22mr12007032wrf.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: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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: 12376467 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=-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 0A03AC07E9A 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 E1C53613C3 for ; Wed, 14 Jul 2021 09:45:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238906AbhGNJry (ORCPT ); Wed, 14 Jul 2021 05:47:54 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:44401 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238869AbhGNJry (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-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-33-aDm-k0nXMvmKjOAqNg0Iwg-1; Wed, 14 Jul 2021 05:45:01 -0400 X-MC-Unique: aDm-k0nXMvmKjOAqNg0Iwg-1 Received: by mail-wr1-f70.google.com with SMTP id m9-20020a0560000089b02901362e1cd6a3so1190912wrx.13 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=KhSyc9waFImqOwCDd4olgpQSWzaUnwlV5Nm9XU2I4CwAEB7GInjjjPSXiJxHiwyyib +gnxA/o0jFaSa/roMqFbVO5z5Am6FJA5SwMc1ToB8er7fGeBNcX5SYpMQkf7vg8yWCT2 W0U/63kcRATc9opF53NzpzuA1ZtdljtfPYPCkbL/XWD/wMzo8tM0SvSDGtUuVly9W/9u y0Nw+Ug9VNSBbHwy7GvxlCqCSoyrv9pLVfRYyCBc5GrMpZj8EQej2Kbpvs8p7rcchyIj L71uSs9Mqi1oKczw6LPP//rcu3hFsD7ggQkXfJbsXbIPRxuK94F6dIF2KiQcnBEcYFeE +a9A== X-Gm-Message-State: AOAM532NIMKUuldIKDIT5LjnzdrZz61uitffuy6kJtyp3dkOxtqDFzN6 nl8RcVH8M4dqtuLu3/r1Xs91TJgEe1YiRbJPupgvUYWln3+ycGiStGtdCSWPGEeRDJjwGM2eSmz THh1KMtzIidxH X-Received: by 2002:a5d:4c87:: with SMTP id z7mr11733052wrs.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: 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. [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: 12376469 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=-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 B652AC07E9A for ; Wed, 14 Jul 2021 09:45:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A3111613C1 for ; Wed, 14 Jul 2021 09:45:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238904AbhGNJsC (ORCPT ); Wed, 14 Jul 2021 05:48:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35403 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238917AbhGNJsC (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-384-wcBXSp0_NGOoN06sPUlHAQ-1; Wed, 14 Jul 2021 05:45:09 -0400 X-MC-Unique: wcBXSp0_NGOoN06sPUlHAQ-1 Received: by mail-wr1-f69.google.com with SMTP id y15-20020a5d614f0000b029013cd60e9baaso1218080wrt.7 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=X/GP4iDpm2Hx21YY9cBfzI3FENIJz6zlGi6/cR3jpzpcbfXKmcaUrigZHpRfKNJOG2 2bAiuATwBO7g4augmefGfw841mBQjjxXowkBiWUwl6ULHCPdQWK5MJR4F2S/yV2LTr5t UDKeKhfNOTkUt7cQl6h7Rg/ItsoCMdvHFx98NQykwv2QVBbA4pqwMXVo1yVb4EIL+0ZV B9Z0H3oW6YHdhGtOo/T8CnuK/+FYOWNV+XUG0ZkET36iGS75ZKkT6ZtVBOmwUxR/lSX2 yskqmC08iL/MSEaNgI013yS5Dt3PCtvoAfvRHtE57hFs3cdV7zk9L/rI5ruz8C4O4e5B /GKQ== X-Gm-Message-State: AOAM530FBPMoOzGeHtWevDUhjuiWmzjixS9WhktDezGlxzE2nfB1qaPQ Wt5qb4VepJlo5tmwqpxBTha5VBRdd0yJQVjbbai7gUtBELJPPiPW/t/wyrj4SHXhyfpcte2LSb0 aAuAZNEh1ivpd X-Received: by 2002:a5d:64aa:: with SMTP id m10mr11766433wrp.351.1626255908344; 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: 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, 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; +}