From nobody Thu Dec 18 19:43:32 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 F2258263F52 for ; Tue, 16 Dec 2025 00:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765845896; cv=none; b=DQIzGBTHhjwYsm/4x4GpaUNNkHyccC27tTW1ZwK6flvxuA9kv2HvSPA05KzpSB4VXG99g0CfEsAoK0SunSwAi/NQc/bgP9e4QWeX3cBMapQGMUF5Kvy73nGdxZhbEkwDhf6/uW977pjeW+srcWzQcJ6pDsbXF0Bxe/Ek5QOf6Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765845896; c=relaxed/simple; bh=zWmWPrNOhUkM882JMpbDBfEGQcxG/fITkb9z+Trg0V8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Wo8Md37RQOUDfJrceaQwx3gs2DbE7QETXgsDEWvRC9ZQ8TBlzJK/Ed+iaZsaFZ327dM9M+m1qDN5lUwSlilcUmqfTJOliAaTxvvPd0tNdiiOvTjUMD721Itppp5TUvPdapm6feUr22aBiMG6LYaw3ctPzqChLLrzuCjBPqZyseA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=irvSTSY0; arc=none smtp.client-ip=209.85.215.202 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--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="irvSTSY0" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b9b9e8b0812so6695454a12.0 for ; Mon, 15 Dec 2025 16:44:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765845893; x=1766450693; 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=cEnDmHEq/pjEOk2KbWPaIkOgwqje/3X84mpIIYgLImI=; b=irvSTSY0F9l89sut/Y3Rx4BbbLpj4feEMCN8G+IkECTrg4a1kdzYbxKZdKh4d1n1x7 uFUKKWj+8KxvIjeyP1d6/zr37o7u/HY0DZCimmd7y6oRLSUcQCGJ+HWtJNvP1uOJq68d jtlh54nb+fgkI/Y3vL+CHu01fouGs6WjYSHY9U98836BOOq+ZDzHwVuYh53R5xMpmWa8 Wj2gVg5DnYnfzENSVWGwPtn1xZGFvFCU4Tmx7QS+NznWzIfCumu43ew1t0JZSCwhiFS2 j+xqGiFMAereyVH5W3N1sWFvS9v7z5Z/Og2sFN2sHko/Lw1qQWPFRDM7++DFZbdthxWO ypmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765845893; x=1766450693; 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=cEnDmHEq/pjEOk2KbWPaIkOgwqje/3X84mpIIYgLImI=; b=TsECQm01FCHhgXbNA/6vkATfoMaNz70G0shWTBk4EwfB6MsWoDtyF4Jqw2+55/4kuL 1jyQg+ADeHW5/V0LNpDv8dGbzbnhwOGS1JgB0oK66nhxqQnqFwQhpYvx+DUHp5lR+Qcv fHrfqQ4QxfWHJ8pNotYxpEQxj7VMyEZImtuz4+nSw5E3l55sjmvYfU9DF8PAaHKa8ePY ywnLl0tPo4FwHKuFqfukeohkiSzUzoEmVAGPJjikZTVQc+R/HN7ipiD9cqzgQFHzr/+n Jsship70h5/eW35DKUZqH8AEWXLUnzhxJfiYDJlA+sTN0sauDTcH+oe5AricawphU7Uw Qsuw== X-Forwarded-Encrypted: i=1; AJvYcCXtaHTXuTOkAwPx7IP1NfDo/5lHDZTbjLQGTe/TK+bT++PrNMj1y0yRRqsJU3K37rpYPnCxzAHXvwn0Guo=@vger.kernel.org X-Gm-Message-State: AOJu0YyarGfiLr3wia0/YNoF1bcihMLnz10U6eQjGy/O2HZPBv0bcHrK g0oly8JLEBHmxRoltjRIH1q2lCZc43ODOtWn6chbSbwIGlDM5HpShi9A6z6tqPiF07vbDlXT2xy CxKboeDQ0Jw== X-Google-Smtp-Source: AGHT+IGRZPMQ2yBe0NhIvb5hOnB2DN4D4G61oVHxl7WF0Mdjz4pPXx29w9FfD3xZ46WBgKt50LDhqF9T4TV7 X-Received: from dyki18.prod.google.com ([2002:a05:7300:7c12:b0:2ac:1eee:fdb4]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:df44:b0:2a4:7fb3:7a96 with SMTP id 5a478bee46e88-2ac3014694emr7291095eec.36.1765845893134; Mon, 15 Dec 2025 16:44:53 -0800 (PST) Date: Tue, 16 Dec 2025 00:44:41 +0000 In-Reply-To: <20251216-transmute-v2-0-b23e5277ad02@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251216-transmute-v2-0-b23e5277ad02@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1765845890; l=3786; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=zWmWPrNOhUkM882JMpbDBfEGQcxG/fITkb9z+Trg0V8=; b=gi00nijuT2TpF/lR3rXX31OTfUtcwxGU0aW4+os2xhsYPx12A8zt05wJ2d5wQPaQFFxPyYnAT Ws6J/pxRM1vAe6a2m82ebV7KlIBKOmy5JfQtFfWVgt065/ITyEPrYC1 X-Mailer: b4 0.14.2 Message-ID: <20251216-transmute-v2-1-b23e5277ad02@google.com> Subject: [PATCH v2 1/3] rust: transmute: Support transmuting slices of AsBytes/FromBytes types From: Matthew Maurer To: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Currently, we support either transmuting a byte slice of the exact size of the target type or a single element from a prefix of a byte slice. This adds support for transmuting from a byte slice to a slice of elements, assuming appropriate traits are implemented. Signed-off-by: Matthew Maurer Reviewed-by: Daniel Almeida --- rust/kernel/transmute.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 72 insertions(+) diff --git a/rust/kernel/transmute.rs b/rust/kernel/transmute.rs index be5dbf3829e25c92f85083cc0f4940f35bb7baec..5cc5f4fde4c31cea3c51ab078b4= d68f3a0a2caa1 100644 --- a/rust/kernel/transmute.rs +++ b/rust/kernel/transmute.rs @@ -122,6 +122,78 @@ fn from_bytes_mut_prefix(bytes: &mut [u8]) -> Option<(= &mut Self, &mut [u8])> } } =20 + /// Converts a slice of bytes to a slice of `Self`. + /// + /// Succeeds if the reference is properly aligned, and the size of `by= tes` is a multiple of that + /// of `Self`. + /// + /// Otherwise, returns [`None`]. + fn from_bytes_to_slice(bytes: &[u8]) -> Option<&[Self]> + where + Self: Sized, + { + let size =3D size_of::(); + if size =3D=3D 0 { + return None; + } + if bytes.len() % size !=3D 0 { + return None; + } + let len =3D bytes.len() / size; + let ptr =3D bytes.as_ptr().cast::(); + + #[allow(clippy::incompatible_msrv)] + if ptr.is_aligned() { + // SAFETY: + // - `ptr` is valid for reads of `bytes.len()` bytes, which is + // `len * size_of::()`. + // - `ptr` is aligned for `Self`. + // - `ptr` points to `len` consecutive properly initialized va= lues of type `Self` + // because `Self` implements `FromBytes` (any bit pattern is= valid). + // - The lifetime of the returned slice is bound to the input = `bytes`. + unsafe { Some(core::slice::from_raw_parts(ptr, len)) } + } else { + None + } + } + + /// Converts a mutable slice of bytes to a mutable slice of `Self`. + /// + /// Succeeds if the reference is properly aligned, and the size of `by= tes` is a multiple of that + /// of `Self`. + /// + /// Otherwise, returns [`None`]. + fn from_bytes_to_mut_slice(bytes: &mut [u8]) -> Option<&mut [Self]> + where + Self: AsBytes + Sized, + { + let size =3D size_of::(); + if size =3D=3D 0 { + return None; + } + if bytes.len() % size !=3D 0 { + return None; + } + let len =3D bytes.len() / size; + let ptr =3D bytes.as_mut_ptr().cast::(); + + #[allow(clippy::incompatible_msrv)] + if ptr.is_aligned() { + // SAFETY: + // - `ptr` is valid for reads and writes of `bytes.len()` byte= s, which is + // `len * size_of::()`. + // - `ptr` is aligned for `Self`. + // - `ptr` points to `len` consecutive properly initialized va= lues of type `Self` + // because `Self` implements `FromBytes`. + // - `AsBytes` guarantees that writing a new `Self` to the res= ulting slice can safely + // be reflected as bytes in the original slice. + // - The lifetime of the returned slice is bound to the input = `bytes`. + unsafe { Some(core::slice::from_raw_parts_mut(ptr, len)) } + } else { + None + } + } + /// Creates an owned instance of `Self` by copying `bytes`. /// /// Unlike [`FromBytes::from_bytes`], which requires aligned input, th= is method can be used on --=20 2.52.0.305.g3fc767764a-goog