Skip to contentSkip to conversation

filemap: fix the conditional folio_put in filemap_fault

PATCH Applied Christoph Hellwig 4 participants 8 messages May 3, 2023 – May 4, 2023

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

PATCH 1/2 filemap: fix the conditional folio_put in filemap_fault

folio can't be NULL here now that __filemap_get_folio returns an ERR_PTR. Remove the conditional folio_put after the out_retry label and add a new label for the cases where we have a valid folio.

Fixes 66dabbb65d67 ("mm: return an ERR_PTR from __filemap_get_folio")
Reported-by [email protected]
---
 mm/filemap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/filemap.c +5 -5
index a34abfe8c65430..ae597f63a9bc54 100644
@@ -3298,7 +3298,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	}
 
 	if (!lock_folio_maybe_drop_mmap(vmf, folio, &fpin))
-		goto out_retry;
+		goto out_retry_put_folio;
 
 	/* Did it get truncated? */
 	if (unlikely(folio->mapping != mapping)) {
@@ -3334,7 +3334,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	 */
 	if (fpin) {
 		folio_unlock(folio);
-		goto out_retry;
+		goto out_retry_put_folio;
 	}
 	if (mapping_locked)
 		filemap_invalidate_unlock_shared(mapping);
@@ -3363,7 +3363,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	fpin = maybe_unlock_mmap_for_io(vmf, fpin);
 	error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
 	if (fpin)
-		goto out_retry;
+		goto out_retry_put_folio;
 	folio_put(folio);
 
 	if (!error || error == AOP_TRUNCATED_PAGE)
@@ -3372,14 +3372,14 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 
 	return VM_FAULT_SIGBUS;
 
+out_retry_put_folio:
+	folio_put(folio);
 out_retry:
 	/*
 	 * We dropped the mmap_lock, we need to return to the fault handler to
 	 * re-find the vma and come back and find our hopefully still populated
 	 * page.
 	 */
-	if (folio)
-		folio_put(folio);
 	if (mapping_locked)
 		filemap_invalidate_unlock_shared(mapping);
 	if (fpin)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 2/2 afs: fix the afs_dir_get_folio return value

Keep returning NULL on failure instead of letting an ERR_PTR escape to callers that don't expect it.

Fixes 66dabbb65d67 ("mm: return an ERR_PTR from __filemap_get_folio")
---
 fs/afs/dir_edit.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/afs/dir_edit.c +4 -3
index f0eddccbdd9541..e2fa577b66fe0a 100644
@@ -115,11 +115,12 @@ static struct folio *afs_dir_get_folio(struct afs_vnode *vnode, pgoff_t index)
 	folio = __filemap_get_folio(mapping, index,
 				    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
 				    mapping->gfp_mask);
-	if (IS_ERR(folio))
+	if (IS_ERR(folio)) {
 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
-	else if (folio && !folio_test_private(folio))
+		return NULL;
+	}
+	if (!folio_test_private(folio))
 		folio_attach_private(folio, (void *)1);
-
 	return folio;
 }
 
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] afs: fix the afs_dir_get_folio return value

Quoted 6 lines · Christoph Hellwig

Keep returning NULL on failure instead of letting an ERR_PTR escape to callers that don't expect it.

Fixes 66dabbb65d67 ("mm: return an ERR_PTR from __filemap_get_folio")

Looks good to me. Feel free to add:

Honza

Quoted 27 lines
---
 fs/afs/dir_edit.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
fs/afs/dir_edit.c +4 -3
index f0eddccbdd9541..e2fa577b66fe0a 100644
@@ -115,11 +115,12 @@ static struct folio *afs_dir_get_folio(struct afs_vnode *vnode, pgoff_t index)
 	folio = __filemap_get_folio(mapping, index,
 				    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
 				    mapping->gfp_mask);
-	if (IS_ERR(folio))
+	if (IS_ERR(folio)) {
 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
-	else if (folio && !folio_test_private(folio))
+		return NULL;
+	}
+	if (!folio_test_private(folio))
 		folio_attach_private(folio, (void *)1);
-
 	return folio;
 }
 
Signature · 1 line
2.39.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Reviewed-by Tested-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] afs: fix the afs_dir_get_folio return value

In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Keep returning NULL on failure instead of letting an ERR_PTR escape to callers that don't expect it.

Fixes 66dabbb65d67 ("mm: return an ERR_PTR from __filemap_get_folio")
maintainer
#4 lore raw
In reply to Christoph Hellwig
Quoted 4 lines · Christoph Hellwig
mm/filemap.c +0 -0
@@ -3372,14 +3372,14 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 
 	return VM_FAULT_SIGBUS;
 

mm/filemap.c vm_fault_t filemap_fault(struct vm_fault *vmf) · line 3382

Quoted 10 lines
mm/filemap.c +2 -2
+out_retry_put_folio:
+	folio_put(folio);
 out_retry:
 	/*
 	 * We dropped the mmap_lock, we need to return to the fault handler to
 	 * re-find the vma and come back and find our hopefully still populated
 	 * page.
 	 */
-	if (folio)
-		folio_put(folio);

Why not simply:

- if (folio)
+ if (!IS_ERR_OR_NULL(folio))

#5 lore raw

Review

Quoted 6 lines · Matthew Wilcox

mm/filemap.c vm_fault_t filemap_fault(struct vm_fault *vmf) · line 3382

Quoted 1 line
mm/filemap.c +0 -1
-		folio_put(folio);

Why not simply:

(quoted diff) +1 -1
-	if (folio)
+	if (!IS_ERR_OR_NULL(folio))

no need for the OR_NULL. But I find the extra label way easier to reason about, and it's exactly the same amount of code.

maintainer
#6 lore raw
Quoted 9 lines · Christoph Hellwig

Review

Quoted 6 lines · Matthew Wilcox

mm/filemap.c vm_fault_t filemap_fault(struct vm_fault *vmf) · line 3382

Quoted 1 line
mm/filemap.c +0 -1
-		folio_put(folio);

Why not simply:

(quoted diff) +1 -1
-	if (folio)
+	if (!IS_ERR_OR_NULL(folio))

no need for the OR_NULL.

Right. I didn't read the whole function.

Quoted 2 lines

But I find the extra label way easier to reason about, and it's exactly the same amount of code.

If it were easy to reason about, it would have a less ugly name.

maintainer Reviewed-by on v1 1/2
#7 lore raw
In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

folio can't be NULL here now that __filemap_get_folio returns an ERR_PTR. Remove the conditional folio_put after the out_retry label and add a new label for the cases where we have a valid folio.

Fixes 66dabbb65d67 ("mm: return an ERR_PTR from __filemap_get_folio")
Reported-by [email protected]

Looks good to me. Feel free to add:

Honza

Quoted 55 lines
---
 mm/filemap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
mm/filemap.c +5 -5
index a34abfe8c65430..ae597f63a9bc54 100644
@@ -3298,7 +3298,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	}
 
 	if (!lock_folio_maybe_drop_mmap(vmf, folio, &fpin))
-		goto out_retry;
+		goto out_retry_put_folio;
 
 	/* Did it get truncated? */
 	if (unlikely(folio->mapping != mapping)) {
@@ -3334,7 +3334,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	 */
 	if (fpin) {
 		folio_unlock(folio);
-		goto out_retry;
+		goto out_retry_put_folio;
 	}
 	if (mapping_locked)
 		filemap_invalidate_unlock_shared(mapping);
@@ -3363,7 +3363,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 	fpin = maybe_unlock_mmap_for_io(vmf, fpin);
 	error = filemap_read_folio(file, mapping->a_ops->read_folio, folio);
 	if (fpin)
-		goto out_retry;
+		goto out_retry_put_folio;
 	folio_put(folio);
 
 	if (!error || error == AOP_TRUNCATED_PAGE)
@@ -3372,14 +3372,14 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
 
 	return VM_FAULT_SIGBUS;
 
+out_retry_put_folio:
+	folio_put(folio);
 out_retry:
 	/*
 	 * We dropped the mmap_lock, we need to return to the fault handler to
 	 * re-find the vma and come back and find our hopefully still populated
 	 * page.
 	 */
-	if (folio)
-		folio_put(folio);
 	if (mapping_locked)
 		filemap_invalidate_unlock_shared(mapping);
 	if (fpin)
Signature · 1 line
2.39.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR