From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9387346E58 for ; Thu, 4 Dec 2025 19:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764876570; cv=none; b=giZ6zp96gx4yACuHvjWuarGu0TWLF2StVOh/z8qRM3OplFINC3pA8p6Ti59hoXOPeDmS3d62Ab9D4ehvRHJfQseP9wtxrYOPAYgMAhVNlP+8sp2IjYDngzDTfo4KB/F2HW06fhZufQUX5Z3nslvxhQr1ROLTQuopgL6fIuYG39A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764876570; c=relaxed/simple; bh=li2jOPCpnZmjfOxkVB/u3Ntjt/9n1Mv+93gAuK1nZY4=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=J22RMfoF0HqHz6HntB+yo6gzLZHd2jLV4la5Yz89estdnMGXSTQFNvBxNmN8nqRl7R9lLF+u9t+Ad/gHHwlol6qBc2VaOWxphOMiAz7q2tkjWzpR136JES6SO26jBvggL68vNbZYEUhj4zQL+vWF8/V2RkXmaA99NR/Cp1yUSFQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WXGNBUCd; arc=none smtp.client-ip=209.85.160.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WXGNBUCd" Received: by mail-qt1-f181.google.com with SMTP id d75a77b69052e-4edb8d6e98aso83661cf.0 for ; Thu, 04 Dec 2025 11:29:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764876567; x=1765481367; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=li2jOPCpnZmjfOxkVB/u3Ntjt/9n1Mv+93gAuK1nZY4=; b=WXGNBUCdN8gqregeMOTpujv/6QXlb0lJ6tHZ1+HNhTOEH9sFdroEm7PO3Eykw7HNVP k22F/0sqTFDjY8gE5w8otLiN1QsI4ttaGg02N7TKqEXLX9qvwzD6kiHfRd/dYlZwbdiG QaC9Eg1uaFdwc8JalYstdEVN4U+eQDESjqdloTYHfnMjpsrYaya1IuFtOKlRPysPKqmX rut6fGao9zwzlgpBoY3lmQtkruvUFJgkmzsFFSTcMzMbq3bjnwv7nrEa+8HWt05AZbMm amtTpOPFI38MYwd7rf7HpRHQWhReqFHtdceLU2pkitcJxy5M5f7v5tqwTRq7XAmBIjpN CcnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764876567; x=1765481367; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=li2jOPCpnZmjfOxkVB/u3Ntjt/9n1Mv+93gAuK1nZY4=; b=UpXXpIMkdKMPwveNvRZP9EK6Xm9d2rOmKvU5s/n4EewaF5BXPUc08rx596nchRMUW5 QaJL+2VZlbHKFalI1NW0AjWZIQuh69QRpvhKingBiE+OZcwKWZ+WbmahL8DLhjcHLVdZ Z+zhFtCr+WiqrHkKmIb8xFScNmp4O6avVwKbKugIZh6prWXJDkSinpcJG+3RaJrTsWaB QUaXq03ACNFCYrrlDarXC3nXCBiJBwYQK/Co2BQRIybDj9jWieIfvxUGUrXAHmV0aZrm EuuOyiC2B9/WIsRnGmOcZSOZ3UMzOCwgvMCwVNv6YTWjwcyypkd/TaD0N+308Nxtdv5P nfCA== X-Forwarded-Encrypted: i=1; AJvYcCUNy+VOGxaVKxFI5ZnFx9k8Qz3z764RA3EHUZxZR4T4+t5Y/XWdR62g/jORhrkKjKrcS6+opqxa+102J87XRA==@vger.kernel.org X-Gm-Message-State: AOJu0Yy2YcNkhnoKl/YMY3ZHvYXzrSG4viZIL45G963fWMrA3w1pXtNj DqxsIm4apvgyAznZYQj6/43TpHQTKb3mywV4GkQSbQnHwykkQm1K2TYqb1BczhXnNMAXoUw4w+j VgdhWHHpBE3M+3lwznuv0IRe4/5vCKCK1fsDqjgaW X-Gm-Gg: ASbGncuVawQ1S5q0QCMpdsHkHWsQj3vTM7Xbo28qi49SUYQwqQJD3WC6ZIzSPCbjLAI ySD7ECfAmvs2DisVWlrCbqE6VXEaIu04HMaQ3E5h4/NUGk9McyYZd1ztxAKOHqPockiyk+uTr+S cWrSSt0xDZjACzIfW4XMX7pr9vlG4i/1Io1fUzB1TuoT2V+bVI1v0InJZB7qNK8lNcFYd/PEVz2 xX+e1jX7oz6U5WfbpNeHftcJoCgXpEV4VOmaTOan3NL1gS3Xkwx44YEXmvYCFLX9Fz6EgJDk1XU VXMqHMhm+63hFtbqWfrOD1/pKQ== X-Google-Smtp-Source: AGHT+IG/7kUZxd/9UMd9RxOWK7oRJw6LsWkyhbSwzHRq9dv3XGK3gE0ZwwYu8Q59dnHZWlQ+QWACJNmSaDdEdt4GFj0= X-Received: by 2002:ac8:7d91:0:b0:4ed:ff77:1a87 with SMTP id d75a77b69052e-4f0310036bbmr1019101cf.19.1764876567093; Thu, 04 Dec 2025 11:29:27 -0800 (PST) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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> <20251204142742.GM2528459@noisy.programming.kicks-ass.net> In-Reply-To: <20251204142742.GM2528459@noisy.programming.kicks-ass.net> From: Matthew Maurer Date: Thu, 4 Dec 2025 11:29:13 -0800 X-Gm-Features: AWmQ_blF4lJt3BfO2o2NOJRf3_Ss0kAS-vrqLdfp0qFcfHcQVmwUbXFySwWbsjc Message-ID: Subject: Re: [PATCH 4/4] build: rust: provide an option to inline C helpers into Rust To: Peter Zijlstra Cc: Alice Ryhl , =?UTF-8?Q?Emilio_Cobos_=C3=81lvarez?= , Miguel Ojeda , Antoni Boucher , Arthur Cohen , Gary Guo , Josh Triplett , Miguel Ojeda , Boqun Feng , =?UTF-8?Q?Bj=C3=B6rn_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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Dec 4, 2025 at 6:27=E2=80=AFAM Peter Zijlstra wrote: > > 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 =C3=81lvarez wro= te: > > > On 12/4/25 12:57 PM, Miguel Ojeda wrote: > > > > On Thu, Dec 4, 2025 at 12:11=E2=80=AFPM Peter Zijlstra wrote: > > > > > > > > > > Right. Earlier I also proposed using libclang to parse the C head= er 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 (tra= nslate > > > the bodies of the functions from C to Rust so that rustc could consum= e them > > > directly rather than via LLVM LTO). > > > > > > I think the intended use case is more "translate a whole C project in= to > > > rust", but it could be interesting to test how well / poorly it perfo= rms > > > 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 / prob= ably > > > 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 n= ow 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 he= lpers > > > 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 thing= s > > 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. I took a shot at `c2rust` as well, the summary was that `c2rust` was missin= g: * Asm goto support * __builtin_types_compatible_p * Deduced types * __count * _Generic I was adding hacky implementations along the way to see if things could be made to work, but the %l and %i missing in Rust itself that Alice pointed out were not things that could really be worked around. I also think that the bitcode-based approach is more future proof - the kernel may pick up new C features that `c2rust` hasn't yet learned about. If `clang` is in charge of generating the bitcode for the to-be-inlined helpers, we don't risk any desync in terms of what features are supported either by `c2rust` or by `rustc` relative to what's in use by the rest of the kernel. > > 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 :-)