Skip to content

Commit b1e5a3d

Browse files
joelagnelakpm00
authored andcommitted
mm/mremap: allow moves within the same VMA for stack moves
For the stack move happening in shift_arg_pages(), the move is happening within the same VMA which spans the old and new ranges. In case the aligned address happens to fall within that VMA, allow such moves and don't abort the mremap alignment optimization. In the regular non-stack mremap case, we cannot allow any such moves as will end up destroying some part of the mapping (either the source of the move, or part of the existing mapping). So just avoid it for stack moves. Link: https://lkml.kernel.org/r/20230903151328.2981432-3-joel@joelfernandes.org Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org> Reviewed-by: Lorenzo Stoakes <lstoakes@gmail.com> Cc: Kalesh Singh <kaleshsingh@google.com> Cc: "Kirill A. Shutemov" <kirill@shutemov.name> Cc: Liam R. Howlett <Liam.Howlett@oracle.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Lokesh Gidra <lokeshgidra@google.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Paul E. McKenney <paulmck@kernel.org> Cc: Shuah Khan <shuah@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent af8ca1c commit b1e5a3d

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

fs/exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
713713
* process cleanup to remove whatever mess we made.
714714
*/
715715
if (length != move_page_tables(vma, old_start,
716-
vma, new_start, length, false))
716+
vma, new_start, length, false, true))
717717
return -ENOMEM;
718718

719719
lru_add_drain();

include/linux/mm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ int get_cmdline(struct task_struct *task, char *buffer, int buflen);
24802480
extern unsigned long move_page_tables(struct vm_area_struct *vma,
24812481
unsigned long old_addr, struct vm_area_struct *new_vma,
24822482
unsigned long new_addr, unsigned long len,
2483-
bool need_rmap_locks);
2483+
bool need_rmap_locks, bool for_stack);
24842484

24852485
/*
24862486
* Flags used by change_protection(). For now we make it a bitmap so

mm/mremap.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,13 @@ static bool move_pgt_entry(enum pgt_entry entry, struct vm_area_struct *vma,
490490
}
491491

492492
/*
493-
* A helper to check if a previous mapping exists. Required for
494-
* move_page_tables() and realign_addr() to determine if a previous mapping
495-
* exists before we can do realignment optimizations.
493+
* A helper to check if aligning down is OK. The aligned address should fall
494+
* on *no mapping*. For the stack moving down, that's a special move within
495+
* the VMA that is created to span the source and destination of the move,
496+
* so we make an exception for it.
496497
*/
497498
static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_align,
498-
unsigned long mask)
499+
unsigned long mask, bool for_stack)
499500
{
500501
unsigned long addr_masked = addr_to_align & mask;
501502

@@ -504,9 +505,13 @@ static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_ali
504505
* of the corresponding VMA, we can't align down or we will destroy part
505506
* of the current mapping.
506507
*/
507-
if (vma->vm_start != addr_to_align)
508+
if (!for_stack && vma->vm_start != addr_to_align)
508509
return false;
509510

511+
/* In the stack case we explicitly permit in-VMA alignment. */
512+
if (for_stack && addr_masked >= vma->vm_start)
513+
return true;
514+
510515
/*
511516
* Make sure the realignment doesn't cause the address to fall on an
512517
* existing mapping.
@@ -517,7 +522,7 @@ static bool can_align_down(struct vm_area_struct *vma, unsigned long addr_to_ali
517522
/* Opportunistically realign to specified boundary for faster copy. */
518523
static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old_vma,
519524
unsigned long *new_addr, struct vm_area_struct *new_vma,
520-
unsigned long mask)
525+
unsigned long mask, bool for_stack)
521526
{
522527
/* Skip if the addresses are already aligned. */
523528
if ((*old_addr & ~mask) == 0)
@@ -528,8 +533,8 @@ static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old
528533
return;
529534

530535
/* Ensure realignment doesn't cause overlap with existing mappings. */
531-
if (!can_align_down(old_vma, *old_addr, mask) ||
532-
!can_align_down(new_vma, *new_addr, mask))
536+
if (!can_align_down(old_vma, *old_addr, mask, for_stack) ||
537+
!can_align_down(new_vma, *new_addr, mask, for_stack))
533538
return;
534539

535540
*old_addr = *old_addr & mask;
@@ -539,7 +544,7 @@ static void try_realign_addr(unsigned long *old_addr, struct vm_area_struct *old
539544
unsigned long move_page_tables(struct vm_area_struct *vma,
540545
unsigned long old_addr, struct vm_area_struct *new_vma,
541546
unsigned long new_addr, unsigned long len,
542-
bool need_rmap_locks)
547+
bool need_rmap_locks, bool for_stack)
543548
{
544549
unsigned long extent, old_end;
545550
struct mmu_notifier_range range;
@@ -559,9 +564,9 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
559564
* If possible, realign addresses to PMD boundary for faster copy.
560565
* Only realign if the mremap copying hits a PMD boundary.
561566
*/
562-
if ((vma != new_vma)
563-
&& (len >= PMD_SIZE - (old_addr & ~PMD_MASK)))
564-
try_realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK);
567+
if (len >= PMD_SIZE - (old_addr & ~PMD_MASK))
568+
try_realign_addr(&old_addr, vma, &new_addr, new_vma, PMD_MASK,
569+
for_stack);
565570

566571
flush_cache_range(vma, old_addr, old_end);
567572
mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma->vm_mm,
@@ -708,7 +713,7 @@ static unsigned long move_vma(struct vm_area_struct *vma,
708713
}
709714

710715
moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
711-
need_rmap_locks);
716+
need_rmap_locks, false);
712717
if (moved_len < old_len) {
713718
err = -ENOMEM;
714719
} else if (vma->vm_ops && vma->vm_ops->mremap) {
@@ -722,7 +727,7 @@ static unsigned long move_vma(struct vm_area_struct *vma,
722727
* and then proceed to unmap new area instead of old.
723728
*/
724729
move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
725-
true);
730+
true, false);
726731
vma = new_vma;
727732
old_len = new_len;
728733
old_addr = new_addr;

0 commit comments

Comments
 (0)