From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796144124; b=lVQhXCl125w6ZAwAkDv49HemwkEq0R0HBppTJhIgI1eKSmY6DWHeCRXat8H2PFIWoSLn /hcdXTIr4ZemqlcneCNxoynVLP82dLtu8/PlkFURz0kQOQmQUOysFkY08iBeMm9amDv1m A8JX+naIA+xeBm7WtzwSMoYlsvfBEueMjBGn55F/GKyyPotLafc+NtIhe6YGqnQduXlfX gFmlRBU60Ltpy+coMIYaX7J9bEfs5WodOc42bHWxnkFL2IQwKpJeHJNlbjGnCVFDUaatW AHRInJIhhCJX2bs87X1QEdNRJoDGhT1+juYsXZu0guTZngWAzYcBki4JwjOANZPKU5A== 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=DSE0BpKajtHo6WXtm7/R6SyisUkXGIh9Wrbhc32gTs9OH01OFKj2t3MEEU5MQ+nt6H8C nSvfzPZw5jYIe17dNMQWHS/D1xyhn5yCBkUvubgaF9kaUyiSCcuGthsz4IcLVGMq/vmK+ Ki3eMtmlRCCOULh7upS+tgtu9KEfxqTreV5lnHRpeKq4ju+398ZL2jjFxgYYCcErVdPap cPjsfxKcwjoPfDUzi/4wR18KJNh17CfRtbk/EnUDWrjVZDbCH3S1igjwidbGT9mwc6z6y gapUGlDB61uvN9zo3Bsh8s7L3Cv9PhNsWm7uPG9jT8u++E8x/p4yEJ2k5GRcZVUCdaw== 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.182 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 263A960A2613 for ; Tue, 1 Dec 2026 16:55:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 263A960A2613 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-f182.google.com with SMTP id d9443c01a7336-2f046b0adc0so70371655ad.1 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=T10JJjLgwIGJBepVYSSoGU7yjxi3oKwqX8lbNnPTRr68fK0ZvzSl1pmNG6Eg2bOmMW ljg5N2xubTXW2tHmXPhuFHLf5m3riNkZCLcrCDCW82YtmOjYX35hj5tqfE2ZIXU+7uOv Z7pxP7IBQUiOP0EdkXK9ZTEF+B2ruoBy4R0vqPIgQGxPlPb7/wTgeV25iRKzUdK+Oufq cExcthJ37R0m1aCKKHbbswLCdFHyGUH5v9xHfG0dXYfVnvA+WZ7iLw3qZ/TGrbWG8rNQ y02Oj2mVUBUcbkbV+wYWk7Oceh4HjftHs7axyF2CR7B0VzSoMq5gGMr5rK8FhA1OuRZz P7TQ== X-Forwarded-Encrypted: i=1; AL+hErF3G1u9Ce0Z0TNt9L9BJ4CrBUyDG5liB5muEQc8MIhvyCiWOPtIvkTluCpTMVsrtHhYkS80jBWty83GDEvNlw==@vger.kernel.org X-Gm-Message-State: APuU4Z/MeNZx5rhrJYuHpPMA5wENvEPv7rEay78DnqT9hx62+xeHXmEZ pI8722U6LfEVKURqoiGuXLMh/ID6tPzRh1MgTenRV7x5/S8sVsD9eneL X-Gm-Gg: AZFdTZfiEAYpOAh4MQzhAQDPj/MbRK8gyflodfJKxNtA1Tx43jjPm/nd5FEUlbUJ7kW Sr0OOWXclBnxGuJ2GavpgGtWp5B9VaowfAJ4+DbMfSo5KBU3yHZnZUtPlKD1rffjz8G+LySwv9t 23T1DHfROwMpgVXqEPgvZtVX3LvvOmVRal3bmtBiqZPPssZ6NjR4MoZqj3Ngl6Zlc2S3GinChtJ 3Ko1CmAh3K5AjwgVhhDNPYx+zf1VXBYCDPAJKn/izmgY1ntUT80ZbBG9yJe2WcpZTgNPN9W9O+s VrU4GYlyM8aWznHB5QxEJRcd3a0aM+vvubZAA8MomOfpji7Zo2PBcDo2y2GCQkjeMTxmc5zw5Yf bh/YSZAmVNddchCSLeiqIgSUk8LsMQLN4opBTSDHEs3Fuc5BnEP225dY8BfK6WoXkO2lNanSlUT bEFM2Zk9xKsjiy8p3iO3QpFyrZ2MsvkGZZS528WY+zJpOs4ySmKnISK6iSiU2w6JWhSDp/c8BWH KaQHKqOfuHRTLavdoTkKlBj+HvaePzx+Hr/9xNXBKAGghoQBF9Lan3T4CGZv3h4780c5ePq7ZS9 iMRhfQI59scmnlM7+1jDJg605biVTLHxAiszFN5wMT0anDaFNx1zL1hnqPOwuXz7Oj6z 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: rust-for-linux@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