fuse: fix invalidate lock leaks on DAX truncate error paths
Subsystems (3)
cover fuse: fix invalidate lock leaks on DAX truncate error paths
Baokun Li (2):
fuse: fix invalidate lock leak on setattr writeback failure
fuse: fix invalidate lock leak on open O_TRUNC DAX failure
fs/fuse/dir.c | 9 ++++----- fs/fuse/file.c | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.7
PATCH 1/2 fuse: fix invalidate lock leak on setattr writeback failure
fuse_do_setattr() takes filemap_invalidate_lock() for a DAX truncate (fault_blocked = true) and releases it at the out:/error: labels. But when a writeback flush is also needed, a write_inode_now() failure returns directly and leaks the lock, so any later fault or truncate on the file stalls on the stale rwsem.
For example, truncate(2) on a setuid file reaches fuse_do_setattr() with both ATTR_SIZE and ATTR_MODE set:
truncate(2)
└─ do_truncate()
├─ dentry_needs_remove_privs() # S_ISUID
└─ notify_change() # KILL_SUID -> ATTR_MODE
└─ fuse_setattr() # no killpriv:
│ # ia_valid |= ATTR_MODE
└─ fuse_do_setattr()
├─ filemap_invalidate_lock() # IS_DAX && is_truncate
└─ write_inode_now() # is_wb && ATTR_MODE
└─ if (err) # e.g. daemon -> -EIO
return err # <- lock leakedFix this by adding an unlock label that releases the lock before returning the error, and use it for the fuse_dax_break_layouts() failure path as well.
--- fs/fuse/dir.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
fs/fuse/dir.c +4 -5
@@ -2161,10 +2161,8 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,filemap_invalidate_lock (mapping); fault_blocked = true; err =fuse_dax_break_layouts (inode,0 ,-1 ); -if (err) { -filemap_invalidate_unlock (mapping); -return err; - } +if (err) + goto unlock; }if (attr->ia_valid &ATTR_OPEN ) {
@@ -2191,7 +2189,7 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry, ATTR_TIMES_SET)) { err =write_inode_now (inode, true);if (err) -return err; + goto unlock;fuse_set_nowrite (inode);fuse_release_nowrite (inode);
@@ -2299,6 +2297,7 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,clear_bit (FUSE_I_SIZE_UNSTABLE , &fi->state); +unlock:if (fault_blocked)filemap_invalidate_unlock (mapping);return err;
Signature · 1 line
2.43.7
PATCH 2/2 fuse: fix invalidate lock leak on open O_TRUNC DAX failure
In reply to Baokun Lifuse_open() takes filemap_invalidate_lock() for a DAX truncate (dax_truncate = true) and releases it before the out_inode_unlock label. But when fuse_dax_break_layouts() fails, the goto out_inode_unlock skips the unlock and leaks the rwsem, so any later fault or truncate on the file stalls on the stale lock.
fuse_dax_break_layouts() can fail with -ERESTARTSYS when a signal interrupts the wait for busy DAX pages to drain:
open("file", O_RDWR | O_TRUNC)
└─ fuse_open()
├─ filemap_invalidate_lock() # dax_truncate
└─ fuse_dax_break_layouts()
└─ dax_break_layout()
└─ wait_page_idle() # TASK_INTERRUPTIBLE
└─ fuse_wait_dax_page() # unlock, schedule, re-lock
└─ signal → -ERESTARTSYS
goto out_inode_unlock # <- lock leakedFix this by moving filemap_invalidate_unlock() below the label so that all error paths release the lock, and rename the label to out_unlock as it now covers more than just the inode lock.
--- fs/fuse/file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/fuse/file.c +2 -2
@@ -272,7 +272,7 @@ static int fuse_open(struct inode *inode, struct file *file)filemap_invalidate_lock (inode->i_mapping); err =fuse_dax_break_layouts (inode,0 ,-1 );if (err) - goto out_inode_unlock; + goto out_unlock; }if (is_wb_truncate || dax_truncate)
@@ -296,9 +296,9 @@ static int fuse_open(struct inode *inode, struct file *file)else if (!(ff->open_flags &FOPEN_KEEP_CACHE ))invalidate_inode_pages2 (inode->i_mapping); } +out_unlock:if (dax_truncate)filemap_invalidate_unlock (inode->i_mapping); -out_inode_unlock:if (is_wb_truncate || dax_truncate)inode_unlock (inode);
Signature · 1 line
2.43.7
Quoted 4 lines · Baokun Li
Baokun Li (2):
fuse: fix invalidate lock leak on setattr writeback failure
fuse: fix invalidate lock leak on openO_TRUNCDAX failure
Applied, thanks.
Miklos