Blame SOURCES/0059-libmpathpersist-fix-thread-safety-of-default-functio.patch

05be62
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
05be62
From: Benjamin Marzinski <bmarzins@redhat.com>
05be62
Date: Mon, 25 Jan 2021 23:31:04 -0600
05be62
Subject: [PATCH] libmpathpersist: fix thread safety of default functions
05be62
05be62
commit a839e39e ("libmpathpersist: factor out initialization and
05be62
teardown") made mpath_presistent_reserve_{in,out} use share variables
05be62
for curmp and pathvec.  There are users of this library that call these
05be62
functions in a multi-threaded process, and this change causes their
05be62
application to crash. config and udev are also shared variables, but
05be62
libmpathpersist doesn't write to the config in
05be62
mpath_presistent_reserve_{in,out}, and looking into the libudev code, I
05be62
don't see any place where libmpathpersist uses the udev object in a way
05be62
that isn't thread-safe.
05be62
05be62
This patch makes mpath_presistent_reserve_{in,out} go back to using
05be62
local variables for curmp and pathvec, so that multiple threads won't
05be62
be operating on these variables at the same time.
05be62
05be62
Fixes: a839e39e ("libmpathpersist: factor out initialization and teardown")
05be62
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
05be62
Reviewed-by: Martin Wilck <mwilck@suse.com>
05be62
---
05be62
 libmpathpersist/mpath_persist.c | 116 +++++++++++++++++++++-----------
05be62
 libmpathpersist/mpath_persist.h |  24 +++++--
05be62
 2 files changed, 94 insertions(+), 46 deletions(-)
05be62
05be62
diff --git a/libmpathpersist/mpath_persist.c b/libmpathpersist/mpath_persist.c
05be62
index a01dfb0b..07a5f17f 100644
05be62
--- a/libmpathpersist/mpath_persist.c
05be62
+++ b/libmpathpersist/mpath_persist.c
05be62
@@ -147,72 +147,60 @@ mpath_prin_activepath (struct multipath *mpp, int rq_servact,
05be62
 	return ret;
05be62
 }
05be62
 
05be62
-int mpath_persistent_reserve_in (int fd, int rq_servact,
05be62
-	struct prin_resp *resp, int noisy, int verbose)
05be62
-{
05be62
-	int ret = mpath_persistent_reserve_init_vecs(verbose);
05be62
-
05be62
-	if (ret != MPATH_PR_SUCCESS)
05be62
-		return ret;
05be62
-	ret = __mpath_persistent_reserve_in(fd, rq_servact, resp, noisy);
05be62
-	mpath_persistent_reserve_free_vecs();
05be62
-	return ret;
05be62
-}
05be62
-
05be62
-int mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
05be62
-	unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy, int verbose)
05be62
-{
05be62
-	int ret = mpath_persistent_reserve_init_vecs(verbose);
05be62
-
05be62
-	if (ret != MPATH_PR_SUCCESS)
05be62
-		return ret;
05be62
-	ret = __mpath_persistent_reserve_out(fd, rq_servact, rq_scope, rq_type,
05be62
-					     paramp, noisy);
05be62
-	mpath_persistent_reserve_free_vecs();
05be62
-	return ret;
05be62
-}
05be62
-
05be62
 static vector curmp;
05be62
 static vector pathvec;
05be62
 
05be62
-void mpath_persistent_reserve_free_vecs(void)
05be62
+static void __mpath_persistent_reserve_free_vecs(vector curmp, vector pathvec)
05be62
 {
05be62
 	free_multipathvec(curmp, KEEP_PATHS);
05be62
 	free_pathvec(pathvec, FREE_PATHS);
05be62
+}
05be62
+
05be62
+void mpath_persistent_reserve_free_vecs(void)
05be62
+{
05be62
+	__mpath_persistent_reserve_free_vecs(curmp, pathvec);
05be62
 	curmp = pathvec = NULL;
05be62
 }
05be62
 
05be62
-int mpath_persistent_reserve_init_vecs(int verbose)
05be62
+static int __mpath_persistent_reserve_init_vecs(vector *curmp_p,
05be62
+						vector *pathvec_p, int verbose)
05be62
 {
05be62
 	struct config *conf = get_multipath_config();
05be62
 
05be62
 	conf->verbosity = verbose;
05be62
 	put_multipath_config(conf);
05be62
 
05be62
-	if (curmp)
05be62
+	if (*curmp_p)
05be62
 		return MPATH_PR_SUCCESS;
05be62
 	/*
05be62
 	 * allocate core vectors to store paths and multipaths
05be62
 	 */
05be62
-	curmp = vector_alloc ();
05be62
-	pathvec = vector_alloc ();
05be62
+	*curmp_p = vector_alloc ();
05be62
+	*pathvec_p = vector_alloc ();
05be62
 
05be62
-	if (!curmp || !pathvec){
05be62
+	if (!*curmp_p || !*pathvec_p){
05be62
 		condlog (0, "vector allocation failed.");
05be62
 		goto err;
05be62
 	}
05be62
 
05be62
-	if (dm_get_maps(curmp))
05be62
+	if (dm_get_maps(*curmp_p))
05be62
 		goto err;
05be62
 
05be62
 	return MPATH_PR_SUCCESS;
05be62
 
05be62
 err:
05be62
-	mpath_persistent_reserve_free_vecs();
05be62
+	__mpath_persistent_reserve_free_vecs(*curmp_p, *pathvec_p);
05be62
+	*curmp_p = *pathvec_p = NULL;
05be62
 	return MPATH_PR_DMMP_ERROR;
05be62
 }
05be62
 
05be62
-static int mpath_get_map(int fd, char **palias, struct multipath **pmpp)
05be62
+int mpath_persistent_reserve_init_vecs(int verbose)
05be62
+{
05be62
+	return __mpath_persistent_reserve_init_vecs(&curmp, &pathvec, verbose);
05be62
+}
05be62
+
05be62
+static int mpath_get_map(vector curmp, vector pathvec, int fd, char **palias,
05be62
+			 struct multipath **pmpp)
05be62
 {
05be62
 	int ret = MPATH_PR_DMMP_ERROR;
05be62
 	struct stat info;
05be62
@@ -272,13 +260,13 @@ out:
05be62
 	return ret;
05be62
 }
05be62
 
05be62
-int __mpath_persistent_reserve_in (int fd, int rq_servact,
05be62
-	struct prin_resp *resp, int noisy)
05be62
+static int do_mpath_persistent_reserve_in (vector curmp, vector pathvec,
05be62
+	int fd, int rq_servact, struct prin_resp *resp, int noisy)
05be62
 {
05be62
 	struct multipath *mpp;
05be62
 	int ret;
05be62
 
05be62
-	ret = mpath_get_map(fd, NULL, &mpp;;
05be62
+	ret = mpath_get_map(curmp, pathvec, fd, NULL, &mpp;;
05be62
 	if (ret != MPATH_PR_SUCCESS)
05be62
 		return ret;
05be62
 
05be62
@@ -287,8 +275,17 @@ int __mpath_persistent_reserve_in (int fd, int rq_servact,
05be62
 	return ret;
05be62
 }
05be62
 
05be62
-int __mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
05be62
-	unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy)
05be62
+
05be62
+int __mpath_persistent_reserve_in (int fd, int rq_servact,
05be62
+	struct prin_resp *resp, int noisy)
05be62
+{
05be62
+	return do_mpath_persistent_reserve_in(curmp, pathvec, fd, rq_servact,
05be62
+					      resp, noisy);
05be62
+}
05be62
+
05be62
+static int do_mpath_persistent_reserve_out(vector curmp, vector pathvec, int fd,
05be62
+	int rq_servact, int rq_scope, unsigned int rq_type,
05be62
+	struct prout_param_descriptor *paramp, int noisy)
05be62
 {
05be62
 	struct multipath *mpp;
05be62
 	char *alias;
05be62
@@ -296,7 +293,7 @@ int __mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
05be62
 	uint64_t prkey;
05be62
 	struct config *conf;
05be62
 
05be62
-	ret = mpath_get_map(fd, &alias, &mpp;;
05be62
+	ret = mpath_get_map(curmp, pathvec, fd, &alias, &mpp;;
05be62
 	if (ret != MPATH_PR_SUCCESS)
05be62
 		return ret;
05be62
 
05be62
@@ -366,6 +363,45 @@ out1:
05be62
 	return ret;
05be62
 }
05be62
 
05be62
+
05be62
+int __mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
05be62
+	unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy)
05be62
+{
05be62
+	return do_mpath_persistent_reserve_out(curmp, pathvec, fd, rq_servact,
05be62
+					       rq_scope, rq_type, paramp,
05be62
+					       noisy);
05be62
+}
05be62
+
05be62
+int mpath_persistent_reserve_in (int fd, int rq_servact,
05be62
+	struct prin_resp *resp, int noisy, int verbose)
05be62
+{
05be62
+	vector curmp = NULL, pathvec;
05be62
+	int ret = __mpath_persistent_reserve_init_vecs(&curmp, &pathvec,
05be62
+						       verbose);
05be62
+
05be62
+	if (ret != MPATH_PR_SUCCESS)
05be62
+		return ret;
05be62
+	ret = do_mpath_persistent_reserve_in(curmp, pathvec, fd, rq_servact,
05be62
+					     resp, noisy);
05be62
+	__mpath_persistent_reserve_free_vecs(curmp, pathvec);
05be62
+	return ret;
05be62
+}
05be62
+
05be62
+int mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
05be62
+	unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy, int verbose)
05be62
+{
05be62
+	vector curmp = NULL, pathvec;
05be62
+	int ret = __mpath_persistent_reserve_init_vecs(&curmp, &pathvec,
05be62
+						       verbose);
05be62
+
05be62
+	if (ret != MPATH_PR_SUCCESS)
05be62
+		return ret;
05be62
+	ret = do_mpath_persistent_reserve_out(curmp, pathvec, fd, rq_servact,
05be62
+					      rq_scope, rq_type, paramp, noisy);
05be62
+	__mpath_persistent_reserve_free_vecs(curmp, pathvec);
05be62
+	return ret;
05be62
+}
05be62
+
05be62
 int
05be62
 get_mpvec (vector curmp, vector pathvec, char * refwwid)
05be62
 {
05be62
diff --git a/libmpathpersist/mpath_persist.h b/libmpathpersist/mpath_persist.h
05be62
index 7cf4faf9..0e4e0e53 100644
05be62
--- a/libmpathpersist/mpath_persist.h
05be62
+++ b/libmpathpersist/mpath_persist.h
05be62
@@ -215,9 +215,13 @@ extern int mpath_persistent_reserve_in (int fd, int rq_servact, struct prin_resp
05be62
 
05be62
 /*
05be62
  * DESCRIPTION :
05be62
- * This function is like mpath_persistent_reserve_in(), except that it doesn't call
05be62
- * mpath_persistent_reserve_init_vecs() and mpath_persistent_reserve_free_vecs()
05be62
- * before and after the actual PR call.
05be62
+ * This function is like mpath_persistent_reserve_in(), except that it
05be62
+ * requires mpath_persistent_reserve_init_vecs() to be called before the
05be62
+ * PR call to set up internal variables. These must later be cleanup up
05be62
+ * by calling mpath_persistent_reserve_free_vecs().
05be62
+ *
05be62
+ * RESTRICTIONS:
05be62
+ * This function uses static internal variables, and is not thread-safe.
05be62
  */
05be62
 extern int __mpath_persistent_reserve_in(int fd, int rq_servact,
05be62
 		struct prin_resp *resp, int noisy);
05be62
@@ -249,9 +253,13 @@ extern int mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
05be62
 		int verbose);
05be62
 /*
05be62
  * DESCRIPTION :
05be62
- * This function is like mpath_persistent_reserve_out(), except that it doesn't call
05be62
- * mpath_persistent_reserve_init_vecs() and mpath_persistent_reserve_free_vecs()
05be62
- * before and after the actual PR call.
05be62
+ * This function is like mpath_persistent_reserve_out(), except that it
05be62
+ * requires mpath_persistent_reserve_init_vecs() to be called before the
05be62
+ * PR call to set up internal variables. These must later be cleanup up
05be62
+ * by calling mpath_persistent_reserve_free_vecs().
05be62
+ *
05be62
+ * RESTRICTIONS:
05be62
+ * This function uses static internal variables, and is not thread-safe.
05be62
  */
05be62
 extern int __mpath_persistent_reserve_out( int fd, int rq_servact, int rq_scope,
05be62
 		unsigned int rq_type, struct prout_param_descriptor *paramp,
05be62
@@ -265,6 +273,7 @@ extern int __mpath_persistent_reserve_out( int fd, int rq_servact, int rq_scope,
05be62
  * @verbose: Set verbosity level. Input argument. value:0 to 3. 0->disabled, 3->Max verbose
05be62
  *
05be62
  * RESTRICTIONS:
05be62
+ * This function uses static internal variables, and is not thread-safe.
05be62
  *
05be62
  * RETURNS: MPATH_PR_SUCCESS if successful else returns any of the status specified
05be62
  *       above in RETURN_STATUS.
05be62
@@ -275,6 +284,9 @@ int mpath_persistent_reserve_init_vecs(int verbose);
05be62
  * DESCRIPTION :
05be62
  * This function frees data structures allocated by
05be62
  * mpath_persistent_reserve_init_vecs().
05be62
+ *
05be62
+ * RESTRICTIONS:
05be62
+ * This function uses static internal variables, and is not thread-safe.
05be62
  */
05be62
 void mpath_persistent_reserve_free_vecs(void);
05be62
 
05be62
-- 
05be62
2.17.2
05be62