linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Btrfs: fix lockdep warning about log_mutex
@ 2016-12-01 21:45 Liu Bo
  2016-12-05 11:11 ` Filipe Manana
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Liu Bo @ 2016-12-01 21:45 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba, fdmanana

While checking INODE_REF/INODE_EXTREF for a corner case, we may acquire a
different inode's log_mutex with holding the current inode's log_mutex, and
lockdep has complained this with a possilble deadlock warning.

Fix this by using mutex_lock_nested() when processing the other inode's
log_mutex.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
 fs/btrfs/tree-log.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 3d33c4e..e961451 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -37,6 +37,7 @@
  */
 #define LOG_INODE_ALL 0
 #define LOG_INODE_EXISTS 1
+#define LOG_OTHER_INODE 2
 
 /*
  * directory trouble cases
@@ -4624,7 +4625,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 	if (S_ISDIR(inode->i_mode) ||
 	    (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
 		       &BTRFS_I(inode)->runtime_flags) &&
-	     inode_only == LOG_INODE_EXISTS))
+	     inode_only >= LOG_INODE_EXISTS))
 		max_key.type = BTRFS_XATTR_ITEM_KEY;
 	else
 		max_key.type = (u8)-1;
@@ -4648,7 +4649,12 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 		return ret;
 	}
 
-	mutex_lock(&BTRFS_I(inode)->log_mutex);
+	if (inode_only == LOG_OTHER_INODE) {
+		inode_only = LOG_INODE_EXISTS;
+		mutex_lock_nested(&BTRFS_I(inode)->log_mutex, 1);
+	} else {
+		mutex_lock(&BTRFS_I(inode)->log_mutex);
+	}
 
 	/*
 	 * a brute force approach to making sure we get the most uptodate
@@ -4800,7 +4806,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 				 * unpin it.
 				 */
 				err = btrfs_log_inode(trans, root, other_inode,
-						      LOG_INODE_EXISTS,
+						      LOG_OTHER_INODE,
 						      0, LLONG_MAX, ctx);
 				iput(other_inode);
 				if (err)
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Btrfs: fix lockdep warning about log_mutex
  2016-12-01 21:45 [PATCH] Btrfs: fix lockdep warning about log_mutex Liu Bo
@ 2016-12-05 11:11 ` Filipe Manana
  2016-12-12 13:16 ` David Sterba
  2016-12-13 23:59 ` [PATCH v2] " Liu Bo
  2 siblings, 0 replies; 4+ messages in thread
From: Filipe Manana @ 2016-12-05 11:11 UTC (permalink / raw)
  To: Liu Bo; +Cc: linux-btrfs@vger.kernel.org, David Sterba

On Thu, Dec 1, 2016 at 9:45 PM, Liu Bo <bo.li.liu@oracle.com> wrote:
> While checking INODE_REF/INODE_EXTREF for a corner case, we may acquire a
> different inode's log_mutex with holding the current inode's log_mutex, and
> lockdep has complained this with a possilble deadlock warning.
>
> Fix this by using mutex_lock_nested() when processing the other inode's
> log_mutex.
>
> Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>

Thanks, it works and it's simple.

> ---
>  fs/btrfs/tree-log.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 3d33c4e..e961451 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -37,6 +37,7 @@
>   */
>  #define LOG_INODE_ALL 0
>  #define LOG_INODE_EXISTS 1
> +#define LOG_OTHER_INODE 2
>
>  /*
>   * directory trouble cases
> @@ -4624,7 +4625,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
>         if (S_ISDIR(inode->i_mode) ||
>             (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
>                        &BTRFS_I(inode)->runtime_flags) &&
> -            inode_only == LOG_INODE_EXISTS))
> +            inode_only >= LOG_INODE_EXISTS))
>                 max_key.type = BTRFS_XATTR_ITEM_KEY;
>         else
>                 max_key.type = (u8)-1;
> @@ -4648,7 +4649,12 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
>                 return ret;
>         }
>
> -       mutex_lock(&BTRFS_I(inode)->log_mutex);
> +       if (inode_only == LOG_OTHER_INODE) {
> +               inode_only = LOG_INODE_EXISTS;
> +               mutex_lock_nested(&BTRFS_I(inode)->log_mutex, 1);
> +       } else {
> +               mutex_lock(&BTRFS_I(inode)->log_mutex);
> +       }
>
>         /*
>          * a brute force approach to making sure we get the most uptodate
> @@ -4800,7 +4806,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
>                                  * unpin it.
>                                  */
>                                 err = btrfs_log_inode(trans, root, other_inode,
> -                                                     LOG_INODE_EXISTS,
> +                                                     LOG_OTHER_INODE,
>                                                       0, LLONG_MAX, ctx);
>                                 iput(other_inode);
>                                 if (err)
> --
> 2.5.5
>



-- 
Filipe David Manana,

"People will forget what you said,
 people will forget what you did,
 but people will never forget how you made them feel."

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Btrfs: fix lockdep warning about log_mutex
  2016-12-01 21:45 [PATCH] Btrfs: fix lockdep warning about log_mutex Liu Bo
  2016-12-05 11:11 ` Filipe Manana
@ 2016-12-12 13:16 ` David Sterba
  2016-12-13 23:59 ` [PATCH v2] " Liu Bo
  2 siblings, 0 replies; 4+ messages in thread
From: David Sterba @ 2016-12-12 13:16 UTC (permalink / raw)
  To: Liu Bo; +Cc: linux-btrfs, David Sterba, fdmanana

On Thu, Dec 01, 2016 at 01:45:04PM -0800, Liu Bo wrote:
> +		mutex_lock_nested(&BTRFS_I(inode)->log_mutex, 1);

Please also use SINGLE_DEPTH_NESTING here.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] Btrfs: fix lockdep warning about log_mutex
  2016-12-01 21:45 [PATCH] Btrfs: fix lockdep warning about log_mutex Liu Bo
  2016-12-05 11:11 ` Filipe Manana
  2016-12-12 13:16 ` David Sterba
@ 2016-12-13 23:59 ` Liu Bo
  2 siblings, 0 replies; 4+ messages in thread
From: Liu Bo @ 2016-12-13 23:59 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

While checking INODE_REF/INODE_EXTREF for a corner case, we may acquire a
different inode's log_mutex with holding the current inode's log_mutex, and
lockdep has complained this with a possilble deadlock warning.

Fix this by using mutex_lock_nested() when processing the other inode's
log_mutex.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
v2: Use SINGLE_DEPTH_NESTING to avoid magic number.

 fs/btrfs/tree-log.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 3d33c4e..298ab3b 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -37,6 +37,7 @@
  */
 #define LOG_INODE_ALL 0
 #define LOG_INODE_EXISTS 1
+#define LOG_OTHER_INODE 2
 
 /*
  * directory trouble cases
@@ -4624,7 +4625,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 	if (S_ISDIR(inode->i_mode) ||
 	    (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
 		       &BTRFS_I(inode)->runtime_flags) &&
-	     inode_only == LOG_INODE_EXISTS))
+	     inode_only >= LOG_INODE_EXISTS))
 		max_key.type = BTRFS_XATTR_ITEM_KEY;
 	else
 		max_key.type = (u8)-1;
@@ -4648,7 +4649,13 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 		return ret;
 	}
 
-	mutex_lock(&BTRFS_I(inode)->log_mutex);
+	if (inode_only == LOG_OTHER_INODE) {
+		inode_only = LOG_INODE_EXISTS;
+		mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
+				  SINGLE_DEPTH_NESTING);
+	} else {
+		mutex_lock(&BTRFS_I(inode)->log_mutex);
+	}
 
 	/*
 	 * a brute force approach to making sure we get the most uptodate
@@ -4800,7 +4807,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
 				 * unpin it.
 				 */
 				err = btrfs_log_inode(trans, root, other_inode,
-						      LOG_INODE_EXISTS,
+						      LOG_OTHER_INODE,
 						      0, LLONG_MAX, ctx);
 				iput(other_inode);
 				if (err)
-- 
2.5.5


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-12-13 23:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-01 21:45 [PATCH] Btrfs: fix lockdep warning about log_mutex Liu Bo
2016-12-05 11:11 ` Filipe Manana
2016-12-12 13:16 ` David Sterba
2016-12-13 23:59 ` [PATCH v2] " Liu Bo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).