From nobody Thu Oct 2 15:34:44 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44FCD286D78 for ; Mon, 15 Sep 2025 16:46:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757954786; cv=none; b=fw1n6CINnKtUaPvikOfXUsQF/iFumqyq14oBkE3mM7r/wMFPGBhSd3iZr7qCGxkje8DdD60Ulrk+m4z08hdNR9lv1xB6aiuVYnyjvBybY5uwllW+X1wDMcQrHKseCr9YxS8ov/rq8Vq0CDw21TKfYJzWZ9FYcd31u9jzxZ67uW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757954786; c=relaxed/simple; bh=lBWlwS7JcG7Bq/AWf4HTbFz+jYysV8dehUYeg6ZHX+w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cjlwMhYa/3pw74BKHus5KVbQeaSQ7UJJME9vRQf/Vecaeqa3RYZpbUGn53S7GL+cQQcH0y1368Xjkp5+2hNDAiZkfLL5YKC3dZedX/fbuJnQ23NZc9+voFpxK+JirkO6gCet0Gn/hgIM2ArA/pumJj+hWZ0XDmqAeUoWd6orGj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kaleshsingh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yU9zVeT3; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kaleshsingh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yU9zVeT3" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-252afdfafe1so46773465ad.0 for ; Mon, 15 Sep 2025 09:46:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1757954783; x=1758559583; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kvhIc5Qwm2MSlXgaqDt0tPsJi6Fn6VtZiCytLA6TTWs=; b=yU9zVeT3pmDrQ8UAASYsebznUX/oI10RS8RbKZE2oeH6l1pSMmASgj7Ww4qlEhy0zR fsrCL2CJCx/fqi3ANRRzAGM7c4j2rNfkO+CWnbYX+gAdruP/oEBOfxUlsZGMbOXhzd9P HzRPsAbD4mbw4yXUlPg4n5ofIJ+lhDRbsjMvMDxdmCxkqeWskN/tReNcR3zuC53qLP9c MMStveY5rq8LvSkT2dwD6CYZ1995U8uvBmeHiLc7qTE+N2GYRj+GKLFWfSmky9EeZGHo tx0tJrxnFVA5wiJL3B4xo4c1ZxvnWYuFgiqTelYI7S0xD9OGTXayj757q9IJjVzQh1Yx GxgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757954783; x=1758559583; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kvhIc5Qwm2MSlXgaqDt0tPsJi6Fn6VtZiCytLA6TTWs=; b=VXZ9O3if+RCCtWdGQDwuKhvhTLCjxb3NMr6pKAYl5Y7OzI/x4flkE+HKTVA6odCaC9 egaICyDtQ0gIRIB6TVVoKJuR38rz4uiLmUyi7KvMJW7Xcy8t0FT8kezV2es/F1ys5Eaa +p9t8YabGSFywpvqafsgTP2REfCaLqjfFxMiKqyBgjPUS1/grm/q+vBiXI96WXhLlSPr icOiDOVd6zaTiOG6P+8l/GJPVpDXzA5S5dwsKQk+QxSlRuCXTyL5XEhzwPrEg4H8yLBQ UQXxylnOn9ZtNovBU2KMT0dFDaBpr/ceBhySrLyXgkuPQwk+XRyLxtcsG4ChrZWK3az+ Tfsw== X-Forwarded-Encrypted: i=1; AJvYcCXP4Vb2qvoDi+uCxN/qTYLsKxvG5zfYzyP3HJ97RT6IAmbLjWboQhuQBUjQ2IyvfzyAnpbgm47KIuyEqVg=@vger.kernel.org X-Gm-Message-State: AOJu0Yyx1GcqN45wPu+lBu5v9U3ZOfp5vKIpbUlp5cYcr+7aqWglCNWn aNl0ZRbhI+EiUZ808dUi3fP34GaYdqRAxzNPMZb3Mkti7cPZDVf+5Kld3GDSCFR7ea0uOG3qZUi IwwRn89/HdvENw3oCOMTOuQpAjQ== X-Google-Smtp-Source: AGHT+IETjSAgl4Bq7YGi4NHOQLEhA++l/mjHETWNYuPpSZDm1ovOxLN6UFeAYtoyBJbEGZn5mcnKagruLzvaK22crw== X-Received: from plap12.prod.google.com ([2002:a17:902:f08c:b0:264:3c1f:6385]) (user=kaleshsingh job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:240c:b0:24e:3cf2:2450 with SMTP id d9443c01a7336-25d23e13c87mr190358195ad.2.1757954783437; Mon, 15 Sep 2025 09:46:23 -0700 (PDT) Date: Mon, 15 Sep 2025 09:36:34 -0700 In-Reply-To: <20250915163838.631445-1-kaleshsingh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250915163838.631445-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.51.0.384.g4c02a37b29-goog Message-ID: <20250915163838.631445-4-kaleshsingh@google.com> Subject: [PATCH v2 3/7] mm: introduce vma_count_remaining() From: Kalesh Singh To: akpm@linux-foundation.org, minchan@kernel.org, lorenzo.stoakes@oracle.com, david@redhat.com, Liam.Howlett@oracle.com, rppt@kernel.org, pfalcato@suse.de Cc: kernel-team@android.com, android-mm@google.com, Kalesh Singh , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Ben Segall , Mel Gorman , Valentin Schneider , Jann Horn , Shuah Khan , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The checks against sysctl_max_map_count are open-coded in multiple places. While simple checks are manageable, the logic in places like mremap.c involves arithmetic with magic numbers that can be difficult to reason about. e.g. ... >=3D sysctl_max_map_count - 3 To improve readability and centralize the logic, introduce a new helper, vma_count_remaining(). This function returns the VMA count headroom available for a givine process. The most common case of checking for a single new VMA can be done with the convenience helper has_vma_count_remaining(): if (!vma_count_remaining(mm)) And the complex checks in mremap.c become clearer by expressing the required capacity directly: if (vma_count_remaining(mm) < 4) While a capacity-based function could be misused (e.g., with an incorrect '<' vs '<=3D' comparison), the improved readability at the call sites makes such errors less likely than with the previous open-coded arithmetic. As part of this change, sysctl_max_map_count is made static to mm/mmap.c to improve encapsulation. Cc: Andrew Morton Cc: David Hildenbrand Cc: "Liam R. Howlett" Cc: Lorenzo Stoakes Cc: Mike Rapoport Cc: Minchan Kim Cc: Pedro Falcato Signed-off-by: Kalesh Singh --- Changes in v2: - Fix documentation comment for vma_count_remaining(), per Mike - Remove extern in header, per Mike and Pedro - Move declaration to mm/internal.h, per Mike - Replace exceeds_max_map_count() with capacity-based vma_count_remaining= (), per Lorenzo. - Fix tools/testing/vma, per Lorenzo. include/linux/mm.h | 2 -- mm/internal.h | 2 ++ mm/mmap.c | 21 ++++++++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 10 +++++----- tools/testing/vma/vma_internal.h | 9 +++++++++ 8 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 1ae97a0b8ec7..138bab2988f8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -192,8 +192,6 @@ static inline void __mm_zero_struct_page(struct page *p= age) #define MAPCOUNT_ELF_CORE_MARGIN (5) #define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN) =20 -extern int sysctl_max_map_count; - extern unsigned long sysctl_user_reserve_kbytes; extern unsigned long sysctl_admin_reserve_kbytes; =20 diff --git a/mm/internal.h b/mm/internal.h index 45b725c3dc03..39f1c9535ae5 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1661,4 +1661,6 @@ static inline bool reclaim_pt_is_enabled(unsigned lon= g start, unsigned long end, void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm); int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm); =20 +int vma_count_remaining(const struct mm_struct *mm); + #endif /* __MM_INTERNAL_H */ diff --git a/mm/mmap.c b/mm/mmap.c index e5370e7fcd8f..af88ce1fbb5f 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long = addr, return -EOVERFLOW; =20 /* Too many mappings? */ - if (mm->map_count >=3D sysctl_max_map_count) + if (!vma_count_remaining(mm)) return -ENOMEM; =20 /* @@ -1504,6 +1504,25 @@ struct vm_area_struct *_install_special_mapping( int sysctl_legacy_va_layout; #endif =20 +static int sysctl_max_map_count __read_mostly =3D DEFAULT_MAX_MAP_COUNT; + +/** + * vma_count_remaining - Determine available VMA slots + * @mm: The memory descriptor for the process. + * + * Check how many more VMAs can be created for the given @mm + * before hitting the sysctl_max_map_count limit. + * + * Return: The number of new VMAs the process can accommodate. + */ +int vma_count_remaining(const struct mm_struct *mm) +{ + const int map_count =3D mm->map_count; + const int max_count =3D sysctl_max_map_count; + + return (max_count > map_count) ? (max_count - map_count) : 0; +} + static const struct ctl_table mmap_table[] =3D { { .procname =3D "max_map_count", diff --git a/mm/mremap.c b/mm/mremap.c index 35de0a7b910e..14d35d87e89b 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -1040,7 +1040,7 @@ static unsigned long prep_move_vma(struct vma_remap_s= truct *vrm) * We'd prefer to avoid failure later on in do_munmap: * which may split one vma into three before unmapping. */ - if (current->mm->map_count >=3D sysctl_max_map_count - 3) + if (vma_count_remaining(current->mm) < 4) return -ENOMEM; =20 if (vma->vm_ops && vma->vm_ops->may_split) { @@ -1814,9 +1814,10 @@ static unsigned long check_mremap_params(struct vma_= remap_struct *vrm) * split in 3 before unmapping it. * That means 2 more maps (1 for each) to the ones we already hold. * Check whether current map count plus 2 still leads us to 4 maps below - * the threshold, otherwise return -ENOMEM here to be more safe. + * the threshold. In other words, is the current map count + 6 at or + * below the threshold? Otherwise return -ENOMEM here to be more safe. */ - if ((current->mm->map_count + 2) >=3D sysctl_max_map_count - 3) + if (vma_count_remaining(current->mm) < 6) return -ENOMEM; =20 return 0; diff --git a/mm/nommu.c b/mm/nommu.c index 8b819fafd57b..dd75f2334812 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -1316,7 +1316,7 @@ static int split_vma(struct vma_iterator *vmi, struct= vm_area_struct *vma, return -ENOMEM; =20 mm =3D vma->vm_mm; - if (mm->map_count >=3D sysctl_max_map_count) + if (!vma_count_remaining(mm)) return -ENOMEM; =20 region =3D kmem_cache_alloc(vm_region_jar, GFP_KERNEL); diff --git a/mm/util.c b/mm/util.c index f814e6a59ab1..b6e83922cafe 100644 --- a/mm/util.c +++ b/mm/util.c @@ -751,7 +751,6 @@ EXPORT_SYMBOL(folio_mc_copy); int sysctl_overcommit_memory __read_mostly =3D OVERCOMMIT_GUESS; static int sysctl_overcommit_ratio __read_mostly =3D 50; static unsigned long sysctl_overcommit_kbytes __read_mostly; -int sysctl_max_map_count __read_mostly =3D DEFAULT_MAX_MAP_COUNT; unsigned long sysctl_user_reserve_kbytes __read_mostly =3D 1UL << 17; /* 1= 28MB */ unsigned long sysctl_admin_reserve_kbytes __read_mostly =3D 1UL << 13; /* = 8MB */ =20 diff --git a/mm/vma.c b/mm/vma.c index 033a388bc4b1..df0e8409f63d 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -491,8 +491,8 @@ void unmap_region(struct ma_state *mas, struct vm_area_= struct *vma, } =20 /* - * __split_vma() bypasses sysctl_max_map_count checking. We use this wher= e it - * has already been checked or doesn't make sense to fail. + * __split_vma() bypasses vma_count_remaining() checks. We use this where + * it has already been checked or doesn't make sense to fail. * VMA Iterator will point to the original VMA. */ static __must_check int @@ -592,7 +592,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_st= ruct *vma, static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long addr, int new_below) { - if (vma->vm_mm->map_count >=3D sysctl_max_map_count) + if (!vma_count_remaining(vma->vm_mm)) return -ENOMEM; =20 return __split_vma(vmi, vma, addr, new_below); @@ -1345,7 +1345,7 @@ static int vms_gather_munmap_vmas(struct vma_munmap_s= truct *vms, * its limit temporarily, to help free resources as expected. */ if (vms->end < vms->vma->vm_end && - vms->vma->vm_mm->map_count >=3D sysctl_max_map_count) { + !vma_count_remaining(vms->vma->vm_mm)) { error =3D -ENOMEM; goto map_count_exceeded; } @@ -2772,7 +2772,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_= area_struct *vma, if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) return -ENOMEM; =20 - if (mm->map_count >=3D sysctl_max_map_count) + if (!vma_count_remaining(mm)) return -ENOMEM; =20 if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_inter= nal.h index 3639aa8dd2b0..52cd7ddc73f4 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -1517,4 +1517,13 @@ static inline vm_flags_t ksm_vma_flags(const struct = mm_struct *, const struct fi return vm_flags; } =20 +/* Helper to get VMA count capacity */ +static int vma_count_remaining(const struct mm_struct *mm) +{ + const int map_count =3D mm->map_count; + const int max_count =3D sysctl_max_map_count; + + return (max_count > map_count) ? (max_count - map_count) : 0; +} + #endif /* __MM_VMA_INTERNAL_H */ --=20 2.51.0.384.g4c02a37b29-goog