From patchwork Tue Dec 17 19:58:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13912450 X-Patchwork-Delegate: bpf@iogearbox.net Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2057.outbound.protection.outlook.com [40.107.237.57]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BB1C1F9426; Tue, 17 Dec 2024 19:58:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734465503; cv=fail; b=TzReEBq/ncfKrasT2OkI0xMohObb8LH/32IbPzODJNioRSLlv5B9Ne2cYPZSTgz5Fa3LVlbhai6+xmrdRsXHc0yJzNplXGPsOuMU4yGxmcz6s3tgmyZuhYrS8g/vYtqeMX4gGXaklZyC93DIQuuQlJ63+NAr8Vt+U9W8Ktr5TXI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734465503; c=relaxed/simple; bh=0izDew6ti5tPIlQv4ts6nGWZbxOC4weqK46vYeDqkds=; h=From:To:Cc:Subject:Date:Message-ID:Content-Type:MIME-Version; b=t8tVUg0Em3eLrUUOx7aRmA9lnaNNLwuBg3OlXHdcchJZskzSEcCi1wCVOHJ86/h4zn3jyLPy6euFv/7rnj/m3RLOr8EG5PJst49prvkML2iwz8CyM+EphzWTKqdMT+R2b077opF2hsOpZMs9qM18fCLozYf4YfNhoUkLB9h4TIg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Uz8JpVkf; arc=fail smtp.client-ip=40.107.237.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Uz8JpVkf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YM5rUXdz/7U0TmaNilaA6PQD+fnzoOCqHRJE/4BSpvLqlKy0yAdbAOCfHZ553IHyU7RSywOluuA2Ysm7MJgbB2wryErDO4As6phl/CJSQ7bWjNBOqBprGYpaA7J13xH76PJWAEcClww5u+SXPYhNN2KDZVuWY1CIpPW29vPX3rSiYg94Csq1P6itR2xKxCWCgK5azvQ+lS8e2vfVcVbFwfsteO+4/txPEoc1lDLMiNcOXoUF2huUG2pylp/niEcjtP9CutxRdoyPPLo0jfxfgI4BkOq0ZycgCXR0PA1UDOYm9h4EIVBV0QYiIABcy28RLIAXW1XzC9AFbKnje0zArg== 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=z7RhkGY6zX7SLEDl24TSzNjhSHcBnCQMKtXciqnEEE0=; b=xoZ6l8NhfjDHA8/X95g1ac0Y/uKUtkWEf1vvBENL36PzJm9saJAz/ffU86v040ZMF/HJJHH0ccnO3EGdTgGVNrHlREaYal5l6JZZtxtTF3DLqKydVrfwduWjQAzlI2vu4a3hv+SzwHKunV7h5acYHGacBOUZyRBxzy6pSh/ljTOv0rmcBGwIrLkQFqF1RigNKcrITgnbXwckCvWhr88kOiDUdCrv86Wo0OnsXD9UyN4ybN53a+MwGct3auT6f7e/MgxHkDUGzXvB+sEbN/u7b5+eM9cKFRQg1X7W1b/zZf/3KKm48s3Mjy868porGC8/Dw+z0X2ZuuUnB9GoNTsbKA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=z7RhkGY6zX7SLEDl24TSzNjhSHcBnCQMKtXciqnEEE0=; b=Uz8JpVkfuy2JyIUNlPO8rEMnviTBUYKj9zyRtVH9hzJEatIb9Y3sJ6KIv5704eVF30/41KKJhv9xIOmAh5ZP63ZKBD3Gz6ugEvQ2sITTGjz78Stb0bL4z8TvBnmS2isnWWlqNygnT2IlZqO1Qqru6CiSz4SAFWeROpSKu9FGLztwe7pSh9YzKVykLQWdQdB0t5bybqIM644utEDSUjsWp4wIBmx4P33E75iT61P9P207pmPESAda6iQqBUBs/gmyY3FvlT2n0TI2lZmg1Pph2MRY32V324Vo4j1DKYd9EVQo+TBzaiUAO2hF4NrQc2BO2CvQXOqXMlVlKKeztPDrWw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CY5PR12MB6405.namprd12.prod.outlook.com (2603:10b6:930:3e::17) by PH0PR12MB8008.namprd12.prod.outlook.com (2603:10b6:510:26f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.20; Tue, 17 Dec 2024 19:58:18 +0000 Received: from CY5PR12MB6405.namprd12.prod.outlook.com ([fe80::2119:c96c:b455:53b5]) by CY5PR12MB6405.namprd12.prod.outlook.com ([fe80::2119:c96c:b455:53b5%3]) with mapi id 15.20.8251.015; Tue, 17 Dec 2024 19:58:17 +0000 From: Andrea Righi To: Alexei Starovoitov , Daniel Borkmann Cc: John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Andrii Nakryiko Subject: [PATCH v2] bpf: Fix bpf_get_smp_processor_id() on !CONFIG_SMP Date: Tue, 17 Dec 2024 20:58:13 +0100 Message-ID: <20241217195813.622568-1-arighi@nvidia.com> X-Mailer: git-send-email 2.47.1 X-ClientProxiedBy: FR0P281CA0151.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:b3::19) To CY5PR12MB6405.namprd12.prod.outlook.com (2603:10b6:930:3e::17) Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY5PR12MB6405:EE_|PH0PR12MB8008:EE_ X-MS-Office365-Filtering-Correlation-Id: a50a3303-efad-407d-c1ab-08dd1ed52659 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: gterSP/pE7n14Zz6QxCQaegfTMYB3Bq/4rpVQEQYZ/if21NFknbAYwsFEsMTjg5D/zXC4g7e0IeRQsCYsnEGYlLXLdHr+lxZ1ixZiMaOFaOGfFCGztbUeReGjPLvIuEvIiZ79cfgKjKH4PDlb+Hbze1nHl9iyjZDdjCbz5x2B11ZtubZ5C2jXKay3yyqqyiumjcZ6bBRRGI80su9U+w27oVvy0SZX1SIcoyxlhME4ThDcUSV+xQdsllYOPOuqcJqY0VnOVEuP0NjUncu8HGUJ0K7g+zcHlAWJQjreThYIEL4nC+XYScEywQcda8a4GSPwBa9VpPvNbFk6DlPgu7qP9ER0kPlGyuNmZDquSkXeRPBaEO8wN/I0ehfSIAVW3z1D3Fro/kadGtRFzb76osCb65EcNuJi5NAfiw76cARvhaepvP7woT1t6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E;SFS:(13230040)(376014)(7416014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8bhwcvCGFAtqvbQzolzEzW72jLcNZj+7S7CTra2BRws2C34/qR8sijM6c63WpF1yfaffaA3HemGXeemW3iDI28swuwElg94vGf0Mpvtx//EBjYceEoUWNcLl+sHJ96VD5a4BQPDcPYa+2ZLx0+vv7ZkAdaaTh3TSZW009i4pXN+Zr+uAQNb0tREcspZaCiAKU5sTSCBDa36l4WZ2XCCCYLImxZMOsAl2lQN8W56pRkf/9PlDoZmgrM7FwCggzZhET42QWCqQfqbctSLpg/RKLEDSsl+opRS6BLfBsDABmUOGg8qp9TqwLlDNLmP8jV1H2pFVL6IIJM+1A8P3XNA335HEIzxAKLnl6dDlDqfBuPLhrZnQL5LsSe9IxM4uhCtIvX3lr4GOWwkB51J0tIccbMvMBReu+YGEPCJ6T9ViLb/2/5b39A09j/cUmpxNmzVmCrJLweZ1UcvDR8Yrq2jzA0OZ3lUQYGZB+6Rc4rgRUKKaQuKUNXkPrQDPFUDfkiCj6MiWbBREHxbu4VHqgw7lfgfUdy4FYwC7xIZvnoI+FRPyXZJuBieJbARDwOsvVj3tpSe8sBl5M8E3BMBiwtHedd4r3zuEijuiXhCL7NaJPZg6uGFhC3dW29pjVsFuw4VayosS/5tj4Gz9fZEa2N0+g7b0Z79S5k0iar/rieWQXx6hwKr7PUGxIV/VaioNktYMRauTjh5hXFSF/08lkU6c2+2UaPik9fkxj3kzD0MhqXu63Na/Sl0Do4Le5a5soKKwVCaWFRlbSTnYyTaLJBmtVye6wQyLb2KR87uKsMvsFjZcuZk6WrL3CgT0jeHWDFlfhlm+LpGx/NOLHDdF2Nw97PRBoG0R9dRBcgMnlc01Us3A+aRS8yEtcGBkFQc3Ugvs5aqG7OD9k/0uLL1QKHXUyH2wTo1YDaq9MvezNmpUCXQwt8VbvL9Q7827f64SoUmlSiX6rZ0S1Wd5/wvGXB01B/bJkRaDGNPqvoWESxlsS1CRcmFf8MpHRB9KmVeu4FlA5mDVqtRcCCRQp9Ocwt0TcTDyHp8ci8y1WPon9tGwGez+lU2SUhrg68QGvHUhugNxp6LxTmkaIJXRHDv3eEE+EzUgDox5VRYlyeCglg1+F5Iyd4MAncYWfGj1/3UpBD4R47r7iSeDCbhPkkQVg/2jqYyODoNJD4XoClBXoMJ2x0UkNBM36IOGRvmI6Rz6Viylbbwa4NwqqE99pKaNCabIO0yreaMTF5MiAUlsA2jL/dYVm2Tr7kqrQKFLcwXRM33dBMaxgRNfPLId9gboAhxBQbdBC8I4p8zgZNmacxurB5giS72yNsDmzeI9fWQG0bOzMa3eIB2AUJv0t5vSKwwfeaL6oPtB2tIKg/thUMpPOBVwJucDyE15NhjnZ1K9KrmAw1hGjT52cKTs67e62cwPRQOdxE3kbMaKNSzw4KIutT4oeZkHIWX3hpeU13e51+whRnWzVJYdImOtQ6JO2U3iWJZU/gaafAg+TioBVGc7A6HKM2QzvE/mWdlprz8sV+nuqn9GfLstg3GtBKKjZM6PoB1iGnN+ZZtPluUcDRZzzAErz3LYyh5qIIqBd1mufh2I X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a50a3303-efad-407d-c1ab-08dd1ed52659 X-MS-Exchange-CrossTenant-AuthSource: CY5PR12MB6405.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2024 19:58:17.8996 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 3YXu9f3PDSgiwKU51X9G45PJDW21CpsPG9zrGsR/U4e5ittwL/QyEe4xevkt91AMZZK+bet8cXhpdB8Yq9erzg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB8008 X-Patchwork-Delegate: bpf@iogearbox.net On x86-64 calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable: [ 8.471774] BUG: unable to handle page fault for address: 00000000936a290c [ 8.471849] #PF: supervisor read access in kernel mode [ 8.471881] #PF: error_code(0x0000) - not-present page Fix by inlining a return 0 in the !CONFIG_SMP case. Fixes: 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper") Cc: Daniel Borkmann Cc: Andrii Nakryiko Signed-off-by: Andrea Righi Acked-by: Daniel Borkmann --- kernel/bpf/verifier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) ChangeLog v1 -> v2: - generate the proper inline code for the !CONFIG_SMP case (this time tested with the right .config) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f7f892a52a37..77f56674aaa9 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -21281,11 +21281,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env) * changed in some incompatible and hard to support * way, it's fine to back out this inlining logic */ +#ifdef CONFIG_SMP insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number); insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0); insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0); cnt = 3; - +#else + insn_buf[0] = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0); + cnt = 1; +#endif new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); if (!new_prog) return -ENOMEM;