Skip to contentSkip to conversation

Race condition and memory leaks fixes.

PATCH New Konrad Rzeszutek 2 participants 7 messages Mar 24, 2009 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Race condition and memory leaks fixes.

Some more fixes to the bugs I've found. Please accept them in the tree.

Thanks!

#1 lore raw

PATCH 1/1 Fix race-condition when reading sysfs attributes.

This fix re-introduces a wait loop when reading sysfs attributes. The reason a wait is necessary is due to the way the kernel sends the event. When a SCSI device is added the SCSI subsystem pursues this path:

_sysfs_add_sdev:
calls device_add ...
[ '/devices/platform/host16/session6/target16:0:0/16:0:0:17'] uevent
bus_attach_device
bus_for_each_drv
driver_probe_device
sd_probe
['/class/scsi_disk/16:0:0:17' ] uevent
add_disk
['/block/sdai'] [ Here multipath starts its job ]

calls class_device_add ...
[ '/class/scsi_device/16:0:0:17' ] uevent
sg_add:
[ '/class/scsi_generic/sg35' ] uevent

done with device_add, and now the kernel adds the attributes:
scsi_sysfs_sdev_attrs[i].vendor, model, rev

There are four uevents sent from the kernel in the creation of a SysFS representation of the device. After the last event, the SysFs entries for vendor,model, rev are populated. The window time between the uevent with /block/sdX sent and the sysfs attributes being populated is small. But it is possible (with a slow serial link, slow SCSI target), to get us in a race condition where multipath investigates the new block device and finds it can't read the vendor, and the block device ends up being discarded and never added to the device mapper multipath map.

---
 libmultipath/discovery.c |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
libmultipath/discovery.c +38 -0
index 9ae2b8f..1d2c396 100644
@@ -124,11 +124,49 @@ path_discovery (vector pathvec, struct config * conf, int flag)
 	return r;
 }
 
+
+#define WAIT_MAX_SECONDS 5
+#define WAIT_LOOP_PER_SECOND 5
+
+static int
+wait_for_file (char * filename)
+{
+	int loop;
+	struct stat stats;
+	/*
+ 	* the daemon can race udev upon path add,
+ 	* not multipath(8), if ran by udev
+ 	*/
+
+	if (!conf->daemon)
+		return 0;
+
+	loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
+
+	while (--loop) {
+		if (stat(filename, &stats) == 0)
+			return 0;
+
+		if (errno != ENOENT)
+			return 1;
+
+		usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+	}
+	return 1;
+}
+
 #define declare_sysfs_get_str(fname) \
 extern int \
 sysfs_get_##fname (struct sysfs_device * dev, char * buff, size_t len) \
 { \
 	char *attr; \
+	char attr_path[SYSFS_PATH_SIZE]; \
+\
+	if (safe_sprintf(attr_path, "%s/%s/%s", sysfs_path, dev->devpath, #fname)) \
+		return 1; \
+\
+	if (wait_for_file(attr_path)) \
+		return 1; \
 \
 	attr = sysfs_attr_get_value(dev->devpath, #fname); \
 	if (!attr) \
Signature · 1 line
1.5.4.1
#2 lore raw

PATCH 1/1 Update the documentation for path checkers and use MALLOC instead of malloc in one path checker.

The internal memory debugger throws a fit if the path checkers are using the malloc instead of the 'MALLOC' macro.

---
 libmultipath/checkers.h              |    4 ++--
 libmultipath/checkers/emc_clariion.c |    3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
libmultipath/checkers.h +2 -2
index b610e6b..23b1c46 100644
@@ -36,7 +36,7 @@
  * - Description: Indicates path not available for "normal" operations
  *
  * PATH_GHOST:
- * - Use: Only hp_sw
+ * - Use: Only hp_sw and rdac
  * - Description: Indicates a "passive/standby" path on active/passive HP
  *   arrays.  These paths will return valid answers to certain SCSI commands
  *   (tur, read_capacity, inquiry, start_stop), but will fail I/O commands.
@@ -101,7 +101,7 @@ struct checker {
 	char message[CHECKER_MSG_LEN];       /* comm with callers */
 	void * context;                      /* store for persistent data */
 	void ** mpcontext;                   /* store for persistent data
-						shared multipath-wide */
+						shared multipath-wide. Use MALLOC if you want to stuff data in.  */
 	int (*check)(struct checker *);
 	int (*init)(struct checker *);       /* to allocate the context */
 	void (*free)(struct checker *);      /* to free the context */
libmultipath/checkers/emc_clariion.c +2 -1
index 9eac31d..200daee 100644
@@ -10,6 +10,7 @@
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <errno.h>
+#include <memory.h>
 
 #include "../libmultipath/sg_include.h"
 #include "libsg.h"
@@ -76,7 +77,7 @@ int libcheck_init (struct checker * c)
 	 * Allocate and initialize the multi-path global context.
 	 */
 	if (c->mpcontext) {
-		void * mpctxt = malloc(sizeof(int));
+		void * mpctxt = MALLOC(sizeof(int));
 		*c->mpcontext = mpctxt;
 		CLR_INACTIVE_SNAP(c);
 	}
Signature · 1 line
1.5.4.1
#3 lore raw

PATCH 1/1 Allocate only once the EMC-clariion global checker's stage area instead of multiple times.

The multi-path global context on every path checker init would be set. In scenarios where you have four paths per multipath, that context would be set four times instead of once causing a small memory leak (4 bytes per block disk).

---
 libmultipath/checkers/emc_clariion.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
libmultipath/checkers/emc_clariion.c +1 -1
index 200daee..08c9c26 100644
@@ -76,7 +76,7 @@ int libcheck_init (struct checker * c)
 	/*
 	 * Allocate and initialize the multi-path global context.
 	 */
-	if (c->mpcontext) {
+	if (c->mpcontext && *c->mpcontext == NULL) {
 		void * mpctxt = MALLOC(sizeof(int));
 		*c->mpcontext = mpctxt;
 		CLR_INACTIVE_SNAP(c);
Signature · 1 line
1.5.4.1
#4 lore raw

Re: [PATCH] Fix race-condition when reading sysfs attributes.

In reply to Konrad Rzeszutek
Quoted 9 lines · Konrad Rzeszutek

This fix re-introduces a wait loop when reading sysfs attributes. The reason a wait is necessary is due to the way the kernel sends the event.

There are four uevents sent from the kernel in the creation of a SysFS representation of the device. After the last event, the SysFs entries for vendor,model, rev are populated. The window time between the uevent with /block/sdX sent and the sysfs attributes being populated is small.

What kernel version are you using? I thought we fixed all that long ago.

Thanks,
Kay

#5 lore raw

Re: [PATCH] Fix race-condition when reading sysfs attributes.

Quoted 5 lines · Kay Sievers
Quoted 3 lines · Konrad Rzeszutek

This fix re-introduces a wait loop when reading sysfs attributes. The reason a wait is necessary is due to the way the kernel sends the event.

Quoted 7 lines
Quoted 5 lines

There are four uevents sent from the kernel in the creation of a SysFS representation of the device. After the last event, the SysFs entries for vendor,model, rev are populated. The window time between the uevent with /block/sdX sent and the sysfs attributes being populated is small.

What kernel version are you using? I thought we fixed all that long ago.

RHEL5.3. Which I thought had many of the fixes from upstream. Let me build the most recent version and see if I can trigger these issues on that version.

Quoted 8 lines

Thanks,
Kay

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

#6 lore raw

Re: [PATCH] Fix race-condition when reading sysfs attributes.

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

There are four uevents sent from the kernel in the creation of a SysFS representation of the device. After the last event, the SysFs entries for vendor,model, rev are populated. The window time between the uevent with /block/sdX sent and the sysfs attributes being populated is small.

What kernel version are you using? I thought we fixed all that long ago.

RHEL5.3. Which I thought had many of the fixes from upstream. Let me build the most recent version and see if I can trigger these issues on that version.

Kay,
You were right. I can't reproduce the failure on the more recent versions of kernels. Thanks for spotting that.