Skip to contentSkip to conversation

multipath: fixes for SAS expanders and root FS access

PATCH Superseded [email protected] 2 participants 10 messages Jan 28, 2021 – Feb 2, 2021 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover multipath: fixes for SAS expanders and root FS access

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

here are 3 patches I'd like to get reviewed before we create a pull request. The first two are related to complex SAS setups, the second one is to avoid accessing the root file system in a possible dangerous situation.

Wrt 2/3: while testing it, I discovered that "I_T_nexus_loss_timeout" is a read-only sysfs attribute, therefore this code does nothing. I considered removing it altogether, observing that the attribute has been read-only as long as it existed (v2.6.17, 2006). I'd like some feedback beforehand, though, perhaps some distros use patched kernels that make this attribute r/w?

Regards
Martin

Martin Wilck (3):
libmultipath: use 3rd digit as transport_id for expanders
libmultipath: sysfs_set_nexus_loss_tmo(): support SAS expanders
multipathd: add code to initalize unwinder

 libmultipath/discovery.c   | 35 ++++++++++++++++++++++++++++-------
 multipathd/Makefile        |  2 +-
 multipathd/init_unwinder.c | 34 ++++++++++++++++++++++++++++++++++
 multipathd/init_unwinder.h | 21 +++++++++++++++++++++
 multipathd/main.c          |  2 ++
 5 files changed, 86 insertions(+), 8 deletions(-)
 create mode 100644 multipathd/init_unwinder.c
 create mode 100644 multipathd/init_unwinder.h
Signature · 7 lines
2.29.2


--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#1 lore raw

PATCH 1/3 libmultipath: use 3rd digit as transport_id for expanders

From: Martin Wilck <[email protected]>

On SAS expanders, node id's have 3 digits. sysfs paths look like this:

/sys/devices/pci0000:80/0000:80:02.0/0000:8b:00.0/0000:8c:09.0/0000:8f:00.0/host9/port-9:0/expander-9:0/port-9:0:13/expander-9:1/port-9:1:12/expander-9:2/port-9:2:4/end_device-9:2:4/target9:0:29/9:0:29:0/block/sdac

In that case, we should use the last digit as transport id.

---
 libmultipath/discovery.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
libmultipath/discovery.c +9 -2
index e818585..f3ce3f8 100644
@@ -358,9 +358,16 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
 	if (value) {
 		tgtdev = udev_device_get_parent(parent);
 		while (tgtdev) {
+			char c;
+
 			tgtname = udev_device_get_sysname(tgtdev);
-			if (tgtname && sscanf(tgtname, "end_device-%d:%d",
-				   &host, &tgtid) == 2)
+			if (!tgtname)
+				continue;
+			if (sscanf(tgtname, "end_device-%d:%d:%d%c",
+				   &host, &channel, &tgtid, &c) == 3)
+				break;
+			if (sscanf(tgtname, "end_device-%d:%d%c",
+				   &host, &tgtid, &c) == 2)
 				break;
 			tgtdev = udev_device_get_parent(tgtdev);
 			tgtid = -1;
Signature · 7 lines
2.29.2


--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#2 lore raw

Re: [dm-devel] [PATCH 1/3] libmultipath: use 3rd digit as transport_id for expanders

Quoted 18 lines · On Thu, Jan 28, 2021 at 09:45:42PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

On SAS expanders, node id's have 3 digits. sysfs paths look like this:

/sys/devices/pci0000:80/0000:80:02.0/0000:8b:00.0/0000:8c:09.0/0000:8f:00.0/host9/port-9:0/expander-9:0/port-9:0:13/expander-9:1/port-9:1:12/expander-9:2/port-9:2:4/end_device-9:2:4/target9:0:29/9:0:29:0/block/sdac

In that case, we should use the last digit as transport id.

---
 libmultipath/discovery.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
libmultipath/discovery.c +0 -0
index e818585..f3ce3f8 100644

libmultipath/discovery.c sysfs_get_tgt_nodename(struct path *pp, char *node) · line 365

Quoted 10 lines
libmultipath/discovery.c +4 -2
@@ -358,9 +358,16 @@ sysfs_get_tgt_nodename(struct path *pp, char *node)
 	if (value) {
 		tgtdev = udev_device_get_parent(parent);
 		while (tgtdev) {
+			char c;
+
 			tgtname = udev_device_get_sysname(tgtdev);
-			if (tgtname && sscanf(tgtname, "end_device-%d:%d",
-				   &host, &tgtid) == 2)
+			if (!tgtname)
+				continue;

won't this make and endless loop if tgtname == NULL

-Ben

Quoted 10 lines
libmultipath/discovery.c +5 -0
+			if (sscanf(tgtname, "end_device-%d:%d:%d%c",
+				   &host, &channel, &tgtid, &c) == 3)
+				break;
+			if (sscanf(tgtname, "end_device-%d:%d%c",
+				   &host, &tgtid, &c) == 2)
 				break;
 			tgtdev = udev_device_get_parent(tgtdev);
 			tgtid = -1;
Signature · 1 line
2.29.2

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#3 lore raw

Re: [dm-devel] [PATCH 1/3] libmultipath: use 3rd digit as transport_id for expanders

Quoted 29 lines · Benjamin Marzinski
Quoted 28 lines · On Thu, Jan 28, 2021 at 09:45:42PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

On SAS expanders, node id's have 3 digits. sysfs paths look like this:

/sys/devices/pci0000:80/0000:80:02.0/0000:8b:00.0/0000:8c:09.0/0000 :8f:00.0/host9/port-9:0/expander-9:0/port-9:0:13/expander-9:1/port- 9:1:12/expander-9:2/port-9:2:4/end_device- 9:2:4/target9:0:29/9:0:29:0/block/sdac

In that case, we should use the last digit as transport id.

---
 libmultipath/discovery.c | 11 +++++++++--

 1 file changed, 9 insertions(+), 2 deletions(-)
libmultipath/discovery.c +2 -0
index e818585..f3ce3f8 100644
@@ -358,9 +358,16 @@ sysfs_get_tgt_nodename(struct path *pp, char
 *node)
         if (value) {
                 tgtdev = udev_device_get_parent(parent);
                 while (tgtdev) {
+                       char c;
+
Quoted 8 lines

libmultipath/discovery.c sysfs_get_tgt_nodename(struct path *pp, char *node) · line 365

Quoted 6 lines
libmultipath/discovery.c +2 -2
                         tgtname = udev_device_get_sysname(tgtdev);
-                       if (tgtname && sscanf(tgtname, "end_device-
 %d:%d",
-                                  &host, &tgtid) == 2)
+                       if (!tgtname)
+                               continue;

won't this make and endless loop if tgtname == NULL

Ouch. Thanks for spotting this.

Martin

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#4 lore raw

PATCH 2/3 libmultipath: sysfs_set_nexus_loss_tmo(): support SAS expanders

In reply to [email protected]

From: Martin Wilck <[email protected]>

With SAS expanders, SAS node names have 3 digits. libmultipath would fail to discover the sas_end_device matching a given SCSI target in this case. Fix it.

---
 libmultipath/discovery.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
libmultipath/discovery.c +19 -5
index f3ce3f8..7878454 100644
@@ -789,14 +789,28 @@ sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
 static void
 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
 {
-	struct udev_device *sas_dev = NULL;
-	char end_dev_id[64];
+	struct udev_device *parent, *sas_dev = NULL;
+	const char *end_dev_id = NULL;
 	char value[11];
+	static const char ed_str[] = "end_device-";
 
-	if (mpp->dev_loss == DEV_LOSS_TMO_UNSET)
+	if (!pp->udev || mpp->dev_loss == DEV_LOSS_TMO_UNSET)
 		return;
-	sprintf(end_dev_id, "end_device-%d:%d",
-		pp->sg_id.host_no, pp->sg_id.transport_id);
+
+	for (parent = udev_device_get_parent(pp->udev);
+	     parent;
+	     parent = udev_device_get_parent(parent)) {
+		const char *ed = udev_device_get_sysname(parent);
+
+		if (!strncmp(ed, ed_str, sizeof(ed_str) - 1)) {
+			end_dev_id = ed;
+			break;
+		}
+	}
+	if (!end_dev_id) {
+		condlog(1, "%s: No SAS end device", pp->dev);
+		return;
+	}
 	sas_dev = udev_device_new_from_subsystem_sysname(udev,
 				"sas_end_device", end_dev_id);
 	if (!sas_dev) {
Signature · 7 lines
2.29.2


--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
maintainer Reviewed-by on v1 2/3
#5 lore raw

Re: [dm-devel] [PATCH 2/3] libmultipath: sysfs_set_nexus_loss_tmo(): support SAS expanders

Quoted 7 lines · On Thu, Jan 28, 2021 at 09:45:43PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

With SAS expanders, SAS node names have 3 digits. libmultipath would fail to discover the sas_end_device matching a given SCSI target in this case. Fix it.

Quoted 44 lines
---
 libmultipath/discovery.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
libmultipath/discovery.c +19 -5
index f3ce3f8..7878454 100644
@@ -789,14 +789,28 @@ sysfs_set_session_tmo(struct multipath *mpp, struct path *pp)
 static void
 sysfs_set_nexus_loss_tmo(struct multipath *mpp, struct path *pp)
 {
-	struct udev_device *sas_dev = NULL;
-	char end_dev_id[64];
+	struct udev_device *parent, *sas_dev = NULL;
+	const char *end_dev_id = NULL;
 	char value[11];
+	static const char ed_str[] = "end_device-";
 
-	if (mpp->dev_loss == DEV_LOSS_TMO_UNSET)
+	if (!pp->udev || mpp->dev_loss == DEV_LOSS_TMO_UNSET)
 		return;
-	sprintf(end_dev_id, "end_device-%d:%d",
-		pp->sg_id.host_no, pp->sg_id.transport_id);
+
+	for (parent = udev_device_get_parent(pp->udev);
+	     parent;
+	     parent = udev_device_get_parent(parent)) {
+		const char *ed = udev_device_get_sysname(parent);
+
+		if (!strncmp(ed, ed_str, sizeof(ed_str) - 1)) {
+			end_dev_id = ed;
+			break;
+		}
+	}
+	if (!end_dev_id) {
+		condlog(1, "%s: No SAS end device", pp->dev);
+		return;
+	}
 	sas_dev = udev_device_new_from_subsystem_sysname(udev,
 				"sas_end_device", end_dev_id);
 	if (!sas_dev) {
Signature · 1 line
2.29.2

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#6 lore raw

PATCH 3/3 multipathd: add code to initalize unwinder

In reply to [email protected]

From: Martin Wilck <[email protected]>

glibc's implementation of pthread_cancel() loads symbols from libgcc_s.so using dlopen() when pthread_cancel() is called for the first time. This happens even with LD_BIND_NOW=1. This may imply the need for file system access when a thread is cancelled, which in the case of multipath-tools might be in a dangerous situation where multipathd must avoid blocking.

Call load_unwinder() during startup to make sure the dynamic linker has all necessary symbols resolved early on.

This implementation simply creates a dummy thread and cancels it. This way all necessary symbols for thread cancellation will be loaded, no matter what the C library needs to implement cancellation.

---
 multipathd/Makefile        |  2 +-
 multipathd/init_unwinder.c | 34 ++++++++++++++++++++++++++++++++++
 multipathd/init_unwinder.h | 21 +++++++++++++++++++++
 multipathd/main.c          |  2 ++
 4 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 multipathd/init_unwinder.c
 create mode 100644 multipathd/init_unwinder.h
multipathd/Makefile +1 -1
index 632b82b..d053c1e 100644
@@ -30,7 +30,7 @@ ifeq ($(ENABLE_DMEVENTS_POLL),0)
 endif
 
 OBJS = main.o pidfile.o uxlsnr.o uxclnt.o cli.o cli_handlers.o waiter.o \
-       dmevents.o
+       dmevents.o init_unwinder.o
 
 EXEC = multipathd
 
multipathd/init_unwinder.c new file +34 -0
new file mode 100644
index 0000000..14467f3
@@ -0,0 +1,34 @@
+#include <pthread.h>
+#include <unistd.h>
+#include "init_unwinder.h"
+
+static pthread_mutex_t dummy_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
+
+static void *dummy_thread(void *arg __attribute__((unused)))
+{
+	pthread_mutex_lock(&dummy_mtx);
+	pthread_cond_broadcast(&dummy_cond);
+	pthread_mutex_unlock(&dummy_mtx);
+	pause();
+	return NULL;
+}
+
+int init_unwinder(void)
+{
+	pthread_t dummy;
+	int rc;
+
+	pthread_mutex_lock(&dummy_mtx);
+
+	rc = pthread_create(&dummy, NULL, dummy_thread, NULL);
+	if (rc != 0) {
+		pthread_mutex_unlock(&dummy_mtx);
+		return rc;
+	}
+
+	pthread_cond_wait(&dummy_cond, &dummy_mtx);
+	pthread_mutex_unlock(&dummy_mtx);
+
+	return pthread_cancel(dummy);
+}
multipathd/init_unwinder.h new file +21 -0
new file mode 100644
index 0000000..ada09f8
@@ -0,0 +1,21 @@
+#ifndef _INIT_UNWINDER_H
+#define _INIT_UNWINDER_H 1
+
+/*
+ * init_unwinder(): make sure unwinder symbols are loaded
+ *
+ * libc's implementation of pthread_cancel() loads symbols from
+ * libgcc_s.so using dlopen() when pthread_cancel() is called
+ * for the first time. This happens even with LD_BIND_NOW=1.
+ * This may imply the need for file system access when a thread is
+ * cancelled, which in the case of multipath-tools might be in a
+ * dangerous situation where multipathd must avoid blocking.
+ *
+ * Call load_unwinder() during startup to make sure the dynamic
+ * linker has all necessary symbols resolved early on.
+ *
+ * Return: 0 if successful, an error number otherwise.
+ */
+int init_unwinder(void);
+
+#endif
multipathd/main.c +2 -0
index 99a89a6..6f851ae 100644
@@ -83,6 +83,7 @@
 #include "wwids.h"
 #include "foreign.h"
 #include "../third-party/valgrind/drd.h"
+#include "init_unwinder.h"
 
 #define FILE_NAME_SIZE 256
 #define CMDSIZE 160
@@ -3041,6 +3042,7 @@ child (__attribute__((unused)) void *param)
 	enum daemon_status state;
 	int exit_code = 1;
 
+	init_unwinder();
 	mlockall(MCL_CURRENT | MCL_FUTURE);
 	signal_init();
 	mp_rcu_data = setup_rcu();
Signature · 7 lines
2.29.2


--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
maintainer Reviewed-by on v1 3/3
#7 lore raw

Re: [dm-devel] [PATCH 3/3] multipathd: add code to initalize unwinder

Quoted 9 lines · On Thu, Jan 28, 2021 at 09:45:44PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

glibc's implementation of pthread_cancel() loads symbols from libgcc_s.so using dlopen() when pthread_cancel() is called for the first time. This happens even with LD_BIND_NOW=1. This may imply the need for file system access when a thread is cancelled, which in the case of multipath-tools might be in a dangerous situation where multipathd must avoid blocking.

Quoted 8 lines

Call load_unwinder() during startup to make sure the dynamic linker has all necessary symbols resolved early on.

This implementation simply creates a dummy thread and cancels it. This way all necessary symbols for thread cancellation will be loaded, no matter what the C library needs to implement cancellation.

Quoted 112 lines
---
 multipathd/Makefile        |  2 +-
 multipathd/init_unwinder.c | 34 ++++++++++++++++++++++++++++++++++
 multipathd/init_unwinder.h | 21 +++++++++++++++++++++
 multipathd/main.c          |  2 ++
 4 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 multipathd/init_unwinder.c
 create mode 100644 multipathd/init_unwinder.h
multipathd/Makefile +1 -1
index 632b82b..d053c1e 100644
@@ -30,7 +30,7 @@ ifeq ($(ENABLE_DMEVENTS_POLL),0)
 endif
 
 OBJS = main.o pidfile.o uxlsnr.o uxclnt.o cli.o cli_handlers.o waiter.o \
-       dmevents.o
+       dmevents.o init_unwinder.o
 
 EXEC = multipathd
 
multipathd/init_unwinder.c new file +34 -0
new file mode 100644
index 0000000..14467f3
@@ -0,0 +1,34 @@
+#include <pthread.h>
+#include <unistd.h>
+#include "init_unwinder.h"
+
+static pthread_mutex_t dummy_mtx = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
+
+static void *dummy_thread(void *arg __attribute__((unused)))
+{
+	pthread_mutex_lock(&dummy_mtx);
+	pthread_cond_broadcast(&dummy_cond);
+	pthread_mutex_unlock(&dummy_mtx);
+	pause();
+	return NULL;
+}
+
+int init_unwinder(void)
+{
+	pthread_t dummy;
+	int rc;
+
+	pthread_mutex_lock(&dummy_mtx);
+
+	rc = pthread_create(&dummy, NULL, dummy_thread, NULL);
+	if (rc != 0) {
+		pthread_mutex_unlock(&dummy_mtx);
+		return rc;
+	}
+
+	pthread_cond_wait(&dummy_cond, &dummy_mtx);
+	pthread_mutex_unlock(&dummy_mtx);
+
+	return pthread_cancel(dummy);
+}
multipathd/init_unwinder.h new file +21 -0
new file mode 100644
index 0000000..ada09f8
@@ -0,0 +1,21 @@
+#ifndef _INIT_UNWINDER_H
+#define _INIT_UNWINDER_H 1
+
+/*
+ * init_unwinder(): make sure unwinder symbols are loaded
+ *
+ * libc's implementation of pthread_cancel() loads symbols from
+ * libgcc_s.so using dlopen() when pthread_cancel() is called
+ * for the first time. This happens even with LD_BIND_NOW=1.
+ * This may imply the need for file system access when a thread is
+ * cancelled, which in the case of multipath-tools might be in a
+ * dangerous situation where multipathd must avoid blocking.
+ *
+ * Call load_unwinder() during startup to make sure the dynamic
+ * linker has all necessary symbols resolved early on.
+ *
+ * Return: 0 if successful, an error number otherwise.
+ */
+int init_unwinder(void);
+
+#endif
multipathd/main.c +2 -0
index 99a89a6..6f851ae 100644
@@ -83,6 +83,7 @@
 #include "wwids.h"
 #include "foreign.h"
 #include "../third-party/valgrind/drd.h"
+#include "init_unwinder.h"
 
 #define FILE_NAME_SIZE 256
 #define CMDSIZE 160
@@ -3041,6 +3042,7 @@ child (__attribute__((unused)) void *param)
 	enum daemon_status state;
 	int exit_code = 1;
 
+	init_unwinder();
 	mlockall(MCL_CURRENT | MCL_FUTURE);
 	signal_init();
 	mp_rcu_data = setup_rcu();
Signature · 1 line
2.29.2

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#8 lore raw
In reply to [email protected]
Quoted 9 lines · On Thu, Jan 28, 2021 at 09:45:41PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

here are 3 patches I'd like to get reviewed before we create a pull request. The first two are related to complex SAS setups, the second one is to avoid accessing the root file system in a possible dangerous situation.

Quoted 6 lines

Wrt 2/3: while testing it, I discovered that "I_T_nexus_loss_timeout" is a read-only sysfs attribute, therefore this code does nothing. I considered removing it altogether, observing that the attribute has been read-only as long as it existed (v2.6.17, 2006). I'd like some feedback beforehand, though, perhaps some distros use patched kernels that make this attribute r/w?

I_T_nexus_loss_timeout appears to have always been read-only on RHEL and Fedora.

-Ben

Quoted 9 lines

Regards
Martin

Martin Wilck (3):
libmultipath: use 3rd digit as transport_id for expanders
libmultipath: sysfs_set_nexus_loss_tmo(): support SAS expanders
multipathd: add code to initalize unwinder

Quoted 11 lines
 libmultipath/discovery.c   | 35 ++++++++++++++++++++++++++++-------
 multipathd/Makefile        |  2 +-
 multipathd/init_unwinder.c | 34 ++++++++++++++++++++++++++++++++++
 multipathd/init_unwinder.h | 21 +++++++++++++++++++++
 multipathd/main.c          |  2 ++
 5 files changed, 86 insertions(+), 8 deletions(-)
 create mode 100644 multipathd/init_unwinder.c
 create mode 100644 multipathd/init_unwinder.h
Signature · 1 line
2.29.2

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

#9 lore raw
Quoted 22 lines · Benjamin Marzinski
Quoted 11 lines · On Thu, Jan 28, 2021 at 09:45:41PM +0100, [email protected] wrote:

From: Martin Wilck <[email protected]>

Hi Christophe, hi Ben,

here are 3 patches I'd like to get reviewed before we create a pull request. The first two are related to complex SAS setups, the second
one is to avoid accessing the root file system in a possible dangerous
situation.

Quoted 9 lines

Wrt 2/3: while testing it, I discovered that "I_T_nexus_loss_timeout"
is a read-only sysfs attribute, therefore this code does nothing. I considered removing it altogether, observing that the attribute has
been read-only as long as it existed (v2.6.17, 2006). I'd like some feedback beforehand, though, perhaps some distros use patched kernels
that make this attribute r/w?

Quoted 3 lines

I_T_nexus_loss_timeout appears to have always been read-only on RHEL and
Fedora.

Thanks. Anyway, given that we're preparing a submission to Christophe, I'd like to give people more time for responding. We can remove this code later.

Regards
Martin

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel