Skip to contentSkip to conversation

remove dangerous cleanup __attribute__ uses

PATCH In review Benjamin Marzinski 2 participants 11 messages Oct 11, 2022 – Oct 21, 2022 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

cover remove dangerous cleanup __attribute__ uses

the cleanup __attribute__ is only run when a variable goes out of scope normally. It is not run on pthread cancellation. This means that multipathd could leak whatever resources were supposed to be cleaned up if the thread was cancelled in a function using variables with the cleanup __attribute__. This patchset removes all these uses in cases where the code is run by multipathd and includes a cancellation point in the variables scope (usually condlog(), which calls fprintf(), a cancellation point, the way multipathd is usually run).

Benjamin Marzinski (4):
libmultipath: don't print garbage keywords
libmultipath: avoid STRBUF_ON_STACK with cancellation points
libmultipath: use regular array for field widths
libmultipath: avoid cleanup __attribute__ with cancellation points

 libmpathutil/parser.c                    | 13 ++--
 libmpathutil/strbuf.h                    |  4 +-
 libmultipath/alias.c                     | 59 ++++++++++-------
 libmultipath/blacklist.c                 |  4 +-
 libmultipath/configure.c                 |  6 +-
 libmultipath/discovery.c                 | 34 ++++++----
 libmultipath/dmparser.c                  | 23 +++----
 libmultipath/foreign.c                   |  9 +--
 libmultipath/generic.c                   | 14 ++--
 libmultipath/libmultipath.version        |  4 +-
 libmultipath/print.c                     | 82 ++++++++++++++----------
 libmultipath/print.h                     |  4 +-
 libmultipath/prioritizers/weightedpath.c | 22 ++++---
 libmultipath/propsel.c                   | 76 ++++++++++++++++------
 libmultipath/sysfs.h                     | 11 +---
 libmultipath/uevent.c                    |  6 +-
 multipath/main.c                         |  5 +-
 multipathd/cli_handlers.c                | 52 +++++++--------
 multipathd/main.c                        | 49 ++++++++------
 19 files changed, 286 insertions(+), 191 deletions(-)
Signature · 6 lines
2.17.2

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

PATCH 1/4 libmultipath: don't print garbage keywords

If snprint_keyword() failed to correctly set up sbuf, don't print it. Instead, return an error.

---
 libmpathutil/parser.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
libmpathutil/parser.c +5 -3
index 014d9b83..8d3ac53a 100644
@@ -152,7 +152,7 @@ int
 snprint_keyword(struct strbuf *buff, const char *fmt, struct keyword *kw,
 		const void *data)
 {
-	int r;
+	int r = 0;
 	char *f;
 	struct config *conf;
 	STRBUF_ON_STACK(sbuf);
@@ -190,8 +190,10 @@ snprint_keyword(struct strbuf *buff, const char *fmt, struct keyword *kw,
 		}
 	} while (*fmt++);
 out:
-	return __append_strbuf_str(buff, get_strbuf_str(&sbuf),
-				   get_strbuf_len(&sbuf));
+	if (r >= 0)
+		r = __append_strbuf_str(buff, get_strbuf_str(&sbuf),
+					get_strbuf_len(&sbuf));
+	return r;
 }
 
 static const char quote_marker[] = { '\0', '"', '\0' };
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
Reviewed-by on v1 1/4
#2 lore raw

Re: [dm-devel] [PATCH 1/4] libmultipath: don't print garbage keywords

Quoted 4 lines · Benjamin Marzinski

If snprint_keyword() failed to correctly set up sbuf, don't print it. Instead, return an error.

Quoted 24 lines

---
 libmpathutil/parser.c | 8 +++++---

 1 file changed, 5 insertions(+), 3 deletions(-)
libmpathutil/parser.c +0 -0
index 014d9b83..8d3ac53a 100644
@@ -152,7 +152,7 @@ int

 snprint_keyword(struct strbuf *buff, const char *fmt, struct keyword *kw,
                const void *data)
 {
-       int r;
+       int r = 0;
        char *f;
        struct config *conf;
        STRBUF_ON_STACK(sbuf);

libmpathutil/parser.c +0 -0
@@ -190,8 +190,10 @@ snprint_keyword(struct strbuf *buff, const char
 *fmt, struct keyword *kw,
                 }
         } while (*fmt++);

 out:
-       return __append_strbuf_str(buff, get_strbuf_str(&sbuf),

Quoted 8 lines

-                                  get_strbuf_len(&sbuf)); +       if (r >= 0)
+               r = __append_strbuf_str(buff, get_strbuf_str(&sbuf), +                                       get_strbuf_len(&sbuf)); +       return r;
 }

 static const char quote_marker[] = { '\0', '"', '\0' };

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

author maintainer
#3 lore raw

PATCH 2/4 libmultipath: avoid STRBUF_ON_STACK with cancellation points

In reply to Benjamin Marzinski

STRBUF_ON_STACK() uses the cleanup __attribute__, which doesn't get run if a thread is cancelled. condlog() will call fprintf() when run under systemd, which is a cancellation point. The snprint function for the generic mutipath and generic path operations both call cancellation points. Also, the keyword print functions can call cancellation points. Because of all this, I did not see any safe uses of STRBUF_ON_STACK() outside of the unit tests. Replace them all with pthread cleanup handlers.

---
 libmpathutil/parser.c                    |  5 +-
 libmpathutil/strbuf.h                    |  4 +-
 libmultipath/alias.c                     | 59 +++++++++++-------
 libmultipath/blacklist.c                 |  4 +-
 libmultipath/discovery.c                 | 34 +++++++----
 libmultipath/dmparser.c                  | 21 +++----
 libmultipath/foreign.c                   |  4 +-
 libmultipath/generic.c                   | 10 +++-
 libmultipath/print.c                     | 50 ++++++++++------
 libmultipath/prioritizers/weightedpath.c | 16 ++---
 libmultipath/propsel.c                   | 76 ++++++++++++++++++------
 libmultipath/sysfs.h                     | 11 +---
 libmultipath/uevent.c                    |  6 +-
 13 files changed, 195 insertions(+), 105 deletions(-)
libmpathutil/parser.c +4 -1
index 8d3ac53a..04c65b51 100644
@@ -25,6 +25,7 @@
 #include "parser.h"
 #include "debug.h"
 #include "strbuf.h"
+#include "util.h"
 
 /* local vars */
 static int sublevel = 0;
@@ -155,11 +156,12 @@ snprint_keyword(struct strbuf *buff, const char *fmt, struct keyword *kw,
 	int r = 0;
 	char *f;
 	struct config *conf;
-	STRBUF_ON_STACK(sbuf);
+	struct strbuf sbuf = STRBUF_INIT;
 
 	if (!kw || !kw->print)
 		return 0;
 
+	pthread_cleanup_push_cast(reset_strbuf, &sbuf);
 	do {
 		f = strchr(fmt, '%');
 		if (f == NULL) {
@@ -193,6 +195,7 @@ out:
 	if (r >= 0)
 		r = __append_strbuf_str(buff, get_strbuf_str(&sbuf),
 					get_strbuf_len(&sbuf));
+	pthread_cleanup_pop(1);
 	return r;
 }
 
libmpathutil/strbuf.h +3 -1
index ae863417..5aa54677 100644
@@ -42,7 +42,9 @@ void free_strbuf(struct strbuf *buf);
  * macro: STRBUF_ON_STACK
  *
  * Define and initialize a local struct @strbuf to be cleaned up when
- * the current scope is left
+ * the current scope is left. This can only be used in non-threaded
+ * programs, or if there are no pthread cancellation points in the
+ * current scope, after the buffer could first be used.
  */
 #define STRBUF_ON_STACK(__x)						\
 	struct strbuf __attribute__((cleanup(reset_strbuf))) (__x) = STRBUF_INIT;
libmultipath/alias.c +36 -23

131 lines not on this page · raw message

libmultipath/blacklist.c +3 -1
index 8d15d2ea..7e0b036a 100644
@@ -341,11 +341,12 @@ int
 filter_protocol(const struct _vector *blist, const struct _vector *elist,
 		const struct path *pp)
 {
-	STRBUF_ON_STACK(buf);
+	struct strbuf buf = STRBUF_INIT;
 	const char *prot;
 	int r = MATCH_NOTHING;
 
 	if (pp) {
+		pthread_cleanup_push_cast(reset_strbuf, &buf);
 		snprint_path_protocol(&buf, pp);
 		prot = get_strbuf_str(&buf);
 
@@ -354,6 +355,7 @@ filter_protocol(const struct _vector *blist, const struct _vector *elist,
 		else if (match_reglist(blist, prot))
 			r = MATCH_PROTOCOL_BLIST;
 		log_filter(pp->dev, NULL, NULL, NULL, NULL, prot, r, 3);
+		pthread_cleanup_pop(1);
 	}
 
 	return r;
libmultipath/discovery.c +22 -12
index f3fccedd..32181259 100644
@@ -838,13 +838,15 @@ static void
 scsi_tmo_error_msg(struct path *pp)
 {
 	STATIC_BITFIELD(bf, LAST_BUS_PROTOCOL_ID + 1);
-	STRBUF_ON_STACK(proto_buf);
+	struct strbuf proto_buf = STRBUF_INIT;
 	unsigned int proto_id = bus_protocol_id(pp);
 
+	pthread_cleanup_push_cast(reset_strbuf, &proto_buf);
 	snprint_path_protocol(&proto_buf, pp);
 	condlog(2, "%s: setting scsi timeouts is unsupported for protocol %s",
 		pp->dev, get_strbuf_str(&proto_buf));
 	set_bit_in_bitfield(proto_id, bf);
+	pthread_cleanup_pop(1);
 }
 
 int
@@ -1142,12 +1144,13 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	size_t len, vpd_len, i;
 	int vpd_type, prio = -1;
 	int err = -ENODATA;
-	STRBUF_ON_STACK(buf);
+	struct strbuf buf = STRBUF_INIT;
 
 	/* Need space at least for one digit */
 	if (out_len <= 1)
 		return 0;
 
+	pthread_cleanup_push_cast(reset_strbuf, &buf);
 	d = in + 4;
 	while (d <= in + in_len - 4) {
 		bool invalid = false;
@@ -1241,8 +1244,9 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 	}
 
 	if (prio <= 0)
-		return err;
+		goto out;
 
+	err = 0;
 	if (d != in + in_len)
 		/* Should this be fatal? (overflow covered above) */
 		condlog(2, "%s: warning: last descriptor end %zd != VPD length %zu",
@@ -1262,10 +1266,10 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 		size_t i;
 
 		if ((err = print_strbuf(&buf, "%d", vpd_type)) < 0)
-			return err;
+			goto out;
 		for (i = 0; i < vpd_len; i++)
 			if ((err = print_strbuf(&buf, "%02x", vpd[i])) < 0)
-				return err;
+				goto out;
 	} else if (vpd_type == 0x8) {
 		char type;
 
@@ -1276,12 +1280,12 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 		else
 			type = '8';
 		if ((err = fill_strbuf(&buf, type, 1)) < 0)
-			return err;
+			goto out;
 
 		vpd += 4;
 		len = vpd_len - 4;
 		if ((err = __append_strbuf_str(&buf, (const char *)vpd, len)) < 0)
-			return err;
+			goto out;
 
 		/* The input is 0-padded, make sure the length is correct */
 		truncate_strbuf(&buf, strlen(get_strbuf_str(&buf)));
@@ -1298,12 +1302,12 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 		size_t p_len;
 
 		if ((err = fill_strbuf(&buf, '1', 1)) < 0)
-			return err;
+			goto out;
 		while (vpd && (p = memchr(vpd, ' ', vpd_len))) {
 			p_len = p - vpd;
 			if ((err = __append_strbuf_str(&buf, (const char *)vpd,
 						       p_len)) < 0)
-				return err;
+				goto out;
 			vpd = p;
 			vpd_len -= p_len;
 			while (vpd && vpd_len > 0 && *vpd == ' ') {
@@ -1311,12 +1315,12 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 				vpd_len --;
 			}
 			if (vpd_len > 0 && (err = fill_strbuf(&buf, '_', 1)) < 0)
-				return err;
+				goto out;
 		}
 		if (vpd_len > 0) {
 			if ((err = __append_strbuf_str(&buf, (const char *)vpd,
 						       vpd_len)) < 0)
-				return err;
+				goto out;
 		}
 	}
 
@@ -1331,6 +1335,10 @@ parse_vpd_pg83(const unsigned char *in, size_t in_len,
 			len = out_len - 1;
 	}
 	strlcpy(out, get_strbuf_str(&buf), len + 1);
+out:
+	pthread_cleanup_pop(1);
+	if (err)
+		return err;
 	return len;
 }
 
@@ -2435,11 +2443,13 @@ int pathinfo(struct path *pp, struct config *conf, int mask)
 				 * It's likely that this path is not fit for
 				 * multipath use.
 				 */
-				STRBUF_ON_STACK(buf);
+				struct strbuf buf = STRBUF_INIT;
 
+				pthread_cleanup_push_cast(reset_strbuf, &buf);
 				snprint_path(&buf, "%T", pp, 0);
 				condlog(1, "%s: no WWID in state \"%s\", giving up",
 					pp->dev, get_strbuf_str(&buf));
+				pthread_cleanup_pop(1);
 				return PATHINFO_SKIPPED;
 			}
 			return PATHINFO_OK;
libmultipath/dmparser.c +11 -10
index 3b37a926..44650aaa 100644
@@ -48,10 +48,10 @@ int assemble_map(struct multipath *mp, char **params)
 {
 	static const char no_path_retry[] = "queue_if_no_path";
 	static const char retain_hwhandler[] = "retain_attached_hw_handler";
-	int i, j;
+	int i, j, r = 1;
 	int minio;
 	int nr_priority_groups, initial_pg_nr;
-	STRBUF_ON_STACK(buff);
+	struct strbuf buff = STRBUF_INIT;
 	struct pathgroup * pgp;
 	struct path * pp;
 
@@ -68,15 +68,16 @@ int assemble_map(struct multipath *mp, char **params)
 	    get_linux_version_code() < KERNEL_VERSION(4, 3, 0))
 		add_feature(&mp->features, retain_hwhandler);
 
+	pthread_cleanup_push_cast(reset_strbuf, &buff);
 	if (print_strbuf(&buff, "%s %s %i %i", mp->features, mp->hwhandler,
 			 nr_priority_groups, initial_pg_nr) < 0)
-		goto err;
+		goto out;
 
 	vector_foreach_slot (mp->pg, pgp, i) {
 		pgp = VECTOR_SLOT(mp->pg, i);
 		if (print_strbuf(&buff, " %s %i 1", mp->selector,
 				 VECTOR_SIZE(pgp->paths)) < 0)
-			goto err;
+			goto out;
 
 		vector_foreach_slot (pgp->paths, pp, j) {
 			int tmp_minio = minio;
@@ -86,19 +87,19 @@ int assemble_map(struct multipath *mp, char **params)
 				tmp_minio = minio * pp->priority;
 			if (!strlen(pp->dev_t) ) {
 				condlog(0, "dev_t not set for '%s'", pp->dev);
-				goto err;
+				goto out;
 			}
 			if (print_strbuf(&buff, " %s %d", pp->dev_t, tmp_minio) < 0)
-				goto err;
+				goto out;
 		}
 	}
 
 	*params = steal_strbuf_str(&buff);
 	condlog(4, "%s: assembled map [%s]", mp->alias, *params);
-	return 0;
-
-err:
-	return 1;
+	r = 0;
+out:
+	pthread_cleanup_pop(1);
+	return r;
 }
 
 /*
libmultipath/foreign.c +3 -1
index d01a5ef0..8981ff58 100644
@@ -547,7 +547,7 @@ int snprint_foreign_topology(struct strbuf *buf, int verbosity,
 
 void print_foreign_topology(int verbosity)
 {
-	STRBUF_ON_STACK(buf);
+	struct strbuf buf = STRBUF_INIT;
 	struct foreign *fgn;
 	int i;
 	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
@@ -559,6 +559,7 @@ void print_foreign_topology(int verbosity)
 		unlock_foreigns(NULL);
 		return;
 	}
+	pthread_cleanup_push_cast(reset_strbuf, &buf);
 	pthread_cleanup_push(unlock_foreigns, NULL);
 	vector_foreach_slot(foreigns, fgn, i) {
 		const struct _vector *vec;
@@ -573,6 +574,7 @@ void print_foreign_topology(int verbosity)
 	__snprint_foreign_topology(&buf, verbosity, width);
 	pthread_cleanup_pop(1);
 	printf("%s", get_strbuf_str(&buf));
+	pthread_cleanup_pop(1);
 }
 
 int snprint_foreign_paths(struct strbuf *buf, const char *style,
libmultipath/generic.c +7 -3
index e7cf2975..3e2268e6 100644
@@ -23,15 +23,19 @@
 int generic_style(const struct gen_multipath* gm, struct strbuf *buf,
 		  __attribute__((unused)) int verbosity)
 {
-	STRBUF_ON_STACK(tmp);
+	struct strbuf tmp = STRBUF_INIT;
 	char *alias_buf __attribute__((cleanup(cleanup_charp)));
 	const char *wwid_buf;
+	int ret;
 
+	pthread_cleanup_push_cast(reset_strbuf, &tmp);
 	gm->ops->snprint(gm, &tmp, 'n');
 	alias_buf = steal_strbuf_str(&tmp);
 	gm->ops->snprint(gm, &tmp, 'w');
 	wwid_buf = get_strbuf_str(&tmp);
 
-	return print_strbuf(buf, "%%n %s[%%G]:%%d %%s",
-			    strcmp(alias_buf, wwid_buf) ? "(%w) " : "");
+	ret = print_strbuf(buf, "%%n %s[%%G]:%%d %%s",
+			   strcmp(alias_buf, wwid_buf) ? "(%w) " : "");
+	pthread_cleanup_pop(1);
+	return ret;
 }
libmultipath/print.c +33 -17

156 lines not on this page · raw message

libmultipath/prioritizers/weightedpath.c +9 -7
index 561ebb48..df700bf3 100644
@@ -63,7 +63,7 @@ build_wwn_path(struct path *pp, struct strbuf *buf)
 /* main priority routine */
 int prio_path_weight(struct path *pp, char *prio_args)
 {
-	STRBUF_ON_STACK(path);
+	struct strbuf path = STRBUF_INIT;
 	char *arg __attribute__((cleanup(cleanup_charp))) = NULL;
 	char *temp, *regex, *prio;
 	char split_char[] = " \t";
@@ -84,24 +84,25 @@ int prio_path_weight(struct path *pp, char *prio_args)
 		return priority;
 	}
 
+	pthread_cleanup_push_cast(reset_strbuf, &path);
 	if (!strcmp(regex, HBTL)) {
 		if (print_strbuf(&path, "%d:%d:%d:%" PRIu64, pp->sg_id.host_no,
 				 pp->sg_id.channel, pp->sg_id.scsi_id,
 				 pp->sg_id.lun) < 0)
-			return priority;
+			goto out;
 	} else if (!strcmp(regex, DEV_NAME)) {
 		if (append_strbuf_str(&path, pp->dev) < 0)
-			return priority;
+			goto out;
 	} else if (!strcmp(regex, SERIAL)) {
 		if (build_serial_path(pp, &path) != 0)
-			return priority;
+			goto out;
 	} else if (!strcmp(regex, WWN)) {
 		if (build_wwn_path(pp, &path) != 0)
-			return priority;
+			goto out;
 	} else {
 		condlog(0, "%s: %s - Invalid arguments", pp->dev,
 			pp->prio.name);
-		return priority;
+		goto out;
 	}
 
 	while (!path_found) {
@@ -121,7 +122,8 @@ int prio_path_weight(struct path *pp, char *prio_args)
 			regfree(&pathe);
 		}
 	}
-
+out:
+	pthread_cleanup_pop(1);
 	return priority;
 }
 
libmultipath/propsel.c +57 -19

373 lines not on this page · raw message

libmultipath/sysfs.h +3 -8
index 799f68e9..7e80b411 100644
@@ -26,14 +26,9 @@ ssize_t sysfs_bin_attr_get_value(struct udev_device *dev, const char *attr_name,
 		sysfs_attr_value_ok(__rc, __l); \
 	})
 
-#define log_sysfs_attr_set_value(prio, rc, fmt, __args...)		\
-do {									\
-	STRBUF_ON_STACK(__buf);						\
-	if (print_strbuf(&__buf, fmt, ##__args) >= 0 &&			\
-	    print_strbuf(&__buf, ": %s", rc < 0 ? strerror(-rc) :	\
-					"write underflow") >= 0)	\
-		condlog(prio, "%s", get_strbuf_str(&__buf));		\
-} while(0)
+#define log_sysfs_attr_set_value(prio, rc, fmt, __args...) \
+	condlog(prio, fmt ": %s", ##__args , rc < 0 ? strerror(-rc) :	\
+					     "write underflow")
 
 int sysfs_get_size (struct path *pp, unsigned long long * size);
 int sysfs_check_holders(char * check_devt, char * new_devt);
libmultipath/uevent.c +4 -2
index 57447ca0..db6da984 100644
@@ -460,11 +460,12 @@ static void print_uevq(const char *msg, struct list_head *uevq)
 {
 	struct uevent *uev;
 	int i = 0;
-	STRBUF_ON_STACK(buf);
+	struct strbuf buf = STRBUF_INIT;
 
 	if (4 > MAX_VERBOSITY || 4 > libmp_verbosity)
 		return;
 
+	pthread_cleanup_push_cast(reset_strbuf, &buf);
 	if (list_empty(uevq))
 		append_strbuf_str(&buf, "*empty*");
 	else
@@ -473,7 +474,8 @@ static void print_uevq(const char *msg, struct list_head *uevq)
 			print_uev(&buf, uev);
 		}
 
-	condlog(4, "uevent queue (%s): %s", msg, steal_strbuf_str(&buf));
+	condlog(4, "uevent queue (%s): %s", msg, get_strbuf_str(&buf));
+	pthread_cleanup_pop(1);
 }
 
 static void
Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
author maintainer
#4 lore raw

PATCH 3/4 libmultipath: use regular array for field widths

In reply to Benjamin Marzinski

We know the size of these arrays, so we can just allocate them on the stack. Also, show_path() doesn't use the width, so don't initialize it in the first place.

---
 libmultipath/foreign.c            |  5 ++--
 libmultipath/libmultipath.version |  4 +--
 libmultipath/print.c              | 32 +++++++++++-------------
 libmultipath/print.h              |  4 +--
 multipath/main.c                  |  5 ++--
 multipathd/cli_handlers.c         | 41 ++++++++++++-------------------
 6 files changed, 38 insertions(+), 53 deletions(-)
libmultipath/foreign.c +2 -3
index 8981ff58..4cc2a8e3 100644
@@ -550,10 +550,9 @@ void print_foreign_topology(int verbosity)
 	struct strbuf buf = STRBUF_INIT;
 	struct foreign *fgn;
 	int i;
-	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+	fieldwidth_t width[path_layout_size()];
 
-	if ((width = alloc_path_layout()) == NULL)
-		return;
+	memset(width, 0, sizeof(width));
 	rdlock_foreigns();
 	if (foreigns == NULL) {
 		unlock_foreigns(NULL);
libmultipath/libmultipath.version +2 -2
index 8a447f7f..af7c5ed2 100644
@@ -38,9 +38,7 @@ global:
 	add_map_with_path;
 	adopt_paths;
 	alloc_multipath;
-	alloc_multipath_layout;
 	alloc_path;
-	alloc_path_layout;
 	alloc_path_with_pathinfo;
 	change_foreign;
 	check_alias_settings;
@@ -126,6 +124,7 @@ global:
 	libmultipath_exit;
 	libmultipath_init;
 	load_config;
+	multipath_layout_size;
 	need_io_err_check;
 	orphan_path;
 	parse_prkey_flags;
@@ -133,6 +132,7 @@ global:
 	path_discovery;
 	path_get_tpgs;
 	pathinfo;
+	path_layout_size;
 	path_offline;
 	print_all_paths;
 	print_foreign_topology;
libmultipath/print.c +14 -18
index 97f9a177..87d6a329 100644
@@ -805,6 +805,12 @@ static const struct multipath_data mpd[] = {
 	{'g', "vpd page data", snprint_multipath_vpd_data},
 };
 
+
+int multipath_layout_size(void)
+{
+	return ARRAY_SIZE(mpd);
+}
+
 static const struct path_data pd[] = {
 	{'w', "uuid",          snprint_path_uuid},
 	{'i', "hcil",          snprint_hcil},
@@ -834,6 +840,11 @@ static const struct path_data pd[] = {
 	{'L', "LUN hex",       snprint_path_lunhex},
 };
 
+int path_layout_size(void)
+{
+	return ARRAY_SIZE(pd);
+}
+
 static const struct pathgroup_data pgd[] = {
 	{'s', "selector",      snprint_pg_selector},
 	{'p', "pri",           snprint_pg_pri},
@@ -871,10 +882,6 @@ int snprint_wildcards(struct strbuf *buff)
 	return get_strbuf_len(buff) - initial_len;
 }
 
-fieldwidth_t *alloc_path_layout(void) {
-	return calloc(ARRAY_SIZE(pd), sizeof(fieldwidth_t));
-}
-
 void get_path_layout(vector pathvec, int header, fieldwidth_t *width)
 {
 	vector gpvec = vector_convert(NULL, pathvec, struct path,
@@ -929,11 +936,6 @@ void _get_path_layout (const struct _vector *gpvec, enum layout_reset reset,
 	}
 }
 
-fieldwidth_t *alloc_multipath_layout(void) {
-
-	return calloc(ARRAY_SIZE(mpd), sizeof(fieldwidth_t));
-}
-
 void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width) {
 	vector gmvec = vector_convert(NULL, mpvec, struct multipath,
 				      dm_multipath_to_gen);
@@ -1187,12 +1189,11 @@ int _snprint_pathgroup(const struct gen_pathgroup *ggp, struct strbuf *line,
 void _print_multipath_topology(const struct gen_multipath *gmp, int verbosity)
 {
 	struct strbuf buff = STRBUF_INIT;
-	fieldwidth_t *p_width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+	fieldwidth_t p_width[ARRAY_SIZE(pd)] = {0};
 	const struct gen_pathgroup *gpg;
 	const struct _vector *pgvec, *pathvec;
 	int j;
 
-	p_width = alloc_path_layout();
 	pgvec = gmp->ops->get_pathgroups(gmp);
 
 	if (pgvec != NULL) {
@@ -1236,14 +1237,11 @@ int _snprint_multipath_topology(const struct gen_multipath *gmp,
 	const struct gen_pathgroup *gpg;
 	struct strbuf style = STRBUF_INIT;
 	size_t initial_len = get_strbuf_len(buff);
-	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+	fieldwidth_t width[ARRAY_SIZE(mpd)] = {0};
 
 	if (verbosity <= 0)
 		return 0;
 
-	if ((width = alloc_multipath_layout()) == NULL)
-		return -ENOMEM;
-
 	if (verbosity == 1)
 		return _snprint_multipath(gmp, buff, "%n", width);
 
@@ -2027,7 +2025,7 @@ static void print_all_paths_custo(vector pathvec, int banner, const char *fmt)
 	int i;
 	struct path * pp;
 	struct strbuf line = STRBUF_INIT;
-	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+	fieldwidth_t width[ARRAY_SIZE(pd)] = {0};
 
 	if (!VECTOR_SIZE(pathvec)) {
 		if (banner)
@@ -2035,8 +2033,6 @@ static void print_all_paths_custo(vector pathvec, int banner, const char *fmt)
 		return;
 	}
 
-	if ((width = alloc_path_layout()) == NULL)
-		return;
 	get_path_layout(pathvec, 1, width);
 
 	pthread_cleanup_push_cast(reset_strbuf, &line);
libmultipath/print.h +2 -2
index 52f5b256..4e50827d 100644
@@ -16,11 +16,11 @@ enum layout_reset {
 };
 
 /* fieldwidth_t is defined in generic.h */
-fieldwidth_t *alloc_path_layout(void);
+int multipath_layout_size(void);
+int path_layout_size(void);
 void _get_path_layout (const struct _vector *gpvec, enum layout_reset,
 		       fieldwidth_t *width);
 void get_path_layout (vector pathvec, int header, fieldwidth_t *width);
-fieldwidth_t *alloc_multipath_layout(void);
 void _get_multipath_layout (const struct _vector *gmvec, enum layout_reset,
 			    fieldwidth_t *width);
 void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width);
multipath/main.c +2 -3
index 7b69a3ce..f4c85409 100644
@@ -457,7 +457,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	int di_flag = 0;
 	char * refwwid = NULL;
 	char * dev = NULL;
-	fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+	fieldwidth_t width[path_layout_size()];
 
 	/*
 	 * allocate core vectors to store paths and multipaths
@@ -544,8 +544,7 @@ configure (struct config *conf, enum mpath_cmds cmd,
 	if (libmp_verbosity > 2)
 		print_all_paths(pathvec, 1);
 
-	if ((width = alloc_path_layout()) == NULL)
-		goto out;
+	memset(width, 0, sizeof(width));
 	get_path_layout(pathvec, 0, width);
 	foreign_path_layout(width);
 
multipathd/cli_handlers.c +16 -25

100 lines not on this page · raw message

Signature · 6 lines
2.17.2

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

Re: [dm-devel] [PATCH 3/4] libmultipath: use regular array for field widths

Quoted 6 lines · Benjamin Marzinski

We know the size of these arrays, so we can just allocate them on the stack. Also, show_path() doesn't use the width, so don't initialize it
in the first place.

This isn't wrong, but I'm not sure what it actually achieves except a few less NULL checks (I'm sure you're aware that this doesn't mean better protection against out-of-memory situations). It comes at the cost of an ABI change. I understand that the intention is to eliminate __attribute__((cleanup())). But if we agree we don't want to do that everywhere, I see no particular reason to do it in this code path.

I'm not totally against it, but I'm not enthusiastic, either.

Regards,
Martin

Quoted 340 lines

---
 libmultipath/foreign.c            |  5 ++-- libmultipath/libmultipath.version |  4 +-- libmultipath/print.c              | 32 +++++++++++------------- libmultipath/print.h              |  4 +-- multipath/main.c                  |  5 ++-- multipathd/cli_handlers.c         | 41 ++++++++++++----------------- --

 6 files changed, 38 insertions(+), 53 deletions(-)
libmultipath/foreign.c +1 -1
index 8981ff58..4cc2a8e3 100644
@@ -550,10 +550,9 @@ void print_foreign_topology(int verbosity)
         struct strbuf buf = STRBUF_INIT;
         struct foreign *fgn;
         int i;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[path_layout_size()];


-       if ((width = alloc_path_layout()) == NULL) -               return;
+       memset(width, 0, sizeof(width)); rdlock_foreigns();
        if (foreigns == NULL) { unlock_foreigns(NULL);

libmultipath/libmultipath.version +2 -2
index 8a447f7f..af7c5ed2 100644
@@ -38,9 +38,7 @@ global:
         add_map_with_path;
         adopt_paths;
         alloc_multipath;
-       alloc_multipath_layout;
         alloc_path;
-       alloc_path_layout;
         alloc_path_with_pathinfo;
         change_foreign;
         check_alias_settings;
@@ -126,6 +124,7 @@ global:
         libmultipath_exit;
         libmultipath_init;
         load_config;
+       multipath_layout_size;
         need_io_err_check;
         orphan_path;
         parse_prkey_flags;
@@ -133,6 +132,7 @@ global:
         path_discovery;
         path_get_tpgs;
         pathinfo;
+       path_layout_size;
         path_offline;
         print_all_paths;
         print_foreign_topology;
libmultipath/print.c +0 -0
index 97f9a177..87d6a329 100644
@@ -805,6 +805,12 @@ static const struct multipath_data mpd[] = {
         {'g', "vpd page data", snprint_multipath_vpd_data},
  };

libmultipath/print.c +6 -0
+
+int multipath_layout_size(void)
+{
+       return ARRAY_SIZE(mpd);
+}
+

 static const struct path_data pd[] = { {'w', "uuid",          snprint_path_uuid}, {'i', "hcil",          snprint_hcil},

libmultipath/print.c +0 -0
@@ -834,6 +840,11 @@ static const struct path_data pd[] = {
         {'L', "LUN hex",       snprint_path_lunhex},
  };

libmultipath/print.c +5 -0
+int path_layout_size(void)
+{
+       return ARRAY_SIZE(pd);
+}
+

 static const struct pathgroup_data pgd[] = { {'s', "selector",      snprint_pg_selector}, {'p', "pri",           snprint_pg_pri},

libmultipath/print.c +0 -0
@@ -871,10 +882,6 @@ int snprint_wildcards(struct strbuf *buff)
         return get_strbuf_len(buff) - initial_len;
  }

libmultipath/print.c +0 -4
-fieldwidth_t *alloc_path_layout(void) {
-       return calloc(ARRAY_SIZE(pd), sizeof(fieldwidth_t));
-}
-

 void get_path_layout(vector pathvec, int header, fieldwidth_t *width)
 {
        vector gpvec = vector_convert(NULL, pathvec, struct path,

libmultipath/print.c +0 -0
@@ -929,11 +936,6 @@ void _get_path_layout (const struct _vector
 *gpvec, enum layout_reset reset,
         }
  }

libmultipath/print.c +0 -5
-fieldwidth_t *alloc_multipath_layout(void) {
-
-       return calloc(ARRAY_SIZE(mpd), sizeof(fieldwidth_t));
-}
-

 void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width) {
        vector gmvec = vector_convert(NULL, mpvec, struct multipath, dm_multipath_to_gen);

libmultipath/print.c +0 -0
@@ -1187,12 +1189,11 @@ int _snprint_pathgroup(const struct
 gen_pathgroup *ggp, struct strbuf *line,

 void _print_multipath_topology(const struct gen_multipath *gmp, int verbosity)
 {
        struct strbuf buff = STRBUF_INIT;
-       fieldwidth_t *p_width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+       fieldwidth_t p_width[ARRAY_SIZE(pd)] = {0};
        const struct gen_pathgroup *gpg;
        const struct _vector *pgvec, *pathvec;
        int j;

-       p_width = alloc_path_layout();
        pgvec = gmp->ops->get_pathgroups(gmp);

        if (pgvec != NULL) {

libmultipath/print.c +1 -1
@@ -1236,14 +1237,11 @@ int _snprint_multipath_topology(const struct
 gen_multipath *gmp,
         const struct gen_pathgroup *gpg;
         struct strbuf style = STRBUF_INIT;
         size_t initial_len = get_strbuf_len(buff);
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[ARRAY_SIZE(mpd)] = {0};


        if (verbosity <= 0)
                return 0;

-       if ((width = alloc_multipath_layout()) == NULL) -               return -ENOMEM;
-
        if (verbosity == 1)
                return _snprint_multipath(gmp, buff, "%n", width);

libmultipath/print.c +1 -1
@@ -2027,7 +2025,7 @@ static void print_all_paths_custo(vector
 pathvec, int banner, const char *fmt)
         int i;
         struct path * pp;
         struct strbuf line = STRBUF_INIT;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[ARRAY_SIZE(pd)] = {0};


        if (!VECTOR_SIZE(pathvec)) {
                if (banner)

libmultipath/print.c +0 -0
@@ -2035,8 +2033,6 @@ static void print_all_paths_custo(vector
 pathvec, int banner, const char *fmt)
                 return;
         }


-       if ((width = alloc_path_layout()) == NULL) -               return; get_path_layout(pathvec, 1, width);

        pthread_cleanup_push_cast(reset_strbuf, &line);

libmultipath/print.h +0 -0
index 52f5b256..4e50827d 100644
@@ -16,11 +16,11 @@ enum layout_reset {
  };


 /* fieldwidth_t is defined in generic.h */

libmultipath/print.h +2 -1
-fieldwidth_t *alloc_path_layout(void);
+int multipath_layout_size(void);
+int path_layout_size(void);

 void _get_path_layout (const struct _vector *gpvec, enum layout_reset,
                       fieldwidth_t *width);
 void get_path_layout (vector pathvec, int header, fieldwidth_t *width);
-fieldwidth_t *alloc_multipath_layout(void);
 void _get_multipath_layout (const struct _vector *gmvec, enum layout_reset,
                            fieldwidth_t *width);
 void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width);

multipath/main.c +1 -1
index 7b69a3ce..f4c85409 100644
@@ -457,7 +457,7 @@ configure (struct config *conf, enum mpath_cmds
 cmd,
         int di_flag = 0;
         char * refwwid = NULL;
         char * dev = NULL;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[path_layout_size()];


        /*
         * allocate core vectors to store paths and multipaths

multipath/main.c +0 -0
@@ -544,8 +544,7 @@ configure (struct config *conf, enum mpath_cmds
 cmd,
         if (libmp_verbosity > 2)
                 print_all_paths(pathvec, 1);


-       if ((width = alloc_path_layout()) == NULL) -               goto out;
+       memset(width, 0, sizeof(width)); get_path_layout(pathvec, 0, width); foreign_path_layout(width);

multipathd/cli_handlers.c +1 -1
index 5b8f647b..ddc807a1 100644
@@ -38,11 +38,10 @@ show_paths (struct strbuf *reply, struct vectors
 *vecs, char *style, int pretty)
         int i;
         struct path * pp;
         int hdr_len = 0;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[path_layout_size()];


+       memset(width, 0, sizeof(width));
        if (pretty) {
-               if ((width = alloc_path_layout()) == NULL) -                       return 1; get_path_layout(vecs->pathvec, 1, width); foreign_path_layout(width);
        }

multipathd/cli_handlers.c +0 -0
@@ -50,10 +49,10 @@ show_paths (struct strbuf *reply, struct vectors
 *vecs, char *style, int pretty)
                 return 1;


        vector_foreach_slot(vecs->pathvec, pp, i) {
-               if (snprint_path(reply, style, pp, width) < 0) +               if (snprint_path(reply, style, pp, pretty? width :
NULL) < 0)
                        return 1;
        }
-       if (snprint_foreign_paths(reply, style, width) < 0) +       if (snprint_foreign_paths(reply, style, pretty? width : NULL) < 0)
                return 1;

        if (pretty && get_strbuf_len(reply) == (size_t)hdr_len)

multipathd/cli_handlers.c +0 -0
@@ -67,12 +66,7 @@ static int

 show_path (struct strbuf *reply, struct vectors *vecs, struct path *pp,
           char *style)
 {
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
-
-       if ((width = alloc_path_layout()) == NULL)
-               return 1;
-       get_path_layout(vecs->pathvec, 1, width);
-       if (snprint_path(reply, style, pp, 0) < 0)
+       if (snprint_path(reply, style, pp, NULL) < 0) return 1;
        return 0;
 }

multipathd/cli_handlers.c +1 -1
@@ -95,10 +89,9 @@ show_maps_topology (struct strbuf *reply, struct
 vectors * vecs)
  {
         int i;
         struct multipath * mpp;
-       fieldwidth_t *p_width
 __attribute__((cleanup(cleanup_ucharp))) = NULL;
+       fieldwidth_t p_width[path_layout_size()];


-       if ((p_width = alloc_path_layout()) == NULL) -               return 1;
+       memset(p_width, 0, sizeof(p_width)); get_path_layout(vecs->pathvec, 0, p_width); foreign_path_layout(p_width);

multipathd/cli_handlers.c +1 -1
@@ -258,10 +251,9 @@ cli_list_map_topology (void *v, struct strbuf
 *reply, void *data)
         struct multipath * mpp;
         struct vectors * vecs = (struct vectors *)data;
         char * param = get_keyparam(v, MAP);
-       fieldwidth_t *p_width
 __attribute__((cleanup(cleanup_ucharp))) = NULL;
+       fieldwidth_t p_width[path_layout_size()];


-       if ((p_width = alloc_path_layout()) == NULL) -               return 1;
+       memset(p_width, 0, sizeof(p_width)); get_path_layout(vecs->pathvec, 0, p_width); param = convert_dev(param, 0);
        mpp = find_mp_by_str(vecs->mpvec, param);

multipathd/cli_handlers.c +1 -1
@@ -357,11 +349,10 @@ show_maps (struct strbuf *reply, struct vectors
 *vecs, char *style,
         int i;
         struct multipath * mpp;
         int hdr_len = 0;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[multipath_layout_size()];


+       memset(width, 0, sizeof(width));
        if (pretty) {
-               if ((width = alloc_multipath_layout()) == NULL) -                       return 1; get_multipath_layout(vecs->mpvec, 1, width); foreign_multipath_layout(width);
        }

multipathd/cli_handlers.c +3 -2
@@ -374,10 +365,11 @@ show_maps (struct strbuf *reply, struct vectors
 *vecs, char *style,
                         i--;
                         continue;
                 }
-               if (snprint_multipath(reply, style, mpp, width) < 0)
+               if (snprint_multipath(reply, style, mpp,
+                                     pretty? width : NULL) < 0)
                         return 1;
         }
-       if (snprint_foreign_multipaths(reply, style, width) < 0)
+       if (snprint_foreign_multipaths(reply, style, pretty? width :
 NULL) < 0)
                 return 1;


        if (pretty && get_strbuf_len(reply) == (size_t)hdr_len)

multipathd/cli_handlers.c +1 -1
@@ -416,10 +408,9 @@ cli_list_map_fmt (void *v, struct strbuf *reply,
 void *data)
         struct vectors * vecs = (struct vectors *)data;
         char * param = get_keyparam(v, MAP);
         char * fmt = get_keyparam(v, FMT);
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[multipath_layout_size()];
Quoted 7 lines


-       if ((width = alloc_multipath_layout()) == NULL) -               return 1;
+       memset(width, 0, sizeof(width)); get_multipath_layout(vecs->mpvec, 1, width); param = convert_dev(param, 0);
        mpp = find_mp_by_str(vecs->mpvec, param);

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

author maintainer
#6 lore raw

Re: [dm-devel] [PATCH 3/4] libmultipath: use regular array for field widths

Quoted 8 lines · Martin Wilck
Quoted 6 lines · Benjamin Marzinski

We know the size of these arrays, so we can just allocate them on the stack. Also, show_path() doesn't use the width, so don't initialize it
in the first place.

Quoted 8 lines

This isn't wrong, but I'm not sure what it actually achieves except a few less NULL checks (I'm sure you're aware that this doesn't mean better protection against out-of-memory situations). It comes at the cost of an ABI change. I understand that the intention is to eliminate __attribute__((cleanup())). But if we agree we don't want to do that everywhere, I see no particular reason to do it in this code path.

I'm not totally against it, but I'm not enthusiastic, either.

That's fine. How about I send a patch to just fix show_path().

-Ben

Quoted 345 lines

Regards,
Martin

Quoted 339 lines

---
 libmultipath/foreign.c            |  5 ++-- libmultipath/libmultipath.version |  4 +-- libmultipath/print.c              | 32 +++++++++++------------- libmultipath/print.h              |  4 +-- multipath/main.c                  |  5 ++-- multipathd/cli_handlers.c         | 41 ++++++++++++----------------- --

 6 files changed, 38 insertions(+), 53 deletions(-)
libmultipath/foreign.c +1 -1
index 8981ff58..4cc2a8e3 100644
@@ -550,10 +550,9 @@ void print_foreign_topology(int verbosity)
         struct strbuf buf = STRBUF_INIT;
         struct foreign *fgn;
         int i;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[path_layout_size()];


-       if ((width = alloc_path_layout()) == NULL) -               return;
+       memset(width, 0, sizeof(width)); rdlock_foreigns();
        if (foreigns == NULL) { unlock_foreigns(NULL);

libmultipath/libmultipath.version +2 -2
index 8a447f7f..af7c5ed2 100644
@@ -38,9 +38,7 @@ global:
         add_map_with_path;
         adopt_paths;
         alloc_multipath;
-       alloc_multipath_layout;
         alloc_path;
-       alloc_path_layout;
         alloc_path_with_pathinfo;
         change_foreign;
         check_alias_settings;
@@ -126,6 +124,7 @@ global:
         libmultipath_exit;
         libmultipath_init;
         load_config;
+       multipath_layout_size;
         need_io_err_check;
         orphan_path;
         parse_prkey_flags;
@@ -133,6 +132,7 @@ global:
         path_discovery;
         path_get_tpgs;
         pathinfo;
+       path_layout_size;
         path_offline;
         print_all_paths;
         print_foreign_topology;
libmultipath/print.c +0 -0
index 97f9a177..87d6a329 100644
@@ -805,6 +805,12 @@ static const struct multipath_data mpd[] = {
         {'g', "vpd page data", snprint_multipath_vpd_data},
  };

libmultipath/print.c +6 -0
+
+int multipath_layout_size(void)
+{
+       return ARRAY_SIZE(mpd);
+}
+

 static const struct path_data pd[] = { {'w', "uuid",          snprint_path_uuid}, {'i', "hcil",          snprint_hcil},

libmultipath/print.c +0 -0
@@ -834,6 +840,11 @@ static const struct path_data pd[] = {
         {'L', "LUN hex",       snprint_path_lunhex},
  };

libmultipath/print.c +5 -0
+int path_layout_size(void)
+{
+       return ARRAY_SIZE(pd);
+}
+

 static const struct pathgroup_data pgd[] = { {'s', "selector",      snprint_pg_selector}, {'p', "pri",           snprint_pg_pri},

libmultipath/print.c +0 -0
@@ -871,10 +882,6 @@ int snprint_wildcards(struct strbuf *buff)
         return get_strbuf_len(buff) - initial_len;
  }

libmultipath/print.c +0 -4
-fieldwidth_t *alloc_path_layout(void) {
-       return calloc(ARRAY_SIZE(pd), sizeof(fieldwidth_t));
-}
-

 void get_path_layout(vector pathvec, int header, fieldwidth_t *width)
 {
        vector gpvec = vector_convert(NULL, pathvec, struct path,

libmultipath/print.c +0 -0
@@ -929,11 +936,6 @@ void _get_path_layout (const struct _vector
 *gpvec, enum layout_reset reset,
         }
  }

libmultipath/print.c +0 -5
-fieldwidth_t *alloc_multipath_layout(void) {
-
-       return calloc(ARRAY_SIZE(mpd), sizeof(fieldwidth_t));
-}
-

 void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width) {
        vector gmvec = vector_convert(NULL, mpvec, struct multipath, dm_multipath_to_gen);

libmultipath/print.c +0 -0
@@ -1187,12 +1189,11 @@ int _snprint_pathgroup(const struct
 gen_pathgroup *ggp, struct strbuf *line,

 void _print_multipath_topology(const struct gen_multipath *gmp, int verbosity)
 {
        struct strbuf buff = STRBUF_INIT;
-       fieldwidth_t *p_width __attribute__((cleanup(cleanup_ucharp))) = NULL;
+       fieldwidth_t p_width[ARRAY_SIZE(pd)] = {0};
        const struct gen_pathgroup *gpg;
        const struct _vector *pgvec, *pathvec;
        int j;

-       p_width = alloc_path_layout();
        pgvec = gmp->ops->get_pathgroups(gmp);

        if (pgvec != NULL) {

libmultipath/print.c +1 -1
@@ -1236,14 +1237,11 @@ int _snprint_multipath_topology(const struct
 gen_multipath *gmp,
         const struct gen_pathgroup *gpg;
         struct strbuf style = STRBUF_INIT;
         size_t initial_len = get_strbuf_len(buff);
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[ARRAY_SIZE(mpd)] = {0};


        if (verbosity <= 0)
                return 0;

-       if ((width = alloc_multipath_layout()) == NULL) -               return -ENOMEM;
-
        if (verbosity == 1)
                return _snprint_multipath(gmp, buff, "%n", width);

libmultipath/print.c +1 -1
@@ -2027,7 +2025,7 @@ static void print_all_paths_custo(vector
 pathvec, int banner, const char *fmt)
         int i;
         struct path * pp;
         struct strbuf line = STRBUF_INIT;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[ARRAY_SIZE(pd)] = {0};


        if (!VECTOR_SIZE(pathvec)) {
                if (banner)

libmultipath/print.c +0 -0
@@ -2035,8 +2033,6 @@ static void print_all_paths_custo(vector
 pathvec, int banner, const char *fmt)
                 return;
         }


-       if ((width = alloc_path_layout()) == NULL) -               return; get_path_layout(pathvec, 1, width);

        pthread_cleanup_push_cast(reset_strbuf, &line);

libmultipath/print.h +0 -0
index 52f5b256..4e50827d 100644
@@ -16,11 +16,11 @@ enum layout_reset {
  };


 /* fieldwidth_t is defined in generic.h */

libmultipath/print.h +2 -1
-fieldwidth_t *alloc_path_layout(void);
+int multipath_layout_size(void);
+int path_layout_size(void);

 void _get_path_layout (const struct _vector *gpvec, enum layout_reset,
                       fieldwidth_t *width);
 void get_path_layout (vector pathvec, int header, fieldwidth_t *width);
-fieldwidth_t *alloc_multipath_layout(void);
 void _get_multipath_layout (const struct _vector *gmvec, enum layout_reset,
                            fieldwidth_t *width);
 void get_multipath_layout (vector mpvec, int header, fieldwidth_t *width);

multipath/main.c +1 -1
index 7b69a3ce..f4c85409 100644
@@ -457,7 +457,7 @@ configure (struct config *conf, enum mpath_cmds
 cmd,
         int di_flag = 0;
         char * refwwid = NULL;
         char * dev = NULL;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[path_layout_size()];


        /*
         * allocate core vectors to store paths and multipaths

multipath/main.c +0 -0
@@ -544,8 +544,7 @@ configure (struct config *conf, enum mpath_cmds
 cmd,
         if (libmp_verbosity > 2)
                 print_all_paths(pathvec, 1);


-       if ((width = alloc_path_layout()) == NULL) -               goto out;
+       memset(width, 0, sizeof(width)); get_path_layout(pathvec, 0, width); foreign_path_layout(width);

multipathd/cli_handlers.c +1 -1
index 5b8f647b..ddc807a1 100644
@@ -38,11 +38,10 @@ show_paths (struct strbuf *reply, struct vectors
 *vecs, char *style, int pretty)
         int i;
         struct path * pp;
         int hdr_len = 0;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[path_layout_size()];


+       memset(width, 0, sizeof(width));
        if (pretty) {
-               if ((width = alloc_path_layout()) == NULL) -                       return 1; get_path_layout(vecs->pathvec, 1, width); foreign_path_layout(width);
        }

multipathd/cli_handlers.c +0 -0
@@ -50,10 +49,10 @@ show_paths (struct strbuf *reply, struct vectors
 *vecs, char *style, int pretty)
                 return 1;


        vector_foreach_slot(vecs->pathvec, pp, i) {
-               if (snprint_path(reply, style, pp, width) < 0) +               if (snprint_path(reply, style, pp, pretty? width :
NULL) < 0)
                        return 1;
        }
-       if (snprint_foreign_paths(reply, style, width) < 0) +       if (snprint_foreign_paths(reply, style, pretty? width : NULL) < 0)
                return 1;

        if (pretty && get_strbuf_len(reply) == (size_t)hdr_len)

multipathd/cli_handlers.c +0 -0
@@ -67,12 +66,7 @@ static int

 show_path (struct strbuf *reply, struct vectors *vecs, struct path *pp,
           char *style)
 {
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp))) = NULL;
-
-       if ((width = alloc_path_layout()) == NULL)
-               return 1;
-       get_path_layout(vecs->pathvec, 1, width);
-       if (snprint_path(reply, style, pp, 0) < 0)
+       if (snprint_path(reply, style, pp, NULL) < 0) return 1;
        return 0;
 }

multipathd/cli_handlers.c +1 -1
@@ -95,10 +89,9 @@ show_maps_topology (struct strbuf *reply, struct
 vectors * vecs)
  {
         int i;
         struct multipath * mpp;
-       fieldwidth_t *p_width
 __attribute__((cleanup(cleanup_ucharp))) = NULL;
+       fieldwidth_t p_width[path_layout_size()];


-       if ((p_width = alloc_path_layout()) == NULL) -               return 1;
+       memset(p_width, 0, sizeof(p_width)); get_path_layout(vecs->pathvec, 0, p_width); foreign_path_layout(p_width);

multipathd/cli_handlers.c +1 -1
@@ -258,10 +251,9 @@ cli_list_map_topology (void *v, struct strbuf
 *reply, void *data)
         struct multipath * mpp;
         struct vectors * vecs = (struct vectors *)data;
         char * param = get_keyparam(v, MAP);
-       fieldwidth_t *p_width
 __attribute__((cleanup(cleanup_ucharp))) = NULL;
+       fieldwidth_t p_width[path_layout_size()];


-       if ((p_width = alloc_path_layout()) == NULL) -               return 1;
+       memset(p_width, 0, sizeof(p_width)); get_path_layout(vecs->pathvec, 0, p_width); param = convert_dev(param, 0);
        mpp = find_mp_by_str(vecs->mpvec, param);

multipathd/cli_handlers.c +1 -1
@@ -357,11 +349,10 @@ show_maps (struct strbuf *reply, struct vectors
 *vecs, char *style,
         int i;
         struct multipath * mpp;
         int hdr_len = 0;
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
+       fieldwidth_t width[multipath_layout_size()];


+       memset(width, 0, sizeof(width));
        if (pretty) {
-               if ((width = alloc_multipath_layout()) == NULL) -                       return 1; get_multipath_layout(vecs->mpvec, 1, width); foreign_multipath_layout(width);
        }

multipathd/cli_handlers.c +3 -2
@@ -374,10 +365,11 @@ show_maps (struct strbuf *reply, struct vectors
 *vecs, char *style,
                         i--;
                         continue;
                 }
-               if (snprint_multipath(reply, style, mpp, width) < 0)
+               if (snprint_multipath(reply, style, mpp,
+                                     pretty? width : NULL) < 0)
                         return 1;
         }
-       if (snprint_foreign_multipaths(reply, style, width) < 0)
+       if (snprint_foreign_multipaths(reply, style, pretty? width :
 NULL) < 0)
                 return 1;


        if (pretty && get_strbuf_len(reply) == (size_t)hdr_len)

multipathd/cli_handlers.c +0 -1
@@ -416,10 +408,9 @@ cli_list_map_fmt (void *v, struct strbuf *reply,
 void *data)
         struct vectors * vecs = (struct vectors *)data;
         char * param = get_keyparam(v, MAP);
         char * fmt = get_keyparam(v, FMT);
-       fieldwidth_t *width __attribute__((cleanup(cleanup_ucharp)))
 = NULL;
Quoted 8 lines
Quoted 8 lines
multipathd/cli_handlers.c +1 -0
+       fieldwidth_t width[multipath_layout_size()];


-       if ((width = alloc_multipath_layout()) == NULL) -               return 1;
+       memset(width, 0, sizeof(width)); get_multipath_layout(vecs->mpvec, 1, width); param = convert_dev(param, 0);
        mpp = find_mp_by_str(vecs->mpvec, param);

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

#7 lore raw

Re: [dm-devel] [PATCH 3/4] libmultipath: use regular array for field widths

Quoted 22 lines · Benjamin Marzinski
Quoted 21 lines · Martin Wilck
Quoted 8 lines · Benjamin Marzinski

We know the size of these arrays, so we can just allocate them on the
stack. Also, show_path() doesn't use the width, so don't initialize
it
in the first place.

This isn't wrong, but I'm not sure what it actually achieves except a
few less NULL checks (I'm sure you're aware that this doesn't mean better protection against out-of-memory situations). It comes at the
cost of an ABI change. I understand that the intention is to eliminate
__attribute__((cleanup())). But if we agree we don't want to do that
everywhere, I see no particular reason to do it in this code path.

Quoted 3 lines
Quoted 1 line

I'm not totally against it, but I'm not enthusiastic, either.

That's fine. How about I send a patch to just fix show_path().

Yes, please.

Martin

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

author maintainer
#8 lore raw

PATCH 4/4 libmultipath: avoid cleanup __attribute__ with cancellation points

In reply to Benjamin Marzinski

the cleanup __attribute__ doesn't get run when a thread is cancelled, so it is only safe in cases where there aren't pthreads or no cancellation points happen in the code block after the variable needs cleaning up.

---
 libmultipath/configure.c                 |  6 +--
 libmultipath/dmparser.c                  |  2 +-
 libmultipath/generic.c                   |  4 +-
 libmultipath/prioritizers/weightedpath.c |  6 ++-
 multipathd/cli_handlers.c                | 11 ++++--
 multipathd/main.c                        | 49 ++++++++++++++----------
 6 files changed, 48 insertions(+), 30 deletions(-)
libmultipath/configure.c +3 -3
index e5249fc1..25708257 100644
@@ -1075,7 +1075,7 @@ int coalesce_paths (struct vectors *vecs, vector mpvec, char *refwwid,
 	int ret = CP_FAIL;
 	int k, i, r;
 	int is_daemon = (cmd == CMD_NONE) ? 1 : 0;
-	char *params __attribute__((cleanup(cleanup_charp))) = NULL;
+	char *params = NULL;
 	struct multipath * mpp;
 	struct path * pp1 = NULL;
 	struct path * pp2;
@@ -1208,6 +1208,7 @@ int coalesce_paths (struct vectors *vecs, vector mpvec, char *refwwid,
 			remove_map(mpp, vecs->pathvec, NULL);
 			continue;
 		}
+		pthread_cleanup_push(cleanup_free_ptr, &params);
 
 		if (cmd == CMD_DRY_RUN)
 			mpp->action = ACT_DRY_RUN;
@@ -1216,8 +1217,7 @@ int coalesce_paths (struct vectors *vecs, vector mpvec, char *refwwid,
 				      force_reload == FORCE_RELOAD_YES ? 1 : 0);
 
 		r = domap(mpp, params, is_daemon);
-		free(params);
-		params = NULL;
+		pthread_cleanup_pop(1);
 
 		if (r == DOMAP_FAIL || r == DOMAP_RETRY) {
 			condlog(3, "%s: domap (%u) failure "
libmultipath/dmparser.c +1 -1
index 44650aaa..066a33af 100644
@@ -94,8 +94,8 @@ int assemble_map(struct multipath *mp, char **params)
 		}
 	}
 
+	condlog(4, "%s: assembled map [%s]", mp->alias, get_strbuf_str(&buff));
 	*params = steal_strbuf_str(&buff);
-	condlog(4, "%s: assembled map [%s]", mp->alias, *params);
 	r = 0;
 out:
 	pthread_cleanup_pop(1);
libmultipath/generic.c +3 -1
index 3e2268e6..cdee21d9 100644
@@ -24,11 +24,12 @@ int generic_style(const struct gen_multipath* gm, struct strbuf *buf,
 		  __attribute__((unused)) int verbosity)
 {
 	struct strbuf tmp = STRBUF_INIT;
-	char *alias_buf __attribute__((cleanup(cleanup_charp)));
+	char *alias_buf = NULL;
 	const char *wwid_buf;
 	int ret;
 
 	pthread_cleanup_push_cast(reset_strbuf, &tmp);
+	pthread_cleanup_push(cleanup_free_ptr, &alias_buf);
 	gm->ops->snprint(gm, &tmp, 'n');
 	alias_buf = steal_strbuf_str(&tmp);
 	gm->ops->snprint(gm, &tmp, 'w');
@@ -37,5 +38,6 @@ int generic_style(const struct gen_multipath* gm, struct strbuf *buf,
 	ret = print_strbuf(buf, "%%n %s[%%G]:%%d %%s",
 			   strcmp(alias_buf, wwid_buf) ? "(%w) " : "");
 	pthread_cleanup_pop(1);
+	pthread_cleanup_pop(1);
 	return ret;
 }
libmultipath/prioritizers/weightedpath.c +4 -2
index df700bf3..02d40a3d 100644
@@ -64,7 +64,7 @@ build_wwn_path(struct path *pp, struct strbuf *buf)
 int prio_path_weight(struct path *pp, char *prio_args)
 {
 	struct strbuf path = STRBUF_INIT;
-	char *arg __attribute__((cleanup(cleanup_charp))) = NULL;
+	char *arg = NULL;
 	char *temp, *regex, *prio;
 	char split_char[] = " \t";
 	int priority = DEFAULT_PRIORITY, path_found = 0;
@@ -77,11 +77,12 @@ int prio_path_weight(struct path *pp, char *prio_args)
 	arg = strdup(prio_args);
 	temp = arg;
 
+	pthread_cleanup_push(cleanup_free_ptr, &arg);
 	regex = get_next_string(&temp, split_char);
 
 	/* Return default priority if the argument is not parseable */
 	if (!regex) {
-		return priority;
+		goto out;
 	}
 
 	pthread_cleanup_push_cast(reset_strbuf, &path);
@@ -123,6 +124,7 @@ int prio_path_weight(struct path *pp, char *prio_args)
 		}
 	}
 out:
+	pthread_cleanup_pop(1);
 	pthread_cleanup_pop(1);
 	return priority;
 }
multipathd/cli_handlers.c +8 -3
index ddc807a1..2d1c5cfe 100644
@@ -812,8 +812,9 @@ cli_reload(void *v, struct strbuf *reply, void *data)
 static int resize_map(struct multipath *mpp, unsigned long long size,
 	       struct vectors * vecs)
 {
-	char *params __attribute__((cleanup(cleanup_charp))) = NULL;
+	char *params = NULL;
 	unsigned long long orig_size = mpp->size;
+	int r = 1;
 
 	mpp->size = size;
 	update_mpp_paths(mpp, vecs->pathvec);
@@ -823,15 +824,19 @@ static int resize_map(struct multipath *mpp, unsigned long long size,
 		mpp->size = orig_size;
 		return 1;
 	}
+	pthread_cleanup_push(cleanup_free_ptr, &params);
 	mpp->action = ACT_RESIZE;
 	mpp->force_udev_reload = 1;
 	if (domap(mpp, params, 1) == DOMAP_FAIL) {
 		condlog(0, "%s: failed to resize map : %s", mpp->alias,
 			strerror(errno));
 		mpp->size = orig_size;
-		return 1;
+		goto out;
 	}
-	return 0;
+	r = 0;
+out:
+	pthread_cleanup_pop(1);
+	return r;
 }
 
 static int
multipathd/main.c +29 -20

134 lines not on this page · raw message

Signature · 6 lines
2.17.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#9 lore raw
In reply to Benjamin Marzinski
Quoted 10 lines · Benjamin Marzinski

the cleanup __attribute__ is only run when a variable goes out of scope
normally. It is not run on pthread cancellation. This means that multipathd could leak whatever resources were supposed to be cleaned up
if the thread was cancelled in a function using variables with the cleanup __attribute__. This patchset removes all these uses in cases where the code is run by multipathd and includes a cancellation point in the variables scope (usually condlog(), which calls fprintf(), a cancellation point, the way multipathd is usually run).

I have to say I don't like this.

Cleaning up resources is certainly very important, but in most of cases it's only about freeing memory on exit: memory which is going to be freed by the system anyway when multipathd terminates. Resource cleanup matters much more during runtime than on exit. The only threads that are cancelled during multipathd runtime are the TUR threads. It's nice to have valgrind show zero leaks when we kill multipathd out if the sudden. But we should weigh this against the side effects it has, which is ugly, hard-to-maintain code.

pthread_cleanup_push()/pop() calls contribute a lot to the bad readability and maintainability of much of the multipath code base, not to mention the weird errors some gcc versions throw for pthread_cleanup() constructs. I'd rather try to get rid of as much of them as we can. I know it won't be possible everywhere, because some resources (like file descriptors) really need to be cleaned up, but I am really unsure whether we need pthread cleanup for every free() operation.

__attribute__((cleanup())), on the contrary, goes a long way to make code more readable and easier to review. It actually helps a lot to ensure resources are properly cleaned up, considering how easily a "goto cleanup;" statement may be forgotten. Replacing it by pthread_cleanup() and goto statements is undesirable, IMO.

If your concern is really condlog(), let's discuss if we can find a way to convert this such that it is no cancellation point any more. I can imagine converting the locking in log_safe() from a mutex into some lockless scheme, using atomic variables, and using the log thread not only for syslog, but also for the fprintf() case.

Regards
Martin

Quoted 27 lines

Benjamin Marzinski (4):
  libmultipath: don't print garbage keywords
  libmultipath: avoid STRBUF_ON_STACK with cancellation points
  libmultipath: use regular array for field widths
  libmultipath: avoid cleanup __attribute__ with cancellation points

 libmpathutil/parser.c                    | 13 ++-- libmpathutil/strbuf.h                    |  4 +- libmultipath/alias.c                     | 59 ++++++++++-------

 libmultipath/blacklist.c                 |  4 +-
 libmultipath/configure.c                 |  6 +-
 libmultipath/discovery.c                 | 34 ++++++----

 libmultipath/dmparser.c                  | 23 +++---- libmultipath/foreign.c                   |  9 +-- libmultipath/generic.c                   | 14 ++-- libmultipath/libmultipath.version        |  4 +- libmultipath/print.c                     | 82 ++++++++++++++-------- --
 libmultipath/print.h                     |  4 +- libmultipath/prioritizers/weightedpath.c | 22 ++++--- libmultipath/propsel.c                   | 76 ++++++++++++++++------ libmultipath/sysfs.h                     | 11 +--- libmultipath/uevent.c                    |  6 +- multipath/main.c                         |  5 +- multipathd/cli_handlers.c                | 52 +++++++-------- multipathd/main.c                        | 49 ++++++++------

Quoted 2 lines
 19 files changed, 286 insertions(+), 191 deletions(-)

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

author maintainer
#10 lore raw
Quoted 38 lines · Martin Wilck
Quoted 10 lines · Benjamin Marzinski

the cleanup __attribute__ is only run when a variable goes out of scope
normally. It is not run on pthread cancellation. This means that multipathd could leak whatever resources were supposed to be cleaned up
if the thread was cancelled in a function using variables with the cleanup __attribute__. This patchset removes all these uses in cases where the code is run by multipathd and includes a cancellation point in the variables scope (usually condlog(), which calls fprintf(), a cancellation point, the way multipathd is usually run).

I have to say I don't like this.

Cleaning up resources is certainly very important, but in most of cases it's only about freeing memory on exit: memory which is going to be freed by the system anyway when multipathd terminates. Resource cleanup matters much more during runtime than on exit. The only threads that are cancelled during multipathd runtime are the TUR threads. It's nice to have valgrind show zero leaks when we kill multipathd out if the sudden. But we should weigh this against the side effects it has, which is ugly, hard-to-maintain code.

pthread_cleanup_push()/pop() calls contribute a lot to the bad readability and maintainability of much of the multipath code base, not to mention the weird errors some gcc versions throw for pthread_cleanup() constructs. I'd rather try to get rid of as much of them as we can. I know it won't be possible everywhere, because some resources (like file descriptors) really need to be cleaned up, but I am really unsure whether we need pthread cleanup for every free() operation.

__attribute__((cleanup())), on the contrary, goes a long way to make code more readable and easier to review. It actually helps a lot to ensure resources are properly cleaned up, considering how easily a "goto cleanup;" statement may be forgotten. Replacing it by pthread_cleanup() and goto statements is undesirable, IMO.

Quoted 5 lines

If your concern is really condlog(), let's discuss if we can find a way to convert this such that it is no cancellation point any more. I can imagine converting the locking in log_safe() from a mutex into some lockless scheme, using atomic variables, and using the log thread not only for syslog, but also for the fprintf() case.

Actually, I've never been a huge fan of all the work that we do to keep multipathd from leaking memory when it's killed, but I though that was the goal. If you don't care about these leaks, then I'm fine with ignoring them. How about just taking the first and third patches?

libmultipath: don't print garbage keywords
libmultipath: use regular array for field widths

-Ben

Quoted 33 lines

Regards
Martin

Quoted 29 lines

Benjamin Marzinski (4):
  libmultipath: don't print garbage keywords
  libmultipath: avoid STRBUF_ON_STACK with cancellation points
  libmultipath: use regular array for field widths
  libmultipath: avoid cleanup __attribute__ with cancellation points

 libmpathutil/parser.c                    | 13 ++-- libmpathutil/strbuf.h                    |  4 +- libmultipath/alias.c                     | 59 ++++++++++-------

 libmultipath/blacklist.c                 |  4 +-
 libmultipath/configure.c                 |  6 +-
 libmultipath/discovery.c                 | 34 ++++++----

 libmultipath/dmparser.c                  | 23 +++---- libmultipath/foreign.c                   |  9 +-- libmultipath/generic.c                   | 14 ++-- libmultipath/libmultipath.version        |  4 +- libmultipath/print.c                     | 82 ++++++++++++++-------- --
 libmultipath/print.h                     |  4 +- libmultipath/prioritizers/weightedpath.c | 22 ++++--- libmultipath/propsel.c                   | 76 ++++++++++++++++------ libmultipath/sysfs.h                     | 11 +--- libmultipath/uevent.c                    |  6 +- multipath/main.c                         |  5 +- multipathd/cli_handlers.c                | 52 +++++++-------- multipathd/main.c                        | 49 ++++++++------

 19 files changed, 286 insertions(+), 191 deletions(-)

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