]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
execmem: rework execmem_cache_free()
authorMike Rapoport (Microsoft) <rppt@kernel.org>
Sun, 13 Jul 2025 07:17:25 +0000 (10:17 +0300)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 2 Aug 2025 19:06:11 +0000 (12:06 -0700)
commit187fd8521dd8b202cbacd7af57f4301da4d5b52d
tree8a53a75b888c1ac7e7bcb53aa7f20deacd1da995
parent838955f64ae7582f009a3538889bb9244f37ab26
execmem: rework execmem_cache_free()

Currently execmem_cache_free() ignores potential allocation failures that
may happen in execmem_cache_add().  Besides, it uses text poking to fill
the memory with trapping instructions before returning it to cache
although it would be more efficient to make that memory writable, update
it using memcpy and then restore ROX protection.

Rework execmem_cache_free() so that in case of an error it will defer
freeing of the memory to a delayed work.

With this the happy fast path will now change permissions to RW, fill the
memory with trapping instructions using memcpy, restore ROX permissions,
add the memory back to the free cache and clear the relevant entry in
busy_areas.

If any step in the fast path fails, the entry in busy_areas will be marked
as pending_free.  These entries will be handled by a delayed work and
freed asynchronously.

To make the fast path faster, use __GFP_NORETRY for memory allocations and
let asynchronous handler try harder with GFP_KERNEL.

Link: https://lkml.kernel.org/r/20250713071730.4117334-4-rppt@kernel.org
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Daniel Gomez <da.gomez@samsung.com>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/execmem.c