Skip to contentSkip to conversation

multipath: add the ability to wait on events

PATCH In review Benjamin Marzinski 2 participants 7 messages Nov 26 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

cover multipath: add the ability to wait on events

Multipathd doesn't react immediately to multiple things. Reconfiguration doesn't happen immediately. Devices can be added and removed from the system, and multipathd doesn't react until it gets a uevent. Sometimes users need to wait for multipathd to finish handling these events before doing further work. For instance a user script that reconfigures multipath, and then verifies the new configuration can fail if the reconfigure doesn't happen fast enough. User scripts might also need to wait till a device is completely removed from multipath handling before they can continue, or wait for a multipath device to be fully added through udev, etc.

This patchset doesn't add all of these wait commands. It just adds the architecture for doing them and adds the "reconfigure wait" and "reconfigure all wait" commands, to schedule and then wait for a reconfigure. Tracking uevent removes and additions (and first change events for multipath devices) so these can be waited on is more complicated and I haven't finished with them yet. But I wanted to make sure that we agree on the overall waiting architecture.

Benjamin Marzinski (4):
multipathd: make the wakeup and drain idle_fd code generic
multipathd: add infrastructure for uxlsnr waiting on events
multipathd: make cli_handlers take a wait_node
multipathd: add commands to wait for reconfigures to complete

 multipathd/callbacks.c     |   3 +
 multipathd/cli.c           |   1 +
 multipathd/cli.h           |   7 +-
 multipathd/cli_handlers.c  | 246 ++++++++++++++++++++-----------------
 multipathd/main.c          |  22 +++-
 multipathd/main.h          |   3 +-
 multipathd/multipathd.8.in |  19 ++-
 multipathd/uxlsnr.c        | 140 ++++++++++++++++++---
 multipathd/uxlsnr.h        |   4 +
 9 files changed, 312 insertions(+), 133 deletions(-)
Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH 1/4 multipathd: make the wakeup and drain idle_fd code generic

A future patch will add a new eventfd. Make the code to wake up and drain an eventfd generic, so it can be reused.

---
 multipathd/uxlsnr.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
multipathd/uxlsnr.c +11 -7
index b8207306..e92784e5 100644
@@ -384,21 +384,25 @@ static int execute_handler(struct client *c, struct vectors *vecs)
 	return c->handler->fn(c->cmdvec, &c->reply, vecs);
 }
 
-static void wakeup_listener(void)
+static void wakeup_listener(int event_fd)
 {
 	uint64_t one = 1;
 
-	if (idle_fd != -1 &&
-	    write(idle_fd, &one, sizeof(one)) != sizeof(one))
+	if (event_fd != -1 && write(event_fd, &one, sizeof(one)) != sizeof(one))
 		condlog(1, "%s: failed", __func__);
 }
 
-static void drain_idle_fd(int fd)
+static void wakeup_idle_listener(void)
+{
+	wakeup_listener(idle_fd);
+}
+
+static void drain_event_fd(int event_fd)
 {
 	uint64_t val;
 	int rc;
 
-	rc = read(fd, &val, sizeof(val));
+	rc = read(event_fd, &val, sizeof(val));
 	condlog(4, "%s: %d, %"PRIu64, __func__, rc, val);
 }
 
@@ -665,7 +669,7 @@ void *uxsock_listen(int n_socks, long *ux_sock_in, void *trigger_data)
 		condlog(1, "failed to create idle fd");
 		exit_daemon();
 	} else
-		set_wakeup_fn(&vecs->lock, wakeup_listener);
+		set_wakeup_fn(&vecs->lock, wakeup_idle_listener);
 
 	sigfillset(&mask);
 	sigdelset(&mask, SIGINT);
@@ -769,7 +773,7 @@ void *uxsock_listen(int n_socks, long *ux_sock_in, void *trigger_data)
 
 		if (polls[POLLFD_IDLE].fd != -1 &&
 		    polls[POLLFD_IDLE].revents & POLLIN)
-			drain_idle_fd(idle_fd);
+			drain_event_fd(idle_fd);
 
 		/* see if a client needs handling */
 		list_for_each_entry_safe(c, tmp, &clients, node) {
Signature · 1 line
2.56.0
author maintainer
#2 lore raw

PATCH 2/4 multipathd: add infrastructure for uxlsnr waiting on events

In reply to Benjamin Marzinski

There are multiple things that can happen to multipathd and the devices it tracks, that multipathd only finishes handling later. User programs may need to wait for these changes to complete before continuing.

This commit add the architecture for waiting for events to occur. A future patch will add the ability to actually register these wait events. uxlnsr clients waiting for events will put themselves on a wait list with wait_for_event(). If they exit early, they will take themselves off with stop_waiting_for_event(). When the event occurs, the thread processing the event will call wakeup_event_waiter() to remove all clients from that wait list, and send a wakeup to the uslsnr thread. This works via an eventfd, using the same code as idle_fd currently does. Waiting clients check if their event has occured by checking if they are still on a wait list.

---
 multipathd/main.c   |   7 +++
 multipathd/uxlsnr.c | 120 ++++++++++++++++++++++++++++++++++++++++----
 multipathd/uxlsnr.h |   4 ++
 3 files changed, 122 insertions(+), 9 deletions(-)
multipathd/main.c +7 -0
index 9bce4d6b..1bb8eac8 100644
@@ -2009,6 +2009,12 @@ uxlsnrloop (void * ap)
 		goto out_sock;
 	}
 
+	if (init_wait_fd() < 0) {
+		condlog(1, "Failed to open wait_fd: %m");
+		exit_daemon();
+		goto out_sock;
+	}
+
 	/* Tell main thread that thread has started */
 	post_config_state(DAEMON_CONFIGURE);
 
@@ -3917,6 +3923,7 @@ static void cleanup_child(void)
 	cleanup_threads();
 	cleanup_vecs();
 	cleanup_bindings();
+	cleanup_wait_fd();
 	if (poll_dmevents)
 		cleanup_dmevent_waiter();
 
multipathd/uxlsnr.c +111 -9

226 lines not on this page · raw message

multipathd/uxlsnr.h +4 -0
index a331e23b..d33920ea 100644
@@ -6,5 +6,9 @@
 bool waiting_clients(void);
 void uxsock_cleanup(void *arg);
 void *uxsock_listen(int n_socks, long *ux_sock, void *trigger_data);
+int init_wait_fd(void);
+void cleanup_wait_fd(void);
+void wait_for_event(struct list_head *wait_node, struct list_head *wait_list);
+void wakeup_event_waiter(struct list_head *wait_list);
 
 #endif
Signature · 1 line
2.56.0
#3 lore raw

Re: [PATCH 2/4] multipathd: add infrastructure for uxlsnr waiting on events

Quoted 116 lines

There are multiple things that can happen to multipathd and the devices it tracks, that multipathd only finishes handling later. User programs may need to wait for these changes to complete before continuing.

This commit add the architecture for waiting for events to occur. A future patch will add the ability to actually register these wait events. uxlnsr clients waiting for events will put themselves on a wait list with wait_for_event(). If they exit early, they will take themselves off with stop_waiting_for_event(). When the event occurs, the thread processing the event will call wakeup_event_waiter() to remove all clients from that wait list, and send a wakeup to the uslsnr thread. This works via an eventfd, using the same code as idle_fd currently does. Waiting clients check if their event has occured by checking if they are still on a wait list.

multipathd/main.c +7 -0
index 9bce4d6..1bb8eac 100644
@@ -2009,6 +2009,12 @@ uxlsnrloop (void * ap)
 		goto out_sock;
 	}
 
+	if (init_wait_fd() < 0) {
+		condlog(1, "Failed to open wait_fd: %m");
+		exit_daemon();
+		goto out_sock;
+	}
+
 	/* Tell main thread that thread has started */
 	post_config_state(DAEMON_CONFIGURE);
 
@@ -3917,6 +3923,7 @@ static void cleanup_child(void)
 	cleanup_threads();
 	cleanup_vecs();
 	cleanup_bindings();
+	cleanup_wait_fd();
 	if (poll_dmevents)
 		cleanup_dmevent_waiter();
 
multipathd/uxlsnr.c +15 -0
index e92784e..0930691 100644
@@ -49,11 +49,13 @@ enum {
 	CLT_PARSE,
 	CLT_LOCKED_WORK,
 	CLT_WORK,
+	CLT_WAIT,
 	CLT_SEND,
 };
 
 struct client {
 	struct list_head node;
+	struct list_head wait_node; /* for clients waiting on an event */
 	struct timespec expires;
 	int state;
 	int fd;
@@ -73,9 +75,13 @@ enum {
 	POLLFD_UX2,
 	POLLFD_NOTIFY,
 	POLLFD_IDLE,
+	POLLFD_WAIT,
 	POLLFDS_BASE,
 };
 
+/* Add a new revents value to signal that a waiter wakeup event occurred */
+#define POLLWAIT 0x10000
+
 #define POLLFD_CHUNK (4096 / sizeof(struct pollfd))
 /* Minimum number of pollfds to reserve for clients */
 #define MIN_POLLS (POLLFD_CHUNK - POLLFDS_BASE)
@@ -99,6 +105,8 @@ static LIST_HEAD(clients);
 static struct pollfd *polls;
 static int notify_fd = -1;
 static int idle_fd = -1;
+static int wait_fd = -1;
+static pthread_mutex_t client_wait_lock = PTHREAD_MUTEX_INITIALIZER;
 static bool clients_need_lock = false;
 
 static bool _socket_client_is_root(int fd)
@@ -116,6 +124,8 @@ static bool _socket_client_is_root(int fd)
 	return false;
 }
 
+static void stop_waiting_for_event(struct client *c);
+
 /*
  * kill off a dead client
  */
@@ -123,6 +133,7 @@ static void dead_client(struct client *c)
 {
 	int fd = c->fd;
 	list_del_init(&c->node);
+	stop_waiting_for_event(c);
 	c->fd = -1;
 	reset_strbuf(&c->reply);
 	if (c->cmdvec)
@@ -152,6 +163,7 @@ static void new_client(int ux_sock, int non_root_clients)
 		return;
 	}
 	INIT_LIST_HEAD(&c->node);
+	INIT_LIST_HEAD(&c->wait_node);
 	c->fd = fd;
 	c->state = CLT_RECV;
 	c->is_root = _socket_client_is_root(c->fd);
@@ -384,6 +396,21 @@ static int execute_handler(struct client *c, struct vectors *vecs)
 	return c->handler->fn(c->cmdvec, &c->reply, vecs);
 }
 
+int init_wait_fd(void)
+{
+	wait_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);

multipathd/uxlsnr.c static int execute_handler(struct client *c, struct vectors *vecs) · line 411

Quoted 10 lines
multipathd/uxlsnr.c +10 -0
+
+	if (wait_fd < 0)
+		return -1;
+	return 0;
+}
+
+void cleanup_wait_fd(void)
+{
+	if (wait_fd >= 0)
+		close(wait_fd);

Nit: set wait_fd = -1 after closing.

Quoted 17 lines
multipathd/uxlsnr.c +10 -0
+}
+
 static void wakeup_listener(int event_fd)
 {
 	uint64_t one = 1;
@@ -397,6 +424,55 @@ static void wakeup_idle_listener(void)
 	wakeup_listener(idle_fd);
 }
 
+void wait_for_event(struct list_head *wait_node, struct list_head *wait_list)
+{
+	pthread_mutex_lock(&client_wait_lock);
+	list_add(wait_node, wait_list);
+	pthread_mutex_unlock(&client_wait_lock);
+}
+
+void wakeup_event_waiter(struct list_head *wait_list)

multipathd/uxlsnr.c static void wakeup_idle_listener(void) · line 444

Quoted 10 lines
multipathd/uxlsnr.c +10 -0
+{
+	bool have_waiter = false;
+
+	pthread_mutex_lock(&client_wait_lock);
+	while (list_pop(wait_list))
+		have_waiter = true;
+	pthread_mutex_unlock(&client_wait_lock);
+	if (have_waiter)
+		wakeup_listener(wait_fd);
+}

This function "wakes up" all waiters, even though the waiters may be waiting for different conditions to come true.

Quoted 5 lines
multipathd/uxlsnr.c +5 -0
+
+/* must only be called by uxlsnr_thr */
+static void stop_waiting_for_event(struct client *c)
+{
+	/*

multipathd/uxlsnr.c static void wakeup_idle_listener(void) · line 459

Quoted 10 lines
multipathd/uxlsnr.c +10 -0
+	 * As long as clients will only be added to wait lists by uxlsnr_thr
+	 * and only removed with list_del_init(), it is safe to check if
+	 * wait_node is empty without holding client_wait_lock. uxlsnr_thr
+	 * can't miss a client being added to the list, since a process is
+	 * guaranteed to see all memory changes it did. It can miss a client
+	 * being removed from the list or see a torn write from a concurrent
+	 * removal, but this will just cause it to unnecessarily grab the lock
+	 * and redo the removal.
+	 */
+	if (!list_empty(&c->wait_node)) {

Tbh, I don't like this super-smart optimization. I would prefer just taking the lock. If we find this to hurt performance, we could change the implementation to RCU.

Quoted 5 lines
multipathd/uxlsnr.c +5 -0
+		pthread_mutex_lock(&client_wait_lock);
+		list_del_init(&c->wait_node);
+		pthread_mutex_unlock(&client_wait_lock);
+	}
+}

multipathd/uxlsnr.c static void wakeup_idle_listener(void) · line 474

Quoted 10 lines
multipathd/uxlsnr.c +10 -0
+
+static bool finished_waiting(struct client *c)
+{
+	bool ret;
+
+	pthread_mutex_lock(&client_wait_lock);
+	ret = list_empty(&c->wait_node);
+	pthread_mutex_unlock(&client_wait_lock);
+	return ret;
+}

wakeup_event_waiter() will remove the wait_node from the list, regardless whether the specific condition this thread is waiting for has come true. So the client may return prematurely.

Quoted 15 lines
multipathd/uxlsnr.c +2 -2
+
 static void drain_event_fd(int event_fd)
 {
 	uint64_t val;
@@ -466,8 +542,7 @@ enum {
 	STM_BREAK,
 };
 
-static int client_state_machine(struct client *c, struct vectors *vecs,
-				short revents)
+static int client_state_machine(struct client *c, struct vectors *vecs, int revents)
 {
 	ssize_t n;
 

multipathd/uxlsnr.c static int client_state_machine(struct client *c, struct vectors *vecs, · line 631

Quoted 10 lines
multipathd/uxlsnr.c +5 -2
@@ -549,8 +624,15 @@ static int client_state_machine(struct client *c, struct vectors *vecs,
 			check_for_locked_work(c);
 			pthread_cleanup_pop(1);
 			condlog(4, "%s: cli[%d] grabbed lock", __func__, c->fd);
-			set_client_state(c, CLT_SEND);
-			/* Wait for POLLOUT */
+			/*
+			 * This list_empty() is safe to test without a lock for
+			 * the same reason as in stop_waiting_for_event().
+			 */
+			if (!c->error && !list_empty(&c->wait_node))

See above about the lock optimization.

Quoted 15 lines
multipathd/uxlsnr.c +6 -2
+				set_client_state(c, CLT_WAIT);
+			else
+				set_client_state(c, CLT_SEND);
+			/* Wait for POLLOUT or POLLWAIT */
 			return STM_BREAK;
 		} else {
 			condlog(4, "%s: cli[%d] waiting for lock", __func__, c->fd);
@@ -559,8 +641,18 @@ static int client_state_machine(struct client *c, struct vectors *vecs,
 
 	case CLT_WORK:
 		c->error = execute_handler(c, vecs);
-		set_client_state(c, CLT_SEND);
-		/* Wait for POLLOUT */
+		if (!c->error && !list_empty(&c->wait_node))
+			set_client_state(c, CLT_WAIT);

multipathd/uxlsnr.c static int client_state_machine(struct client *c, struct vectors *vecs, · line 655

Quoted 10 lines
multipathd/uxlsnr.c +10 -0
+		else
+			set_client_state(c, CLT_SEND);
+		/* Wait for POLLOUT or POLLWAIT */
+		return STM_BREAK;
+
+	case CLT_WAIT:
+		if (!(revents & POLLWAIT))
+			return STM_BREAK;
+		if (finished_waiting(c))
+			set_client_state(c, CLT_SEND);

As noted above, any call to wakeup_event_waiter() will wake up this client and make finished_waiting() return true. This is fine as long we have just a single condition to wait for, but as soon as we use this mechanism more widely, we will need a way to check whether our wait condition is actually true and keep waiting otherwise, like __wait_event() in the kernel.

Or am I overlooking something here?

Signature · 1 line
Martin Wilck <[email protected]>
author maintainer
#4 lore raw

PATCH 3/4 multipathd: make cli_handlers take a wait_node

In reply to Benjamin Marzinski

To enable client handler functions to add clients to wait lists, add c->wait_node as a cli_handler argument.

---
 multipathd/cli.h          |   4 +-
 multipathd/cli_handlers.c | 224 ++++++++++++++++++++------------------
 multipathd/uxlsnr.c       |   2 +-
 3 files changed, 120 insertions(+), 110 deletions(-)
multipathd/cli.h +3 -1
index 3e607389..6d599ca7 100644
@@ -2,6 +2,7 @@
 #define CLI_H_INCLUDED
 
 #include <stdint.h>
+#include "list.h"
 
 /*
  * CLI commands consist of 4 bytes, a verb (byte 0) and up to
@@ -133,7 +134,8 @@ struct key {
 
 struct strbuf;
 
-typedef int (cli_handler)(void *keywords, struct strbuf *reply, void *data);
+typedef int (cli_handler)(void *keywords, struct strbuf *reply,
+			  struct list_head *wait_node, void *data);
 
 struct handler {
 	uint32_t fingerprint;
multipathd/cli_handlers.c +116 -108

570 lines not on this page · raw message

multipathd/uxlsnr.c +1 -1
index 09306918..e8ce459d 100644
@@ -393,7 +393,7 @@ static int execute_handler(struct client *c, struct vectors *vecs)
 	if (!c->handler || !c->handler->fn)
 		return -EINVAL;
 
-	return c->handler->fn(c->cmdvec, &c->reply, vecs);
+	return c->handler->fn(c->cmdvec, &c->reply, &c->wait_node, vecs);
 }
 
 int init_wait_fd(void)
Signature · 1 line
2.56.0
author maintainer
#5 lore raw

PATCH 4/4 multipathd: add commands to wait for reconfigures to complete

In reply to Benjamin Marzinski

Add the "reconfigure wait" and "reconfigure all wait" multipathd commands. These work just like the non-wait options, but they do not return until there are no more scheduled reconfigures.

---
 multipathd/callbacks.c     |  3 +++
 multipathd/cli.c           |  1 +
 multipathd/cli.h           |  3 +++
 multipathd/cli_handlers.c  | 22 ++++++++++++++++++++--
 multipathd/main.c          | 15 +++++++++++++--
 multipathd/main.h          |  3 ++-
 multipathd/multipathd.8.in | 19 +++++++++++++++++--
 7 files changed, 59 insertions(+), 7 deletions(-)
multipathd/callbacks.c +3 -0
index 2027c3a6..60647519 100644
@@ -44,6 +44,9 @@ void init_handler_callbacks(void)
 	set_unlocked_handler_callback(VRB_RECONFIGURE, HANDLER(cli_reconfigure));
 	set_unlocked_handler_callback(VRB_RECONFIGURE | Q1_ALL,
 				      HANDLER(cli_reconfigure_all));
+	set_unlocked_handler_callback(VRB_RECONFIGURE | Q1_WAIT, HANDLER(cli_reconfigure_wait));
+	set_unlocked_handler_callback(VRB_RECONFIGURE | Q1_ALL | Q2_WAIT,
+				      HANDLER(cli_reconfigure_all_wait));
 	set_handler_callback(VRB_SUSPEND | Q1_MAP, HANDLER(cli_suspend));
 	set_handler_callback(VRB_RESUME | Q1_MAP, HANDLER(cli_resume));
 	set_handler_callback(VRB_RESIZE | Q1_MAP, HANDLER(cli_resize));
multipathd/cli.c +1 -0
index 0d679c86..25dce4e5 100644
@@ -228,6 +228,7 @@ load_keys (void)
 	r += add_key(keys, "setprhold", VRB_SETPRHOLD, 0);
 	r += add_key(keys, "unsetprhold", VRB_UNSETPRHOLD, 0);
 	r += add_key(keys, "pathlist", KEY_PATHLIST, 1);
+	r += add_key(keys, "wait", KEY_WAIT, 0);
 
 	if (r) {
 		free_keys(keys);
multipathd/cli.h +3 -0
index 6d599ca7..1c6c42e1 100644
@@ -64,6 +64,7 @@ enum {
 	KEY_GROUP		= 82,
 	KEY_KEY			= 83,
 	KEY_PATHLIST		= 84,
+	KEY_WAIT		= 85,
 };
 
 /*
@@ -85,6 +86,7 @@ enum {
 	Q1_ALL			= KEY_ALL << 8,
 	Q1_DAEMON		= KEY_DAEMON << 8,
 	Q1_STATUS		= KEY_STATUS << 8,
+	Q1_WAIT			= KEY_WAIT << 8,
 
 	/* byte 2: qualifier 2 */
 	Q2_FMT			= KEY_FMT << 16,
@@ -97,6 +99,7 @@ enum {
 	Q2_GROUP		= KEY_GROUP << 16,
 	Q2_KEY			= KEY_KEY << 16,
 	Q2_PATHLIST		= KEY_PATHLIST << 16,
+	Q2_WAIT			= KEY_WAIT << 16,
 
 	/* byte 3: qualifier 3 */
 	Q3_FMT			= KEY_FMT << 24,
multipathd/cli_handlers.c +20 -2
index e5c43bd8..86f050a0 100644
@@ -1021,7 +1021,7 @@ static int cli_reconfigure(void *v, struct strbuf *reply,
 {
 	condlog(2, "reconfigure (operator)");
 
-	schedule_reconfigure(FORCE_RELOAD_WEAK);
+	schedule_reconfigure(FORCE_RELOAD_WEAK, NULL);
 	return 0;
 }
 
@@ -1030,7 +1030,25 @@ int cli_reconfigure_all(void *v, struct strbuf *reply,
 {
 	condlog(2, "reconfigure all (operator)");
 
-	schedule_reconfigure(FORCE_RELOAD_YES);
+	schedule_reconfigure(FORCE_RELOAD_YES, NULL);
+	return 0;
+}
+
+static int cli_reconfigure_wait(void *v, struct strbuf *reply,
+				struct list_head *wait_node, void *data)
+{
+	condlog(2, "reconfigure wait (operator)");
+
+	schedule_reconfigure(FORCE_RELOAD_WEAK, wait_node);
+	return 0;
+}
+
+static int cli_reconfigure_all_wait(void *v, struct strbuf *reply,
+			            struct list_head *wait_node, void *data)
+{
+	condlog(2, "reconfigure all wait (operator)");
+
+	schedule_reconfigure(FORCE_RELOAD_YES, wait_node);
 	return 0;
 }
 
multipathd/main.c +13 -2
index 1bb8eac8..83773d90 100644
@@ -135,6 +135,8 @@ static enum daemon_status running_state = DAEMON_INIT;
 static bool delayed_reconfig;
 /* Don't access this variable without holding config_lock */
 static enum force_reload_types reconfigure_pending = FORCE_RELOAD_NONE;
+/* Don't access this variable without holding config_lock */
+LIST_HEAD(config_wait_list);
 pid_t daemon_pid;
 static pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t config_cond;
@@ -336,7 +338,8 @@ void remove_map_callback(struct multipath *mpp)
 		unblock_reconfigure();
 }
 
-void schedule_reconfigure(enum force_reload_types requested_type)
+void schedule_reconfigure(enum force_reload_types requested_type,
+			  struct list_head *wait_node)
 {
 	pthread_mutex_lock(&config_lock);
 	pthread_cleanup_push(config_cleanup, NULL);
@@ -360,6 +363,8 @@ void schedule_reconfigure(enum force_reload_types requested_type)
 	default:
 		break;
 	}
+	if (wait_node)
+		wait_for_event(wait_node, &config_wait_list);
 	pthread_cleanup_pop(1);
 }
 
@@ -3657,7 +3662,7 @@ handle_signals(bool nonfatal)
 		return;
 	if (reconfig_sig) {
 		condlog(3, "reconfigure (signal)");
-		schedule_reconfigure(FORCE_RELOAD_WEAK);
+		schedule_reconfigure(FORCE_RELOAD_WEAK, NULL);
 	}
 	if (log_reset_sig) {
 		condlog(3, "reset log (signal)");
@@ -4169,6 +4174,12 @@ child (__attribute__((unused)) void *param)
 			pthread_mutex_unlock(&config_lock);
 
 			rc = reconfigure(vecs, reload_type);
+
+			/* wake up waiters if there are no pending reconfigs */
+			pthread_mutex_lock(&config_lock);
+			if (reconfigure_pending == FORCE_RELOAD_NONE)
+				wakeup_event_waiter(&config_wait_list);
+			pthread_mutex_unlock(&config_lock);
 		} else {
 			pthread_mutex_lock(&config_lock);
 			delayed_reconfig = true;
multipathd/main.h +2 -1
index b66208ec..76d09196 100644
@@ -34,7 +34,8 @@ void exit_daemon(void);
 const char *daemon_status(bool *pending_reconfig);
 enum daemon_status wait_for_state_change_if(enum daemon_status oldstate,
 					    unsigned long ms);
-void schedule_reconfigure(enum force_reload_types requested_type);
+void schedule_reconfigure(enum force_reload_types requested_type,
+			  struct list_head *wait_node);
 int need_to_delay_reconfig (struct vectors *);
 int ev_add_path (struct path *, struct vectors *, int);
 int ev_remove_path (struct path *, struct vectors *, int);
multipathd/multipathd.8.in +17 -2
index 632cbe40..aee67004 100644
@@ -274,13 +274,28 @@ group index, starting with 1.
 .B reconfigure
 Rereads the configuration, and reloads all changed multipath devices. This
 also happens at startup, when the service is reload, or when a SIGHUP is
-received.
+received. This command schedules the reconfiguration and returns immediately.
+.
+.TP
+.B reconfigure wait
+Rereads the configuration, and reloads all changed multipath devices. This
+also happens at startup, when the service is reload, or when a SIGHUP is
+received. This command waits for for the reconfiguration to complete before
+returning
 .
 .TP
 .B reconfigure all
 Rereads the configuration, and reloads all multipath devices regardless of
 whether or not they have changed. This also happens when \fImultipath -r\fR is
-run.
+run. This command schedules the reconfiguration and returns immediately.
+.
+.TP
+.B reconfigure all wait
+Rereads the configuration, and reloads all multipath devices regardless of
+whether or not they have changed. This also happens when \fImultipath -r\fR is
+run. This command waits for for the reconfiguration to complete before
+returning
+.
 .TP
 .B suspend map|multipath $map
 Sets map $map into suspend state.
Signature · 1 line
2.56.0
#6 lore raw

Re: [PATCH 4/4] multipathd: add commands to wait for reconfigures to complete

Quoted 105 lines

Add the "reconfigure wait" and "reconfigure all wait" multipathd commands. These work just like the non-wait options, but they do not return until there are no more scheduled reconfigures.

multipathd/callbacks.c +3 -0
index 2027c3a..6064751 100644
@@ -44,6 +44,9 @@ void init_handler_callbacks(void)
 	set_unlocked_handler_callback(VRB_RECONFIGURE, HANDLER(cli_reconfigure));
 	set_unlocked_handler_callback(VRB_RECONFIGURE | Q1_ALL,
 				      HANDLER(cli_reconfigure_all));
+	set_unlocked_handler_callback(VRB_RECONFIGURE | Q1_WAIT, HANDLER(cli_reconfigure_wait));
+	set_unlocked_handler_callback(VRB_RECONFIGURE | Q1_ALL | Q2_WAIT,
+				      HANDLER(cli_reconfigure_all_wait));
 	set_handler_callback(VRB_SUSPEND | Q1_MAP, HANDLER(cli_suspend));
 	set_handler_callback(VRB_RESUME | Q1_MAP, HANDLER(cli_resume));
 	set_handler_callback(VRB_RESIZE | Q1_MAP, HANDLER(cli_resize));
multipathd/cli.c +1 -0
index 0d679c8..25dce4e 100644
@@ -228,6 +228,7 @@ load_keys (void)
 	r += add_key(keys, "setprhold", VRB_SETPRHOLD, 0);
 	r += add_key(keys, "unsetprhold", VRB_UNSETPRHOLD, 0);
 	r += add_key(keys, "pathlist", KEY_PATHLIST, 1);
+	r += add_key(keys, "wait", KEY_WAIT, 0);
 
 	if (r) {
 		free_keys(keys);
multipathd/cli.h +3 -0
index 6d599ca..1c6c42e 100644
@@ -64,6 +64,7 @@ enum {
 	KEY_GROUP		= 82,
 	KEY_KEY			= 83,
 	KEY_PATHLIST		= 84,
+	KEY_WAIT		= 85,
 };
 
 /*
@@ -85,6 +86,7 @@ enum {
 	Q1_ALL			= KEY_ALL << 8,
 	Q1_DAEMON		= KEY_DAEMON << 8,
 	Q1_STATUS		= KEY_STATUS << 8,
+	Q1_WAIT			= KEY_WAIT << 8,
 
 	/* byte 2: qualifier 2 */
 	Q2_FMT			= KEY_FMT << 16,
@@ -97,6 +99,7 @@ enum {
 	Q2_GROUP		= KEY_GROUP << 16,
 	Q2_KEY			= KEY_KEY << 16,
 	Q2_PATHLIST		= KEY_PATHLIST << 16,
+	Q2_WAIT			= KEY_WAIT << 16,
 
 	/* byte 3: qualifier 3 */
 	Q3_FMT			= KEY_FMT << 24,
multipathd/cli_handlers.c +20 -2
index e5c43bd..86f050a 100644
@@ -1021,7 +1021,7 @@ static int cli_reconfigure(void *v, struct strbuf *reply,
 {
 	condlog(2, "reconfigure (operator)");
 
-	schedule_reconfigure(FORCE_RELOAD_WEAK);
+	schedule_reconfigure(FORCE_RELOAD_WEAK, NULL);
 	return 0;
 }
 
@@ -1030,7 +1030,25 @@ int cli_reconfigure_all(void *v, struct strbuf *reply,
 {
 	condlog(2, "reconfigure all (operator)");
 
-	schedule_reconfigure(FORCE_RELOAD_YES);
+	schedule_reconfigure(FORCE_RELOAD_YES, NULL);
+	return 0;
+}
+
+static int cli_reconfigure_wait(void *v, struct strbuf *reply,
+				struct list_head *wait_node, void *data)
+{
+	condlog(2, "reconfigure wait (operator)");
+
+	schedule_reconfigure(FORCE_RELOAD_WEAK, wait_node);
+	return 0;
+}
+
+static int cli_reconfigure_all_wait(void *v, struct strbuf *reply,
+			            struct list_head *wait_node, void *data)
+{
+	condlog(2, "reconfigure all wait (operator)");
+
+	schedule_reconfigure(FORCE_RELOAD_YES, wait_node);
 	return 0;
 }
 
multipathd/main.c +0 -0
index 1bb8eac..83773d9 100644

multipathd/main.c static enum daemon_status running_state = DAEMON_INIT; · line 139

Quoted 5 lines
multipathd/main.c +2 -0
@@ -135,6 +135,8 @@ static enum daemon_status running_state = DAEMON_INIT;
 static bool delayed_reconfig;
 /* Don't access this variable without holding config_lock */
 static enum force_reload_types reconfigure_pending = FORCE_RELOAD_NONE;
+/* Don't access this variable without holding config_lock */
+LIST_HEAD(config_wait_list);

I am unsure about the purpose of using config_lock for this list. The uxlsnr side uses client_wait_lock, and knows nothing about config_lock, so it may manipulate elements of this list without taking config_lock. The list should be protected by client_wait_lock.

Quoted 14 lines
multipathd/main.c +2 -1
 pid_t daemon_pid;
 static pthread_mutex_t config_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t config_cond;
@@ -336,7 +338,8 @@ void remove_map_callback(struct multipath *mpp)
 		unblock_reconfigure();
 }
 
-void schedule_reconfigure(enum force_reload_types requested_type)
+void schedule_reconfigure(enum force_reload_types requested_type,
+			  struct list_head *wait_node)
 {
 	pthread_mutex_lock(&config_lock);
 	pthread_cleanup_push(config_cleanup, NULL);

multipathd/main.c void schedule_reconfigure(enum force_reload_types requested_type) · line 367

Quoted 5 lines
multipathd/main.c +2 -0
@@ -360,6 +363,8 @@ void schedule_reconfigure(enum force_reload_types requested_type)
 	default:
 		break;
 	}
+	if (wait_node)
+		wait_for_event(wait_node, &config_wait_list);

We should only wait for this event if reconfigure_pending ws actually set. Waiting in DAEMON_SHUTDOWN state seems definitely wrong.

Quoted 46 lines
multipathd/main.c +7 -1
 	pthread_cleanup_pop(1);
 }
 
@@ -3657,7 +3662,7 @@ handle_signals(bool nonfatal)
 		return;
 	if (reconfig_sig) {
 		condlog(3, "reconfigure (signal)");
-		schedule_reconfigure(FORCE_RELOAD_WEAK);
+		schedule_reconfigure(FORCE_RELOAD_WEAK, NULL);
 	}
 	if (log_reset_sig) {
 		condlog(3, "reset log (signal)");
@@ -4169,6 +4174,12 @@ child (__attribute__((unused)) void *param)
 			pthread_mutex_unlock(&config_lock);
 
 			rc = reconfigure(vecs, reload_type);
+
+			/* wake up waiters if there are no pending reconfigs */
+			pthread_mutex_lock(&config_lock);
+			if (reconfigure_pending == FORCE_RELOAD_NONE)
+				wakeup_event_waiter(&config_wait_list);
+			pthread_mutex_unlock(&config_lock);
 		} else {
 			pthread_mutex_lock(&config_lock);
 			delayed_reconfig = true;
multipathd/main.h +2 -1
index b66208e..76d0919 100644
@@ -34,7 +34,8 @@ void exit_daemon(void);
 const char *daemon_status(bool *pending_reconfig);
 enum daemon_status wait_for_state_change_if(enum daemon_status oldstate,
 					    unsigned long ms);
-void schedule_reconfigure(enum force_reload_types requested_type);
+void schedule_reconfigure(enum force_reload_types requested_type,
+			  struct list_head *wait_node);
 int need_to_delay_reconfig (struct vectors *);
 int ev_add_path (struct path *, struct vectors *, int);
 int ev_remove_path (struct path *, struct vectors *, int);
multipathd/multipathd.8.in +0 -0
index 632cbe4..aee6700 100644
@@ -274,13 +274,28 @@ group index, starting with 1.
 .B reconfigure
 Rereads the configuration, and reloads all changed multipath devices. This

multipathd/multipathd.8.in group index, starting with 1. · line 284

Quoted 10 lines
multipathd/multipathd.8.in +8 -1
 also happens at startup, when the service is reload, or when a SIGHUP is
-received.
+received. This command schedules the reconfiguration and returns immediately.
+.
+.TP
+.B reconfigure wait
+Rereads the configuration, and reloads all changed multipath devices. This
+also happens at startup, when the service is reload, or when a SIGHUP is
+received. This command waits for for the reconfiguration to complete before
+returning

Nit: "for for", missing period at end.

The actual implementation waits for all scheduled reconfigure operations to end, not just for "the" reconfiguration started by this command. Not sure if we need to mention that, it may cause more confusion than insight, as overlapping reconfigure requests are rare in practice.

Quoted 4 lines
multipathd/multipathd.8.in +0 -0
 .
 .TP
 .B reconfigure all
 Rereads the configuration, and reloads all multipath devices regardless of

multipathd/multipathd.8.in group index, starting with 1. · line 297

Quoted 10 lines
multipathd/multipathd.8.in +8 -1
 whether or not they have changed. This also happens when \fImultipath -r\fR is
-run.
+run. This command schedules the reconfiguration and returns immediately.
+.
+.TP
+.B reconfigure all wait
+Rereads the configuration, and reloads all multipath devices regardless of
+whether or not they have changed. This also happens when \fImultipath -r\fR is
+run. This command waits for for the reconfiguration to complete before
+returning

Same comment as for "reconfigure wait".

Signature · 1 line
Martin Wilck <[email protected]>