Skip to contentSkip to conversation

ntfs3: fix deadlock in ntfs_force_shutdown

PATCH In review Deepanshu Kartikey 2 participants 4 messages Apr 4 – Apr 16 NTFS3 filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntfs3: fix deadlock in ntfs_force_shutdown

ntfs_force_shutdown() calls bdev_freeze() which internally calls freeze_super(). freeze_super() calls sb_wait_write() which waits for all active sb_writers holders to finish.

However active writers (ntfs_compress_write) can be stuck waiting for ni->file.run_lock while holding the sb_writers read lock acquired via file_start_write() in the VFS layer. This creates a deadlock where freeze_super() waits for writers that can never complete because they are blocked on run_lock contention.

Fix by removing bdev_freeze/bdev_thaw entirely. The shutdown bit NTFS_FLAGS_SHUTDOWN_BIT is already checked at entry of all ntfs3 write paths (file.c, inode.c, namei.c, frecord.c, fsntfs.c, super.c, xattr.c) and causes them to return errors immediately, making further writes impossible without risking a deadlock.

Reported-by [email protected]
Fixes ae91dfe38966 ("fs/ntfs3: implement NTFS3_IOC_SHUTDOWN ioctl")
---
 fs/ntfs3/file.c | 6 ------
 1 file changed, 6 deletions(-)
fs/ntfs3/file.c +0 -6
index 7eecf1e01f74..cbbc7d81875f 100644
@@ -118,18 +118,12 @@ static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
  */
 static int ntfs_force_shutdown(struct super_block *sb, u32 flags)
 {
-	int err;
 	struct ntfs_sb_info *sbi = sb->s_fs_info;
 
 	if (unlikely(ntfs3_forced_shutdown(sb)))
 		return 0;
 
-	/* No additional options yet (flags). */
-	err = bdev_freeze(sb->s_bdev);
-	if (err)
-		return err;
 	set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags);
-	bdev_thaw(sb->s_bdev);
 	return 0;
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 50 lines · Deepanshu Kartikey

ntfs_force_shutdown() calls bdev_freeze() which internally calls freeze_super(). freeze_super() calls sb_wait_write() which waits for all active sb_writers holders to finish.

However active writers (ntfs_compress_write) can be stuck waiting for ni->file.run_lock while holding the sb_writers read lock acquired via file_start_write() in the VFS layer. This creates a deadlock where freeze_super() waits for writers that can never complete because they are blocked on run_lock contention.

Fix by removing bdev_freeze/bdev_thaw entirely. The shutdown bit NTFS_FLAGS_SHUTDOWN_BIT is already checked at entry of all ntfs3 write paths (file.c, inode.c, namei.c, frecord.c, fsntfs.c, super.c, xattr.c) and causes them to return errors immediately, making further writes impossible without risking a deadlock.

Reported-by [email protected]
Fixes ae91dfe38966 ("fs/ntfs3: implement NTFS3_IOC_SHUTDOWN ioctl")
---
 fs/ntfs3/file.c | 6 ------
 1 file changed, 6 deletions(-)
fs/ntfs3/file.c +0 -7
index 7eecf1e01f74..cbbc7d81875f 100644
@@ -118,18 +118,12 @@ static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
  */
 static int ntfs_force_shutdown(struct super_block *sb, u32 flags)
 {
-       int err;
        struct ntfs_sb_info *sbi = sb->s_fs_info;
 
        if (unlikely(ntfs3_forced_shutdown(sb)))
                return 0;
 
-       /* No additional options yet (flags). */
-       err = bdev_freeze(sb->s_bdev);
-       if (err)
-               return err;
        set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags);
-       bdev_thaw(sb->s_bdev);
        return 0;
 }
 
--
Quoted 2 lines

2.43.0

Gentle ping on this patch. Please let me know the status of this patch.

Thanks

Deepanshu

#2 lore raw
In reply to Deepanshu Kartikey
Quoted 38 lines · Deepanshu Kartikey

ntfs_force_shutdown() calls bdev_freeze() which internally calls freeze_super(). freeze_super() calls sb_wait_write() which waits for all active sb_writers holders to finish.

However active writers (ntfs_compress_write) can be stuck waiting for ni->file.run_lock while holding the sb_writers read lock acquired via file_start_write() in the VFS layer. This creates a deadlock where freeze_super() waits for writers that can never complete because they are blocked on run_lock contention.

Fix by removing bdev_freeze/bdev_thaw entirely. The shutdown bit NTFS_FLAGS_SHUTDOWN_BIT is already checked at entry of all ntfs3 write paths (file.c, inode.c, namei.c, frecord.c, fsntfs.c, super.c, xattr.c) and causes them to return errors immediately, making further writes impossible without risking a deadlock.

Reported-by [email protected]
Fixes ae91dfe38966 ("fs/ntfs3: implement NTFS3_IOC_SHUTDOWN ioctl")
---
  fs/ntfs3/file.c | 6 ------
  1 file changed, 6 deletions(-)
fs/ntfs3/file.c +0 -1
index 7eecf1e01f74..cbbc7d81875f 100644
@@ -118,18 +118,12 @@ static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
   */
  static int ntfs_force_shutdown(struct super_block *sb, u32 flags)
  {
-	int err;
  	struct ntfs_sb_info *sbi = sb->s_fs_info;
 
  	if (unlikely(ntfs3_forced_shutdown(sb)))
  		return 0;

fs/ntfs3/file.c static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf) · line 129

Quoted 10 lines
fs/ntfs3/file.c +0 -5
 
-	/* No additional options yet (flags). */
-	err = bdev_freeze(sb->s_bdev);
-	if (err)
-		return err;
  	set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags);
-	bdev_thaw(sb->s_bdev);
  	return 0;
  }
 

Hello,

Your patch is applied. Thank you.

Regards,
Konstantin

#3 lore raw
In reply to Deepanshu Kartikey
Quoted 38 lines · Deepanshu Kartikey

ntfs_force_shutdown() calls bdev_freeze() which internally calls freeze_super(). freeze_super() calls sb_wait_write() which waits for all active sb_writers holders to finish.

However active writers (ntfs_compress_write) can be stuck waiting for ni->file.run_lock while holding the sb_writers read lock acquired via file_start_write() in the VFS layer. This creates a deadlock where freeze_super() waits for writers that can never complete because they are blocked on run_lock contention.

Fix by removing bdev_freeze/bdev_thaw entirely. The shutdown bit NTFS_FLAGS_SHUTDOWN_BIT is already checked at entry of all ntfs3 write paths (file.c, inode.c, namei.c, frecord.c, fsntfs.c, super.c, xattr.c) and causes them to return errors immediately, making further writes impossible without risking a deadlock.

Reported-by [email protected]
Fixes ae91dfe38966 ("fs/ntfs3: implement NTFS3_IOC_SHUTDOWN ioctl")
---
  fs/ntfs3/file.c | 6 ------
  1 file changed, 6 deletions(-)
fs/ntfs3/file.c +0 -1
index 7eecf1e01f74..cbbc7d81875f 100644
@@ -118,18 +118,12 @@ static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
   */
  static int ntfs_force_shutdown(struct super_block *sb, u32 flags)
  {
-	int err;
  	struct ntfs_sb_info *sbi = sb->s_fs_info;
 
  	if (unlikely(ntfs3_forced_shutdown(sb)))
  		return 0;

fs/ntfs3/file.c static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf) · line 129

Quoted 10 lines
fs/ntfs3/file.c +0 -5
 
-	/* No additional options yet (flags). */
-	err = bdev_freeze(sb->s_bdev);
-	if (err)
-		return err;
  	set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags);
-	bdev_thaw(sb->s_bdev);
  	return 0;
  }
 

Hello, I missed this before, but internal testing shows that this patch causes several failures. I will not include it in the pull request for now. I am looking into the root cause and will reply with details once I have them. If the fix turns out to be straightforward, I will send an updated patch as soon as possible. Regards, Konstantin