Skip to content

Commit 8ecb65b

Browse files
committed
Merge tag 'alloc-next-v6.17-2025-07-15' of https://github.com/Rust-for-Linux/linux into rust-next
Pull alloc and DMA updates from Danilo Krummrich: Box: - Implement Borrow / BorrowMut for Box<T, A>. Vec: - Implement Default for Vec<T, A>. - Implement Borrow / BorrowMut for Vec<T, A>. DMA: - Clarify wording and be consistent in 'coherent' nomenclature. - Convert the read!() / write!() macros to return a Result. - Add as_slice() / write() methods in CoherentAllocation. - Fix doc-comment of dma_handle(). - Expose count() and size() in CoherentAllocation and add the corresponding type invariants. - Implement CoherentAllocation::dma_handle_with_offset(). - Require mutable reference for as_slice_mut() and write(). MAINTAINERS: - Add Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki and Lorenzo Stoakes as reviewers (thanks everyone). * tag 'alloc-next-v6.17-2025-07-15' of https://github.com/Rust-for-Linux/linux: MAINTAINERS: add mm folks as reviewers to rust alloc rust: dma: require mutable reference for as_slice_mut() and write() rust: dma: add dma_handle_with_offset method to CoherentAllocation rust: dma: expose the count and size of CoherentAllocation rust: dma: fix doc-comment of dma_handle() rust: dma: add as_slice/write functions for CoherentAllocation rust: dma: convert the read/write macros to return Result rust: dma: clarify wording and be consistent in `coherent` nomenclature rust: alloc: implement `Borrow` and `BorrowMut` for `KBox` rust: alloc: implement `Borrow` and `BorrowMut` for `Vec` rust: vec: impl Default for Vec with any allocator
2 parents 7c098cd + d49ac77 commit 8ecb65b

5 files changed

Lines changed: 295 additions & 48 deletions

File tree

MAINTAINERS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21708,6 +21708,10 @@ K: \b(?i:rust)\b
2170821708

2170921709
RUST [ALLOC]
2171021710
M: Danilo Krummrich <dakr@kernel.org>
21711+
R: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
21712+
R: Vlastimil Babka <vbabka@suse.cz>
21713+
R: Liam R. Howlett <Liam.Howlett@oracle.com>
21714+
R: Uladzislau Rezki <urezki@gmail.com>
2171121715
L: rust-for-linux@vger.kernel.org
2171221716
S: Maintained
2171321717
T: git https://github.com/Rust-for-Linux/linux.git alloc-next

rust/kernel/alloc/kbox.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use super::allocator::{KVmalloc, Kmalloc, Vmalloc};
77
use super::{AllocError, Allocator, Flags};
88
use core::alloc::Layout;
9+
use core::borrow::{Borrow, BorrowMut};
910
use core::fmt;
1011
use core::marker::PhantomData;
1112
use core::mem::ManuallyDrop;
@@ -504,6 +505,62 @@ where
504505
}
505506
}
506507

508+
/// # Examples
509+
///
510+
/// ```
511+
/// # use core::borrow::Borrow;
512+
/// # use kernel::alloc::KBox;
513+
/// struct Foo<B: Borrow<u32>>(B);
514+
///
515+
/// // Owned instance.
516+
/// let owned = Foo(1);
517+
///
518+
/// // Owned instance using `KBox`.
519+
/// let owned_kbox = Foo(KBox::new(1, GFP_KERNEL)?);
520+
///
521+
/// let i = 1;
522+
/// // Borrowed from `i`.
523+
/// let borrowed = Foo(&i);
524+
/// # Ok::<(), Error>(())
525+
/// ```
526+
impl<T, A> Borrow<T> for Box<T, A>
527+
where
528+
T: ?Sized,
529+
A: Allocator,
530+
{
531+
fn borrow(&self) -> &T {
532+
self.deref()
533+
}
534+
}
535+
536+
/// # Examples
537+
///
538+
/// ```
539+
/// # use core::borrow::BorrowMut;
540+
/// # use kernel::alloc::KBox;
541+
/// struct Foo<B: BorrowMut<u32>>(B);
542+
///
543+
/// // Owned instance.
544+
/// let owned = Foo(1);
545+
///
546+
/// // Owned instance using `KBox`.
547+
/// let owned_kbox = Foo(KBox::new(1, GFP_KERNEL)?);
548+
///
549+
/// let mut i = 1;
550+
/// // Borrowed from `i`.
551+
/// let borrowed = Foo(&mut i);
552+
/// # Ok::<(), Error>(())
553+
/// ```
554+
impl<T, A> BorrowMut<T> for Box<T, A>
555+
where
556+
T: ?Sized,
557+
A: Allocator,
558+
{
559+
fn borrow_mut(&mut self) -> &mut T {
560+
self.deref_mut()
561+
}
562+
}
563+
507564
impl<T, A> fmt::Display for Box<T, A>
508565
where
509566
T: ?Sized + fmt::Display,

rust/kernel/alloc/kvec.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::{
88
AllocError, Allocator, Box, Flags,
99
};
1010
use core::{
11+
borrow::{Borrow, BorrowMut},
1112
fmt,
1213
marker::PhantomData,
1314
mem::{ManuallyDrop, MaybeUninit},
@@ -851,7 +852,7 @@ where
851852
}
852853
}
853854

854-
impl<T> Default for KVec<T> {
855+
impl<T, A: Allocator> Default for Vec<T, A> {
855856
#[inline]
856857
fn default() -> Self {
857858
Self::new()
@@ -890,6 +891,58 @@ where
890891
}
891892
}
892893

894+
/// # Examples
895+
///
896+
/// ```
897+
/// # use core::borrow::Borrow;
898+
/// struct Foo<B: Borrow<[u32]>>(B);
899+
///
900+
/// // Owned array.
901+
/// let owned_array = Foo([1, 2, 3]);
902+
///
903+
/// // Owned vector.
904+
/// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);
905+
///
906+
/// let arr = [1, 2, 3];
907+
/// // Borrowed slice from `arr`.
908+
/// let borrowed_slice = Foo(&arr[..]);
909+
/// # Ok::<(), Error>(())
910+
/// ```
911+
impl<T, A> Borrow<[T]> for Vec<T, A>
912+
where
913+
A: Allocator,
914+
{
915+
fn borrow(&self) -> &[T] {
916+
self.as_slice()
917+
}
918+
}
919+
920+
/// # Examples
921+
///
922+
/// ```
923+
/// # use core::borrow::BorrowMut;
924+
/// struct Foo<B: BorrowMut<[u32]>>(B);
925+
///
926+
/// // Owned array.
927+
/// let owned_array = Foo([1, 2, 3]);
928+
///
929+
/// // Owned vector.
930+
/// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);
931+
///
932+
/// let mut arr = [1, 2, 3];
933+
/// // Borrowed slice from `arr`.
934+
/// let borrowed_slice = Foo(&mut arr[..]);
935+
/// # Ok::<(), Error>(())
936+
/// ```
937+
impl<T, A> BorrowMut<[T]> for Vec<T, A>
938+
where
939+
A: Allocator,
940+
{
941+
fn borrow_mut(&mut self) -> &mut [T] {
942+
self.as_mut_slice()
943+
}
944+
}
945+
893946
impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {}
894947

895948
impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>

0 commit comments

Comments
 (0)