[PATCH] fs/ntfs3: fix deadlock in ni_readpage_cmpr

Szymon Wilczek posted 1 patch 1 month, 2 weeks ago
fs/ntfs3/frecord.c | 2 ++
fs/ntfs3/inode.c   | 3 +--
2 files changed, 3 insertions(+), 2 deletions(-)
[PATCH] fs/ntfs3: fix deadlock in ni_readpage_cmpr
Posted by Szymon Wilczek 1 month, 2 weeks ago
Syzbot reported a task hung in ni_readpage_cmpr. This is caused by a lock
inversion deadlock involving the inode mutex (ni_lock) and page locks.

Scenario:
1. Task A enters ntfs_read_folio() for page X. It acquires ni_lock.
2. Task A calls ni_readpage_cmpr(), which attempts to lock all pages in
   the compressed frame (including page Y).
3. Concurrently, Task B (e.g., via readahead) has locked page Y and
   calls ntfs_read_folio().
4. Task B waits for ni_lock (held by A).
5. Task A waits for page Y lock (held by B).
   -> DEADLOCK.

The fix is to restructure locking: do not take ni_lock in ntfs_read_folio().
Instead, acquire ni_lock inside ni_readpage_cmpr() ONLY AFTER all required
page locks for the frame have been successfully acquired. This restores the
correct lock ordering (Page Lock -> ni_lock) consistent with VFS.

Reported-by: syzbot+5af33dd272b913b65880@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=5af33dd272b913b65880
Fixes: f35590ee26f5 ("fs/ntfs3: remove ntfs_bio_pages and use page cache for compressed I/O")
Signed-off-by: Szymon Wilczek <swilczek.lx@gmail.com>
---
 fs/ntfs3/frecord.c | 2 ++
 fs/ntfs3/inode.c   | 3 +--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/frecord.c b/fs/ntfs3/frecord.c
index 641ddaf8d4a0..f09a149cff9f 100644
--- a/fs/ntfs3/frecord.c
+++ b/fs/ntfs3/frecord.c
@@ -2107,7 +2107,9 @@ int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio)
 		pages[i] = pg;
 	}
 
+	ni_lock(ni);
 	err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame, 0);
+	ni_unlock(ni);
 
 out1:
 	for (i = 0; i < pages_per_frame; i++) {
diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c
index 0a9ac5efeb67..33f819b162a5 100644
--- a/fs/ntfs3/inode.c
+++ b/fs/ntfs3/inode.c
@@ -735,9 +735,8 @@ static int ntfs_read_folio(struct file *file, struct folio *folio)
 	}
 
 	if (is_compressed(ni)) {
-		ni_lock(ni);
+		/* ni_lock is taken inside ni_readpage_cmpr after page locks */
 		err = ni_readpage_cmpr(ni, folio);
-		ni_unlock(ni);
 		return err;
 	}
 
-- 
2.52.0
Re: [PATCH] fs/ntfs3: fix deadlock in ni_readpage_cmpr
Posted by Konstantin Komarov 1 month, 1 week ago
On 12/22/25 16:10, Szymon Wilczek wrote:

> Syzbot reported a task hung in ni_readpage_cmpr. This is caused by a lock
> inversion deadlock involving the inode mutex (ni_lock) and page locks.
>
> Scenario:
> 1. Task A enters ntfs_read_folio() for page X. It acquires ni_lock.
> 2. Task A calls ni_readpage_cmpr(), which attempts to lock all pages in
>     the compressed frame (including page Y).
> 3. Concurrently, Task B (e.g., via readahead) has locked page Y and
>     calls ntfs_read_folio().
> 4. Task B waits for ni_lock (held by A).
> 5. Task A waits for page Y lock (held by B).
>     -> DEADLOCK.
>
> The fix is to restructure locking: do not take ni_lock in ntfs_read_folio().
> Instead, acquire ni_lock inside ni_readpage_cmpr() ONLY AFTER all required
> page locks for the frame have been successfully acquired. This restores the
> correct lock ordering (Page Lock -> ni_lock) consistent with VFS.
>
> Reported-by: syzbot+5af33dd272b913b65880@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=5af33dd272b913b65880
> Fixes: f35590ee26f5 ("fs/ntfs3: remove ntfs_bio_pages and use page cache for compressed I/O")
> Signed-off-by: Szymon Wilczek <swilczek.lx@gmail.com>
> ---
>   fs/ntfs3/frecord.c | 2 ++
>   fs/ntfs3/inode.c   | 3 +--
>   2 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/fs/ntfs3/frecord.c b/fs/ntfs3/frecord.c
> index 641ddaf8d4a0..f09a149cff9f 100644
> --- a/fs/ntfs3/frecord.c
> +++ b/fs/ntfs3/frecord.c
> @@ -2107,7 +2107,9 @@ int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio)
>   		pages[i] = pg;
>   	}
>   
> +	ni_lock(ni);
>   	err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame, 0);
> +	ni_unlock(ni);
>   
>   out1:
>   	for (i = 0; i < pages_per_frame; i++) {
> diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c
> index 0a9ac5efeb67..33f819b162a5 100644
> --- a/fs/ntfs3/inode.c
> +++ b/fs/ntfs3/inode.c
> @@ -735,9 +735,8 @@ static int ntfs_read_folio(struct file *file, struct folio *folio)
>   	}
>   
>   	if (is_compressed(ni)) {
> -		ni_lock(ni);
> +		/* ni_lock is taken inside ni_readpage_cmpr after page locks */
>   		err = ni_readpage_cmpr(ni, folio);
> -		ni_unlock(ni);
>   		return err;
>   	}
>   

Your patch is applied, thanks.

Regards,
Konstantin