HomeFreeBSD

kern_malloc: fold free and zfree together into one __always_inline func

Description

kern_malloc: fold free and zfree together into one __always_inline func

free() and zfree() are essentially the same copy and pasted code with
the extra explicit_bzero() (and formerly kasan) calls. Add a bool to add
the extra functionality and make both functions a wrapper around the common
code and let the compiler do the optimization based on the bool input
when inlining.

No functional changes intended.

Suggested by: kib (in D45812)
Sponsored by: The FreeBSD Foundation
Reviewed by: kib, markj
Differential Revision: https://reviews.freebsd.org/D46101

And the fix from Olivier Certner (olce):

kern_malloc: Restore working KASAN runtime after free() and zfree() folding

In the zfree() case, the explicit_bzero() calls zero all the allocation,
including the redzone which malloc() has marked as invalid. So calling
kasan_mark() before those is in fact necessary.

This fixes a crash at boot when 'ldconfig' is run and tries to get
random bytes through getrandom() (relevant part of the stack is
read_random_uio() -> zfree() -> explicit_bzero()) for kernels with KASAN
compiled in.

Approved by: markj (mentor)
Fixes: 4fab5f005482 ("kern_malloc: fold free and zfree together into one __always_inline func")
Sponsored by: The FreeBSD Foundation

(cherry picked from commit 4fab5f005482aa88bc0f7d7a0a5e81b436869112)
(cherry picked from commit 28391f188ca18b6251ba46040adf81946b0ccb03)

Details

Provenance
bzAuthored on Jul 24 2024, 3:56 PM
Reviewer
kib
Differential Revision
D46101: kern_malloc: fold free and zfree together into one __always_inline func
Parents
rG1364bc30deec: kern_malloc: remove '.' and '\n' from KASSERT/panic
Branches
Unknown
Tags
Unknown