Skip to contentSkip to conversation

Question on Box<T> smart pointer

DISCUSSION Fabio Aiuto 2 participants 4 messages Apr 27, 2021 Rust

lore
Share & tools
lore
#0 lore raw

Hi all,

I have a question. There's one thing I miss.

If Box<T> smart pointer allows allocating space on the heap, how is this behaviour overridden in linux kernel Rust?

I mean, in c we have all API's to allocate heap space, but using "pure" Box<T> in kernel space what kind of allocation provides?

If this question is stupid I apologize in advance,

thank you,

fabio

#1 lore raw

Box<T> in the kernel allocates on the kernel heap, via kmalloc. This is done by providing a Rust GlobalAllocator:
https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/allocator.rs#L11

Alex

Quoted 17 lines · Fabio Aiuto

Hi all,

I have a question. There's one thing I miss.

If Box<T> smart pointer allows allocating space on the heap, how is this behaviour overridden in linux kernel Rust?

I mean, in c we have all API's to allocate heap space, but using "pure" Box<T> in kernel space what kind of allocation provides?

If this question is stupid I apologize in advance,

thank you,

fabio

Signature · 1 line
All that is necessary for evil to succeed is for good people to do nothing.
#2 lore raw
Quoted 3 lines · Alex Gaynor

Box<T> in the kernel allocates on the kernel heap, via kmalloc. This is done by providing a Rust GlobalAllocator:
https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/allocator.rs#L11

so this code:

pub struct KernelAllocator;

unsafe impl GlobalAlloc for KernelAllocator {

    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        // `krealloc()` is used instead of `kmalloc()` because the latter is
        // an inline function and cannot be bound to as a result.
        bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8
    }

    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
        bindings::kfree(ptr as *const c_types::c_void);
    }
}

overrides the default behaviour of Box<T> defined
in core::alloc::GlobalAlloc, doesn't it?

So this code overrides the general heap allocation facility, not just Box<T>, right?

thank you,

fabio

Quoted 26 lines

Alex

Quoted 17 lines · Fabio Aiuto

Hi all,

I have a question. There's one thing I miss.

If Box<T> smart pointer allows allocating space on the heap, how is this behaviour overridden in linux kernel Rust?

I mean, in c we have all API's to allocate heap space, but using "pure" Box<T> in kernel space what kind of allocation provides?

If this question is stupid I apologize in advance,

thank you,

fabio

Signature · 1 line
All that is necessary for evil to succeed is for good people to do nothing.
#3 lore raw

Yes, it overrides it for any APIs in the alloc crate that allocate memory (Arc, Rc, Vec, String, etc.).

Technically https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/lib.rs#L203-L204
is where the overriding really happens. The previous code I linked is
where the implementation is.

Alex

Quoted 58 lines · Fabio Aiuto
Quoted 3 lines · Alex Gaynor

Box<T> in the kernel allocates on the kernel heap, via kmalloc. This is done by providing a Rust GlobalAllocator:
https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/allocator.rs#L11

so this code:

pub struct KernelAllocator;

unsafe impl GlobalAlloc for KernelAllocator {

    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        // `krealloc()` is used instead of `kmalloc()` because the latter is
        // an inline function and cannot be bound to as a result.
        bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8
    }

    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
        bindings::kfree(ptr as *const c_types::c_void);
    }
}

overrides the default behaviour of Box<T> defined
in core::alloc::GlobalAlloc, doesn't it?

So this code overrides the general heap allocation facility, not just Box<T>, right?

thank you,

fabio

Quoted 26 lines

Alex

Quoted 12 lines · Fabio Aiuto

Hi all,

I have a question. There's one thing I miss.

If Box<T> smart pointer allows allocating space on the heap, how is this behaviour overridden in linux kernel Rust?

I mean, in c we have all API's to allocate heap space, but using "pure" Box<T> in kernel space what kind of allocation provides?

Quoted 5 lines

If this question is stupid I apologize in advance,

thank you,

fabio

--
All that is necessary for evil to succeed is for good people to do nothing.

Signature · 1 line
All that is necessary for evil to succeed is for good people to do nothing.