mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-10 18:11:43 -04:00
We must correctly update VMA anonymous page offset state on all VMA operations that would result in it changing, with special attention given to remapping. We cover most cases by simply updating vma_set_range() to do so (with a new anonymous page offset parameter), but also notably must update the merging and mapping logic to propagate this parameter correctly. The remap logic remains the same - we may update the anonymous page offset if the VMA is unfaulted, but now this applies to MAP_PRIVATE file-backed mappings too, so we update the code to reflect this. Note that we use __linear_anon_page_index() upon remap as the VMA may be shared, in order that we update the field consistently regardless of VMA type. Similarly, pass through anon page offset to the merge logic, updating the vma_merge_struct struct to propagate it, and also use __linear_anon_page_index() to obtain the anonymous page index so it can be safely used for both shared and MAP_PRIVATE file-backed mappings. In copy_vma(), the anonymous page offset is updated regardless of whether the mapping is a CoW mapping or not. This is both to keep the anonymous page offset consistent even for non-CoW mappings (it is set so should at least remain correct) and makes the logic cleaner. A self-merge however remains permitted only for mappings which can have a populated vma->anon_vma and do not require alignment on a separate file offset - that is pure anonymous VMAs, so only set can_self_merge if vma_is_anonymous(). Finally, we update insert_vm_struct() to correctly set the anonymous page offset on insertion of a VMA. We simply ensure state is correctly propagated here, so no functional changes are intended. Also update VMA userland tests to reflect this change. Link: https://lore.kernel.org/20260813-b4-scalable-cow-virt-pgoff-v5-9-c21581c0c3c8@kernel.org Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> Acked-by: David Hildenbrand (Arm) <david@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Deucher <alexander.deucher@amd.com> Cc: Alexander Gordeev <agordeev@linux.ibm.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Baoquan He <baoquan.he@linux.dev> Cc: Barry Song <baohua@kernel.org> Cc: Boris Brezillon <boris.brezillon@collabora.com> Cc: Byungchul Park <byungchul@sk.com> Cc: Chengming Zhou <chengming.zhou@linux.dev> Cc: Chris Li <chrisl@kernel.org> Cc: Christan König <christian.koenig@amd.com> Cc: Christian Borntraeger <borntraeger@linux.ibm.com> Cc: Claudio Imbrenda <imbrenda@linux.ibm.com> Cc: Dave Airlie <airlied@gmail.com> Cc: Dev Jain <dev.jain@arm.com> Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Gregory Price (Meta) <gourry@gourry.net> Cc: Harry Yoo <harry@kernel.org> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Huang Ray <Ray.Huang@amd.com> Cc: "Huang, Ying" <ying.huang@linux.alibaba.com> Cc: Ian Rogers <irogers@google.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@linaro.org> Cc: Jan Kara <jack@suse.cz> Cc: Jann Horn <jannh@google.com> Cc: Janosch Frank <frankja@linux.ibm.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: Jiri Olsa <jolsa@kernel.org> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Joshua Hahn <joshua.hahnjy@gmail.com> Cc: Kairui Song <kasong@tencent.com> Cc: Kees Cook <kees@kernel.org> Cc: Kemeng Shi <shikemeng@huaweicloud.com> Cc: Lance Yang <lance.yang@linux.dev> Cc: Liam R. Howlett <liam@infradead.org> Cc: Liviu Dudau <liviu.dudau@arm.com> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Cc: Marc Rutland <mark.rutland@arm.com> Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org> Cc: Matthew Auld <matthew.auld@intel.com> Cc: Matthew Brost <matthew.brost@intel.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Maxime Ripard <mripard@kernel.org> Cc: Miaohe Lin <linmiaohe@huawei.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Mike Rapoport <rppt@kernel.org> Cc: Muchun Song <muchun.song@linux.dev> Cc: Namhyung kim <namhyung@kernel.org> Cc: Naoya Horiguchi <nao.horiguchi@gmail.com> Cc: Nhat Pham <nphamcs@gmail.com> Cc: Nico Pache <npache@redhat.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Pedro Falcato <pfalcato@suse.de> Cc: Peter Xu <peterx@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rakie Kim <rakie.kim@sk.com> Cc: Rik van Riel <riel@surriel.com> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Steven Price <steven.price@arm.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Sven Schnelle <svens@linux.ibm.com> Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com> Cc: Thomas Zimemrmann <tzimmermann@suse.de> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Vlastimil Babka <vbabka@kernel.org> Cc: xu xin <xu.xin16@zte.com.cn> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
124 lines
2.6 KiB
C
124 lines
2.6 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "shared.h"
|
|
|
|
|
|
bool fail_prealloc;
|
|
unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
|
|
unsigned long dac_mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
|
|
unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
|
|
|
|
const struct vm_operations_struct vma_dummy_vm_ops;
|
|
struct anon_vma dummy_anon_vma;
|
|
struct task_struct __current;
|
|
|
|
struct vm_area_struct *alloc_vma(struct mm_struct *mm,
|
|
unsigned long start, unsigned long end,
|
|
pgoff_t pgoff, vma_flags_t vma_flags)
|
|
{
|
|
struct vm_area_struct *vma = vm_area_alloc(mm);
|
|
|
|
if (vma == NULL)
|
|
return NULL;
|
|
|
|
vma->vm_start = start;
|
|
vma->vm_end = end;
|
|
vma_set_pgoff(vma, pgoff);
|
|
vma_set_anon_pgoff(vma, start >> PAGE_SHIFT);
|
|
vma->flags = vma_flags;
|
|
vma_assert_detached(vma);
|
|
|
|
return vma;
|
|
}
|
|
|
|
void detach_free_vma(struct vm_area_struct *vma)
|
|
{
|
|
vma_mark_detached(vma);
|
|
vm_area_free(vma);
|
|
}
|
|
|
|
struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
|
|
unsigned long start, unsigned long end,
|
|
pgoff_t pgoff, vma_flags_t vma_flags)
|
|
{
|
|
struct vm_area_struct *vma = alloc_vma(mm, start, end, pgoff, vma_flags);
|
|
|
|
if (vma == NULL)
|
|
return NULL;
|
|
|
|
if (attach_vma(mm, vma)) {
|
|
detach_free_vma(vma);
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* Reset this counter which we use to track whether writes have
|
|
* begun. Linking to the tree will have caused this to be incremented,
|
|
* which means we will get a false positive otherwise.
|
|
*/
|
|
vma->vm_lock_seq = UINT_MAX;
|
|
|
|
return vma;
|
|
}
|
|
|
|
void reset_dummy_anon_vma(void)
|
|
{
|
|
dummy_anon_vma.was_cloned = false;
|
|
dummy_anon_vma.was_unlinked = false;
|
|
}
|
|
|
|
int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi)
|
|
{
|
|
struct vm_area_struct *vma;
|
|
int count = 0;
|
|
|
|
fail_prealloc = false;
|
|
reset_dummy_anon_vma();
|
|
|
|
vma_iter_set(vmi, 0);
|
|
for_each_vma(*vmi, vma) {
|
|
detach_free_vma(vma);
|
|
count++;
|
|
}
|
|
|
|
mtree_destroy(&mm->mm_mt);
|
|
mm->map_count = 0;
|
|
return count;
|
|
}
|
|
|
|
bool vma_write_started(struct vm_area_struct *vma)
|
|
{
|
|
int seq = vma->vm_lock_seq;
|
|
|
|
/* We reset after each check. */
|
|
vma->vm_lock_seq = UINT_MAX;
|
|
|
|
/* The vma_start_write() stub simply increments this value. */
|
|
return seq > -1;
|
|
}
|
|
|
|
void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
|
|
struct anon_vma_chain *avc, struct anon_vma *anon_vma)
|
|
{
|
|
vma->anon_vma = anon_vma;
|
|
INIT_LIST_HEAD(&vma->anon_vma_chain);
|
|
list_add(&avc->same_vma, &vma->anon_vma_chain);
|
|
avc->anon_vma = vma->anon_vma;
|
|
}
|
|
|
|
void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
|
|
struct anon_vma_chain *avc)
|
|
{
|
|
__vma_set_dummy_anon_vma(vma, avc, &dummy_anon_vma);
|
|
}
|
|
|
|
struct task_struct *get_current(void)
|
|
{
|
|
return &__current;
|
|
}
|
|
|
|
unsigned long rlimit(unsigned int limit)
|
|
{
|
|
return (unsigned long)-1;
|
|
}
|