Page MenuHomeFreeBSD

LinuxKPI: switch mallocarray to an lpi implementation using __kmalloc()
Needs ReviewPublic

Authored by bz on Sep 12 2024, 6:27 PM.
Tags
None
Referenced Files
Unknown Object (File)
Jan 28 2025, 8:00 PM
Unknown Object (File)
Jan 28 2025, 3:47 AM
Unknown Object (File)
Jan 24 2025, 10:39 PM
Unknown Object (File)
Jan 24 2025, 10:36 AM
Unknown Object (File)
Jan 17 2025, 4:20 PM
Unknown Object (File)
Jan 13 2025, 11:13 AM
Unknown Object (File)
Jan 13 2025, 10:57 AM
Unknown Object (File)
Jan 9 2025, 10:13 PM
Subscribers

Details

Reviewers
jhb
manu
Group Reviewers
linuxkpi
Summary

With mallocarray() we cannot guarantee that any size larger than
PAGE_SIZE will be contiguous. Switch to use the internal __kmalloc()
implementation which now does provide that guarantee.

Sponsored by: The FreeBSD Foundation
MFC after: 3 days

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 63041
Build 59925: arc lint + arc unit

Event Timeline

bz requested review of this revision.Sep 12 2024, 6:27 PM

Also change the (internal) mallocarray() calls in linux_compat.c to lkpi_mallocarray()
given we are using the LinuxKPI malloc type (M_KMALLOC) there already; better for
consistency.

manu requested changes to this revision.Jan 7 2025, 8:01 PM
manu added a subscriber: manu.

I'm not a fan of this, mallocarray is a OpenBSD thing, not a Linux thing, I don't see why we should have an lkpi implementation.

This revision now requires changes to proceed.Jan 7 2025, 8:01 PM

It's a helper function; because we do use mallocarray internally and it doesn't go by Linux guarantees on memory being physically contiguous in some cases. I can replace all the mallocarray calls directly with __kmalloc() but that just makes it harder to maintain in many places rather one. We can name this whatever we want it to be if you do not like the name...

This seems certainly fine. This is the semantics required for kcalloc and kmalloc_array

sys/compat/linuxkpi/common/include/linux/slab.h
116–117

This isn't enforcing contigmalloc for size > PAGE_SIZE?

148–149

Similarly, this also needs to enforce contigmalloc.

I would be tempted to implement the *calloc functions by setting the ZERO flag and then calling the malloc_array function, e.g.:

kcalloc_node()
{
   flags |= __GFP_ZERO;
   return kmalloc_array_node(...)
}

Same can be true for kcalloc() calling kmalloc_array(). Then, you could just have the ABI symbols be lkpi_kmalloc_array and lkpi_kmalloc_array_node as you do today for kmalloc. This also suggests btw that you need to add a shim for kmalloc_node that uses contigmalloc for larger sizes.

sys/compat/linuxkpi/common/src/linux_compat.c
2770–2771

Maybe use just use the kmalloc_array here instead and elsewhere in this file, then you aren't exposing the internal symbol more widely?

bz marked 2 inline comments as done.

Drop lkpi_mallocarray as requested by @manu.

Add a lkpi___kmalloc_node() in parallel to lkpi___kmalloc() which does the if and the actual
allocation in a C file so we do not spread malloc calls in inline functions anymore.

Follow the path suggested by @jhb which works out nicely:
Use two former functions as base for their *_array functions. Use those as base for the
remaing wrappers.

Switch to kmalloc_array() inseatd of our lkpi_ version to avoid further exposure of it.

Add comments about realloc as it seems kern_malloc.c does not handle that yet and we need
to have a chat about it.

bz marked an inline comment as done.Fri, Mar 21, 12:44 AM
bz added inline comments.
sys/compat/linuxkpi/common/src/linux_slab.c
230

@jhb, @emaste, This should be <= as well, correct?

bz planned changes to this revision.Fri, Mar 21, 6:54 PM

I really shouldn't code with a fever. Need to reshuffle the functions so they will compile. Also )

Make compile as well fixing order and ()