Skip to contentSkip to conversation

fix deadlock caused by recursive cluster locking

PATCH Superseded Eric Ren 4 participants 32 messages Jan 5, 2017 – Jan 16, 2017 OCFS2

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eric Ren

author
#0 lore raw

cover fix deadlock caused by recursive cluster locking

This is a formal patch set to solve the deadlock issue on which I previously started a RFC (draft patch), and the discussion happened here:
[https://oss.oracle.com/pipermail/ocfs2-devel/2016-October/012455.html]

Compared to the previous draft patch, this one is much simple and neat. It neither messes up the dlmglue core, nor has a performance penalty on the whole cluster locking system. Instead, it is only used in places where such recursive cluster locking may happen.

Your comments and feedbacks are always welcomed.

Eric Ren (2):
ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock
ocfs2: fix deadlocks when taking inode lock at vfs entry points

 fs/ocfs2/acl.c     | 39 ++++++++++++++++++++++++++++++++++-----
 fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
 fs/ocfs2/file.c    | 44 ++++++++++++++++++++++++++++++++++----------
 fs/ocfs2/ocfs2.h   |  1 +
 5 files changed, 131 insertions(+), 18 deletions(-)
Signature · 1 line
2.6.6
author
#1 lore raw

PATCH 1/2 ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

    .permission     = ocfs2_permission,
    .get_acl        = ocfs2_iop_get_acl,
    .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission
ocfs2_permission

    ocfs2_inode_lock() <=== first time
     generic_permission
      get_acl
       ocfs2_iop_get_acl
	ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started
on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full: OCFS2_META_LOCK_GETBH; it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines can call into each other.

The performance penalty of processing the holder list should only be seen at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world, as far as I can see, including permission check, (get|set)_(acl|attr), and the gfs2 code also do so.

---
 fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
 fs/ocfs2/ocfs2.h   |  1 +
 3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +44 -3
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
 	init_waitqueue_head(&res->l_event);
 	INIT_LIST_HEAD(&res->l_blocked_list);
 	INIT_LIST_HEAD(&res->l_mask_waiters);
+	INIT_LIST_HEAD(&res->l_holders);
 }
 
 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
 	res->l_flags = 0UL;
 }
 
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+				   struct ocfs2_holder *oh)
+{
+	INIT_LIST_HEAD(&oh->oh_list);
+	oh->oh_owner_pid =  get_pid(task_pid(current));
+
+	spin_lock(&lockres->l_lock);
+	list_add_tail(&oh->oh_list, &lockres->l_holders);
+	spin_unlock(&lockres->l_lock);
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+				       struct ocfs2_holder *oh)
+{
+	spin_lock(&lockres->l_lock);
+	list_del(&oh->oh_list);
+	spin_unlock(&lockres->l_lock);
+
+	put_pid(oh->oh_owner_pid);
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)
+{
+	struct ocfs2_holder *oh;
+	struct pid *pid;
+
+	/* look in the list of holders for one with the current task as owner */
+	spin_lock(&lockres->l_lock);
+	pid = task_pid(current);
+	list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+		if (oh->oh_owner_pid == pid)
+			goto out;
+	}
+	oh = NULL;
+out:
+	spin_unlock(&lockres->l_lock);
+	return oh;
+}
+
 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
 				     int level)
 {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
 		goto getbh;
 	}
 
-	if (ocfs2_mount_local(osb))
-		goto local;
+	if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+	    ocfs2_mount_local(osb))
+		goto update;
 
 	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
 		ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
 	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
 		ocfs2_wait_for_recovery(osb);
 
-local:
+update:
 	/*
 	 * We only see this flag if we're being called from
 	 * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +18 -0
index d293a22..d65ff1e 100644
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
 	__be32	lvb_os_seqno;
 };
 
+struct ocfs2_holder {
+	struct list_head oh_list;
+	struct pid *oh_owner_pid;
+};
+
 /* ocfs2_inode_lock_full() 'arg_flags' flags */
 /* don't wait on recovery. */
 #define OCFS2_META_LOCK_RECOVERY	(0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
 #define OCFS2_META_LOCK_NOQUEUE		(0x02)
 /* don't block waiting for the downconvert thread, instead return -EAGAIN */
 #define OCFS2_LOCK_NONBLOCK		(0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH		(0x08)
 
 /* Locking subclasses of inode cluster lock */
 enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
 
 /* To set the locking protocol on module initialization */
 void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
+
 #endif	/* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
 
 	struct list_head         l_blocked_list;
 	struct list_head         l_mask_waiters;
+	struct list_head	 l_holders;
 
 	unsigned long		 l_flags;
 	char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
Signature · 1 line
2.6.6
#2 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Hi Eric,

Quoted 108 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

     .permission     = ocfs2_permission,
     .get_acl        = ocfs2_iop_get_acl,
     .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission

    ocfs2_permission
     ocfs2_inode_lock() <=== first time
      generic_permission
       get_acl
        ocfs2_iop_get_acl
	ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started
on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full: OCFS2_META_LOCK_GETBH; it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines can call into each other.

The performance penalty of processing the holder list should only be seen at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world, as far as I can see, including permission check, (get|set)_(acl|attr), and the gfs2 code also do so.

---
  fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
  fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
  fs/ocfs2/ocfs2.h   |  1 +
  3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +29 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
  	init_waitqueue_head(&res->l_event);
  	INIT_LIST_HEAD(&res->l_blocked_list);
  	INIT_LIST_HEAD(&res->l_mask_waiters);
+	INIT_LIST_HEAD(&res->l_holders);
  }
 
  void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
  	res->l_flags = 0UL;
  }
 
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+				   struct ocfs2_holder *oh)
+{
+	INIT_LIST_HEAD(&oh->oh_list);
+	oh->oh_owner_pid =  get_pid(task_pid(current));
+
+	spin_lock(&lockres->l_lock);
+	list_add_tail(&oh->oh_list, &lockres->l_holders);
+	spin_unlock(&lockres->l_lock);
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+				       struct ocfs2_holder *oh)
+{
+	spin_lock(&lockres->l_lock);
+	list_del(&oh->oh_list);
+	spin_unlock(&lockres->l_lock);
+
+	put_pid(oh->oh_owner_pid);
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)
+{
+	struct ocfs2_holder *oh;
+	struct pid *pid;
+
+	/* look in the list of holders for one with the current task as owner */
+	spin_lock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 790

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+	pid = task_pid(current);
+	list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+		if (oh->oh_owner_pid == pid)
+			goto out;
+	}
+	oh = NULL;
+out:
+	spin_unlock(&lockres->l_lock);
+	return oh;
+}

Since this ocfs2_holder won't be used in the caller, I suggest just return a bool value here.
Something like:
spin_lock();

list_for_each_entry() {
     if (oh->oh_owner_pid == pid) {
         spin_unlock();
         return 1;
     }
}

spin_unlock();
return 0;

Thanks,
Joseph

Quoted 78 lines
fs/ocfs2/dlmglue.c +5 -3
+
  static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
  				     int level)
  {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
  		goto getbh;
  	}
 
-	if (ocfs2_mount_local(osb))
-		goto local;
+	if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+	    ocfs2_mount_local(osb))
+		goto update;
 
  	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  		ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
  	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  		ocfs2_wait_for_recovery(osb);
 
-local:
+update:
  	/*
  	 * We only see this flag if we're being called from
  	 * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +18 -0
index d293a22..d65ff1e 100644
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
  	__be32	lvb_os_seqno;
  };
 
+struct ocfs2_holder {
+	struct list_head oh_list;
+	struct pid *oh_owner_pid;
+};
+
  /* ocfs2_inode_lock_full() 'arg_flags' flags */
  /* don't wait on recovery. */
  #define OCFS2_META_LOCK_RECOVERY	(0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
  #define OCFS2_META_LOCK_NOQUEUE		(0x02)
  /* don't block waiting for the downconvert thread, instead return -EAGAIN */
  #define OCFS2_LOCK_NONBLOCK		(0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH		(0x08)
 
  /* Locking subclasses of inode cluster lock */
  enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
 
  /* To set the locking protocol on module initialization */
  void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
+
  #endif	/* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
 
  	struct list_head         l_blocked_list;
  	struct list_head         l_mask_waiters;
+	struct list_head	 l_holders;
 
  	unsigned long		 l_flags;
  	char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
author
#3 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Quoted 123 lines · Joseph Qi

Hi Eric,

Quoted 106 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

     .permission     = ocfs2_permission,
     .get_acl        = ocfs2_iop_get_acl,
     .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission

    ocfs2_permission
     ocfs2_inode_lock() <=== first time
      generic_permission
       get_acl
        ocfs2_iop_get_acl
    ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started
on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full: OCFS2_META_LOCK_GETBH; it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines can call into each other.

The performance penalty of processing the holder list should only be seen at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world, as far as I can see, including permission check, (get|set)_(acl|attr), and the gfs2 code also do so.

---
  fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
  fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
  fs/ocfs2/ocfs2.h   |  1 +
  3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +28 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
      init_waitqueue_head(&res->l_event);
      INIT_LIST_HEAD(&res->l_blocked_list);
      INIT_LIST_HEAD(&res->l_mask_waiters);
+    INIT_LIST_HEAD(&res->l_holders);
  }
    void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
      res->l_flags = 0UL;
  }
  +inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                   struct ocfs2_holder *oh)
+{
+    INIT_LIST_HEAD(&oh->oh_list);
+    oh->oh_owner_pid =  get_pid(task_pid(current));
+
+    spin_lock(&lockres->l_lock);
+    list_add_tail(&oh->oh_list, &lockres->l_holders);
+    spin_unlock(&lockres->l_lock);
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                       struct ocfs2_holder *oh)
+{
+    spin_lock(&lockres->l_lock);
+    list_del(&oh->oh_list);
+    spin_unlock(&lockres->l_lock);
+
+    put_pid(oh->oh_owner_pid);
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)
+{
+    struct ocfs2_holder *oh;
+    struct pid *pid;
+
+    /* look in the list of holders for one with the current task as owner */
+    spin_lock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 789

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+    pid = task_pid(current);
+    list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+        if (oh->oh_owner_pid == pid)
+            goto out;
+    }
+    oh = NULL;
+out:
+    spin_unlock(&lockres->l_lock);
+    return oh;
+}

Since this ocfs2_holder won't be used in the caller, I suggest just return a bool value here. Something like:
spin_lock();

Quoted 8 lines
list_for_each_entry() {
    if (oh->oh_owner_pid == pid) {
        spin_unlock();
        return 1;
    }
}

spin_unlock();
return 0;

Aha, you have the point. However, it is also reasonable to return the lock holder by the way. When debugging with printk or crash, it's easy to get the pid of the holder without any further analysis. So, I tend to keep it;-)

Thanks very much for your review!
Eric

Quoted 75 lines

Thanks,
Joseph

Quoted 70 lines
fs/ocfs2/dlmglue.c +5 -1
+
  static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
                       int level)
  {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
          goto getbh;
      }
  -    if (ocfs2_mount_local(osb))
-        goto local;
+    if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+        ocfs2_mount_local(osb))
+        goto update;
        if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
      if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
  -local:
+update:
      /*
       * We only see this flag if we're being called from
       * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +17 -0
index d293a22..d65ff1e 100644
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
      __be32    lvb_os_seqno;
  };
  +struct ocfs2_holder {
+    struct list_head oh_list;
+    struct pid *oh_owner_pid;
+};
+
  /* ocfs2_inode_lock_full() 'arg_flags' flags */
  /* don't wait on recovery. */
  #define OCFS2_META_LOCK_RECOVERY    (0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
  #define OCFS2_META_LOCK_NOQUEUE        (0x02)
  /* don't block waiting for the downconvert thread, instead return -EAGAIN */
  #define OCFS2_LOCK_NONBLOCK        (0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH        (0x08)
    /* Locking subclasses of inode cluster lock */
  enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
    /* To set the locking protocol on module initialization */
  void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
+
  #endif    /* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
        struct list_head         l_blocked_list;
      struct list_head         l_mask_waiters;
+    struct list_head     l_holders;
        unsigned long         l_flags;
      char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
#4 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Quoted 151 lines · Eric Ren
Quoted 141 lines · Joseph Qi

Hi Eric,

Quoted 123 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking,
but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

     .permission     = ocfs2_permission,
     .get_acl        = ocfs2_iop_get_acl,
     .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission

    ocfs2_permission
     ocfs2_inode_lock() <=== first time
      generic_permission
       get_acl
        ocfs2_iop_get_acl
    ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock()
because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why?
because there is no chance for the first cluster lock on this node to be
unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full:
OCFS2_META_LOCK_GETBH;
it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines
can call into each other.

The performance penalty of processing the holder list should only be seen
at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time
we want a EX lock? fortunately, this case never happens in the real world,
as far as I can see, including permission check, (get|set)_(acl|attr), and
the gfs2 code also do so.

---
  fs/ocfs2/dlmglue.c | 47 
++++++++++++++++++++++++++++++++++++++++++++---
  fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
  fs/ocfs2/ocfs2.h   |  1 +
  3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +27 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct 
 ocfs2_lock_res *res)
      init_waitqueue_head(&res->l_event);
      INIT_LIST_HEAD(&res->l_blocked_list);
      INIT_LIST_HEAD(&res->l_mask_waiters);
+    INIT_LIST_HEAD(&res->l_holders);
  }
    void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res 
 *res)
      res->l_flags = 0UL;
  }
  +inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                   struct ocfs2_holder *oh)
+{
+    INIT_LIST_HEAD(&oh->oh_list);
+    oh->oh_owner_pid =  get_pid(task_pid(current));
+
+    spin_lock(&lockres->l_lock);
+    list_add_tail(&oh->oh_list, &lockres->l_holders);
+    spin_unlock(&lockres->l_lock);
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                       struct ocfs2_holder *oh)
+{
+    spin_lock(&lockres->l_lock);
+    list_del(&oh->oh_list);
+    spin_unlock(&lockres->l_lock);
+
+    put_pid(oh->oh_owner_pid);
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct 
 ocfs2_lock_res *lockres)
+{
+    struct ocfs2_holder *oh;
+    struct pid *pid;
+
+    /* look in the list of holders for one with the current task as 

owner */

fs/ocfs2/dlmglue.c +1 -0
+    spin_lock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 790

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+    pid = task_pid(current);
+    list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+        if (oh->oh_owner_pid == pid)
+            goto out;
+    }
+    oh = NULL;
+out:
+    spin_unlock(&lockres->l_lock);
+    return oh;
+}

Since this ocfs2_holder won't be used in the caller, I suggest just return a bool value here.
Something like:
spin_lock();

Quoted 8 lines
list_for_each_entry() {
    if (oh->oh_owner_pid == pid) {
        spin_unlock();
        return 1;
    }
}

spin_unlock();
return 0;

Quoted 5 lines

Aha, you have the point. However, it is also reasonable to return the lock holder by the way.
When debugging with printk or crash, it's easy to get the pid of the holder without any further
analysis. So, I tend to keep it;-)

IC, but we can also get the ocfs2_holder from ocfs2_lock_res in case of coredump, right?
I think it is indirectly in your way, and mismatch with the function name:)

Thanks
,Joseph

Quoted 85 lines

Thanks very much for your review!
Eric

Quoted 80 lines

Thanks,
Joseph

Quoted 75 lines
fs/ocfs2/dlmglue.c +5 -1
+
  static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
                       int level)
  {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode 
 *inode,
          goto getbh;
      }
  -    if (ocfs2_mount_local(osb))
-        goto local;
+    if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+        ocfs2_mount_local(osb))
+        goto update;
        if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode 
 *inode,
      if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
  -local:
+update:
      /*
       * We only see this flag if we're being called from
       * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +17 -1
index d293a22..d65ff1e 100644
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
      __be32    lvb_os_seqno;
  };
  +struct ocfs2_holder {
+    struct list_head oh_list;
+    struct pid *oh_owner_pid;
+};
+
  /* ocfs2_inode_lock_full() 'arg_flags' flags */
  /* don't wait on recovery. */
  #define OCFS2_META_LOCK_RECOVERY    (0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
  #define OCFS2_META_LOCK_NOQUEUE        (0x02)
  /* don't block waiting for the downconvert thread, instead return 
-EAGAIN */
  #define OCFS2_LOCK_NONBLOCK        (0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH        (0x08)
    /* Locking subclasses of inode cluster lock */
  enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug 
 *dlm_debug);
    /* To set the locking protocol on module initialization */
  void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct 
 ocfs2_lock_res *lockres);
+
  #endif    /* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
        struct list_head         l_blocked_list;
      struct list_head         l_mask_waiters;
+    struct list_head     l_holders;
        unsigned long         l_flags;
      char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
author
#5 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Quoted 137 lines · Joseph Qi
Quoted 134 lines · Eric Ren
Quoted 132 lines · Joseph Qi

Hi Eric,

Quoted 106 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

     .permission     = ocfs2_permission,
     .get_acl        = ocfs2_iop_get_acl,
     .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission

    ocfs2_permission
     ocfs2_inode_lock() <=== first time
      generic_permission
       get_acl
        ocfs2_iop_get_acl
    ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started
on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full: OCFS2_META_LOCK_GETBH; it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines can call into each other.

The performance penalty of processing the holder list should only be seen at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world, as far as I can see, including permission check, (get|set)_(acl|attr), and the gfs2 code also do so.

---
  fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
  fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
  fs/ocfs2/ocfs2.h   |  1 +
  3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +28 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
      init_waitqueue_head(&res->l_event);
      INIT_LIST_HEAD(&res->l_blocked_list);
      INIT_LIST_HEAD(&res->l_mask_waiters);
+    INIT_LIST_HEAD(&res->l_holders);
  }
    void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
      res->l_flags = 0UL;
  }
  +inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                   struct ocfs2_holder *oh)
+{
+    INIT_LIST_HEAD(&oh->oh_list);
+    oh->oh_owner_pid =  get_pid(task_pid(current));
+
+    spin_lock(&lockres->l_lock);
+    list_add_tail(&oh->oh_list, &lockres->l_holders);
+    spin_unlock(&lockres->l_lock);
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                       struct ocfs2_holder *oh)
+{
+    spin_lock(&lockres->l_lock);
+    list_del(&oh->oh_list);
+    spin_unlock(&lockres->l_lock);
+
+    put_pid(oh->oh_owner_pid);
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)
+{
+    struct ocfs2_holder *oh;
+    struct pid *pid;
+
+    /* look in the list of holders for one with the current task as owner */
+    spin_lock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 789

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+    pid = task_pid(current);
+    list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+        if (oh->oh_owner_pid == pid)
+            goto out;
+    }
+    oh = NULL;
+out:
+    spin_unlock(&lockres->l_lock);
+    return oh;
+}

Since this ocfs2_holder won't be used in the caller, I suggest just return a bool value here.
Something like:
spin_lock();

list_for_each_entry() {
    if (oh->oh_owner_pid == pid) {
        spin_unlock();
        return 1;
    }
}

spin_unlock();
return 0;

Quoted 6 lines
Quoted 5 lines

Aha, you have the point. However, it is also reasonable to return the lock holder by the way.
When debugging with printk or crash, it's easy to get the pid of the holder without any further
analysis. So, I tend to keep it;-)

IC, but we can also get the ocfs2_holder from ocfs2_lock_res in case of coredump, right?

You are right, but it's not straightforward that you will need the list commands of crash to do that.
And what if using printk?

Quoted 1 line

I think it is indirectly in your way, and mismatch with the function name:)

Yes, it's not good.

Thanks,
Eric

Quoted 85 lines

Thanks
,Joseph

Quoted 80 lines

Thanks very much for your review!
Eric

Quoted 75 lines

Thanks,
Joseph

Quoted 70 lines
fs/ocfs2/dlmglue.c +5 -1
+
  static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
                       int level)
  {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
          goto getbh;
      }
  -    if (ocfs2_mount_local(osb))
-        goto local;
+    if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+        ocfs2_mount_local(osb))
+        goto update;
        if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
      if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
  -local:
+update:
      /*
       * We only see this flag if we're being called from
       * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +17 -0
index d293a22..d65ff1e 100644
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
      __be32    lvb_os_seqno;
  };
  +struct ocfs2_holder {
+    struct list_head oh_list;
+    struct pid *oh_owner_pid;
+};
+
  /* ocfs2_inode_lock_full() 'arg_flags' flags */
  /* don't wait on recovery. */
  #define OCFS2_META_LOCK_RECOVERY    (0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
  #define OCFS2_META_LOCK_NOQUEUE        (0x02)
  /* don't block waiting for the downconvert thread, instead return -EAGAIN */
  #define OCFS2_LOCK_NONBLOCK        (0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH        (0x08)
    /* Locking subclasses of inode cluster lock */
  enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
    /* To set the locking protocol on module initialization */
  void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
+
  #endif    /* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
        struct list_head         l_blocked_list;
      struct list_head         l_mask_waiters;
+    struct list_head     l_holders;
        unsigned long         l_flags;
      char l_name[OCFS2_LOCK_ID_MAX_LEN];
#6 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

In reply to Eric Ren
Quoted 77 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

    .permission     = ocfs2_permission,
    .get_acl        = ocfs2_iop_get_acl,
    .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission
ocfs2_permission

    ocfs2_inode_lock() <=== first time
     generic_permission
      get_acl
       ocfs2_iop_get_acl
	ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started
on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full: OCFS2_META_LOCK_GETBH; it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines can call into each other.

The performance penalty of processing the holder list should only be seen at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world, as far as I can see, including permission check, (get|set)_(acl|attr), and the gfs2 code also do so.

---
 fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
 fs/ocfs2/ocfs2.h   |  1 +
 3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +1 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
 	init_waitqueue_head(&res->l_event);
 	INIT_LIST_HEAD(&res->l_blocked_list);
 	INIT_LIST_HEAD(&res->l_mask_waiters);
+	INIT_LIST_HEAD(&res->l_holders);
 }
 
 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 757

Quoted 8 lines
fs/ocfs2/dlmglue.c +5 -0
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
 	res->l_flags = 0UL;
 }
 
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+				   struct ocfs2_holder *oh)
+{
+	INIT_LIST_HEAD(&oh->oh_list);
+	oh->oh_owner_pid =  get_pid(task_pid(current));

struct pid(oh->oh_owner_pid) looks complicated here, why not use task_struct(current) or pid_t(current->pid) directly? Also i didn't see the ref count needs to be considered.

Quoted 4 lines
fs/ocfs2/dlmglue.c +4 -0
+
+	spin_lock(&lockres->l_lock);
+	list_add_tail(&oh->oh_list, &lockres->l_holders);
+	spin_unlock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 771

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+				       struct ocfs2_holder *oh)
+{
+	spin_lock(&lockres->l_lock);
+	list_del(&oh->oh_list);
+	spin_unlock(&lockres->l_lock);
+
+	put_pid(oh->oh_owner_pid);

same the above

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 774

Quoted 3 lines
fs/ocfs2/dlmglue.c +3 -0
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)

Agree with Joseph, return bool looks better. I didn't see how that help debug since the return value is not used.

Quoted 46 lines
fs/ocfs2/dlmglue.c +21 -3
+{
+	struct ocfs2_holder *oh;
+	struct pid *pid;
+
+	/* look in the list of holders for one with the current task as owner */
+	spin_lock(&lockres->l_lock);
+	pid = task_pid(current);
+	list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+		if (oh->oh_owner_pid == pid)
+			goto out;
+	}
+	oh = NULL;
+out:
+	spin_unlock(&lockres->l_lock);
+	return oh;
+}
+
 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
 				     int level)
 {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
 		goto getbh;
 	}
 
-	if (ocfs2_mount_local(osb))
-		goto local;
+	if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+	    ocfs2_mount_local(osb))
+		goto update;
 
 	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
 		ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
 	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
 		ocfs2_wait_for_recovery(osb);
 
-local:
+update:
 	/*
 	 * We only see this flag if we're being called from
 	 * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +0 -0
index d293a22..d65ff1e 100644

fs/ocfs2/dlmglue.h struct ocfs2_orphan_scan_lvb { · line 73

Quoted 4 lines
fs/ocfs2/dlmglue.h +1 -0
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
 	__be32	lvb_os_seqno;
 };
 
+struct ocfs2_holder {

will ocfs2_lock_holder more clearly? The same to the function name.

Thanks,
Junxiao.

Quoted 45 lines
fs/ocfs2/dlmglue.h +17 -0
+	struct list_head oh_list;
+	struct pid *oh_owner_pid;
+};
+
 /* ocfs2_inode_lock_full() 'arg_flags' flags */
 /* don't wait on recovery. */
 #define OCFS2_META_LOCK_RECOVERY	(0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
 #define OCFS2_META_LOCK_NOQUEUE		(0x02)
 /* don't block waiting for the downconvert thread, instead return -EAGAIN */
 #define OCFS2_LOCK_NONBLOCK		(0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH		(0x08)
 
 /* Locking subclasses of inode cluster lock */
 enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
 
 /* To set the locking protocol on module initialization */
 void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
+
 #endif	/* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
 
 	struct list_head         l_blocked_list;
 	struct list_head         l_mask_waiters;
+	struct list_head	 l_holders;
 
 	unsigned long		 l_flags;
 	char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
author
#7 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Hi Junxiao!

Quoted 81 lines · Junxiao Bi
Quoted 80 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

     .permission     = ocfs2_permission,
     .get_acl        = ocfs2_iop_get_acl,
     .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission

    ocfs2_permission
     ocfs2_inode_lock() <=== first time
      generic_permission
       get_acl
        ocfs2_iop_get_acl
	ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started
on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full: OCFS2_META_LOCK_GETBH; it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines can call into each other.

The performance penalty of processing the holder list should only be seen at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world, as far as I can see, including permission check, (get|set)_(acl|attr), and the gfs2 code also do so.

---
  fs/ocfs2/dlmglue.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
  fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
  fs/ocfs2/ocfs2.h   |  1 +
  3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +1 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
  	init_waitqueue_head(&res->l_event);
  	INIT_LIST_HEAD(&res->l_blocked_list);
  	INIT_LIST_HEAD(&res->l_mask_waiters);
+	INIT_LIST_HEAD(&res->l_holders);
  }
 
  void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
  	res->l_flags = 0UL;
  }
 
Quoted 8 lines

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 757

Quoted 5 lines
fs/ocfs2/dlmglue.c +5 -0
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+				   struct ocfs2_holder *oh)
+{
+	INIT_LIST_HEAD(&oh->oh_list);
+	oh->oh_owner_pid =  get_pid(task_pid(current));

struct pid(oh->oh_owner_pid) looks complicated here, why not use task_struct(current) or pid_t(current->pid) directly? Also i didn't see the ref count needs to be considered.

This is learned from gfs2 code, which is tested by practice. So, I think it's not bad to keep it;-)

Quoted 73 lines
Quoted 5 lines
fs/ocfs2/dlmglue.c +4 -0
+
+	spin_lock(&lockres->l_lock);
+	list_add_tail(&oh->oh_list, &lockres->l_holders);
+	spin_unlock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 771

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+				       struct ocfs2_holder *oh)
+{
+	spin_lock(&lockres->l_lock);
+	list_del(&oh->oh_list);
+	spin_unlock(&lockres->l_lock);
+
+	put_pid(oh->oh_owner_pid);

same the above

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 774

Quoted 3 lines
fs/ocfs2/dlmglue.c +3 -0
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres)

Agree with Joseph, return bool looks better. I didn't see how that help debug since the return value is not used.

Quoted 49 lines
fs/ocfs2/dlmglue.c +21 -3
+{
+	struct ocfs2_holder *oh;
+	struct pid *pid;
+
+	/* look in the list of holders for one with the current task as owner */
+	spin_lock(&lockres->l_lock);
+	pid = task_pid(current);
+	list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+		if (oh->oh_owner_pid == pid)
+			goto out;
+	}
+	oh = NULL;
+out:
+	spin_unlock(&lockres->l_lock);
+	return oh;
+}
+
  static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
  				     int level)
  {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
  		goto getbh;
  	}
 
-	if (ocfs2_mount_local(osb))
-		goto local;
+	if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+	    ocfs2_mount_local(osb))
+		goto update;
 
  	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  		ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode *inode,
  	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  		ocfs2_wait_for_recovery(osb);
 
-local:
+update:
  	/*
  	 * We only see this flag if we're being called from
  	 * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +0 -0
index d293a22..d65ff1e 100644
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
  	__be32	lvb_os_seqno;
  };
 
Quoted 2 lines

fs/ocfs2/dlmglue.h struct ocfs2_orphan_scan_lvb { · line 73

Quoted 1 line
fs/ocfs2/dlmglue.h +1 -0
+struct ocfs2_holder {

will ocfs2_lock_holder more clearly? The same to the function name.

OK, good suggestion. Hrm, but in order to align with "ocfs2_inc_holders", I think it's good to keep those function names as it is;-)

Thanks for your review!
Eric

Quoted 50 lines

Thanks,
Junxiao.

Quoted 45 lines
fs/ocfs2/dlmglue.h +17 -0
+	struct list_head oh_list;
+	struct pid *oh_owner_pid;
+};
+
  /* ocfs2_inode_lock_full() 'arg_flags' flags */
  /* don't wait on recovery. */
  #define OCFS2_META_LOCK_RECOVERY	(0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
  #define OCFS2_META_LOCK_NOQUEUE		(0x02)
  /* don't block waiting for the downconvert thread, instead return -EAGAIN */
  #define OCFS2_LOCK_NONBLOCK		(0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH		(0x08)
 
  /* Locking subclasses of inode cluster lock */
  enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
 
  /* To set the locking protocol on module initialization */
  void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+			     struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
+
  #endif	/* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
 
  	struct list_head         l_blocked_list;
  	struct list_head         l_mask_waiters;
+	struct list_head	 l_holders;
 
  	unsigned long		 l_flags;
  	char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
#8 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Quoted 181 lines · Eric Ren

Hi Junxiao!

Quoted 90 lines · Junxiao Bi
Quoted 89 lines · Eric Ren

We are in the situation that we have to avoid recursive cluster locking, but there is no way to check if a cluster lock has been taken by a precess already.

Mostly, we can avoid recursive locking by writing code carefully. However, we found that it's very hard to handle the routines that are invoked directly by vfs code. For instance:

const struct inode_operations ocfs2_file_iops = {

     .permission     = ocfs2_permission,
     .get_acl        = ocfs2_iop_get_acl,
     .set_acl        = ocfs2_iop_set_acl,

};

Both ocfs2_permission() and ocfs2_iop_get_acl() call ocfs2_inode_lock(PR):
do_sys_open
may_open
inode_permission

    ocfs2_permission
     ocfs2_inode_lock() <=== first time
      generic_permission
       get_acl
        ocfs2_iop_get_acl
    ocfs2_inode_lock() <=== recursive one

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock()
because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked - we block ourselves in the code path.

The idea to fix this issue is mostly taken from gfs2 code.
1. introduce a new field: struct ocfs2_lock_res.l_holders, to keep track of the processes' pid who has taken the cluster lock of this lock resource;
2. introduce a new flag for ocfs2_inode_lock_full:
OCFS2_META_LOCK_GETBH;
it means just getting back disk inode bh for us if we've got cluster lock.
3. export a helper: ocfs2_is_locked_by_me() is used to check if we have got the cluster lock in the upper code path.

The tracking logic should be used by some of the ocfs2 vfs's callbacks, to solve the recursive locking issue cuased by the fact that vfs routines
can call into each other.

The performance penalty of processing the holder list should only be seen
at a few cases where the tracking logic is used, such as get/set acl.

You may ask what if the first time we got a PR lock, and the second time we want a EX lock? fortunately, this case never happens in the real world,
as far as I can see, including permission check, (get|set)_(acl|attr), and
the gfs2 code also do so.

---
  fs/ocfs2/dlmglue.c | 47
++++++++++++++++++++++++++++++++++++++++++++---
  fs/ocfs2/dlmglue.h | 18 ++++++++++++++++++
  fs/ocfs2/ocfs2.h   |  1 +
  3 files changed, 63 insertions(+), 3 deletions(-)
fs/ocfs2/dlmglue.c +1 -0
index 83d576f..500bda4 100644
@@ -532,6 +532,7 @@ void ocfs2_lock_res_init_once(struct
 ocfs2_lock_res *res)
      init_waitqueue_head(&res->l_event);
      INIT_LIST_HEAD(&res->l_blocked_list);
      INIT_LIST_HEAD(&res->l_mask_waiters);
+    INIT_LIST_HEAD(&res->l_holders);
  }
    void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
@@ -749,6 +750,45 @@ void ocfs2_lock_res_free(struct ocfs2_lock_res
 *res)
      res->l_flags = 0UL;
  }
Quoted 8 lines

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 757

Quoted 5 lines
fs/ocfs2/dlmglue.c +4 -0
  +inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                   struct ocfs2_holder *oh)
+{
+    INIT_LIST_HEAD(&oh->oh_list);
+    oh->oh_owner_pid =  get_pid(task_pid(current));

struct pid(oh->oh_owner_pid) looks complicated here, why not use task_struct(current) or pid_t(current->pid) directly? Also i didn't see the ref count needs to be considered.

This is learned from gfs2 code, which is tested by practice. So, I think it's not bad
to keep it;-)

Quoted 67 lines
Quoted 5 lines
fs/ocfs2/dlmglue.c +4 -0
+
+    spin_lock(&lockres->l_lock);
+    list_add_tail(&oh->oh_list, &lockres->l_holders);
+    spin_unlock(&lockres->l_lock);

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res) · line 771

Quoted 10 lines
fs/ocfs2/dlmglue.c +10 -0
+}
+
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                       struct ocfs2_holder *oh)
+{
+    spin_lock(&lockres->l_lock);
+    list_del(&oh->oh_list);
+    spin_unlock(&lockres->l_lock);
+
+    put_pid(oh->oh_owner_pid);

same the above

fs/ocfs2/dlmglue.c void ocfs2_lock_res_free(struct ocfs2_lock_res *res)

Quoted 4 lines
fs/ocfs2/dlmglue.c +3 -0
+}
+
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct
 ocfs2_lock_res *lockres)

Agree with Joseph, return bool looks better. I didn't see how that help debug since the return value is not used.

Quoted 42 lines
fs/ocfs2/dlmglue.c +5 -0
+{
+    struct ocfs2_holder *oh;
+    struct pid *pid;
+
+    /* look in the list of holders for one with the current task as

owner */

fs/ocfs2/dlmglue.c +16 -1
+    spin_lock(&lockres->l_lock);
+    pid = task_pid(current);
+    list_for_each_entry(oh, &lockres->l_holders, oh_list) {
+        if (oh->oh_owner_pid == pid)
+            goto out;
+    }
+    oh = NULL;
+out:
+    spin_unlock(&lockres->l_lock);
+    return oh;
+}
+
  static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
                       int level)
  {
@@ -2333,8 +2373,9 @@ int ocfs2_inode_lock_full_nested(struct inode
 *inode,
          goto getbh;
      }
  -    if (ocfs2_mount_local(osb))
-        goto local;
+    if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
+        ocfs2_mount_local(osb))
+        goto update;
        if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
@@ -2363,7 +2404,7 @@ int ocfs2_inode_lock_full_nested(struct inode
 *inode,
      if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
          ocfs2_wait_for_recovery(osb);
  -local:
+update:
      /*
       * We only see this flag if we're being called from
       * ocfs2_read_locked_inode(). It means we're locking an inode
fs/ocfs2/dlmglue.h +0 -0
Quoted 8 lines

fs/ocfs2/dlmglue.h struct ocfs2_orphan_scan_lvb { · line 72

Quoted 7 lines

index d293a22..d65ff1e 100644

fs/ocfs2/dlmglue.h +0 -0
@@ -70,6 +70,11 @@ struct ocfs2_orphan_scan_lvb {
      __be32    lvb_os_seqno;
  };
  +struct ocfs2_holder {

will ocfs2_lock_holder more clearly? The same to the function name.

Quoted 2 lines

OK, good suggestion. Hrm, but in order to align with "ocfs2_inc_holders", I think it's good to keep those function names as it is;-)

that name is also not very clear. Maybe you can make another patch to clear it.

Thanks,
Junxiao.

Quoted 54 lines

Thanks for your review!
Eric

Quoted 49 lines

Thanks,
Junxiao.

Quoted 44 lines
fs/ocfs2/dlmglue.h +17 -1
+    struct list_head oh_list;
+    struct pid *oh_owner_pid;
+};
+
  /* ocfs2_inode_lock_full() 'arg_flags' flags */
  /* don't wait on recovery. */
  #define OCFS2_META_LOCK_RECOVERY    (0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
  #define OCFS2_META_LOCK_NOQUEUE        (0x02)
  /* don't block waiting for the downconvert thread, instead return
-EAGAIN */
  #define OCFS2_LOCK_NONBLOCK        (0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH        (0x08)
    /* Locking subclasses of inode cluster lock */
  enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug
 *dlm_debug);
    /* To set the locking protocol on module initialization */
  void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct
 ocfs2_lock_res *lockres);
+
  #endif    /* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
        struct list_head         l_blocked_list;
      struct list_head         l_mask_waiters;
+    struct list_head     l_holders;
        unsigned long         l_flags;
      char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
 
author
#9 lore raw

Re: [PATCH 1/2] ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

Hi Junxiao,

Quoted 4 lines
Quoted 2 lines

OK, good suggestion. Hrm, but in order to align with "ocfs2_inc_holders", I think it's good to keep those function names as it is;-)

that name is also not very clear. Maybe you can make another patch to clear it.

Maybe, the name completeness needs to compromise with the name length at some time. One of basic rules is whether the name may confuse the reader. In this case, "ocfs2_inc_holders" in dlmglue.c sounds good to me, not ambiguous.

I want to go with it. Anyone who don't like the name can propose their patch for it;-)

Thanks,
Eric

Quoted 56 lines

Thanks,
Junxiao.

Quoted 50 lines

Thanks for your review!
Eric

Quoted 47 lines

Thanks,
Junxiao.

Quoted 44 lines
fs/ocfs2/dlmglue.h +17 -1
+    struct list_head oh_list;
+    struct pid *oh_owner_pid;
+};
+
   /* ocfs2_inode_lock_full() 'arg_flags' flags */
   /* don't wait on recovery. */
   #define OCFS2_META_LOCK_RECOVERY    (0x01)
@@ -77,6 +82,8 @@ struct ocfs2_orphan_scan_lvb {
   #define OCFS2_META_LOCK_NOQUEUE        (0x02)
   /* don't block waiting for the downconvert thread, instead return
-EAGAIN */
   #define OCFS2_LOCK_NONBLOCK        (0x04)
+/* just get back disk inode bh if we've got cluster lock. */
+#define OCFS2_META_LOCK_GETBH        (0x08)
     /* Locking subclasses of inode cluster lock */
   enum {
@@ -170,4 +177,15 @@ void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug
 *dlm_debug);
     /* To set the locking protocol on module initialization */
   void ocfs2_set_locking_protocol(void);
+
+/*
+ * Keep a list of processes who have interest in a lockres.
+ * Note: this is now only uesed for check recursive cluster lock.
+ */
+inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
+                 struct ocfs2_holder *oh);
+inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct
 ocfs2_lock_res *lockres);
+
   #endif    /* DLMGLUE_H */
fs/ocfs2/ocfs2.h +1 -0
index 7e5958b..0c39d71 100644
@@ -172,6 +172,7 @@ struct ocfs2_lock_res {
         struct list_head         l_blocked_list;
       struct list_head         l_mask_waiters;
+    struct list_head     l_holders;
         unsigned long         l_flags;
       char                     l_name[OCFS2_LOCK_ID_MAX_LEN];
 
author
#10 lore raw

PATCH 2/2 ocfs2: fix deadlocks when taking inode lock at vfs entry points

In reply to Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

     may_open
      inode_permission
       ocfs2_permission
        ocfs2_inode_lock() <=== take PR
         generic_permission
          get_acl
           ocfs2_iop_get_acl
            ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
    chmod_common
     notify_change
      ocfs2_setattr <=== take EX
       posix_acl_chmod
        get_acl
         ocfs2_iop_get_acl <=== take PR
        ocfs2_iop_set_acl <=== take EX

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

---
 fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
 fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +34 -5
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	struct buffer_head *bh = NULL;
 	int status = 0;
-
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
 	if (status < 0) {
 		if (status != -ENOENT)
 			mlog_errno(status);
 		return status;
 	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
+
 	status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-	ocfs2_inode_unlock(inode, 1);
+
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 1);
+	}
 	brelse(bh);
+
 	return status;
 }
 
@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
 	struct buffer_head *di_bh = NULL;
 	struct posix_acl *acl;
 	int ret;
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
 	osb = OCFS2_SB(inode->i_sb);
 	if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
 		return NULL;
-	ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
 	if (ret < 0) {
 		if (ret != -ENOENT)
 			mlog_errno(ret);
 		return ERR_PTR(ret);
 	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
 
 	acl = ocfs2_get_acl_nolock(inode, type, di_bh);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
 	brelse(di_bh);
+
 	return acl;
 }
 
fs/ocfs2/file.c +34 -10

88 lines not on this page · raw message

Signature · 1 line
2.6.6
#11 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi Eric,

Quoted 28 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX
Quoted 3 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

Thanks,
Joseph

Quoted 181 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +34 -5
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  {
  	struct buffer_head *bh = NULL;
  	int status = 0;
-
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
  	if (status < 0) {
  		if (status != -ENOENT)
  			mlog_errno(status);
  		return status;
  	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
+
  	status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-	ocfs2_inode_unlock(inode, 1);
+
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 1);
+	}
  	brelse(bh);
+
  	return status;
  }
 
@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
  	struct buffer_head *di_bh = NULL;
  	struct posix_acl *acl;
  	int ret;
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
  	osb = OCFS2_SB(inode->i_sb);
  	if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  		return NULL;
-	ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
  	if (ret < 0) {
  		if (ret != -ENOENT)
  			mlog_errno(ret);
  		return ERR_PTR(ret);
  	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
 
  	acl = ocfs2_get_acl_nolock(inode, type, di_bh);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
  	brelse(di_bh);
+
  	return acl;
  }
 
fs/ocfs2/file.c +34 -10
index c488965..62be75d 100644
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  	handle_t *handle = NULL;
  	struct dquot *transfer_to[MAXQUOTAS] = { };
  	int qtype;
+	int arg_flags = 0, had_lock;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
  	trace_ocfs2_setattr(inode, dentry,
  			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  		}
  	}
 
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (had_lock)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
  	if (status < 0) {
  		if (status != -ENOENT)
  			mlog_errno(status);
  		goto bail_unlock_rw;
  	}
-	inode_locked = 1;
+	if (!had_lock) {
+		ocfs2_add_holder(lockres, &oh);
+		inode_locked = 1;
+	}
 
  	if (size_change) {
  		status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
  	ocfs2_commit_trans(osb, handle);
  bail_unlock:
-	if (status) {
+	if (status && inode_locked) {
+		ocfs2_remove_holder(lockres, &oh);
  		ocfs2_inode_unlock(inode, 1);
  		inode_locked = 0;
  	}
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  		if (status < 0)
  			mlog_errno(status);
  	}
-	if (inode_locked)
+	if (inode_locked) {
+		ocfs2_remove_holder(lockres, &oh);
  		ocfs2_inode_unlock(inode, 1);
+	}
 
  	brelse(bh);
  	return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
  	int ret;
+	int has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
  	if (mask & MAY_NOT_BLOCK)
  		return -ECHILD;
 
-	ret = ocfs2_inode_lock(inode, NULL, 0);
-	if (ret) {
-		if (ret != -ENOENT)
-			mlog_errno(ret);
-		goto out;
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (!has_locked) {
+		ret = ocfs2_inode_lock(inode, NULL, 0);
+		if (ret) {
+			if (ret != -ENOENT)
+				mlog_errno(ret);
+			goto out;
+		}
+		ocfs2_add_holder(lockres, &oh);
  	}
 
  	ret = generic_permission(inode, mask);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
  out:
  	return ret;
  }
author
#12 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 32 lines · Joseph Qi

Hi Eric,

Quoted 28 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX
Quoted 4 lines
Quoted 3 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added

    ocfs2_iop_get_acl <=== still take PR, because there is no lock holder on the tracking list

Thanks for your review;-)
Eric

Quoted 174 lines

Thanks,
Joseph

Quoted 169 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, 
 int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }
  @@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +34 -7
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
#13 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 41 lines · Eric Ren
Quoted 34 lines · Joseph Qi

Hi Eric,

Quoted 30 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()")
results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX
Quoted 5 lines
Quoted 3 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

Quoted 7 lines

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no lock
holder on the tracking list

I mean we have no need to check if locked by me, just do inode lock and add holder.
This will make code more clean, IMO.

Thanks,
Joseph

Quoted 184 lines

Thanks for your review;-)
Eric

Quoted 179 lines

Thanks,
Joseph

Quoted 174 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, 
 struct posix_acl *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }

@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct
inode *inode, int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +14 -2
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
fs/ocfs2/file.c +20 -5
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
author
#14 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 45 lines · Joseph Qi
Quoted 42 lines · Eric Ren
Quoted 32 lines · Joseph Qi

Hi Eric,

Quoted 28 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX
Quoted 4 lines
Quoted 3 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

Quoted 5 lines
Quoted 3 lines

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no lock holder on the tracking
list

I mean we have no need to check if locked by me, just do inode lock and add holder. This will make code more clean, IMO.

Oh, sorry, I get your point this time. I think we need to check it if there are more than one processes that hold
PR lock on the same resource. If I don't understand you correctly, please tell me why you think it's not neccessary
to check before getting lock?

Thanks,
Eric

Quoted 184 lines

Thanks,
Joseph

Quoted 179 lines

Thanks for your review;-)
Eric

Quoted 174 lines

Thanks,
Joseph

Quoted 169 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, 
 int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }
  @@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +34 -7
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
#15 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 52 lines · Eric Ren
Quoted 51 lines · Joseph Qi
Quoted 48 lines · Eric Ren
Quoted 34 lines · Joseph Qi

Hi Eric,

Quoted 30 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()")
results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX
Quoted 5 lines
Quoted 3 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no lock
holder on the tracking list

Quoted 8 lines
Quoted 3 lines

I mean we have no need to check if locked by me, just do inode lock and add holder.
This will make code more clean, IMO.

Oh, sorry, I get your point this time. I think we need to check it if there are more than one processes that hold
PR lock on the same resource. If I don't understand you correctly, please tell me why you think it's not neccessary
to check before getting lock?

The code logic can only check if it is locked by myself. In the case described above, ocfs2_permission is the first entry to take inode lock. And even if check succeeds, it is a bug without unlock, but not the case of recursive lock.

Thanks,
Joseph

Quoted 193 lines

Thanks,
Eric

Quoted 189 lines

Thanks,
Joseph

Quoted 184 lines

Thanks for your review;-)
Eric

Quoted 179 lines

Thanks,
Joseph

Quoted 174 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, 
 struct posix_acl *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }

@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct
inode *inode, int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +14 -2
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
fs/ocfs2/file.c +20 -5
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
author
#16 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no lock holder on the
tracking list

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

I mean we have no need to check if locked by me, just do inode lock and add holder. This will make code more clean, IMO.

Oh, sorry, I get your point this time. I think we need to check it if there are more than one processes that hold
PR lock on the same resource. If I don't understand you correctly, please tell me why you think it's not neccessary
to check before getting lock?

The code logic can only check if it is locked by myself. In the case

Why only...?

Quoted 3 lines

described above, ocfs2_permission is the first entry to take inode lock. And even if check succeeds, it is a bug without unlock, but not the case of recursive lock.

By checking succeeds, you mean it's locked by me, right? If so, this flag
"arg_flags = OCFS2_META_LOCK_GETBH"
will be passed down to ocfs2_inode_lock_full(), which gets back buffer head of the disk inode for us if necessary, but doesn't take cluster locking again. So, there is no need to unlock in such case.

Thanks,
Eric

Quoted 194 lines

Thanks,
Joseph

Quoted 189 lines

Thanks,
Eric

Quoted 185 lines

Thanks,
Joseph

Quoted 180 lines

Thanks for your review;-)
Eric

Quoted 175 lines

Thanks,
Joseph

Quoted 170 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl 
 *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }

@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int
type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +34 -7
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
#17 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 30 lines · Eric Ren

Hi,

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

Quoted 7 lines

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no lock
holder on the tracking list

I mean we have no need to check if locked by me, just do inode lock and add holder.
This will make code more clean, IMO.

Quoted 6 lines
Quoted 5 lines

Oh, sorry, I get your point this time. I think we need to check it if there are more than one processes that hold
PR lock on the same resource. If I don't understand you correctly, please tell me why you think it's not neccessary
to check before getting lock?

The code logic can only check if it is locked by myself. In the case

Why only...?

Quoted 3 lines

described above, ocfs2_permission is the first entry to take inode lock. And even if check succeeds, it is a bug without unlock, but not the case of recursive lock.

Quoted 7 lines

By checking succeeds, you mean it's locked by me, right? If so, this flag
"arg_flags = OCFS2_META_LOCK_GETBH"
will be passed down to ocfs2_inode_lock_full(), which gets back buffer head of
the disk inode for us if necessary, but doesn't take cluster locking again. So, there is
no need to unlock in such case.

I am trying to state my point more clearly...
The issue case you are trying to fix is:
Process A
take inode lock (phase1)
...
<<< race window (phase2, Process B)
...
take inode lock again (phase3)

Deadlock happens because Process B in phase2 and Process A in phase3 are waiting for each other.
So you are trying to fix it by making phase3 finish without really doing __ocfs2_cluster_lock, then Process B can continue either.
Let us bear in mind that phase1 and phase3 are in the same context and executed in order. That's why I think there is no need to check if locked by myself in phase1.
If phase1 finds it is already locked by myself, that means the holder is left by last operation without dec holder. That's why I think it is a bug instead of a recursive lock case.

Thanks,
Joseph

Quoted 204 lines

Thanks,
Eric

Quoted 199 lines

Thanks,
Joseph

Quoted 194 lines

Thanks,
Eric

Quoted 190 lines

Thanks,
Joseph

Quoted 185 lines

Thanks for your review;-)
Eric

Quoted 180 lines

Thanks,
Joseph

Quoted 175 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, 
 struct posix_acl *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, 
 NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;

}
@@ -303,21 +318,35 @@ struct posix_acl
*ocfs2_iop_get_acl(struct inode *inode, int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +14 -2
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
fs/ocfs2/file.c +20 -5
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
author
#18 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi!

Quoted 29 lines · Joseph Qi
Quoted 28 lines · Eric Ren

Hi,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion()
is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no lock holder on the
tracking list

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

I mean we have no need to check if locked by me, just do inode lock and add holder. This will make code more clean, IMO.

Oh, sorry, I get your point this time. I think we need to check it if there are more than one processes that hold
PR lock on the same resource. If I don't understand you correctly, please tell me why you think it's not neccessary
to check before getting lock?

The code logic can only check if it is locked by myself. In the case

Why only...?

Quoted 3 lines

described above, ocfs2_permission is the first entry to take inode lock. And even if check succeeds, it is a bug without unlock, but not the case of recursive lock.

Quoted 6 lines
Quoted 5 lines

By checking succeeds, you mean it's locked by me, right? If so, this flag
"arg_flags = OCFS2_META_LOCK_GETBH"
will be passed down to ocfs2_inode_lock_full(), which gets back buffer head of the disk inode for us if necessary, but doesn't take cluster locking again. So, there is no need to unlock in such case.

I am trying to state my point more clearly...

Thanks a lot!

Quoted 5 lines

The issue case you are trying to fix is:
Process A
take inode lock (phase1)
...
<<< race window (phase2, Process B)

The deadlock only happens if process B is on a remote node and request EX lock.

Quote the patch[1/2]'s commit message:

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked
- we block ourselves in the code path.
---

Quoted 5 lines

...
take inode lock again (phase3)

Deadlock happens because Process B in phase2 and Process A in phase3 are waiting for each other.

It's local lock's (like i_mutex) responsibility to protect critical section from racing among processes on the same node.

Quoted 1 line

So you are trying to fix it by making phase3 finish without really doing

Phase3 can go ahead because this node is already under protection of cluster lock.

Quoted 7 lines

__ocfs2_cluster_lock, then Process B can continue either.
Let us bear in mind that phase1 and phase3 are in the same context and executed in order. That's why I think there is no need to check if locked by myself in phase1.
If phase1 finds it is already locked by myself, that means the holder is left by last operation without dec holder. That's why I think it is a bug instead of a recursive lock case.

Did I answer your question?

Thanks!
Eric

Quoted 204 lines

Thanks,
Joseph

Quoted 199 lines

Thanks,
Eric

Quoted 194 lines

Thanks,
Joseph

Quoted 189 lines

Thanks,
Eric

Quoted 185 lines

Thanks,
Joseph

Quoted 180 lines

Thanks for your review;-)
Eric

Quoted 175 lines

Thanks,
Joseph

Quoted 170 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl 
 *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }

@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode,
int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +34 -7
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
#19 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 73 lines · Eric Ren

Hi!

Quoted 42 lines · Joseph Qi
Quoted 33 lines · Eric Ren

Hi,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

Fixes them by adding the tracking logic (in the previous patch) for
these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

As described cases above, shall we just add the tracking logic only for set/get_acl()?

Quoted 7 lines

The idea is to detect recursive locking on the running task stack. Take case 1) for example if ocfs2_permisssion() is not changed:

ocfs2_permission() <=== take PR, ocfs2_holder is not added
ocfs2_iop_get_acl <=== still take PR, because there is no
lock holder on the tracking list

I mean we have no need to check if locked by me, just do inode lock and add holder.
This will make code more clean, IMO.

Quoted 6 lines
Quoted 5 lines

Oh, sorry, I get your point this time. I think we need to check it if there are more than one processes that hold
PR lock on the same resource. If I don't understand you correctly, please tell me why you think it's not neccessary to check before getting lock?

The code logic can only check if it is locked by myself. In the case

Why only...?

Quoted 5 lines

described above, ocfs2_permission is the first entry to take inode lock.
And even if check succeeds, it is a bug without unlock, but not the case
of recursive lock.

Quoted 8 lines

By checking succeeds, you mean it's locked by me, right? If so, this flag
"arg_flags = OCFS2_META_LOCK_GETBH"
will be passed down to ocfs2_inode_lock_full(), which gets back buffer head of
the disk inode for us if necessary, but doesn't take cluster locking again. So, there is
no need to unlock in such case.

Quoted 1 line

I am trying to state my point more clearly...

Thanks a lot!

Quoted 5 lines

The issue case you are trying to fix is:
Process A
take inode lock (phase1)
...
<<< race window (phase2, Process B)

The deadlock only happens if process B is on a remote node and request EX lock.

Quote the patch[1/2]'s commit message:

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the
second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked
- we block ourselves in the code path.
---

Quoted 8 lines
Quoted 5 lines

...
take inode lock again (phase3)

Deadlock happens because Process B in phase2 and Process A in phase3 are waiting for each other.

It's local lock's (like i_mutex) responsibility to protect critical section from racing
among processes on the same node.

I know we are talking a cluster lock issue. And the Process B I described is downconvert thread.

Quoted 5 lines
Quoted 2 lines

So you are trying to fix it by making phase3 finish without really doing

Phase3 can go ahead because this node is already under protection of cluster lock.

You said it was blocked...
"Another hand, the recursive cluster lock (the second one) will be blocked in
__ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED."

Quoted 227 lines
Quoted 10 lines

__ocfs2_cluster_lock, then Process B can continue either. Let us bear in mind that phase1 and phase3 are in the same context and executed in order. That's why I think there is no need to check if locked
by myself in phase1.
If phase1 finds it is already locked by myself, that means the holder is left by last operation without dec holder. That's why I think it is a bug
instead of a recursive lock case.

Did I answer your question?

Thanks!
Eric

Quoted 210 lines

Thanks,
Joseph

Quoted 205 lines

Thanks,
Eric

Quoted 200 lines

Thanks,
Joseph

Quoted 195 lines

Thanks,
Eric

Quoted 191 lines

Thanks,
Joseph

Quoted 186 lines

Thanks for your review;-)
Eric

Quoted 181 lines

Thanks,
Joseph

Quoted 176 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 
++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode 
 *inode, struct posix_acl *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, 
 NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;

}
@@ -303,21 +318,35 @@ struct posix_acl
*ocfs2_iop_get_acl(struct inode *inode, int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +14 -2
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry 
 *dentry, struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, 
 struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
fs/ocfs2/file.c +20 -5
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry 
 *dentry, struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
author
#20 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi,

Quoted 27 lines · Joseph Qi

...

Quoted 26 lines
Quoted 6 lines

The issue case you are trying to fix is:
Process A
take inode lock (phase1)
...
<<< race window (phase2, Process B)

The deadlock only happens if process B is on a remote node and request EX lock.

Quote the patch[1/2]'s commit message:

A deadlock will occur if a remote EX request comes in between two of ocfs2_inode_lock(). Briefly describe how the deadlock is formed:

On one hand, OCFS2_LOCK_BLOCKED flag of this lockres is set in BAST(ocfs2_generic_handle_bast) when downconvert is started on behalf of the remote EX lock request. Another hand, the recursive cluster lock (the second one) will be blocked in in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED. But, the downconvert never complete, why? because there is no chance for the first cluster lock on this node to be unlocked
- we block ourselves in the code path.
---

Quoted 3 lines

...
take inode lock again (phase3)

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Deadlock happens because Process B in phase2 and Process A in phase3 are waiting for each other.

It's local lock's (like i_mutex) responsibility to protect critical section from racing among processes on the same node.

I know we are talking a cluster lock issue. And the Process B I described is downconvert thread.

That's fine!

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

So you are trying to fix it by making phase3 finish without really doing

Phase3 can go ahead because this node is already under protection of cluster lock.

You said it was blocked...

Oh, sorry, I meant phase3 can go ahead if this patch set is applied;-)

Quoted 7 lines

"Another hand, the recursive cluster lock (the second one) will be blocked in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED."

Quoted 5 lines
Quoted 5 lines

__ocfs2_cluster_lock, then Process B can continue either.
Let us bear in mind that phase1 and phase3 are in the same context and executed in order. That's why I think there is no need to check if locked by myself in phase1.

Sorry, I still cannot see it. Without keeping track of the first cluster lock, how can we know if
we are under a context that has already been in the protecting of cluster lock? How can we handle
the recursive locking (the second cluster lock) if we don't have this information?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If phase1 finds it is already locked by myself, that means the holder is left by last operation without dec holder. That's why I think it is a bug instead of a recursive lock case.

I think I got your point here. Do you mean that we should just add the lock holder at the first locking position
without checking before that? Unfortunately, it's tricky here to know exactly which ocfs2 routine will be the first vfs
entry point, such as ocfs2_get_acl() which can be both the first vfs entry point and the second vfs entry point after
ocfs2_permission(), right?

It will be a coding bug if the problem you concern about happens. I think we don't need to worry about this much because
the code logic here is quite simple;-)

Thanks for your patience!
Eric

Quoted 213 lines
Quoted 211 lines

Did I answer your question?

Thanks!
Eric

Quoted 204 lines

Thanks,
Joseph

Quoted 199 lines

Thanks,
Eric

Quoted 194 lines

Thanks,
Joseph

Quoted 189 lines

Thanks,
Eric

Quoted 185 lines

Thanks,
Joseph

Quoted 180 lines

Thanks for your review;-)
Eric

Quoted 175 lines

Thanks,
Joseph

Quoted 170 lines
---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl 
 *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }

@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode,
int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +34 -7
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
author
#21 lore raw

Re: [Ocfs2-devel] [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi Joseph,

Quoted 27 lines · Eric Ren
Quoted 3 lines
Quoted 2 lines
Quoted 1 line

So you are trying to fix it by making phase3 finish without really doing

Phase3 can go ahead because this node is already under protection of cluster lock.

You said it was blocked...

Oh, sorry, I meant phase3 can go ahead if this patch set is applied;-)

Quoted 6 lines

"Another hand, the recursive cluster lock (the second one) will be blocked in __ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED."

Quoted 4 lines
Quoted 4 lines

__ocfs2_cluster_lock, then Process B can continue either.
Let us bear in mind that phase1 and phase3 are in the same context and executed in order. That's why I think there is no need to check if locked by myself in phase1.

Sorry, I still cannot see it. Without keeping track of the first cluster lock, how can we know if
we are under a context that has already been in the protecting of cluster lock? How can we handle
the recursive locking (the second cluster lock) if we don't have this information?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If phase1 finds it is already locked by myself, that means the holder is left by last operation without dec holder. That's why I think it is a bug instead of a recursive lock case.

I think I got your point here. Do you mean that we should just add the lock holder at the first locking position
without checking before that? Unfortunately, it's tricky here to know exactly which ocfs2 routine will be the first vfs
entry point, such as ocfs2_get_acl() which can be both the first vfs entry point and the second vfs entry point after
ocfs2_permission(), right?

Quoted 3 lines

It will be a coding bug if the problem you concern about happens. I think we don't need to worry about this much because
the code logic here is quite simple;-)

Ping...

Did I clear your doubts by the last email? I really want to get your point, if not.

If there's any problem, I will fix them in the next version;-)

Thanks,
Eric

Quoted 5 lines

Thanks for your patience!
Eric

Quoted 1 line
Quoted 1 line

D

#22 lore raw

Re: [Ocfs2-devel] [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 46 lines · Eric Ren

Hi Joseph,

Quoted 39 lines · Eric Ren
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

So you are trying to fix it by making phase3 finish without really doing

Phase3 can go ahead because this node is already under protection of cluster lock.

You said it was blocked...

Oh, sorry, I meant phase3 can go ahead if this patch set is applied;-)

Quoted 9 lines

"Another hand, the recursive cluster lock (the second one) will be blocked in
__ocfs2_cluster_lock() because of OCFS2_LOCK_BLOCKED."

Quoted 6 lines
Quoted 6 lines

__ocfs2_cluster_lock, then Process B can continue either. Let us bear in mind that phase1 and phase3 are in the same context and
executed in order. That's why I think there is no need to check if locked
by myself in phase1.

Sorry, I still cannot see it. Without keeping track of the first cluster lock, how can we
know if
we are under a context that has already been in the protecting of cluster lock? How can we
handle
the recursive locking (the second cluster lock) if we don't have this information?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

If phase1 finds it is already locked by myself, that means the holder is left by last operation without dec holder. That's why I think it is a bug
instead of a recursive lock case.

I think I got your point here. Do you mean that we should just add the lock holder at the
first locking position
without checking before that? Unfortunately, it's tricky here to know exactly which ocfs2
routine will be the first vfs
entry point, such as ocfs2_get_acl() which can be both the first vfs entry point and the
second vfs entry point after
ocfs2_permission(), right?

Quoted 4 lines

It will be a coding bug if the problem you concern about happens. I think we don't need to
worry about this much because
the code logic here is quite simple;-)

Quoted 6 lines

Ping...

Did I clear your doubts by the last email? I really want to get your point, if not.

If there's any problem, I will fix them in the next version;-)

Yes, but I still worry about the code bug case will be hidden behind recursive lock...
Anyway, It depends on others...

Thanks,
Joseph

Quoted 10 lines

Thanks,
Eric

Quoted 5 lines

Thanks for your patience!
Eric

Quoted 1 line
Quoted 1 line

D

#23 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

In reply to Eric Ren
url: https://github.com/0day-ci/linux/commits/Eric-Ren/fix-deadlock-caused-by-recursive-cluster-locking/20170106-200837 config: ia64-allyesconfig (attached as .config) compiler: ia64-linux-gcc (GCC)…

Hi Eric,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc2 next-20170106]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Eric-Ren/fix-deadlock-caused-by-recursive-cluster-locking/20170106-200837
config: ia64-allyesconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=ia64

All errors (new ones prefixed by >>):

In file included from fs/ocfs2/acl.c:31:0:
fs/ocfs2/acl.c: In function 'ocfs2_iop_set_acl':

>> fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available
    inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                ^~~~~~~~~~~~~~~~~~~~~
   fs/ocfs2/acl.c:292:16: note: called from here
     has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from fs/ocfs2/acl.c:31:0:
>> fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available
    inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                ^~~~~~~~~~~~~~~~~~~~~
   fs/ocfs2/acl.c:292:16: note: called from here
     has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from fs/ocfs2/acl.c:31:0:
>> fs/ocfs2/dlmglue.h:185:13: error: inlining failed in call to always_inline 'ocfs2_add_holder': function body not available
    inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
                ^~~~~~~~~~~~~~~~
   fs/ocfs2/acl.c:302:3: note: called from here
      ocfs2_add_holder(lockres, &oh);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from fs/ocfs2/acl.c:31:0:
>> fs/ocfs2/dlmglue.h:187:13: error: inlining failed in call to always_inline 'ocfs2_remove_holder': function body not available
    inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
                ^~~~~~~~~~~~~~~~~~~
   fs/ocfs2/acl.c:307:3: note: called from here
      ocfs2_remove_holder(lockres, &oh);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
   In file included from arch/ia64/include/uapi/asm/intrinsics.h:21:0,
                    from arch/ia64/include/asm/intrinsics.h:10,
                    from arch/ia64/include/asm/bitops.h:18,
                    from include/linux/bitops.h:36,
                    from include/linux/kernel.h:10,
                    from include/linux/list.h:8,
                    from include/linux/wait.h:6,
                    from include/linux/fs.h:5,
                    from fs/ocfs2/file.c:27:
   fs/ocfs2/file.c: In function 'ocfs2_file_write_iter':
   arch/ia64/include/uapi/asm/cmpxchg.h:56:2: warning: value computed is not used [-Wunused-value]
    ((__typeof__(*(ptr))) __xchg((unsigned long) (x), (ptr), sizeof(*(ptr))))
    ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   fs/ocfs2/file.c:2334:3: note: in expansion of macro 'xchg'
      xchg(&iocb->ki_complete, saved_ki_complete);
      ^~~~
   In file included from fs/ocfs2/file.c:49:0:
   fs/ocfs2/file.c: In function 'ocfs2_permission':
>> fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available
    inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                ^~~~~~~~~~~~~~~~~~~~~
   fs/ocfs2/file.c:1345:16: note: called from here
     has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from fs/ocfs2/file.c:49:0:
>> fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available
    inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                ^~~~~~~~~~~~~~~~~~~~~
   fs/ocfs2/file.c:1345:16: note: called from here
     has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from fs/ocfs2/file.c:49:0:
>> fs/ocfs2/dlmglue.h:185:13: error: inlining failed in call to always_inline 'ocfs2_add_holder': function body not available
    inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
                ^~~~~~~~~~~~~~~~
   fs/ocfs2/file.c:1353:3: note: called from here
      ocfs2_add_holder(lockres, &oh);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In file included from fs/ocfs2/file.c:49:0:
>> fs/ocfs2/dlmglue.h:187:13: error: inlining failed in call to always_inline 'ocfs2_remove_holder': function body not available
    inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
                ^~~~~~~~~~~~~~~~~~~
   fs/ocfs2/file.c:1359:3: note: called from here
      ocfs2_remove_holder(lockres, &oh);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/ocfs2_is_locked_by_me +189 fs/ocfs2/dlmglue.h

63e0c48a Joel Becker 2008-01-30  179  void ocfs2_set_locking_protocol(void);
9fb5ed3a Eric Ren    2017-01-05  180  
9fb5ed3a Eric Ren    2017-01-05  181  /*
9fb5ed3a Eric Ren    2017-01-05  182   * Keep a list of processes who have interest in a lockres.
9fb5ed3a Eric Ren    2017-01-05  183   * Note: this is now only uesed for check recursive cluster lock.
9fb5ed3a Eric Ren    2017-01-05  184   */
9fb5ed3a Eric Ren    2017-01-05 @185  inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
9fb5ed3a Eric Ren    2017-01-05  186  			     struct ocfs2_holder *oh);
9fb5ed3a Eric Ren    2017-01-05 @187  inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
9fb5ed3a Eric Ren    2017-01-05  188  			     struct ocfs2_holder *oh);
9fb5ed3a Eric Ren    2017-01-05 @189  inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
9fb5ed3a Eric Ren    2017-01-05  190  

ccd979bd Mark Fasheh 2005-12-15 191 #endif /* DLMGLUE_H */

:::::: The code at line 189 was first introduced by commit
:::::: 9fb5ed3abab2100ae8d99cee9b25fb92e3154224 ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

:::::: TO: Eric Ren <[email protected]>
:::::: CC: 0day robot <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 48810 bytes
Desc: not available
Url : http://oss.oracle.com/pipermail/ocfs2-devel/attachments/20170106/c484897a/attachment-0001.bin

author
#24 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi Fengguang,

Quoted 6 lines · kbuild test robot

Hi Eric,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc2 next-20170106]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

Quoted 8 lines

url: https://github.com/0day-ci/linux/commits/Eric-Ren/fix-deadlock-caused-by-recursive-cluster-locking/20170106-200837
config: ia64-allyesconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=ia64

I failed to reproduce this issue locally by following the above instructions, after rebasing my patch set onto the lastest
mainline(Linux 4.10-rc3), only seeing this compiler error message:
"
test:/mnt/build/linux # make.cross ARCH=ia64
make CROSS_COMPILE=/opt/gcc-4.9.0-nolibc/ia64-linux/bin/ia64-linux- --jobs=4 ARCH=ia64
...
CALL scripts/checksyscalls.sh
<stdin>:1184:2: warning: #warning syscall perf_event_open not implemented [-Wcpp] <stdin>:1238:2: warning: #warning syscall seccomp not implemented [-Wcpp] <stdin>:1316:2: warning: #warning syscall pkey_mprotect not implemented [-Wcpp] <stdin>:1319:2: warning: #warning syscall pkey_alloc not implemented [-Wcpp] <stdin>:1322:2: warning: #warning syscall pkey_free not implemented [-Wcpp]
...
AS arch/ia64/kernel/gate.o
arch/ia64/kernel/entry.S: Assembler messages:
arch/ia64/kernel/entry.S:622: Error: Operand 2 of adds' should be a 14-bit integer (-8192-8191) arch/ia64/kernel/entry.S:729: Error: Operand 2 of adds' should be a 14-bit integer (-8192-8191) arch/ia64/kernel/entry.S:860: Error: Operand 2 of `adds' should be a 14-bit integer (-8192-8191) make[1]: *** [scripts/Makefile.build:393: arch/ia64/kernel/entry.o] Error 1 make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:988: arch/ia64/kernel] Error 2
make: *** Waiting for unfinished jobs....
"

The obvious difference I noticed is my gcc version is little newer than kbuild, not sure if it's related:
"
test:/mnt/build/linux # gcc -v
gcc version 6.2.1 20160830 [gcc-6-branch revision 239856] (SUSE Linux)
"

Quoted 7 lines

All errors (new ones prefixed by >>):

In file included from fs/ocfs2/acl.c:31:0:
fs/ocfs2/acl.c: In function 'ocfs2_iop_set_acl':

Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available

     inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);

This error is probably because I should not add "inline" at the declaration while putting the function body into source file.
But, no error or warning occurred when I built and tested locally this way:
"
test:/mnt/build/linux/fs/ocfs2 # make -C /lib/modules/4.9.0-2-vanilla/build M=pwd modules "

Anyway, I wanna make kbuild silent before resending again;-) Please correct me if I'm missing something?

Thanks!
Eric

Quoted 97 lines
                                 ^~~~~~~~~~~~~~~~~~~~~
    fs/ocfs2/acl.c:292:16: note: called from here
      has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from fs/ocfs2/acl.c:31:0:
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available

     inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                 ^~~~~~~~~~~~~~~~~~~~~
    fs/ocfs2/acl.c:292:16: note: called from here
      has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from fs/ocfs2/acl.c:31:0:
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:185:13: error: inlining failed in call to always_inline 'ocfs2_add_holder': function body not available

     inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
                 ^~~~~~~~~~~~~~~~
    fs/ocfs2/acl.c:302:3: note: called from here
       ocfs2_add_holder(lockres, &oh);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from fs/ocfs2/acl.c:31:0:
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:187:13: error: inlining failed in call to always_inline 'ocfs2_remove_holder': function body not available

     inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
                 ^~~~~~~~~~~~~~~~~~~
    fs/ocfs2/acl.c:307:3: note: called from here
       ocfs2_remove_holder(lockres, &oh);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
    In file included from arch/ia64/include/uapi/asm/intrinsics.h:21:0,
                     from arch/ia64/include/asm/intrinsics.h:10,
                     from arch/ia64/include/asm/bitops.h:18,
                     from include/linux/bitops.h:36,
                     from include/linux/kernel.h:10,
                     from include/linux/list.h:8,
                     from include/linux/wait.h:6,
                     from include/linux/fs.h:5,
                     from fs/ocfs2/file.c:27:
    fs/ocfs2/file.c: In function 'ocfs2_file_write_iter':
    arch/ia64/include/uapi/asm/cmpxchg.h:56:2: warning: value computed is not used [-Wunused-value]
     ((__typeof__(*(ptr))) __xchg((unsigned long) (x), (ptr), sizeof(*(ptr))))
     ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    fs/ocfs2/file.c:2334:3: note: in expansion of macro 'xchg'
       xchg(&iocb->ki_complete, saved_ki_complete);
       ^~~~
    In file included from fs/ocfs2/file.c:49:0:
    fs/ocfs2/file.c: In function 'ocfs2_permission':
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available

     inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                 ^~~~~~~~~~~~~~~~~~~~~
    fs/ocfs2/file.c:1345:16: note: called from here
      has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from fs/ocfs2/file.c:49:0:
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:189:29: error: inlining failed in call to always_inline 'ocfs2_is_locked_by_me': function body not available

     inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
                                 ^~~~~~~~~~~~~~~~~~~~~
    fs/ocfs2/file.c:1345:16: note: called from here
      has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from fs/ocfs2/file.c:49:0:
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:185:13: error: inlining failed in call to always_inline 'ocfs2_add_holder': function body not available

     inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
                 ^~~~~~~~~~~~~~~~
    fs/ocfs2/file.c:1353:3: note: called from here
       ocfs2_add_holder(lockres, &oh);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from fs/ocfs2/file.c:49:0:
Quoted 1 line
Quoted 1 line

fs/ocfs2/dlmglue.h:187:13: error: inlining failed in call to always_inline 'ocfs2_remove_holder': function body not available

     inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
                 ^~~~~~~~~~~~~~~~~~~
    fs/ocfs2/file.c:1359:3: note: called from here
       ocfs2_remove_holder(lockres, &oh);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/ocfs2_is_locked_by_me +189 fs/ocfs2/dlmglue.h

63e0c48a Joel Becker 2008-01-30  179  void ocfs2_set_locking_protocol(void);
9fb5ed3a Eric Ren    2017-01-05  180
9fb5ed3a Eric Ren    2017-01-05  181  /*
9fb5ed3a Eric Ren    2017-01-05  182   * Keep a list of processes who have interest in a lockres.
9fb5ed3a Eric Ren    2017-01-05  183   * Note: this is now only uesed for check recursive cluster lock.
9fb5ed3a Eric Ren    2017-01-05  184   */
9fb5ed3a Eric Ren    2017-01-05 @185  inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
9fb5ed3a Eric Ren    2017-01-05  186  			     struct ocfs2_holder *oh);
9fb5ed3a Eric Ren    2017-01-05 @187  inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
9fb5ed3a Eric Ren    2017-01-05  188  			     struct ocfs2_holder *oh);
9fb5ed3a Eric Ren    2017-01-05 @189  inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
9fb5ed3a Eric Ren    2017-01-05  190

ccd979bd Mark Fasheh 2005-12-15 191 #endif /* DLMGLUE_H */

:::::: The code at line 189 was first introduced by commit
:::::: 9fb5ed3abab2100ae8d99cee9b25fb92e3154224 ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

:::::: TO: Eric Ren <[email protected]>
:::::: CC: 0day robot <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#25 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

In reply to Eric Ren
url: https://github.com/0day-ci/linux/commits/Eric-Ren/fix-deadlock-caused-by-recursive-cluster-locking/20170106-200837 reproduce:

Hi Eric,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.10-rc2 next-20170106]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Eric-Ren/fix-deadlock-caused-by-recursive-cluster-locking/20170106-200837
reproduce:

        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__

sparse warnings: (new ones prefixed by >>)

include/linux/compiler.h:253:8: sparse: attribute 'no_sanitize_address': unknown attribute

>> fs/ocfs2/dlmglue.h:189:50: sparse: marked inline, but without a definition
   fs/ocfs2/dlmglue.h:185:29: sparse: marked inline, but without a definition
   fs/ocfs2/dlmglue.h:187:32: sparse: marked inline, but without a definition
>> fs/ocfs2/dlmglue.h:189:50: sparse: marked inline, but without a definition

fs/ocfs2/dlmglue.h:185:29: sparse: marked inline, but without a definition fs/ocfs2/dlmglue.h:187:32: sparse: marked inline, but without a definition
--
include/linux/compiler.h:253:8: sparse: attribute 'no_sanitize_address': unknown attribute

>> fs/ocfs2/dlmglue.h:189:50: sparse: marked inline, but without a definition
   fs/ocfs2/dlmglue.h:185:29: sparse: marked inline, but without a definition
   fs/ocfs2/dlmglue.h:187:32: sparse: marked inline, but without a definition
   fs/ocfs2/dlmglue.h:187:32: sparse: marked inline, but without a definition
>> fs/ocfs2/dlmglue.h:189:50: sparse: marked inline, but without a definition

fs/ocfs2/dlmglue.h:185:29: sparse: marked inline, but without a definition fs/ocfs2/dlmglue.h:187:32: sparse: marked inline, but without a definition

vim +189 fs/ocfs2/dlmglue.h

34d024f8 Mark Fasheh 2007-09-24  173  void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb);
ccd979bd Mark Fasheh 2005-12-15  174  
ccd979bd Mark Fasheh 2005-12-15  175  struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void);
ccd979bd Mark Fasheh 2005-12-15  176  void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug);
ccd979bd Mark Fasheh 2005-12-15  177  
63e0c48a Joel Becker 2008-01-30  178  /* To set the locking protocol on module initialization */
63e0c48a Joel Becker 2008-01-30  179  void ocfs2_set_locking_protocol(void);
9fb5ed3a Eric Ren    2017-01-05  180  
9fb5ed3a Eric Ren    2017-01-05  181  /*
9fb5ed3a Eric Ren    2017-01-05  182   * Keep a list of processes who have interest in a lockres.
9fb5ed3a Eric Ren    2017-01-05  183   * Note: this is now only uesed for check recursive cluster lock.
9fb5ed3a Eric Ren    2017-01-05  184   */
9fb5ed3a Eric Ren    2017-01-05  185  inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
9fb5ed3a Eric Ren    2017-01-05  186  			     struct ocfs2_holder *oh);
9fb5ed3a Eric Ren    2017-01-05  187  inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
9fb5ed3a Eric Ren    2017-01-05  188  			     struct ocfs2_holder *oh);

9fb5ed3a Eric Ren 2017-01-05 @189 inline struct ocfs2_holder *ocfs2_is_locked_by_me(struct ocfs2_lock_res *lockres);
9fb5ed3a Eric Ren 2017-01-05 190
ccd979bd Mark Fasheh 2005-12-15 191 #endif /* DLMGLUE_H */

:::::: The code at line 189 was first introduced by commit
:::::: 9fb5ed3abab2100ae8d99cee9b25fb92e3154224 ocfs2/dlmglue: prepare tracking logic to avoid recursive cluster lock

:::::: TO: Eric Ren <[email protected]>
:::::: CC: 0day robot <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#26 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

In reply to Eric Ren
Quoted 130 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

     may_open
      inode_permission
       ocfs2_permission
        ocfs2_inode_lock() <=== take PR
         generic_permission
          get_acl
           ocfs2_iop_get_acl
            ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
    chmod_common
     notify_change
      ocfs2_setattr <=== take EX
       posix_acl_chmod
        get_acl
         ocfs2_iop_get_acl <=== take PR
        ocfs2_iop_set_acl <=== take EX

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

---
 fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
 fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +34 -5
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	struct buffer_head *bh = NULL;
 	int status = 0;
-
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
 	if (status < 0) {
 		if (status != -ENOENT)
 			mlog_errno(status);
 		return status;
 	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
+
 	status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-	ocfs2_inode_unlock(inode, 1);
+
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 1);
+	}
 	brelse(bh);
+
 	return status;
 }
 
@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
 	struct buffer_head *di_bh = NULL;
 	struct posix_acl *acl;
 	int ret;
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
 	osb = OCFS2_SB(inode->i_sb);
 	if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
 		return NULL;
-	ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
 	if (ret < 0) {
 		if (ret != -ENOENT)
 			mlog_errno(ret);
 		return ERR_PTR(ret);
 	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
 
 	acl = ocfs2_get_acl_nolock(inode, type, di_bh);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
 	brelse(di_bh);
+
 	return acl;
 }
 
fs/ocfs2/file.c +3 -0
index c488965..62be75d 100644
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
 	handle_t *handle = NULL;
 	struct dquot *transfer_to[MAXQUOTAS] = { };
 	int qtype;
+	int arg_flags = 0, had_lock;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
 	trace_ocfs2_setattr(inode, dentry,
 			    (unsigned long long)OCFS2_I(inode)->ip_blkno,

fs/ocfs2/file.c int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) · line 1180

Quoted 6 lines
fs/ocfs2/file.c +2 -1
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
 		}
 	}
 
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);

If had_lock==true, it is a bug? I think we should BUG_ON for it, that can help us catch bug at the first time.

Quoted 46 lines
fs/ocfs2/file.c +15 -3
+	if (had_lock)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
 	if (status < 0) {
 		if (status != -ENOENT)
 			mlog_errno(status);
 		goto bail_unlock_rw;
 	}
-	inode_locked = 1;
+	if (!had_lock) {
+		ocfs2_add_holder(lockres, &oh);
+		inode_locked = 1;
+	}
 
 	if (size_change) {
 		status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
 bail_commit:
 	ocfs2_commit_trans(osb, handle);
 bail_unlock:
-	if (status) {
+	if (status && inode_locked) {
+		ocfs2_remove_holder(lockres, &oh);
 		ocfs2_inode_unlock(inode, 1);
 		inode_locked = 0;
 	}
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
 		if (status < 0)
 			mlog_errno(status);
 	}
-	if (inode_locked)
+	if (inode_locked) {
+		ocfs2_remove_holder(lockres, &oh);
 		ocfs2_inode_unlock(inode, 1);
+	}
 
 	brelse(bh);
 	return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
 int ocfs2_permission(struct inode *inode, int mask)
 {
 	int ret;
+	int has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 

fs/ocfs2/file.c int ocfs2_getattr(struct vfsmount *mnt, · line 1345

Quoted 10 lines
fs/ocfs2/file.c +2 -5
 	if (mask & MAY_NOT_BLOCK)
 		return -ECHILD;
 
-	ret = ocfs2_inode_lock(inode, NULL, 0);
-	if (ret) {
-		if (ret != -ENOENT)
-			mlog_errno(ret);
-		goto out;
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);

The same thing as ocfs2_setattr.

Thanks,
Junxiao.

Quoted 21 lines
fs/ocfs2/file.c +12 -1
+	if (!has_locked) {
+		ret = ocfs2_inode_lock(inode, NULL, 0);
+		if (ret) {
+			if (ret != -ENOENT)
+				mlog_errno(ret);
+			goto out;
+		}
+		ocfs2_add_holder(lockres, &oh);
 	}
 
 	ret = generic_permission(inode, mask);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
 out:
 	return ret;
 }
author
#27 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi!

Quoted 134 lines · Junxiao Bi
Quoted 133 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +34 -5
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  {
  	struct buffer_head *bh = NULL;
  	int status = 0;
-
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
  	if (status < 0) {
  		if (status != -ENOENT)
  			mlog_errno(status);
  		return status;
  	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
+
  	status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-	ocfs2_inode_unlock(inode, 1);
+
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 1);
+	}
  	brelse(bh);
+
  	return status;
  }
 
@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
  	struct buffer_head *di_bh = NULL;
  	struct posix_acl *acl;
  	int ret;
+	int arg_flags = 0, has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
  	osb = OCFS2_SB(inode->i_sb);
  	if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  		return NULL;
-	ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+	if (has_locked)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
  	if (ret < 0) {
  		if (ret != -ENOENT)
  			mlog_errno(ret);
  		return ERR_PTR(ret);
  	}
+	if (!has_locked)
+		ocfs2_add_holder(lockres, &oh);
 
  	acl = ocfs2_get_acl_nolock(inode, type, di_bh);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
  	brelse(di_bh);
+
  	return acl;
  }
 
fs/ocfs2/file.c +3 -0
index c488965..62be75d 100644
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  	handle_t *handle = NULL;
  	struct dquot *transfer_to[MAXQUOTAS] = { };
  	int qtype;
+	int arg_flags = 0, had_lock;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
  	trace_ocfs2_setattr(inode, dentry,
  			    (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  		}
  	}
 
Quoted 5 lines

fs/ocfs2/file.c int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) · line 1180

Quoted 3 lines
fs/ocfs2/file.c +2 -1
-	status = ocfs2_inode_lock(inode, &bh, 1);
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);

If had_lock==true, it is a bug? I think we should BUG_ON for it, that can help us catch bug at the first time.

Good idea! But I'm not sure if "ocfs2_setattr" is always the first one who takes the cluster lock.
It's harder for me to name all the possible paths;-/

Quoted 51 lines
Quoted 51 lines
fs/ocfs2/file.c +15 -3
+	if (had_lock)
+		arg_flags = OCFS2_META_LOCK_GETBH;
+	status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
  	if (status < 0) {
  		if (status != -ENOENT)
  			mlog_errno(status);
  		goto bail_unlock_rw;
  	}
-	inode_locked = 1;
+	if (!had_lock) {
+		ocfs2_add_holder(lockres, &oh);
+		inode_locked = 1;
+	}
 
  	if (size_change) {
  		status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  bail_commit:
  	ocfs2_commit_trans(osb, handle);
  bail_unlock:
-	if (status) {
+	if (status && inode_locked) {
+		ocfs2_remove_holder(lockres, &oh);
  		ocfs2_inode_unlock(inode, 1);
  		inode_locked = 0;
  	}
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  		if (status < 0)
  			mlog_errno(status);
  	}
-	if (inode_locked)
+	if (inode_locked) {
+		ocfs2_remove_holder(lockres, &oh);
  		ocfs2_inode_unlock(inode, 1);
+	}
 
  	brelse(bh);
  	return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
  	int ret;
+	int has_locked;
+	struct ocfs2_holder oh;
+	struct ocfs2_lock_res *lockres;
 
  	if (mask & MAY_NOT_BLOCK)
  		return -ECHILD;
 
Quoted 8 lines

fs/ocfs2/file.c int ocfs2_getattr(struct vfsmount *mnt, · line 1345

Quoted 7 lines
fs/ocfs2/file.c +2 -5
-	ret = ocfs2_inode_lock(inode, NULL, 0);
-	if (ret) {
-		if (ret != -ENOENT)
-			mlog_errno(ret);
-		goto out;
+	lockres = &OCFS2_I(inode)->ip_inode_lockres;
+	has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);

The same thing as ocfs2_setattr.

OK. I will think over your suggestions!

Thanks,
Eric

Quoted 25 lines

Thanks,
Junxiao.

Quoted 21 lines
fs/ocfs2/file.c +12 -1
+	if (!has_locked) {
+		ret = ocfs2_inode_lock(inode, NULL, 0);
+		if (ret) {
+			if (ret != -ENOENT)
+				mlog_errno(ret);
+			goto out;
+		}
+		ocfs2_add_holder(lockres, &oh);
  	}
 
  	ret = generic_permission(inode, mask);
 
-	ocfs2_inode_unlock(inode, 0);
+	if (!has_locked) {
+		ocfs2_remove_holder(lockres, &oh);
+		ocfs2_inode_unlock(inode, 0);
+	}
  out:
  	return ret;
  }
#28 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 142 lines · Eric Ren

Hi!

Quoted 130 lines · Junxiao Bi
Quoted 129 lines · Eric Ren

Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()")
results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here
(https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html).

The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series.

So far, we have seen two different code paths that have this issue.
1. do_sys_open

      may_open
       inode_permission
        ocfs2_permission
         ocfs2_inode_lock() <=== take PR
          generic_permission
           get_acl
            ocfs2_iop_get_acl
             ocfs2_inode_lock() <=== take PR
2. fchmod|fchmodat
     chmod_common
      notify_change
       ocfs2_setattr <=== take EX
        posix_acl_chmod
         get_acl
          ocfs2_iop_get_acl <=== take PR
         ocfs2_iop_set_acl <=== take EX

Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr().

---
  fs/ocfs2/acl.c  | 39 ++++++++++++++++++++++++++++++++++-----
  fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++----------
  2 files changed, 68 insertions(+), 15 deletions(-)
fs/ocfs2/acl.c +18 -3
index bed1fcb..c539890 100644
@@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode,
 struct posix_acl *acl, int type)
  {
      struct buffer_head *bh = NULL;
      int status = 0;
-
-    status = ocfs2_inode_lock(inode, &bh, 1);
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          return status;
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
+
      status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
-    ocfs2_inode_unlock(inode, 1);
+
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 1);
+    }
      brelse(bh);
+
      return status;
  }

@@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct
inode *inode, int type)

      struct buffer_head *di_bh = NULL;
      struct posix_acl *acl;
      int ret;
fs/ocfs2/acl.c +16 -1
+    int arg_flags = 0, has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        osb = OCFS2_SB(inode->i_sb);
      if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
          return NULL;
-    ret = ocfs2_inode_lock(inode, &di_bh, 0);
+
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);
+    if (has_locked)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags);
      if (ret < 0) {
          if (ret != -ENOENT)
              mlog_errno(ret);
          return ERR_PTR(ret);
      }
+    if (!has_locked)
+        ocfs2_add_holder(lockres, &oh);
        acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
      brelse(di_bh);
+
      return acl;
  }
  diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c

index c488965..62be75d 100644

fs/ocfs2/file.c +3 -0
@@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct
 iattr *attr)
      handle_t *handle = NULL;
      struct dquot *transfer_to[MAXQUOTAS] = { };
      int qtype;
+    int arg_flags = 0, had_lock;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        trace_ocfs2_setattr(inode, dentry,
                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
@@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry,
Quoted 8 lines

fs/ocfs2/file.c int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) · line 1180

Quoted 6 lines
fs/ocfs2/file.c +2 -0
 struct iattr *attr)
          }
      }
  -    status = ocfs2_inode_lock(inode, &bh, 1);
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    had_lock = (ocfs2_is_locked_by_me(lockres) != NULL);

If had_lock==true, it is a bug? I think we should BUG_ON for it, that can help us catch bug at the first time.

Quoted 3 lines

Good idea! But I'm not sure if "ocfs2_setattr" is always the first one who takes the cluster lock.
It's harder for me to name all the possible paths;-/

The BUG_ON() can help catch the path where ocfs2_setattr is not the first one.

Thanks,
Junxiao.

Quoted 87 lines
Quoted 50 lines
Quoted 50 lines
fs/ocfs2/file.c +9 -2
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
      if (status < 0) {
          if (status != -ENOENT)
              mlog_errno(status);
          goto bail_unlock_rw;
      }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
        if (size_change) {
          status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct
 iattr *attr)
  bail_commit:
      ocfs2_commit_trans(osb, handle);
  bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
          inode_locked = 0;
      }
fs/ocfs2/file.c +6 -1
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry,
 struct iattr *attr)
          if (status < 0)
              mlog_errno(status);
      }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
          ocfs2_inode_unlock(inode, 1);
+    }
        brelse(bh);
      return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
  int ocfs2_permission(struct inode *inode, int mask)
  {
      int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
        if (mask & MAY_NOT_BLOCK)
          return -ECHILD;
Quoted 8 lines

fs/ocfs2/file.c int ocfs2_getattr(struct vfsmount *mnt, · line 1345

Quoted 7 lines
fs/ocfs2/file.c +2 -4
  -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);

The same thing as ocfs2_setattr.

OK. I will think over your suggestions!

Thanks,
Eric

Quoted 23 lines

Thanks,
Junxiao.

Quoted 19 lines
fs/ocfs2/file.c +12 -0
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
      }
        ret = generic_permission(inode, mask);
  -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
  out:
      return ret;
  }
 
author
#29 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Hi Junxiao,

Quoted 7 lines · Junxiao Bi
Quoted 5 lines
Quoted 2 lines

If had_lock==true, it is a bug? I think we should BUG_ON for it, that can help us catch bug at the first time.

Good idea! But I'm not sure if "ocfs2_setattr" is always the first one who takes the cluster lock.
It's harder for me to name all the possible paths;-/

The BUG_ON() can help catch the path where ocfs2_setattr is not the first one.

Yes, I understand. But, the problem is that the vfs entries calling order is out of our control. I don't want to place an assertion where I'm not 100% sure it's absolutely right;-)

Thanks,
Eric

Quoted 87 lines

Thanks,
Junxiao.

Quoted 82 lines
Quoted 48 lines
Quoted 48 lines
fs/ocfs2/file.c +9 -2
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
       if (status < 0) {
           if (status != -ENOENT)
               mlog_errno(status);
           goto bail_unlock_rw;
       }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
         if (size_change) {
           status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct
 iattr *attr)
   bail_commit:
       ocfs2_commit_trans(osb, handle);
   bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
           ocfs2_inode_unlock(inode, 1);
           inode_locked = 0;
       }
fs/ocfs2/file.c +6 -1
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry,
 struct iattr *attr)
           if (status < 0)
               mlog_errno(status);
       }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
           ocfs2_inode_unlock(inode, 1);
+    }
         brelse(bh);
       return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
   int ocfs2_permission(struct inode *inode, int mask)
   {
       int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
         if (mask & MAY_NOT_BLOCK)
           return -ECHILD;
Quoted 8 lines

fs/ocfs2/file.c int ocfs2_getattr(struct vfsmount *mnt, · line 1345

Quoted 7 lines
fs/ocfs2/file.c +2 -4
   -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);

The same thing as ocfs2_setattr.

OK. I will think over your suggestions!

Thanks,
Eric

Quoted 21 lines

Thanks,
Junxiao.

Quoted 19 lines
fs/ocfs2/file.c +12 -0
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
       }
         ret = generic_permission(inode, mask);
   -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
   out:
       return ret;
   }
 
#30 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 8 lines · Eric Ren

Hi Junxiao,

Quoted 5 lines · Junxiao Bi
Quoted 5 lines
Quoted 2 lines

If had_lock==true, it is a bug? I think we should BUG_ON for it, that can help us catch bug at the first time.

Good idea! But I'm not sure if "ocfs2_setattr" is always the first one who takes the cluster lock.
It's harder for me to name all the possible paths;-/

Quoted 6 lines
Quoted 2 lines

The BUG_ON() can help catch the path where ocfs2_setattr is not the first one.

Yes, I understand. But, the problem is that the vfs entries calling order is out of our control.
I don't want to place an assertion where I'm not 100% sure it's absolutely right;-)

If it is not the first one, is it another recursive locking bug? In this case, if you don't like BUG_ON(), you can dump the call trace and print some warning message.

Thanks,
Junxiao.

Quoted 92 lines

Thanks,
Eric

Quoted 87 lines

Thanks,
Junxiao.

Quoted 82 lines
Quoted 48 lines
Quoted 48 lines
fs/ocfs2/file.c +9 -2
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
       if (status < 0) {
           if (status != -ENOENT)
               mlog_errno(status);
           goto bail_unlock_rw;
       }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
         if (size_change) {
           status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct
 iattr *attr)
   bail_commit:
       ocfs2_commit_trans(osb, handle);
   bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
           ocfs2_inode_unlock(inode, 1);
           inode_locked = 0;
       }
fs/ocfs2/file.c +6 -1
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry,
 struct iattr *attr)
           if (status < 0)
               mlog_errno(status);
       }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
           ocfs2_inode_unlock(inode, 1);
+    }
         brelse(bh);
       return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
   int ocfs2_permission(struct inode *inode, int mask)
   {
       int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
         if (mask & MAY_NOT_BLOCK)
           return -ECHILD;
Quoted 8 lines

fs/ocfs2/file.c int ocfs2_getattr(struct vfsmount *mnt, · line 1345

Quoted 7 lines
fs/ocfs2/file.c +2 -4
   -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);

The same thing as ocfs2_setattr.

OK. I will think over your suggestions!

Thanks,
Eric

Quoted 21 lines

Thanks,
Junxiao.

Quoted 19 lines
fs/ocfs2/file.c +12 -0
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
       }
         ret = generic_permission(inode, mask);
   -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
   out:
       return ret;
   }
 
author
#31 lore raw

Re: [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points

Quoted 11 lines · Junxiao Bi
Quoted 10 lines · Eric Ren

Hi Junxiao,

Quoted 7 lines · Junxiao Bi
Quoted 5 lines
Quoted 2 lines

If had_lock==true, it is a bug? I think we should BUG_ON for it, that can help us catch bug at the first time.

Good idea! But I'm not sure if "ocfs2_setattr" is always the first one who takes the cluster lock.
It's harder for me to name all the possible paths;-/

The BUG_ON() can help catch the path where ocfs2_setattr is not the first one.

Quoted 7 lines
Quoted 4 lines

Yes, I understand. But, the problem is that the vfs entries calling order is out of our control.
I don't want to place an assertion where I'm not 100% sure it's absolutely right;-)

If it is not the first one, is it another recursive locking bug? In this case, if you don't like BUG_ON(), you can dump the call trace and print some warning message.

Yes! I like this idea, will add it in next version, thanks!

Eric

Quoted 91 lines

Thanks,
Junxiao.

Quoted 87 lines

Thanks,
Eric

Quoted 84 lines

Thanks,
Junxiao.

Quoted 81 lines
Quoted 47 lines
Quoted 47 lines
fs/ocfs2/file.c +9 -2
+    if (had_lock)
+        arg_flags = OCFS2_META_LOCK_GETBH;
+    status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags);
        if (status < 0) {
            if (status != -ENOENT)
                mlog_errno(status);
            goto bail_unlock_rw;
        }
-    inode_locked = 1;
+    if (!had_lock) {
+        ocfs2_add_holder(lockres, &oh);
+        inode_locked = 1;
+    }
          if (size_change) {
            status = inode_newsize_ok(inode, attr->ia_size);
@@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct
 iattr *attr)
    bail_commit:
        ocfs2_commit_trans(osb, handle);
    bail_unlock:
-    if (status) {
+    if (status && inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
            ocfs2_inode_unlock(inode, 1);
            inode_locked = 0;
        }
fs/ocfs2/file.c +6 -1
@@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry,
 struct iattr *attr)
            if (status < 0)
                mlog_errno(status);
        }
-    if (inode_locked)
+    if (inode_locked) {
+        ocfs2_remove_holder(lockres, &oh);
            ocfs2_inode_unlock(inode, 1);
+    }
          brelse(bh);
        return status;
@@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt,
    int ocfs2_permission(struct inode *inode, int mask)
    {
        int ret;
+    int has_locked;
+    struct ocfs2_holder oh;
+    struct ocfs2_lock_res *lockres;
          if (mask & MAY_NOT_BLOCK)
            return -ECHILD;
Quoted 8 lines

fs/ocfs2/file.c int ocfs2_getattr(struct vfsmount *mnt, · line 1345

Quoted 7 lines
fs/ocfs2/file.c +2 -4
    -    ret = ocfs2_inode_lock(inode, NULL, 0);
-    if (ret) {
-        if (ret != -ENOENT)
-            mlog_errno(ret);
-        goto out;
+    lockres = &OCFS2_I(inode)->ip_inode_lockres;
+    has_locked = (ocfs2_is_locked_by_me(lockres) != NULL);

The same thing as ocfs2_setattr.

OK. I will think over your suggestions!

Thanks,
Eric

Quoted 21 lines

Thanks,
Junxiao.

Quoted 19 lines
fs/ocfs2/file.c +12 -0
+    if (!has_locked) {
+        ret = ocfs2_inode_lock(inode, NULL, 0);
+        if (ret) {
+            if (ret != -ENOENT)
+                mlog_errno(ret);
+            goto out;
+        }
+        ocfs2_add_holder(lockres, &oh);
        }
          ret = generic_permission(inode, mask);
    -    ocfs2_inode_unlock(inode, 0);
+    if (!has_locked) {
+        ocfs2_remove_holder(lockres, &oh);
+        ocfs2_inode_unlock(inode, 0);
+    }
    out:
        return ret;
    }