Skip to contentSkip to conversation

mm/mmap.c: prev could be retrieved from vma->vm_prev

PATCH Superseded Wei Yang 5 participants 13 messages Aug 14, 2019 – Aug 21, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/3 mm/mmap.c: prev could be retrieved from vma->vm_prev

Currently __vma_unlink_common handles two cases:

* has_prev
* or not

When has_prev is false, it is obvious prev is calculated from vma->vm_prev in __vma_unlink_common.

When has_prev is true, the prev is passed through from __vma_unlink_prev in __vma_adjust for non-case 8. And at the beginning next is calculated from vma->vm_next, which implies vma is next->vm_prev.

The above statement sounds a little complicated, while to think in another point of view, no matter whether vma and next is swapped, the mmap link list still preserves its property. It is proper to access vma->vm_prev.

---
 mm/mmap.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
mm/mmap.c +7 -13
index b8072630766f..3d56340fea36 100644
@@ -675,23 +675,17 @@ static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma)
 
 static __always_inline void __vma_unlink_common(struct mm_struct *mm,
 						struct vm_area_struct *vma,
-						struct vm_area_struct *prev,
-						bool has_prev,
 						struct vm_area_struct *ignore)
 {
-	struct vm_area_struct *next;
+	struct vm_area_struct *prev, *next;
 
 	vma_rb_erase_ignore(vma, &mm->mm_rb, ignore);
 	next = vma->vm_next;
-	if (has_prev)
+	prev = vma->vm_prev;
+	if (prev)
 		prev->vm_next = next;
-	else {
-		prev = vma->vm_prev;
-		if (prev)
-			prev->vm_next = next;
-		else
-			mm->mmap = next;
-	}
+	else
+		mm->mmap = next;
 	if (next)
 		next->vm_prev = prev;
 
@@ -703,7 +697,7 @@ static inline void __vma_unlink_prev(struct mm_struct *mm,
 				     struct vm_area_struct *vma,
 				     struct vm_area_struct *prev)
 {
-	__vma_unlink_common(mm, vma, prev, true, vma);
+	__vma_unlink_common(mm, vma, vma);
 }
 
 /*
@@ -891,7 +885,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
 			 * "next" (which is stored in post-swap()
 			 * "vma").
 			 */
-			__vma_unlink_common(mm, next, NULL, false, vma);
+			__vma_unlink_common(mm, next, vma);
 		if (file)
 			__remove_shared_vm_struct(next, file, mapping);
 	} else if (insert) {
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/3 mm/mmap.c: __vma_unlink_prev is not necessary now

The third parameter of __vma_unlink_common could differentiate these two types. __vma_unlink_prev is not necessary now.

---
 mm/mmap.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
mm/mmap.c +1 -8
index 3d56340fea36..3fde0ec18554 100644
@@ -693,13 +693,6 @@ static __always_inline void __vma_unlink_common(struct mm_struct *mm,
 	vmacache_invalidate(mm);
 }
 
-static inline void __vma_unlink_prev(struct mm_struct *mm,
-				     struct vm_area_struct *vma,
-				     struct vm_area_struct *prev)
-{
-	__vma_unlink_common(mm, vma, vma);
-}
-
 /*
  * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
  * is already present in an i_mmap tree without adjusting the tree.
@@ -874,7 +867,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
 		 * us to remove next before dropping the locks.
 		 */
 		if (remove_next != 3)
-			__vma_unlink_prev(mm, next, vma);
+			__vma_unlink_common(mm, next, next);
 		else
 			/*
 			 * vma is not before next if they've been
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH 3/3 mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

In reply to Wei Yang

Just make the code a little easy to read.

---
Note: For nommu part, the code is not tested.

---
 mm/internal.h |  1 +
 mm/mmap.c     | 12 +-----------
 mm/nommu.c    |  8 +-------
 mm/util.c     | 14 ++++++++++++++
 4 files changed, 17 insertions(+), 18 deletions(-)
mm/internal.h +1 -0
index 41a49574acc3..4736aeb37dae 100644
@@ -291,6 +291,7 @@ static inline bool is_data_mapping(vm_flags_t flags)
 /* mm/util.c */
 void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
 		struct vm_area_struct *prev);
+void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma);
 
 #ifdef CONFIG_MMU
 extern long populate_vma_page_range(struct vm_area_struct *vma,
mm/mmap.c +1 -11
index 3fde0ec18554..aa66753b175e 100644
@@ -677,18 +677,8 @@ static __always_inline void __vma_unlink_common(struct mm_struct *mm,
 						struct vm_area_struct *vma,
 						struct vm_area_struct *ignore)
 {
-	struct vm_area_struct *prev, *next;
-
 	vma_rb_erase_ignore(vma, &mm->mm_rb, ignore);
-	next = vma->vm_next;
-	prev = vma->vm_prev;
-	if (prev)
-		prev->vm_next = next;
-	else
-		mm->mmap = next;
-	if (next)
-		next->vm_prev = prev;
-
+	__vma_unlink_list(mm, vma);
 	/* Kill the cache */
 	vmacache_invalidate(mm);
 }
mm/nommu.c +1 -7
index 12a66fbeb988..1a403f65b99e 100644
@@ -673,13 +673,7 @@ static void delete_vma_from_mm(struct vm_area_struct *vma)
 	/* remove from the MM's tree and list */
 	rb_erase(&vma->vm_rb, &mm->mm_rb);
 
-	if (vma->vm_prev)
-		vma->vm_prev->vm_next = vma->vm_next;
-	else
-		mm->mmap = vma->vm_next;
-
-	if (vma->vm_next)
-		vma->vm_next->vm_prev = vma->vm_prev;
+	__vma_unlink_list(mm, vma);
 }
 
 /*
mm/util.c +14 -0
index 80632db29247..5f113cd0acad 100644
@@ -281,6 +281,20 @@ void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
 		next->vm_prev = vma;
 }
 
+void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma)
+{
+	struct vm_area_struct *prev, *next;
+
+	next = vma->vm_next;
+	prev = vma->vm_prev;
+	if (prev)
+		prev->vm_next = next;
+	else
+		mm->mmap = next;
+	if (next)
+		next->vm_prev = prev;
+}
+
 /* Check if the vma is being used as a stack by this task */
 int vma_is_stack_for_current(struct vm_area_struct *vma)
 {
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Btw, is there any good reason we don't use a list_head for vma linkage?

author
#4 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Signature · 2 lines
Wei Yang
Help you, Help me
#5 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

#6 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 11 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

Seems it will touch many code ...

Signature · 2 lines
Wei Yang
Help you, Help me
#7 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

In reply to Vlastimil Babka
Quoted 11 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

I'm working to get rid of vm_prev and vm_next, so it would probably be wasted effort.

author
#8 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Quoted 8 lines
Quoted 5 lines

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

I'm working to get rid of vm_prev and vm_next, so it would probably be wasted effort.

You mean replace it with list_head?

Signature · 2 lines
Wei Yang
Help you, Help me
#9 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 11 lines · Wei Yang
Quoted 10 lines · Matthew Wilcox
Quoted 9 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

I'm working to get rid of vm_prev and vm_next, so it would probably be wasted effort.

You mean replace it with list_head?

No, replace the rbtree with a new tree. https://lwn.net/Articles/787629/

author
#10 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 15 lines · Matthew Wilcox
Quoted 14 lines · Wei Yang
Quoted 13 lines · Matthew Wilcox
Quoted 11 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I'm working to get rid of vm_prev and vm_next, so it would probably be wasted effort.

You mean replace it with list_head?

No, replace the rbtree with a new tree. https://lwn.net/Articles/787629/

Sounds interesting.

While I am not sure the plan is settled down, and how long it would take to replace the rb_tree with maple tree. I guess it would probably take some time to get merged upstream.

IMHO, it would be good to have this cleanup in current kernel. Do you agree?

Signature · 2 lines
Wei Yang
Help you, Help me
#11 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 23 lines · Wei Yang
Quoted 15 lines · Matthew Wilcox
Quoted 14 lines · Wei Yang
Quoted 13 lines · Matthew Wilcox
Quoted 11 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I'm working to get rid of vm_prev and vm_next, so it would probably be wasted effort.

You mean replace it with list_head?

No, replace the rbtree with a new tree. https://lwn.net/Articles/787629/

Quoted 7 lines

Sounds interesting.

While I am not sure the plan is settled down, and how long it would take to replace the rb_tree with maple tree. I guess it would probably take some time to get merged upstream.

IMHO, it would be good to have this cleanup in current kernel. Do you agree?

The three cleanups you've posted are fine. Doing more work (ie the list_head) seems like wasted effort to me.

author
#12 lore raw

Re: [PATCH 3/3] mm/mmap.c: extract __vma_unlink_list as counter part for __vma_link_list

Quoted 26 lines · Matthew Wilcox
Quoted 25 lines · Wei Yang
Quoted 15 lines · Matthew Wilcox
Quoted 14 lines · Wei Yang
Quoted 13 lines · Matthew Wilcox
Quoted 11 lines · Vlastimil Babka
Quoted 4 lines · Wei Yang
Quoted 1 line · Christoph Hellwig

Btw, is there any good reason we don't use a list_head for vma linkage?

Not sure, maybe there is some historical reason?

Seems it was single-linked until 2010 commit 297c5eee3724 ("mm: make the vma list be doubly linked") and I guess it was just simpler to add the vm_prev link.

Conversion to list_head might be an interesting project for some "advanced beginner" in the kernel :)

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I'm working to get rid of vm_prev and vm_next, so it would probably be wasted effort.

You mean replace it with list_head?

No, replace the rbtree with a new tree. https://lwn.net/Articles/787629/

Sounds interesting.

Quoted 8 lines
Quoted 5 lines

While I am not sure the plan is settled down, and how long it would take to replace the rb_tree with maple tree. I guess it would probably take some time to get merged upstream.

IMHO, it would be good to have this cleanup in current kernel. Do you agree?

The three cleanups you've posted are fine. Doing more work (ie the list_head) seems like wasted effort to me.

Ah, got your point. I misunderstand it.

Signature · 2 lines
Wei Yang
Help you, Help me