From: Benjamin Marzinski <bmarzins@redhat.com>
To: Martin Wilck <martin.wilck@suse.com>
Cc: Christophe Varoqui <christophe.varoqui@opensvc.com>,
device-mapper development <dm-devel@lists.linux.dev>
Subject: Re: [PATCH 2/4] multipathd: add infrastructure for uxlsnr waiting on events
Date: Mon, 30 Nov 2026 11:55:04 -0500 [thread overview]
Message-ID: <aw2qaMZPG0gSeI8t@redhat.com> (raw)
In-Reply-To: <179569332052.310906.11503714620320054070.b4-review@b4>
On Thu, Nov 26, 2026 at 12:42:00PM +0100, Martin Wilck wrote:
> > 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 <bmarzins@redhat.com>
> >
> > 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.
Sure.
> > +}
> > +
> > 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.
The way this is supposed to work is that there will be a list per event
that a client could wait on. For removing paths, for instance, there
will be a new per-path list_head (well, basically. There is extra issues
I want to deal with in the wait on addition and wait on remove code,
which I'm still working on). Multiple clients could be waiting for a
reconfigure or a device to get removed, and we want all of them woken.
But there shouldn't be clients on a list who aren't waiting for that
specific event.
> > +
> > +/* 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.
It just seems wrong to grab the lock every time any client exits. Would
you be o.k. with having a special return value from the client handler
functions to signal that the client needs to wait and a flag that
clients can set when they get this return? Clients who haven't set the
flag can skip the lock, just like with the current list empty code. Only
the uxlsnr thread would set and check the flag, so there's no locking
needed.
> > + 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.
Like I said, clients will be on a list exclusively for the event they
are waiting on. When the get removed, their event occurred.
> > +
> > 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.
We can avoid this with the client waiting flag as well. It might be
over-optimizing but it's not a fancy lock-skipping optimization and I'd
rather avoid grabbing a lock twice for every client, when almost none of
them are going to need it. If your really against that, that's fine. We
can just always check this under a lock.
> > + 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?
See above.
-Ben
>
> --
> Martin Wilck <mwilck@suse.com>
next prev parent reply other threads:[~2026-11-30 16:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 5:24 [PATCH 0/4] multipath: add the ability to wait on events Benjamin Marzinski
2026-11-26 5:24 ` [PATCH 1/4] multipathd: make the wakeup and drain idle_fd code generic Benjamin Marzinski
2026-11-26 5:24 ` [PATCH 2/4] multipathd: add infrastructure for uxlsnr waiting on events Benjamin Marzinski
2026-11-26 11:42 ` Martin Wilck
2026-11-30 16:55 ` Benjamin Marzinski [this message]
2026-12-01 23:46 ` Benjamin Marzinski
2026-11-26 5:24 ` [PATCH 3/4] multipathd: make cli_handlers take a wait_node Benjamin Marzinski
2026-11-26 5:24 ` [PATCH 4/4] multipathd: add commands to wait for reconfigures to complete Benjamin Marzinski
2026-11-26 11:42 ` Martin Wilck
2026-11-30 19:05 ` Benjamin Marzinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aw2qaMZPG0gSeI8t@redhat.com \
--to=bmarzins@redhat.com \
--cc=christophe.varoqui@opensvc.com \
--cc=dm-devel@lists.linux.dev \
--cc=martin.wilck@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.