From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796144124; b=LRwL71MFYAds8Sp5jUU1GvLs6niTLwMf361MxZo+Q1xrYzsyCT9fimhjOcYefNhJ2Dxv skqN5d8HFpi51agtReKOGwMyna55vp7WzDHO7P7Uf7HXxWTc1VgZz81P9IEHXya3TrydM tiLIdpoENXiwxi4p9esxDZ9s97Z0KzLNIVnJcqow9d9zk6ay/PfDRVIrXxJbklt7q5nf8 7a8pABHuVS9h76DLRlOaOuf2yAPy8RL89oiBM6o6XYy6MycV0N0kLZD9Rx38cTMej7a2F 1Nyww14ENITZ/FPt6sfrmx9xkuwunN0/RV7u9RaLUVJWnYN7r8bD7/SxP8Ac8fCI5xQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796144124; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=qhesYuumCFx5RFuR8BQnjgSNxR6kL+F4hwWpqiutO4g=; b=crPcJZOx7mKFxD/aNLhPCBvn3F/PbfqaHgqE+m1rPQ0aYim5f/tM8Hf3vMuWoZNDqhtM dP3YqBoWYtd0k2Y5mNMrtnHYsetVJCAOKxNBOMLnZgPFYtWGlvkWTY1SwAACKPPeUvnci jvLPA69YBG706uPJGRd/jmSlO7q/zDrwaGcY3sEuziuAJqUscLQNb2nvfgHapLmu7kZRH H3kKB8OUBoTkA6pwo9zjEh6Z3jtX8pI2P0xFPvRDZEh/aAafPu4CrFk5k2ib4VIQeNpMR C/G/hQIznmTtpccY/pRvIuk/KlR3r9WeNuVTUFUqeqw2JaLQmqs5OO8rXFXgxMGTGVg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=HcsMZ5Y5; arc=none smtp.remote-ip=209.85.216.51 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3243860A263F for ; Tue, 1 Dec 2026 16:55:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3243860A263F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=HcsMZ5Y5 Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3b41e5a1719so5011485a91.3 for ; Tue, 01 Dec 2026 08:55:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796144123; x=1796748923; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qhesYuumCFx5RFuR8BQnjgSNxR6kL+F4hwWpqiutO4g=; b=HcsMZ5Y57nb1rVy4NINeMVrmkue8kTUoz3ZU2GKCsem2pxiohrl19ME2fZDNhQhA2R m/jzDexdw/2EuUCHxZRQiULDjyTqWF3PDL3cc4U9WR8wjq6jpyG9y1l2PXp8KzKv10aq 8or92CAcOEl3xx/C26CE6AroXQcY8nop2+xrWss3l1JqBTU9Jr9Zm7yu3EYPv+thcD1h MLwDp3j2mIVY6Qe9PzVdvPiVedQj8fPpaqqkmHJXYiJyHaJW9Osjrfl7nque7OGMS2Sq hKy4FqsZP6Nmy8XXUwfSlOJf2G6j5ghMem6GJ2uvUOtcUqPZBcx9sYGEDMo7eU7/95ag 8IBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796144123; x=1796748923; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=qhesYuumCFx5RFuR8BQnjgSNxR6kL+F4hwWpqiutO4g=; b=eIObFSae0nlh81KSdUe2XpDv19Sq//pRPfbPYZ0kgV/nyr/QkEWBJMnC+N04ucqBWZ Mhnew3xtA+nfGnY/fawS/m0DYB81K+cQ09eNk+IrJhG1RJv4vhgO16/BrynnTl/Wudqk +J2F2fPhCtkHJMRuUF7B8M6YAEBABuaECTBsA+K4RDI3SRNAO8B869qfo2CSqPc5qDkQ ZqePSg6vLcrHZHlVG9PV4UOjRN7VpHOc4EUCQOXmIK6Y/xMiRB6ex82xuQUsjGb5kLP8 7scP1QyjJ9spoob8RgC+XyINCe6dpL+fyf7ivOCmtYPglwD6ejGqP1Yrj50TjuDnCh4B ZqXQ== X-Forwarded-Encrypted: i=1; AL+hErGrds0Z+o7szrOBfktDrXDM7t11QTVCwPuZpBfuBufc6QU2x+G1tNMxiTPYODjwrro1Vag/tJuoTv4=@vger.kernel.org X-Gm-Message-State: APuU4Z+gf90jsS85c/LefbGqee+YoWiZzft3E99Aho3LDNxHrMEtKhI/ kFoZLEA8XFJvwbH4XYwoS3ULJJZ72XLikFysT1OKhDFXAzOFr4GEIA0C X-Gm-Gg: AZFdTZdtHu8F/8+UTL1sxjY0FToxkO4xYG6c+SL5Qlhs9svfIZ8Lt/UoNyLsbfgLJKe QhQuc/i7+5zCFGwtBcDrLrfs34OTMr1vFiauTpyS6VWE8u5G5bT7AZR9fEl1wAGQpgNEKegc5DK o6WQmhVwT1Hl/38o0X6zYWIe1Fljh3EZCI2kRmfyEZuRYrN9lyNMu4yWl00EcXKHBHlmjty8iFp 8CSk1/9iNC0ijURPp81vSlyZb/gWqI0+ce/ODkT2hjQTpmD+poUJvQ+Qrv9rrc2dUi7AkmEwWsj ggLiyOYRbGYTiQtmPVa17FL/yc8IIqNg6qoMy5U9Ze01XiLC/vkMbzK/Bow9wJjSjnVU/6NNFiq 63oEg7MpZZr6yT7LXlQH1/CyeuOsFNkW5r3dlStVF8XB35al8J0j6QrEvur5zCOTzmxkB+TFOd1 +aerKXMxRNJ6VcgL6zPzMDAC3Jyd/n6uxH3jwHZxksqj8F7IgUwOfFO804pjpSPe2Rb1FnfCAwX k2Ay2/WV85hwEHHhqm779QzxC93FdrkTQxAr/8Fr9nXqKgQhdaURmZLcf7S81rmy4wtvJJsKMbV SU2SKStNkN8B9yRwPG6MT6f4qgmKIx1QxLgIrCHcxlFc31sw9MvRq5RtlKp528FEPu0n X-Received: by 2002:a17:90b:57ef:b0:3b5:a3:4425 with SMTP id 98e67ed59e1d1-3b500a34698mr20650974a91.42.1796144123128; Tue, 01 Dec 2026 08:55:23 -0800 (PST) Received: from localhost.localdomain ([36.68.98.203]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b73b2b3sm29159174a91.2.2026.12.01.08.55.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 08:55:22 -0800 (PST) From: Muchamad Coirul Anwar To: Jonathan Cameron , Miguel Ojeda , Igor Korotin , Danilo Krummrich Cc: David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , Andi Shyti , Daniel Almeida , Alice Ryhl , Mark Brown , Joshua Crofts , Markus Probst , Shuai Liu <847807867@qq.com>, linux-iio@vger.kernel.org, linux-i2c@vger.kernel.org, rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [RFC PATCH v8 1/4] rust: io: add FallibleIoCapable Date: Tue, 1 Dec 2026 23:55:05 +0700 Message-ID: <20261201165508.39336-2-muchamadcoirulanwar@gmail.com> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20261201165508.39336-1-muchamadcoirulanwar@gmail.com> References: <20261201165508.39336-1-muchamadcoirulanwar@gmail.com> X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add a fallible I/O capability for backends whose accesses may fail independently of address validation. Route try_read, try_write, try_write_reg, and try_update through the fallible capability. Provide a blanket implementation for infallible IoCapable backends so existing users retain their current behavior. This allows transport-backed implementations such as I2C to expose only fallible access while keeping infallible operations unavailable. Link: https://lore.kernel.org/all/DJVQ852J7SOH.26YBIJTQ9B66G@kernel.org/ Suggested-by: Danilo Krummrich Signed-off-by: Muchamad Coirul Anwar --- rust/kernel/io.rs | 62 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index a877df11a03e..b93a540c28da 100644 --- a/rust/kernel/io.rs +++ b/rust/kernel/io.rs @@ -356,6 +356,31 @@ pub trait IoCapable: IoBackend { fn io_write<'a>(view: Self::View<'a, T>, value: T); } +/// I/O backend capability for operations that may fail independently of address validation. +/// +/// Infallible backends implementing [`IoCapable`] also support fallible operations. Backends +/// implementing only this trait cannot use the infallible [`Io`] accessors. +pub trait FallibleIoCapable: IoBackend { + /// Read from `view`, returning a transport error if the operation fails. + fn io_try_read<'a>(view: Self::View<'a, T>) -> Result; + + /// Write `value` to `view`, returning a transport error if the operation fails. + fn io_try_write<'a>(view: Self::View<'a, T>, value: T) -> Result; +} + +impl, T> FallibleIoCapable for B { + #[inline(always)] + fn io_try_read<'a>(view: Self::View<'a, T>) -> Result { + Ok(Self::io_read(view)) + } + + #[inline(always)] + fn io_try_write<'a>(view: Self::View<'a, T>, value: T) -> Result { + Self::io_write(view, value); + Ok(()) + } +} + /// Trait indicating that an I/O backend supports memory copy operations. pub trait IoCopyable: IoBackend { /// Copy contents of `view` to `buffer`. @@ -759,7 +784,7 @@ fn copy_to_slice(self, data: &mut [u8]) fn try_read8(self, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_read(offset) } @@ -769,7 +794,7 @@ fn try_read8(self, offset: usize) -> Result fn try_read16(self, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_read(offset) } @@ -779,7 +804,7 @@ fn try_read16(self, offset: usize) -> Result fn try_read32(self, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_read(offset) } @@ -789,7 +814,7 @@ fn try_read32(self, offset: usize) -> Result fn try_read64(self, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_read(offset) } @@ -799,7 +824,7 @@ fn try_read64(self, offset: usize) -> Result fn try_write8(self, value: u8, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_write(offset, value) } @@ -809,7 +834,7 @@ fn try_write8(self, value: u8, offset: usize) -> Result fn try_write16(self, value: u16, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_write(offset, value) } @@ -819,7 +844,7 @@ fn try_write16(self, value: u16, offset: usize) -> Result fn try_write32(self, value: u32, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_write(offset, value) } @@ -829,7 +854,7 @@ fn try_write32(self, value: u32, offset: usize) -> Result fn try_write64(self, value: u64, offset: usize) -> Result where usize: IoLoc, - Self::Backend: IoCapable, + Self::Backend: FallibleIoCapable, { self.try_write(offset, value) } @@ -958,9 +983,12 @@ fn try_read(self, location: L) -> Result where T: AsReprMut, L: IoLoc, - Self::Backend: IoCapable<::Repr>, + Self::Backend: FallibleIoCapable<::Repr>, { - Ok(io_read!(self, try: location)) + let view = io_project!(self, try: location); + Ok(T::from_repr(Self::Backend::io_try_read(io_view_as_repr( + view, + ))?)) } /// Generic fallible write with runtime bounds check. @@ -991,10 +1019,10 @@ fn try_write(self, location: L, value: T) -> Result where T: AsRepr, L: IoLoc, - Self::Backend: IoCapable<::Repr>, + Self::Backend: FallibleIoCapable<::Repr>, { - io_write!(self, try: location, value); - Ok(()) + let view = io_project!(self, try: location); + Self::Backend::io_try_write(io_view_as_repr(view), T::into_repr(value)) } /// Generic fallible write of a fully-located register value. @@ -1037,7 +1065,7 @@ fn try_write_reg(self, value: V) -> Result T: AsRepr, L: IoLoc, V: LocatedRegister, - Self::Backend: IoCapable<::Repr>, + Self::Backend: FallibleIoCapable<::Repr>, { let (location, value) = value.into_io_op(); @@ -1071,12 +1099,12 @@ fn try_update(self, location: L, f: F) -> Result where T: AsReprMut, L: IoLoc, - Self::Backend: IoCapable<::Repr>, + Self::Backend: FallibleIoCapable<::Repr>, F: FnOnce(T) -> T, { let view = io_project!(self, try: location); - view.write_val(f(view.read_val())); - Ok(()) + let value = T::from_repr(Self::Backend::io_try_read(io_view_as_repr(view))?); + Self::Backend::io_try_write(io_view_as_repr(view), T::into_repr(f(value))) } /// Generic infallible read with compile-time bounds check. -- 2.50.0