All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
@ 2023-09-30 23:10 Lorenzo Stoakes
  2023-10-02  6:26 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Lorenzo Stoakes @ 2023-09-30 23:10 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Andrew Morton
  Cc: Matthew Wilcox, Jan Kara, Lorenzo Stoakes

The existing comments in filemap_fault() suggest that, after either a minor
fault has occurred and filemap_get_folio() found a folio in the page cache,
or a major fault arose and __filemap_get_folio(FGP_CREATE...) did the job
(having relied on do_sync_mmap_readahead() or filemap_read_folio() to read
in the folio), the only possible reason it could not be uptodate is because
of an error.

This is not so, as if, for instance, the fault occurred within a VMA which
had the VM_RAND_READ flag set (via madvise() with the MADV_RANDOM flag
specified), this would cause even synchronous readahead to fail to read in
the folio.

I confirmed this by dropping page caches and faulting in memory madvise()'d
this way, observing that this code path was reached on each occasion.

Clarify the comments to include this case, and additionally update the
comment recently added around the invalidate lock logic to make it clear
the comment explicitly refers to the minor fault case.

In addition, while we're here, refer to folios rather than pages.

Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>
---
 mm/filemap.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index d285ec5f9162..959694a2ade7 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3308,21 +3308,28 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
 
 	/*
-	 * We have a locked page in the page cache, now we need to check
-	 * that it's up-to-date. If not, it is going to be due to an error.
+	 * We have a locked folio in the page cache, now we need to check
+	 * that it's up-to-date. If not, it is going to be due to an error,
+	 * or because readahead was otherwise unable to retrieve it.
 	 */
 	if (unlikely(!folio_test_uptodate(folio))) {
 		/*
-		 * The page was in cache and uptodate and now it is not.
-		 * Strange but possible since we didn't hold the page lock all
-		 * the time. Let's drop everything get the invalidate lock and
-		 * try again.
+		 * If the invalidate lock is not held, the folio was in cache and
+		 * uptodate and now it is not. Strange but possible since we
+		 * didn't hold the page lock all the time. Let's drop everything,
+		 * get the invalidate lock and try again.
 		 */
 		if (!mapping_locked) {
 			folio_unlock(folio);
 			folio_put(folio);
 			goto retry_find;
 		}
+
+		/*
+		 * OK, the folio is really not uptodate. This can be because the
+		 * VMA has the VM_RAND_READ flag set, or because an error
+		 * arose. Let's read it in directly.
+		 */
 		goto page_not_uptodate;
 	}
 
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-09-30 23:10 [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case Lorenzo Stoakes
@ 2023-10-02  6:26 ` Christoph Hellwig
  2023-10-02  6:39   ` Vlastimil Babka
  2023-10-02  6:47   ` Lorenzo Stoakes
  2023-10-02  9:55 ` Jan Kara
  2023-10-02 16:02 ` Matthew Wilcox
  2 siblings, 2 replies; 8+ messages in thread
From: Christoph Hellwig @ 2023-10-02  6:26 UTC (permalink / raw)
  To: Lorenzo Stoakes
  Cc: linux-mm, linux-kernel, Andrew Morton, Matthew Wilcox, Jan Kara

> +		 * If the invalidate lock is not held, the folio was in cache and
> +		 * uptodate and now it is not. Strange but possible since we

Please don't spill block comments ôver the 80 charater limit, that makes
them impossible to read nicely.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-10-02  6:26 ` Christoph Hellwig
@ 2023-10-02  6:39   ` Vlastimil Babka
  2023-10-02  9:43     ` Joe Perches
  2023-10-02  6:47   ` Lorenzo Stoakes
  1 sibling, 1 reply; 8+ messages in thread
From: Vlastimil Babka @ 2023-10-02  6:39 UTC (permalink / raw)
  To: Christoph Hellwig, Lorenzo Stoakes, Joe Perches
  Cc: linux-mm, linux-kernel, Andrew Morton, Matthew Wilcox, Jan Kara

On 10/2/23 08:26, Christoph Hellwig wrote:
>> +		 * If the invalidate lock is not held, the folio was in cache and
>> +		 * uptodate and now it is not. Strange but possible since we
> 
> Please don't spill block comments ôver the 80 charater limit, that makes
> them impossible to read nicely.

Joe, can we revert the default to 80 to avoid everyone having to alias
--max-line-length=80 locally?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-10-02  6:26 ` Christoph Hellwig
  2023-10-02  6:39   ` Vlastimil Babka
@ 2023-10-02  6:47   ` Lorenzo Stoakes
  2023-10-02  6:50     ` Christoph Hellwig
  1 sibling, 1 reply; 8+ messages in thread
From: Lorenzo Stoakes @ 2023-10-02  6:47 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: linux-mm, linux-kernel, Andrew Morton, Matthew Wilcox, Jan Kara

On Sun, Oct 01, 2023 at 11:26:18PM -0700, Christoph Hellwig wrote:
> > +		 * If the invalidate lock is not held, the folio was in cache and
> > +		 * uptodate and now it is not. Strange but possible since we
>
> Please don't spill block comments ôver the 80 charater limit, that makes
> them impossible to read nicely.
>

Yup this was because checkpatch ok'd it, I've aliased the strict 80 char param
so it won't happen again.

Fix patch enclosed

----8<----
From 3abb740abb67505b6f83d7602f7b4a8dcd29977f Mon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <lstoakes@gmail.com>
Date: Mon, 2 Oct 2023 07:37:24 +0100
Subject: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case

Correct identation as per Christopher's feedback.

Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>
---
 mm/filemap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 959694a2ade7..951709089f38 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3314,10 +3314,10 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	 */
 	if (unlikely(!folio_test_uptodate(folio))) {
 		/*
-		 * If the invalidate lock is not held, the folio was in cache and
-		 * uptodate and now it is not. Strange but possible since we
-		 * didn't hold the page lock all the time. Let's drop everything,
-		 * get the invalidate lock and try again.
+		 * If the invalidate lock is not held, the folio was in cache
+		 * and uptodate and now it is not. Strange but possible since we
+		 * didn't hold the page lock all the time. Let's drop
+		 * everything, get the invalidate lock and try again.
 		 */
 		if (!mapping_locked) {
 			folio_unlock(folio);
--
2.42.0

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-10-02  6:47   ` Lorenzo Stoakes
@ 2023-10-02  6:50     ` Christoph Hellwig
  0 siblings, 0 replies; 8+ messages in thread
From: Christoph Hellwig @ 2023-10-02  6:50 UTC (permalink / raw)
  To: Lorenzo Stoakes
  Cc: Christoph Hellwig, linux-mm, linux-kernel, Andrew Morton,
	Matthew Wilcox, Jan Kara

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-10-02  6:39   ` Vlastimil Babka
@ 2023-10-02  9:43     ` Joe Perches
  0 siblings, 0 replies; 8+ messages in thread
From: Joe Perches @ 2023-10-02  9:43 UTC (permalink / raw)
  To: Vlastimil Babka, Christoph Hellwig, Lorenzo Stoakes
  Cc: linux-mm, linux-kernel, Andrew Morton, Matthew Wilcox, Jan Kara

On Mon, 2023-10-02 at 08:39 +0200, Vlastimil Babka wrote:
> On 10/2/23 08:26, Christoph Hellwig wrote:
> > > +		 * If the invalidate lock is not held, the folio was in cache and
> > > +		 * uptodate and now it is not. Strange but possible since we
> > 
> > Please don't spill block comments ôver the 80 charater limit, that makes
> > them impossible to read nicely.
> 
> Joe, can we revert the default to 80 to avoid everyone having to alias
> --max-line-length=80 locally?

Given the current --max-line-length default is 100,
and that was Linus' preference, I think not.

https://lore.kernel.org/lkml/CAHk-=wj3iGQqjpvc+gf6+C29Jo4COj6OQQFzdY0h5qvYKTdCow@mail.gmail.com/

and

https://lore.kernel.org/lkml/CAHk-=wjR0H3+2ba0UUWwoYzYBH0GX9yTf5dj2MZyo0xvyzvJnA@mail.gmail.com/



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-09-30 23:10 [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case Lorenzo Stoakes
  2023-10-02  6:26 ` Christoph Hellwig
@ 2023-10-02  9:55 ` Jan Kara
  2023-10-02 16:02 ` Matthew Wilcox
  2 siblings, 0 replies; 8+ messages in thread
From: Jan Kara @ 2023-10-02  9:55 UTC (permalink / raw)
  To: Lorenzo Stoakes
  Cc: linux-mm, linux-kernel, Andrew Morton, Matthew Wilcox, Jan Kara

On Sun 01-10-23 00:10:29, Lorenzo Stoakes wrote:
> The existing comments in filemap_fault() suggest that, after either a minor
> fault has occurred and filemap_get_folio() found a folio in the page cache,
> or a major fault arose and __filemap_get_folio(FGP_CREATE...) did the job
> (having relied on do_sync_mmap_readahead() or filemap_read_folio() to read
> in the folio), the only possible reason it could not be uptodate is because
> of an error.
> 
> This is not so, as if, for instance, the fault occurred within a VMA which
> had the VM_RAND_READ flag set (via madvise() with the MADV_RANDOM flag
> specified), this would cause even synchronous readahead to fail to read in
> the folio.
> 
> I confirmed this by dropping page caches and faulting in memory madvise()'d
> this way, observing that this code path was reached on each occasion.
> 
> Clarify the comments to include this case, and additionally update the
> comment recently added around the invalidate lock logic to make it clear
> the comment explicitly refers to the minor fault case.
> 
> In addition, while we're here, refer to folios rather than pages.
> 
> Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>

After the alignment fixup the patch looks good to me. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  mm/filemap.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/filemap.c b/mm/filemap.c
> index d285ec5f9162..959694a2ade7 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -3308,21 +3308,28 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
>  	VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
>  
>  	/*
> -	 * We have a locked page in the page cache, now we need to check
> -	 * that it's up-to-date. If not, it is going to be due to an error.
> +	 * We have a locked folio in the page cache, now we need to check
> +	 * that it's up-to-date. If not, it is going to be due to an error,
> +	 * or because readahead was otherwise unable to retrieve it.
>  	 */
>  	if (unlikely(!folio_test_uptodate(folio))) {
>  		/*
> -		 * The page was in cache and uptodate and now it is not.
> -		 * Strange but possible since we didn't hold the page lock all
> -		 * the time. Let's drop everything get the invalidate lock and
> -		 * try again.
> +		 * If the invalidate lock is not held, the folio was in cache and
> +		 * uptodate and now it is not. Strange but possible since we
> +		 * didn't hold the page lock all the time. Let's drop everything,
> +		 * get the invalidate lock and try again.
>  		 */
>  		if (!mapping_locked) {
>  			folio_unlock(folio);
>  			folio_put(folio);
>  			goto retry_find;
>  		}
> +
> +		/*
> +		 * OK, the folio is really not uptodate. This can be because the
> +		 * VMA has the VM_RAND_READ flag set, or because an error
> +		 * arose. Let's read it in directly.
> +		 */
>  		goto page_not_uptodate;
>  	}
>  
> -- 
> 2.42.0
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case
  2023-09-30 23:10 [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case Lorenzo Stoakes
  2023-10-02  6:26 ` Christoph Hellwig
  2023-10-02  9:55 ` Jan Kara
@ 2023-10-02 16:02 ` Matthew Wilcox
  2 siblings, 0 replies; 8+ messages in thread
From: Matthew Wilcox @ 2023-10-02 16:02 UTC (permalink / raw)
  To: Lorenzo Stoakes; +Cc: linux-mm, linux-kernel, Andrew Morton, Jan Kara

On Sun, Oct 01, 2023 at 12:10:29AM +0100, Lorenzo Stoakes wrote:
> The existing comments in filemap_fault() suggest that, after either a minor
> fault has occurred and filemap_get_folio() found a folio in the page cache,
> or a major fault arose and __filemap_get_folio(FGP_CREATE...) did the job
> (having relied on do_sync_mmap_readahead() or filemap_read_folio() to read
> in the folio), the only possible reason it could not be uptodate is because
> of an error.
> 
> This is not so, as if, for instance, the fault occurred within a VMA which
> had the VM_RAND_READ flag set (via madvise() with the MADV_RANDOM flag
> specified), this would cause even synchronous readahead to fail to read in
> the folio.
> 
> I confirmed this by dropping page caches and faulting in memory madvise()'d
> this way, observing that this code path was reached on each occasion.
> 
> Clarify the comments to include this case, and additionally update the
> comment recently added around the invalidate lock logic to make it clear
> the comment explicitly refers to the minor fault case.

I do appreciate the comment being made accurate, but I wonder if we
shouldn't change the code to match the comment.  We're got two "should"s
pointing in different directions -- we "should" not use readahead if
readahead is disabled, and we "should" always use readahead first,
using read_folio() only if readahead doesn't succeed.

The code isn't ideally structured for this, but I'm going to play with
it a bit and see what I can create.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-10-02 16:02 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-30 23:10 [PATCH] mm/filemap: clarify filemap_fault() comments for not uptodate case Lorenzo Stoakes
2023-10-02  6:26 ` Christoph Hellwig
2023-10-02  6:39   ` Vlastimil Babka
2023-10-02  9:43     ` Joe Perches
2023-10-02  6:47   ` Lorenzo Stoakes
2023-10-02  6:50     ` Christoph Hellwig
2023-10-02  9:55 ` Jan Kara
2023-10-02 16:02 ` Matthew Wilcox

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.