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 396392FD7CD for ; Mon, 3 Nov 2025 10:22:52 +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=1762165375; cv=none; b=V2DBvITNGXLrFmG/AAfSQCl7oAxCHnpoc6RXozxdn+SHvajOhAjS390hx5awWf9h3x7xtoTG07sG8yoQztgCLVsAFVCh565jWmMb6Wfi8S28zwPOu4ZQn/9Jc/qk8ViIsRkE56H8EUuLx9aqci/lXMpofPxS10jocqdIk6YEw+s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762165375; c=relaxed/simple; bh=ZRv2Bik0mh3ATM87xP1ZEX31SBMDxa+G2+N2PQfmDDY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BEjQCDbTZnpop9f49KRj5TfXsSv+l61zRWEonnhdxMjD9EdyKeyQEWMC1Zs8PFzboRQMssoo4bBjN9mF15E70RgVLAtnHfUCd0W2+1VTaHJJSuOUJl0WH7/41ZEZXNHjhaPcnzH6NYVGXcO57tYHHSremI8AMzg43iv/cSSdUto= 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=IgG8yfUI; 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="IgG8yfUI" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-b448c864d45so394610666b.2 for ; Mon, 03 Nov 2025 02:22:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1762165371; x=1762770171; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=74nlGMCrnYUSq61pxDhzCKYDlRI/c4Z3CWqyM8Nq0u4=; b=IgG8yfUInXpn7+jSRUl915/XmW3eRHHS9FuY7Ytbg1vkMY6UqWAwJ7rQ9W/zVPuFtc i5DL1X4URQbfeaX4CJKnLeQPbZjv027v+2VZLADQDMWFepB8Jb58qbVDil9v414ns93S JUPfXu3b5avbhmT8KhTOYiaLR0glOVHsacKRz2N+K1pTRJbiC/MKIVh+Zoh+ruxFCz7O 7coPEy1FIUg0zVVa3NoDRpEm15qosYJ5nNdc3QTp9I5zZRaR42JS65053z5mSSetN0j/ TRnwsTlWuHaKgpodXEfg9JEXHrn5CiN2QrQhClnScAi+xfpfnaUV0joUqFFazlh+9OnE 3jeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762165371; x=1762770171; h=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=74nlGMCrnYUSq61pxDhzCKYDlRI/c4Z3CWqyM8Nq0u4=; b=tabKh+6qtPRJwqsK+I0bDmMBY8dUhgvcidWWKtE9+0y8Rnr/OjD4fYMgKVZxHifM0K h9nl/o2ybA5WAMpO6kG7MLNmxxicvFEayrsbsubxPFdtPmtKsDNrvPOOliX+8mkThA3t +o65PR6VkD7gW4CM5+TeEeyT/djOTbpRV59J0jXZ8QldnSUSaJY8tkrYPD982z7UGFx/ frvTpK+XVlrueB6wU01+uAOlRJXcazy4CVyMj/DC1ANvD0uGahDR5Wp9wHAxWLFBQKEj mEkI0SDhPFGNpAW/zQPL/tQX846MNCl+48umws5JtyG1NkCB2N5HcyDtEyZHTBFiz2l9 e94w== X-Forwarded-Encrypted: i=1; AJvYcCVOg1zlBj7Nj3IUuh/d8u7uxxyT/Zfr2nPI3rkE5Mfe7UzOViJS9/C060GK95zAZ1k3LS2k1mEINCSKEMJPNw==@vger.kernel.org X-Gm-Message-State: AOJu0YzP2Q5E/6BvYqrruyjbWp9ojsUVUEcsqyZZ9rK4DX5VPz1/YHC/ bGB3eskuBgFi6CeCf6qbYkUFSRryTUOn2DJ+6m/IAgy8NuTmmAahlFodOaeW/o760oaNMWRKvqK MXVJnC/iyh4zfdiCkjQ== X-Google-Smtp-Source: AGHT+IHn9x6PSa83XXQosPVx5KOgBWzu3uFfEDGplbMkxxWGLEi0O/THMcQiIWHNoig+hnN4QKP3KjebTPJUtss= X-Received: from ede26.prod.google.com ([2002:a05:6402:20da:b0:640:b66f:1e57]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:5111:b0:63e:2d4d:5a60 with SMTP id 4fb4d7f45d1cf-64076f67066mr10626790a12.8.1762165370729; Mon, 03 Nov 2025 02:22:50 -0800 (PST) Date: Mon, 3 Nov 2025 10:22:49 +0000 In-Reply-To: <20251101-b4-as-flattened-v1-1-860f2ebeedfd@nvidia.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251101-b4-as-flattened-v1-1-860f2ebeedfd@nvidia.com> Message-ID: Subject: Re: [PATCH RESEND] rust: enable slice_flatten feature and abstract it through an extension trait From: Alice Ryhl To: Alexandre Courbot Cc: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Nathan Chancellor , Nicolas Schier , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Sat, Nov 01, 2025 at 10:31:38PM +0900, Alexandre Courbot wrote: > In Rust 1.80, the previously unstable `slice::flatten` family of methods > have been stabilized and renamed to `as_flattened`. > > This creates an issue as we want to use `as_flattened`, but need to > support the MSRV (which at the moment is Rust 1.78) where it is named > `flatten`. > > Solve this by enabling the `slice_flatten` feature, and abstracting > `as_flatten` behind an extension trait that calls the right method > depending on the Rust version. > > This extension trait can be removed once the MSRV passes 1.80. > > Suggested-by: Miguel Ojeda > Link: https://lore.kernel.org/all/CANiq72kK4pG=O35NwxPNoTO17oRcg1yfGcvr3==Fi4edr+sfmw@mail.gmail.com/ > Signed-off-by: Alexandre Courbot > --- > This patch was part of the Nova GSP boot series [1], but since it > requires attention from the core Rust team (and possibly the build > maintainers?) and is otherwise buried under Nova patches, I am taking > the freedom to send it separately for visibility. > > Hopefully it captures Miguel's suggestion [2] accurately, but please let > me know if I missed something. > > Since the Nova GSP boot series makes use of this, I hope to eventually > merge it alongside the series, through the DRM tree. > > Thanks! > > [1] https://lore.kernel.org/all/20251029-gsp_boot-v7-0-34227afad347@nvidia.com/ > [2] https://lore.kernel.org/all/CANiq72kK4pG=O35NwxPNoTO17oRcg1yfGcvr3==Fi4edr+sfmw@mail.gmail.com/ > --- > init/Kconfig | 3 +++ > rust/kernel/lib.rs | 4 ++++ > rust/kernel/slice.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ > scripts/Makefile.build | 3 ++- > 4 files changed, 72 insertions(+), 1 deletion(-) > > diff --git a/init/Kconfig b/init/Kconfig > index cab3ad28ca49..7da93c9cccc3 100644 > --- a/init/Kconfig > +++ b/init/Kconfig > @@ -147,6 +147,9 @@ config LD_CAN_USE_KEEP_IN_OVERLAY > # https://github.com/llvm/llvm-project/pull/130661 > def_bool LD_IS_BFD || LLD_VERSION >= 210000 > > +config RUSTC_HAS_SLICE_AS_FLATTENED > + def_bool RUSTC_VERSION >= 108000 > + > config RUSTC_HAS_COERCE_POINTEE > def_bool RUSTC_VERSION >= 108400 > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 3dd7bebe7888..2581a356d114 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -21,6 +21,9 @@ > #![feature(inline_const)] > #![feature(pointer_is_aligned)] > // > +// Stable since Rust 1.80.0. > +#![feature(slice_flatten)] > +// > // Stable since Rust 1.81.0. > #![feature(lint_reasons)] > // > @@ -128,6 +131,7 @@ > pub mod security; > pub mod seq_file; > pub mod sizes; > +pub mod slice; > mod static_assert; > #[doc(hidden)] > pub mod std_vendor; > diff --git a/rust/kernel/slice.rs b/rust/kernel/slice.rs > new file mode 100644 > index 000000000000..7e837bec4bed > --- /dev/null > +++ b/rust/kernel/slice.rs > @@ -0,0 +1,63 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Additional (and temporary) slice helpers. > + > +/// Extension trait providing a portable version of [`as_flattened`] and > +/// [`as_flattened_mut`]. > +/// > +/// In Rust 1.80, the previously unstable `slice::flatten` family of methods > +/// have been stabilized and renamed from `flatten` to `as_flattened`. > +/// > +/// This creates an issue for as long as the MSRV is < 1.80, as the same functionality is provided > +/// by different methods depending on the compiler version. > +/// > +/// This extension trait solves this by abstracting `as_flatten` and calling the correct method > +/// depending on the Rust version. > +/// > +/// This trait can be removed once the MSRV passes 1.80. > +/// > +/// [`as_flattened`]: slice::as_flattened > +/// [`as_flattened_mut`]: slice::as_flattened_mut > +pub trait AsFlattened { > + /// Takes an `&[[T; N]]` and flattens it to a `&[T]`. > + /// > + /// This is an portable layer on top of [`as_flattened`]; see its documentation for details. > + /// > + /// [`as_flattened`]: slice::as_flattened > + fn as_flattened_slice(&self) -> &[T]; > + > + /// Takes an `&mut [[T; N]]` and flattens it to a `&mut [T]`. > + /// > + /// This is an portable layer on top of [`as_flattened_mut`]; see its documentation for details. > + /// > + /// [`as_flattened_mut`]: slice::as_flattened_mut > + fn as_flattened_slice_mut(&mut self) -> &mut [T]; > +} > + > +impl AsFlattened for [[T; N]] { > + #[allow(clippy::incompatible_msrv)] > + fn as_flattened_slice(&self) -> &[T] { > + #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))] > + { > + self.flatten() > + } > + > + #[cfg(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED)] > + { > + self.as_flattened() > + } > + } > + > + #[allow(clippy::incompatible_msrv)] > + fn as_flattened_slice_mut(&mut self) -> &mut [T] { > + #[cfg(not(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED))] > + { > + self.flatten_mut() > + } > + > + #[cfg(CONFIG_RUSTC_HAS_SLICE_AS_FLATTENED)] > + { > + self.as_flattened_mut() > + } Hmm. Why not have this match the name that this was stabilized under? That way, when bumping the MSRV, we can just remove this trait without changing the callers. Alice