From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796144124; b=fhwk5Msku+lzugu4yY2wMMZAWgwryv/nvNlicd94gEW0QkPxeUY/N4LjCMwOip2D0r/C yeJ8ZmYsX9p2YvkIr/tsB0vUzsh5iwHmmukvfdPjUI0lcEbw2I9SvL3aMVBtbv7wkGwbr QKMb8CXL/ViBp78VSW7NYR8kfQ7rzBP8rZoLKFcQlXNSToUUhBRd/P04FKenduVFkRttf NJgrFRMpR7is2L4tTL6XYJzuw84PzKJDztqxKpmFTEWGMK1JAKmrQfY752BEKPqhoi+5V MdQ8Nc5NvMA+R+2vP8liLJ/fRg+3Bwsf/3FbNZj0XTG7Enw8eO/wgDGzB+HRXtDYFFQ== 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=EmbXmOzdbSydCRVMNCYOPqDi5hz4iDqwrF55OKOlvMB0iQ7N/XJqKSl+8cVahxEXc0Bg 2lzyBlrB2BkuGVJu2nhXz34RNE1w985qfGnePOf57m5fyEPDpxVApH1x4xEXooxGaM59r L+3+VwoDFOrJLXr0cogXW6NMDLEgh9+DPb1kROaz9sOv76wJJsmOYPI7IO+Wo+Ompm2H7 /4Cw/rclk+lQMPxo1RE0MeJxKPVRBgJ+bPTs0q/pw4HVwzq+rSJWx0EtLHHWyks9Qrw4E UASGJtfw3TkAs2JsQvzY9GE3G6ZTArcKoHsg3QyobygYACWYHV/TzfqDock+XEGDg3Q== 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.214.170 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 1FF5E606A6AF for ; Tue, 1 Dec 2026 16:55:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1FF5E606A6AF 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-pl1-f170.google.com with SMTP id d9443c01a7336-2f06ff94acfso58266785ad.0 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=ZG3InOfElT8RKFBtRo/GvHg1iqh/sKO5suIGIHA7H0sPKSC7gGjn2GHJf48GSDOM+o hCxa1kyXz5F+bfakHHQeaHkro1uV3VSu9E8yx+qAzuhSLu3BpSb9qmRDeCsQm4+Bp7RQ PPm3OMzP8Vvb5axknrHNiF0SH7UrEjNjXpY9AWhfM6aVoZFs8Z1y69660ElMm0Z0rmZ1 Zebbd1PbZz9mswLgP/UJGdsOtnBMg5FA2eArujSxfr4FaPw5ee3Q1eInG+6185MtfpWd JkDX7hqSyk3LJ60a/cdw5KcyHejXTz2vjGjjKBXc5bU9fzTFBHaS1fV5AcNNqOT+sYqH 6rJA== X-Forwarded-Encrypted: i=1; AL+hErGzZpAaSMhtxztEa7lgk0gDz5ie6mfcINW5vKuMb9NXXY2utUW9iY1TtMXVjLvDYkni5JEY12p0odPNXco=@vger.kernel.org X-Gm-Message-State: APuU4Z8yb20vjZIpF3IdKELAiWP0Ef9N+eWdYdLy42bZOBJZET4lRZYQ FOwvgXmG6v2g3F/TvIBbIz+KGIAoDmu2tybAoE8dcIgxsXXPuHWzrOMf X-Gm-Gg: AZFdTZf5P+qPVaaiOt1FllsUlKuXJGyFEHIO2qd1VnZPrBbJAoTDy3c/uTBw8bGlg6n h8U8fahZnBXByxEnumRtDVwkfMDghXdEM2QBS4fUhqRP+eYzeyL1yLxovGihWT/wqp74LmOjxlu SRDUEeiDEs3evNRGSWs9ma69vqh123wu57xocJyDF6E74/jpXlZYyXPGrWSScWbl20zKSaTVerz Pr5qCLnG/uh01jBgW4rEbEGZfEf7GPRYy95pUBet4eYOUkNpkjpP0zaewFdv0R6BYEUR6r2QbJp 0/pzSym3/pPSqByQlbw4vwJ2/tfqa19hKsplN3X34rcOwuKydAy/pMTci44cg2HuxqAy5m1KRTA qH5ri9P9if799ga4Z46+pklCNLx+OznB1V2C/8yYExosNztGJp0Vz8+2dswt6iFDFSSdcJ65D6h W+w8tbi0zCyq5IsPnq0XA6jNnNwiDPlHC/c0VlirKfEOmlcSBB24xdtyUyyf2czIYtkURy1HW38 PHmHeEFnzD0rs9+W5LoAzs6NgTdspDHP5VeeQlqtr0YODlPGxmX2Qgw3zSnW3qLhskspUXrubgz 685DgBXgpI3Q3p2tvFNaGPVHzVK9jVMFL/MiGCqO9dlbKx1IXFo9vaQFrWO5SW1ZfpKA 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-kernel@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