Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794062618; b=5fGUMCL1ij6bTsDIKLrc614IFm0UTVAI8IrTTgXQDsU9P+V5GA17Fdn86L/wrRezXocX dZgz1HC4tCWrZ92nkFDxfSsx4Vx9OLiM1FYDeOAg5BD+rjHZzChED9+NcsYqBz9epE8rW zAX3o+T4fUCfsYB7rgO/kBvp7HxhU0pOUeMBYc5dOmOeRijB8RSYa/FFuIcOBYx3d9jDn YERAw4ZZ5cIqnkME0fkfjguwSDbHk186ax70BOCUS1IsoNu7ZQccAtkdAkylbqsuHjckc erH/EWeFrc3ocs4K6tDLsTZ6SuIqnjkFeEKPaZ47i0rQIru25N9cgcGV2tM3WDSlskw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794062618; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=DOy7LOGG7tbsNQO68R7nKkCVnfEcHulIWE7+FeuHHpo=; b=jeVq9tpbc0+pSGoFuhjXyTemXDGu2b7yEeIlO0c40GxpZPn/SrzJvAPB6HEpRC3THzCK d1OmiY3uGybL21gEemtzDa9KMDOg2e/Pj7sYhjxU7/9ay7TRxSnjrhEZLYFvuosspHzZj ln2y9LBtLQ1tqOyudIMY/9JVt3Mv2CW/ns0MHlsX+ugCDu3CkeXIyqPcrFC0sNpPEtJ8Z QWNOVvEhdv63UXcd95ZuuOOXhpnA67tNd3dpQ3uqQ2IaE0uYUoeq/kBp/53OYMn8ZyhOh 5/azZ1pqTfZwXvR2yg2K7ZhLGEe4F/B2SXQVocuTYVB2+yirJwqXncqnTOD3UHz5JBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=DPxkXVYt; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 709B0606A333; Sat, 7 Nov 2026 14:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=DPxkXVYt Received: by smtp.kernel.org (Postfix) with ESMTPSA id 201411F0089F; Sat, 7 Nov 2026 14:43:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794062618; bh=DOy7LOGG7tbsNQO68R7nKkCVnfEcHulIWE7+FeuHHpo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DPxkXVYteEbRNv8uNFu98YdHvay3ULwlzG05dKXKF4Zyr+OyWQx68I8XzLgKtXvGO SdoHfQCxRjSjNCX4N7TFClUTh6NYCc3fK1GrT4IwKmAnbf3YgZI5+glUZ+bZS1FJEb vRFPyLiXKyWxS2JgAPElrJkBkLTcLaFDoO8sPvAY= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Nickolai Zeldovich , Andrew Morton , Pedro Falcato , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Sasha Levin Subject: [PATCH 6.12 1441/1536] mm/mlock: use a long for the page count in mlock_fixup() Date: Sat, 7 Nov 2026 08:57:37 -0500 Message-ID: <20261107135038.311182840@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134957.653319350@linuxfoundation.org> References: <20261107134957.653319350@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Nickolai Zeldovich [ Upstream commit 3ae91e3079b1504ef162b2884454d5f1421f4eb9 ] mlock_fixup() adjusts mm->locked_vm, an unsigned long, by the number of pages whose VM_LOCKED state it changes, but computes that number in an int: int nr_pages; ... nr_pages = (end - start) >> PAGE_SHIFT; ... mm->locked_vm += nr_pages; When a single VMA range of 2^31 pages (8 TiB with 4 KiB pages) or more is locked or unlocked, the count is truncated to 32 bits and then sign-extended, so locked_vm is changed by the wrong amount, while munmap(), mremap() and the other paths that maintain locked_vm use the full unsigned long page count. Such a range is reachable on 64-bit architectures with a 47-bit or larger user address space (x86_64, arm64 with 48-bit VA, riscv64 Sv48) by a task with CAP_IPC_LOCK or a raised RLIMIT_MEMLOCK, which do_mlock() checks against the full untruncated length. It costs nothing with MAP_NORESERVE and MLOCK_ONFAULT/MCL_ONFAULT, since then no page is populated. On x86_64, after p = mmap(NULL, 8UL << 40, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); mlock2(p, 8UL << 40, MLOCK_ONFAULT); VmLck in /proc/self/status reads 18446744065119617024 kB instead of 8589934592 kB; munmap() of the region then subtracts the true size and leaves VmLck at 18446744056529682432 kB instead of 0. With 16 TiB, locked_vm is not changed at all, VmLck stays 0 kB, and after the task drops CAP_IPC_LOCK or lowers RLIMIT_MEMLOCK, mlock(), mmap(MAP_LOCKED) and the other mlock_future_ok() and account_locked_vm() users enforce RLIMIT_MEMLOCK against that count, so they let the task lock memory beyond its limit (or, in the 8 TiB case, refuse every further lock). The error persists for the life of the mm; unlocking the range in pieces skews it further. The wrong count is per-mm accounting only: it does not change which pages are locked. The int has been there since before git history (the variable was named "pages" until it was renamed in 2.6.28); every other updater of locked_vm uses an unsigned long or long count. Make nr_pages a long, which holds the page count of any user range and its negation, so the existing sign handling is unchanged. Found by formal verification of mm/mlock.c against the kernel binary. Link: https://lore.kernel.org/20261014131358.780240-1-nickolai@csail.mit.edu Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Nickolai Zeldovich Signed-off-by: Andrew Morton Reviewed-by: Pedro Falcato Assisted-by: LLM Cc: Liam R. Howlett Cc: Lorenzo Stoakes Cc: Vlastimil Babka Cc: Jann Horn Cc: Signed-off-by: Sasha Levin --- mm/mlock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/mlock.c b/mm/mlock.c index 8293d2d03ab0a..9d5fd901d0f45 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -468,7 +468,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long end, vm_flags_t newflags) { struct mm_struct *mm = vma->vm_mm; - int nr_pages; + long nr_pages; int ret = 0; vm_flags_t oldflags = vma->vm_flags; -- 2.53.0