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

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