Skip to contentSkip to conversation

mm: lock a vma before stack expansion

PATCH Superseded Suren Baghdasaryan 6 participants 14 messages Jul 7, 2023 – Jul 8, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mm: lock a vma before stack expansion

With recent changes necessitating mmap_lock to be held for write while expanding a stack, per-VMA locks should follow the same rules and be write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

---
 mm/mmap.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/mmap.c +4 -0
index 204ddcd52625..c66e4622a557 100644
@@ -1977,6 +1977,8 @@ static int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 		return -ENOMEM;
 	}
 
+	/* Lock the VMA before expanding to prevent concurrent page faults */
+	vma_start_write(vma);
 	/*
 	 * vma->vm_start/vm_end cannot change under us because the caller
 	 * is required to hold the mmap_lock in read mode.  We need the
@@ -2064,6 +2066,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 		return -ENOMEM;
 	}
 
+	/* Lock the VMA before expanding to prevent concurrent page faults */
+	vma_start_write(vma);
 	/*
 	 * vma->vm_start/vm_end cannot change under us because the caller
 	 * is required to hold the mmap_lock in read mode.  We need the
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#1 lore raw

PATCH 2/2 mm: lock newly mapped VMA which can be modified after it becomes visible

mmap_region adds a newly created VMA into VMA tree and might modify it afterwards before dropping the mmap_lock. This poses a problem for page faults handled under per-VMA locks because they don't take the mmap_lock and can stumble on this VMA while it's still being modified. Currently this does not pose a problem since post-addition modifications are done only for file-backed VMAs, which are not handled under per-VMA lock. However, once support for handling file-backed page faults with per-VMA locks is added, this will become a race.
Fix this by write-locking the VMA before inserting it into the VMA tree. Other places where a new VMA is added into VMA tree do not modify it after the insertion, so do not need the same locking.

---
 mm/mmap.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mmap.c +2 -0
index c66e4622a557..84c71431a527 100644
@@ -2812,6 +2812,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 	if (vma->vm_file)
 		i_mmap_lock_write(vma->vm_file->f_mapping);
 
+	/* Lock the VMA since it is modified after insertion into VMA tree */
+	vma_start_write(vma);
 	vma_iter_store(&vmi, vma);
 	mm->map_count++;
 	if (vma->vm_file) {
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#4 lore raw
In reply to Suren Baghdasaryan
FYI: kernel test robot notices the stable kernel rule is not satisfied.

Hi,

Thanks for your patch.

FYI: kernel test robot notices the stable kernel rule is not satisfied.

Rule: 'Cc: [email protected]' or 'commit <sha1> upstream.'
Subject: [PATCH 1/2] mm: lock a vma before stack expansion

The check is based on https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#5 lore raw
In reply to Suren Baghdasaryan
Quoted 4 lines · On Thu, 6 Jul 2023 21:32:10 -0700 Suren Baghdasaryan <[email protected]> wrote:

With recent changes necessitating mmap_lock to be held for write while expanding a stack, per-VMA locks should follow the same rules and be write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

What are the possible runtime effects of this change?

Quoted 22 lines
mm/mmap.c +4 -0
@@ -1977,6 +1977,8 @@ static int expand_upwards(struct vm_area_struct *vma, unsigned long address)
 		return -ENOMEM;
 	}
 
+	/* Lock the VMA before expanding to prevent concurrent page faults */
+	vma_start_write(vma);
 	/*
 	 * vma->vm_start/vm_end cannot change under us because the caller
 	 * is required to hold the mmap_lock in read mode.  We need the
@@ -2064,6 +2066,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
 		return -ENOMEM;
 	}
 
+	/* Lock the VMA before expanding to prevent concurrent page faults */
+	vma_start_write(vma);
 	/*
 	 * vma->vm_start/vm_end cannot change under us because the caller
 	 * is required to hold the mmap_lock in read mode.  We need the
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#2 lore raw

Re: [PATCH 2/2] mm: lock newly mapped VMA which can be modified after it becomes visible

In reply to Suren Baghdasaryan
Quoted 22 lines · Suren Baghdasaryan

mmap_region adds a newly created VMA into VMA tree and might modify it afterwards before dropping the mmap_lock. This poses a problem for page faults handled under per-VMA locks because they don't take the mmap_lock and can stumble on this VMA while it's still being modified. Currently this does not pose a problem since post-addition modifications are done only for file-backed VMAs, which are not handled under per-VMA lock. However, once support for handling file-backed page faults with per-VMA locks is added, this will become a race.
Fix this by write-locking the VMA before inserting it into the VMA tree. Other places where a new VMA is added into VMA tree do not modify it after the insertion, so do not need the same locking.

---
 mm/mmap.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mmap.c +0 -0
index c66e4622a557..84c71431a527 100644

mm/mmap.c unsigned long mmap_region(struct file *file, unsigned long addr, · line 2815

Quoted 4 lines
mm/mmap.c +1 -0
@@ -2812,6 +2812,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 	if (vma->vm_file)
 		i_mmap_lock_write(vma->vm_file->f_mapping);
 
+	/* Lock the VMA since it is modified after insertion into VMA tree */

So it is modified, but that i_mmap_lock_write() directly above this comment is potentially moving below the insert and that is why this lock is needed.

Quoted 7 lines
mm/mmap.c +1 -0
+	vma_start_write(vma);
 	vma_iter_store(&vmi, vma);
 	mm->map_count++;
 	if (vma->vm_file) {
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#7 lore raw
In reply to Suren Baghdasaryan

…

Quoted 2 lines

write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

1. Would it a bit nicer to put the second sentence on a separate line in such a change description?

2. I noticed that you put the address “[email protected]” into the message field “Cc”.
Would you like to specify such a hint as a tag?

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.4#n264

3. How do you think about to add the tag “Fixes”?

4. Will a cover letter become helpful also for the presented small patch series?

Regards,
Markus

#8 lore raw
Quoted 11 lines · Markus Elfring

…

Quoted 2 lines

write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

1. Would it a bit nicer to put the second sentence on a separate line in such a change description?

2. I noticed that you put the address “[email protected]” into the message field “Cc”.
Would you like to specify such a hint as a tag?

Quoted 6 lines

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.4#n264

3. How do you think about to add the tag “Fixes”?

4. Will a cover letter become helpful also for the presented small patch series?

Markus, your nitpicking is not useful. Please stop.

#6 lore raw
In reply to Andrew Morton
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Thu, 6 Jul 2023 21:32:10 -0700 Suren Baghdasaryan <[email protected]> wrote:

With recent changes necessitating mmap_lock to be held for write while expanding a stack, per-VMA locks should follow the same rules and be write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

What are the possible runtime effects of this change?

During the stack expansion concurrent page faults would have to wait and visa versa (stack expansion would have to wait if there are ongoing page faults). I think it's the same runtime effects as the recent similar change requiring mmap_lock to be write lock before stack expansion.

Quoted 27 lines
Quoted 22 lines
mm/mmap.c +4 -1
@@ -1977,6 +1977,8 @@ static int expand_upwards(struct vm_area_struct *vma, unsigned long address)
              return -ENOMEM;
      }
 
+     /* Lock the VMA before expanding to prevent concurrent page faults */
+     vma_start_write(vma);
      /*
       * vma->vm_start/vm_end cannot change under us because the caller
       * is required to hold the mmap_lock in read mode.  We need the
@@ -2064,6 +2066,8 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address)
              return -ENOMEM;
      }
 
+     /* Lock the VMA before expanding to prevent concurrent page faults */
+     vma_start_write(vma);
      /*
       * vma->vm_start/vm_end cannot change under us because the caller
       * is required to hold the mmap_lock in read mode.  We need the
--
Quoted 1 line

2.41.0.255.g8b1d071c50-goog

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

#9 lore raw
In reply to Matthew Wilcox
Quoted 8 lines · Matthew Wilcox
Quoted 6 lines · Markus Elfring

…

Quoted 2 lines

write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

1. Would it a bit nicer to put the second sentence on a separate line in such a change description?

Maybe. Will do if there is a need to post a v2.

Quoted 7 lines
Quoted 7 lines

2. I noticed that you put the address “[email protected]” into the message field “Cc”.
Would you like to specify such a hint as a tag?

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.4#n264

Yeah, I always forget that :(

Quoted 2 lines
Quoted 2 lines

3. How do you think about to add the tag “Fixes”?

I thought about it but was not sure which patch I should list under such tag because the rules for stack expansion changed recently.

Quoted 2 lines
Quoted 2 lines

4. Will a cover letter become helpful also for the presented small patch series?

Not much to say other than "add some missing locking" :)

Quoted 2 lines

Markus, your nitpicking is not useful. Please stop.

I'll fix the nits, at least the ones I can, if there is a need for v2.

#3 lore raw

Re: [PATCH 2/2] mm: lock newly mapped VMA which can be modified after it becomes visible

In reply to Liam R. Howlett
Quoted 27 lines · Liam R. Howlett
Quoted 25 lines · Suren Baghdasaryan

mmap_region adds a newly created VMA into VMA tree and might modify it afterwards before dropping the mmap_lock. This poses a problem for page faults handled under per-VMA locks because they don't take the mmap_lock and can stumble on this VMA while it's still being modified. Currently this does not pose a problem since post-addition modifications are done only for file-backed VMAs, which are not handled under per-VMA lock. However, once support for handling file-backed page faults with per-VMA locks is added, this will become a race.
Fix this by write-locking the VMA before inserting it into the VMA tree. Other places where a new VMA is added into VMA tree do not modify it after the insertion, so do not need the same locking.

---
 mm/mmap.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mmap.c +0 -0
index c66e4622a557..84c71431a527 100644
@@ -2812,6 +2812,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
      if (vma->vm_file)
              i_mmap_lock_write(vma->vm_file->f_mapping);
 
Quoted 5 lines

mm/mmap.c unsigned long mmap_region(struct file *file, unsigned long addr, · line 2815

Quoted 1 line
mm/mmap.c +1 -0
+     /* Lock the VMA since it is modified after insertion into VMA tree */

So it is modified, but that i_mmap_lock_write() directly above this comment is potentially moving below the insert and that is why this lock is needed.

Correct, we should not rely on i_mmap_lock_write() which can be moved (and is suggested to be moved in
https://lore.kernel.org/all/[email protected]/).

Quoted 12 lines
Quoted 8 lines
mm/mmap.c +1 -1
+     vma_start_write(vma);
      vma_iter_store(&vmi, vma);
      mm->map_count++;
      if (vma->vm_file) {
--

2.41.0.255.g8b1d071c50-goog

--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

#13 lore raw
In reply to Matthew Wilcox
Quoted 11 lines
Quoted 11 lines

…

Quoted 2 lines

write-locked to prevent page faults into the VMA being expanded. Add the necessary locking.

1. Would it a bit nicer to put the second sentence on a separate line in such a change description?

2. I noticed that you put the address “[email protected]” into the message field “Cc”.
Would you like to specify such a hint as a tag?

Quoted 8 lines
Quoted 6 lines

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.4#n264

3. How do you think about to add the tag “Fixes”?

4. Will a cover letter become helpful also for the presented small patch series?

Markus, your nitpicking is not useful. …

Do you like patch reviews more by other contributors?

Regards,
Markus

#10 lore raw
In reply to Suren Baghdasaryan

…

Quoted 3 lines
Quoted 1 line

Markus, your nitpicking is not useful. Please stop.

I'll fix the nits, at least the ones I can, if there is a need for v2.

Thanks for such a constructive feedback.

Would you like to take any further information better into account for subsequent patch variants?

Regards,
Markus

#11 lore raw
Quoted 10 lines · Markus Elfring

…

Quoted 3 lines
Quoted 1 line

Markus, your nitpicking is not useful. Please stop.

I'll fix the nits, at least the ones I can, if there is a need for v2.

Thanks for such a constructive feedback.

Would you like to take any further information better into account for subsequent patch variants?

All feedback is appreciated. The comments you provided so far seem to be nice-to-haves but not critical enough to post a new version of the patch IMHO. If something more substantial is found that requires a new version then I'll address your feedback there as well. Thanks,
Suren.

Quoted 3 lines

Regards,
Markus

#12 lore raw
Quoted 4 lines
Quoted 2 lines

Would you like to take any further information better into account for subsequent patch variants?

All feedback is appreciated.

Thanks for such a positive response.

Quoted 3 lines

The comments you provided so far seem to
be nice-to-haves but not critical enough to post a new version of the patch IMHO.

I guess that our dialogue can increase the probability for more desirable improvements.

I got used to point items out which can affect also the development process for Linux software components.
It seems that some contributors stumble still on difficulties to handle presented patch review approaches in more constructive ways.

Quoted 2 lines

If something more substantial is found that requires a new
version then I'll address your feedback there as well.

Will the chances grow to pick further recommendations, guidelines and related change possibilities up?

Regards,
Markus