[PATCH 1/3] mm/mlock: make a zero length request a no-op whatever the alignment
From: Jose A. Perez de Azpillaga
Date: Mon Oct 05 2026 - 02:53:24 EST
A zero length mlock() or munlock() at an unaligned address is rounded up
to a page, so mlock(addr + 5, 0) locks a page that was never asked for,
and munlock(addr + 5, 0) unlocks one the caller may still rely on. An
aligned zero length is already a no-op.
Return early for a zero length, before the rounding and before
can_do_mlock(). This is a uapi change: with RLIMIT_MEMLOCK=0 and no
CAP_IPC_LOCK, mlock(addr, 0) returns 0 instead of -EPERM, which is what
munlock(addr, 0) already returns. man 2 mlock does not say what a zero
length should do, and -EINVAL would break callers that pass one today and
get 0.
Reported-by: Park Tae-sun <ts930@xxxxxxxxx>
Link: https://lore.kernel.org/all/179066531783.50175.13377521828379196177@xxxxxxxxx/
Signed-off-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>
---
mm/mlock.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/mm/mlock.c b/mm/mlock.c
index 4235a1518fc9..cc28e5d7413a 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -612,6 +612,9 @@ static __must_check int do_mlock(unsigned long start, size_t len,
start = untagged_addr(start);
+ if (!len)
+ return 0;
+
if (!can_do_mlock())
return -EPERM;
@@ -678,6 +681,9 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len)
start = untagged_addr(start);
+ if (!len)
+ return 0;
+
len = PAGE_ALIGN(len + (offset_in_page(start)));
start &= PAGE_MASK;
--
2.55.0