From patchwork Wed Jul 7 19:46:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 471134 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EF9DC07E9B for ; Wed, 7 Jul 2021 19:46:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 05CFA61CC2 for ; Wed, 7 Jul 2021 19:46:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232930AbhGGTtN (ORCPT ); Wed, 7 Jul 2021 15:49:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:39268 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232660AbhGGTtN (ORCPT ); Wed, 7 Jul 2021 15:49:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QgBr4LV9SR6/Qelufj3zindVEajUhYS2+l3zjx7rHSU=; b=LnVMXQFH5pgY3ojaN0hM8HLv970Zpb82XHV+zT1gXHIwUuO8Om/4c/NiWUKOlBAIQ6MLmf iVUh+YdUKT6dQeQjT+pIZJzqBvafPFR0JnAaqUtcQo1iI0J5D25R5u1nNnOk7DWQ8Y0+Gv mMwfn45MpGDr3HJ7Y1Qc/OD8v9MLjVE= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-228-SArVoU4cOF68W-dbU_wvBg-1; Wed, 07 Jul 2021 15:46:31 -0400 X-MC-Unique: SArVoU4cOF68W-dbU_wvBg-1 Received: by mail-wm1-f71.google.com with SMTP id p4-20020a05600c3584b0290210725ac6bcso2555458wmq.2 for ; Wed, 07 Jul 2021 12:46:31 -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=g9WpF5HdG3Jd4+7xSNNtW4J5X1bQT69iUwqvG7IrCSsUe9tugMyz0vRwbwH/DSRtjM YxxZMJq+d5+OaRnckuKkNPtsOQpPyqJTxIZLF7rV1Olh20yCRMveALDrX2I7m/z99PxV OwAGCJmXxfOWLxckbNfYJ9o/6BzA85Rbhmyba1kBVZwkV34ol/zBFs75xqRsMRJN0Z4J OgYALfmWgZhHT5jF59dAMRjE3TwJlNlWPsUKfWNRe3qL16f3F2CoOpI29ntPLlFs85vw PEIQYBcHxn3tbdVzc1LlrFlrLv8ZZnBOZ8UYP/+fuRoXt8KTXCed7OyHjRRweBdG5hGX Iz8w== X-Gm-Message-State: AOAM531qFV1nM7OA6V8wZ93MfqC9RrlNm6i+t0Cx543Mkpm/Tcj0VAay G4u6eUA/yfWxpmdKXRbdqZ0L/cZWa+NrYIlA0viMcLA7Q2g6pAPlLOkofEsGV8glpTuwCHnvzRc qA7lqEjNEa0myKeab X-Received: by 2002:adf:9084:: with SMTP id i4mr30099347wri.23.1625687190057; Wed, 07 Jul 2021 12:46:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxLO79EMn3rV783LaMxncMKtw60ZojKfQRrfALZnttyQBWpER13DmAyWOA56j8/XujiSgM20A== X-Received: by 2002:adf:9084:: with SMTP id i4mr30099335wri.23.1625687189932; Wed, 07 Jul 2021 12:46:29 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id b7sm17543795wri.96.2021.07.07.12.46.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:29 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 1/7] bpf, x86: Store caller's ip in trampoline stack Date: Wed, 7 Jul 2021 21:46:13 +0200 Message-Id: <20210707194619.151676-2-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Storing caller's ip in trampoline's stack. Trampoline programs can reach the IP in (ctx - 8) address, so there's no change in program's arguments interface. The IP address is takes from [fp + 8], which is return address from the initial 'call fentry' call to trampoline. This IP address will be returned via bpf_get_func_ip helper helper, which is added in following patches. Signed-off-by: Jiri Olsa --- arch/x86/net/bpf_jit_comp.c | 19 +++++++++++++++++++ include/linux/bpf.h | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index e835164189f1..c320b3ce7b58 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1951,6 +1951,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i if (flags & BPF_TRAMP_F_CALL_ORIG) stack_size += 8; /* room for return value of orig_call */ + if (flags & BPF_TRAMP_F_IP_ARG) + stack_size += 8; /* room for IP address argument */ + if (flags & BPF_TRAMP_F_SKIP_FRAME) /* skip patched call instruction and point orig_call to actual * body of the kernel function. @@ -1964,6 +1967,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i EMIT4(0x48, 0x83, 0xEC, stack_size); /* sub rsp, stack_size */ EMIT1(0x53); /* push rbx */ + if (flags & BPF_TRAMP_F_IP_ARG) { + /* Store IP address of the traced function: + * mov rax, QWORD PTR [rbp + 8] + * sub rax, X86_PATCH_SIZE + * mov QWORD PTR [rbp - stack_size], rax + */ + emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, 8); + EMIT4(0x48, 0x83, 0xe8, X86_PATCH_SIZE); + emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -stack_size); + + /* Continue with stack_size for regs storage, stack will + * be correctly restored with 'leave' instruction. + */ + stack_size -= 8; + } + save_regs(m, &prog, nr_args, stack_size); if (flags & BPF_TRAMP_F_CALL_ORIG) { diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f309fc1509f2..6b3da9bc3d16 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -554,6 +554,11 @@ struct btf_func_model { */ #define BPF_TRAMP_F_SKIP_FRAME BIT(2) +/* Store IP address of the caller on the trampoline stack, + * so it's available for trampoline's programs. + */ +#define BPF_TRAMP_F_IP_ARG BIT(3) + /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 */ From patchwork Wed Jul 7 19:46:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 471133 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1AB8C07E9B for ; Wed, 7 Jul 2021 19:46:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A83A961CB0 for ; Wed, 7 Jul 2021 19:46:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232766AbhGGTta (ORCPT ); Wed, 7 Jul 2021 15:49:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:28143 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232572AbhGGTt2 (ORCPT ); Wed, 7 Jul 2021 15:49:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FQtsSphgnYl9qFWBMJGlJNrhM3rcnyrmEFUQ3XZzsHM=; b=hPv4Y+Db+TnTVyPRR8Sfm5bPkHrkxRFVZA+AyurMktUNEu8fDaNN37gl3fi3kHzYUwwD5x 8PsRZrh9sshw3G8m+y6G4G3GlG0zjohhEHX1M4U7fgh6SJiJjz0YGLGwUuWO7tL0sgb7Fd 8YhtPjHLFbSJFlNRO5VtQOqV1BcXQro= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-523-ah0vV0w4NDK0HGggFePFxA-1; Wed, 07 Jul 2021 15:46:46 -0400 X-MC-Unique: ah0vV0w4NDK0HGggFePFxA-1 Received: by mail-wm1-f70.google.com with SMTP id n37-20020a05600c3ba5b02901fe49ba3bd0so1413031wms.1 for ; Wed, 07 Jul 2021 12:46:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FQtsSphgnYl9qFWBMJGlJNrhM3rcnyrmEFUQ3XZzsHM=; b=le4ABOrO7u/AaoVxAepOFiwllKgtFlRCwjqkJr0uHdP9qNP973bXeXQULCWx3Wy0zt X2caJz7Do7xuqB9Uiq7mIiaaQxAEituZN/flvJ1ajlQG9CIzbVVK+mclXOp5bC0iK0St IqAQp3l6dllA5DITIpEtECzmfEC9YbxvBnBeBDFSmeDnvP+T57c7C5TEztnSAgzm9dAF 9ygxP0RrTGs/4Ih0X3fCzC1OwVgtoEe64/TL851MVmSN4Pt3HgWgmIxR5m5C/TfT5z5G ERueaGVCxtTnbKf39RChSjH6b+Ksgxj7pkA0goxlsyAxXAtf+69Ej4cXwezNkkwtun2n vvHg== X-Gm-Message-State: AOAM532dqjajBG3WrApnVvhmR6T0r4+Qy2gtn/YVzn8fYIh9lUrUaISm aR3FbJ/6Jfm/mC7+7CJb3oUYYOOcARFe1u4q1jonGlPIiRjBLO5VjQIfxRD68Fi6o/TgUjaaZa5 z50VB9IRRavCokjAN X-Received: by 2002:a05:600c:4b98:: with SMTP id e24mr29173937wmp.61.1625687205386; Wed, 07 Jul 2021 12:46:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysmUi2r9/ejoClvXbNGkVGIne0VRSRhD66jvlDPHpaqc/LLRXHwdCN6MdaDStGbnsXXyPaTw== X-Received: by 2002:a05:600c:4b98:: with SMTP id e24mr29173919wmp.61.1625687205174; Wed, 07 Jul 2021 12:46:45 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id b9sm25293403wrh.81.2021.07.07.12.46.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:44 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: kernel test robot , Dan Carpenter , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 3/7] bpf: Add bpf_get_func_ip helper for tracing programs Date: Wed, 7 Jul 2021 21:46:15 +0200 Message-Id: <20210707194619.151676-4-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding bpf_get_func_ip helper for BPF_PROG_TYPE_TRACING programs, specifically for all trampoline attach types. The trampoline's caller IP address is stored in (ctx - 8) address. so there's no reason to actually call the helper, but rather fixup the call instruction and return [ctx - 8] value directly (suggested by Alexei). [fixed has_get_func_ip wrong return type] Reported-by: kernel test robot Reported-by: Dan Carpenter Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 7 +++++ kernel/bpf/verifier.c | 53 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 15 ++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++ 4 files changed, 82 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index bf9252c7381e..83e87ffdbb6e 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4780,6 +4780,12 @@ union bpf_attr { * Execute close syscall for given FD. * Return * A syscall result. + * + * u64 bpf_get_func_ip(void *ctx) + * Description + * Get address of the traced function (for tracing programs). + * Return + * Address of the traced function. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4951,6 +4957,7 @@ union bpf_attr { FN(sys_bpf), \ FN(btf_find_by_name_kind), \ FN(sys_close), \ + FN(get_func_ip), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index be38bb930bf1..f975a3aa9368 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5149,6 +5149,11 @@ static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); } +static bool allow_get_func_ip_tracing(struct bpf_verifier_env *env) +{ + return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); +} + static int check_map_func_compatibility(struct bpf_verifier_env *env, struct bpf_map *map, int func_id) { @@ -5955,6 +5960,32 @@ static int check_bpf_snprintf_call(struct bpf_verifier_env *env, return err; } +static int has_get_func_ip(struct bpf_verifier_env *env) +{ + enum bpf_attach_type eatype = env->prog->expected_attach_type; + enum bpf_prog_type type = resolve_prog_type(env->prog); + int func_id = BPF_FUNC_get_func_ip; + + if (type == BPF_PROG_TYPE_TRACING) { + if (eatype != BPF_TRACE_FENTRY && eatype != BPF_TRACE_FEXIT && + eatype != BPF_MODIFY_RETURN) { + verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", + func_id_name(func_id), func_id); + return -ENOTSUPP; + } + if (!allow_get_func_ip_tracing(env)) { + verbose(env, "func %s#%d for tracing programs supported only for JITed x86_64\n", + func_id_name(func_id), func_id); + return -ENOTSUPP; + } + return 0; + } + + verbose(env, "func %s#%d not supported for program type %d\n", + func_id_name(func_id), func_id, type); + return -ENOTSUPP; +} + static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, int *insn_idx_p) { @@ -6225,6 +6256,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) env->prog->call_get_stack = true; + if (func_id == BPF_FUNC_get_func_ip) { + if (has_get_func_ip(env)) + return -ENOTSUPP; + env->prog->call_get_func_ip = true; + } + if (changes_data) clear_all_pkt_pointers(env); return 0; @@ -12369,6 +12406,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env) { struct bpf_prog *prog = env->prog; bool expect_blinding = bpf_jit_blinding_enabled(prog); + enum bpf_prog_type prog_type = resolve_prog_type(prog); struct bpf_insn *insn = prog->insnsi; const struct bpf_func_proto *fn; const int insn_cnt = prog->len; @@ -12702,6 +12740,21 @@ static int do_misc_fixups(struct bpf_verifier_env *env) continue; } + /* Implement bpf_get_func_ip inline. */ + if (prog_type == BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_get_func_ip) { + /* Load IP address from ctx - 8 */ + insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + + new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); + if (!new_prog) + return -ENOMEM; + + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; + continue; + } + patch_call_imm: fn = env->ops->get_func_proto(insn->imm, env->prog); /* all functions that have prototype and verifier allowed diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 64bd2d84367f..9edd3b1a00ad 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -948,6 +948,19 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .arg5_type = ARG_ANYTHING, }; +BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) +{ + /* Stub, the helper call is inlined in the program. */ + return 0; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { + .func = bpf_get_func_ip_tracing, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, +}; + const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1058,6 +1071,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_for_each_map_elem_proto; case BPF_FUNC_snprintf: return &bpf_snprintf_proto; + case BPF_FUNC_get_func_ip: + return &bpf_get_func_ip_proto_tracing; default: return NULL; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index bf9252c7381e..83e87ffdbb6e 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4780,6 +4780,12 @@ union bpf_attr { * Execute close syscall for given FD. * Return * A syscall result. + * + * u64 bpf_get_func_ip(void *ctx) + * Description + * Get address of the traced function (for tracing programs). + * Return + * Address of the traced function. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -4951,6 +4957,7 @@ union bpf_attr { FN(sys_bpf), \ FN(btf_find_by_name_kind), \ FN(sys_close), \ + FN(get_func_ip), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper From patchwork Wed Jul 7 19:46:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 471132 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 044E1C07E9B for ; Wed, 7 Jul 2021 19:47:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E40DF61CB0 for ; Wed, 7 Jul 2021 19:47:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233056AbhGGTtr (ORCPT ); Wed, 7 Jul 2021 15:49:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26651 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232572AbhGGTtp (ORCPT ); Wed, 7 Jul 2021 15:49:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687224; 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=O9bsZnK7+qv8cOkpRTvCMfenheVTECQhA06bGXkOaX/eK+AEi6I5Pj4A5vBHWz5D27l61s KcheVnjE0OrOr8DXXdueC6olOoR1LqsVSG2ba9/g75n0zRBmoYSfnb9I7YwDZgfR5JP/Pc 7Z+n8MxD+J5V1H+tptLF1q5/auDeqgg= 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-58-WvPtYhwoOM-XtB43Cn_Y2A-1; Wed, 07 Jul 2021 15:47:01 -0400 X-MC-Unique: WvPtYhwoOM-XtB43Cn_Y2A-1 Received: by mail-wm1-f72.google.com with SMTP id t12-20020a7bc3cc0000b02901f290c9c44eso1401208wmj.7 for ; Wed, 07 Jul 2021 12:47: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=Yu/yssvroh5wN9vZzKT4DxZ0/5KDe3VlIF6FttuA20I=; b=WQibmMddriT/b5FIJHdkaACq/BWS1DU90K5KR5HDLLIWsVqTva1hWWGEOrqyI7ZXBh YMvm7N5RtHcuefyB5fbJ0+Ez/30oWavWcC9qE4HBxOiAh1sn1zB+A9wMKQVerJMJNtZ/ nApVuOtvv2XmHBF4EQ2YW+mdqg3NpYoRP/5tFSUiMPZKli9TGuZErg8IvmMOQ17LgfKM nurFBFiJdlnY7EeHReeXYVVU07HVtXN0PqcrAMYCKoOX1Rm09SQvrSIqIucBhbP+hQVz aWbi1V1aWxT7POAl1kLrXqihQSSDnohvF18kT1lmr5AhesXcbbsTO7b+mGawx/wGvkWT hlMw== X-Gm-Message-State: AOAM532k+vSX5+/7K3dIxuNsDnswh5TpOzAENNn76lzCrd/G7GcrarJa dIsF4GbpHAITbK0D56b1WQI0SW1wo5LNgQgDSBwU4OrQAMR92ofGhq8UqAXN6fxP715XrbPjqGG 2By+iDefuVbpKNhBF X-Received: by 2002:a7b:c0c6:: with SMTP id s6mr800182wmh.176.1625687220101; Wed, 07 Jul 2021 12:47:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4ko4VbSlhXPsLn2IGSoVpTJFVa9bwyIz+SEM80cwEG3J9oL6KvVet35WTjUpZbLWaKDSmkA== X-Received: by 2002:a7b:c0c6:: with SMTP id s6mr800164wmh.176.1625687219924; Wed, 07 Jul 2021 12:46:59 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id c16sm19416281wmr.2.2021.07.07.12.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:46:59 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 5/7] selftests/bpf: Add test for bpf_get_func_ip helper Date: Wed, 7 Jul 2021 21:46:17 +0200 Message-Id: <20210707194619.151676-6-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding test for bpf_get_func_ip helper for fentry, fexit, kprobe, kretprobe and fmod_ret programs. Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/get_func_ip_test.c | 42 +++++++++++++ .../selftests/bpf/progs/get_func_ip_test.c | 62 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c create mode 100644 tools/testing/selftests/bpf/progs/get_func_ip_test.c diff --git a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c new file mode 100644 index 000000000000..06d34f566bbb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "get_func_ip_test.skel.h" + +void test_get_func_ip_test(void) +{ + struct get_func_ip_test *skel = NULL; + __u32 duration = 0, retval; + int err, prog_fd, i; + __u64 *result; + + skel = get_func_ip_test__open_and_load(); + if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open_and_load")) + goto cleanup; + + err = get_func_ip_test__attach(skel); + if (!ASSERT_OK(err, "get_func_ip_test__attach")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.test1); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(retval, 0, "test_run"); + + prog_fd = bpf_program__fd(skel->progs.fmod_ret_test); + err = bpf_prog_test_run(prog_fd, 1, NULL, 0, + NULL, NULL, &retval, &duration); + + ASSERT_OK(err, "test_run"); + + result = (__u64 *)skel->bss; + for (i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) { + if (!ASSERT_EQ(result[i], 1, "fentry_result")) + break; + } + + get_func_ip_test__detach(skel); + +cleanup: + get_func_ip_test__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c new file mode 100644 index 000000000000..8ca54390d2b1 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +extern const void bpf_fentry_test1 __ksym; +extern const void bpf_fentry_test2 __ksym; +extern const void bpf_fentry_test3 __ksym; +extern const void bpf_fentry_test4 __ksym; +extern const void bpf_modify_return_test __ksym; + +__u64 test1_result = 0; +SEC("fentry/bpf_fentry_test1") +int BPF_PROG(test1, int a) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test1_result = (const void *) addr == &bpf_fentry_test1; + return 0; +} + +__u64 test2_result = 0; +SEC("fexit/bpf_fentry_test2") +int BPF_PROG(test2, int a) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test2_result = (const void *) addr == &bpf_fentry_test2; + return 0; +} + +__u64 test3_result = 0; +SEC("kprobe/bpf_fentry_test3") +int test3(struct pt_regs *ctx) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test3_result = (const void *) addr == &bpf_fentry_test3; + return 0; +} + +__u64 test4_result = 0; +SEC("kretprobe/bpf_fentry_test4") +int BPF_KRETPROBE(test4) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test4_result = (const void *) addr == &bpf_fentry_test4; + return 0; +} + +__u64 test5_result = 0; +SEC("fmod_ret/bpf_modify_return_test") +int BPF_PROG(fmod_ret_test, int a, int *b, int ret) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test5_result = (const void *) addr == &bpf_modify_return_test; + return ret; +} From patchwork Wed Jul 7 19:46:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 471131 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9ED10C07E95 for ; Wed, 7 Jul 2021 19:47:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81A4D61CBE for ; Wed, 7 Jul 2021 19:47:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233089AbhGGTt7 (ORCPT ); Wed, 7 Jul 2021 15:49:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33200 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233086AbhGGTt6 (ORCPT ); Wed, 7 Jul 2021 15:49:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1625687237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zbTjAalRT8G7ucBl7KamxhzUVx0mh7mTfA4EZ32X+1U=; b=gwodM7Q5OGzlScxodxH/mBqHiQwVRtah9sz55ovP9qsFTX/Q4eqaLpl6919aB+qLPyjOTz 3cTQxNm6GL32zOc37v6UnJQKsJ5rkll646CMpbXH88Qxc0kgEZ1bSNTQLq/gPVQpCkLsRk 7W0aC0RA8ngkSSwXwWqCv8YMoVlPBU0= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-359-ym0Ub-7nPcKR7lvtAeWDtA-1; Wed, 07 Jul 2021 15:47:16 -0400 X-MC-Unique: ym0Ub-7nPcKR7lvtAeWDtA-1 Received: by mail-wr1-f71.google.com with SMTP id w4-20020a05600018c4b0290134e4f784e8so1127398wrq.10 for ; Wed, 07 Jul 2021 12:47:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zbTjAalRT8G7ucBl7KamxhzUVx0mh7mTfA4EZ32X+1U=; b=dIXHl2xt1gwvHGYT5OaYum+KIx329Vnh/Srq012KVxOV4AQRaIcuDNGeIm2F2cz7DY hg18JLbO8vSK/A+XzZ6WkYzmbrRVznbWs97++BKVhobWGDTm5OiAU/6VNcfHioPIghwA oSWuWgbQhYTf4kemWXBQggNnZT+upfz7nryC/E4o+35w+D3zfbqYjt8bdgkO/4Kcq0/l ADo8la4cdLAkK5Nzdkcuj88uH5v+YRmiv06c9yxDxMsAd4Vk9NeuwdafkF5Qg7tJgB21 1EkgcLdKZmUf+9wRJaNfhv8XwXRNUtgk148QqBwopK4JFirDHgtJdEEVs+vH9puGfzYV Ad4w== X-Gm-Message-State: AOAM530vpzQvK2eODVnVxmiJWPukyiIn6sBZVkHk0ennh5QzdChWOIxk 5xz0h740mBsc8zPFmJYMBrFyc98MNM7l38oUP2PdWhAYlS6u6tLaVX10yuewUjKlbICCbYl0mhH oYwrg9eAmHEwAKqkv X-Received: by 2002:a5d:4d4b:: with SMTP id a11mr5812959wru.325.1625687235391; Wed, 07 Jul 2021 12:47:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCeEyHGUskiHM337qfooE0f3Ro/cCDcwbEMlSi+dsBwOUNqQw0kh8Z0/fopTxAKcSeFTYUDw== X-Received: by 2002:a5d:4d4b:: with SMTP id a11mr5812949wru.325.1625687235220; Wed, 07 Jul 2021 12:47:15 -0700 (PDT) Received: from krava.redhat.com ([185.153.78.55]) by smtp.gmail.com with ESMTPSA id l16sm7619853wmj.47.2021.07.07.12.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:47:14 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Alan Maguire Subject: [PATCH 7/7] selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe Date: Wed, 7 Jul 2021 21:46:19 +0200 Message-Id: <20210707194619.151676-8-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707194619.151676-1-jolsa@kernel.org> References: <20210707194619.151676-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding test for bpf_get_func_ip in kprobe+ofset probe. Because of the offset value it's arch specific, adding it only for x86_64 architecture. Signed-off-by: Jiri Olsa --- .../testing/selftests/bpf/progs/get_func_ip_test.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c index 8ca54390d2b1..e8a9428a0ea3 100644 --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c @@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym; extern const void bpf_fentry_test3 __ksym; extern const void bpf_fentry_test4 __ksym; extern const void bpf_modify_return_test __ksym; +extern const void bpf_fentry_test6 __ksym; __u64 test1_result = 0; SEC("fentry/bpf_fentry_test1") @@ -60,3 +61,15 @@ int BPF_PROG(fmod_ret_test, int a, int *b, int ret) test5_result = (const void *) addr == &bpf_modify_return_test; return ret; } + +#ifdef __x86_64__ +__u64 test6_result = 0; +SEC("kprobe/bpf_fentry_test6+0x5") +int test6(struct pt_regs *ctx) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test6_result = (const void *) addr == &bpf_fentry_test6 + 5; + return 0; +} +#endif