From patchwork Tue Jun 29 19:29:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12350679 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.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B6CAC11F67 for ; Tue, 29 Jun 2021 19:29:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 114D561DE6 for ; Tue, 29 Jun 2021 19:29:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234186AbhF2TcW (ORCPT ); Tue, 29 Jun 2021 15:32:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:24460 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234171AbhF2TcV (ORCPT ); Tue, 29 Jun 2021 15:32:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624994993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QgBr4LV9SR6/Qelufj3zindVEajUhYS2+l3zjx7rHSU=; b=jN8Mb9waVl6aEdlN2rQLLeHjomuDBp9x8ciwcANSFVFpozF/uqAb6xfNq7F9MUZn59D6eb y2ZRJUlXJt68i2qwTAH33bGCiI4jv1iapnKYGhosqK8MSAfED3yh1SvW3YbolvTYhqzFm2 tcLB+SQ9KTqQ47ZBo4uMBCjR2WlmvAk= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-97-Vxx7jdzFOOOXDqgx8g9TZA-1; Tue, 29 Jun 2021 15:29:51 -0400 X-MC-Unique: Vxx7jdzFOOOXDqgx8g9TZA-1 Received: by mail-ed1-f71.google.com with SMTP id j15-20020a05640211cfb0290394f9de5750so11368172edw.16 for ; Tue, 29 Jun 2021 12:29:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QgBr4LV9SR6/Qelufj3zindVEajUhYS2+l3zjx7rHSU=; b=pKy0F901A8IgaurZYhAdYW699Nhp/DzR9FbNY4F6PU54hUXOg9MJFJQ096LWGrZ8xr nv4AP+IvWJaGuyHZZ1EMgejSOSBc7x+fXaDpNxZCEzhSv0XUTfu8o7jzX3XOcypyfY7w vWp5EjnPMOjYCdNV6ttGg3BXhjwHcoKCqJkNqJ+gtmW21Yvg9+j9Hw/n4wIpa/rKXvpD DmpQtgje6v5B5etWDRFFRRDVOZUdx+0asAjCrbvj31w9l/44hnhom9o+1rlaT36pIhbH M2sDuduciEpP3H8DdDNY++xeztXQkrDwvI57N47fho3xO4s2bbR+7QO/Vq9mvoBxjlk7 qcUA== X-Gm-Message-State: AOAM530t19hitjViRFUxrOy4HrhX+987L+me0X+3Q8R6tbg5iocD0UK9 Wxsogc96JapudIZA4GxkASTGVOZBHc/9SMTZq0iqkO1hP1WWW9nyKIPI6gz6Sh/lxf9lbFJuLhb odKkZPZHXy/Sb X-Received: by 2002:a05:6402:1c11:: with SMTP id ck17mr29205326edb.370.1624994990334; Tue, 29 Jun 2021 12:29:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXHlbTwtU81vylTCzW77E+j+DPUwj3/0W67M0b+va71aRhfz4ja1tt1bjornX+DBMxs/e2RA== X-Received: by 2002:a05:6402:1c11:: with SMTP id ck17mr29205314edb.370.1624994990153; Tue, 29 Jun 2021 12:29:50 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id n22sm472559eje.3.2021.06.29.12.29.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 12:29:49 -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 Subject: [PATCH bpf-next 1/5] bpf, x86: Store caller's ip in trampoline stack Date: Tue, 29 Jun 2021 21:29:41 +0200 Message-Id: <20210629192945.1071862-2-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210629192945.1071862-1-jolsa@kernel.org> References: <20210629192945.1071862-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Storing caller's ip in trampoline's stack. Trampoline programs can reach the IP in (ctx - 8) address, so there's no change in program's arguments interface. The IP address is takes from [fp + 8], which is return address from the initial 'call fentry' call to trampoline. This IP address will be returned via bpf_get_func_ip helper helper, which is added in following patches. Signed-off-by: Jiri Olsa --- arch/x86/net/bpf_jit_comp.c | 19 +++++++++++++++++++ include/linux/bpf.h | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index e835164189f1..c320b3ce7b58 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1951,6 +1951,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i if (flags & BPF_TRAMP_F_CALL_ORIG) stack_size += 8; /* room for return value of orig_call */ + if (flags & BPF_TRAMP_F_IP_ARG) + stack_size += 8; /* room for IP address argument */ + if (flags & BPF_TRAMP_F_SKIP_FRAME) /* skip patched call instruction and point orig_call to actual * body of the kernel function. @@ -1964,6 +1967,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i EMIT4(0x48, 0x83, 0xEC, stack_size); /* sub rsp, stack_size */ EMIT1(0x53); /* push rbx */ + if (flags & BPF_TRAMP_F_IP_ARG) { + /* Store IP address of the traced function: + * mov rax, QWORD PTR [rbp + 8] + * sub rax, X86_PATCH_SIZE + * mov QWORD PTR [rbp - stack_size], rax + */ + emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, 8); + EMIT4(0x48, 0x83, 0xe8, X86_PATCH_SIZE); + emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -stack_size); + + /* Continue with stack_size for regs storage, stack will + * be correctly restored with 'leave' instruction. + */ + stack_size -= 8; + } + save_regs(m, &prog, nr_args, stack_size); if (flags & BPF_TRAMP_F_CALL_ORIG) { diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f309fc1509f2..6b3da9bc3d16 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -554,6 +554,11 @@ struct btf_func_model { */ #define BPF_TRAMP_F_SKIP_FRAME BIT(2) +/* Store IP address of the caller on the trampoline stack, + * so it's available for trampoline's programs. + */ +#define BPF_TRAMP_F_IP_ARG BIT(3) + /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 */ From patchwork Tue Jun 29 19:29:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12350683 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.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED905C11F67 for ; Tue, 29 Jun 2021 19:29:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D57F561DDE for ; Tue, 29 Jun 2021 19:29:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234749AbhF2TcZ (ORCPT ); Tue, 29 Jun 2021 15:32:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:58180 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234594AbhF2TcY (ORCPT ); Tue, 29 Jun 2021 15:32:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624994996; 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=cCLo6L8xdEkdjjqJvWpg9bl8KYp0p1bhWd5aESB0bv5/sQqDzU+t1eiFCOM3WOYoX4cdwn CRaMWocCytNgYKn3jtt10kn1pxYoKc6FUkvi4hInMD0RRnwaEFydSjD9ipdUFgvlnxT6gS 7XmktfwI5eNiJZi4vI3jTosy7ul/no0= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-349-DKMX2oaSOpeDzM9FItwh8w-1; Tue, 29 Jun 2021 15:29:54 -0400 X-MC-Unique: DKMX2oaSOpeDzM9FItwh8w-1 Received: by mail-ed1-f72.google.com with SMTP id w1-20020aa7da410000b029039533674a84so7305699eds.9 for ; Tue, 29 Jun 2021 12:29:54 -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=Kanvb6e4emQA1Bipk/bN4H/oxWAjjif6oI1cdYzUsiuBUxw7lq/XXSxsD+aJ1/mEVR rJYXTWCjwmf9m0eolM5jCcMXTqgpfD4KwQIpTs//aPAfh3k7DD92trCLHvqYhmNfD4mX c/foqO1c+hjGWIGDhFwf7OuzouW0XDl3/vrHZ3iJaKjMQ+s9hCtum/HbI3+Fmz/uYSRr bhulnz4OlbWcYGCEiURyHn8cDz+dqLZK7ZB5iPvPlEAzXqvSpWDGg4wSwFn6DFgiMOck B3oh7Bhh1pdWbAE6k1IDN+neO1XMotLoXsEUTFfxaqP4oZurJ0frJoJaexugmOdHuADM r4hA== X-Gm-Message-State: AOAM532pkqBeiJcLU7j0QSbHRZMfGMK2ttsTAF8ZKHJCLw4GG8Ws9hEg EpJF8JUI+obnhrMGNZaKh0bAO3OLwxOj8Rd9AEmK9bALrRB32MoQ8OEpSCtH3KhgleihXug5ZdK WMn8Uf8BPikwl X-Received: by 2002:aa7:d8d4:: with SMTP id k20mr41688296eds.143.1624994993070; Tue, 29 Jun 2021 12:29:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5OvjTkKZzLuY/9wnAZ95R6xQ/Jmsq4KCZeL0+5Rkjq44ZUqUpAvXSe1v+Q79LdCNb87I4/g== X-Received: by 2002:aa7:d8d4:: with SMTP id k20mr41688287eds.143.1624994992931; Tue, 29 Jun 2021 12:29:52 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id n22sm472559eje.3.2021.06.29.12.29.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 12:29:52 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Masami Hiramatsu Subject: [PATCH bpf-next 2/5] bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip Date: Tue, 29 Jun 2021 21:29:42 +0200 Message-Id: <20210629192945.1071862-3-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210629192945.1071862-1-jolsa@kernel.org> References: <20210629192945.1071862-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 Tue Jun 29 19:29:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12350685 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.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECADEC11F69 for ; Tue, 29 Jun 2021 19:29:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D702E61DE9 for ; Tue, 29 Jun 2021 19:29:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234582AbhF2Tc0 (ORCPT ); Tue, 29 Jun 2021 15:32:26 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:20894 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234180AbhF2TcZ (ORCPT ); Tue, 29 Jun 2021 15:32:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624994997; 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=7POGsUdxxXzK+4ImUmkSrNYlLw4tFmtrg6J6zcZB3co=; b=DWLp1l4kgZBpgu0BSB/NimwUp/RtXGiu7gLXLofy0OYIJKOo6On+RG3EJJhrUF/ia2PnOm ZoAk7gwo6cPcXGfdG7gDl+6ACQ4N6T1pTdU4owT3wQfQtQle0NrBUWT2FZGZrBJDSBjMNa yEF/BSwOnjF/St1knsq6lsmTkzMulyY= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-339-_68f7rR0OqSI7AGR1uzXkA-1; Tue, 29 Jun 2021 15:29:56 -0400 X-MC-Unique: _68f7rR0OqSI7AGR1uzXkA-1 Received: by mail-ed1-f72.google.com with SMTP id n13-20020a05640206cdb029039589a2a771so479692edy.5 for ; Tue, 29 Jun 2021 12:29:56 -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=7POGsUdxxXzK+4ImUmkSrNYlLw4tFmtrg6J6zcZB3co=; b=VlpRNY2AacTEt0W6wYA8JW1bAaVqDWE0w8kv3O8U8cIJ7l96FCCKMqcxslKsaQulGX GuC6MxLW73Ba4X+5/8iLRsZHbn0+VNeBEcheTuvl/xI9ewvihIR4ZfsBWZc5l5SjhKcD 202W2QMPi/peOgI6SjPRzL+/Q0S/EUlMdjSgDyejHbsoynZFsCf+kSLXyJCOSqvCe8Qn C5PfQ+IpppA5nYC4cO76Kjfxs6n+ET7I3Vl2FutYmbeiRBxRcdac2l2Q1aT5yzuHWUwi JrEbeG1OvQiIjBliTF4uPsmtWW9HVQrxInptCJX90cliQZBkPy8SHAIHRi/fWAIHAc/f DKkQ== X-Gm-Message-State: AOAM530/eyamTbmxRla51zfVRV0A8vYXphDANodfByIpNzVa3+90FEUr VlC+11D4KwcrBecLYPQDdesDq6mBSkhZk3YGvslMcdaBBVk2BONtvGqyJIPyUfBuqGHwtyyFT7l haOF6deWDUKem X-Received: by 2002:a17:906:3d3:: with SMTP id c19mr30851640eja.202.1624994995074; Tue, 29 Jun 2021 12:29:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw23NIpSXqFpaqDGHPNVNZiDYiZzRVkvF/Ui1ls7XbzLlUZgCjf3IWzZlcShrGYuCb8MruN3Q== X-Received: by 2002:a17:906:3d3:: with SMTP id c19mr30851628eja.202.1624994994878; Tue, 29 Jun 2021 12:29:54 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id n22sm472559eje.3.2021.06.29.12.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 12:29:54 -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 Subject: [PATCH bpf-next 3/5] bpf: Add bpf_get_func_ip helper for tracing programs Date: Tue, 29 Jun 2021 21:29:43 +0200 Message-Id: <20210629192945.1071862-4-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210629192945.1071862-1-jolsa@kernel.org> References: <20210629192945.1071862-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). Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 7 +++++ kernel/bpf/verifier.c | 53 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 15 ++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++ 4 files changed, 82 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index bf9252c7381e..83e87ffdbb6e 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4780,6 +4780,12 @@ union bpf_attr { * Execute close syscall for given FD. * Return * A syscall result. + * + * u64 bpf_get_func_ip(void *ctx) + * Description + * Get address of the traced function (for tracing programs). + * Return + * Address of the traced function. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4951,6 +4957,7 @@ union bpf_attr { FN(sys_bpf), \ FN(btf_find_by_name_kind), \ FN(sys_close), \ + FN(get_func_ip), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index e04e33893cff..701ff7384fa7 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5149,6 +5149,11 @@ static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); } +static bool allow_get_func_ip_tracing(struct bpf_verifier_env *env) +{ + return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); +} + static int check_map_func_compatibility(struct bpf_verifier_env *env, struct bpf_map *map, int func_id) { @@ -5955,6 +5960,32 @@ static int check_bpf_snprintf_call(struct bpf_verifier_env *env, return err; } +static bool has_get_func_ip(struct bpf_verifier_env *env) +{ + enum bpf_attach_type eatype = env->prog->expected_attach_type; + enum bpf_prog_type type = resolve_prog_type(env->prog); + int func_id = BPF_FUNC_get_func_ip; + + if (type == BPF_PROG_TYPE_TRACING) { + if (eatype != BPF_TRACE_FENTRY && eatype != BPF_TRACE_FEXIT && + eatype != BPF_MODIFY_RETURN) { + verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", + func_id_name(func_id), func_id); + return -ENOTSUPP; + } + if (!allow_get_func_ip_tracing(env)) { + verbose(env, "func %s#%d for tracing programs supported only for JITed x86_64\n", + func_id_name(func_id), func_id); + return -ENOTSUPP; + } + return 0; + } + + verbose(env, "func %s#%d not supported for program type %d\n", + func_id_name(func_id), func_id, type); + return -ENOTSUPP; +} + static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, int *insn_idx_p) { @@ -6225,6 +6256,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) env->prog->call_get_stack = true; + if (func_id == BPF_FUNC_get_func_ip) { + if (has_get_func_ip(env)) + return -ENOTSUPP; + env->prog->call_get_func_ip = true; + } + if (changes_data) clear_all_pkt_pointers(env); return 0; @@ -12367,6 +12404,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; @@ -12700,6 +12738,21 @@ static int do_misc_fixups(struct bpf_verifier_env *env) continue; } + /* Implement bpf_get_func_ip inline. */ + if (prog_type == BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_get_func_ip) { + /* Load IP address from ctx - 8 */ + insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); + if (!new_prog) + return -ENOMEM; + + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; + continue; + } + patch_call_imm: fn = env->ops->get_func_proto(insn->imm, env->prog); /* all functions that have prototype and verifier allowed diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 64bd2d84367f..9edd3b1a00ad 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -948,6 +948,19 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .arg5_type = ARG_ANYTHING, }; +BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) +{ + /* Stub, the helper call is inlined in the program. */ + return 0; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { + .func = bpf_get_func_ip_tracing, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, +}; + const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1058,6 +1071,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_for_each_map_elem_proto; case BPF_FUNC_snprintf: return &bpf_snprintf_proto; + case BPF_FUNC_get_func_ip: + return &bpf_get_func_ip_proto_tracing; default: return NULL; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index bf9252c7381e..83e87ffdbb6e 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4780,6 +4780,12 @@ union bpf_attr { * Execute close syscall for given FD. * Return * A syscall result. + * + * u64 bpf_get_func_ip(void *ctx) + * Description + * Get address of the traced function (for tracing programs). + * Return + * Address of the traced function. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4951,6 +4957,7 @@ union bpf_attr { FN(sys_bpf), \ FN(btf_find_by_name_kind), \ FN(sys_close), \ + FN(get_func_ip), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper From patchwork Tue Jun 29 19:29:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12350687 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.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D4E4C11F67 for ; Tue, 29 Jun 2021 19:30:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 46A8761DE7 for ; Tue, 29 Jun 2021 19:30:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234665AbhF2Tca (ORCPT ); Tue, 29 Jun 2021 15:32:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:22628 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234180AbhF2Tc0 (ORCPT ); Tue, 29 Jun 2021 15:32:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624994999; 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=OGsWiBOuAVqf4btUf4cftMKBn0bF9gV/Gnv0HJUJGi8=; b=YhRBXOlD6t/wuk7Wmw1HQoFY8LSiRS5bHo3KqG9yrijDsNYRdqICmMHkS+yf5i58tbMj3p BV0PEBWxyqc+F0H6gWad662ZgyHIEIuy6IOlK4pkOuliTWgAfAT/opXXuE91kKWy+HIoZA Ho3aIfMxCKZWGp1DHsu5OTjmPVOQH5w= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-544-RHnEGyE2PFiYYEluz2N_aw-1; Tue, 29 Jun 2021 15:29:57 -0400 X-MC-Unique: RHnEGyE2PFiYYEluz2N_aw-1 Received: by mail-ed1-f69.google.com with SMTP id w15-20020a05640234cfb02903951279f8f3so6829129edc.11 for ; Tue, 29 Jun 2021 12:29:57 -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=OGsWiBOuAVqf4btUf4cftMKBn0bF9gV/Gnv0HJUJGi8=; b=CZHhZ3KFO539381jSvGp4I7VungCixqvgSYNVef2PGGNGIRwyTioKUSB5Vogd669yL guyQxFizJ/1/A9nAGTYOzazCqEZgQ6o4ho/4qNGBgYXZ2pR0oPDASZvmVXLAKRqIM0X0 eYX9bOcDMcwyMaWrV2Q5SIqDOfqn7apK6dqfhT/NhsfYpNi21E5Wv2llWcC3lumMyqfq n9V6mOpzNjFitW/I7DY3kSQ70DfAsDB+E/5rcg/z4MvX9jjNHrVUF8z+0rKTBXL0rQo9 FV6xUwwf8alus6NI5/K+DpLG/Sr6fOffAKa2pHkyZd5xyKnaK9BbQ2gjR+8wKFiPFaUL b35Q== X-Gm-Message-State: AOAM530URXlcxX+THHTtOYbsoUJEShlOY1tjl1yb7Gpfkv/UauyjQHJM A+jKmHoz5FhmAWpzBIFtgDLl9o9nZqtp/Cm5xAmZbAN79bY0EwB7m/bPy28GZrdKxJKlfQIEWpc v/udJx5bq5wvh X-Received: by 2002:a17:906:4d99:: with SMTP id s25mr28229218eju.349.1624994996596; Tue, 29 Jun 2021 12:29:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWtTBoRXPMKCr1KyV2AoM6ZTq/GwiLioCcV0nux5msmh3rBiUzFuwNQgLyVEVrtLD951GCgg== X-Received: by 2002:a17:906:4d99:: with SMTP id s25mr28229209eju.349.1624994996411; Tue, 29 Jun 2021 12:29:56 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id n22sm472559eje.3.2021.06.29.12.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 12:29:56 -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 Subject: [PATCH bpf-next 4/5] bpf: Add bpf_get_func_ip helper for kprobe programs Date: Tue, 29 Jun 2021 21:29:44 +0200 Message-Id: <20210629192945.1071862-5-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210629192945.1071862-1-jolsa@kernel.org> References: <20210629192945.1071862-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding bpf_get_func_ip helper for BPF_PROG_TYPE_KPROBE programs, so it's now possible to call bpf_get_func_ip from both kprobe and kretprobe programs. Taking the caller's address from 'struct kprobe::addr', which is defined for both kprobe and kretprobe. Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 2 +- kernel/bpf/verifier.c | 2 ++ kernel/trace/bpf_trace.c | 14 ++++++++++++++ kernel/trace/trace_kprobe.c | 20 ++++++++++++++++++-- kernel/trace/trace_probe.h | 5 +++++ tools/include/uapi/linux/bpf.h | 2 +- 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 83e87ffdbb6e..4894f99a1993 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4783,7 +4783,7 @@ union bpf_attr { * * u64 bpf_get_func_ip(void *ctx) * Description - * Get address of the traced function (for tracing programs). + * Get address of the traced function (for tracing and kprobe programs). * Return * Address of the traced function. */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 701ff7384fa7..b66e0a7104f8 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5979,6 +5979,8 @@ static bool has_get_func_ip(struct bpf_verifier_env *env) return -ENOTSUPP; } return 0; + } else if (type == BPF_PROG_TYPE_KPROBE) { + return 0; } verbose(env, "func %s#%d not supported for program type %d\n", diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 9edd3b1a00ad..1a5bddce9abd 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -961,6 +961,18 @@ 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) +{ + return trace_current_kprobe_addr(); +} + +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 +1104,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/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index ea6178cb5e33..b07d5888db14 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1570,6 +1570,18 @@ static int kretprobe_event_define_fields(struct trace_event_call *event_call) } #ifdef CONFIG_PERF_EVENTS +/* Used by bpf get_func_ip helper */ +DEFINE_PER_CPU(u64, current_kprobe_addr) = 0; + +u64 trace_current_kprobe_addr(void) +{ + return *this_cpu_ptr(¤t_kprobe_addr); +} + +static void trace_current_kprobe_set(struct trace_kprobe *tk) +{ + __this_cpu_write(current_kprobe_addr, (u64) tk->rp.kp.addr); +} /* Kprobe profile handler */ static int @@ -1585,6 +1597,7 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) unsigned long orig_ip = instruction_pointer(regs); int ret; + trace_current_kprobe_set(tk); ret = trace_call_bpf(call, regs); /* @@ -1631,8 +1644,11 @@ kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, int size, __size, dsize; int rctx; - if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs)) - return; + if (bpf_prog_array_valid(call)) { + trace_current_kprobe_set(tk); + if (!trace_call_bpf(call, regs)) + return; + } head = this_cpu_ptr(call->perf_events); if (hlist_empty(head)) diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index 227d518e5ba5..19c979834916 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -199,6 +199,7 @@ DECLARE_BASIC_PRINT_TYPE_FUNC(symbol); #ifdef CONFIG_KPROBE_EVENTS bool trace_kprobe_on_func_entry(struct trace_event_call *call); bool trace_kprobe_error_injectable(struct trace_event_call *call); +u64 trace_current_kprobe_addr(void); #else static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call) { @@ -209,6 +210,10 @@ static inline bool trace_kprobe_error_injectable(struct trace_event_call *call) { return false; } +static inline u64 trace_current_kprobe_addr(void) +{ + return 0; +} #endif /* CONFIG_KPROBE_EVENTS */ struct probe_arg { 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 Tue Jun 29 19:29:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12350689 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.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE0D0C11F68 for ; Tue, 29 Jun 2021 19:30:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D860161DF6 for ; Tue, 29 Jun 2021 19:30:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235063AbhF2Tca (ORCPT ); Tue, 29 Jun 2021 15:32:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:29932 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234853AbhF2Tc2 (ORCPT ); Tue, 29 Jun 2021 15:32:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624995000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Yu/yssvroh5wN9vZzKT4DxZ0/5KDe3VlIF6FttuA20I=; b=ew9WRxm1eUwmXg5GZsjslM0fXGHWD0lqwSDTcUyF6/mKCqIWR4v+F/5ajMqIoKt5gxo0xf Q/J9vqDpH3RQAF4+GtKaET0G9KYiIJ16vWun0fLuh2qeD8GbSQxdGu2N29gOUTsElPMoNO y0D31mvSZ4BQ46yunxzMZ4Lr9sD/xG0= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-545-daqy5WELOkWyuGMijRtIpA-1; Tue, 29 Jun 2021 15:29:59 -0400 X-MC-Unique: daqy5WELOkWyuGMijRtIpA-1 Received: by mail-ej1-f70.google.com with SMTP id l6-20020a1709062a86b029046ec0ceaf5cso6170193eje.8 for ; Tue, 29 Jun 2021 12:29:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Yu/yssvroh5wN9vZzKT4DxZ0/5KDe3VlIF6FttuA20I=; b=GpBm2cGWYZG2PBI7ocU67SES3ojLCVYwopK4wWHQ08BMmb16Baa5AgKbOzT0DZwWfH Mid6hFVYHXH66VC1MSGIqKzNsI2MLUF28Foq/yrg9aWOsXcTaNW64BfoAfkw/+dhTreR BGoBF8EGVAiBuN4XwOqZJ7wu0IK3D+iuWzV+PLxk3rRdGhS6lrSNihkcJkuHiYwW00i5 Ry6H0kz+QgfkxIRzSSkve1aBdN9gi5NtzoA5eSymjTeKYAeOn/+1vj0vVbZTgQIwxaiX yVvp5sw0EknkqbBiSmm2FbW9p8ypNfvp5owm04h//64muGZF/0rL/6mGUodTAjgU40yv u6CQ== X-Gm-Message-State: AOAM532NxVAqFfpJsLj/5LkRLvyD8/IutrApUL/yfnOHW4PNz8EZpZcH jMXvSmXPINwO70HMXHjj9CX08mNF4rHBb9DkXBGZz/fJCoDBWnIhb85HfhsqKzVn7Ritxv8Qemu ouF1fjleO+fDN X-Received: by 2002:a17:906:31d4:: with SMTP id f20mr30740839ejf.383.1624994998032; Tue, 29 Jun 2021 12:29:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFqLD5YCRlfenVEivGZyabGHfxF7mU0HSy+wA1Y71DK6LzEYJsG5ZYRxtGnReuiXs6VqLUjg== X-Received: by 2002:a17:906:31d4:: with SMTP id f20mr30740823ejf.383.1624994997819; Tue, 29 Jun 2021 12:29:57 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id n22sm472559eje.3.2021.06.29.12.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Jun 2021 12:29:57 -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 Subject: [PATCH bpf-next 5/5] selftests/bpf: Add test for bpf_get_func_ip helper Date: Tue, 29 Jun 2021 21:29:45 +0200 Message-Id: <20210629192945.1071862-6-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210629192945.1071862-1-jolsa@kernel.org> References: <20210629192945.1071862-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding test for bpf_get_func_ip helper for fentry, fexit, kprobe, kretprobe and fmod_ret programs. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/get_func_ip_test.c | 42 +++++++++++++ .../selftests/bpf/progs/get_func_ip_test.c | 62 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c create mode 100644 tools/testing/selftests/bpf/progs/get_func_ip_test.c diff --git a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c new file mode 100644 index 000000000000..06d34f566bbb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "get_func_ip_test.skel.h" + +void test_get_func_ip_test(void) +{ + struct get_func_ip_test *skel = NULL; + __u32 duration = 0, retval; + int err, prog_fd, i; + __u64 *result; + + skel = get_func_ip_test__open_and_load(); + if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open_and_load")) + goto cleanup; + + err = get_func_ip_test__attach(skel); + if (!ASSERT_OK(err, "get_func_ip_test__attach")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.test1); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + prog_fd = bpf_program__fd(skel->progs.fmod_ret_test); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + + ASSERT_OK(err, "test_run"); + + result = (__u64 *)skel->bss; + for (i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) { + if (!ASSERT_EQ(result[i], 1, "fentry_result")) + break; + } + + get_func_ip_test__detach(skel); + +cleanup: + get_func_ip_test__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c new file mode 100644 index 000000000000..8ca54390d2b1 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +extern const void bpf_fentry_test1 __ksym; +extern const void bpf_fentry_test2 __ksym; +extern const void bpf_fentry_test3 __ksym; +extern const void bpf_fentry_test4 __ksym; +extern const void bpf_modify_return_test __ksym; + +__u64 test1_result = 0; +SEC("fentry/bpf_fentry_test1") +int BPF_PROG(test1, int a) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test1_result = (const void *) addr == &bpf_fentry_test1; + return 0; +} + +__u64 test2_result = 0; +SEC("fexit/bpf_fentry_test2") +int BPF_PROG(test2, int a) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test2_result = (const void *) addr == &bpf_fentry_test2; + return 0; +} + +__u64 test3_result = 0; +SEC("kprobe/bpf_fentry_test3") +int test3(struct pt_regs *ctx) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test3_result = (const void *) addr == &bpf_fentry_test3; + return 0; +} + +__u64 test4_result = 0; +SEC("kretprobe/bpf_fentry_test4") +int BPF_KRETPROBE(test4) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test4_result = (const void *) addr == &bpf_fentry_test4; + return 0; +} + +__u64 test5_result = 0; +SEC("fmod_ret/bpf_modify_return_test") +int BPF_PROG(fmod_ret_test, int a, int *b, int ret) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test5_result = (const void *) addr == &bpf_modify_return_test; + return ret; +}