Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp2795650imw; Sun, 17 Jul 2022 17:27:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tliLiK/OxspuM2WRiZvMFr+ZkvEC6x1jMlIm6dDJ0KondfvMUSOvs6ioy5DlQ2/yz5k4OM X-Received: by 2002:a17:90a:1c01:b0:1f1:bf54:67ff with SMTP id s1-20020a17090a1c0100b001f1bf5467ffmr4648303pjs.172.1658104059951; Sun, 17 Jul 2022 17:27:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658104059; cv=none; d=google.com; s=arc-20160816; b=IdnCvLsdLoPT/vsg1WuEpYr4JuhGhEPnB+PX3oZDZU0nnKyg2CNLE3816ju421sa+l m44gkvo9Myjwcvv8oc/R4Thl6WrbsCkemsMOhHYsnvZ/kHKTCfGwbA76YtBctg9HEoJ2 FaHspPUvt503DLj7w12kabi6JOtAnGRwlkUYtQhw0F09Hg9+0jPqFRsOz8NFwOcF23+U sYgnkkk7ipnNkpmIvOZu+wtU4/LR1+z/03gF8mBWuWa4KqhQrLQIPn5h9HH+JUfl5S8y 5xn8i+fumCtMj82FNtAvGYk+jSRK1pOP8ArG/Nv27ZB0hi+O9o/YA+sqfMhG08I+uKDO sKww== 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=ZF9V6gFHNy+DlDhVCq9uRK7pJZ2p7JHGRKT0MLeDEgw=; b=OGrNGyK4Gwcc/bo66Q220hGFCVfaeLg4ywGo5JS4Na1jl8DDghSSWIo/StaEM1Zen6 +E+cP8DR1VxZKsRiDSFJfLYaSBhAFQXdNcZBpooPN/b/rAX70/JShxF9WID9jR84V2M2 AJqJtra2tCDJow5p9VH/2T4gqxcwM+F2Xt/53XXlFXI0IpPK5p1kBm8YyIA0m8mnW0di ho9MT22klTVuj2/O1TiSuy1bgJDkla77L/MiCrinNeNuPibU2otW1LxD51JPj55aVBQH zXB60OzXscPjcH4UrbL6zjEfhA7iw4WrFNu71fQ/uIiXA80F2KfejJcvwbeFYIbViO0i e5iQ== 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 q17-20020a056a00151100b005286a2a16b5si9233850pfu.110.2022.07.17.17.27.25; Sun, 17 Jul 2022 17:27:39 -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 S233006AbiGRAOq convert rfc822-to-8bit (ORCPT + 99 others); Sun, 17 Jul 2022 20:14:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232803AbiGRAOo (ORCPT ); Sun, 17 Jul 2022 20:14:44 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71EBE9FE8 for ; Sun, 17 Jul 2022 17:14:37 -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 26HM5bAf009114 for ; Sun, 17 Jul 2022 17:14:37 -0700 Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3hbrnqe1fy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sun, 17 Jul 2022 17:14:37 -0700 Received: from twshared34609.14.frc2.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Sun, 17 Jul 2022 17:14:33 -0700 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id E6A3FA495D03; Sun, 17 Jul 2022 17:14:22 -0700 (PDT) From: Song Liu To: , , CC: , , , , Song Liu Subject: [PATCH v3 bpf-next 3/4] bpf, x64: Allow to use caller address from stack Date: Sun, 17 Jul 2022 17:14:04 -0700 Message-ID: <20220718001405.2236811-4-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220718001405.2236811-1-song@kernel.org> References: <20220718001405.2236811-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: HaBRjEb90im_X4ahKX8I1UwkCGGp3mdo X-Proofpoint-ORIG-GUID: HaBRjEb90im_X4ahKX8I1UwkCGGp3mdo X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-07-17_17,2022-07-15_01,2022-06-22_01 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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 54c7f46c453f..e1b0c5ed0b7c 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -2119,10 +2119,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 a5bf00649995..7496842a4671 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -751,6 +751,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 trampolines with fexit or fmod_ret programs. + */ +#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