Skip to contentSkip to conversation

mm: vma: skip anonymous vma when inserting vma to file rmap tree

PATCH Superseded Yang Shi 3 participants 7 messages Mar 6, 2025 – Mar 7, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yang Shi

author
#0 lore raw

PATCH 1/1 mm: vma: skip anonymous vma when inserting vma to file rmap tree

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

However it sounds rare that real life applications would create that many maps with mmap'ing private /dev/zero and share the same struct file, so the cache false sharing problem may be not that bad. But i_mmap_rwsem contention problem seems more real since all /dev/zero private mappings even from different applications share the same struct address_space so the same i_mmap_rwsem.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/Z6RshwXCWhAGoMOK@xsang-OptiPlex-9020/#t

---
 mm/vma.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
mm/vma.c +13 -2
index c7abef5177cc..f4cf85c32b7a 100644
@@ -1648,6 +1648,9 @@ static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)
 void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
 			       struct vm_area_struct *vma)
 {
+	if (vma_is_anonymous(vma))
+		return;
+
 	if (vma->vm_file == NULL)
 		return;
 
@@ -1671,8 +1674,12 @@ void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
  */
 void unlink_file_vma(struct vm_area_struct *vma)
 {
-	struct file *file = vma->vm_file;
+	struct file *file;
+
+	if (vma_is_anonymous(vma))
+		return;
 
+	file = vma->vm_file;
 	if (file) {
 		struct address_space *mapping = file->f_mapping;
 
@@ -1684,9 +1691,13 @@ void unlink_file_vma(struct vm_area_struct *vma)
 
 void vma_link_file(struct vm_area_struct *vma)
 {
-	struct file *file = vma->vm_file;
+	struct file *file;
 	struct address_space *mapping;
 
+	if (vma_is_anonymous(vma))
+		return;
+
+	file = vma->vm_file;
 	if (file) {
 		mapping = file->f_mapping;
 		i_mmap_lock_write(mapping);
Signature · 1 line
2.47.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Yang Shi

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

Yeah sorry about that, but unfortunately something we really do have to think about (among other things, the VMA edge cases are always the source of weirdness...)

Quoted 3 lines

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

Great work tracking that down! Sorry I lost track of the other thread.

Quoted 10 lines

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Ughhhh god haha.

Quoted 3 lines

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

That's insane. Amazing work.

Quoted 10 lines

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

Interesting, I guess you'll take a look at this also?

Quoted 4 lines

However it sounds rare that real life applications would create that many maps with mmap'ing private /dev/zero and share the same struct file, so the cache false sharing problem may be not that bad. But

Right

Quoted 3 lines

i_mmap_rwsem contention problem seems more real since all /dev/zero private mappings even from different applications share the same struct address_space so the same i_mmap_rwsem.

Yeah, lord above, that's crazy. Again, great work!

Quoted 7 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/Z6RshwXCWhAGoMOK@xsang-OptiPlex-9020/#t

Thanks, this looks sensible, other than nits below, so:

Quoted 9 lines
---
 mm/vma.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
mm/vma.c +0 -0
index c7abef5177cc..f4cf85c32b7a 100644

mm/vma.c static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb) · line 1653

Quoted 6 lines
mm/vma.c +3 -0
@@ -1648,6 +1648,9 @@ static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)
 void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
 			       struct vm_area_struct *vma)
 {
+	if (vma_is_anonymous(vma))
+		return;
+

This really needs a comment imo, something simple like:

/* Rare, but e.g. /dev/zero sets vma->vm_file on an anon VMA */

mm/vma.c void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb) · line 1680

Quoted 12 lines
mm/vma.c +4 -1
 	if (vma->vm_file == NULL)
 		return;
 
@@ -1671,8 +1674,12 @@ void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
  */
 void unlink_file_vma(struct vm_area_struct *vma)
 {
-	struct file *file = vma->vm_file;
+	struct file *file;
+
+	if (vma_is_anonymous(vma))
+		return;

Same comment as above.

Quoted 6 lines
mm/vma.c +1 -0
+	file = vma->vm_file;
 	if (file) {
 		struct address_space *mapping = file->f_mapping;
 

mm/vma.c void unlink_file_vma(struct vm_area_struct *vma) · line 1698

Quoted 9 lines
mm/vma.c +3 -1
@@ -1684,9 +1691,13 @@ void unlink_file_vma(struct vm_area_struct *vma)
 
 void vma_link_file(struct vm_area_struct *vma)
 {
-	struct file *file = vma->vm_file;
+	struct file *file;
 	struct address_space *mapping;
 
+	if (vma_is_anonymous(vma))
+		return;

Same comment as above.

Quoted 8 lines
mm/vma.c +2 -1
+
+	file = vma->vm_file;
 	if (file) {
 		mapping = file->f_mapping;
 		i_mmap_lock_write(mapping);
--

2.47.0

#2 lore raw
Quoted 28 lines · Lorenzo Stoakes
Quoted 4 lines · Yang Shi

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

Yeah sorry about that, but unfortunately something we really do have to think about (among other things, the VMA edge cases are always the source of weirdness...)

Quoted 3 lines

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

Great work tracking that down! Sorry I lost track of the other thread.

Quoted 10 lines

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Quoted 8 lines

Ughhhh god haha.

Quoted 3 lines

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

That's insane. Amazing work.

Ok, so the real question (to Yang) is: who are these /dev/zero users that require an insane degree of scalability, and why didn't they switch to regular MAP_ANONYMOUS? Are they in the room with us?

Quoted 11 lines
Quoted 10 lines

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

Quoted 1 line

Interesting, I guess you'll take a look at this also?

... And this is probably a non-issue in 99% of !/dev/zero mmaps unless it's something like libc.so.6 at an insane rate of execs/second.

This seems like a patch in search of a problem and I really don't see why we should wart up the mmap code otherwise. Not that I have a huge problem with this patch, which is somewhat simple and obvious. It'd be great if there was a real workload driving this rather than useless synthetic benchmarks.

Signature · 1 line
Pedro
#3 lore raw
Quoted 36 lines · Pedro Falcato
Quoted 34 lines · Lorenzo Stoakes
Quoted 4 lines · Yang Shi

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

Yeah sorry about that, but unfortunately something we really do have to think about (among other things, the VMA edge cases are always the source of weirdness...)

Quoted 3 lines

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

Great work tracking that down! Sorry I lost track of the other thread.

Quoted 10 lines

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Ughhhh god haha.

Quoted 3 lines

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

Quoted 6 lines
Quoted 2 lines

That's insane. Amazing work.

Ok, so the real question (to Yang) is: who are these /dev/zero users that require an insane degree of scalability, and why didn't they switch to regular MAP_ANONYMOUS? Are they in the room with us?

This could be said about a lot of benchmarks.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

Quoted 4 lines
Quoted 1 line

Interesting, I guess you'll take a look at this also?

... And this is probably a non-issue in 99% of !/dev/zero mmaps unless it's something like libc.so.6 at an insane rate of execs/second.

But the cost of fixing this is...?

Quoted 6 lines

This seems like a patch in search of a problem and I really don't see why we should wart up the mmap code otherwise. Not that I have a huge problem with this patch, which is somewhat simple and obvious. It'd be great if there was a real workload driving this rather than useless synthetic benchmarks.

Disagree with first part. Disallowing a known-broken situation for very low cost in the majority of cases, as well as documenting that such odd creatures exist is valuable.

Improving benchmarks, however synthetic they may be, also valuable.

But on the latter bit, yes it'd be nice if we could get information on real-life scenarios where this is an issue if you have it Yang.

Quoted 3 lines

--
Pedro

The patch is fine as-is AFAIC, and I am very happy to reduce lock contention on heavily contested locks wherever I can, especially when the cost for doing so, in this case, is so low.

author
#4 lore raw
Quoted 50 lines · Lorenzo Stoakes
Quoted 26 lines · Pedro Falcato
Quoted 24 lines · Lorenzo Stoakes
Quoted 4 lines · Yang Shi

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

Yeah sorry about that, but unfortunately something we really do have to think about (among other things, the VMA edge cases are always the source of weirdness...)

Quoted 2 lines

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

Great work tracking that down! Sorry I lost track of the other thread.

Quoted 9 lines

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Ughhhh god haha.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

That's insane. Amazing work.

Ok, so the real question (to Yang) is: who are these /dev/zero users that require an insane degree of scalability, and why didn't they switch to regular MAP_ANONYMOUS? Are they in the room with us?

This could be said about a lot of benchmarks.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

Quoted 3 lines
Quoted 1 line

Interesting, I guess you'll take a look at this also?

... And this is probably a non-issue in 99% of !/dev/zero mmaps unless it's something like libc.so.6 at an insane rate of execs/second.

But the cost of fixing this is...?

Quoted 8 lines
Quoted 5 lines

This seems like a patch in search of a problem and I really don't see why we should wart up the mmap code otherwise. Not that I have a huge problem with this patch, which is somewhat simple and obvious. It'd be great if there was a real workload driving this rather than useless synthetic benchmarks.

Disagree with first part. Disallowing a known-broken situation for very low cost in the majority of cases, as well as documenting that such odd creatures exist is valuable.

Yes, I agree. This is one of the points of this patch too. The commit log may focus too much on the benchmark improvement. I will add more words about fixing the special odd behavior.

Quoted 5 lines

Improving benchmarks, however synthetic they may be, also valuable.

But on the latter bit, yes it'd be nice if we could get information on real-life scenarios where this is an issue if you have it Yang.

I wish I could. As I said in the reply to Pedro, creating anonymous mapping via mmap'ing private /dev/zero is an established way. So we can not rule out it is *NOT* used.

Thanks,
Yang

Quoted 6 lines
Quoted 3 lines

--
Pedro

The patch is fine as-is AFAIC, and I am very happy to reduce lock contention on heavily contested locks wherever I can, especially when the cost for doing so, in this case, is so low.

author
#5 lore raw
In reply to Pedro Falcato
Quoted 26 lines · Pedro Falcato
Quoted 24 lines · Lorenzo Stoakes
Quoted 4 lines · Yang Shi

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

Yeah sorry about that, but unfortunately something we really do have to think about (among other things, the VMA edge cases are always the source of weirdness...)

Quoted 2 lines

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

Great work tracking that down! Sorry I lost track of the other thread.

Quoted 9 lines

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Ughhhh god haha.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

That's insane. Amazing work.

Ok, so the real question (to Yang) is: who are these /dev/zero users that require an insane degree of scalability, and why didn't they switch to regular MAP_ANONYMOUS? Are they in the room with us?

I wish I could. Who knows what applications use /dev/zero. But mmap'ing private /dev/zero is definitely an established way to create anonymous mappings. So we can't rule out it is *NOT* used.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

Interesting, I guess you'll take a look at this also?

Quoted 8 lines

... And this is probably a non-issue in 99% of !/dev/zero mmaps unless it's something like libc.so.6 at an insane rate of execs/second.

This seems like a patch in search of a problem and I really don't see why we should wart up the mmap code otherwise. Not that I have a huge problem with this patch, which is somewhat simple and obvious. It'd be great if there was a real workload driving this rather than useless synthetic benchmarks.

Inserting an anonymous VMA to file rmap tree is definitely not expected by other parts of kernel. This is a broken behavior (or special case) IMHO. Making it behave in right way and making no surprise (also less special) to other parts of kernel is worth it even though we don't count the potential performance gain.

Thanks,
Yang

Quoted 1 line
author
#6 lore raw
In reply to Lorenzo Stoakes
Quoted 37 lines · Lorenzo Stoakes
Quoted 4 lines · Yang Shi

LKP reported 800% performance improvement for small-allocs benchmark from vm-scalability [1] with patch ("/dev/zero: make private mapping full anonymous mapping") [2], but the patch was nack'ed since it changes the output of smaps somewhat.

Yeah sorry about that, but unfortunately something we really do have to think about (among other things, the VMA edge cases are always the source of weirdness...)

Quoted 2 lines

The profiling shows one of the major sources of the performance improvement is the less contention to i_mmap_rwsem.

Great work tracking that down! Sorry I lost track of the other thread.

Quoted 9 lines

The small-allocs benchmark creates a lot of 40K size memory maps by mmap'ing private /dev/zero then triggers page fault on the mappings. When creating private mapping for /dev/zero, the anonymous VMA is created, but it has valid vm_file. Kernel basically assumes anonymous VMAs should have NULL vm_file, for example, mmap inserts VMA to the file rmap tree if vm_file is not NULL. So the private /dev/zero mapping will be inserted to the file rmap tree, this resulted in the contention to i_mmap_rwsem. But it is actually anonymous VMA, so it is pointless to insert it to file rmap tree.

Ughhhh god haha.

Quoted 2 lines

Skip anonymous VMA for this case. Over 400% performance improvement was reported [3].

That's insane. Amazing work.

Quoted 9 lines

It is not on par with the 800% improvement from the original patch. It is because page fault handler needs to access some members of struct file if vm_file is not NULL, for example, f_mode and f_mapping. They are in the same cacheline with file refcount. When mmap'ing a file the file refcount is inc'ed and dec'ed, this caused bad cache false sharing problem. The further debug showed checking whether the VMA is anonymous or not can alleviate the problem. But I'm not sure whether it is the best way to handle it, maybe we should consider shuffle the layout of struct file.

Quoted 1 line

Interesting, I guess you'll take a look at this also?

I looked into it, but I can't determine what is the best way. We basically have two options:

1. Regroup the layout of struct file, for example, move refcount to another cache line. But I'm not sure whether it will cause any other problems. This needs extensive testing.
2. Put refcount in a dedicated cacheline. Currently struct file is 192 bytes, a dedicated cacheline will increase the size to 256 bytes. I'm not sure whether it is worth it or not.

Quoted 15 lines
Quoted 4 lines

However it sounds rare that real life applications would create that many maps with mmap'ing private /dev/zero and share the same struct file, so the cache false sharing problem may be not that bad. But

Right

Quoted 3 lines

i_mmap_rwsem contention problem seems more real since all /dev/zero private mappings even from different applications share the same struct address_space so the same i_mmap_rwsem.

Yeah, lord above, that's crazy. Again, great work!

Quoted 4 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/Z6RshwXCWhAGoMOK@xsang-OptiPlex-9020/#t

Quoted 5 lines
Quoted 2 lines

Thanks, this looks sensible, other than nits below, so:

Thank you.

Quoted 18 lines
Quoted 10 lines
---
  mm/vma.c | 15 +++++++++++++--
  1 file changed, 13 insertions(+), 2 deletions(-)
mm/vma.c +0 -0
index c7abef5177cc..f4cf85c32b7a 100644

mm/vma.c static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb) · line 1653

Quoted 6 lines
mm/vma.c +3 -0
@@ -1648,6 +1648,9 @@ static void unlink_file_vma_batch_process(struct unlink_vma_file_batch *vb)
  void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
  			       struct vm_area_struct *vma)
  {
+	if (vma_is_anonymous(vma))
+		return;
+

This really needs a comment imo, something simple like:

Quoted 1 line

/* Rare, but e.g. /dev/zero sets vma->vm_file on an anon VMA */

Thanks for the suggestion, will add the comment in the new spin.

Yang

Quoted 39 lines

mm/vma.c void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb) · line 1680

Quoted 13 lines
mm/vma.c +4 -1
  	if (vma->vm_file == NULL)
  		return;
 
@@ -1671,8 +1674,12 @@ void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
   */
  void unlink_file_vma(struct vm_area_struct *vma)
  {
-	struct file *file = vma->vm_file;
+	struct file *file;
+
+	if (vma_is_anonymous(vma))
+		return;

Same comment as above.

Quoted 5 lines
mm/vma.c +1 -0
+	file = vma->vm_file;
  	if (file) {
  		struct address_space *mapping = file->f_mapping;
 

mm/vma.c void unlink_file_vma(struct vm_area_struct *vma) · line 1698

Quoted 9 lines
mm/vma.c +3 -1
@@ -1684,9 +1691,13 @@ void unlink_file_vma(struct vm_area_struct *vma)
 
  void vma_link_file(struct vm_area_struct *vma)
  {
-	struct file *file = vma->vm_file;
+	struct file *file;
  	struct address_space *mapping;
 
+	if (vma_is_anonymous(vma))
+		return;

Same comment as above.

Quoted 8 lines
mm/vma.c +2 -1
+
+	file = vma->vm_file;
  	if (file) {
  		mapping = file->f_mapping;
  		i_mmap_lock_write(mapping);
--

2.47.0