From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796057711; b=aADb48msr46RwcZB1JQ671NBAjJxzQY2ESQ2dHCwvRHhyrO40JB304xoesS9X4Cs5Amj mD7mIdqgrR2WvyjkW87iAJVStL2vI2xU+jgYUUU+YO7+SIKrhtfdth+sSrN6IOcvuVNKS BHMeh0ZueUac/GmP5D2WCMytnnCEM3mQMZFaeKHmuuE0WJu5JVjJmt7HzfCaA2mdUkzPI PrUZex/eUK6ECoIaC92zydryUDt2oOCrxwqGgqZZlwvCqjWkixGGSwa0TrmaNTIwHUphC vIxnFIRxW7blOrAKY9p8VvWolmKf8OB7az59bdC8a7U8RGzinpfl/aCGUL39OSR+8gA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796057711; h=DMARC-Filter:DKIM-Signature:X-MC-Rep:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:Received:Received:Date:From: To:Cc:Subject:Message-ID:References:MIME-Version:In-Reply-To: X-Scanned-By:X-Mimecast-Spam-Score:X-Mimecast-MFC-PROC-ID: X-Mimecast-Originator:Content-Type:Content-Disposition; bh=kQ74oVUeb9AalfZXFzOiRC9eWepIiHLmmEU6p0HzSwA=; b=PS6WhbQMDgRwl5d3gBoqBLIHR19tYsm5Zdy4qjQAc3XBb5kIbBKMqMddUgj3ehOK6eu8 8TbCWDW6CwXZSchR873twriIh1BeCxDwMGzL0IP0yxB1thORdOJPakKw28TVkWY7jEz0/ ldIazyywX9MaKk52XFU1sdQO2Xg8QJGVwJE5ClyEcrgTNAqCUFpepiuqVDMLOaKqAnImJ CtPAcFmc2XblX5rdot9laI/mvvHrJB7jy3rUZUcsWNQcc+agLamXD47ZDEgSH32hECPk3 mLPLWOLKMBUkpeKvjFm2PRyWs/U7Z/r1phAoGfXuTuv3lvZK6IK3UxNnHlo+0Hm6y/g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=FD1DD6F5; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6131606A320 for ; Mon, 30 Nov 2026 16:55:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B6131606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=FD1DD6F5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796057709; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=kQ74oVUeb9AalfZXFzOiRC9eWepIiHLmmEU6p0HzSwA=; b=FD1DD6F5Maw/pHkKCwStK5D4wLyUrxQcThypwVFICIlSAVJB682W4TVIHAWGmMDn9310+u 8Nm6FsZxmOgJKjySPq9VlDPEOTxWCkfsQ+ndH5LKYrZqrFUgxlRz1EoV6c0BsJ0RNYCY1b T2ORo8gxEqNBEVgPjpnQjPqY8yHwHC8= X-MC-Rep: binary re single Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-231-XM6n0aDZOtqiQG6ONIMTIw-1; Mon, 30 Nov 2026 16:55:07 +0000 X-MC-Unique: XM6n0aDZOtqiQG6ONIMTIw-1 X-Mimecast-MFC-AGG-ID: XM6n0aDZOtqiQG6ONIMTIw_1796057706 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3A0A4180047F; Mon, 30 Nov 2026 16:55:06 +0000 (UTC) Received: from bmarzins-01.fast.eng.rdu2.dc.redhat.com (bmarzins-01.fast.eng.rdu2.dc.redhat.com [10.6.23.12]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E8C8418011D3; Mon, 30 Nov 2026 16:55:05 +0000 (UTC) Received: from bmarzins-01.fast.eng.rdu2.dc.redhat.com (localhost [127.0.0.1]) by bmarzins-01.fast.eng.rdu2.dc.redhat.com (8.18.1/8.17.1) with ESMTPS id 6AUGt4JC3480397 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 30 Nov 2026 11:55:04 -0500 Received: (from bmarzins@localhost) by bmarzins-01.fast.eng.rdu2.dc.redhat.com (8.18.1/8.18.1/Submit) id 6AUGt4Uq3480396; Mon, 30 Nov 2026 11:55:04 -0500 Date: Mon, 30 Nov 2026 11:55:04 -0500 From: Benjamin Marzinski To: Martin Wilck Cc: Christophe Varoqui , device-mapper development Subject: Re: [PATCH 2/4] multipathd: add infrastructure for uxlsnr waiting on events Message-ID: References: <20261126052434.725762-1-bmarzins@redhat.com> <20261126052434.725762-3-bmarzins@redhat.com> <179569332052.310906.11503714620320054070.b4-review@b4> X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 In-Reply-To: <179569332052.310906.11503714620320054070.b4-review@b4> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: p1QuApPob7lyWT7vpRF-sbruwJJSqMOm5tsJjddaAV4_1796057706 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > > > > 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