Fix RCU hang
Benjamin Marzinski
maintainercover Fix RCU hang
I have run into a RCU related hang with multipathd, when I reconfigured and then shutdown multipathd in quick succession. I haven't been able to recreate it without manually adding a sleep() call to the code, but I have figured out what could have caused it.
While the rcu code is waiting for a grace period to elapse, no threads can register or unregister as rcu reader threads. If for some reason, a thread never calls put_multipath_config() to exit a read side critical section, then any threads trying to start or stop will hang. This can happen if a thread is cancelled between calls to get_multipath_config() and put_multipath_config(), and multipathd is reconfigured (which causes the rcu code to wait for a grace period).
The only threads that get cancelled before the end of multipathd (at least in the setup I was running) are the waiter threads and the tur checker threads. The tur checker threads never hit a cancellation point while in a read side critcal section (although they do have read side critical sections due to using condlog, so they need to register with the rcu code). The waiter threads can do this, but only while holding the vecs lock, which should protect them from being cancelled.
The other threads occasionally hit cancellation points inside read side critical sections while not protected by the vecs lock. What appears to have happened is that one of the threads was taking a long time in a read side critical section starting before the reconfigure call to call_rcu(). It remained in that section until the reconfigure ended, and multipathd was shutdown, cancelling the thread. I can easily reproduce this by adding a sleep() into one of these sections.
The second patch may be overkill. In it, I fix all the multipathd code to either not have a cancellation point in a read side critical section, or to use a pthread_cleanup_handler. I could just fix the code that is called without holding the vecs lock, since you can guarantee that the waiter threads will never be cancelled while holding the vecs lock, and that main threads can't hold a vecs lock while reconfigure is running, and reconfigure will never run after the main threads have been cancelled. I did this for three reasons.
1. Without digging more into the rcu code, I couldn't be sure that the reader thread simply needed to be in a quiescent state after call_rcu() to count for the grace period. Since call_rcu() uses another thread to wait for the grace period, it seemed possible that this rcu thread would start checking threads for being in a queiescent state some time after call_rcu() happened. If this is the case, then holding the vecs lock would protect a thread from having the rcu code start to wait for a grace period. This could probably be overcome be forcing the main threads to be cancelled while holding the vecs lock, so that they could simply never be cancelled in these sections.
2. It seems sloppy to have threads exit without correctly finishing their read side critical sections, even if we know that the rcu won't be waiting for a grace period again before multipathd is shut down.
3. This would be adding one more surprising thing for the vecs lock to be be in charge of.
Benjamin Marzinski (2):
multipathd: register threads that use rcu calls
multipath: fix rcu thread cancellation hang
libmultipath/checkers/rbd.c | 7 +++- libmultipath/checkers/tur.c | 9 +++-- libmultipath/config.h | 2 +- libmultipath/configure.c | 90 +++++++++++++++++++++++++++------------------ libmultipath/devmapper.c | 10 +++-- libmultipath/discovery.c | 13 +++++-- libmultipath/parser.c | 3 +- libmultipath/structs_vec.c | 9 +++-- libmultipath/uevent.c | 15 +++++--- libmultipath/wwids.c | 18 +++++---- mpathpersist/main.c | 2 +- multipath/main.c | 2 +- multipathd/cli_handlers.c | 86 +++++++++++++++++++++++++++---------------- multipathd/main.c | 64 +++++++++++++++++++++----------- tests/globals.c | 2 +- 15 files changed, 209 insertions(+), 123 deletions(-)
Signature · 1 line
2.7.4
Benjamin Marzinski
maintainerPATCH 1/2 multipathd: register threads that use rcu calls
All calls to condlog() are rcu reader side calls, so any thread that uses condlog() must register itself. The only threads that are exempt are log_thread, since it never calls condlog (or any other function that calls get_multipath_config) and mpath_pr_event_handler_fn, which is only called by mpath_persist, which disables the rcu handling.
--- libmultipath/checkers/rbd.c | 7 +++++-- libmultipath/checkers/tur.c | 9 ++++++--- multipathd/main.c | 7 +++++-- 3 files changed, 16 insertions(+), 7 deletions(-)
libmultipath/checkers/rbd.c +5 -2
@@ -19,6 +19,7 @@#include <sys/ioctl.h> #include <sys/time.h> #include <sys/wait.h> +#include <urcu.h> #include "rados/librados.h"
@@ -517,6 +518,7 @@ static void cleanup_func(void *data)pthread_spin_unlock (&ct->hldr_lock);if (!holders)cleanup_context (ct); +rcu_unregister_thread (); }static void *rbd_thread (void *ctx)
@@ -524,11 +526,12 @@ static void *rbd_thread(void *ctx)struct rbd_checker_context *ct = ctx;int state; +/* This thread can be canceled, so setup clean up */ +rbd_thread_cleanup_push (ct) +rcu_register_thread ();condlog (3 ,"rbd%d: thread starting up" , ct->rbd_bus_id); ct->message[0 ] ='\0' ; -/* This thread can be canceled, so setup clean up */ - rbd_thread_cleanup_push(ct)/* checker start up */ pthread_mutex_lock (&ct->lock);
libmultipath/checkers/tur.c +6 -3
@@ -15,6 +15,7 @@#include <errno.h> #include <sys/time.h> #include <pthread.h> +#include <urcu.h> #include <urcu/uatomic.h> #include "checkers.h"
@@ -220,6 +221,7 @@ static void cleanup_func(void *data) holders =uatomic_sub_return (&ct->holders,1 );if (!holders)cleanup_context (ct); +rcu_unregister_thread (); }static void copy_msg_to_tcc (void *ct_p,const char *msg)
@@ -237,11 +239,12 @@ static void *tur_thread(void *ctx)int state, running;char devt[32 ]; -condlog (3 ,"%s: tur checker starting up" , -tur_devt (devt,sizeof (devt), ct)); -/* This thread can be canceled, so setup clean up */ tur_thread_cleanup_push (ct); +rcu_register_thread (); + +condlog (3 ,"%s: tur checker starting up" , +tur_devt (devt,sizeof (devt), ct));/* TUR checker start up */ pthread_mutex_lock (&ct->lock);
multipathd/main.c +5 -2
@@ -3041,12 +3041,13 @@ void * mpath_pr_event_handler_fn (void * pathp )struct prout_param_descriptor *param;struct prin_resp *resp; +rcu_register_thread (); mpp = pp->mpp; resp =mpath_alloc_prin_response (MPATH_PRIN_RKEY_SA );if (!resp){condlog (0 ,"%s Alloc failed for prin response" , pp->dev); -return NULL ; + goto out; } ret =prin_do_scsi_ioctl (pp->dev,MPATH_PRIN_RKEY_SA , resp,0 );
@@ -3104,7 +3105,9 @@ void * mpath_pr_event_handler_fn (void * pathp )free (param); out: -free (resp); +if (resp) +free (resp); +rcu_unregister_thread ();return NULL ; }
Signature · 1 line
2.7.4
Re: [PATCH 1/2] multipathd: register threads that use rcu calls
Quoted 9 lines · Benjamin Marzinski
All calls to
condlog()are rcu reader side calls, so any thread that usescondlog()must register itself. The only threads that are exempt arelog_thread, since it never calls condlog (or any other function that
callsget_multipath_config) andmpath_pr_event_handler_fn, which is only
called bympath_persist, which disables the rcu handling.Signed-off-by Benjamin Marzinski <[email protected]>
Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107 SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) -- dm-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/dm-devel
Benjamin Marzinski
maintainerPATCH 2/2 multipath: fix rcu thread cancellation hang
In reply to Benjamin MarzinskiWhile the rcu code is waiting for a grace period to elapse, no threads can register or unregister as rcu reader threads. If for some reason, a thread never calls put_multipath_config() to exit a read side critical section, then any threads trying to start or stop will hang. This can happen if a thread is cancelled between calls to get_multipath_config() and put_multipath_config(), and multipathd is reconfigured (which causes the rcu code to wait for a grace period).
This patch fixes this issue in two ways. Where possible, it reorders the code or saves config values into local variables to remove cancellation points between calls to get_multipath_config() and put_multipath_config(). In cases where this isn't possible (or where it would cause a significant amount of extra work to be done) multipath now pushes a cleanup handler to call put_multipath_config().
The only functions that were not modified were ones that were only called by multipath or mpathpersist, since these are single threaded and already disable rcu thread registration.
--- libmultipath/config.h | 2 +- libmultipath/configure.c | 90 +++++++++++++++++++++++++++------------------- libmultipath/devmapper.c | 10 ++++-- libmultipath/discovery.c | 13 ++++--- libmultipath/parser.c | 3 +- libmultipath/structs_vec.c | 9 +++-- libmultipath/uevent.c | 15 ++++---- libmultipath/wwids.c | 18 +++++----- mpathpersist/main.c | 2 +- multipath/main.c | 2 +- multipathd/cli_handlers.c | 86 ++++++++++++++++++++++++++++---------------- multipathd/main.c | 57 ++++++++++++++++++----------- tests/globals.c | 2 +- 13 files changed, 193 insertions(+), 116 deletions(-)
libmultipath/config.h +1 -1
@@ -232,6 +232,6 @@ struct config *load_config (char * file);struct config *alloc_config (void );void free_config (struct config * conf);extern struct config *get_multipath_config (void ); -extern void put_multipath_config (struct config *); +extern void put_multipath_config (void *);#endif
libmultipath/configure.c +54 -36
228 lines not on this page · raw message
libmultipath/devmapper.c +7 -3
@@ -241,12 +241,16 @@ void libmp_udev_set_sync_support(int on)void libmp_dm_init (void ) {struct config *conf; +int verbosity; +unsigned int version[3 ]; conf =get_multipath_config (); -dm_init (conf->verbosity); -if (dm_prereq (conf->version)) -exit (1 ); + verbosity = conf->verbosity; +memcpy (version, conf->version,sizeof (version));put_multipath_config (conf); +dm_init (verbosity); +if (dm_prereq (version)) +exit (1 );dm_udev_set_sync_support (libmp_dm_udev_sync); }
libmultipath/discovery.c +9 -4
@@ -170,10 +170,11 @@ path_discovery (vector pathvec, int flag)if (devtype && !strncmp (devtype,"disk" ,4 )) { total_paths++; conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf);if (path_discover (pathvec, conf, udevice, flag) ==PATHINFO_OK ) num_paths++; -put_multipath_config (conf); +pthread_cleanup_pop (1 ); }udev_device_unref (udevice); }
@@ -1617,6 +1618,7 @@ get_prio (struct path * pp) {struct prio * p;struct config *conf; +int checker_timeout;if (!pp)return 0 ;
@@ -1624,9 +1626,10 @@ get_prio (struct path * pp) p = &pp->prio;if (!prio_selected (p)) { conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf);select_detect_prio (conf, pp);select_prio (conf, pp); -put_multipath_config (conf); +pthread_cleanup_pop (1 );if (!prio_selected (p)) {condlog (3 ,"%s: no prio selected" , pp->dev); pp->priority =PRIO_UNDEF ;
@@ -1634,8 +1637,9 @@ get_prio (struct path * pp) } } conf =get_multipath_config (); - pp->priority =prio_getprio (p, pp, conf->checker_timeout); + checker_timeout = conf->checker_timeout;put_multipath_config (conf); + pp->priority =prio_getprio (p, pp, checker_timeout);if (pp->priority <0 ) {condlog (3 ,"%s: %s prio error" , pp->dev,prio_name (p)); pp->priority =PRIO_UNDEF ;
@@ -1849,8 +1853,9 @@ get_uid (struct path * pp, int path_state, struct udev_device *udev)if (!pp->uid_attribute && !pp->getuid) { conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf);select_getuid (conf, pp); -put_multipath_config (conf); +pthread_cleanup_pop (1 ); }memset (pp->wwid,0 ,WWID_SIZE );
libmultipath/parser.c +2 -1
@@ -171,8 +171,9 @@ snprint_keyword(char *buff, int len, char *fmt, struct keyword *kw,break ;case 'v' : conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf); r = kw->put_multipath_config (conf); +pthread_cleanup_pop (1 );if (!r) {/* no output if no value */ buff[0 ] ='\0' ;return 0 ;
libmultipath/structs_vec.c +6 -3
@@ -71,9 +71,10 @@ int adopt_paths(vector pathvec, struct multipath *mpp) store_path(mpp->paths, pp))return 1 ; conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf); ret =pathinfo (pp, conf,DI_PRIO |DI_CHECKER ); -put_multipath_config (conf); +pthread_cleanup_pop (1 );if (ret)return 1 ; }
@@ -276,7 +277,10 @@ update_multipath_strings(struct multipath *mpp, vector pathvec, int is_daemon)void enter_recovery_mode (struct multipath *mpp) { +int checkint;struct config *conf =get_multipath_config (); + checkint = conf->checkint; +put_multipath_config (conf);/* * Enter retry mode.
@@ -284,10 +288,9 @@ void enter_recovery_mode(struct multipath *mpp)* starting retry. */ mpp->stat_queueing_timeouts++; - mpp->retry_tick = mpp->no_path_retry * conf->checkint +1 ; + mpp->retry_tick = mpp->no_path_retry * checkint +1 ;condlog (1 ,"%s: Entering recovery mode: max_retries=%d" , mpp->alias, mpp->no_path_retry); -put_multipath_config (conf); }void
libmultipath/uevent.c +9 -6
@@ -162,8 +162,9 @@ uevent_get_wwid(struct uevent *uev)struct config * conf; conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf); uid_attribute =parse_uid_attribute_by_attrs (conf->uid_attrs, uev->kernel); -put_multipath_config (conf); +pthread_cleanup_pop (1 ); val =uevent_get_env_var (uev, uid_attribute);if (val)
@@ -188,6 +189,7 @@ uevent_need_merge(void)bool uevent_can_discard (struct uevent *uev) { +int invalid =0 ;struct config * conf;/*
@@ -199,13 +201,14 @@ uevent_can_discard(struct uevent *uev)* filter paths devices by devnode */ conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf);if (filter_devnode (conf->blist_devnode, conf->elist_devnode, - uev->kernel) >0 ) { -put_multipath_config (conf); -return true; - } -put_multipath_config (conf); + uev->kernel) >0 ) + invalid =1 ; +pthread_cleanup_pop (1 ); +if (invalid) +return true;return false; }
libmultipath/wwids.c +10 -8
@@ -92,8 +92,9 @@ replace_wwids(vector mp)struct config *conf; conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf); fd =open_file (conf->wwids_file, &can_write,WWIDS_FILE_HEADER ); -put_multipath_config (conf); +pthread_cleanup_pop (1 );if (fd <0 ) goto out;if (!can_write) {
@@ -206,8 +207,9 @@ remove_wwid(char *wwid) { }condlog (3 ,"removing line '%s' from wwids file" , str); conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf); fd =open_file (conf->wwids_file, &can_write,WWIDS_FILE_HEADER ); -put_multipath_config (conf); +pthread_cleanup_pop (1 );if (fd <0 ) goto out;if (!can_write) {
@@ -231,8 +233,9 @@ check_wwids_file(char *wwid, int write_wwid)struct config *conf; conf =get_multipath_config (); +pthread_cleanup_push (put_multipath_config, conf); fd =open_file (conf->wwids_file, &can_write,WWIDS_FILE_HEADER ); -put_multipath_config (conf); +pthread_cleanup_pop (1 );if (fd <0 )return -1 ;
@@ -273,17 +276,16 @@ out:int should_multipath (struct path *pp1,vector pathvec) { -int i, ignore_new_devs; +int i, ignore_new_devs, find_multipaths;struct path *pp2;struct config *conf; conf =get_multipath_config (); ignore_new_devs = conf->ignore_new_devs; -if (!conf->find_multipaths && !ignore_new_devs) { -put_multipath_config (conf); -return 1 ; - } + find_multipaths = conf->find_multipaths;put_multipath_config (conf); +if (find_multipaths && !ignore_new_devs) +return 1 ;condlog (4 ,"checking if %s should be multipathed" , pp1->dev);if (!ignore_new_devs) {
mpathpersist/main.c +1 -1
@@ -47,7 +47,7 @@ struct config *get_multipath_config(void)return multipath_conf; } -void put_multipath_config (struct config *conf) +void put_multipath_config (void * arg) {/* Noop for now */ }
multipath/main.c +1 -1
@@ -70,7 +70,7 @@ struct config *get_multipath_config(void)return multipath_conf; } -void put_multipath_config (struct config *conf) +void put_multipath_config (void *arg) {/* Noop for now */ }
multipathd/cli_handlers.c +55 -31
239 lines not on this page · raw message
multipathd/main.c +37 -20
180 lines not on this page · raw message
tests/globals.c +1 -1
@@ -14,5 +14,5 @@ struct config *get_multipath_config(void)return &conf; } -void put_multipath_config (struct config * c) +void put_multipath_config (void *arg) {}
Signature · 1 line
2.7.4
Re: [PATCH 2/2] multipath: fix rcu thread cancellation hang
Quoted 24 lines · Benjamin Marzinski
While the rcu code is waiting for a grace period to elapse, no threads
can register or unregister as rcu reader threads. If for some reason, a
thread never callsput_multipath_config()to exit a read side critical
section, then any threads trying to start or stop will hang. This can happen if a thread is cancelled between calls toget_multipath_config()
andput_multipath_config(), and multipathd is reconfigured (which causes
the rcu code to wait for a grace period).This patch fixes this issue in two ways. Where possible, it reorders the
code or saves config values into local variables to remove cancellation
points between calls toget_multipath_config()andput_multipath_config(). In cases where this isn't possible (or where it
would cause a significant amount of extra work to be done) multipath now
pushes a cleanup handler to callput_multipath_config().
Quoted 5 lines
The only functions that were not modified were ones that were only called by multipath or mpathpersist, since these are single threaded and already disable rcu thread registration.
Signed-off-by Benjamin Marzinski <[email protected]>
Kudos for doing this meticulous work!
(I admit my review wasn't in depth. I fully ack the idea of the patch, and I scanned through it without spotting obvious errors. I did not check whether you should have changed more code as you already did).
Here's a suggestion, as I think this is getting pretty ugly (not your fault). Maybe we should rename get_multipath_config() to __get_multipath_config() and do something like
#define begin_with_config(conf) \
__get_multipath_config(conf); \
pthread_cleanup_push(__put_multipath_config, conf); \
do#define end_with_config(conf) \
while(0); \
pthread_cleanup_pop(1)... and require that all code blocks accessing the configuration should be coded like this:
begin_with_config(conf) {
... CODE ...
} end_with_config(conf);IMO that'd improve readability and reduce likelihood of errors.
As you're touching so many lines of code anyway, that wouldn't be that much harder :-/
Regards,
Martin
Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107 SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) -- dm-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/dm-devel