mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-10-26 10:57:38 +08:00
To lay the foundation for a future change that converts MAP_PRIVATE-/dev/zero mappings to be truly anonymous, add the ability to uniquely identify these mappings. With the memory character device now part of mm/ this is trivially achievable through a file_is_dev_zero() predicate that simply tests that the file operation hooks are zero_fops. Also update userland VMA tests to expose file_is_dev_zero() and provide a stub zero_fops for testing. Link: https://lore.kernel.org/20260926-map-private-dev-zero-v3-2-d4781e84ccfc@kernel.org Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: David Hildenbrand (Arm) <david@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Liam R. Howlett <liam@infradead.org> Cc: Vlastimil Babka <vbabka@kernel.org> Cc: Jann Horn <jannh@google.com> Cc: Pedro Falcato <pfalcato@suse.de> Cc: Mike Rapoport <rppt@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Hugh Dickins <hughd@google.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Jan Kara <jack@suse.cz>
133 lines
2.8 KiB
C
133 lines
2.8 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;
|
|
|
|
static int mmap_zero_prepare(struct vm_area_desc *desc)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
const struct file_operations zero_fops = {
|
|
.mmap_prepare = mmap_zero_prepare,
|
|
};
|
|
|
|
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;
|
|
}
|