Skip to contentSkip to conversation

fs: dlm: remove reverse locking order

RFC PATCH New Alexander Aring 1 participant 3 messages Oct 4, 2021 Distributed lock manager (DLM)

lore
Share & tools
lore

Alexander Aring

author maintainer
#0 lore raw

cover fs: dlm: remove reverse locking order

Hi,

This patch series is another try to remove the ls_waiter_mutex and try to make some locking behaviour in case of dlm_recover_waiters_pre(). Instead of earlier using the res_mutex to as a lock for some lkb fields, this patch introduces a lkb lock, so it is less complex to keep track of all locks which are needed to held.

- Alex

Alexander Aring (2):
fs: dlm: provide some lockless functionality
fs: dlm: change dlm logic locking behaviour

 fs/dlm/debug_fs.c     |   7 +-
 fs/dlm/dlm_internal.h |   5 +-
 fs/dlm/lock.c         | 256 ++++++++++++++++++++++++++----------------
 fs/dlm/lock.h         |  10 ++
 fs/dlm/lockspace.c    |   9 +-
 fs/dlm/recover.c      |   5 +
 6 files changed, 192 insertions(+), 100 deletions(-)
Signature · 1 line
2.27.0
author maintainer
#1 lore raw

RFC PATCH 1/2 fs: dlm: provide some lockless functionality

This patch provides some lockless functionality for the central dlm locking logic. It can be used internally to call central dlm locking logic functionality if the necessary resource lock is already held.

---
 fs/dlm/lock.c | 72 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 48 insertions(+), 24 deletions(-)
fs/dlm/lock.c +48 -24
index 3d4eeab213da..c419a5b5b7c0 100644
@@ -4641,25 +4641,33 @@ static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
 	}
 }
 
-static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
+static void _receive_convert_reply_lockless(struct dlm_lkb *lkb,
+					    struct dlm_message *ms,
+					    struct dlm_rsb *r)
 {
-	struct dlm_rsb *r = lkb->lkb_resource;
 	int error;
 
-	hold_rsb(r);
-	lock_rsb(r);
-
 	error = validate_message(lkb, ms);
 	if (error)
-		goto out;
+		return;
 
 	/* stub reply can happen with waiters_mutex held */
 	error = remove_from_waiters_ms(lkb, ms);
 	if (error)
-		goto out;
+		return;
 
 	__receive_convert_reply(r, lkb, ms);
- out:
+}
+
+static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
+{
+	struct dlm_rsb *r = lkb->lkb_resource;
+
+	hold_rsb(r);
+	lock_rsb(r);
+
+	_receive_convert_reply_lockless(lkb, ms, r);
+
 	unlock_rsb(r);
 	put_rsb(r);
 }
@@ -4678,22 +4686,20 @@ static int receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
 	return 0;
 }
 
-static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
+static void _receive_unlock_reply_lockless(struct dlm_lkb *lkb,
+					   struct dlm_message *ms,
+					   struct dlm_rsb *r)
 {
-	struct dlm_rsb *r = lkb->lkb_resource;
 	int error;
 
-	hold_rsb(r);
-	lock_rsb(r);
-
 	error = validate_message(lkb, ms);
 	if (error)
-		goto out;
+		return;
 
 	/* stub reply can happen with waiters_mutex held */
 	error = remove_from_waiters_ms(lkb, ms);
 	if (error)
-		goto out;
+		return;
 
 	/* this is the value returned from do_unlock() on the master */
 
@@ -4709,7 +4715,17 @@ static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
 		log_error(r->res_ls, "receive_unlock_reply %x error %d",
 			  lkb->lkb_id, ms->m_result);
 	}
- out:
+}
+
+static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
+{
+	struct dlm_rsb *r = lkb->lkb_resource;
+
+	hold_rsb(r);
+	lock_rsb(r);
+
+	_receive_unlock_reply_lockless(lkb, ms, r);
+
 	unlock_rsb(r);
 	put_rsb(r);
 }
@@ -4728,22 +4744,20 @@ static int receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
 	return 0;
 }
 
-static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
+static void _receive_cancel_reply_lockless(struct dlm_lkb *lkb,
+					   struct dlm_message *ms,
+					   struct dlm_rsb *r)
 {
-	struct dlm_rsb *r = lkb->lkb_resource;
 	int error;
 
-	hold_rsb(r);
-	lock_rsb(r);
-
 	error = validate_message(lkb, ms);
 	if (error)
-		goto out;
+		return;
 
 	/* stub reply can happen with waiters_mutex held */
 	error = remove_from_waiters_ms(lkb, ms);
 	if (error)
-		goto out;
+		return;
 
 	/* this is the value returned from do_cancel() on the master */
 
@@ -4759,7 +4773,17 @@ static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
 		log_error(r->res_ls, "receive_cancel_reply %x error %d",
 			  lkb->lkb_id, ms->m_result);
 	}
- out:
+}
+
+static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
+{
+	struct dlm_rsb *r = lkb->lkb_resource;
+
+	hold_rsb(r);
+	lock_rsb(r);
+
+	_receive_cancel_reply_lockless(lkb, ms, r);
+
 	unlock_rsb(r);
 	put_rsb(r);
 }
Signature · 1 line
2.27.0
author maintainer
#2 lore raw

RFC PATCH 2/2 fs: dlm: change dlm logic locking behaviour

In reply to Alexander Aring

This patch will try to make some lock behaviour better inside the central dlm locking logic functionality. This behaviour is that ls_waiter_mutex and res_mutex is sometimes locked in a reverse order. The reverse order can happen between the locking functionality dlm_lock()/dlm_unlock() and recovery handling dlm_recover_waiters_pre().

The reverse order is not the only problem. DLM has a internal flag to signal a different lock order between ls_waiter_mutex and res_mutex by setting the DLM_IFL_STUB_MS flag so the ls_waiter_mutex will not get held because it is already hold in case of dlm_recover_waiters_pre().

To somehow unwind this behaviour we remove the ls_waiter_mutex and replace it with rcu handling. To lock anything else while central dlm locking logic functionality is running concurrent this patch introduces the lkb_lock and it's lock_lkb() and unlock_lkb() functionality similar like the existing rsb lock.

Being able to not holding the ls_waiter_mutex before iterate ls_waiters list allows us to never run a different lock order by using internal lockless central dlm locking logic functionality and hold the requiered locks lkb/rsb lock in each iteration before call dlm locking functionality.

As benefit we move the per lockspace ls_waiter_mutex to a per lkb lock.

---
 fs/dlm/debug_fs.c     |   7 +-
 fs/dlm/dlm_internal.h |   5 +-
 fs/dlm/lock.c         | 190 ++++++++++++++++++++++++++----------------
 fs/dlm/lock.h         |  10 +++
 fs/dlm/lockspace.c    |   9 +-
 fs/dlm/recover.c      |   5 ++
 6 files changed, 147 insertions(+), 79 deletions(-)
fs/dlm/debug_fs.c +4 -3
index 47e9d57e4cae..d51da780ea12 100644
@@ -704,12 +704,13 @@ static ssize_t waiters_read(struct file *file, char __user *userbuf,
 	struct dlm_ls *ls = file->private_data;
 	struct dlm_lkb *lkb;
 	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;
+	int idx;
 
 	mutex_lock(&debug_buf_lock);
-	mutex_lock(&ls->ls_waiters_mutex);
 	memset(debug_buf, 0, sizeof(debug_buf));
 
-	list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
+	idx = srcu_read_lock(&ls->ls_lkb_srcu);
+	list_for_each_entry_rcu(lkb, &ls->ls_waiters, lkb_wait_reply) {
 		ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
 			       lkb->lkb_id, lkb->lkb_wait_type,
 			       lkb->lkb_nodeid, lkb->lkb_resource->res_name);
@@ -717,7 +718,7 @@ static ssize_t waiters_read(struct file *file, char __user *userbuf,
 			break;
 		pos += ret;
 	}
-	mutex_unlock(&ls->ls_waiters_mutex);
+	srcu_read_unlock(&ls->ls_lkb_srcu, idx);
 
 	rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
 	mutex_unlock(&debug_buf_lock);
fs/dlm/dlm_internal.h +4 -1
index 3fb610dfea19..4ac0d513d307 100644
@@ -279,6 +279,8 @@ struct dlm_lkb {
 		void			*lkb_astparam;	/* caller's ast arg */
 		struct dlm_user_args	*lkb_ua;
 	};
+	struct mutex		lkb_mutex;
+	struct rcu_head		rcu;
 };
 
 /*
@@ -568,7 +570,8 @@ struct dlm_ls {
 	struct dlm_rsbtable	*ls_rsbtbl;
 	uint32_t		ls_rsbtbl_size;
 
-	struct mutex		ls_waiters_mutex;
+	struct srcu_struct	ls_lkb_srcu;
+	spinlock_t		ls_waiters_lock;
 	struct list_head	ls_waiters;	/* lkbs needing a reply */
 
 	struct mutex		ls_orphans_mutex;
fs/dlm/lock.c +116 -74

484 lines not on this page · raw message

fs/dlm/lock.h +10 -0
index 456c6ec3ef6f..8029aa96e47c 100644
@@ -74,5 +74,15 @@ static inline void unlock_rsb(struct dlm_rsb *r)
 	mutex_unlock(&r->res_mutex);
 }
 
+static inline void lock_lkb(struct dlm_lkb *lkb)
+{
+	mutex_lock(&lkb->lkb_mutex);
+}
+
+static inline void unlock_lkb(struct dlm_lkb *lkb)
+{
+	mutex_unlock(&lkb->lkb_mutex);
+}
+
 #endif
 
fs/dlm/lockspace.c +8 -1
index 2896f96cf2d3..e1199c41301f 100644
@@ -524,7 +524,8 @@ static int new_lockspace(const char *name, const char *cluster,
 	spin_lock_init(&ls->ls_lkbidr_spin);
 
 	INIT_LIST_HEAD(&ls->ls_waiters);
-	mutex_init(&ls->ls_waiters_mutex);
+	init_srcu_struct(&ls->ls_lkb_srcu);
+	spin_lock_init(&ls->ls_waiters_lock);
 	INIT_LIST_HEAD(&ls->ls_orphans);
 	mutex_init(&ls->ls_orphans_mutex);
 	INIT_LIST_HEAD(&ls->ls_timeout);
@@ -810,6 +811,12 @@ static int release_lockspace(struct dlm_ls *ls, int force)
 	idr_destroy(&ls->ls_recover_idr);
 	kfree(ls->ls_recover_buf);
 
+	/*
+	 * wait all readers of ls_waiters and srcu callers are left before free
+	 */
+	srcu_barrier(&ls->ls_lkb_srcu);
+	cleanup_srcu_struct(&ls->ls_lkb_srcu);
+
 	/*
 	 * Free all lkb's in idr
 	 */
fs/dlm/recover.c +5 -0
index 8928e99dfd47..ed5a248af641 100644
@@ -939,6 +939,11 @@ void dlm_clear_toss(struct dlm_ls *ls)
 	unsigned int count = 0;
 	int i;
 
+	/* _dlm_free_lkb() called by ls_lkb_srcu will clear toss as well,
+	 * so call srcu_barrier() to be sure all pending srcu are done
+	 */
+	srcu_barrier(&ls->ls_lkb_srcu);
+
 	for (i = 0; i < ls->ls_rsbtbl_size; i++) {
 		spin_lock(&ls->ls_rsbtbl[i].lock);
 		for (n = rb_first(&ls->ls_rsbtbl[i].toss); n; n = next) {
Signature · 1 line
2.27.0