Re: [PATCH v4 10/20] rust: add `kernel` crate

From: Sergey Senozhatsky
Date: Mon Feb 14 2022 - 00:27:19 EST


On (22/02/12 14:03), Miguel Ojeda wrote:
[..]
> +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.
> + unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }

[..]

> +impl<const ORDER: u32> Pages<ORDER> {
> + /// Allocates a new set of contiguous pages.
> + pub fn new() -> Result<Self> {
> + // TODO: Consider whether we want to allow callers to specify flags.
> + // SAFETY: This only allocates pages. We check that it succeeds in the next statement.
> + let pages = unsafe {
> + bindings::alloc_pages(
> + bindings::GFP_KERNEL | bindings::__GFP_ZERO | bindings::__GFP_HIGHMEM,
> + ORDER,
> + )
> + };

[..]

Is this flexible enough? Why not let user pass bindings::GFP_* bitmask,
just like what the underlying kernel API does.