From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 D166C343D67 for ; Thu, 4 Dec 2025 13:15:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764854138; cv=none; b=rLz8kf99+a3yQnKoCzYv4r39/RurHDJYs6I1Ktf7LcVeXljW956xkMgvpZ5/yONQ8szsYb7YlDeEliG7YFIKc/zwUsMdYTm1k55sWElBW7RG9ZZMX0mt5Fed/bpTgvtCe4y/5YTOKjQY4bCKVAzMSZy3tIPIfBpv5lyfM6l7IE0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764854138; c=relaxed/simple; bh=Wny4M191Yr6Td48Z53labtwrelyi+1dAudOVsu2AK4M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b5X21ABYGYYB2rPI/pfybzrkIDuOBgkomxnB+qYxu8NeYFBWjZg9pcEB/B64KfNiPHQS6Lv3ptfv+DVFSjo7G81FzJHZOHfGhoW2R+HTJIBGkvfIdCKf4ytk0MweXom2YDUYEWBDUGt8mKDeZpssNxSHkQIGi9cmTa7g6ZY5Qms= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eEfcJc7r; arc=none smtp.client-ip=209.85.128.74 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=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eEfcJc7r" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-477cabba65dso5528855e9.2 for ; Thu, 04 Dec 2025 05:15:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764854135; x=1765458935; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=Wny4M191Yr6Td48Z53labtwrelyi+1dAudOVsu2AK4M=; b=eEfcJc7rhw0cV4AVNryfwcBVWVzGB9WlKaupTm7Da2GPKv/F15Drx6dff9i7hmhXux 91ywzr7ehL8Jn8Cut8F//aFCM80hZOtldydSRH4RAqYCnGEinGYUCxy8JUIqMrXsoZlW BmIdCP9sV61gj9hEiL+kVcqoRbMxuM7+ZgYs3O7Xh37HoRrDiIV1wmR0BCWgRI+008zJ w0zBqPuyuZV6kQiL9aHMLGG3vtrKBmqZlKEJKruCOG0Uo1AcFdjC/i1fb5zUZ2rzfthb n3yegWPw/vgiNu1jFIejjQreRJlLUrkazHluobEuyp8VEpe2L2LXEpwuK+6A1DRuq7z0 EJjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764854135; x=1765458935; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=Wny4M191Yr6Td48Z53labtwrelyi+1dAudOVsu2AK4M=; b=ekArWFdTmjHz5731eb48SxFNg1j1V86VNasYPEfFTuT42C9/6DHZ+zK5iiFhRwzsfv kDSFlmbLs2FlG5l/S8GoXgcmCIg1C8cUy8fqq2wQ6/aXhQQrFbcXmmYgCmdiK3jKMgAx /6ItKkGRU1g7aTTRdmg1PX6wct+SOirR93Ose7EsT3z39GStyygjCog6XyNm3iSSODG4 JB2KxGzNZGV9kXfaiUkuvB8E8NIrvfF1tJB3/1X2D7RGe/JmDs3iqq/yyvVH4DhrOAeM 40/+2JISJF6vlfjBkWMwi1CtTBkXt2c4mcG8AmxB8us1kESeGjI1w/E6YdD6n7OEMRJq iX+A== X-Forwarded-Encrypted: i=1; AJvYcCVnFkmWmD3qsFJoY4WovMvpo3c+gU4km+Ql46KxLBU6z6KXd2VIshoA0o1ALXyOHJGwb1YClSIhKYOsu7ToGw==@vger.kernel.org X-Gm-Message-State: AOJu0YznCzfcuSA9pfcchBus6LVh0gt8YWAnUBDqKuqYKzGlwmKM1VSm e1Us+ByEgeRTCkETak+A06iuv0fjNcc2JTt9seJUwcT/ZTZBJJWpgMEAPxlB6RCUQGbuJidlkae nIalOxRIxG0XmRi7LYA== X-Google-Smtp-Source: AGHT+IHhq1jpZSgsrG7+u70UeMI118GpL4lyrcZgSJfzAV0BpquOo2d1tIrYUXW40rz39aPBfrTMkT06vKIZZ8M= X-Received: from wmbjj12.prod.google.com ([2002:a05:600c:6a0c:b0:470:fd92:351d]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4e89:b0:477:55c9:c3ea with SMTP id 5b1f17b1804b1-4792af485a4mr64437335e9.35.1764854135213; Thu, 04 Dec 2025 05:15:35 -0800 (PST) Date: Thu, 4 Dec 2025 13:15:34 +0000 In-Reply-To: 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> Message-ID: Subject: Re: [PATCH 4/4] build: rust: provide an option to inline C helpers into Rust From: Alice Ryhl To: "Emilio Cobos =?utf-8?Q?=C3=81lvarez?=" Cc: Miguel Ojeda , Peter Zijlstra , Antoni Boucher , Arthur Cohen , Gary Guo , Josh Triplett , Miguel Ojeda , Boqun Feng , "=?utf-8?B?QmrDtnJu?= 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Thu, Dec 04, 2025 at 01:49:28PM +0100, Emilio Cobos =C3=81lvarez wrote: > On 12/4/25 12:57 PM, Miguel Ojeda wrote: > > On Thu, Dec 4, 2025 at 12:11=E2=80=AFPM Peter Zijlstra wrote: > > >=20 > > > Right. Earlier I also proposed using libclang to parse the C header a= nd > > > inject that. This might be a little simpler, in that.. > >=20 > > Yeah, that would be closer to the `bindgen` route in that `libclang` > > gets already involved. >=20 > Yeah, so... there are existing tools (c2rust [0] being the actively > maintained one IIUC) that in theory could do something like that (transla= te > the bodies of the functions from C to Rust so that rustc could consume th= em > directly rather than via LLVM LTO). >=20 > 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. >=20 > 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. >=20 > 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, whic= h > is a bit unfortunate... But this approach seems sensible to me, for now a= t > least. >=20 > FWIW Bindgen recently gained an option to generate inline functions [1], > which could help avoid at least the bindgen ifdef in the patch series? >=20 > Anyways, it might be interesting to give c2rust a go on the kernel helper= s > 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 buil= t > 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. Alice