All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] ovl: use a dedicated semaphore for dir upperfile caching
@ 2021-01-04  7:28 Icenowy Zheng
  2021-01-04  8:14 ` Icenowy Zheng
  0 siblings, 1 reply; 2+ messages in thread
From: Icenowy Zheng @ 2021-01-04  7:28 UTC (permalink / raw)
  To: Miklos Szeredi, Amir Goldstein, Xiao Yang
  Cc: linux-unionfs, linux-kernel, Icenowy Zheng, stable

The function ovl_dir_real_file() currently uses the semaphore of the
inode to synchronize write to the upperfile cache field.

However, this function will get called by ovl_ioctl_set_flags(), which
utilizes the inode semaphore too. In this case ovl_dir_real_file() will
try to claim a lock that is owned by a function in its call stack, which
won't get released before ovl_dir_real_file() returns.

Define a dedicated semaphore for the upperfile cache, so that the
deadlock won't happen.

Fixes: 61536bed2149 ("ovl: support [S|G]ETFLAGS and FS[S|G]ETXATTR ioctls for directories")
Cc: stable@vger.kernel.org # v5.10
Signed-off-by: Icenowy Zheng <icenowy@aosc.io>
---
 fs/overlayfs/readdir.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fs/overlayfs/readdir.c b/fs/overlayfs/readdir.c
index 01620ebae1bd..fa1844ff8db4 100644
--- a/fs/overlayfs/readdir.c
+++ b/fs/overlayfs/readdir.c
@@ -56,6 +56,7 @@ struct ovl_dir_file {
 	struct list_head *cursor;
 	struct file *realfile;
 	struct file *upperfile;
+	struct semaphore upperfile_sem;
 };
 
 static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
@@ -874,8 +875,6 @@ struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
 	 * Need to check if we started out being a lower dir, but got copied up
 	 */
 	if (!od->is_upper) {
-		struct inode *inode = file_inode(file);
-
 		realfile = READ_ONCE(od->upperfile);
 		if (!realfile) {
 			struct path upperpath;
@@ -883,10 +882,10 @@ struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
 			ovl_path_upper(dentry, &upperpath);
 			realfile = ovl_dir_open_realfile(file, &upperpath);
 
-			inode_lock(inode);
+			down(&od->upperfile_sem);
 			if (!od->upperfile) {
 				if (IS_ERR(realfile)) {
-					inode_unlock(inode);
+					up(&od->upperfile_sem);
 					return realfile;
 				}
 				smp_store_release(&od->upperfile, realfile);
@@ -896,7 +895,7 @@ struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
 					fput(realfile);
 				realfile = od->upperfile;
 			}
-			inode_unlock(inode);
+			up(&od->upperfile_sem);
 		}
 	}
 
@@ -959,6 +958,7 @@ static int ovl_dir_open(struct inode *inode, struct file *file)
 	od->realfile = realfile;
 	od->is_real = ovl_dir_is_real(file->f_path.dentry);
 	od->is_upper = OVL_TYPE_UPPER(type);
+	sema_init(&od->upperfile_sem, 1);
 	file->private_data = od;
 
 	return 0;
-- 
2.28.0

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

* Re: [PATCH v2] ovl: use a dedicated semaphore for dir upperfile caching
  2021-01-04  7:28 [PATCH v2] ovl: use a dedicated semaphore for dir upperfile caching Icenowy Zheng
@ 2021-01-04  8:14 ` Icenowy Zheng
  0 siblings, 0 replies; 2+ messages in thread
From: Icenowy Zheng @ 2021-01-04  8:14 UTC (permalink / raw)
  To: Miklos Szeredi, Amir Goldstein, Xiao Yang
  Cc: linux-unionfs, linux-kernel, stable



于 2021年1月4日 GMT+08:00 下午3:28:35, Icenowy Zheng <icenowy@aosc.io> 写到:
>The function ovl_dir_real_file() currently uses the semaphore of the
>inode to synchronize write to the upperfile cache field.
>
>However, this function will get called by ovl_ioctl_set_flags(), which
>utilizes the inode semaphore too. In this case ovl_dir_real_file() will
>try to claim a lock that is owned by a function in its call stack,
>which
>won't get released before ovl_dir_real_file() returns.
>
>Define a dedicated semaphore for the upperfile cache, so that the
>deadlock won't happen.
>
>Fixes: 61536bed2149 ("ovl: support [S|G]ETFLAGS and FS[S|G]ETXATTR
>ioctls for directories")
>Cc: stable@vger.kernel.org # v5.10
>Signed-off-by: Icenowy Zheng <icenowy@aosc.io>
>---

Sorry for lack of changelog.

A missing replacement of inode_lock() is added in v2.

> fs/overlayfs/readdir.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
>diff --git a/fs/overlayfs/readdir.c b/fs/overlayfs/readdir.c
>index 01620ebae1bd..fa1844ff8db4 100644
>--- a/fs/overlayfs/readdir.c
>+++ b/fs/overlayfs/readdir.c
>@@ -56,6 +56,7 @@ struct ovl_dir_file {
> 	struct list_head *cursor;
> 	struct file *realfile;
> 	struct file *upperfile;
>+	struct semaphore upperfile_sem;
> };
> 
>static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node
>*n)
>@@ -874,8 +875,6 @@ struct file *ovl_dir_real_file(const struct file
>*file, bool want_upper)
>	 * Need to check if we started out being a lower dir, but got copied
>up
> 	 */
> 	if (!od->is_upper) {
>-		struct inode *inode = file_inode(file);
>-
> 		realfile = READ_ONCE(od->upperfile);
> 		if (!realfile) {
> 			struct path upperpath;
>@@ -883,10 +882,10 @@ struct file *ovl_dir_real_file(const struct file
>*file, bool want_upper)
> 			ovl_path_upper(dentry, &upperpath);
> 			realfile = ovl_dir_open_realfile(file, &upperpath);
> 
>-			inode_lock(inode);
>+			down(&od->upperfile_sem);
> 			if (!od->upperfile) {
> 				if (IS_ERR(realfile)) {
>-					inode_unlock(inode);
>+					up(&od->upperfile_sem);
> 					return realfile;
> 				}
> 				smp_store_release(&od->upperfile, realfile);
>@@ -896,7 +895,7 @@ struct file *ovl_dir_real_file(const struct file
>*file, bool want_upper)
> 					fput(realfile);
> 				realfile = od->upperfile;
> 			}
>-			inode_unlock(inode);
>+			up(&od->upperfile_sem);
> 		}
> 	}
> 
>@@ -959,6 +958,7 @@ static int ovl_dir_open(struct inode *inode, struct
>file *file)
> 	od->realfile = realfile;
> 	od->is_real = ovl_dir_is_real(file->f_path.dentry);
> 	od->is_upper = OVL_TYPE_UPPER(type);
>+	sema_init(&od->upperfile_sem, 1);
> 	file->private_data = od;
> 
> 	return 0;

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

end of thread, other threads:[~2021-01-04  8:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-04  7:28 [PATCH v2] ovl: use a dedicated semaphore for dir upperfile caching Icenowy Zheng
2021-01-04  8:14 ` Icenowy Zheng

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.