Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp8401870ioo; Sat, 4 Jun 2022 07:25:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhKuWhhXgVEqLdclQUlVnYXUw9OATU1c8rLhB7RnIcm68bvQBkx3YOAGfywwnL4h0aVA/Y X-Received: by 2002:a17:903:240e:b0:158:eab9:2662 with SMTP id e14-20020a170903240e00b00158eab92662mr14887132plo.87.1654352745037; Sat, 04 Jun 2022 07:25:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654352745; cv=none; d=google.com; s=arc-20160816; b=JerMLFm3A2z7A3EpHe1DGLDhm+ypTvH88ynWno1lTYpkWC10Sgpcyv1B3KvhfF3jBW qGMMSKdWEddIathQoF3M5UxRTF12GkVlNgJDS6ecMJxF33WELoZbd8gCt/36rj0vfQe+ rJJ1C2337Mb5shjFwua7ArzRYrYqXyl1pzbShO9MLq3GpBvE5zBO/rrc40b7OoBnRbqL RBRhS//MAIaNrH4Mk0DNqn0NMWiSJr/ajkC5kxk1H0ClASl+6a+Lu7wl0dEzP1vZsbgz 74cdE8P4OPzn5iCLkrnBBbEZ42Dp0zKjtytStmNIEyCBaS6x5zmiaCitOlD69QbNBllG 6PeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=hgNM5fZXFMukPbYwbp4K6lFiEHUDdnwgwPgKfULsuDw=; b=Fzomx+uuA4lH7+IrA64hV7MIk2Nn3esFlryINK2kGxtZC8s/OO1zUyi2clNYr+NA1D 5T4l2SynB/jYZWfsLnj7mDEYQNxlfe5SjVKXU7io09PT0gbS1zXinrgKhgQKTa1k1qu0 O0CdobHO/adcPTxOIn3qTOWiUEBSYT4o928Bp5UoUdovOWoiC+DKwTRjB/idtEd95qc6 bqJWMVKdpMXWBuyAn2ZD1VgLo1JVZhZ2VHg9KOl7iqR0xDERsyXPNjtcvwBwiv+V5wW0 KaxjDwj2+/OHJMgIwKEFV/HtD5zokT2prB/8VhzTGDAujaY6g3PaHCgXfjlgZzdHMOd9 qSfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g3-20020a056a000b8300b0051842cffa45si15116160pfj.356.2022.06.04.07.25.29; Sat, 04 Jun 2022 07:25:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238904AbiFBTtD convert rfc822-to-8bit (ORCPT + 99 others); Thu, 2 Jun 2022 15:49:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239006AbiFBTs4 (ORCPT ); Thu, 2 Jun 2022 15:48:56 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DFDE3388C for ; Thu, 2 Jun 2022 12:48:46 -0700 (PDT) Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2529scbL008820 for ; Thu, 2 Jun 2022 12:48:45 -0700 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3geu05bgdg-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 02 Jun 2022 12:48:45 -0700 Received: from twshared14818.18.frc3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Thu, 2 Jun 2022 12:48:43 -0700 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id E309486C00F3; Thu, 2 Jun 2022 12:37:19 -0700 (PDT) From: Song Liu To: , , CC: , , , , , , , Song Liu Subject: [PATCH v2 bpf-next 4/5] bpf, x64: Allow to use caller address from stack Date: Thu, 2 Jun 2022 12:37:05 -0700 Message-ID: <20220602193706.2607681-5-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220602193706.2607681-1-song@kernel.org> References: <20220602193706.2607681-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: MFbdeSM3JrJqGjbviu3Z5VIyjh6-Dmsg X-Proofpoint-ORIG-GUID: MFbdeSM3JrJqGjbviu3Z5VIyjh6-Dmsg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-02_05,2022-06-02_01,2022-02-23_01 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Olsa Currently we call the original function by using the absolute address given at the JIT generation. That's not usable when having trampoline attached to multiple functions, or the target address changes dynamically (in case of live patch). In such cases we need to take the return address from the stack. Adding support to retrieve the original function address from the stack by adding new BPF_TRAMP_F_ORIG_STACK flag for arch_prepare_bpf_trampoline function. Basically we take the return address of the 'fentry' call: function + 0: call fentry # stores 'function + 5' address on stack function + 5: ... The 'function + 5' address will be used as the address for the original function to call. Signed-off-by: Jiri Olsa Signed-off-by: Song Liu --- arch/x86/net/bpf_jit_comp.c | 13 +++++++++---- include/linux/bpf.h | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index f298b18a9a3d..c835a9f18fd8 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -2130,10 +2130,15 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i if (flags & BPF_TRAMP_F_CALL_ORIG) { restore_regs(m, &prog, nr_args, regs_off); - /* call original function */ - if (emit_call(&prog, orig_call, prog)) { - ret = -EINVAL; - goto cleanup; + if (flags & BPF_TRAMP_F_ORIG_STACK) { + emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, 8); + EMIT2(0xff, 0xd0); /* call *rax */ + } else { + /* call original function */ + if (emit_call(&prog, orig_call, prog)) { + ret = -EINVAL; + goto cleanup; + } } /* remember return value in a stack for bpf prog to access */ emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8); diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 8e6092d0ea95..a6e06f384e81 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -733,6 +733,11 @@ struct btf_func_model { /* Return the return value of fentry prog. Only used by bpf_struct_ops. */ #define BPF_TRAMP_F_RET_FENTRY_RET BIT(4) +/* Get original function from stack instead of from provided direct address. + * Makes sense for fexit programs only. + */ +#define BPF_TRAMP_F_ORIG_STACK BIT(5) + /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. */ -- 2.30.2