Skip to contentSkip to conversation

multipathd: cleanup global queues after threads exit

PATCH In review Ganesh Andhale 3 participants 3 messages Nov 19 – Nov 23 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 multipathd: cleanup global queues after threads exit

cleanup_global_uevq() and cleanup_global_purge_queue() were registered as pthread cleanup handlers in worker threads. During shutdown, these handlers could run as soon as their owning thread was cancelled, while another thread that also accessed the same global queue had not been joined yet.

This allowed the global uevent or purge queue to be cleaned while a producer or consumer thread could still touch it.

Move the global queue cleanup to cleanup_queues(), which is called after cleanup_threads(), once all related threads have been joined. Keep the pthread cleanup handlers only for thread-local temporary lists and other resources owned by the cancelling thread.

Export cleanup_global_uevq() from libmultipath so multipathd can call it during daemon shutdown.

---
 libmultipath/libmultipath.version |  1 +
 libmultipath/uevent.c             |  4 +---
 libmultipath/uevent.h             |  2 ++
 multipathd/main.c                 | 12 ++++++++++++
 multipathd/purge.c                |  4 +---
 multipathd/purge.h                |  2 ++
 6 files changed, 19 insertions(+), 6 deletions(-)
libmultipath/libmultipath.version +1 -0
index f631f453..93f213b6 100644
@@ -203,6 +203,7 @@ global:
 	trigger_path_udev_change;
 	trigger_paths_udev_change;
 	udev;
+	cleanup_global_uevq;
 	uevent_dispatch;
 	uevent_get_dm_str;
 	uevent_get_env_positive_int;
libmultipath/uevent.c +1 -3
index b0c43340..e1c8aa72 100644
@@ -503,7 +503,7 @@ static void cleanup_uevq(void *arg)
 	uevq_cleanup(arg);
 }
 
-static void cleanup_global_uevq(void *arg __attribute__((unused)))
+void cleanup_global_uevq(void)
 {
 	pthread_mutex_lock(uevq_lockp);
 	uevq_cleanup(&uevq);
@@ -726,7 +726,6 @@ int uevent_listen(struct udev *udev)
 		goto out;
 	}
 
-	pthread_cleanup_push(cleanup_global_uevq, NULL);
 	pthread_cleanup_push(cleanup_uevq, &uevlisten_tmp);
 	while (1) {
 		int fdcount, events;
@@ -755,7 +754,6 @@ int uevent_listen(struct udev *udev)
 		pthread_mutex_unlock(uevq_lockp);
 	}
 	pthread_cleanup_pop(1);
-	pthread_cleanup_pop(1);
 out:
 	pthread_cleanup_pop(1);
 out_udev:
libmultipath/uevent.h +2 -0
index 69296789..f21419ea 100644
@@ -28,6 +28,8 @@ struct uevent {
 struct uevent *alloc_uevent(void);
 int is_uevent_busy(void);
 
+void cleanup_global_uevq(void);
+
 int uevent_listen(struct udev *udev);
 int uevent_dispatch(int (*store_uev)(struct uevent *, void * trigger_data),
 		    void * trigger_data);
multipathd/main.c +12 -0
index 9bce4d6b..11bd1d01 100644
@@ -3812,6 +3812,17 @@ static void cleanup_vecs(void)
 	gvecs = NULL;
 }
 
+static void cleanup_queues(void)
+{
+	/*
+	 * Shared queues must be cleaned only after every producer and
+	 * consumer has been joined. Worker cleanup handlers must not
+	 * free these globals while another thread may still access them.
+	 */
+	cleanup_global_purge_queue();
+	cleanup_global_uevq();
+}
+
 static void cleanup_threads(void)
 {
 	stop_io_err_stat_thread();
@@ -3915,6 +3926,7 @@ static void cleanup_rcu(void)
 static void cleanup_child(void)
 {
 	cleanup_threads();
+	cleanup_queues();
 	cleanup_vecs();
 	cleanup_bindings();
 	if (poll_dmevents)
multipathd/purge.c +1 -3
index 68db8ced..078b4079 100644
@@ -235,7 +235,7 @@ void cleanup_purge_list(void *arg)
  * Cleanup handler for the global purge queue.
  * Used during shutdown to free any remaining queued items.
  */
-static void cleanup_global_purge_queue(void *arg __attribute__((unused)))
+void cleanup_global_purge_queue(void)
 {
 	pthread_mutex_lock(&purge_mutex);
 	cleanup_purge_list(&purge_queue);
@@ -268,7 +268,6 @@ void *purgeloop(void *ap __attribute__((unused)))
 	 * Cleanup handler for global purge_queue.
 	 * This handles items that were queued but not yet moved to purge_list.
 	 */
-	pthread_cleanup_push(cleanup_global_purge_queue, NULL);
 
 	while (1) {
 		LIST_HEAD(purge_list);
@@ -320,7 +319,6 @@ void *purgeloop(void *ap __attribute__((unused)))
 		pthread_cleanup_pop(0);
 	}
 
-	pthread_cleanup_pop(1);
 	pthread_cleanup_pop(1);
 	return NULL;
 }
multipathd/purge.h +2 -0
index 1fe755f3..81ed952b 100644
@@ -33,6 +33,8 @@ void build_purge_list(struct vectors *vecs, struct list_head *tmpq);
  */
 void cleanup_purge_list(void *arg);
 
+void cleanup_global_purge_queue(void);
+
 /*
  * Main purge thread loop
  */
Signature · 1 line
2.56.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Ganesh Andhale

cleanup_global_uevq() and cleanup_global_purge_queue() were registered as pthread cleanup handlers in worker threads. During shutdown, these handlers could run as soon as their owning thread was cancelled, while another thread that also accessed the same global queue had not been joined yet.

This allowed the global uevent or purge queue to be cleaned while a producer or consumer thread could still touch it.

Move the global queue cleanup to cleanup_queues(), which is called after cleanup_threads(), once all related threads have been joined. Keep the pthread cleanup handlers only for thread-local temporary lists and other resources owned by the cancelling thread.

Quoted 4 lines

Export cleanup_global_uevq() from libmultipath so multipathd can call it during daemon shutdown.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Ganesh Andhale
Quoted 8 lines

cleanup_global_uevq() and cleanup_global_purge_queue() were registered as pthread cleanup handlers in worker threads. During shutdown, these handlers could run as soon as their owning thread was cancelled, while another thread that also accessed the same global queue had not been joined yet.

This allowed the global uevent or purge queue to be cleaned while a producer or consumer thread could still touch it.

The consumers of these lists take/hold the respective locks. So no race would be possible, AFAICT.

If you don't mind, I'll modify the commit message accordingly before applying.

Quoted 10 lines

Move the global queue cleanup to cleanup_queues(), which is called after cleanup_threads(), once all related threads have been joined. Keep the pthread cleanup handlers only for thread-local temporary lists and other resources owned by the cancelling thread.

Export cleanup_global_uevq() from libmultipath so multipathd can call it during daemon shutdown.

Signature · 1 line
Martin Wilck <[email protected]>