diff mbox series

[v4,64/66] nommu: Remove uses of VMA linked list

Message ID 20211201142918.921493-65-Liam.Howlett@oracle.com (mailing list archive)
State New
Headers show
Series Introducing the Maple Tree | expand

Commit Message

Liam R. Howlett Dec. 1, 2021, 2:30 p.m. UTC
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>

Use the maple tree or VMA iterator instead.  This is faster and will
allow us to shrink the VMA.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
---
 mm/nommu.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Comments

Vlastimil Babka Jan. 20, 2022, 3:06 p.m. UTC | #1
On 12/1/21 15:30, Liam Howlett wrote:
> From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> 
> Use the maple tree or VMA iterator instead.  This is faster and will
> allow us to shrink the VMA.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

But I think some fixup needed:

> @@ -1456,12 +1458,14 @@ void exit_mmap(struct mm_struct *mm)
>  
>  	mm->total_vm = 0;
>  
> -	while ((vma = mm->mmap)) {
> -		mm->mmap = vma->vm_next;
> +	mmap_write_lock(mm);

If locking was missing, should have been added sooner than now?

> +	for_each_vma(vmi, vma) {
>  		delete_vma_from_mm(vma);
>  		delete_vma(mm, vma);
>  		cond_resched();
>  	}
> +	__mt_destroy(&mm->mm_mt);

And this at the point mm_mt was added?

> +	mmap_write_unlock(mm);
>  }
>  
>  int vm_brk(unsigned long addr, unsigned long len)
Matthew Wilcox Jan. 20, 2022, 3:54 p.m. UTC | #2
On Thu, Jan 20, 2022 at 04:06:21PM +0100, Vlastimil Babka wrote:
> On 12/1/21 15:30, Liam Howlett wrote:
> > From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> > 
> > Use the maple tree or VMA iterator instead.  This is faster and will
> > allow us to shrink the VMA.
> > 
> > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> 
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> 
> But I think some fixup needed:
> 
> > @@ -1456,12 +1458,14 @@ void exit_mmap(struct mm_struct *mm)
> >  
> >  	mm->total_vm = 0;
> >  
> > -	while ((vma = mm->mmap)) {
> > -		mm->mmap = vma->vm_next;
> > +	mmap_write_lock(mm);
> 
> If locking was missing, should have been added sooner than now?

I don't think so?  This is the exit_mmap() path, so we know nobody
has access to the mm.  We didn't need to hold the lock at this point
before, but now for_each_vma() will check we're holding the mmap_lock.

> > +	for_each_vma(vmi, vma) {
> >  		delete_vma_from_mm(vma);
> >  		delete_vma(mm, vma);
> >  		cond_resched();
> >  	}
> > +	__mt_destroy(&mm->mm_mt);
> 
> And this at the point mm_mt was added?

You mean we should have been calling __mt_destroy() earlier in the
patch series?  Umm ... I'll defer to Liam on that one.
Vlastimil Babka Jan. 20, 2022, 5:06 p.m. UTC | #3
On 1/20/22 16:54, Matthew Wilcox wrote:
> On Thu, Jan 20, 2022 at 04:06:21PM +0100, Vlastimil Babka wrote:
>> On 12/1/21 15:30, Liam Howlett wrote:
>> > From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
>> > 
>> > Use the maple tree or VMA iterator instead.  This is faster and will
>> > allow us to shrink the VMA.
>> > 
>> > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
>> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
>> 
>> Acked-by: Vlastimil Babka <vbabka@suse.cz>
>> 
>> But I think some fixup needed:
>> 
>> > @@ -1456,12 +1458,14 @@ void exit_mmap(struct mm_struct *mm)
>> >  
>> >  	mm->total_vm = 0;
>> >  
>> > -	while ((vma = mm->mmap)) {
>> > -		mm->mmap = vma->vm_next;
>> > +	mmap_write_lock(mm);
>> 
>> If locking was missing, should have been added sooner than now?
> 
> I don't think so?  This is the exit_mmap() path, so we know nobody
> has access to the mm.  We didn't need to hold the lock at this point
> before, but now for_each_vma() will check we're holding the mmap_lock.

It has crossed my mind that it is there to make asserts happy, in which case
a clarifying comment would be useful.

>> > +	for_each_vma(vmi, vma) {
>> >  		delete_vma_from_mm(vma);
>> >  		delete_vma(mm, vma);
>> >  		cond_resched();
>> >  	}
>> > +	__mt_destroy(&mm->mm_mt);
>> 
>> And this at the point mm_mt was added?
> 
> You mean we should have been calling __mt_destroy() earlier in the
> patch series?

Yeah.

> Umm ... I'll defer to Liam on that one.
Liam R. Howlett Jan. 27, 2022, 4:36 p.m. UTC | #4
* Vlastimil Babka <vbabka@suse.cz> [220120 12:06]:
> On 1/20/22 16:54, Matthew Wilcox wrote:
> > On Thu, Jan 20, 2022 at 04:06:21PM +0100, Vlastimil Babka wrote:
> >> On 12/1/21 15:30, Liam Howlett wrote:
> >> > From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> >> > 
> >> > Use the maple tree or VMA iterator instead.  This is faster and will
> >> > allow us to shrink the VMA.
> >> > 
> >> > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> >> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> >> 
> >> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> >> 
> >> But I think some fixup needed:
> >> 
> >> > @@ -1456,12 +1458,14 @@ void exit_mmap(struct mm_struct *mm)
> >> >  
> >> >  	mm->total_vm = 0;
> >> >  
> >> > -	while ((vma = mm->mmap)) {
> >> > -		mm->mmap = vma->vm_next;
> >> > +	mmap_write_lock(mm);
> >> 
> >> If locking was missing, should have been added sooner than now?
> > 
> > I don't think so?  This is the exit_mmap() path, so we know nobody
> > has access to the mm.  We didn't need to hold the lock at this point
> > before, but now for_each_vma() will check we're holding the mmap_lock.
> 
> It has crossed my mind that it is there to make asserts happy, in which case
> a clarifying comment would be useful.

I will add the comment.

> 
> >> > +	for_each_vma(vmi, vma) {
> >> >  		delete_vma_from_mm(vma);
> >> >  		delete_vma(mm, vma);
> >> >  		cond_resched();
> >> >  	}
> >> > +	__mt_destroy(&mm->mm_mt);
> >> 
> >> And this at the point mm_mt was added?
> > 
> > You mean we should have been calling __mt_destroy() earlier in the
> > patch series?
> 
> Yeah.
> 
> > Umm ... I'll defer to Liam on that one.

Yes, I will move this to the correct patch.
diff mbox series

Patch

diff --git a/mm/nommu.c b/mm/nommu.c
index 11ae550834cd..acb9aafb0afc 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1370,6 +1370,7 @@  static int shrink_vma(struct mm_struct *mm,
  */
 int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
 {
+	MA_STATE(mas, &mm->mm_mt, start, start);
 	struct vm_area_struct *vma;
 	unsigned long end;
 	int ret;
@@ -1381,7 +1382,7 @@  int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
 	end = start + len;
 
 	/* find the first potentially overlapping VMA */
-	vma = find_vma(mm, start);
+	vma = mas_find(&mas, end - 1);
 	if (!vma) {
 		static int limit;
 		if (limit < 5) {
@@ -1400,7 +1401,7 @@  int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list
 				return -EINVAL;
 			if (end == vma->vm_end)
 				goto erase_whole_vma;
-			vma = vma->vm_next;
+			vma = mas_next(&mas, end - 1);
 		} while (vma);
 		return -EINVAL;
 	} else {
@@ -1449,6 +1450,7 @@  SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  */
 void exit_mmap(struct mm_struct *mm)
 {
+	VMA_ITERATOR(vmi, mm, 0);
 	struct vm_area_struct *vma;
 
 	if (!mm)
@@ -1456,12 +1458,14 @@  void exit_mmap(struct mm_struct *mm)
 
 	mm->total_vm = 0;
 
-	while ((vma = mm->mmap)) {
-		mm->mmap = vma->vm_next;
+	mmap_write_lock(mm);
+	for_each_vma(vmi, vma) {
 		delete_vma_from_mm(vma);
 		delete_vma(mm, vma);
 		cond_resched();
 	}
+	__mt_destroy(&mm->mm_mt);
+	mmap_write_unlock(mm);
 }
 
 int vm_brk(unsigned long addr, unsigned long len)