From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.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 CC4FF3D412E for ; Mon, 4 May 2026 12:57:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777899471; cv=none; b=iytjmVmx1+e5hSmBmNvUZDTUf2y93X1S7qcO4KRvfgfTShzNImHBq3KT6LKd5aA8E36vtYzhg2xwfgHtvhFLnxiWa1+mNRT4s4qwf6rIPDbYLfnea6pFoTCgpdY5HcCZMH9fSL2A7NTzNVLqw1tism8D62TfvZPdBwtfwmuFjU4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777899471; c=relaxed/simple; bh=JpKw4YGYZqTSeDouLbV/J5I+mhkq6/HGzXNcBfTURsA=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=EvDk90mOLX1f5SBOBNOEVSCwm1n/Q33UuqGYoC1YHK9JGM26rQIrH0hV9kaJfR4+TZzs26kmbrElqgggentRefwwjJWO1kkO8mJ3JC1jnnqJcT1UDEVK+UuBvT6M7fMlTBB0NHowemguEmj1mD7W2RsCOKsgu2YZNSoa4+swWUI= 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=SZvJylww; arc=none smtp.client-ip=209.85.218.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="SZvJylww" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-bc34159f48dso39794866b.1 for ; Mon, 04 May 2026 05:57:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777899468; x=1778504268; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=4Zg+P5Ciirgaisv29teHnbm3lGZWHfTdji8uo+YNlso=; b=SZvJylwwsv0ocw7/Y1uUu35YfV1EdPt8qxUXlIsbjAwKfygBR49sHs3KJT3CKKHjBX F5BmVrDAVXiYGJp4A01dm0t963UutfFC+4CrIq9Uue8gTQzAQrvXvnOtkL8DS2qX506u 24VTWlh0Sl//9ySf2QFP1OOO4HmGyJpba28OVxUL7JIxcc27fOz3VtpIirZmHsAk/hDg gTf3wEl2h0P+QAVbxybNSR8es23JpeG+psZ/CMY4V5VIxAXDnq/HnYOWCTdIzAx18AzJ OPl3f/24rglhxJy/vP2B7+BF1cEN2lqpIbFXIdDSNsvqqdlT9TZHmAJ1P59rufVKeI07 OSfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777899468; x=1778504268; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=4Zg+P5Ciirgaisv29teHnbm3lGZWHfTdji8uo+YNlso=; b=kQbBBTYF1v4J78Myhiar4sGmtK25+CypgL1TKoZwYNWQLuRjY0Sw2MyLYlQq8hqn3e EZJLbDWLNDPFaDvypFNGDgVQ3r8nBp1ChHBBMejHrnay3NU+mkBq/pBmUAdGOxC1xQUE fn15IfuZB5Pwl4tiLWdE4rPGTofQeGBvFSdvOyH/BMfQyS/tUBOMQkd+ZHwbP2Lg2wDw dPYHGnsjQ/0BxCoE8WUkjStcFVfChCP5B0QxslT/gTuJczs5qY3SrKF4kmLrdHWN+yYY JZbIkElB73rWxKahI++iTTkQNNA7mIpjvjTlvX1eAu929gHhuUM5Zhnn4VCAVOQWQ0a3 Be2A== X-Forwarded-Encrypted: i=1; AFNElJ+T1Uwz/cKkg0rbzmqO3tpwfiQfcAEsNB6689pDTCX/lC4hid36nwVsHNjUXdAX2F4oMnvEpy3YbB2hPjgJAw==@vger.kernel.org X-Gm-Message-State: AOJu0Yw9WgBXono6PArJLYJkKKEEgqju8nhshI6FcN0AXCCQlr6Qry8C sO7jUmUVgPO285dcPBq0inzYJsxhoMMLRyezSAJFASyr6Er+Jftisq4K4JGqdnVD7JPm67EVW07 FMP7ODTn3aigOYg1Obg== X-Received: from eddp19.prod.google.com ([2002:a05:6402:46d3:b0:676:3630:b7f9]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:80e:b0:baa:1db1:ef9b with SMTP id a640c23a62f3a-bbff992c153mr499332666b.18.1777899467950; Mon, 04 May 2026 05:57:47 -0700 (PDT) Date: Mon, 04 May 2026 12:57:37 +0000 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMGX+GkC/x2MQQ5AMBAAvyJ7tklVi/iKOFCLvajsIhLxd43jJ DPzgJIwKbTZA0IXK8ctQZFnENZhWwh5SgzW2Mp441BOPQLG/UgiBomqWI/eujmU3k0NpHAXmvn +p13/vh8w427wZAAAAA== X-Change-Id: 20260504-rustc-option-cross-7b524fc354d8 X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4375; i=aliceryhl@google.com; h=from:subject:message-id; bh=JpKw4YGYZqTSeDouLbV/J5I+mhkq6/HGzXNcBfTURsA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBp+JfGB0Q2qinQ0FAwhZoBoMieWhmyYsAhdzQ/Y V1a3PG2x6WJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCafiXxgAKCRAEWL7uWMY5 Rt2yD/9d+F5SkEGar3glfqRRwNrM28H7EkLbjdv55qgJ9gpwGh6aXPx2RS/P+YKITwkYQF1uKRA BqvPJcOGgRj6Ut8ujVA2wLAGnMBio3k/PmWv3u0k6/jhclmze25AoBIPlMcQhNm+5ILRbkTXuAj 9z6FjbUEciC1s6p2urzpeYm0FZO/ggcy4nKCUJ3zPXwM3yVS+E6Nj6SngQsZUDCPozXoHVvHatP xl05HV9tSpl0kchRvcL+MsRxlvv0slFlc7hvHd7UDrxDQn1HHuQSH6Slu+fiYaC/z5KmWnKHBDu 9Qsn1ebtgntv6eFHYfZ012pcfCDttMPLar5P9+Re7wZvul+lc4j8NkMdUEvAYNv5kmFzcOwM9Hn BdAP170NqWxlUmsOZvM+0JUBDrDWn+k9ea/N5I+wp5eyG7cTORFzPMB2+sW7g8IWG3L1/rjHOTB z3t3x+aeMFXfwsbffBef6ykPeJpFwaHvSNppCkSPlDMJELSmbPPelk6WHJk0MeQpbK1lZHXez4d BPJ6ig+nVuLQBIXU7AxcLgzH68IVeuMNvLzhbJU3KiRwgc0nJLGeFJHM6kCMUHss5zzYzmR7Hhy FYc1SmbQTOgzZg+GxgUgsWEIHWR6uV5uk36SHuAoW8ObwRCn6gizw46Zpaww+NXRndHxuQuhrSe dPNORfqF8ZEDdvQ== X-Mailer: b4 0.14.3 Message-ID: <20260504-rustc-option-cross-v1-1-09f2726b680f@google.com> Subject: [PATCH] rust: kasan/kbuild: fix rustc-option when cross-compiling From: Alice Ryhl To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Nathan Chancellor , Nicolas Schier , Miguel Ojeda Cc: Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, rust-for-linux@vger.kernel.org, stable@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" The Makefile version of rustc-option currently checks whether the option exists for the host target instead of the target actually being compiled for. It was done this way in commit 46e24a545cdb ("rust: kasan/kbuild: fix missing flags on first build") to avoid a circular dependency on target.json. However, because of this, rustc-option currently does not function when cross-compiling from x86_64 to aarch64 if CONFIG_SHADOW_CALL_STACK is enabled. This is because KBUILD_RUSTFLAGS contains -Zfixed-x18 under this configuration. Since that flag does not exist on the host target, rustc-option runs into a compilation failure every time, leading to all flags being rejected as unsupported. To fix this, update rustc-option to pass a --target parameter so that the host target is not used. For targets using target.json, use a built-in target that is as close as possible to the target created with target.json to avoid the circular dependency on target.json. One scenario where this causes a boot failure: * Cross-compiled from x86_64 to aarch64. * With CONFIG_SHADOW_CALL_STACK=y * With CONFIG_KASAN_SW_TAGS=y * With CONFIG_KASAN_INLINE=n Then the resulting kernel image will fail to boot when it first calls into Rust code with a crash along the lines of "Unable to handle kernel paging request at virtual address 0ffffffc08541796". This is because the call threshold is not specified, so rustc will inline kasan operations, but the kasan shadow offset is not specified, which leads to the inlined kasan instructions being incorrect. The above scenario actually also fails without -Zfixed-x18 since the SW_TAGS sanitizer itself is aarch64-specific. But it's a problem with normal KASAN too because of -Zfixed-x18. Cc: stable@vger.kernel.org Fixes: 46e24a545cdb ("rust: kasan/kbuild: fix missing flags on first build") Signed-off-by: Alice Ryhl --- arch/x86/Makefile | 4 ++++ arch/x86/Makefile.um | 8 ++++++++ scripts/Makefile.compiler | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 46fec0b08487..1d526a5d2a83 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -77,6 +77,10 @@ KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -mno-sse4a KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 +# The target.json file is not available when invoking rustc-option, so use the +# built-in target when checking whether flags are supported instead. +KBUILD_RUSTFLAGS_OPTION_CHKS += --target=x86_64-unknown-none + # # CFLAGS for compiling floating point code inside the kernel. # diff --git a/arch/x86/Makefile.um b/arch/x86/Makefile.um index 19c13afa474e..9adecd65639f 100644 --- a/arch/x86/Makefile.um +++ b/arch/x86/Makefile.um @@ -14,6 +14,14 @@ endif KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json +# The target.json file is not available when invoking rustc-option, so use the +# built-in target when checking whether flags are supported instead. +ifeq ($(CONFIG_X86_32),y) +KBUILD_RUSTFLAGS_OPTION_CHKS += --target=i686-unknown-linux-gnu +else +KBUILD_RUSTFLAGS_OPTION_CHKS += --target=x86_64-unknown-linux-gnu +endif + ifeq ($(CONFIG_X86_32),y) START := 0x8048000 diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler index ef91910de265..06bbe29c846c 100644 --- a/scripts/Makefile.compiler +++ b/scripts/Makefile.compiler @@ -80,7 +80,7 @@ ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3)) # TODO: remove RUSTC_BOOTSTRAP=1 when we raise the minimum GNU Make version to 4.4 __rustc-option = $(call try-run,\ echo '$(pound)![allow(missing_docs)]$(pound)![feature(no_core)]$(pound)![no_core]' | RUSTC_BOOTSTRAP=1\ - $(1) --sysroot=/dev/null $(filter-out --sysroot=/dev/null --target=%,$(2)) $(3)\ + $(1) --sysroot=/dev/null $(KBUILD_RUSTFLAGS_OPTION_CHKS) $(filter-out --sysroot=/dev/null --target=%target.json,$(2)) $(3)\ --crate-type=rlib --out-dir=$(TMPOUT) --emit=obj=- - >/dev/null,$(3),$(4)) # rustc-option --- base-commit: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32 change-id: 20260504-rustc-option-cross-7b524fc354d8 Best regards, -- Alice Ryhl