Skip to contentSkip to conversation

btrfs: fix lockdep warnings on io_uring encoded reads

PATCH Superseded Mark Harmstone 4 participants 6 messages Nov 12, 2024 – Nov 15, 2024 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix lockdep warnings on io_uring encoded reads

Lockdep doesn't like the fact that btrfs_uring_read_extent() returns to userspace still holding the inode lock, even though we release it once the I/O finishes. Add calls to rwsem_release() and rwsem_acquire_read() to work round this.

---
 fs/btrfs/ioctl.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/btrfs/ioctl.c +14 -0
index 1fdeb216bf6c..6ea01e4f940e 100644
@@ -4752,6 +4752,11 @@ static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss
 	size_t page_offset;
 	ssize_t ret;
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
+	rwsem_acquire_read(&inode->vfs_inode.i_rwsem.dep_map, 0, 0, _THIS_IP_);
+#endif
+
 	if (priv->err) {
 		ret = priv->err;
 		goto out;
@@ -4860,6 +4865,15 @@ static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
 	 * and inode and freeing the allocations.
 	 */
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/*
+	 * We're returning to userspace with the inode lock held, and that's
+	 * okay - it'll get unlocked in a kthread.  Call rwsem_release to
+	 * avoid confusing lockdep.
+	 */
+	rwsem_release(&inode->vfs_inode.i_rwsem.dep_map, _THIS_IP_);
+#endif
+
 	return -EIOCBQUEUED;
 
 out_fail:
Signature · 1 line
2.45.2
#1 lore raw
Quoted 33 lines · Mark Harmstone

Lockdep doesn't like the fact that btrfs_uring_read_extent() returns to userspace still holding the inode lock, even though we release it once the I/O finishes. Add calls to rwsem_release() and rwsem_acquire_read() to work round this.

---
  fs/btrfs/ioctl.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
fs/btrfs/ioctl.c +7 -0
index 1fdeb216bf6c..6ea01e4f940e 100644
@@ -4752,6 +4752,11 @@ static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss
  	size_t page_offset;
  	ssize_t ret;
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
+	rwsem_acquire_read(&inode->vfs_inode.i_rwsem.dep_map, 0, 0, _THIS_IP_);
+#endif
+
  	if (priv->err) {
  		ret = priv->err;
  		goto out;
@@ -4860,6 +4865,15 @@ static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
  	 * and inode and freeing the allocations.
  	 */
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/*

fs/btrfs/ioctl.c static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter, · line 4879

Quoted 10 lines
fs/btrfs/ioctl.c +7 -0
+	 * We're returning to userspace with the inode lock held, and that's
+	 * okay - it'll get unlocked in a kthread.  Call rwsem_release to
+	 * avoid confusing lockdep.
+	 */
+	rwsem_release(&inode->vfs_inode.i_rwsem.dep_map, _THIS_IP_);
+#endif
+
  	return -EIOCBQUEUED;
 
  out_fail:

Can't say anything about the correctness (as I have no clue), but we have wrappers around rwsem_release (btrfs_lockdep_release()) and rwsem_acquire_read (btrfs_lockdep_acquire()) that I think serve the documentation purpose.

#2 lore raw
Quoted 44 lines · Johannes Thumshirn
Quoted 1 line
Quoted 42 lines · Mark Harmstone

Lockdep doesn't like the fact that btrfs_uring_read_extent() returns to userspace still holding the inode lock, even though we release it once the I/O finishes. Add calls to rwsem_release() and rwsem_acquire_read() to work round this.

---
   fs/btrfs/ioctl.c | 14 ++++++++++++++
   1 file changed, 14 insertions(+)
fs/btrfs/ioctl.c +14 -0
index 1fdeb216bf6c..6ea01e4f940e 100644
@@ -4752,6 +4752,11 @@ static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss
   	size_t page_offset;
   	ssize_t ret;
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
+	rwsem_acquire_read(&inode->vfs_inode.i_rwsem.dep_map, 0, 0, _THIS_IP_);
+#endif
+
   	if (priv->err) {
   		ret = priv->err;
   		goto out;
@@ -4860,6 +4865,15 @@ static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
   	 * and inode and freeing the allocations.
   	 */
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/*
+	 * We're returning to userspace with the inode lock held, and that's
+	 * okay - it'll get unlocked in a kthread.  Call rwsem_release to
+	 * avoid confusing lockdep.
+	 */
+	rwsem_release(&inode->vfs_inode.i_rwsem.dep_map, _THIS_IP_);
+#endif
+
   	return -EIOCBQUEUED;
 
Quoted 6 lines

fs/btrfs/ioctl.c static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter, · line 4879

Quoted 1 line
fs/btrfs/ioctl.c +0 -0
   out_fail:

Can't say anything about the correctness (as I have no clue), but we have wrappers around rwsem_release (btrfs_lockdep_release()) and rwsem_acquire_read (btrfs_lockdep_acquire()) that I think serve the documentation purpose.

btrfs_lockdep_acquire(a,b) calls rwsem_acquire_read on &a->b_map, so can't be used for something located at &inode->vfs_inode.i_rwsem.dep_map.

Mark

#3 lore raw
Quoted 52 lines · Mark Harmstone
Quoted 44 lines · Johannes Thumshirn
Quoted 1 line
Quoted 42 lines · Mark Harmstone

Lockdep doesn't like the fact that btrfs_uring_read_extent() returns to userspace still holding the inode lock, even though we release it once the I/O finishes. Add calls to rwsem_release() and rwsem_acquire_read() to work round this.

---
    fs/btrfs/ioctl.c | 14 ++++++++++++++
    1 file changed, 14 insertions(+)
fs/btrfs/ioctl.c +14 -0
index 1fdeb216bf6c..6ea01e4f940e 100644
@@ -4752,6 +4752,11 @@ static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss
    	size_t page_offset;
    	ssize_t ret;
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
+	rwsem_acquire_read(&inode->vfs_inode.i_rwsem.dep_map, 0, 0, _THIS_IP_);
+#endif
+
    	if (priv->err) {
    		ret = priv->err;
    		goto out;
@@ -4860,6 +4865,15 @@ static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
    	 * and inode and freeing the allocations.
    	 */
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/*
+	 * We're returning to userspace with the inode lock held, and that's
+	 * okay - it'll get unlocked in a kthread.  Call rwsem_release to
+	 * avoid confusing lockdep.
+	 */
+	rwsem_release(&inode->vfs_inode.i_rwsem.dep_map, _THIS_IP_);
+#endif
+
    	return -EIOCBQUEUED;
 
Quoted 6 lines

fs/btrfs/ioctl.c static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter, · line 4879

Quoted 1 line
fs/btrfs/ioctl.c +0 -0
    out_fail:

Can't say anything about the correctness (as I have no clue), but we have wrappers around rwsem_release (btrfs_lockdep_release()) and rwsem_acquire_read (btrfs_lockdep_acquire()) that I think serve the documentation purpose.

Quoted 2 lines

btrfs_lockdep_acquire(a,b) calls rwsem_acquire_read on &a->b_map, so can't be used for something located at &inode->vfs_inode.i_rwsem.dep_map.

Ah my bad, sorry.

#4 lore raw
In reply to Mark Harmstone
Quoted 16 lines · Mark Harmstone

Lockdep doesn't like the fact that btrfs_uring_read_extent() returns to userspace still holding the inode lock, even though we release it once the I/O finishes. Add calls to rwsem_release() and rwsem_acquire_read() to work round this.

---
 fs/btrfs/ioctl.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/btrfs/ioctl.c +0 -0
index 1fdeb216bf6c..6ea01e4f940e 100644

fs/btrfs/ioctl.c static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss · line 4758

Quoted 7 lines
fs/btrfs/ioctl.c +4 -0
@@ -4752,6 +4752,11 @@ static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss
 	size_t page_offset;
 	ssize_t ret;
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
+	rwsem_acquire_read(&inode->vfs_inode.i_rwsem.dep_map, 0, 0, _THIS_IP_);
+#endif

Please put that to a wrapper, we want to avoid #ifdefs in the middle of functions (there are exceptions), especially when the macro is for lock debugging.

The wrapper name can follow naming and syntax of the other btrfs_lockde_* wrappers, so like btrfs_lockdep_inode_acquire(owner, lock). There is only one rwsem in inode, but for clarity and consistency I think it makes sense.

btrfs_lockdep_inode_acquire(inode, i_rwsem);

#5 lore raw
Quoted 30 lines · David Sterba
Quoted 1 line
Quoted 16 lines · Mark Harmstone

Lockdep doesn't like the fact that btrfs_uring_read_extent() returns to userspace still holding the inode lock, even though we release it once the I/O finishes. Add calls to rwsem_release() and rwsem_acquire_read() to work round this.

---
  fs/btrfs/ioctl.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
fs/btrfs/ioctl.c +0 -0
index 1fdeb216bf6c..6ea01e4f940e 100644

fs/btrfs/ioctl.c static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss · line 4758

Quoted 7 lines
fs/btrfs/ioctl.c +4 -0
@@ -4752,6 +4752,11 @@ static void btrfs_uring_read_finished(struct io_uring_cmd *cmd, unsigned int iss
  	size_t page_offset;
  	ssize_t ret;
 
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	/* The inode lock has already been acquired in btrfs_uring_read_extent.  */
+	rwsem_acquire_read(&inode->vfs_inode.i_rwsem.dep_map, 0, 0, _THIS_IP_);
+#endif

Please put that to a wrapper, we want to avoid #ifdefs in the middle of functions (there are exceptions), especially when the macro is for lock debugging.

Quoted 6 lines

The wrapper name can follow naming and syntax of the other btrfs_lockde_* wrappers, so like btrfs_lockdep_inode_acquire(owner, lock). There is only one rwsem in inode, but for clarity and consistency I think it makes sense.

btrfs_lockdep_inode_acquire(inode, i_rwsem);

Thanks David. The CONFIG_DEBUG_LOCK_ALLOC #ifdefs turned out not to be necessary, lockdep handles this, but I'll create the wrappers to match everything else.

Mark