All of lore.kernel.org
 help / color / mirror / Atom feed
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 4/4] multipathd: add commands to wait for reconfigures to complete
Date: Mon, 30 Nov 2026 14:05:00 -0500	[thread overview]
Message-ID: <aw3I3OICdpsGFEyL@redhat.com> (raw)
In-Reply-To: <179569332053.310906.14178249137263551915.b4-review@b4>

On Thu, Nov 26, 2026 at 12:42:00PM +0100, Martin Wilck wrote:
> > 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.
> > 
> > Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
> >
> > diff --git a/multipathd/callbacks.c b/multipathd/callbacks.c
> > index 2027c3a..6064751 100644
> > --- a/multipathd/callbacks.c
> > +++ b/multipathd/callbacks.c
> > @@ -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));
> > diff --git a/multipathd/cli.c b/multipathd/cli.c
> > index 0d679c8..25dce4e 100644
> > --- a/multipathd/cli.c
> > +++ b/multipathd/cli.c
> > @@ -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);
> > diff --git a/multipathd/cli.h b/multipathd/cli.h
> > index 6d599ca..1c6c42e 100644
> > --- a/multipathd/cli.h
> > +++ b/multipathd/cli.h
> > @@ -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,
> > diff --git a/multipathd/cli_handlers.c b/multipathd/cli_handlers.c
> > index e5c43bd..86f050a 100644
> > --- a/multipathd/cli_handlers.c
> > +++ b/multipathd/cli_handlers.c
> > @@ -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;
> >  }
> >  
> > diff --git a/multipathd/main.c b/multipathd/main.c
> > index 1bb8eac..83773d9 100644
> > --- a/multipathd/main.c
> > +++ b/multipathd/main.c
> > @@ -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.

The client_wait_lock is a fast lock for clients to grab to check if they
have been woken up or to remove themselves from the list. The
config_lock is to make sure that scheduling the reconfigure and adding
the wait_node to the config_wait_list are atomic, so there isn't a race
between adding a waiter for a reconfigure and completing the
reconfigure. Otherwise, you could have the following race:

1. The uxlsnr thread calls schedule_reconfigure() and schedules a
   reconfigure

2. The main thread child() does the reconfigure

3. The main thread child() removes all waiters from its list.

4. The uxlsnr thread schedule_reconfigure() adds the waiter to the list.

5. The client's wait_node remains on the config_wait_list, even though
   there is no pending reconfigure.

You need to make sure that scheduling the reconfigure and adding the
wait_node to the list happen under the same lock, otherwise the
reconfigure and wakeup could happen in between them and the client could
never get woken up.

In theory, you could skip the client_wait_lock completely, and (in this
case) use the config_lock for all the synchronization. But it won't
always be the config_lock that you need to synchronize things. For
wating on per-device events, you might have to hold the vecs lock to
make sure that device isn't removed while you are adding the wait_node
to the per-path wait list. We don't want to have to grab the vecs lock
just to check if client has been woken up. So, there is a
client_wait_lock that should almost never be under contention, to grab
for simple list checking and manipulation.

There is also another potential race if you don't check
reconfigure_pending and wake up the waiters atomically. A
schedule_reconfigure could come in after child() checks reconfigure_pending
post reconfigure(), but before it calls wakeup_event_waiter(). Then a
waiter would get woken up, without having waited for the reconfigure it
scheduled.

However, this issue can get cleaned up if we don't do my too-fancy
lockless check. If we always check c->wait_node under a lock, then it's
safe to modify it in the child() thread. In that case, while we're
holding the config lock before the reconfigure call, where we set
reconfigure_pending to FORCE_RELOAD_NONE, we can also use
list_splice_tail_init() to move config_wait_list to a temporary list.
Then we just wake up that temporary list after calling reconfigure. This
means that waiters just wait for the first reconfigure that happens
after they call a waiting reconfigure function, instead of waiting till
there are no outstanding reconfigures.

Calling list_splice_tail_init() on a config_wait_list will change the
value of the tail wait_node's next pointer. It's possible that my
too-fancy lockless read could see a torn write that just happened to
make it look like wait_node->next it was pointing to wait_node, causing
list_empty() to return true.

> >  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);
> 
> We should only wait for this event if reconfigure_pending ws actually set.
> Waiting in DAEMON_SHUTDOWN state seems definitely wrong.

Yep. Good catch.
 
> >  	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;
> > diff --git a/multipathd/main.h b/multipathd/main.h
> > index b66208e..76d0919 100644
> > --- a/multipathd/main.h
> > +++ b/multipathd/main.h
> > @@ -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);
> > diff --git a/multipathd/multipathd.8.in b/multipathd/multipathd.8.in
> > index 632cbe4..aee6700 100644
> > --- a/multipathd/multipathd.8.in
> > +++ b/multipathd/multipathd.8.in
> > @@ -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
> 
> 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.

It didn't seem worthwhile to try and explain that. Also, like I said
above, if we remove my lockless checking code, we can just wait for the
next reconfigure to complete.

-Ben
 
> >  .
> >  .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
> 
> Same comment as for "reconfigure wait".
> 
> -- 
> Martin Wilck <mwilck@suse.com>


      reply	other threads:[~2026-11-30 19:05 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
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 [this message]

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=aw3I3OICdpsGFEyL@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.