mm/vma: slightly rework the anonymous check in __mmap_new_vma()

Determine if the VMA is anonymous early on, so we separate the logic which
invokes callbacks from the rest of the logic.

This is in order that a subsequent commit which asserts correct virtual
page offset for anonymous mappings correctly asserts this for new anonymous
mappings.

No functional change intended.

Link: https://lore.kernel.org/20260710-b4-pre-scalable-cow-v2-28-2a5aa403d977@kernel.org
Signed-off-by: Lorenzo Stoakes <ljs@kernel.org>
Reviewed-by: Gregory Price <gourry@gourry.net>
Reviewed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
Cc: Ackerley Tng <ackerleytng@google.com>
Cc: David Hildenbrand (Arm) <david@kernel.org>
Cc: Kai Huang <kai.huang@intel.com>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Pedro Falcato <pfalcato@suse.de>
Cc: SJ Park <sj@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: Liam R. Howlett (Oracle) <liam@infradead.org>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Lorenzo Stoakes
2026-07-10 21:17:09 +01:00
committed by Andrew Morton
parent ce4ec991da
commit bcb52fc56e

View File

@@ -2587,6 +2587,8 @@ static int __mmap_new_file_vma(struct mmap_state *map,
static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
struct mmap_action *action)
{
const bool is_anon = !map->file &&
!vma_flags_test(&map->vma_flags, VMA_SHARED_BIT);
struct vma_iterator *vmi = map->vmi;
int error = 0;
struct vm_area_struct *vma;
@@ -2601,6 +2603,10 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
return -ENOMEM;
vma_iter_config(vmi, map->addr, map->end);
if (is_anon)
vma_set_anonymous(vma);
vma_set_range(vma, map->addr, map->end, map->pgoff);
vma->flags = map->vma_flags;
vma->vm_page_prot = map->page_prot;
@@ -2610,12 +2616,11 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
goto free_vma;
}
/* Invoke callbacks. */
if (map->file)
error = __mmap_new_file_vma(map, vma);
else if (vma_flags_test(&map->vma_flags, VMA_SHARED_BIT))
else if (!is_anon)
error = shmem_zero_setup(vma);
else
vma_set_anonymous(vma);
if (error)
goto free_iter_vma;