From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.53.43 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991736; b=iG6oCM4geE7kFDGGkSQUat66k/x3HbOr+aZi0r8ppTde4RsnWziP4E4UKV+PW6pGV4U0 wAkUb3JHZoFTrNX6i+dAz/qvpbpz+2tKPow6X7+vuN9ILi9j6vw0ruo00FThrhc36l5gN R0rRdfH1fpP2dfqDDRVzJ7DwU/6IYf+wZVlu6MxAcgQymfoM7CGPQIr4/PJ8KhbaKIst6 0C6w+dSuSUQvBLDp/l++hBcjTXzWRC8RBXUClUPW3LgtWcxvB5tGM/B9v7jRtqOy4sDUk FRHUH9l1pMke7jQK95816hpMQkR6DIY4XX/xvXOpzsmRmYE20uycTA1z83n1Uqj2gnQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991736; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=/lxyYdGAxzKkIvBB5I1H6SIlSynGgj2CcgZhCVNxYv4=; b=Riw1+7DQ+7zimd8yilquUR8nPfWJ2AOc6fus3hYCldtKP/+xG3IlrCvBf5EvQLTwb2VS kHkn/tq3HvXNFZrZEcWGAzvbYQg/qoQYV79MB/4ucmfvuAGWkw4aZRrpH8DPGOOKO74u9 r/Ecdf1BVPtYyf9XtpvTGUzGaF0oYlU/YL6jA5xvOfq8aM8MB34koHMCBDhzB9AlttKZP xeQuO32Gsz7eRXBcCZz/mCzizI+4tNzUm29IEQe+kKVBrHWMIqzdfwJaRnx8J+4PJQwdq xLEnzUCS4pUKsOckvIM0DO32J9h82Q2QmlgsNF8hgH7xkomncSfAw68LN0JZ/TAN41A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=fQNCrLGV; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.53.43 Received: from BL0PR03CU003.outbound.protection.outlook.com (mail-eastusazon11012043.outbound.protection.outlook.com [52.101.53.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F105606A327 for ; Sun, 29 Nov 2026 22:35:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8F105606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=fQNCrLGV ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=OIq7IeuqHBL0vpBH8AD5TbwybabbHnq8yKnUIpBy6pd8Ag/sO1X1FKf/3WdGBmUfpQjZS4YZQDEblPkWJVIyUihCxVrHjDP2rBvql2Cwxs87Q5pzAo0H+bPtxIjP0m65PwH3/k1/EsniTUpyBqb/lXapaPCTMqHmkzjyCb8n5EwfLqoXHGYBRWwXIIWSp4qQ5oweba7XQ3NDFLuyZp2eHOKfOdiUy3qW1h6fUNbPvwaD8ZebdzvwQnux+3kHgwK0vBjt/h5NyLMac980nCq8F4XXBVtkkwaaTnfY/2bl4sy0qHNsY/GCfX3Q14H15nb7V162on91Q8unylQvR47MGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/lxyYdGAxzKkIvBB5I1H6SIlSynGgj2CcgZhCVNxYv4=; b=SyAYSVHeh+kyhkc+cl3rp09eUntGyElkc/eUujGn1gz3CertS63ELFT4Gj7ueWJfHLplc/igH2UHw5MPpF3tcxdKY+EPh4QVeP4QpQTiRzHNHqzhdRK+4RBypcz7drdaIkWN2bSabESD9Q+GdpXSwAu9KDrxuvNnZfOqGyXDNqipcFDdCrSZBnPNPQyytlA8El+1Px+9wyIWL9dJYWIVblYiUI4ZvG9ZbMpSViDRdu180vFtZHWIxZ2SCDrX9PH5jEceyHfshSlZ+3SxvztZKitOSamj5/OVaJo0EUnBbvd1fTRNwhHEibrngKVpb+ybcQyoxJ7IOvuXAErBgx82vA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linux.intel.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/lxyYdGAxzKkIvBB5I1H6SIlSynGgj2CcgZhCVNxYv4=; b=fQNCrLGVq3hpyVyzCsywoGJMtTIYZv5aSJo2w3j11rsQZNBNoAy9tdDQzPus3WdzJCJdAVszBe5ZaO+g9D5LYzG0Te+sLnyctYqdNPs2Eoo4QB0FXPtYRD4L9W0Icnsqas+hJBr9J01YgoIi7whKikG8muwZv3OpPt+STebzA6k= Received: from PH8P220CA0001.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:345::9) by IA1PR12MB9522.namprd12.prod.outlook.com (2603:10b6:208:594::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.11; Sun, 29 Nov 2026 22:35:30 +0000 Received: from CY7PEPF000000F5.namprd05.prod.outlook.com (2603:10b6:510:345:cafe::76) by PH8P220CA0001.outlook.office365.com (2603:10b6:510:345::9) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.12 via Frontend Transport; Sun, 29 Nov 2026 22:35:29 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by CY7PEPF000000F5.mail.protection.outlook.com (10.167.247.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Sun, 29 Nov 2026 22:35:29 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Sun, 29 Nov 2026 16:35:24 -0600 From: K Prateek Nayak To: Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , the arch/x86 maintainers , CC: Andy Lutomirski , Peter Zijlstra , Rick Edgecombe , Andrew Cooper , Kees Cook , Pawan Gupta , K Prateek Nayak Subject: [RFC PATCH 18/27] x86/bpf: Make BPF JIT aware of CET handling Date: Sun, 29 Nov 2026 22:29:43 +0000 Message-ID: <20261129222953.9124-19-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com> References: <20261129222953.9124-1-kprateek.nayak@amd.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY7PEPF000000F5:EE_|IA1PR12MB9522:EE_ X-MS-Office365-Filtering-Correlation-Id: 1505ddb4-fc62-406a-3f35-08df4e691245 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|23010399003|1800799024|82310400026|376014|7416014|10067099003|56012099006|261028144727099003|261026121527099003|261026140227099003|261102123227099003|261021123027099003|261021121427099003|261022115727099003|261022075727099003|261022105027099003|261009223027099003|6133799003|22082099003|18002099003|11063799006; X-Microsoft-Antispam-Message-Info: YLW/XkBdeJJvp7YvoMAkfGhHXc4zxr68gH07AgP+n+IkVCJgO36HMykBSwVmh384hkIul1SGZCWTK2E0kv80/Pq4VC6woplop9YYrfO5QeCW22v5spT35iwsWa5mBkY8U0cyyEfAe+UnmcQoANlABL7XQxsD8rLAGU8oXzvHi1K9Z8DQsm9bYqqH2zpqQRLBc4NB0ZM6RFpteS1h1HEkYCrcgLah0vCK0odBU5fBQq78761N3yQY7V9W3xii2Z8fMwl6O+0vyS1i5ZO3mCmSgRsMHJuIToeQaeQ+nkbOCaLg43hb6vmgKDB+2UikBjmh2Yy7EA5UmhzKNSfMmc8uxukXrovHA2wcWMS03/KWsv5wNUD4cO7JQK5r6q0JYwA86BsLLr1ZnEQTLWgL8llRM43seZLK3C8hD/33+bHwWTNYs69gCX1Ubn/1mvzl7gVswTM6x3X+U5yN9P/3iVKmcHQro+Njd4jUneDyx71OC8ZSNA1sz1eHzMpCyimRbmVWeATzPEAWuDxZXZSceHvUsl1dSd96KoMm8Pi8cvc/f49VTJH7BSukATC9idUjxOcvx3SiKpAT1dNY8GRaW5M7nHMzpJXib6p0qXs8l91cCzaloKj7nY5bRkSOKMuL+4f9Q/q5WW3KEm24dG1yjy6NGvBeIIIlN9wzqNCqXpcy37Xbq5GwkP/uOVNuXZcuDdJHMDt/YpMxaJ68nHV2q64cCQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(36860700016)(23010399003)(1800799024)(82310400026)(376014)(7416014)(10067099003)(56012099006)(261028144727099003)(261026121527099003)(261026140227099003)(261102123227099003)(261021123027099003)(261021121427099003)(261022115727099003)(261022075727099003)(261022105027099003)(261009223027099003)(6133799003)(22082099003)(18002099003)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0gLF4WBq6Mw+g5JJkWl19RQjZyf7A/zMhf8XXkDYzKKSaHXWn6vtGF61OylgBg+DsDnCFvdFyZ6Ihupp+QnkIl+nWH3bdgdO3KpDyk6/GCk7+zEYx+FAz6Of2RP8dpAZ6WD4IjF1zjEJih95mwNegT9QqFwJEhYH10KQqT798YSc6csiZ8td6MO6XvaNGu72kWyChKElTeDWnjzYYI8QxczJvkdpyc7nlFtP18NDU89leXIs+QbxsXX1uqhMLl6+vDMAzo6lt1d9bPckLwUT54RWNm5QcUbDTVGKbO8aHw9NlBIIFnCu+H2I7SZVtzdOB2MFsHw/UtyVoslovgRHhiBapRAg9NBdT5Dgh89xq/8jv9y6neEMbbso8uVwf/ttoI+T78B+nD+D/Grgxx9cMw3bRIV/v6u+/Zt8JpAeY7dVh4JqxT5u1oyhyJngTNUZ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:35:29.4085 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1505ddb4-fc62-406a-3f35-08df4e691245 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY7PEPF000000F5.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB9522 The retpolies that BPF JIT uses are already aware of shadow stack handling. Emit a incsspq %ecx for the trampoline that return to parent directly when a function return hook is active and BPF runs the function body as part of the trampoline. For the unwinding case, use incsspq %rcx again with %rcx set to (unwinding depth + 1) when transferring the control back to a caller in chain for do_exception callback. The "+ 1" is for the extra frame that call to "bpf_exception_cb" adds. Since unwinding and call to bpf_exception_cb is done from the bpf core, use the 4th argument to pass the unwinding depth. Signed-off-by: K Prateek Nayak --- arch/x86/net/bpf_jit_comp.c | 18 +++++++++++++++++- kernel/bpf/helpers.c | 3 ++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 6bca87457e87..9d6be7de7521 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -560,6 +560,12 @@ static void emit_prologue(u8 **pprog, u8 *ip, u32 stack_depth, bool ebpf_from_cb } /* Exception callback receives FP as third parameter */ if (is_exception_cb) { + /* + * The number of slots to unwind to reach the + * return address is in the fourth parameter. + */ + if (cpu_feature_enabled(X86_FEATURE_KERN_SHSTK)) + EMIT5(0xF3, 0x48, 0x0F, 0xAE, 0xE9); /* incsspq rcx */ EMIT3(0x48, 0x89, 0xF4); /* mov rsp, rsi */ EMIT3(0x48, 0x89, 0xD5); /* mov rbp, rdx */ /* The main frame must have exception_boundary as true, so we @@ -1530,7 +1536,7 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs) /* jump over faulting load and clear dest register */ if (reg != DONT_CLEAR) *(unsigned long *)((void *)regs + reg) = 0; - regs->ip += insn_len; + regs_set_return_ip(regs, regs->ip + insn_len); return true; } @@ -3756,6 +3762,16 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im if (flags & BPF_TRAMP_F_SKIP_FRAME) { /* skip our return address and return to parent */ EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */ + + /* + * Drop the same return address from the shadow stack. The + * traced function is returning here, so every caller-saved + * register except RAX is dead. + */ + if (cpu_feature_enabled(X86_FEATURE_KERN_SHSTK)) { + EMIT1_off32(0xB9, 1); /* mov ecx, 1 */ + EMIT5(0xF3, 0x48, 0x0F, 0xAE, 0xE9); /* incsspq rcx */ + } } emit_return(&prog, image + (prog - (u8 *)rw_image)); /* Make sure the trampoline generation logic doesn't overflow */ diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 712dca5a2c5b..4d1cdd507563 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3402,7 +3402,8 @@ __bpf_kfunc void bpf_throw(u64 cookie) * architectures cannot recover sp while unwinding, so fall back to bp. */ kasan_unpoison_task_stack_below((void *)(long)(ctx.sp ?: ctx.bp)); - ctx.aux->bpf_exception_cb(cookie, ctx.sp + ctx.aux->stack_arg_sp_adjust, ctx.bp, 0, 0); + ctx.aux->bpf_exception_cb(cookie, ctx.sp + ctx.aux->stack_arg_sp_adjust, + ctx.bp, ctx.cnt + 1, 0); WARN(1, "A call to BPF exception callback should never return\n"); } -- 2.43.0