Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: dma: use NonNull<T> instead of *mut T

In struct CoherentAllocation, use NonNull<T> instead of a raw *mut T for
the CPU address; the CPU address of a valid CoherentAllocation won't
ever be NULL.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Link: https://patch.msgid.link/20251103190655.2326191-2-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+8 -9
+8 -9
rust/kernel/dma.rs
··· 12 12 sync::aref::ARef, 13 13 transmute::{AsBytes, FromBytes}, 14 14 }; 15 + use core::ptr::NonNull; 15 16 16 17 /// DMA address type. 17 18 /// ··· 359 358 dev: ARef<device::Device>, 360 359 dma_handle: DmaAddress, 361 360 count: usize, 362 - cpu_addr: *mut T, 361 + cpu_addr: NonNull<T>, 363 362 dma_attrs: Attrs, 364 363 } 365 364 ··· 393 392 .ok_or(EOVERFLOW)?; 394 393 let mut dma_handle = 0; 395 394 // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`. 396 - let ret = unsafe { 395 + let addr = unsafe { 397 396 bindings::dma_alloc_attrs( 398 397 dev.as_raw(), 399 398 size, ··· 402 401 dma_attrs.as_raw(), 403 402 ) 404 403 }; 405 - if ret.is_null() { 406 - return Err(ENOMEM); 407 - } 404 + let addr = NonNull::new(addr).ok_or(ENOMEM)?; 408 405 // INVARIANT: 409 406 // - We just successfully allocated a coherent region which is accessible for 410 407 // `count` elements, hence the cpu address is valid. We also hold a refcounted reference ··· 413 414 dev: dev.into(), 414 415 dma_handle, 415 416 count, 416 - cpu_addr: ret.cast::<T>(), 417 + cpu_addr: addr.cast(), 417 418 dma_attrs, 418 419 }) 419 420 } ··· 445 446 446 447 /// Returns the base address to the allocated region in the CPU's virtual address space. 447 448 pub fn start_ptr(&self) -> *const T { 448 - self.cpu_addr 449 + self.cpu_addr.as_ptr() 449 450 } 450 451 451 452 /// Returns the base address to the allocated region in the CPU's virtual address space as 452 453 /// a mutable pointer. 453 454 pub fn start_ptr_mut(&mut self) -> *mut T { 454 - self.cpu_addr 455 + self.cpu_addr.as_ptr() 455 456 } 456 457 457 458 /// Returns a DMA handle which may be given to the device as the DMA address base of ··· 579 580 // and we've just checked that the range and index is within bounds. 580 581 // - `offset` can't overflow since it is smaller than `self.count` and we've checked 581 582 // that `self.count` won't overflow early in the constructor. 582 - Ok(unsafe { self.cpu_addr.add(offset) }) 583 + Ok(unsafe { self.cpu_addr.as_ptr().add(offset) }) 583 584 } 584 585 585 586 /// Reads the value of `field` and ensures that its type is [`FromBytes`].