[PATCH] mm/vma: correctly unaccount on mmap_prepare() failure

Lorenzo Stoakes (ARM) posted 1 patch 3 weeks, 2 days ago
mm/vma.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
[PATCH] mm/vma: correctly unaccount on mmap_prepare() failure
Posted by Lorenzo Stoakes (ARM) 3 weeks, 2 days ago
__mmap_setup() accounts memory for relevant mappings via:

security_vm_enough_memory_mm()
  -> __vm_enough_memory()
    -> vm_acct_memory()

If __mmap_setup() fails, this indicates that this accounting did not take
place, and thus it's appropriate for __mmap_region() to jump to
abort_munmap.

However if call_mmap_prepare() fails, it also jumps there and any accounted
memory is not correctly unaccounted.

Fix this by handling each error separately.

Fixes: c84bf6dd2b83 ("mm: introduce new .mmap_prepare() file callback")
Cc: stable@vger.kernel.org
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 mm/vma.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mm/vma.c b/mm/vma.c
index 35e7a64855fa..f29abb30956b 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -2859,10 +2859,12 @@ static unsigned long __mmap_region(struct file *file, unsigned long addr,
 	map.check_ksm_early = can_set_ksm_flags_early(&map);
 
 	error = __mmap_setup(&map, &desc, uf);
-	if (!error && have_mmap_prepare)
-		error = call_mmap_prepare(&map, &desc);
 	if (error)
 		goto abort_munmap;
+	if (have_mmap_prepare)
+		error = call_mmap_prepare(&map, &desc);
+	if (error)
+		goto unacct_error;
 
 	if (map.check_ksm_early)
 		update_ksm_flags(&map);

---
base-commit: e3b5239afe1b8f0194db7436b17c33e94c1988c4
change-id: 20260902-fix-unaccount-mmap_prepare-0daa93d56f74

Best regards,
-- 
Lorenzo Stoakes (ARM) <ljs@kernel.org>