Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1090394lqt; Fri, 7 Jun 2024 07:48:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWJtOHmr+ep7ZG2sBBgBldxfAXkeKq+N6scp2Gkj4J2zznbqw9tREao/XqDtO9CvEPBu4eZ3s7djwGE9Va/o56lS1QMVgqng8fLTsqJdg== X-Google-Smtp-Source: AGHT+IFLtLVCS60ugj/t9LmiW/2zYNLJ1LcC3ZSJm4zCMtoLJqbagVtvb7qyBRFyy2qBhpaixkLa X-Received: by 2002:a50:cddd:0:b0:57c:6463:9960 with SMTP id 4fb4d7f45d1cf-57c64639a49mr316041a12.37.1717771688171; Fri, 07 Jun 2024 07:48:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717771688; cv=pass; d=google.com; s=arc-20160816; b=MrSW+XtoZfXXpvXAKjLxUNGQCLkcA9R5eWpy1O7N8o549rTzph3447XkdrMjijz18q +KYd7HCDec49c6WtKEpxDNisfbMJvua50F5Gj1dWNpVX2BCaxLRGTzGwP7IlYd1MhGcH wz+AqO1iJcX9LNOC6qv9fyOMJ1weM8jVGCOqf0cC8SJt2X5J1trO08Lpft7gRM9ym2jb E4Ri3sK95A/kMWbkzSiLPqBwt0y3VkZWMMG2VhEEXIgJFbhg6rNAPx6o65Cw0JvYhQEr 9CNnN+0aHNd72vcmMoBIiNKF/N0PZdU/svncs1OuL1Qu0tiHwWHXuRX0rgqXhpqqZPHq kY3g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=kt9qUYYFSO6k5VmbzbT82tlK3CSHJaSCL9vF7bZshII=; fh=MP75tgeZA+X2duwief/Dmjmlz7nvGno0ZklTmn19Dhk=; b=1B+VqNihnNNM2EymBu/BJ2s+FwTavBoQLTUt4vJHjHXZyXeF/33e4JeG8a0zRtp7gF Fvs8S1/wPO4qI/q+7jRSNnAsuIKz2vXqv5aEyAxTg1B4uefs6zkTY7bxAnJqjh23oxad mk/kW5WcPQZs4xu2bEhwZFu1o73WDVsgVqh7WIf68/WZd0kmlpToXUcGxs7mQzTICT5T BuhG4wldP6u3JjnnVXia7qpszFs76cpTIl5AoWBikfSlNVb6lqqxU2qf8xqfxshG2kMG Md+q9Zq0iPsRy484fhPHkX3CYePT5KI0IqmI/MmVwVsFh+bwQ/VImhwGnvD05afmIRbH S5mQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ylopEWRW; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-206287-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206287-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57c60f1ee8fsi313350a12.174.2024.06.07.07.48.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 07:48:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206287-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ylopEWRW; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-206287-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206287-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B50071F22F42 for ; Fri, 7 Jun 2024 14:48:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B0C8A1A0DF6; Fri, 7 Jun 2024 14:41:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ylopEWRW" Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 682FA1A0AFF for ; Fri, 7 Jun 2024 14:41:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717771263; cv=none; b=CUrmRYhK/tIQtJxysY7ji0E7miKY/IUmjfXrEQ3YlC8PtW9zJ4GLd729g2UPf57WGLqMcHz/zZgZWoL2cNMf4akRu5IdfXeh02hqU/s0qZ6IXz9+aU6XjU4Mbs5JPDakqOftSm8jkZCiR2beZcDsWOJTnuxDKY0aFzl7phiweGw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717771263; c=relaxed/simple; bh=+8evMGg2n1u/8yOm9qRPDDeti+4TBuqjZRybbC+w58U=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=EEpc0BjS0YOfpVmnofUlBl5iJ2JkTUPaBa4+y9L+MLQLims5HexZ+l7OrJvCAq9BgIjTKMqMgxnq4nStm0kt22HmUslrm4VqB3naxWsJVZsetcm/OXStI7oeliQIERRqo8LTc8RM9BpzIrw1UnHdUBESKrgVjnF6qzLU9rhkMR8= 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=ylopEWRW; arc=none smtp.client-ip=209.85.208.41 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 Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-57c5ec83886so8271a12.1 for ; Fri, 07 Jun 2024 07:41:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717771260; x=1718376060; 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=kt9qUYYFSO6k5VmbzbT82tlK3CSHJaSCL9vF7bZshII=; b=ylopEWRWiYiIov7hPCSgQbzCFrqmJbVrFTgzRBQ2oRVhLGprUTYiLKKbIYu2LONx3p dOgNZwLwVrCg6V24skMVWOJwTVJlyIHxfdsSs3z9eHawzM2QG5lGRBC6ttidNTuEAAb7 wLvyVl3+PqdXqM7h/RJEuB29mNZghNXjrDr97Q8sYfnETt31yiepPcsfJ3jpOeyQ1L7e vlEXDb7/LMUalrF6LVQ13IB5xs+P+AD/WJfmPk6rYF1EKuKj18Blfg5TyItpG1tzBsyY 8QzcudJM6cQPk1jQZBR9DRsLl1/juAUf2abpCjattXfhJ+ziab0kR/ztYB+wBIzTQQ0K rNzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717771260; x=1718376060; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kt9qUYYFSO6k5VmbzbT82tlK3CSHJaSCL9vF7bZshII=; b=Uk88/Kol9F5xprqqWdJ36pi/Ocl5pR6d3R/xFQ/9sY+GxvNxEFDugvxmMt33FH6il9 7RwlQPDVuP1q6uuthIMEv0atxty/tijFbtaBuC6O6ZDCuf0tjQqqDuIerBZSS83Nls6x T0Vyzr+ZcjDNX78SLO+3KB1Hnjwi0Rpk6QtzlZwXnSdyv4LMWvUgPI7SNEsqe8p1RK5U qONwK7ayFA2If4gF5sybewFgBz+caXaIrba19E2raHiGLZFkp+stdLmhyNTeGWNihNSO pBtsg9Foq55PFS3ImLsAiGbPGHOKsxzOJlvxNTbW4s8xxUVo9rqj6iLr3OC2aEKv5So7 +ekQ== X-Forwarded-Encrypted: i=1; AJvYcCWPkw1CLxvAVn1o6UOA11K0KucaVMl0gs8kzQkEDaYXRAHihSPVb4W79dit/wWnoqu9TT0RS7ENL1tV6X9Ej7IEUXyA5ODPaOEHCyRm X-Gm-Message-State: AOJu0YyfqBGLWhtXKRN/90CAKUi3btjKG1F7hBC29SG9x4m3Kb4nLOFp YeKTuk04uDgGTjJhrwiruyvsgHghqizi8I8S2w+Eh6etdmncb5hccRSpsqHKNeMjPRNYqnMPvTG B0IjknvdH7dvkCm3Qvey0HUspgN1KOIGE+6hd X-Received: by 2002:aa7:c0c5:0:b0:57c:5ffb:9917 with SMTP id 4fb4d7f45d1cf-57c5ffb99b5mr114826a12.4.1717771257885; Fri, 07 Jun 2024 07:40:57 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240529202817.3641974-1-gary@garyguo.net> <20240529202817.3641974-3-gary@garyguo.net> In-Reply-To: <20240529202817.3641974-3-gary@garyguo.net> From: Matthew Maurer Date: Fri, 7 Jun 2024 07:40:45 -0700 Message-ID: Subject: Re: [RFC PATCH 2/2] kbuild: rust: provide an option to inline C helpers into Rust To: Gary Guo Cc: Masahiro Yamada , Nathan Chancellor , Nicolas Schier , Andrew Morton , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable The LLVM `.ll` textual bitcode representation may change from version to version, which makes using `sed` to edit it potentially fragile. Would it be possible to attach similar attributes without this? (Or other attributes that can accomplish something similar, see the second part.) Would it be possible to use a Rust-only-LTO (actual LTO) flag rather than a `llvm-link`? It seems likely that someone that wants to use a synced `clang` and `rustc` to allow helpers to be inlined would be unlikely to object to thin LTO of Rust. This would avoid introducing a new tool. As one possible route around editing the `.ll` file, we could consider *not* hiding these symbols, just marking them as "good idea to inline if possible", and then masking any symbol that ends up in the final `.o` via `objcopy --localize-symbol=3Dblah`. If we're doing full-kernel LTO rather than rust-only LTO, we'd need to use `llvm-objcopy` instead, but the same process would follow - a postprocessing step on the final crate object that localizes the symbol, with LTO giving the compiler the option (but not requirement) to inline these functions if it seems like a good idea. On Wed, May 29, 2024 at 1:30=E2=80=AFPM Gary Guo wrote: > > A new Kconfig option, `RUST_LTO_HELPERS` is added to allow C helpers > (which was created to allow Rust to call into inline/macro C functions > without having to re-implement the logic in Rust) to be inlined into > Rust crates without performing a global LTO. > > If the option is enabled, the following is performed: > * For helpers, instead of compiling them to object file to be linked > into vmlinux, we compile them to LLVM IR. > * The LLVM IR is patched to add `linkonce_odr` linkage. This linkage > means that the function is inlineable (effect of `_odr`), and the > symbols generated will have weak linkage if emitted into object file > (important since as later described, we might have multiple copies of > the same symbol) and it will may be discarded if it is not invoked or > all invocations are inlined. > * The LLVM IR is compiled to bitcode (This is step is not necessary, but > is a performance optimisation to prevent LLVM from always have to > reparse the same IR). > * When a Rust crate is compiled, instead of generating object file, we > ask LLVM bitcode to be generated. > * llvm-link is invoked to combine the helper bitcode with the crate > bitcode. This step is similar to what's done in a full LTO, hence the > option name, but this is much faster since it only needs to inline the > helpers. > * clang is invoked to turn the combined bitcode into object file. > > Some caveats with the option: > * clang and Rust doesn't have the exact target string. Manual inspection > shows that they should be compatible, but since they are not exactly > the same LLVM seems to prefer not inlining them. This is bypassed with > `--ignore-tti-inline-compatible`. > * LLVM doesn't want to inline functions combined with > `-fno-delete-null-pointer-checks` with code compiled without. So we > remove this command when compiling helpers. I think this should be > okay since this is one of the hardening features and we shouldn't have > null pointer dereferences in these helpers. > > The checks can also be bypassed with force inlining (`__always_inline`) > but the behaviour is the same with extra options. > > This option requires clang to be used for CC and the LLVM version of > clang and rustc to be matching (at least to the same major version), and > therefore is gated as EXPERT option. > > Co-developed-by: Boqun Feng > Signed-off-by: Boqun Feng > Signed-off-by: Gary Guo > --- > Makefile | 4 +++- > lib/Kconfig.debug | 10 ++++++++++ > rust/Makefile | 34 ++++++++++++++++++++++++++++++---- > rust/exports.c | 3 +++ > rust/helpers.c | 41 +++++++++++++++++++++-------------------- > scripts/Makefile.build | 5 ++++- > 6 files changed, 71 insertions(+), 26 deletions(-) > > diff --git a/Makefile b/Makefile > index f975b6396328..c6f12093fb1c 100644 > --- a/Makefile > +++ b/Makefile > @@ -492,6 +492,8 @@ OBJCOPY =3D $(LLVM_PREFIX)llvm-objcopy$(L= LVM_SUFFIX) > OBJDUMP =3D $(LLVM_PREFIX)llvm-objdump$(LLVM_SUFFIX) > READELF =3D $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX) > STRIP =3D $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX) > +LLVM_LINK =3D $(LLVM_PREFIX)llvm-link$(LLVM_SUFFIX) > +LLVM_AS =3D $(LLVM_PREFIX)llvm-as$(LLVM_SUFFIX) > else > CC =3D $(CROSS_COMPILE)gcc > LD =3D $(CROSS_COMPILE)ld > @@ -601,7 +603,7 @@ endif > export RUSTC_BOOTSTRAP :=3D 1 > > export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COM= PILE LD CC HOSTPKG_CONFIG > -export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDG= EN CARGO > +export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDG= EN CARGO LLVM_LINK LLVM_AS > export HOSTRUSTC KBUILD_HOSTRUSTFLAGS > export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX= YACC AWK INSTALLKERNEL > export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug > index 59b6765d86b8..8b9a1bd8ce71 100644 > --- a/lib/Kconfig.debug > +++ b/lib/Kconfig.debug > @@ -3049,6 +3049,16 @@ config RUST_KERNEL_DOCTESTS > > If unsure, say N. > > +config RUST_LTO_HELPERS > + bool "LTO C helpers with Rust crates" > + depends on RUST && CC_IS_CLANG && EXPERT > + help > + Links C helpers together with Rust crates using LTO. > + > + This is achieved by compiling both helpers and Rust crates into > + LLVM bitcode and use llvm-link to combine them. > + > + This requires a matching LLVM version for Clang and rustc. > + > + If unsure, say N. > + > endmenu # "Rust" > > endmenu # Kernel hacking > diff --git a/rust/Makefile b/rust/Makefile > index b4d63ea9209f..ad0797467102 100644 > --- a/rust/Makefile > +++ b/rust/Makefile > @@ -6,9 +6,14 @@ rustdoc_output :=3D $(objtree)/Documentation/output/rust= /rustdoc > obj-$(CONFIG_RUST) +=3D core.o compiler_builtins.o > always-$(CONFIG_RUST) +=3D exports_core_generated.h > > +ifdef CONFIG_RUST_LTO_HELPERS > +always-$(CONFIG_RUST) +=3D helpers.bc > +else > +obj-$(CONFIG_RUST) +=3D helpers.o > +always-$(CONFIG_RUST) +=3D exports_helpers_generated.h > +endif > # Missing prototypes are expected in the helpers since these are exporte= d > # for Rust only, thus there is no header nor prototypes. > -obj-$(CONFIG_RUST) +=3D helpers.o > CFLAGS_REMOVE_helpers.o =3D -Wmissing-prototypes -Wmissing-declarations > > always-$(CONFIG_RUST) +=3D libmacros.so > @@ -17,7 +22,7 @@ no-clean-files +=3D libmacros.so > always-$(CONFIG_RUST) +=3D bindings/bindings_generated.rs bindings/bindi= ngs_helpers_generated.rs > obj-$(CONFIG_RUST) +=3D alloc.o bindings.o kernel.o > always-$(CONFIG_RUST) +=3D exports_alloc_generated.h exports_bindings_ge= nerated.h \ > - exports_kernel_generated.h exports_helpers_generated.h > + exports_kernel_generated.h > > always-$(CONFIG_RUST) +=3D uapi/uapi_generated.rs > obj-$(CONFIG_RUST) +=3D uapi.o > @@ -350,12 +355,24 @@ $(obj)/bindings/bindings_helpers_generated.rs: priv= ate bindgen_target_flags =3D \ > --blocklist-type '.*' --allowlist-var '' \ > --allowlist-function 'rust_helper_.*' > $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cf= lags =3D \ > - -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declaration= s > + -I$(objtree)/$(obj) $(CFLAGS_helpers.o) -Wno-missing-prototypes -Wno= -missing-declarations > $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_ex= tra =3D ; \ > sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name=3D"rust_he= lper_\1"]\n pub fn \1/g' $@ > $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE > $(call if_changed_dep,bindgen) > > +# When compiling helpers, filter `-fno-delete-null-pointer-checks` since= LLVM > +# prevents inlining such functions to be inlined into functions compiled > +# without the option (e.g. Rust functions). > +quiet_cmd_rust_helper =3D HELPER $@ > + cmd_rust_helper =3D \ > + $(CC) $(filter-out -fno-delete-null-pointer-checks $(CFLAGS_REMOV= E_helpers.o), $(c_flags) $(CFLAGS_helpers.o)) -S $< -emit-llvm -o $(patsubs= t %.bc,%.ll,$@); \ > + sed -i 's/^define dso_local/define linkonce_odr dso_local/g' $(pa= tsubst %.bc,%.ll,$@); \ > + $(LLVM_AS) $(patsubst %.bc,%.ll,$@) -o $@ > + > +$(obj)/helpers.bc: $(obj)/helpers.c FORCE > + +$(call if_changed_dep,rust_helper) > + > quiet_cmd_exports =3D EXPORTS $@ > cmd_exports =3D \ > $(NM) -p --defined-only $< \ > @@ -396,11 +413,13 @@ quiet_cmd_rustc_library =3D $(if $(skip_clippy),RUS= TC,$(RUSTC_OR_CLIPPY_QUIET)) L > OBJTREE=3D$(abspath $(objtree)) \ > $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ > $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_f= lags)) \ > - --emit=3Ddep-info=3D$(depfile) --emit=3Dobj=3D$@ \ > + --emit=3Ddep-info=3D$(depfile) --emit=3D$(if $(link_helpe= r),llvm-bc=3D$(patsubst %.o,%.bc,$@),obj=3D$@) \ > --emit=3Dmetadata=3D$(dir $@)$(patsubst %.o,lib%.rmeta,$(= notdir $@)) \ > --crate-type rlib -L$(objtree)/$(obj) \ > --crate-name $(patsubst %.o,%,$(notdir $@)) $< \ > --sysroot=3D/dev/null \ > + $(if $(link_helper),;$(LLVM_LINK) $(patsubst %.o,%.bc,$@) $(obj)/= helpers.bc -o $(patsubst %.o,%.m.bc,$@); \ > + $(CC) $(KBUILD_CFLAGS) -mllvm=3D--ignore-tti-inline-compa= tible -c $(patsubst %.o,%.m.bc,$@) -o $@) \ > $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) > > rust-analyzer: > @@ -463,4 +482,11 @@ $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o= $(obj)/build_error.o \ > $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE > +$(call if_changed_dep,rustc_library) > > +ifdef CONFIG_RUST_LTO_HELPERS > + > +$(obj)/kernel.o: private link_helper =3D 1 > +$(obj)/kernel.o: $(obj)/helpers.bc > + > +endif > + > endif # CONFIG_RUST > diff --git a/rust/exports.c b/rust/exports.c > index aa1218b325e5..212c1ec1d38b 100644 > --- a/rust/exports.c > +++ b/rust/exports.c > @@ -19,7 +19,10 @@ > #include "exports_alloc_generated.h" > #include "exports_bindings_generated.h" > #include "exports_kernel_generated.h" > + > +#ifndef CONFIG_RUST_LTO_HELPERS > #include "exports_helpers_generated.h" > +#endif > > // For modules using `rust/build_error.rs`. > #ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW > diff --git a/rust/helpers.c b/rust/helpers.c > index 895f4b696962..3abf96f14148 100644 > --- a/rust/helpers.c > +++ b/rust/helpers.c > @@ -32,18 +32,19 @@ > #include > #include > #include > +#include "helpers.h" > > -__noreturn void rust_helper_BUG(void) > +__rust_helper __noreturn void rust_helper_BUG(void) > { > BUG(); > } > > -void rust_helper_mutex_lock(struct mutex *lock) > +__rust_helper void rust_helper_mutex_lock(struct mutex *lock) > { > mutex_lock(lock); > } > > -void rust_helper___spin_lock_init(spinlock_t *lock, const char *name, > +__rust_helper void rust_helper___spin_lock_init(spinlock_t *lock, const = char *name, > struct lock_class_key *key) > { > #ifdef CONFIG_DEBUG_SPINLOCK > @@ -53,82 +54,82 @@ void rust_helper___spin_lock_init(spinlock_t *lock, c= onst char *name, > #endif > } > > -void rust_helper_spin_lock(spinlock_t *lock) > +__rust_helper void rust_helper_spin_lock(spinlock_t *lock) > { > spin_lock(lock); > } > > -void rust_helper_spin_unlock(spinlock_t *lock) > +__rust_helper void rust_helper_spin_unlock(spinlock_t *lock) > { > spin_unlock(lock); > } > > -void rust_helper_init_wait(struct wait_queue_entry *wq_entry) > +__rust_helper void rust_helper_init_wait(struct wait_queue_entry *wq_ent= ry) > { > init_wait(wq_entry); > } > > -int rust_helper_signal_pending(struct task_struct *t) > +__rust_helper int rust_helper_signal_pending(struct task_struct *t) > { > return signal_pending(t); > } > > -refcount_t rust_helper_REFCOUNT_INIT(int n) > +__rust_helper refcount_t rust_helper_REFCOUNT_INIT(int n) > { > return (refcount_t)REFCOUNT_INIT(n); > } > > -void rust_helper_refcount_inc(refcount_t *r) > +__rust_helper void rust_helper_refcount_inc(refcount_t *r) > { > refcount_inc(r); > } > > -bool rust_helper_refcount_dec_and_test(refcount_t *r) > +__rust_helper bool rust_helper_refcount_dec_and_test(refcount_t *r) > { > return refcount_dec_and_test(r); > } > > -__force void *rust_helper_ERR_PTR(long err) > +__rust_helper __force void *rust_helper_ERR_PTR(long err) > { > return ERR_PTR(err); > } > > -bool rust_helper_IS_ERR(__force const void *ptr) > +__rust_helper bool rust_helper_IS_ERR(__force const void *ptr) > { > return IS_ERR(ptr); > } > > -long rust_helper_PTR_ERR(__force const void *ptr) > +__rust_helper long rust_helper_PTR_ERR(__force const void *ptr) > { > return PTR_ERR(ptr); > } > > -const char *rust_helper_errname(int err) > +__rust_helper const char *rust_helper_errname(int err) > { > return errname(err); > } > > -struct task_struct *rust_helper_get_current(void) > +__rust_helper struct task_struct *rust_helper_get_current(void) > { > return current; > } > > -void rust_helper_get_task_struct(struct task_struct *t) > +__rust_helper void rust_helper_get_task_struct(struct task_struct *t) > { > get_task_struct(t); > } > > -void rust_helper_put_task_struct(struct task_struct *t) > +__rust_helper void rust_helper_put_task_struct(struct task_struct *t) > { > put_task_struct(t); > } > > -struct kunit *rust_helper_kunit_get_current_test(void) > +__rust_helper struct kunit *rust_helper_kunit_get_current_test(void) > { > return kunit_get_current_test(); > } > > -void rust_helper_init_work_with_key(struct work_struct *work, work_func_= t func, > +__rust_helper void rust_helper_init_work_with_key(struct work_struct *wo= rk, work_func_t func, > bool onstack, const char *name, > struct lock_class_key *key) > { > @@ -139,7 +140,7 @@ void rust_helper_init_work_with_key(struct work_struc= t *work, work_func_t func, > work->func =3D func; > } > > -void * __must_check __realloc_size(2) > +__rust_helper void * __must_check __realloc_size(2) > rust_helper_krealloc(const void *objp, size_t new_size, gfp_t flags) > { > return krealloc(objp, new_size, flags); > diff --git a/scripts/Makefile.build b/scripts/Makefile.build > index efacca63c897..201e7dc5ae5d 100644 > --- a/scripts/Makefile.build > +++ b/scripts/Makefile.build > @@ -288,7 +288,10 @@ rust_common_cmd =3D \ > # would not match each other. > > quiet_cmd_rustc_o_rs =3D $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ > - cmd_rustc_o_rs =3D $(rust_common_cmd) --emit=3Dobj=3D$@ $< > + cmd_rustc_o_rs =3D $(rust_common_cmd) --emit=3D$(if $(CONFIG_RUST_= LTO_HELPERS),llvm-bc=3D$(patsubst %.o,%.bc,$@),obj=3D$@) $< \ > + $(if $(CONFIG_RUST_LTO_HELPERS),;$(LLVM_LINK) $(patsubst %.o,%.bc= ,$@) $(objtree)/rust/helpers.bc -o $(patsubst %.o,%.m.bc,$@); \ > + $(CC) $(KBUILD_CFLAGS) -mllvm=3D--ignore-tti-inline-compa= tible -c $(patsubst %.o,%.m.bc,$@) -o $@) > + > > $(obj)/%.o: $(obj)/%.rs FORCE > +$(call if_changed_dep,rustc_o_rs) > -- > 2.42.0 > >