lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <902d6f21-a6c9-4776-9e55-26f089acd0da@gmail.com>
Date: Tue, 11 Mar 2025 19:45:18 +0200
From: Abdiel Janulgue <abdiel.janulgue@...il.com>
To: Andreas Hindborg <a.hindborg@...nel.org>
Cc: rust-for-linux@...r.kernel.org, daniel.almeida@...labora.com,
 dakr@...nel.org, robin.murphy@....com, aliceryhl@...gle.com,
 Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
 Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
 Björn Roy Baron <bjorn3_gh@...tonmail.com>,
 Benno Lossin <benno.lossin@...ton.me>, Trevor Gross <tmgross@...ch.edu>,
 Valentin Obst <kernel@...entinobst.de>, linux-kernel@...r.kernel.org,
 Christoph Hellwig <hch@....de>, Marek Szyprowski <m.szyprowski@...sung.com>,
 airlied@...hat.com, iommu@...ts.linux.dev
Subject: Re: [PATCH v13 4/7] rust: device: add dma addressing capabilities

Hi,

On 07/03/2025 22:12, Andreas Hindborg wrote:
> "Abdiel Janulgue" <abdiel.janulgue@...il.com> writes:
> 
>> Add functions to set the DMA mask to inform the kernel about the
>> device's DMA addressing capabilities.
>>
>> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@...il.com>
>> ---
>>   rust/helpers/dma.c     |  8 ++++++++
>>   rust/helpers/helpers.c |  1 +
>>   rust/kernel/device.rs  | 29 +++++++++++++++++++++++++++++
>>   3 files changed, 38 insertions(+)
>>   create mode 100644 rust/helpers/dma.c
>>
>> diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c
>> new file mode 100644
>> index 000000000000..8eb482386f93
>> --- /dev/null
>> +++ b/rust/helpers/dma.c
>> @@ -0,0 +1,8 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +
>> +#include <linux/dma-mapping.h>
>> +
>> +int rust_helper_dma_set_mask_and_coherent(struct device *dev, u64 mask)
>> +{
>> +	return dma_set_mask_and_coherent(dev, mask);
>> +}
>> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
>> index 0640b7e115be..8f3808c8b7fe 100644
>> --- a/rust/helpers/helpers.c
>> +++ b/rust/helpers/helpers.c
>> @@ -13,6 +13,7 @@
>>   #include "build_bug.c"
>>   #include "cred.c"
>>   #include "device.c"
>> +#include "dma.c"
>>   #include "err.c"
>>   #include "fs.c"
>>   #include "io.c"
>> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
>> index db2d9658ba47..f9d3d4f60ddb 100644
>> --- a/rust/kernel/device.rs
>> +++ b/rust/kernel/device.rs
>> @@ -6,10 +6,12 @@
>>
>>   use crate::{
>>       bindings,
>> +    error::Result,
>>       str::CStr,
>>       types::{ARef, Opaque},
>>   };
>>   use core::{fmt, ptr};
>> +use kernel::prelude::*;
>>
>>   #[cfg(CONFIG_PRINTK)]
>>   use crate::c_str;
>> @@ -187,6 +189,33 @@ pub fn property_present(&self, name: &CStr) -> bool {
>>           // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
>>           unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
>>       }
>> +
>> +    /// Inform the kernel about the device's DMA addressing capabilities.
>> +    ///
>> +    /// Set both the DMA mask and the coherent DMA mask to the same thing.
>> +    /// Note that we don't check the return value from the C `dma_set_coherent_mask`
>> +    /// as the DMA API guarantees that the coherent DMA mask can be set to
>> +    /// the same or smaller than the streaming DMA mask.
>> +    pub fn dma_set_mask_and_coherent(&mut self, mask: u64) -> Result {
>> +        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
>> +        let ret = unsafe { bindings::dma_set_mask_and_coherent(self.as_raw(), mask) };
>> +        if ret != 0 {
>> +            Err(Error::from_errno(ret))
>> +        } else {
>> +            Ok(())
>> +        }
>> +    }
> 
> I think we can use `Error::from_errno` here (and below). As far as I can
> tell, these C functions return negative on error.

This already uses `Error::from_errno`?

Thanks!

> 
>> +
>> +    /// Same as [`dma_set_mask_and_coherent`], but set the mask only for streaming mappings.
>> +    pub fn dma_set_mask(&mut self, mask: u64) -> Result {
>> +        // SAFETY: device pointer is guaranteed as valid by invariant on `Device`.
>> +        let ret = unsafe { bindings::dma_set_mask(self.as_raw(), mask) };
>> +        if ret != 0 {
>> +            Err(Error::from_errno(ret))
>> +        } else {
>> +            Ok(())
>> +        }
>> +    }
>>   }
>>
>>   // SAFETY: Instances of `Device` are always reference-counted.
> 
> 
> Best regards,
> Andreas Hindborg
> 
> 


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ