[PATCH 04/32] mm/shmem: fix commit limit bypass with very large shmem sizes
From: David Hildenbrand (Arm)
Date: Sun Nov 29 2026 - 05:08:18 EST
VM_ACCT() uses PAGE_ALIGN() on a loff_t, a signed type, which overflows
for sizes in the last page up to LLONG_MAX (MAX_LFS_FILESIZE on 64-bit).
With the default shmmax and shmall, userspace can trigger this through
shmget(IPC_PRIVATE, LLONG_MAX, IPC_CREAT).
Consequently, the resulting negative count makes __vm_enough_memory()
succeed and lowers vm_committed_as such that it will go negative. This
effectively disables the commit limit with vm.overcommit_memory = 2 for
as long as the shmem file exists.
Let's fix it by avoiding PAGE_ALIGN() and just using DIV_ROUND_UP_POW2(),
which cannot overflow. We now reject such problematic sizes with ENOMEM,
like any other size above the limit.
On 32-bit, VM_ACCT() can still return numbers that don't fit into
the long that __vm_enough_memory() consumes. However, only ftruncate()
to 8 TiB or more, through an fd obtained via /proc/$PID/map_files for a
shared anonymous mapping, can trigger that, which requires CAP_SYS_ADMIN
or CAP_CHECKPOINT_RESTORE. So we'll leave that alone for now.
This has low urgency, as it only affects systems with
vm.overcommit_memory = 2.
Fixes: 09cbfeaf1a5a ("mm, fs: get rid of PAGE_CACHE_* and page_cache_{get,release} macros")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/shmem.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index ce8f8b5f00ed7..0bdbbdcac4aee 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -88,7 +88,7 @@ static struct vfsmount *shm_mnt __ro_after_init;
#include "internal.h"
-#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
+#define VM_ACCT(size) DIV_ROUND_UP_POW2(size, PAGE_SIZE)
/* Pretend that each entry is of this size in directory's i_size */
#define BOGO_DIRENT_SIZE 20
--
2.56.1