From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011033.outbound.protection.outlook.com [52.101.52.33]) (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 9B629F9E8; Sun, 21 Sep 2025 00:45:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.52.33 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758415524; cv=fail; b=Qo8ZM1Q03ymtunNltvtTOcDK9BDz6vCT/pqzhII87tVVqlHK69go8Mgasd3oAvscMUzm65C5m73tKUky0WJXQiha35Kr12kBKhTID1aBcrvRNf7CTba/bZnxjZTP6Et68+4zS/7s1v+m2uRrKia7yXfqPx34o3+lqVpEXdo48I0= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758415524; c=relaxed/simple; bh=A0GLNPkgjqC2ertUZN0kN9RRIInUGD+/J/OwyP+xJJw=; h=Date:From:To:Cc:Subject:Message-ID:References:Content-Type: Content-Disposition:In-Reply-To:MIME-Version; b=Dn5unDwZPhqSwQHaC80kIFEQX3kTCOdeQUHbQlE6U3CQcwOzcTJQcopiwe7sqGX7s6yuBAuAPeYovSF4uFl9pbEyPPeUeWPaI/dX7UwGLminFLU9bEwJT3OLxUbnrJSvt6NMn+K9yfCbFfv437jTRbmZgA7mResIdIY2s81/FPY= 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=rYCmiVGl; arc=fail smtp.client-ip=52.101.52.33 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="rYCmiVGl" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NcYW4NMRqGwhDthzmA1j4GE6QD67b33erONsatyIZ0vnfOsdI0tAA74NUt46LqZD6tLUhTEliUWBX/MLRcHN+ZJiJFiRj5PGkwlLf1CaQCtdwIAGs5OkEpqCGKuyXthBBVCXmA1iN75mpK0Q4VzBs5zDe1afXFyMXV+/oADIS/zgAOk0q49Aer9RsF+sbvRHP6Qr45JICxTUQ8p0gFM84hCxeGass2qAG/T/0dHum0qm0ZIjAqgpJYtlhI3VAnbkKNuxSmdWitde0EQZco61d17uGpY0OysONzpeXW5Vi271XKAsfejFrQ9Ok1tMZSPKtzq7vlOmVRGMCPspS8Ky5A== 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=nk9cCzxyE3mNYL0JI4tP1MINf0deRD8ySnu0NHQ4U0s=; b=lpBSVQIHgLAduGYshzrvjEYCfWZ7Jnf08aEqKFNAtMcDXZb3lRY/yjDiThqebfYlcBF09xLWhZlMPjRYkw21b16pX8gjsCH8EseG6YWUjvFHDwbYspxirQMI/QzoSwXB6mKeA6pY3WD2SMKFvlMbGLjs5QqOsDcs0SNSa4kZ4VXAdQpTM0lHDqbDzoQ7g/xmCbeRhDB+hYDCi+um/TGwGB0pXnVysK9czLuvsIv3YDs5/EiWENRVi3rtNYs1Jfuq7hm1oG7fu0si9Gn949Kk4HVJNPUkTTs2mxzt8kdEYQJ0KMdLvsXkSrOmGiK60F8ejBL4tjgrSUmDREUNKAQKsQ== 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=nk9cCzxyE3mNYL0JI4tP1MINf0deRD8ySnu0NHQ4U0s=; b=rYCmiVGl21HhQYGSiQqrGPt1AaZkTG+MVeOak0Z8o/wXC3iIaqWKrmxXwENt6Q2L+3HO9JjNeDT560f1+1FY4Pz1A7f0cQ6pl0bScClgKE90hdgallBHyugm0bVJTk6VKAEGtETRh5tR1ggD1cocuy7HI1VeJdUtn8vJf41XCJ0lo3Pyo7gUEVMvUekjFdc+2reIfyfcRad1cahKOq9OSvmilJW+pdHZVaavVCAFlvY3oZj0LOvUMHy6AbbTPzUJm/RraHHtTdlcNfFW9fkj2YqOF+PiJh2xZZIlBnHtzDCzjzW0a0tDU6dgOO7JkiJ4/wTACZA/E4KZId/kDLq6XQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) by DS4PR12MB9585.namprd12.prod.outlook.com (2603:10b6:8:27e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9137.16; Sun, 21 Sep 2025 00:45:19 +0000 Received: from SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91]) by SN7PR12MB8059.namprd12.prod.outlook.com ([fe80::4ee2:654e:1fe8:4b91%3]) with mapi id 15.20.9137.018; Sun, 21 Sep 2025 00:45:18 +0000 Date: Sat, 20 Sep 2025 20:45:17 -0400 From: Joel Fernandes To: Benno Lossin Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , acourbot@nvidia.com, Alistair Popple , Timur Tabi , rust-for-linux@vger.kernel.org Subject: Re: [PATCH] rust: print: Fix issue with rust_build_error Message-ID: <20250921004517.GA2101443@joelbox2> References: <20250920161958.2079105-1-joelagnelf@nvidia.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-ClientProxiedBy: BN9PR03CA0113.namprd03.prod.outlook.com (2603:10b6:408:fd::28) To SN7PR12MB8059.namprd12.prod.outlook.com (2603:10b6:806:32b::7) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN7PR12MB8059:EE_|DS4PR12MB9585:EE_ X-MS-Office365-Filtering-Correlation-Id: 299cb321-7597-4b95-e43f-08ddf8a8234a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?4Qi5CxciovMdJn1mrkowFa7/aM4BzfO0O2TzUOlm73XHv4SEm73hwrr2nQ+4?= =?us-ascii?Q?dlAqloIy51HJO6eoxtU+Q8L0uobPEzV3HXc7KfVXhl75zQv+7u2D3wG7xpVX?= =?us-ascii?Q?RmeJXrpp2lmopjuFVhplZNUaN/c6esjmRj4sRkQ2/NEL7BhiYrZ/+aCmixN9?= =?us-ascii?Q?RhdKauZv4llIDlyBMpKmAjjcjZbn+ugrQP7Uf8NLXPalL4SLNcSuGIjzrtJa?= =?us-ascii?Q?HbVvCeZoPFTUmb7dzsA5q2DQaFA11yAUyRt0td40UfCrSUZE7mSsrne20rT3?= =?us-ascii?Q?l45KO/F/HIlsl8vTVpdmn7fUNkrJFtoXQFRFGPo1TQy7FSJkavybefacaSjx?= =?us-ascii?Q?olwYdt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D0+E0seuVdeHGCciJ?= =?us-ascii?Q?L1h+Fjz76XzI0MdotbEEGDdSndwqrEYgtW+zZPGGP0EhWN3GMknNxgQs1y46?= =?us-ascii?Q?+tbaoEpsPvY2xnNb1NaecS5vKRFVJLaReDcxswBDt2yRvH2Q31xhjrlMAruD?= =?us-ascii?Q?Hr7UHkJDa0zt6eUr9Df5fYufD/XEcW4ovB7/fXF0zGZbjwl5riyS+6QYI0eP?= =?us-ascii?Q?v9942q3tQug=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN7PR12MB8059.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?xCxvHCCzK7uRMI0wV0gxSg574MnvXdn3Ca3nWQavadbSwvHplYKwhmqwWR4U?= =?us-ascii?Q?R8bWslo30bsYdFFiQU9qtQ0ClT9u5souhTKaUVSYe7kCypeDnn953lhMDjlW?= =?us-ascii?Q?xIHOUHHlPWmjmxkdWqNLTWHFor+/cMoMoicreFi0yFEuoOl4ImQWOiH9S3Mv?= =?us-ascii?Q?kd+nCJi2xX/xadBFHYTMcu6hUOjQH06XcPTEu88jfzeng8w2iqBaG3Iib2lP?= =?us-ascii?Q?KKve4cxs9DD8hHqM6LgYX/GEXgRoUTulLljnGpyGYGwRUp5vX9k8Sn4CzS0N?= =?us-ascii?Q?DJsbbuFnFvVWdj4t3S4tGSHQ6zR24HzAYJKsGxEc0zfEDySxMwzG6YsMfKJg?= =?us-ascii?Q?RqAVOmaUHeqq+Sayu8OoiPlfUTusdvWlYgLXyEk3m1W7romcUVdGHefsAPfw?= =?us-ascii?Q?C1sPg0nEleNELNhfB3O0vSZ1oaFJPdKzNMvqf2ewcic7BmVAgdPR9mDCuN7t?= =?us-ascii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mwXp?= =?us-ascii?Q?8Cl9YDROjAYa66iZSaEJPO6McMZpzgwIN9Vi6BZx1c+Uobnz7kYd6Lbr0O41?= =?us-ascii?Q?ey04ZyWV4pgi+4Hvp3KpZIQI6EqLT17p1EaJ8Fpz3iXh43UwO4+x8BteiIYZ?= =?us-ascii?Q?H9cgI+tDcqRQNGW/nDZo23AyqFoldyl+xb7oJOBsgTNYEO/QEvw8mDc3Y0e4?= =?us-ascii?Q?Nb8L73+diuMrxC+8cAim0QOH2cY6SgtxS5GG86zb?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 299cb321-7597-4b95-e43f-08ddf8a8234a X-MS-Exchange-CrossTenant-AuthSource: SN7PR12MB8059.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2025 00:45:18.8330 (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: QmA4hHe/koSyH72ZRr0E1JrU0rkpOPXc9kK5Vh5vLMmko127rJ61e7rmVdpLC11LRuOFXSRPoyFuGXJSyF9WEw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS4PR12MB9585 On Sat, Sep 20, 2025 at 10:09:30PM +0200, Benno Lossin wrote: > On Sat Sep 20, 2025 at 6:19 PM CEST, Joel Fernandes wrote: > > When printing just before calling io.write32(), modpost fails due to > > build_assert's missing rust_build_error symbol. The issue is that, the > > printk arguments are passed as reference in bindings code, thus Rust > > cannot trust its value and fails to optimize away the build_assert. > > > > The issue can be reproduced with the following simple snippet: > > let offset = 0; > > pr_err!("{}", offset); > > io.write32(base, offset); > > I took a long time to understand this and I think I got it now, but > maybe I'm still wrong: passing `offset` to `printk` via a reference > results in the compiler thinking that the value of `offset` might be > changed (even though its a shared reference I assume). For this reason > the `build_assert!` used by `io.write32` cannot be optimized away. Yes, that's correct and that's my understanding. I in fact also dumped the IR and see that the compiler is trying to reload the pointer to the offset. I feel this is a compiler bug, and for immutable variables, the compiler should not be reloading them even if they are passed to external code? Because if external code is modifying immutable variables, that is buggy anyway. > > Fix it by just using a closure to call printk. Rust captures the > > arguments into the closure's arguments thus breaking the dependency. > > This can be fixed by simply creating a variable alias for each variable > > however the closure is a simple and concise fix. > > I don't think this is the fix we want to have. Yeah its ugly, though a small workaround. IMO ideally the fix should be in the compiler. I want to send a proposal for this in fact. I looked at the MIR and I believe with my limited understanding, that the MIR should be issuing a copy before making a pointer of the immutable variable if pointers to immutable variables are being passed to external functions. If I were to send a proposal to the Rust language to start a discussion leading to a potential RFC stage, would you mind guiding me on doing so? > In fact it already > doesn't compile all of the existing code: Oh gosh, I should have run doctests. I just did a normal build. Let me see if I can fix this closure issue. > > error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`) > --> rust/doctests_kernel_generated.rs:3446:70 > | > 3446 | pr_info!("The frequency at index 0 is: {:?}\n", table.freq(index)?); > | -----------------------------------------------------------------^- > | | | > | | cannot use the `?` operator in a closure that returns `()` > | this function should return `Result` or `Option` to accept `?` > > (originating from `rust/kernel/cpufreq.rs:217`) > > Can't we just mark the pointer properly as read-only? I found no way yet to do that. If there is something you'd like me to try, let me know. But even if the pointer is a C const pointer, LLVM seems to always want to reload it. Thanks! - Joel > > --- > Cheers, > Benno > > > Another approach with using const-generics for the io.write32 API was > > investigated, but it cannot work with code that dynamically calculates > > the write offset. > > > > Disassembly of users of pr_err!() with/without patch shows identical > > code generation, thus the fix has no difference in the final binary. > > > > Signed-off-by: Joel Fernandes