erofs: protect s_inodes with s_inode_list_lock

Message ID TYCP286MB23237A9993E0FFCFE5C2BDBECA269@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM
State New
Headers
Series erofs: protect s_inodes with s_inode_list_lock |

Commit Message

Dawei Li Oct. 16, 2022, 12:37 p.m. UTC
  s_inodes is superblock-specific resource, which should be
protected by sb's specific lock s_inode_list_lock.

base-commit: 8436c4a57bd147b0bd2943ab499bb8368981b9e1

Signed-off-by: Dawei Li <set_pte_at@outlook.com>
---
 fs/erofs/fscache.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
  

Comments

Jia Zhu Oct. 16, 2022, 3:05 p.m. UTC | #1
在 2022/10/16 20:37, Dawei Li 写道:
> s_inodes is superblock-specific resource, which should be
> protected by sb's specific lock s_inode_list_lock.
>  > base-commit: 8436c4a57bd147b0bd2943ab499bb8368981b9e1
> 
> Signed-off-by: Dawei Li <set_pte_at@outlook.com>
> ---
>   fs/erofs/fscache.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
> index 998cd26a1b3b..bbf5268440df 100644
> --- a/fs/erofs/fscache.c
> +++ b/fs/erofs/fscache.c
> @@ -589,15 +589,18 @@ struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
>   	struct erofs_domain *domain = EROFS_SB(sb)->domain;
>   	struct super_block *psb = erofs_pseudo_mnt->mnt_sb;
>   
> -	mutex_lock(&erofs_domain_cookies_lock);
Hi Dawei,
Thanks for catching this.

I would suggest holding this mutex lock during inode searches and
inserts to avoid the following case:

P1				P2
lock inode_list lock
traverse sb->s_inodes
unlock inode_list lock
				lock inode_list
				traverse sb->s_inodes
				unlock inode_list
				domain_init_cookie
domain_init_cookie

Thanks,
Jia
> +	spin_lock(&psb->s_inode_list_lock);
>   	list_for_each_entry(inode, &psb->s_inodes, i_sb_list) {
>   		ctx = inode->i_private;
>   		if (!ctx || ctx->domain != domain || strcmp(ctx->name, name))
>   			continue;
>   		igrab(inode);
> -		mutex_unlock(&erofs_domain_cookies_lock);
> +		spin_unlock(&psb->s_inode_list_lock);
>   		return ctx;
>   	}
> +	spin_unlock(&psb->s_inode_list_lock);
> +
> +	mutex_lock(&erofs_domain_cookies_lock);
>   	ctx = erofs_fscache_domain_init_cookie(sb, name, need_inode);
>   	mutex_unlock(&erofs_domain_cookies_lock);
>   	return ctx;
  

Patch

diff --git a/fs/erofs/fscache.c b/fs/erofs/fscache.c
index 998cd26a1b3b..bbf5268440df 100644
--- a/fs/erofs/fscache.c
+++ b/fs/erofs/fscache.c
@@ -589,15 +589,18 @@  struct erofs_fscache *erofs_domain_register_cookie(struct super_block *sb,
 	struct erofs_domain *domain = EROFS_SB(sb)->domain;
 	struct super_block *psb = erofs_pseudo_mnt->mnt_sb;
 
-	mutex_lock(&erofs_domain_cookies_lock);
+	spin_lock(&psb->s_inode_list_lock);
 	list_for_each_entry(inode, &psb->s_inodes, i_sb_list) {
 		ctx = inode->i_private;
 		if (!ctx || ctx->domain != domain || strcmp(ctx->name, name))
 			continue;
 		igrab(inode);
-		mutex_unlock(&erofs_domain_cookies_lock);
+		spin_unlock(&psb->s_inode_list_lock);
 		return ctx;
 	}
+	spin_unlock(&psb->s_inode_list_lock);
+
+	mutex_lock(&erofs_domain_cookies_lock);
 	ctx = erofs_fscache_domain_init_cookie(sb, name, need_inode);
 	mutex_unlock(&erofs_domain_cookies_lock);
 	return ctx;