From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 54AF831B107; Thu, 4 Dec 2025 14:27:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764858477; cv=none; b=FUhyu8QHRcS5Qnc2b1RNIi/nXhQuIGwRvB0EtRF/NF7/NZ/uHcVYxoVe7Hcd8p6lNEgWn/c5zk4EhWdzT8FozeGB8k5KSc2+K74p2rifY0W7wdXhI1uXeVVTfs1xxZh5+8rNzKhopizEV9aIjZhT3wBDSDMOvBsMMw/Z4h8vEGE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764858477; c=relaxed/simple; bh=xtdePjrSkMnCMeffdJhgcTAykP6ckhRuVfR6k+uF+lE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=msch6Kzl/hPkOecFnlxLQxpHO3EYLfLv88Hyex1N3w+zlD/QDf0J+OuIJUicBBG1CPmHxMzNHKxWdtPhkUgNbUyOMTH2Ou0Wt41xBD+0DXJiMzs/STZQpC4JpPjI2Fl4E5S3dqGH5Vyg7oM3nCRn+IsmQJ8YHSS6kaYN0GfL3DQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=YwrF7ndD; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="YwrF7ndD" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=fFnv22fcQB5aFcCjIbe5QaQi5Ay10FFsnk0wQwnxL/U=; b=YwrF7ndD6dZYtSnBju5djLf3B3 9BwkHu8OrlzXfyMe9mncsj4GToO/8tqG0JtcRmUtUn95c3+ke4l7EthXBRpW1hDCA4txDm0KfBxlr uac/gVsFQHXWTT+0ldDi39DQYGDL7a09ckPOmCBRint6yxH5rrJeIDc88cwxVabg2dXtL8kn2nMt+ vgvICwVvQw2tHBO07bBT954nsePfsujxuOldWJA2g2bIZA7a2me0JT2k5VO8kTeZWtLVle+Ebt+MS Q7bCGN4x5drE0np+zjvUTukF6CuC4mr5TlM7VO2qJC14ZL3jBLYAn5c9sIqH8WtCmYeUUz4Lh9uCL XJbsVcSw==; Received: from 2001-1c00-8d85-5700-266e-96ff-fe07-7dcc.cable.dynamic.v6.ziggo.nl ([2001:1c00:8d85:5700:266e:96ff:fe07:7dcc] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1vRAJ6-00000004IZt-18oA; Thu, 04 Dec 2025 14:27:44 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id EF8FC3030B1; Thu, 04 Dec 2025 15:27:42 +0100 (CET) Date: Thu, 4 Dec 2025 15:27:42 +0100 From: Peter Zijlstra To: Alice Ryhl Cc: Emilio Cobos =?iso-8859-1?Q?=C1lvarez?= , Miguel Ojeda , Antoni Boucher , Arthur Cohen , Gary Guo , Josh Triplett , Miguel Ojeda , Boqun Feng , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Alexandre Courbot , Will Deacon , Mark Rutland , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Nicolas Schier , Andrew Morton , Uladzislau Rezki , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, linux-kbuild@vger.kernel.org, linux-mm@kvack.org, nouveau@lists.freedesktop.org, Matthew Maurer Subject: Re: [PATCH 4/4] build: rust: provide an option to inline C helpers into Rust Message-ID: <20251204142742.GM2528459@noisy.programming.kicks-ass.net> References: <20251202-inline-helpers-v1-0-879dae33a66a@google.com> <20251202-inline-helpers-v1-4-879dae33a66a@google.com> <20251204100725.GF2528459@noisy.programming.kicks-ass.net> <20251204111124.GJ2528459@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, Dec 04, 2025 at 01:15:34PM +0000, Alice Ryhl wrote: > On Thu, Dec 04, 2025 at 01:49:28PM +0100, Emilio Cobos Álvarez wrote: > > On 12/4/25 12:57 PM, Miguel Ojeda wrote: > > > On Thu, Dec 4, 2025 at 12:11 PM Peter Zijlstra wrote: > > > > > > > > Right. Earlier I also proposed using libclang to parse the C header and > > > > inject that. This might be a little simpler, in that.. > > > > > > Yeah, that would be closer to the `bindgen` route in that `libclang` > > > gets already involved. > > > > Yeah, so... there are existing tools (c2rust [0] being the actively > > maintained one IIUC) that in theory could do something like that (translate > > the bodies of the functions from C to Rust so that rustc could consume them > > directly rather than via LLVM LTO). > > > > I think the intended use case is more "translate a whole C project into > > rust", but it could be interesting to test how well / poorly it performs > > with the kernel helpers / with a single header translated to Rust. > > > > I personally haven't tried it because for work I need to deal with C++, > > which means that automatic translation to Rust is a lot harder / probably > > impossible in general. So for Firefox we end up relying on bindgen + > > cross-language LTO for this kind of thing, and it works well for us. > > > > If I'm understanding correctly, it seems the kernel needs this extra bit of > > help (__always_inline) to push LLVM to inline C functions into rust, which > > is a bit unfortunate... But this approach seems sensible to me, for now at > > least. > > > > FWIW Bindgen recently gained an option to generate inline functions [1], > > which could help avoid at least the bindgen ifdef in the patch series? > > > > Anyways, it might be interesting to give c2rust a go on the kernel helpers > > if nobody has done so, and see how well / poorly it works in practice? Of > > course probably introducing a new dependency would be kind of a pain, but > > could be a good data point for pushing into adding something like it built > > into rustc... > > I already tried c2rust as an alternative to this patch. It works okay > for many functions, but it's missing support for some features such as > asm goto, though this is fixable. But a larger issue is that some things > simply do not translate to Rust right now. For example: > > * Atomics use the Ir operand. > * static_branch uses the i operand. > > neither of which translate directly to Rust. Right this. AFAIK Rust simply does not have feature parity with inline asm. Them having picked a wildly different syntax for inline asm didn't help either of course. But Rust is Rust, must have terrible syntax :-)