From nobody Fri Dec 19 00:27:25 2025 Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 738761DDC11; Mon, 2 Jun 2025 08:54:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748854495; cv=none; b=SAIbYJitx6WM49Z9RdcmvEDyfsZ3l2oI5dn3X8tK+mYOdYaMU5z+OKpDtifIbzqjoyCnKGlWnjLW7Ji8sknNfoK+7HjTxyg1gNIUpZdgb0SuiH8qeafdGgdXaq+hq7+r0t6hR5ExcXOaryEBFzHgbkQm13pOIka6Lj1f7fpyrQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748854495; c=relaxed/simple; bh=vdte7XjxyvO5sYF/1isE++FjO7g1ns05+zWEcuh9RwQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VZlqUj2/kTBJ1ww3TUzdyZzO6MLisXUOBsAU08LpM/nIwmgsrBHTmVXLc+adZmJNzxtQzRxNfNVQPnxDPKMoRs/7Vvo+Y0P8V6AQhARZm0BG8vh9YGy7d3DbhyiDvW0qP9518Pdl7K+FLNNSXtsg6msnaGywvkOhCbUdrhC/WW0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IgfDv43b; arc=none smtp.client-ip=209.85.167.52 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.b="IgfDv43b" Received: by mail-lf1-f52.google.com with SMTP id 2adb3069b0e04-55324587a53so5894182e87.0; Mon, 02 Jun 2025 01:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748854491; x=1749459291; 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; bh=mmm9TMxx1UWco/se+EJ0p1bxOhoMbO6epU94xaJakwI=; b=IgfDv43b+ijsCAV4j/rfwHNB+tQDhAUiM43p0rD6u2TRWoDGQTccFvAD/fiB/6TlbY /C7iR/HlAJi6AaS+DJ8e2WabwEp+OHX3gEKk2VI2D9l4enTFZrsD9hcqmgzSzpgFH4fn Y7T6MaJrO+h9jiC9DA6vlR5Y7gpBpo+y73BTShgBJtcsGH7AKaJTMQtZq5dpMuFme2iI vh4RkV8w2qn4rGjI8oogAr8ESZ4Fbl70B7uA4uOYqdjakUp8AvHn6/DPTyFHFZ3c/MRM DZ9FGZnss9zAUW2dQLptP5yW4SNivaHRzlni1bTQhg1ntHrELeXSN859D/Vt/U2J40ac LAig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748854491; x=1749459291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mmm9TMxx1UWco/se+EJ0p1bxOhoMbO6epU94xaJakwI=; b=gjhyC+YHVfDKcqUO8mCFxAWRfGiLX043rkLrFJ+eBo4SlIeh5z4I5GoXpn0FNEDXqh jccCIQx3Ko0gWDgu3CqPsIPKCBuNoH1M4nY2Hu/GYClga79xunfBQijWkmiVn92bus8m V8YO8ABznRvovjSixCBdhkx4zcQ05ZxIZUAHknSSw1hQrA5rEpiWmFHR6IJJSSH1u5bs 6p3GZBWjTqPGXNT2Qb+kAlZpMEHe4fMilQ/fJvYC2hFdfo813VfzFxsQEr7/UIqCRbEL 0CjdUHN7O6dF3bfZVWzWpg8Zj4PRQJoqxdUWwsO6d2tisiZfOwa8YFS6XMKvLuTSeXP1 7f3A== X-Forwarded-Encrypted: i=1; AJvYcCViaIh4xzk9dGqjTfw4kBK3VKnKohScTQ6aHXaZxI6vm/dSNTBywruGv7ACizbOm7j3jTtOIiW2Y9KTjqOwfjc=@vger.kernel.org, AJvYcCXYt/0+ug5JzEAxTW2k3phrHaJacmgq1uSXzmA5VQGpH4HkdLhnKYt5TgX/ZqiUIUrJ7tq+1gH1RkhXT0I=@vger.kernel.org X-Gm-Message-State: AOJu0YwL9KSoEkaFZ3trMS7K4JMmo5/CBJXKzW/apTrARmD88YsbRRKY fL9dmc1s8tja3quiYxGZXIsTy9fB7tPML24a6Vj3mW+hfyKBGv+cYspR X-Gm-Gg: ASbGncsZcbfpOC9sUHx9Nf53vcNXPEkORbD+JR4IOuMU4X3XSJVeEc7P/1D505J1bDx QdKRMbpGAH6nIiwgIif7TXh6ntyU00GzzD8lpJPRT0xev8mRZNLxhQWvUvU5rg4jdf8SrbpXtg0 afUAq7gtjBeYeY1cRUt8gn6y3Fu62kH14wQsMVQ3Js86FUHKkeZ/AHczUNBxiICV8thcpDz2niW W4EP6yQzDsGdzzZeAAKkpL+rMTPlOJDOFggYpTeJlRKGMl9efmcxrzXaao8ytMzyNx6fSwl3nwR hbevtgLwXkXOOe591X0OhXXSAG7OD0ojNMxhN172vMyec3Nw11WLg6iTfDHyykbIAEuOoQ0kgji jtoaXcoK0nhJAGk7oQ/A3zsX2XoIE X-Google-Smtp-Source: AGHT+IGeQ2VJfS2pSQq9y4fFCIu/d3EP9UcOUYcg/V5Nm0QONGa3Ygn6eN+EP+hDSxJMw7LdEHQWFA== X-Received: by 2002:a05:6512:1108:b0:553:2927:9864 with SMTP id 2adb3069b0e04-5533ba23ab1mr3493372e87.6.1748854491275; Mon, 02 Jun 2025 01:54:51 -0700 (PDT) Received: from abj-NUC9VXQNX.. (dsl-hkibng22-54f8dc-251.dhcp.inet.fi. [84.248.220.251]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-553378a13basm1524325e87.81.2025.06.02.01.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Jun 2025 01:54:50 -0700 (PDT) From: Abdiel Janulgue To: acourbot@nvidia.com, dakr@kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Valentin Obst , linux-kernel@vger.kernel.org (open list), Marek Szyprowski , Robin Murphy , airlied@redhat.com, rust-for-linux@vger.kernel.org, iommu@lists.linux.dev (open list:DMA MAPPING HELPERS), Petr Tesarik , Andrew Morton , Herbert Xu , Sui Jingfeng , Randy Dunlap , Michael Kelley , Abdiel Janulgue Subject: [PATCH v4 1/3] rust: dma: clarify wording and be consistent in `coherent` nomenclature Date: Mon, 2 Jun 2025 11:53:11 +0300 Message-ID: <20250602085444.1925053-2-abdiel.janulgue@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> References: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the kernel, `consistent` and `coherent` are used interchangeably for the region described in this api. Stick with `coherent` nomenclature to show that dma_alloc_coherent() is being used, in addition to improving the clarity in the DMA mapping attributes documentation. Reviewed-by: Benno Lossin Signed-off-by: Abdiel Janulgue Reviewed-by: Andreas Hindborg --- rust/kernel/dma.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 35fd8a638473..c3fe39e7eb0b 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -89,7 +89,7 @@ pub mod attrs { /// Forces contiguous allocation of the buffer in physical memory. pub const DMA_ATTR_FORCE_CONTIGUOUS: Attrs =3D Attrs(bindings::DMA_ATT= R_FORCE_CONTIGUOUS); =20 - /// This is a hint to the DMA-mapping subsystem that it's probably not= worth the time to try + /// Hints DMA-mapping subsystem that it's probably not worth the time = to try /// to allocate memory to in a way that gives better TLB efficiency. pub const DMA_ATTR_ALLOC_SINGLE_PAGES: Attrs =3D Attrs(bindings::DMA_A= TTR_ALLOC_SINGLE_PAGES); =20 @@ -97,7 +97,7 @@ pub mod attrs { /// `__GFP_NOWARN`). pub const DMA_ATTR_NO_WARN: Attrs =3D Attrs(bindings::DMA_ATTR_NO_WARN= ); =20 - /// Used to indicate that the buffer is fully accessible at an elevate= d privilege level (and + /// Indicates that the buffer is fully accessible at an elevated privi= lege level (and /// ideally inaccessible or at least read-only at lesser-privileged le= vels). pub const DMA_ATTR_PRIVILEGED: Attrs =3D Attrs(bindings::DMA_ATTR_PRIV= ILEGED); } @@ -105,7 +105,7 @@ pub mod attrs { /// An abstraction of the `dma_alloc_coherent` API. /// /// This is an abstraction around the `dma_alloc_coherent` API which is us= ed to allocate and map -/// large consistent DMA regions. +/// large coherent DMA regions. /// /// A [`CoherentAllocation`] instance contains a pointer to the allocated = region (in the /// processor's virtual address space) and the device address which can be= given to the device @@ -115,7 +115,7 @@ pub mod attrs { /// # Invariants /// /// For the lifetime of an instance of [`CoherentAllocation`], the `cpu_ad= dr` is a valid pointer -/// to an allocated region of consistent memory and `dma_handle` is the DM= A address base of +/// to an allocated region of coherent memory and `dma_handle` is the DMA = address base of /// the region. // TODO // @@ -138,7 +138,7 @@ pub struct CoherentAllocation { } =20 impl CoherentAllocation { - /// Allocates a region of `size_of:: * count` of consistent memory. + /// Allocates a region of `size_of:: * count` of coherent memory. /// /// # Examples /// --=20 2.43.0 From nobody Fri Dec 19 00:27:25 2025 Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 95D7E1DE4EC; Mon, 2 Jun 2025 08:54:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748854496; cv=none; b=Po50qCq2qD/xJoI7+qny+jq+d4OFQLDqovnvmvQHYjq11zo8NFP3IxSmwWvTjx6k21UE1qIzJxtZFC1wjo1775sgc9WYyBZW5CwLs+f0rEH4KXrdky04t2fgL4+1hOGfS1gnzbmn/n9+l1dcicVBM99NdxwJbfKWqi5AnUZRx1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748854496; c=relaxed/simple; bh=aVxlq706IujBRj+HiQ3ah/IIAv77R+CvWdXj/E/ybvI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uaGNWmzmHTgwicPQZu4sZO6qER71jZ+9MHc5KCgc+vfCQRCnt3AmdY5ZqTjxLfroQgNtvs5F9FZlSxiGRWEdIfYeoaFW5vznA4P3AAQO5SMo4yVw0h8UF1vpyP2d2eA0Ur1E1iPm6GN8UNl4AI4q1QQCvFKIL1Wh/KIZx+WDo9c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AFLxrG2B; arc=none smtp.client-ip=209.85.167.46 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.b="AFLxrG2B" Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-5532f6d184eso5252381e87.0; Mon, 02 Jun 2025 01:54:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748854493; x=1749459293; 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; bh=oRX6ycinlDpWw2cKVx7yjaGxT6xMop9u+LnJF5PNiJQ=; b=AFLxrG2BCpV/8CuShJOahxs9CEC64nryKEr5/8YbkmpucZ/Q7dA4jMj7QmlwO2x9eo t9mY0gqkltnyu37OY+wSKMtEYofTt+ZKQsKSSXEAoFRzQNJ2t2icGXIwMJPlrDkYtVzp oVioK1eIW6aY5YkeMCWyqsLW9/wRV7i3aBCQK9CbxZftATTcPGFZZF69hP0AanggkuB7 moLKnPyA3NLarQ1QcUL0hreaMb0q4LeYNK5nKmPmL8QudMFbJ02JgEKRv6/IbDuNQdJD LjTXVbXnLVdWz8Ll75EHcBDOs7gWvvehQ0xVbB+rRDFfgbXk0W+SXk6+VamrtTMeZNGr /YNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748854493; x=1749459293; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oRX6ycinlDpWw2cKVx7yjaGxT6xMop9u+LnJF5PNiJQ=; b=mIt8n8mWlWuwzWy/uKP+6y53B970/0UGXjlt+tRTWPkh6E9CFjik1/fqrLNhj6IN7t mVH0nQ5uqe2AGVrCJ/AsDJFPxP/xMT9IByu8tExUDTJINJVXMvSq4Qlhw1JNqumrTXOJ O5wWRXfHRkR7fpE1aVMzyonppIiWNpAv1vTyl+tUDAYBgfJOFBTewgALk6AXQ/zPjCXh 8t1s3oaD3P8CqIn69asymMEY3pB7tnjZv2un+vzuimt/UL9YmI3pfnd6Eq+IJggr3QqT jMYXmuNU5LP/dTmlIPgDvESZ/uEqTTybpAKzv9rmlaKVJDMyxWzGP6N5XIH+Vt30DOhW o8vw== X-Forwarded-Encrypted: i=1; AJvYcCVn21Q/bDJNkD/fiedgMROT6OPSUYkMh+uCyswyfSHRStu+vRplOwc4b36CliQ6vcqGsZ//DQe3Wdao6nmmNQ0=@vger.kernel.org, AJvYcCWsVerTo19/2tYp56Da6QV9+G/rK2kJaOvwD+BG7u9CFzk4Byp6+fZnYMXrNoyNF5r/IfemOtoxpcMY+DM=@vger.kernel.org X-Gm-Message-State: AOJu0YxF9smeQX0WNbfOJaJexXYKkEdtiOtMonUwHew+0s6BHO9wYIXO AuO5ibLTAzlJwDCPK1YfYMuyZG8nuLT0++IMaMwV50pEkQ3WnJ8z7N2K X-Gm-Gg: ASbGncvBQRjpcWpJmTIPv0YPm6Tnbh7UWitPIZQCKuq0FcTObUvUn83P1F3KYEZhqgz 0PCYFyheCs9ueb0N9SnbDrgrj0cHEbRpU6cW2kAQrFq1wAjzwUvgoPdaTMlxr6MtDEvpy5she0T HgxsJTCo+TIAlsMoEAWvrBzjzzjhcnESsTUoAWkAyp/DaRGRdde7rsOJu3e+ntOLejQ1fOJxh9Z Qwtzf2myJ32DRO5kMxh/rZkqMBQ6oDWax9URNspZf2ew3XCGLC/LipZgzyceYd5DG8652Nkhrfg VqDZL7CnWo6SJF/3jQEicv4t/EGszgGYUplJv253rK3iqVo6d4666fV4LnCTjRgyLfmf3MmWZ/b 228PzkaOYhUIcHjAluiJb8LeHqT4V X-Google-Smtp-Source: AGHT+IG2LmHus1rr8ydQkHeOUpuq/nYTVQnVNjpmNMi+kbH3v2KVxyMZpriwJL9CZN4aoHvOpzgf9A== X-Received: by 2002:a05:6512:1328:b0:54e:81ec:2c83 with SMTP id 2adb3069b0e04-5533b8f412cmr3757007e87.18.1748854492276; Mon, 02 Jun 2025 01:54:52 -0700 (PDT) Received: from abj-NUC9VXQNX.. (dsl-hkibng22-54f8dc-251.dhcp.inet.fi. [84.248.220.251]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-553378a13basm1524325e87.81.2025.06.02.01.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Jun 2025 01:54:51 -0700 (PDT) From: Abdiel Janulgue To: acourbot@nvidia.com, dakr@kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Valentin Obst , linux-kernel@vger.kernel.org (open list), Marek Szyprowski , Robin Murphy , airlied@redhat.com, rust-for-linux@vger.kernel.org, iommu@lists.linux.dev (open list:DMA MAPPING HELPERS), Petr Tesarik , Andrew Morton , Herbert Xu , Sui Jingfeng , Randy Dunlap , Michael Kelley , Abdiel Janulgue Subject: [PATCH v4 2/3] rust: dma: convert the read/write macros to return Result Date: Mon, 2 Jun 2025 11:53:12 +0300 Message-ID: <20250602085444.1925053-3-abdiel.janulgue@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> References: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We could do better here by having the macros return `Result`, so that we don't have to wrap these calls in a closure for validation which is confusing. Co-developed-by: Andreas Hindborg Signed-off-by: Andreas Hindborg Link: https://lore.kernel.org/rust-for-linux/87h63qhz4q.fsf@kernel.org/ Reviewed-by: Andreas Hindborg Signed-off-by: Abdiel Janulgue --- rust/kernel/dma.rs | 54 +++++++++++++++++++++++----------------- samples/rust/rust_dma.rs | 28 +++++++++++---------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index c3fe39e7eb0b..5a690e5f1e66 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -328,20 +328,22 @@ unsafe impl Send for C= oherentAllocation {} #[macro_export] macro_rules! dma_read { ($dma:expr, $idx: expr, $($field:tt)*) =3D> {{ - let item =3D $crate::dma::CoherentAllocation::item_from_index(&$dm= a, $idx)?; - // SAFETY: `item_from_index` ensures that `item` is always a valid= pointer and can be - // dereferenced. The compiler also further validates the expressio= n on whether `field` - // is a member of `item` when expanded by the macro. - unsafe { - let ptr_field =3D ::core::ptr::addr_of!((*item) $($field)*); - $crate::dma::CoherentAllocation::field_read(&$dma, ptr_field) - } + (|| -> ::core::result::Result<_, $crate::error::Error> { + let item =3D $crate::dma::CoherentAllocation::item_from_index(= &$dma, $idx)?; + // SAFETY: `item_from_index` ensures that `item` is always a v= alid pointer and can be + // dereferenced. The compiler also further validates the expre= ssion on whether `field` + // is a member of `item` when expanded by the macro. + unsafe { + let ptr_field =3D ::core::ptr::addr_of!((*item) $($field)*= ); + ::core::result::Result::Ok($crate::dma::CoherentAllocation= ::field_read(&$dma, ptr_field)) + } + })() }}; ($dma:ident [ $idx:expr ] $($field:tt)* ) =3D> { - $crate::dma_read!($dma, $idx, $($field)*); + $crate::dma_read!($dma, $idx, $($field)*) }; ($($dma:ident).* [ $idx:expr ] $($field:tt)* ) =3D> { - $crate::dma_read!($($dma).*, $idx, $($field)*); + $crate::dma_read!($($dma).*, $idx, $($field)*) }; } =20 @@ -368,24 +370,30 @@ macro_rules! dma_read { #[macro_export] macro_rules! dma_write { ($dma:ident [ $idx:expr ] $($field:tt)*) =3D> {{ - $crate::dma_write!($dma, $idx, $($field)*); + $crate::dma_write!($dma, $idx, $($field)*) }}; ($($dma:ident).* [ $idx:expr ] $($field:tt)* ) =3D> {{ - $crate::dma_write!($($dma).*, $idx, $($field)*); + $crate::dma_write!($($dma).*, $idx, $($field)*) }}; ($dma:expr, $idx: expr, =3D $val:expr) =3D> { - let item =3D $crate::dma::CoherentAllocation::item_from_index(&$dm= a, $idx)?; - // SAFETY: `item_from_index` ensures that `item` is always a valid= item. - unsafe { $crate::dma::CoherentAllocation::field_write(&$dma, item,= $val) } + (|| -> ::core::result::Result<_, $crate::error::Error> { + let item =3D $crate::dma::CoherentAllocation::item_from_index(= &$dma, $idx)?; + // SAFETY: `item_from_index` ensures that `item` is always a v= alid item. + unsafe { $crate::dma::CoherentAllocation::field_write(&$dma, i= tem, $val) } + ::core::result::Result::Ok(()) + })() }; ($dma:expr, $idx: expr, $(.$field:ident)* =3D $val:expr) =3D> { - let item =3D $crate::dma::CoherentAllocation::item_from_index(&$dm= a, $idx)?; - // SAFETY: `item_from_index` ensures that `item` is always a valid= pointer and can be - // dereferenced. The compiler also further validates the expressio= n on whether `field` - // is a member of `item` when expanded by the macro. - unsafe { - let ptr_field =3D ::core::ptr::addr_of_mut!((*item) $(.$field)= *); - $crate::dma::CoherentAllocation::field_write(&$dma, ptr_field,= $val) - } + (|| -> ::core::result::Result<_, $crate::error::Error> { + let item =3D $crate::dma::CoherentAllocation::item_from_index(= &$dma, $idx)?; + // SAFETY: `item_from_index` ensures that `item` is always a v= alid pointer and can be + // dereferenced. The compiler also further validates the expre= ssion on whether `field` + // is a member of `item` when expanded by the macro. + unsafe { + let ptr_field =3D ::core::ptr::addr_of_mut!((*item) $(.$fi= eld)*); + $crate::dma::CoherentAllocation::field_write(&$dma, ptr_fi= eld, $val) + } + ::core::result::Result::Ok(()) + })() }; } diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs index 874c2c964afa..9e05d5c0cdae 100644 --- a/samples/rust/rust_dma.rs +++ b/samples/rust/rust_dma.rs @@ -54,13 +54,9 @@ fn probe(pdev: &pci::Device, _info: &Self::IdInfo)= -> Result =3D CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.= len(), GFP_KERNEL)?; =20 - || -> Result { - for (i, value) in TEST_VALUES.into_iter().enumerate() { - kernel::dma_write!(ca[i] =3D MyStruct::new(value.0, value.= 1)); - } - - Ok(()) - }()?; + for (i, value) in TEST_VALUES.into_iter().enumerate() { + kernel::dma_write!(ca[i] =3D MyStruct::new(value.0, value.1))?; + } =20 let drvdata =3D KBox::new( Self { @@ -78,13 +74,19 @@ impl Drop for DmaSampleDriver { fn drop(&mut self) { dev_info!(self.pdev.as_ref(), "Unload DMA test driver.\n"); =20 - let _ =3D || -> Result { - for (i, value) in TEST_VALUES.into_iter().enumerate() { - assert_eq!(kernel::dma_read!(self.ca[i].h), value.0); - assert_eq!(kernel::dma_read!(self.ca[i].b), value.1); + for (i, value) in TEST_VALUES.into_iter().enumerate() { + let val0 =3D kernel::dma_read!(self.ca[i].h); + let val1 =3D kernel::dma_read!(self.ca[i].b); + assert!(val0.is_ok()); + assert!(val1.is_ok()); + + if let Ok(val0) =3D val0 { + assert_eq!(val0, value.0); + } + if let Ok(val1) =3D val1 { + assert_eq!(val1, value.1); } - Ok(()) - }(); + } } } =20 --=20 2.43.0 From nobody Fri Dec 19 00:27:25 2025 Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 8F1211DEFDD; Mon, 2 Jun 2025 08:54:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748854497; cv=none; b=rM/lnpqkEUlBDn5OOCv1u+JItC21k0zdf5FWZUrfFQEzmccyt7PWicdfZlKLBUQtrkZz5Tf8jjLxpXlvI0L5E2Uk4eNLziTvH1Y1wI6pxPbZIX6IabPIXcFQocOv6cmfqOdnHSriDWthYvSP0SLif1DTHgwCa4UohlSsz7KMQDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748854497; c=relaxed/simple; bh=N7DR+bUmuN4+fswwjtPRhudtA1jd7yX7JIbNPakSJqg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TLThK+fybBsLNVvdzhYcMDGWi5wz/hyPQ+++waRWpk7iXTdu32QpdP1Vj3BndpwNzTKHSPNl91GyI2hcW7oBoG53UBf8dUZe+GwLHJwPTaYhyf5GA0vwKKnUJ8z/3H5LdHm6ojpkKoM/DOwCk8npD0JMIFy4lW9kGqS4BYcmf6k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Q2ZS50pW; arc=none smtp.client-ip=209.85.167.50 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.b="Q2ZS50pW" Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-54b09cb06b0so5512300e87.1; Mon, 02 Jun 2025 01:54:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748854493; x=1749459293; 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; bh=eNjzh0lCljAImtPQH0yiCxzp0Zoe2cG4gqiqoZ8GiaA=; b=Q2ZS50pWMZYAImdErYVKlTyH3V0y5f2v7hn5WcLgCtgM8fuuyLblNqxTyHSsN2e68O 7qD8DAW932eFSFWf3LXMq3kTQW/zPzLhlb3TbqW01ltRyFuPUfZfQGy/Xgwj+t+5XfdV /aH1lYxfo0GSHBdgKKTeInLU2/1XDQ/iLwqzElJy0QqYymrwI0i7rFtbXEo0lXxQwg7t 1XL6PIb1aDK4UkD6sh8pLIoZ+WzpvGJu7bj0Elhn+b0gArg9UzMPwMJOQ0iddZSo5hoN Xv1a4AR9e3N0kegyAnQZJcsr9dgEpdt9nfrgkwrjEBJOZJbOFtjWEHJ/vLzkLLtARRsu 0fHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748854493; x=1749459293; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eNjzh0lCljAImtPQH0yiCxzp0Zoe2cG4gqiqoZ8GiaA=; b=sBrvTdlyHtUNI1HoCySOQfYuIYjduz8sSvCJo/qtPBNLLYwtiaXZnrRU4GHZSLUwd6 04ATbNb9SLrB0eUcGPJ+xeQae2HUmddYOhkw0gB0qb73GmQwQ0CBXuURLCUrGzJ9crMJ LmLjTUJFIdwlLZV6Bl+ncBWpSYn476NuwDQBzqGP2qYsMeBvtqkEBIlSYrmAbsdi/DcS Y77YKO5Q4s3wYEoA2OdzBXKU4qqad9eXfWn2SRXyiZb5YIf3dKFmRRbEHiJDDFa0MCFO fQzXPh/VMIrBSNEfsVecjqirWTxuF0zSZ8Pys2bPI3KKa6Okr5eKfeJ8qtr1kdsaefw7 Zkqg== X-Forwarded-Encrypted: i=1; AJvYcCVB20W/Y9vPCqBUILrg0m0Ji+uQwBVUVF3gbd5hUoyzKVliECM8BhLjdEkBo2iMLGKL67HpD0z66jcKlkmCgFI=@vger.kernel.org, AJvYcCVtDVRL+c+Qn3+6oXthOjE9EtmrsV2279ZQWPMZB4pgwz9qW8JJp8MLoyBtH3tqyQSjsGEpSLvPgjvxamw=@vger.kernel.org X-Gm-Message-State: AOJu0YypxLlKOCuC2sZYcu3piZmulLoPzJppscocmF1wEx2gkiCubFxA aitMTAdG2JEBesDZ/AhTHbZg2RF1QKR7IAWU3SHL7L1zvxHl+1vlVuE0 X-Gm-Gg: ASbGncuEOXDb2exJJSI47BOsqEbPeQbkxYvVFofREUsz8ojjdR9MQx0zsW4sIxd28JE vIzEFpqDcaAzJ+MWnt8mtRwrahNic4QYpydk7ct8Snvvh1I9OhjQTAT5xyXh5jgSjWI+wKswOEu ysT+XOj4kZqvuvu+EZxwdWiYZiEATXjH0ftZQf1JHzeszGYTtQ6RqsuCTmJ2v50Yi9+Efcq/jF8 EV0dBl2NP+gkH6G9OBIg0wCXmykxRFOyIYQ0XG6G7PM03wRxepfsVMnJilM/F6rRxnUNuoGq7rr ak/69abZHa8pqis2O9duzbHy6lOP298s1jBhD7+F/DftRmwOdxL1Xwbsn2OqOSZ3gfrbZRJsPBQ 3bWa/F9awBjmCsdZajL0bqMKgcmtm X-Google-Smtp-Source: AGHT+IENRfJOf3u52FspEjXhewCzmDRxrviHPyUYBNoN+lgkyQ/r5Trmcti6p6EUjGTFWv55XZjmlQ== X-Received: by 2002:a05:6512:23a6:b0:553:2f33:ac0e with SMTP id 2adb3069b0e04-55343190b94mr1983224e87.41.1748854493216; Mon, 02 Jun 2025 01:54:53 -0700 (PDT) Received: from abj-NUC9VXQNX.. (dsl-hkibng22-54f8dc-251.dhcp.inet.fi. [84.248.220.251]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-553378a13basm1524325e87.81.2025.06.02.01.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Jun 2025 01:54:52 -0700 (PDT) From: Abdiel Janulgue To: acourbot@nvidia.com, dakr@kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Valentin Obst , linux-kernel@vger.kernel.org (open list), Marek Szyprowski , Robin Murphy , airlied@redhat.com, rust-for-linux@vger.kernel.org, iommu@lists.linux.dev (open list:DMA MAPPING HELPERS), Petr Tesarik , Andrew Morton , Herbert Xu , Sui Jingfeng , Randy Dunlap , Michael Kelley , Abdiel Janulgue Subject: [PATCH v4 3/3] rust: dma: add as_slice/write functions for CoherentAllocation Date: Mon, 2 Jun 2025 11:53:13 +0300 Message-ID: <20250602085444.1925053-4-abdiel.janulgue@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> References: <20250602085444.1925053-1-abdiel.janulgue@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add unsafe accessors for the region for reading or writing large blocks of data. Reviewed-by: Andreas Hindborg Signed-off-by: Abdiel Janulgue Reviewed-by: Alexandre Courbot --- rust/kernel/dma.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 5a690e5f1e66..b486f63c1d3a 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -218,6 +218,92 @@ pub fn dma_handle(&self) -> bindings::dma_addr_t { self.dma_handle } =20 + /// Common helper to validate a range applied from the allocated regio= n in the CPU's virtual + /// address space. + fn validate_range(&self, offset: usize, count: usize) -> Result + { + if offset.checked_add(count).ok_or(EOVERFLOW)? > self.count { + return Err(EINVAL); + } + Ok(()) + } + + /// Returns the data from the region starting from `offset` as a slice. + /// `offset` and `count` are in units of `T`, not the number of bytes. + /// + /// For ringbuffer type of r/w access or use-cases where the pointer t= o the live data is needed, + /// [`CoherentAllocation::start_ptr`] or [`CoherentAllocation::start_p= tr_mut`] could be used instead. + /// + /// # Safety + /// + /// * Callers must ensure that the device does not read/write to/from = memory while the returned + /// slice is live. + /// * Callers must ensure that this call does not race with a write to= the same region while + /// the returned slice is live. + pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result<&= [T]> { + self.validate_range(offset, count)?; + // SAFETY: + // - The pointer is valid due to type invariant on `CoherentAlloca= tion`, + // we've just checked that the range and index is within bounds.= The immutability of the + // data is also guaranteed by the safety requirements of the fun= ction. + // - `offset + count` can't overflow since it is smaller than `sel= f.count` and we've checked + // that `self.count` won't overflow early in the constructor. + Ok(unsafe { core::slice::from_raw_parts(self.cpu_addr.add(offset),= count) }) + } + + /// Performs the same functionality as [`CoherentAllocation::as_slice`= ], except that a mutable + /// slice is returned. + /// + /// # Safety + /// + /// * Callers must ensure that the device does not read/write to/from = memory while the returned + /// slice is live. + /// * Callers must ensure that this call does not race with a read or = write to the same region + /// while the returned slice is live. + pub unsafe fn as_slice_mut(&self, offset: usize, count: usize) -> Resu= lt<&mut [T]> { + self.validate_range(offset, count)?; + // SAFETY: + // - The pointer is valid due to type invariant on `CoherentAlloca= tion`, + // we've just checked that the range and index is within bounds.= The immutability of the + // data is also guaranteed by the safety requirements of the fun= ction. + // - `offset + count` can't overflow since it is smaller than `sel= f.count` and we've checked + // that `self.count` won't overflow early in the constructor. + Ok(unsafe { core::slice::from_raw_parts_mut(self.cpu_addr.add(offs= et), count) }) + } + + /// Writes data to the region starting from `offset`. `offset` is in u= nits of `T`, not the + /// number of bytes. + /// + /// # Safety + /// + /// * Callers must ensure that the device does not read/write to/from = memory while the returned + /// slice is live. + /// * Callers must ensure that this call does not race with a read or = write to the same region + /// that overlaps with this write. + /// + /// # Examples + /// + /// ``` + /// # fn test(alloc: &mut kernel::dma::CoherentAllocation) -> Resu= lt { + /// let somedata: [u8; 4] =3D [0xf; 4]; + /// let buf: &[u8] =3D &somedata; + /// // SAFETY: No hw operation on the device and no other r/w access t= o the region at this point. + /// unsafe { alloc.write(buf, 0)?; } + /// # Ok::<(), Error>(()) } + /// ``` + pub unsafe fn write(&self, src: &[T], offset: usize) -> Result { + self.validate_range(offset, src.len())?; + // SAFETY: + // - The pointer is valid due to type invariant on `CoherentAlloca= tion` + // and we've just checked that the range and index is within bou= nds. + // - `offset + count` can't overflow since it is smaller than `sel= f.count` and we've checked + // that `self.count` won't overflow early in the constructor. + unsafe { + core::ptr::copy_nonoverlapping(src.as_ptr(), self.cpu_addr.add= (offset), src.len()) + }; + Ok(()) + } + /// Returns a pointer to an element from the region with bounds checki= ng. `offset` is in /// units of `T`, not the number of bytes. /// --=20 2.43.0