Skip to contentSkip to conversation

coda: fix reference counting in coda_file_mmap error path

PATCH Applied Christian König 3 participants 8 messages Apr 21, 2021 – Apr 23, 2021

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

PATCH 1/2 coda: fix reference counting in coda_file_mmap error path

mmap_region() now calls fput() on the vma->vm_file.

So we need to drop the extra reference on the coda file instead of the host file.

Fixes 1527f926fd04 ("mm: mmap: fix fput in error path v2")
---
 fs/coda/file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/file.c +3 -3
index 128d63df5bfb..ef5ca22bfb3e 100644
@@ -175,10 +175,10 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
 	ret = call_mmap(vma->vm_file, vma);
 
 	if (ret) {
-		/* if call_mmap fails, our caller will put coda_file so we
-		 * should drop the reference to the host_file that we got.
+		/* if call_mmap fails, our caller will put host_file so we
+		 * should drop the reference to the coda_file that we got.
 		 */
-		fput(host_file);
+		fput(coda_file);
 		kfree(cvm_ops);
 	} else {
 		/* here we add redirects for the open/close vm_operations */
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 ovl: fix reference counting in ovl_mmap error path

mmap_region() now calls fput() on the vma->vm_file.

Fix this by using vma_set_file() so it doesn't need to be handled manually here any more.

Fixes 1527f926fd04 ("mm: mmap: fix fput in error path v2")
---
 fs/overlayfs/file.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
fs/overlayfs/file.c +1 -10
index dbfb35fb0ff7..3847cdc069b5 100644
@@ -430,20 +430,11 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
 	if (WARN_ON(file != vma->vm_file))
 		return -EIO;
 
-	vma->vm_file = get_file(realfile);
+	vma_set_file(vma, realfile);
 
 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
 	ret = call_mmap(vma->vm_file, vma);
 	revert_creds(old_cred);
-
-	if (ret) {
-		/* Drop reference count from new vm_file value */
-		fput(realfile);
-	} else {
-		/* Drop reference count from previous vm_file value */
-		fput(file);
-	}
-
 	ovl_file_accessed(file);
 
 	return ret;
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] ovl: fix reference counting in ovl_mmap error path

Quoted 17 lines · Christian König

mmap_region() now calls fput() on the vma->vm_file.

Fix this by using vma_set_file() so it doesn't need to be handled manually here any more.

Fixes 1527f926fd04 ("mm: mmap: fix fput in error path v2")
---
 fs/overlayfs/file.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
fs/overlayfs/file.c +0 -0
index dbfb35fb0ff7..3847cdc069b5 100644

fs/overlayfs/file.c static int ovl_mmap(struct file *file, struct vm_area_struct *vma) · line 433

Quoted 5 lines
fs/overlayfs/file.c +1 -1
@@ -430,20 +430,11 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
 	if (WARN_ON(file != vma->vm_file))
 		return -EIO;
 
-	vma->vm_file = get_file(realfile);
+	vma_set_file(vma, realfile);
Quoted 23 lines
fs/overlayfs/file.c +0 -9
 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
 	ret = call_mmap(vma->vm_file, vma);
 	revert_creds(old_cred);
-
-	if (ret) {
-		/* Drop reference count from new vm_file value */
-		fput(realfile);
-	} else {
-		/* Drop reference count from previous vm_file value */
-		fput(file);
-	}
-
 	ovl_file_accessed(file);
 
 	return ret;
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/2
#3 lore raw
In reply to Christian König
Quoted 8 lines · Christian König

mmap_region() now calls fput() on the vma->vm_file.

So we need to drop the extra reference on the coda file instead of the host file.

Fixes 1527f926fd04 ("mm: mmap: fix fput in error path v2")
Quoted 29 lines
---
 fs/coda/file.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/file.c +3 -3
index 128d63df5bfb..ef5ca22bfb3e 100644
@@ -175,10 +175,10 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
 	ret = call_mmap(vma->vm_file, vma);
 
 	if (ret) {
-		/* if call_mmap fails, our caller will put coda_file so we
-		 * should drop the reference to the host_file that we got.
+		/* if call_mmap fails, our caller will put host_file so we
+		 * should drop the reference to the coda_file that we got.
 		 */
-		fput(host_file);
+		fput(coda_file);
 		kfree(cvm_ops);
 	} else {
 		/* here we add redirects for the open/close vm_operations */
Signature · 6 lines
2.25.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
maintainer
#4 lore raw
In reply to Christian König

Looks good to me.

I'm also maintaining an out of tree coda module build that people sometimes use, which has workarounds for differences between the various kernel versions.

Do you have a reference to the corresponding mmap_region change? If it is merged already I'll probably be able to find it. Is this mmap_region change expected to be backported to any lts kernels?

Jan

Quoted 31 lines · Christian König

mmap_region() now calls fput() on the vma->vm_file.

So we need to drop the extra reference on the coda file instead of the host file.

Fixes 1527f926fd04 ("mm: mmap: fix fput in error path v2")

---
fs/coda/file.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/file.c +3 -3
index 128d63df5bfb..ef5ca22bfb3e 100644
@@ -175,10 +175,10 @@ coda_file_mmap(struct file *coda_file, struct
 vm_area_struct *vma)
 	ret = call_mmap(vma->vm_file, vma);
 
 	if (ret) {
-		/* if call_mmap fails, our caller will put coda_file so we
-		 * should drop the reference to the host_file that we got.
+		/* if call_mmap fails, our caller will put host_file so we
+		 * should drop the reference to the coda_file that we got.
 		 */
-		fput(host_file);
+		fput(coda_file);
 		kfree(cvm_ops);
 	} else {
		/* here we add redirects for the open/close vm_operations */
#5 lore raw

Hi Jan,

Am 22.04.21 um 14:27 schrieb Jan Harkes:

Quoted 5 lines

Looks good to me.

I'm also maintaining an out of tree coda module build that people sometimes use, which has workarounds for differences between the various kernel versions.

Do you have a reference to the corresponding mmap_region change? If it is merged already I'll probably be able to find it. Is this mmap_region change expected to be backported to any lts kernels?

That is the following upstream commit in Linus tree:

commit 1527f926fd04490f648c42f42b45218a04754f87
Author: Christian König <[email protected]> Date:   Fri Oct 9 15:08:55 2020 +0200

    mm: mmap: fix fput in error path v2

But I don't think we should backport that.

And sorry for the noise. We had so many places which expected different behavior that I didn't noticed that two occasions in the fs code actually rely on the current behavior.

For your out of tree module you could make the code version independent by setting the vma back to the original file in case of an error. That should work with both behaviors in mmap_region.

Thanks,
Christian.

Quoted 24 lines

Jan

Quoted 20 lines · Christian König

mmap_region() now calls fput() on the vma->vm_file.

So we need to drop the extra reference on the coda file instead of the host file.

Fixes 1527f926fd04 ("mm: mmap: fix fput in error path v2")

---
fs/coda/file.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
fs/coda/file.c +0 -0
index 128d63df5bfb..ef5ca22bfb3e 100644
@@ -175,10 +175,10 @@ coda_file_mmap(struct file *coda_file, struct
 vm_area_struct *vma)
 	ret = call_mmap(vma->vm_file, vma);
 
Quoted 11 lines
Quoted 11 lines
fs/coda/file.c +3 -3
 	if (ret) {
-		/* if call_mmap fails, our caller will put coda_file so we
-		 * should drop the reference to the host_file that we got.
+		/* if call_mmap fails, our caller will put host_file so we
+		 * should drop the reference to the coda_file that we got.
 		 */
-		fput(host_file);
+		fput(coda_file);
 		kfree(cvm_ops);
 	} else {
		/* here we add redirects for the open/close vm_operations */

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

maintainer
#6 lore raw
Quoted 17 lines · Christian König

Am 22.04.21 um 14:27 schrieb Jan Harkes:

Quoted 5 lines

Looks good to me.

I'm also maintaining an out of tree coda module build that people sometimes use, which has workarounds for differences between the various kernel versions.

Do you have a reference to the corresponding mmap_region change? If it is merged already I'll probably be able to find it. Is this mmap_region change expected to be backported to any lts kernels?

That is the following upstream commit in Linus tree:

commit 1527f926fd04490f648c42f42b45218a04754f87
Author: Christian König <[email protected]> Date:   Fri Oct 9 15:08:55 2020 +0200

    mm: mmap: fix fput in error path v2

But I don't think we should backport that.

Quoted 7 lines

And sorry for the noise. We had so many places which expected different behavior that I didn't noticed that two occasions in the fs code actually rely on the current behavior.

For your out of tree module you could make the code version independent by setting the vma back to the original file in case of an error. That should work with both behaviors in mmap_region.

Awesome, I'll give that a try, it may very well be a cleaner solution either way.

And thank you for following up after your original patch and finding the filesystems that mess around with those mappings. I'm sure it would have taken me a while to figure out why file refcounts would go weird for some people, especially because this only happens in the error path.

Jan

#7 lore raw

Am 22.04.21 um 15:51 schrieb Jan Harkes:

Quoted 24 lines · Christian König
Quoted 16 lines

Am 22.04.21 um 14:27 schrieb Jan Harkes:

Quoted 5 lines

Looks good to me.

I'm also maintaining an out of tree coda module build that people sometimes use, which has workarounds for differences between the various kernel versions.

Do you have a reference to the corresponding mmap_region change? If it is merged already I'll probably be able to find it. Is this mmap_region change expected to be backported to any lts kernels?

That is the following upstream commit in Linus tree:

commit 1527f926fd04490f648c42f42b45218a04754f87
Author: Christian König <[email protected]> Date:   Fri Oct 9 15:08:55 2020 +0200

    mm: mmap: fix fput in error path v2

But I don't think we should backport that.

Quoted 7 lines

And sorry for the noise. We had so many places which expected different behavior that I didn't noticed that two occasions in the fs code actually rely on the current behavior.

For your out of tree module you could make the code version independent by setting the vma back to the original file in case of an error. That should work with both behaviors in mmap_region.

Quoted 7 lines

Awesome, I'll give that a try, it may very well be a cleaner solution either way.

And thank you for following up after your original patch and finding the filesystems that mess around with those mappings. I'm sure it would have taken me a while to figure out why file refcounts would go weird for some people, especially because this only happens in the error path.

Kudos goes to Miklos for figured out why the refcount for overlayfs was suddenly wrong.

And please also see the follow up commit:

commit 295992fb815e791d14b18ef7cdbbaf1a76211a31 (able/vma_file)
Author: Christian König <[email protected]> Date:   Mon Sep 14 15:09:33 2020 +0200

    mm: introduce vma_set_file function v5

It adds a new vma_set_file() function which implements the necessary refcount dance for changing the vma file in a clean manner.

Thanks,
Christian.

Quoted 3 lines

Jan

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel