Skip to contentSkip to conversation

mm/filemap: set folio->mapping to NULL before xas_store()

PATCH In review Soma Nakata 3 participants 4 messages Mar 22, 2024 – Mar 26, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/filemap: set folio->mapping to NULL before xas_store()

Functions such as __filemap_get_folio() check the truncation of folios based on the mapping field. Therefore setting this field to NULL earlier prevents unnecessary operations on already removed folios.

---
 mm/filemap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/filemap.c +3 -2
index 2723104cc06a..79bac7c00084 100644
@@ -139,11 +139,12 @@ static void page_cache_delete(struct address_space *mapping,
 
 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
 
+	folio->mapping = NULL;
+	/* Leave page->index set: truncation lookup relies upon it */
+
 	xas_store(&xas, shadow);
 	xas_init_marks(&xas);
 
-	folio->mapping = NULL;
-	/* Leave page->index set: truncation lookup relies upon it */
 	mapping->nrpages -= nr;
 }
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 12 lines · On Sat, 23 Mar 2024 06:04:54 +0900 Soma Nakata <[email protected]> wrote:

Functions such as __filemap_get_folio() check the truncation of folios based on the mapping field. Therefore setting this field to NULL earlier prevents unnecessary operations on already removed folios.

...

mm/filemap.c +0 -0
@@ -139,11 +139,12 @@ static void page_cache_delete(struct address_space *mapping,
 
 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
 

mm/filemap.c static void page_cache_delete(struct address_space *mapping, · line 149

Quoted 10 lines
mm/filemap.c +3 -2
+	folio->mapping = NULL;
+	/* Leave page->index set: truncation lookup relies upon it */
+
 	xas_store(&xas, shadow);
 	xas_init_marks(&xas);
 
-	folio->mapping = NULL;
-	/* Leave page->index set: truncation lookup relies upon it */
 	mapping->nrpages -= nr;
 }

Seems at least harmless, but I wonder if it can really make any difference. Don't readers of folio->mapping lock the folio first?

maintainer
#2 lore raw
Quoted 19 lines · Andrew Morton
Quoted 18 lines · On Sat, 23 Mar 2024 06:04:54 +0900 Soma Nakata <[email protected]> wrote:

Functions such as __filemap_get_folio() check the truncation of folios based on the mapping field. Therefore setting this field to NULL earlier prevents unnecessary operations on already removed folios.

...

mm/filemap.c +3 -0
@@ -139,11 +139,12 @@ static void page_cache_delete(struct address_space *mapping,
 
 	VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
 
+	folio->mapping = NULL;
+	/* Leave page->index set: truncation lookup relies upon it */
+
 	xas_store(&xas, shadow);
 	xas_init_marks(&xas);
 
Quoted 7 lines

mm/filemap.c static void page_cache_delete(struct address_space *mapping, · line 149

Quoted 4 lines
mm/filemap.c +0 -2
-	folio->mapping = NULL;
-	/* Leave page->index set: truncation lookup relies upon it */
 	mapping->nrpages -= nr;
 }

Seems at least harmless, but I wonder if it can really make any difference. Don't readers of folio->mapping lock the folio first?

I can't think of anywhere that doesn't ... most of the places that check folio->mapping have "goto unlock" as the very next line. I don't think this patch accomplishes anything.

author
#3 lore raw
In reply to Andrew Morton
Quoted 21 lines · Andrew Morton
Quoted 18 lines · On Sat, 23 Mar 2024 06:04:54 +0900 Soma Nakata <[email protected]> wrote:

Functions such as __filemap_get_folio() check the truncation of folios based on the mapping field. Therefore setting this field to NULL earlier prevents unnecessary operations on already removed folios.

...

mm/filemap.c +3 -0
@@ -139,11 +139,12 @@ static void page_cache_delete(struct address_space *mapping,
 
      VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
 
+     folio->mapping = NULL;
+     /* Leave page->index set: truncation lookup relies upon it */
+
      xas_store(&xas, shadow);
      xas_init_marks(&xas);
 
Quoted 7 lines

mm/filemap.c static void page_cache_delete(struct address_space *mapping, · line 149

Quoted 4 lines
mm/filemap.c +0 -2
-     folio->mapping = NULL;
-     /* Leave page->index set: truncation lookup relies upon it */
      mapping->nrpages -= nr;
 }

Seems at least harmless, but I wonder if it can really make any difference. Don't readers of folio->mapping lock the folio first?

Yes, the reader locks the folio.
Only __filemap_remove_folio() calls page_cache_delete(), and it says the caller has to lock the folio or make sure that usage is safe. In the latter case, this patch improves efficiency a little bit.
However, I found that there is not any latter case actually, so discard it or apply, also to make the order of operations in page_cache_delete() and page_cache_delete_batch() the same for a cleanup.
Thanks,