Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5755559ioo; Wed, 1 Jun 2022 11:54:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYCZ4hgYQ56UzdfLE5FNaAAc7iLdcICthWpwCL3tGWISqaHKViYmHbYWwgUQYn4qRyqtiQ X-Received: by 2002:a63:e449:0:b0:3fb:10d9:ec18 with SMTP id i9-20020a63e449000000b003fb10d9ec18mr676925pgk.43.1654109658280; Wed, 01 Jun 2022 11:54:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654109658; cv=none; d=google.com; s=arc-20160816; b=BPzylWGGtfHqxGfx6LANXH05JX/1P8ltFDwncvb1Pyh2bQYQ3rS+p4rRxkb7O/4VNg qf08ZnCCdB0wy00Sk0fN//pMKMvidDugKtePzBjMXtQgJwPMuP3QXjL7u/+dvyKtIxlJ SfZyVaLz6cMgWxELH/Sd6wdGpy5SZpUiceQKnlkYJjuMB6LlkNWHdnpqe7nRYsBlQSN3 RVJGz245ZZf3s0/LDvrHYN1UVHXAuOzwuItZOvBzP+UeiDUyraWidA7fP+4zyHwUdoNF 3sv9p7wTnrkQfsGpAnSCUPi+nZUOWEXZ0Jph9NUz/CzrOiOTKvmuGAy2jWa65e58ElIC twnA== 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=NoUwJd2+mcdNmPC0eDlo0GLCQ3tt/+PLl2IhJx6P9HlI+McZ5wNDfWCX01MzmvOphB MAT8CIqVyNCI7EUQ1o54UV3SnIvQiH+qGftmaN70z536f3q3FGnrtbIjfcdv7E4cHcX9 ct7Td1rTxrievHv1NcqNkfD+sRjxDCn1PFKHpIj6E+nnRqMPS4/Zg77Os1LpPXeR/UGv ZKxH0sS0hs8Odlu/ZQDgfCXHu9UwxNMLqJks1goINOeiFqdDR4hQFXGMnAmdkhzD09X4 UMkws1X18HRzc3ZJyl/tVudaeLT8bufuJAWTKo0ko55VhIKh2k/RUXQlgBhcLZVNlOhJ lV7g== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id p8-20020a637408000000b003fa74c52be4si3022916pgc.676.2022.06.01.11.54.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 11:54:18 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7BF815BE65; Wed, 1 Jun 2022 11:42:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242841AbiFAR6d convert rfc822-to-8bit (ORCPT + 99 others); Wed, 1 Jun 2022 13:58:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356539AbiFAR6W (ORCPT ); Wed, 1 Jun 2022 13:58:22 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E15349A9BD for ; Wed, 1 Jun 2022 10:58:19 -0700 (PDT) Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 251H20K4025963 for ; Wed, 1 Jun 2022 10:58:19 -0700 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3ge144m6hh-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 01 Jun 2022 10:58:19 -0700 Received: from twshared5413.23.frc3.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Wed, 1 Jun 2022 10:58:17 -0700 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id 42C7B8603CF8; Wed, 1 Jun 2022 10:58:11 -0700 (PDT) From: Song Liu To: , , CC: , , , , , , Song Liu Subject: [PATCH bpf-next 4/5] bpf, x64: Allow to use caller address from stack Date: Wed, 1 Jun 2022 10:57:48 -0700 Message-ID: <20220601175749.3071572-5-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220601175749.3071572-1-song@kernel.org> References: <20220601175749.3071572-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: 8CpoP1pw91Fk0h56iVa29dUbHsg1a5jb X-Proofpoint-ORIG-GUID: 8CpoP1pw91Fk0h56iVa29dUbHsg1a5jb 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-01_06,2022-06-01_01,2022-02-23_01 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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