Skip to contentSkip to conversation

allowing path checking to be interrupted.

RFC PATCH New Benjamin Marzinski 1 participant 7 messages Jul 30, 2022 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

cover allowing path checking to be interrupted.

When there are a huge number of paths (> 10000) The about of time that the checkerloop can hold the vecs lock for while checking the paths can get to be large enough that it starves other vecs lock users. If path checking takes long enough, it's possible that uxlsnr threads will never run. To deal with this, this patchset makes it possible to drop the vecs lock while checking the paths, and then reacquire it and continue with the next path to check.

My choice of only checking if there are waiters every 128 paths checked and only interrupting if path checking has taken more than a second are arbitrary. I didn't want to slow down path checking in the common case where this isn't an issue, and I wanted to avoid path checking getting starved by other vecs->lock users. Having the checkerloop wait for 10000 nsec was based on my own testing with a setup using 4K multipath devies with 4 paths each. This was almost always long enough for the uevent or uxlsnr client to grab the vecs lock, but I'm not sure how dependent this is on details of the system. For instance with my setup in never took more than 20 seconds to check the paths. and usually, a looping through all the paths took well under 10 seconds, most often under 5. I would only occasionally run into situations where a uxlsnr client would time out.

Benjamin Marzinski (6):
multipathd: Use regular pthread_mutex_t for waiter_lock
multipathd: track waiters for mutex_lock
multipathd: Occasionally allow waiters to interrupt checking paths
multipathd: allow uxlsnr clients to interrupt checking paths
multipathd: fix uxlsnr timeout
multipathd: Don't check if timespec.tv_sec is zero

 libmultipath/lock.h    |  16 +++++
 libmultipath/structs.h |   1 +
 multipathd/main.c      | 144 +++++++++++++++++++++++++----------------
 multipathd/uxlsnr.c    |  23 +++++--
 multipathd/uxlsnr.h    |   1 +
 multipathd/waiter.c    |  14 ++--
 6 files changed, 132 insertions(+), 67 deletions(-)
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#1 lore raw

PATCH 1/6 multipathd: Use regular pthread_mutex_t for waiter_lock

waiter_lock doesn't need any special lock handing. Also make it static.

---
 multipathd/waiter.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
multipathd/waiter.c +7 -7
index 2a221465..52793698 100644
@@ -24,7 +24,7 @@
 #include "main.h"
 
 pthread_attr_t waiter_attr;
-struct mutex_lock waiter_lock = { .mutex = PTHREAD_MUTEX_INITIALIZER };
+static pthread_mutex_t waiter_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static struct event_thread *alloc_waiter (void)
 {
@@ -65,11 +65,11 @@ void stop_waiter_thread (struct multipath *mpp)
 		(unsigned long)mpp->waiter);
 	thread = mpp->waiter;
 	mpp->waiter = (pthread_t)0;
-	pthread_cleanup_push(cleanup_lock, &waiter_lock);
-	lock(&waiter_lock);
+	pthread_cleanup_push(cleanup_mutex, &waiter_lock);
+	pthread_mutex_lock(&waiter_lock);
 	pthread_kill(thread, SIGUSR2);
 	pthread_cancel(thread);
-	lock_cleanup_pop(&waiter_lock);
+	pthread_cleanup_pop(1);
 }
 
 /*
@@ -126,10 +126,10 @@ static int waiteventloop (struct event_thread *waiter)
 	waiter->dmt = NULL;
 
 	if (!r)	{ /* wait interrupted by signal. check for cancellation */
-		pthread_cleanup_push(cleanup_lock, &waiter_lock);
-		lock(&waiter_lock);
+		pthread_cleanup_push(cleanup_mutex, &waiter_lock);
+		pthread_mutex_lock(&waiter_lock);
 		pthread_testcancel();
-		lock_cleanup_pop(&waiter_lock);
+		pthread_cleanup_pop(1);
 		return 1; /* If we weren't cancelled, just reschedule */
 	}
 
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#2 lore raw

PATCH 2/6 multipathd: track waiters for mutex_lock

In reply to Benjamin Marzinski

use the uatomic operations to track how many threads are waiting in lock() for mutex_locks. This will be used by a later patch.

---
 libmultipath/lock.h | 16 ++++++++++++++++
 multipathd/main.c   |  2 +-
 2 files changed, 17 insertions(+), 1 deletion(-)
libmultipath/lock.h +16 -0
index d7b779e7..20ca77e6 100644
@@ -2,17 +2,28 @@
 #define _LOCK_H
 
 #include <pthread.h>
+#include <urcu/uatomic.h>
+#include <stdbool.h>
 
 typedef void (wakeup_fn)(void);
 
 struct mutex_lock {
 	pthread_mutex_t mutex;
 	wakeup_fn *wakeup;
+	int waiters; /* uatomic access only */
 };
 
+static inline void init_lock(struct mutex_lock *a)
+{
+	pthread_mutex_init(&a->mutex, NULL);
+	uatomic_set(&a->waiters, 0);
+}
+
 static inline void lock(struct mutex_lock *a)
 {
+	uatomic_inc(&a->waiters);
 	pthread_mutex_lock(&a->mutex);
+	uatomic_dec(&a->waiters);
 }
 
 static inline int trylock(struct mutex_lock *a)
@@ -30,6 +41,11 @@ static inline void __unlock(struct mutex_lock *a)
 	pthread_mutex_unlock(&a->mutex);
 }
 
+static inline bool lock_has_waiters(struct mutex_lock *a)
+{
+	return (uatomic_read(&a->waiters) > 0);
+}
+
 #define lock_cleanup_pop(a) pthread_cleanup_pop(1)
 
 void cleanup_lock (void * data);
multipathd/main.c +1 -1
index 2f2b9d4c..71079113 100644
@@ -2940,7 +2940,7 @@ init_vecs (void)
 	if (!vecs)
 		return NULL;
 
-	pthread_mutex_init(&vecs->lock.mutex, NULL);
+	init_lock(&vecs->lock);
 
 	return vecs;
 }
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#3 lore raw

PATCH 3/6 multipathd: Occasionally allow waiters to interrupt checking paths

In reply to Benjamin Marzinski

If there are a very large number of paths that all get checked at the same time, it is possible for the path checkers to starve out other vecs->lock users, like uevent processing. To avoid this, if the path checkers are taking a long time, checkerloop now occasionally unlocks and allows other vecs->lock waiters to run.

In order to make sure that path checking is always making real progress, checkerloop() only checks if it should unlock every 128 path checks. To check, it sees if there are waiters and more than a second has passed since it acquired the vecs->lock. If both of these are true, it drops the lock and calls nanosleep() to schedule.

When checkerloop() reacquires the lock, it may no longer be at the correct place in the vector. While paths can be deleted from any point of the pathvec, they can only be added at the end. This means that the next path to check must be between its previous location and the start of the vector. To help determine the correct starting place, checkerloop() assigns a check_id, starting from 1, to paths as it checks them. The paths save this id. Newly added paths start with a special id of 0. As paths are deleted, later paths, with higher ids, are shifted towards the start of the vector. checkerloop() just needs to check backwards from the array index where it was at when in dropped the lock until it finds the first path with a check_id greater than zero and smaller than the last one it gave out. This will be the last path it checked. For this to work, the check_ids must always increase as you go through the pathvec array. To guarantee this, checkloop() cannot drop the lock unless it can guarantee that no matter what happens before it regains the lock, it will have enough ids to not roll over before it hits the end of the pathvec (check_id must be less than INT_MAX).

---
 libmultipath/structs.h |  1 +
 multipathd/main.c      | 79 +++++++++++++++++++++++++++++++++---------
 2 files changed, 63 insertions(+), 17 deletions(-)
libmultipath/structs.h +1 -0
index a6a09441..47358091 100644
@@ -351,6 +351,7 @@ struct path {
 	int fast_io_fail;
 	unsigned int dev_loss;
 	int eh_deadline;
+	unsigned int check_id;
 	/* configlet pointers */
 	vector hwe;
 	struct gen_path generic_path;
multipathd/main.c +62 -17

98 lines not on this page · raw message

Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#4 lore raw

PATCH 4/6 multipathd: allow uxlsnr clients to interrupt checking paths

In reply to Benjamin Marzinski

The uxlsnr clients never block waiting on the vecs->lock. Instead they register to get woken up and call trylock() when the lock is dropped. Add code to track when they are registered to get woken up. The checkerloop now checks if there are waiting uxlsnr clients as well.

---
 multipathd/main.c   |  3 ++-
 multipathd/uxlsnr.c | 20 +++++++++++++++-----
 multipathd/uxlsnr.h |  1 +
 3 files changed, 18 insertions(+), 6 deletions(-)
multipathd/main.c +2 -1
index 73c95806..78374377 100644
@@ -2647,7 +2647,8 @@ checkerloop (void *ap)
 					num_paths += rc;
 				if (++paths_checked % 128 == 0 &&
 				    check_id < INT_MAX &&
-				    lock_has_waiters(&vecs->lock)) {
+				    (lock_has_waiters(&vecs->lock) ||
+				     waiting_clients())) {
 					get_monotonic_time(&end_time);
 					timespecsub(&end_time, &chk_start_time,
 						    &diff_time);
multipathd/uxlsnr.c +15 -5
index 645e356c..04bcd020 100644
@@ -91,6 +91,7 @@ static LIST_HEAD(clients);
 static struct pollfd *polls;
 static int notify_fd = -1;
 static int idle_fd = -1;
+static bool clients_need_lock = false;
 
 static bool _socket_client_is_root(int fd)
 {
@@ -309,15 +310,22 @@ static struct timespec *get_soonest_timeout(struct timespec *ts)
 	return ts;
 }
 
-static bool need_vecs_lock(void)
+bool waiting_clients(void)
+{
+	return clients_need_lock;
+}
+
+static void check_for_locked_work(struct client *skip)
 {
 	struct client *c;
 
 	list_for_each_entry(c, &clients, node) {
-		if (c->state == CLT_LOCKED_WORK)
-			return true;
+		if (c != skip && c->state == CLT_LOCKED_WORK) {
+			clients_need_lock = true;
+			return;
+		}
 	}
-	return false;
+	clients_need_lock = false;
 }
 
 static int parse_cmd(struct client *c)
@@ -494,6 +502,7 @@ static int client_state_machine(struct client *c, struct vectors *vecs,
 			/* don't use cleanup_lock(), lest we wakeup ourselves */
 			pthread_cleanup_push_cast(__unlock, &vecs->lock);
 			c->error = execute_handler(c, vecs);
+			check_for_locked_work(c);
 			pthread_cleanup_pop(1);
 			condlog(4, "%s: cli[%d] grabbed lock", __func__, c->fd);
 			free_keys(c->cmdvec);
@@ -661,7 +670,8 @@ void *uxsock_listen(long ux_sock, void *trigger_data)
 			polls[POLLFD_NOTIFY].events = POLLIN;
 
 		polls[POLLFD_IDLE].fd = idle_fd;
-		if (need_vecs_lock())
+		check_for_locked_work(NULL);
+		if (clients_need_lock)
 			polls[POLLFD_IDLE].events = POLLIN;
 		else
 			polls[POLLFD_IDLE].events = 0;
multipathd/uxlsnr.h +1 -0
index 60c3a2c7..3e45930b 100644
@@ -3,6 +3,7 @@
 
 #include <stdbool.h>
 
+bool waiting_clients(void);
 void uxsock_cleanup(void *arg);
 void *uxsock_listen(long ux_sock,
 		    void * trigger_data);
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#5 lore raw

PATCH 5/6 multipathd: fix uxlsnr timeout

In reply to Benjamin Marzinski

check_timeout() is called whenever it's time to handle a client, and if it detects a timeout, it will switch to the CLT_SEND state. However it may already be in the CLT_SEND state, and may have already sent the length, and possibly some data. Restarting the CLT_SEND state will cause it to restart sending the length, messing up communication with the client. If we are already sending a reply to the client, we should just finish the send. Disable timeouts in the CLT_SEND state.

---
 multipathd/uxlsnr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
multipathd/uxlsnr.c +2 -1
index 04bcd020..23cb123d 100644
@@ -405,10 +405,11 @@ static void set_client_state(struct client *c, int state)
 	case CLT_RECV:
 		reset_strbuf(&c->reply);
 		memset(c->cmd, '\0', sizeof(c->cmd));
-		c->expires = ts_zero;
 		c->error = 0;
 		/* fallthrough */
 	case CLT_SEND:
+		/* no timeout while waiting for the client or sending a reply */
+		c->expires = ts_zero;
 		/* reuse these fields for next data transfer */
 		c->len = c->cmd_len = 0;
 		break;
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#6 lore raw

PATCH 6/6 multipathd: Don't check if timespec.tv_sec is zero

In reply to Benjamin Marzinski

checking if tv_sec is 0 is a holdover from before we had get_monotonic_time(), when we used to zero out tv_sec on failure. Also, use normalize_timespec() to simplify setting the sleep time.

---
 multipathd/main.c | 62 ++++++++++++++++++++---------------------------
 1 file changed, 26 insertions(+), 36 deletions(-)
multipathd/main.c +26 -36
index 78374377..5c2a7272 100644
@@ -2589,16 +2589,11 @@ checkerloop (void *ap)
 			break;
 
 		get_monotonic_time(&start_time);
-		if (start_time.tv_sec && last_time.tv_sec) {
-			timespecsub(&start_time, &last_time, &diff_time);
-			condlog(4, "tick (%ld.%06lu secs)",
-				(long)diff_time.tv_sec, diff_time.tv_nsec / 1000);
-			last_time = start_time;
-			ticks = diff_time.tv_sec;
-		} else {
-			ticks = 1;
-			condlog(4, "tick (%d ticks)", ticks);
-		}
+		timespecsub(&start_time, &last_time, &diff_time);
+		condlog(4, "tick (%ld.%06lu secs)",
+			(long)diff_time.tv_sec, diff_time.tv_nsec / 1000);
+		last_time = start_time;
+		ticks = diff_time.tv_sec;
 #ifdef USE_SYSTEMD
 		if (use_watchdog)
 			sd_notify(0, "WATCHDOG=1");
@@ -2688,26 +2683,23 @@ unlock:
 			lock_cleanup_pop(vecs->lock);
 		}
 
-		diff_time.tv_nsec = 0;
-		if (start_time.tv_sec) {
-			get_monotonic_time(&end_time);
-			timespecsub(&end_time, &start_time, &diff_time);
-			if (num_paths) {
-				unsigned int max_checkint;
-
-				condlog(4, "checked %d path%s in %ld.%06lu secs",
-					num_paths, num_paths > 1 ? "s" : "",
-					(long)diff_time.tv_sec,
-					diff_time.tv_nsec / 1000);
-				conf = get_multipath_config();
-				max_checkint = conf->max_checkint;
-				put_multipath_config(conf);
-				if (diff_time.tv_sec > (time_t)max_checkint)
-					condlog(1, "path checkers took longer "
-						"than %ld seconds, consider "
-						"increasing max_polling_interval",
-						(long)diff_time.tv_sec);
-			}
+		get_monotonic_time(&end_time);
+		timespecsub(&end_time, &start_time, &diff_time);
+		if (num_paths) {
+			unsigned int max_checkint;
+
+			condlog(4, "checked %d path%s in %ld.%06lu secs",
+				num_paths, num_paths > 1 ? "s" : "",
+				(long)diff_time.tv_sec,
+				diff_time.tv_nsec / 1000);
+			conf = get_multipath_config();
+			max_checkint = conf->max_checkint;
+			put_multipath_config(conf);
+			if (diff_time.tv_sec > (time_t)max_checkint)
+				condlog(1, "path checkers took longer "
+					"than %ld seconds, consider "
+					"increasing max_polling_interval",
+					(long)diff_time.tv_sec);
 		}
 
 		if (foreign_tick == 0) {
@@ -2725,12 +2717,10 @@ unlock:
 		if (!strict_timing)
 			sleep(1);
 		else {
-			if (diff_time.tv_nsec) {
-				diff_time.tv_sec = 0;
-				diff_time.tv_nsec =
-				     1000UL * 1000 * 1000 - diff_time.tv_nsec;
-			} else
-				diff_time.tv_sec = 1;
+			diff_time.tv_sec = 0;
+			diff_time.tv_nsec =
+			     1000UL * 1000 * 1000 - diff_time.tv_nsec;
+			normalize_timespec(&diff_time);
 
 			condlog(3, "waiting for %ld.%06lu secs",
 				(long)diff_time.tv_sec,
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel