Blame SOURCES/0055-multipathd-avoid-io_err_stat-ABBA-deadlock.patch

b7337d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b7337d
From: Benjamin Marzinski <bmarzins@redhat.com>
b7337d
Date: Thu, 14 Jan 2021 20:20:24 -0600
b7337d
Subject: [PATCH] multipathd: avoid io_err_stat ABBA deadlock
b7337d
b7337d
When the checker thread enqueues paths for the io_err_stat thread to
b7337d
check, it calls enqueue_io_err_stat_by_path() with the vecs lock held.
b7337d
start_io_err_stat_thread() is also called with the vecs lock held.
b7337d
These two functions both lock io_err_pathvec_lock. When the io_err_stat
b7337d
thread updates the paths in vecs->pathvec in poll_io_err_stat(), it has
b7337d
the io_err_pathvec_lock held, and then locks the vecs lock. This can
b7337d
cause an ABBA deadlock.
b7337d
b7337d
To solve this, service_paths() no longer updates the paths in
b7337d
vecs->pathvec with the io_err_pathvec_lock held.  It does this by moving
b7337d
the io_err_stat_path from io_err_pathvec to a local vector when it needs
b7337d
to update the path. After releasing the io_err_pathvec_lock, it goes
b7337d
through this temporary vector, updates the paths with the vecs lock
b7337d
held, and then frees everything.
b7337d
b7337d
This change fixes a bug in service_paths() where elements were being
b7337d
deleted from io_err_pathvec, without the index being decremented,
b7337d
causing the loop to skip elements. Also, service_paths() could be
b7337d
cancelled while holding the io_err_pathvec_lock, so it should have a
b7337d
cleanup handler.
b7337d
b7337d
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
b7337d
Reviewed-by: Martin Wilck <mwilck@suse.com>
b7337d
---
b7337d
 libmultipath/io_err_stat.c | 56 ++++++++++++++++++++++----------------
b7337d
 1 file changed, 32 insertions(+), 24 deletions(-)
b7337d
b7337d
diff --git a/libmultipath/io_err_stat.c b/libmultipath/io_err_stat.c
b7337d
index f6c564f0..63ee2e07 100644
b7337d
--- a/libmultipath/io_err_stat.c
b7337d
+++ b/libmultipath/io_err_stat.c
b7337d
@@ -390,20 +390,6 @@ recover:
b7337d
 	return 0;
b7337d
 }
b7337d
 
b7337d
-static int delete_io_err_stat_by_addr(struct io_err_stat_path *p)
b7337d
-{
b7337d
-	int i;
b7337d
-
b7337d
-	i = find_slot(io_err_pathvec, p);
b7337d
-	if (i != -1)
b7337d
-		vector_del_slot(io_err_pathvec, i);
b7337d
-
b7337d
-	destroy_directio_ctx(p);
b7337d
-	free_io_err_stat_path(p);
b7337d
-
b7337d
-	return 0;
b7337d
-}
b7337d
-
b7337d
 static void account_async_io_state(struct io_err_stat_path *pp, int rc)
b7337d
 {
b7337d
 	switch (rc) {
b7337d
@@ -420,17 +406,26 @@ static void account_async_io_state(struct io_err_stat_path *pp, int rc)
b7337d
 	}
b7337d
 }
b7337d
 
b7337d
-static int poll_io_err_stat(struct vectors *vecs, struct io_err_stat_path *pp)
b7337d
+static int io_err_stat_time_up(struct io_err_stat_path *pp)
b7337d
 {
b7337d
 	struct timespec currtime, difftime;
b7337d
-	struct path *path;
b7337d
-	double err_rate;
b7337d
 
b7337d
 	if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
b7337d
-		return 1;
b7337d
+		return 0;
b7337d
 	timespecsub(&currtime, &pp->start_time, &difftime);
b7337d
 	if (difftime.tv_sec < pp->total_time)
b7337d
 		return 0;
b7337d
+	return 1;
b7337d
+}
b7337d
+
b7337d
+static void end_io_err_stat(struct io_err_stat_path *pp)
b7337d
+{
b7337d
+	struct timespec currtime;
b7337d
+	struct path *path;
b7337d
+	double err_rate;
b7337d
+
b7337d
+	if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
b7337d
+		currtime = pp->start_time;
b7337d
 
b7337d
 	io_err_stat_log(4, "%s: check end", pp->devname);
b7337d
 
b7337d
@@ -469,10 +464,6 @@ static int poll_io_err_stat(struct vectors *vecs, struct io_err_stat_path *pp)
b7337d
 				pp->devname);
b7337d
 	}
b7337d
 	lock_cleanup_pop(vecs->lock);
b7337d
-
b7337d
-	delete_io_err_stat_by_addr(pp);
b7337d
-
b7337d
-	return 0;
b7337d
 }
b7337d
 
b7337d
 static int send_each_async_io(struct dio_ctx *ct, int fd, char *dev)
b7337d
@@ -632,6 +623,7 @@ static void process_async_ios_event(int timeout_nsecs, char *dev)
b7337d
 	struct timespec	timeout = { .tv_nsec = timeout_nsecs };
b7337d
 
b7337d
 	errno = 0;
b7337d
+	pthread_testcancel();
b7337d
 	n = io_getevents(ioctx, 1L, CONCUR_NR_EVENT, events, &timeout);
b7337d
 	if (n < 0) {
b7337d
 		io_err_stat_log(3, "%s: async io events returned %d (errno=%s)",
b7337d
@@ -644,17 +636,33 @@ static void process_async_ios_event(int timeout_nsecs, char *dev)
b7337d
 
b7337d
 static void service_paths(void)
b7337d
 {
b7337d
+	struct _vector _pathvec = {0};
b7337d
+	/* avoid gcc warnings that &_pathvec will never be NULL in vector ops */
b7337d
+	struct _vector * const tmp_pathvec = &_pathvec;
b7337d
 	struct io_err_stat_path *pp;
b7337d
 	int i;
b7337d
 
b7337d
 	pthread_mutex_lock(&io_err_pathvec_lock);
b7337d
+	pthread_cleanup_push(cleanup_mutex, &io_err_pathvec_lock);
b7337d
 	vector_foreach_slot(io_err_pathvec, pp, i) {
b7337d
 		send_batch_async_ios(pp);
b7337d
 		process_async_ios_event(TIMEOUT_NO_IO_NSEC, pp->devname);
b7337d
 		poll_async_io_timeout();
b7337d
-		poll_io_err_stat(vecs, pp);
b7337d
+		if (io_err_stat_time_up(pp)) {
b7337d
+			if (!vector_alloc_slot(tmp_pathvec))
b7337d
+				continue;
b7337d
+			vector_del_slot(io_err_pathvec, i--);
b7337d
+			vector_set_slot(tmp_pathvec, pp);
b7337d
+		}
b7337d
 	}
b7337d
-	pthread_mutex_unlock(&io_err_pathvec_lock);
b7337d
+	pthread_cleanup_pop(1);
b7337d
+	vector_foreach_slot_backwards(tmp_pathvec, pp, i) {
b7337d
+		end_io_err_stat(pp);
b7337d
+		vector_del_slot(tmp_pathvec, i);
b7337d
+		destroy_directio_ctx(pp);
b7337d
+		free_io_err_stat_path(pp);
b7337d
+	}
b7337d
+	vector_reset(tmp_pathvec);
b7337d
 }
b7337d
 
b7337d
 static void cleanup_exited(__attribute__((unused)) void *arg)
b7337d
-- 
b7337d
2.17.2
b7337d