> 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.
>
> Signed-off-by: Benjamin Marzinski <[email protected]>
>
> diff --git a/multipathd/main.c b/multipathd/main.c
> index 9bce4d6..1bb8eac 100644
> --- a/multipathd/main.c
> +++ b/multipathd/main.c
> @@ -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();
>
> diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
> index e92784e..0930691 100644
> --- a/multipathd/uxlsnr.c
> +++ b/multipathd/uxlsnr.c
> @@ -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);
> +
> + 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.
> +}
> +
> 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)
> +{
> + 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.
> +
> +/* must only be called by uxlsnr_thr */
> +static void stop_waiting_for_event(struct client *c)
> +{
> + /*
> + * 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.
> + pthread_mutex_lock(&client_wait_lock);
> + list_del_init(&c->wait_node);
> + pthread_mutex_unlock(&client_wait_lock);
> + }
> +}
> +
> +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.
> +
> 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;
>
> @@ -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.
> + 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);
> + 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?
--
Martin Wilck <[email protected]>