2023-07-05 19:47:34

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH v2 1/4] mm/memory: convert do_page_mkwrite() to use folios

Saves one implicit call to compound_head();

Signed-off-by: Sidhartha Kumar <[email protected]>
---

v2:
change folio_mapping() to folio->mapping

mm/memory.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 21fab27272092..e73245b791a7a 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2932,7 +2932,7 @@ static gfp_t __get_fault_gfp_mask(struct vm_area_struct *vma)
static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
{
vm_fault_t ret;
- struct page *page = vmf->page;
+ struct folio *folio = page_folio(vmf->page);
unsigned int old_flags = vmf->flags;

vmf->flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
@@ -2947,14 +2947,14 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf)
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
return ret;
if (unlikely(!(ret & VM_FAULT_LOCKED))) {
- lock_page(page);
- if (!page->mapping) {
- unlock_page(page);
+ folio_lock(folio);
+ if (!folio->mapping) {
+ folio_unlock(folio);
return 0; /* retry */
}
ret |= VM_FAULT_LOCKED;
} else
- VM_BUG_ON_PAGE(!PageLocked(page), page);
+ VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
return ret;
}

--
2.41.0



2023-07-05 19:48:19

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH v2 4/4] mm/memory: convert do_read_fault() to use folios

Saves one implicit call to compound_head()

Signed-off-by: Sidhartha Kumar <[email protected]>
---
v2
- move folio initialization after __do_fault()

mm/memory.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index ce7826d3f6200..e40a03e488ca2 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4527,6 +4527,7 @@ static inline bool should_fault_around(struct vm_fault *vmf)
static vm_fault_t do_read_fault(struct vm_fault *vmf)
{
vm_fault_t ret = 0;
+ struct folio *folio;

/*
* Let's call ->map_pages() first and use ->fault() as fallback
@@ -4543,10 +4544,12 @@ static vm_fault_t do_read_fault(struct vm_fault *vmf)
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
return ret;

+ folio = page_folio(vmf->page);
+
ret |= finish_fault(vmf);
- unlock_page(vmf->page);
+ folio_unlock(folio);
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
- put_page(vmf->page);
+ folio_put(folio);
return ret;
}

--
2.41.0


2023-07-05 19:53:22

by Sidhartha Kumar

[permalink] [raw]
Subject: [PATCH v2 3/4] mm/memory: convert do_shared_fault() to folios

Saves three implicit calls to compound_head().

Signed-off-by: Sidhartha Kumar <[email protected]>
---

v2:
- move folio initialization after __do_fault()

mm/memory.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index fba33fb55a010..ce7826d3f6200 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4593,21 +4593,24 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)
{
struct vm_area_struct *vma = vmf->vma;
vm_fault_t ret, tmp;
+ struct folio *folio;

ret = __do_fault(vmf);
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
return ret;

+ folio = page_folio(vmf->page);
+
/*
* Check if the backing address space wants to know that the page is
* about to become writable
*/
if (vma->vm_ops->page_mkwrite) {
- unlock_page(vmf->page);
+ folio_unlock(folio);
tmp = do_page_mkwrite(vmf);
if (unlikely(!tmp ||
(tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
- put_page(vmf->page);
+ folio_put(folio);
return tmp;
}
}
@@ -4615,8 +4618,8 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)
ret |= finish_fault(vmf);
if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
VM_FAULT_RETRY))) {
- unlock_page(vmf->page);
- put_page(vmf->page);
+ folio_unlock(folio);
+ folio_put(folio);
return ret;
}

--
2.41.0


2023-07-05 20:38:05

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH v2 1/4] mm/memory: convert do_page_mkwrite() to use folios

On Wed, Jul 05, 2023 at 12:43:32PM -0700, Sidhartha Kumar wrote:
> Saves one implicit call to compound_head();
>
> Signed-off-by: Sidhartha Kumar <[email protected]>

Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

2023-07-05 20:43:52

by Sidhartha Kumar

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] mm/memory: convert do_read_fault() to use folios

On 7/5/23 1:19 PM, Matthew Wilcox wrote:
> On Wed, Jul 05, 2023 at 12:43:35PM -0700, Sidhartha Kumar wrote:
>> Saves one implicit call to compound_head()
>>
>> Signed-off-by: Sidhartha Kumar <[email protected]>
>
> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>
>
>> @@ -4543,10 +4544,12 @@ static vm_fault_t do_read_fault(struct vm_fault *vmf)
>> if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
>> return ret;
>>
>> + folio = page_folio(vmf->page);
>
> Why not move this down to after the call to finish_fault()? The
> compiler should be able to do a better job with that; it may have to
> spill it to the stack to preserve it over the function call.
>

I just noticed that the page inside the vmf was being used in
finish_fault() so it would stable enough to do the folio conversion
before. I can move it after for compiler reasons.

>> ret |= finish_fault(vmf);
>> - unlock_page(vmf->page);
>> + folio_unlock(folio);
>> if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
>> - put_page(vmf->page);
>> + folio_put(folio);
>> return ret;
>
>


2023-07-05 21:10:23

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] mm/memory: convert do_read_fault() to use folios

On Wed, Jul 05, 2023 at 12:43:35PM -0700, Sidhartha Kumar wrote:
> Saves one implicit call to compound_head()
>
> Signed-off-by: Sidhartha Kumar <[email protected]>

Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

> @@ -4543,10 +4544,12 @@ static vm_fault_t do_read_fault(struct vm_fault *vmf)
> if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
> return ret;
>
> + folio = page_folio(vmf->page);

Why not move this down to after the call to finish_fault()? The
compiler should be able to do a better job with that; it may have to
spill it to the stack to preserve it over the function call.

> ret |= finish_fault(vmf);
> - unlock_page(vmf->page);
> + folio_unlock(folio);
> if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
> - put_page(vmf->page);
> + folio_put(folio);
> return ret;


2023-07-05 21:16:18

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] mm/memory: convert do_shared_fault() to folios

On Wed, Jul 05, 2023 at 01:16:25PM -0700, Sidhartha Kumar wrote:
> On 7/5/23 1:13 PM, Matthew Wilcox wrote:
> > On Wed, Jul 05, 2023 at 12:43:34PM -0700, Sidhartha Kumar wrote:
> > > /*
> > > * Check if the backing address space wants to know that the page is
> > > * about to become writable
> > > */
> > > if (vma->vm_ops->page_mkwrite) {
> > > - unlock_page(vmf->page);
> > > + folio_unlock(folio);
> > > tmp = do_page_mkwrite(vmf);
> > > if (unlikely(!tmp ||
> > > (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
> > > - put_page(vmf->page);
> > > + folio_put(folio);
> >
> > This is _probably_ OK. However, do_page_mkwrite() calls
> > vm_ops->page_mkwrite(), and I think it's theoretically possible for the
> > driver to replace vmf->page with a different one. The chance of them
> > actually doing that is pretty low (particularly if they return error or
> > nopage!), but I'm going to flag it just in case it comes up.
> >
> > Also, should we pass a folio to do_page_mkwrite() instead of having it
> > extract the folio from vmf->page?
>
> I can take a look at doing this in a follow-up patch.
> >
> > Signed-off-by: Matthew Wilcox (Oracle) <[email protected]>
>
> Did you mean for this to be reviewed-by?

Uh, yes. Maybe I need to get more rest ...

2023-07-05 21:21:53

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] mm/memory: convert do_shared_fault() to folios

On Wed, Jul 05, 2023 at 12:43:34PM -0700, Sidhartha Kumar wrote:
> /*
> * Check if the backing address space wants to know that the page is
> * about to become writable
> */
> if (vma->vm_ops->page_mkwrite) {
> - unlock_page(vmf->page);
> + folio_unlock(folio);
> tmp = do_page_mkwrite(vmf);
> if (unlikely(!tmp ||
> (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
> - put_page(vmf->page);
> + folio_put(folio);

This is _probably_ OK. However, do_page_mkwrite() calls
vm_ops->page_mkwrite(), and I think it's theoretically possible for the
driver to replace vmf->page with a different one. The chance of them
actually doing that is pretty low (particularly if they return error or
nopage!), but I'm going to flag it just in case it comes up.

Also, should we pass a folio to do_page_mkwrite() instead of having it
extract the folio from vmf->page?

Signed-off-by: Matthew Wilcox (Oracle) <[email protected]>

2023-07-05 21:22:19

by Sidhartha Kumar

[permalink] [raw]
Subject: Re: [PATCH v2 3/4] mm/memory: convert do_shared_fault() to folios

On 7/5/23 1:13 PM, Matthew Wilcox wrote:
> On Wed, Jul 05, 2023 at 12:43:34PM -0700, Sidhartha Kumar wrote:
>> /*
>> * Check if the backing address space wants to know that the page is
>> * about to become writable
>> */
>> if (vma->vm_ops->page_mkwrite) {
>> - unlock_page(vmf->page);
>> + folio_unlock(folio);
>> tmp = do_page_mkwrite(vmf);
>> if (unlikely(!tmp ||
>> (tmp & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))) {
>> - put_page(vmf->page);
>> + folio_put(folio);
>
> This is _probably_ OK. However, do_page_mkwrite() calls
> vm_ops->page_mkwrite(), and I think it's theoretically possible for the
> driver to replace vmf->page with a different one. The chance of them
> actually doing that is pretty low (particularly if they return error or
> nopage!), but I'm going to flag it just in case it comes up.
>
> Also, should we pass a folio to do_page_mkwrite() instead of having it
> extract the folio from vmf->page?

I can take a look at doing this in a follow-up patch.
>
> Signed-off-by: Matthew Wilcox (Oracle) <[email protected]>

Did you mean for this to be reviewed-by?

Thanks,
Sidhartha Kumar