Skip to contentSkip to conversation

Some minor fixes/additions for nvme-cli

PATCH Superseded [email protected] 4 participants 30 messages Mar 6, 2021 – Mar 16, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Some minor fixes/additions for nvme-cli

From: Martin Wilck <[email protected]>

This series collects patches from my former submission "RFC: add "nvme monitor" subcommand" which aren't directly related to the monitor functionality, but required to make the monitor in its current form compile. A v2 of the monitor code is about to follow soon.

The main part is the "generic logging functionality".

Changes wrt the RFC: add "nvme monitor" subcommand series: many.

- rebased to current upstream code base e9d43dd ("[nvme-cli] Fix Max/Min User data erase counts displayed in 0xC2 Log Page"))
- renamed log() to msg() in order to prevent name clash with <math.h>
- put logging and cleanup files in "util/" subdirectory
- converted the bulk of the logging code from a macro into a function (Hannes)
- added functionality to optionally log the PID (default off).
- added Hannes' patch he recently submitted via github
(https://github.com/linux-nvme/nvme-cli/pull/942), with two minor fixes on top, rationale in the commit messages
- various additional fixes for memory handling

Both cleanup and logging functions / macros could be used in more places with positive effects, but my focus is currently on preparing the monitor functionality.

I've pushed this series https://github.com/linux-nvme/nvme-cli/pull/877,
together with the follow-up monitor series.

Regards
Martin

Hannes Reinecke (1):
nvme-discover: lookup existing persistent controllers

Martin Wilck (9):
nvme-discover: assume device given on command line is persistent
do_discover: free cfg.device when resetting it nvme-connect-all(1): fix documentation for --quiet/-S
nvme: add some simplifying macros for __attribute__((cleanup()))
nvme-cli: add generic logging functionality
nvme: convert some function arguments from "char *" to "const char *"
fabrics: use "const char *" in struct config
fabrics: fix some memory leaks
fabrics: fix invalid memory access in discover_from_conf_file()

 Documentation/nvme-connect-all.1    |   8 +-
 Documentation/nvme-connect-all.html |  10 +-
 Documentation/nvme-connect-all.txt  |   4 +-
 Makefile                            |   2 +-
 fabrics.c                           | 223 +++++++++++++++++-----------
 nvme-topology.c                     |   2 +-
 nvme.c                              |   2 +-
 nvme.h                              |   4 +-
 util/cleanup.c                      |   4 +
 util/cleanup.h                      |  18 +++
 util/log.c                          |  90 +++++++++++
 util/log.h                          |  34 +++++
 12 files changed, 298 insertions(+), 103 deletions(-)
 create mode 100644 util/cleanup.c
 create mode 100644 util/cleanup.h
 create mode 100644 util/log.c
 create mode 100644 util/log.h
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#1 lore raw

PATCH 1/10 nvme-discover: lookup existing persistent controllers

From: Hannes Reinecke <[email protected]>

If persistent controller connections are present they should be preferred even if no --device option is given on the commandline. To avoid selecting a temporary non-persistent controller the 'kato' attribute is checked; any controller which does not have this attribute or for which the attribute is '0' will be skipped.

---
 fabrics.c | 50 ++++++++++++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 22 deletions(-)
fabrics.c +28 -22
index f84c45b..14899cc 100644
@@ -320,6 +320,27 @@ static bool ctrl_matches_connectargs(char *name, struct connect_args *args)
 	cargs.trsvcid = parse_conn_arg(addr, ' ', conarg_trsvcid);
 	cargs.host_traddr = parse_conn_arg(addr, ' ', conarg_host_traddr);
 
+	if (!strcmp(cargs.subsysnqn, NVME_DISC_SUBSYS_NAME)) {
+		char *kato_str = nvme_get_ctrl_attr(path, "kato"), *p;
+		unsigned int kato = 0;
+
+		/*
+		 * When looking up discovery controllers we have to skip
+		 * any non-persistent controllers (ie those with a zero
+		 * kato value). Otherwise the controller will vanish from
+		 * underneath us as they are owned by another program.
+		 *
+		 * The 'kato' attribute might not be present; assume a
+		 * non-persistent controller for these installations.
+		 */
+		if (kato_str) {
+			kato = strtoul(kato_str, &p, 0);
+			if (p == kato_str)
+				kato = 0;
+		}
+		if (kato == 0)
+			return found;
+	}
 	if (!strcmp(cargs.subsysnqn, args->subsysnqn) &&
 	    !strcmp(cargs.transport, args->transport) &&
 	    (!strcmp(cargs.traddr, args->traddr) ||
@@ -1345,30 +1366,15 @@ static int do_discover(char *argstr, bool connect, enum nvme_print_flags flags)
 	char *dev_name;
 	int instance, numrec = 0, ret, err;
 	int status = 0;
+	struct connect_args *cargs;
 
-	if (cfg.device) {
-		struct connect_args *cargs;
+	cargs = extract_connect_args(argstr);
+	if (!cargs)
+		return -ENOMEM;
 
-		cargs = extract_connect_args(argstr);
-		if (!cargs)
-			return -ENOMEM;
-
-		/*
-		 * if the cfg.device passed in matches the connect args
-		 *    cfg.device is left as-is
-		 * else if there exists a controller that matches the
-		 *         connect args
-		 *    cfg.device is the matching ctrl name
-		 * else if no ctrl matches the connect args
-		 *    cfg.device is set to null. This will attempt to
-		 *    create a new ctrl.
-		 * endif
-		 */
-		if (!ctrl_matches_connectargs(cfg.device, cargs))
-			cfg.device = find_ctrl_with_connectargs(cargs);
-
-		free_connect_args(cargs);
-	}
+	if (!cfg.device || !ctrl_matches_connectargs(cfg.device, cargs))
+		cfg.device = find_ctrl_with_connectargs(cargs);
+	free_connect_args(cargs);
 
 	if (!cfg.device) {
 		instance = add_ctrl(argstr);
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer
#2 lore raw

Re: [PATCH 01/10] nvme-discover: lookup existing persistent controllers

Quoted 8 lines

From: Hannes Reinecke <[email protected]>

If persistent controller connections are present they should be preferred even if no --device option is given on the commandline. To avoid selecting a temporary non-persistent controller the 'kato' attribute is checked; any controller which does not have this attribute or for which the attribute is '0' will be skipped.

Why?

You also need a fallback in case kato is not exposed in prior kernels.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#3 lore raw

Re: [PATCH 01/10] nvme-discover: lookup existing persistent controllers

Quoted 10 lines · Sagi Grimberg
Quoted 8 lines

From: Hannes Reinecke <[email protected]>

If persistent controller connections are present they should be preferred even if no --device option is given on the commandline. To avoid selecting a temporary non-persistent controller the 'kato' attribute is checked; any controller which does not have this attribute or for which the attribute is '0' will be skipped.

Why?

The idea is that an existing non-persistent controller is likely controller by another process, and could be disconnected any time.

Quoted 4 lines

You also need a fallback in case kato is not exposed in prior kernels.

Agreed. That's handled in patch 02/10. I didn't want to modify Hannes' patch directly.

Martin

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#4 lore raw

Re: [PATCH 01/10] nvme-discover: lookup existing persistent controllers

In reply to Sagi Grimberg
Quoted 9 lines · Sagi Grimberg
Quoted 8 lines

From: Hannes Reinecke <[email protected]>

If persistent controller connections are present they should be preferred even if no --device option is given on the commandline. To avoid selecting a temporary non-persistent controller the 'kato' attribute is checked; any controller which does not have this attribute or for which the attribute is '0' will be skipped.

Quoted 4 lines

Why?

You also need a fallback in case kato is not exposed in prior kernels.

The idea was to make the '--device' attribute optional _iff_ the kernel exposes the 'kato' attribute _and_ that attribute is non-zero.

Nothing more, nothing less.

Apparently the comment was slightly misleading.
I never intended to change the current behaviour if the 'kato' attribute is not exposed.

Cheers

Hannes

Signature · 9 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#5 lore raw

PATCH 2/10 nvme-discover: assume device given on command line is persistent

In reply to [email protected]

From: Martin Wilck <[email protected]>

After commit "nvme-discover: lookup existing persistent controllers", controllers without the "kato" sysfs attribute will never be used by do_discover(). This makes sense for controllers found while traversing sysfs in find_ctrl_with_connectargs(), but if the user passed a device explicitly, it should be used, even on older kernels that don't support the "kato" attribute.

Furthermore, make sure allocated memory in ctrl_matches_connectargs() is freed.

---
 fabrics.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fabrics.c +14 -8
index 14899cc..dcee0c4 100644
@@ -297,12 +297,14 @@ static int ctrl_instance(char *device)
  * given.
  * Return true/false based on whether it matches
  */
-static bool ctrl_matches_connectargs(char *name, struct connect_args *args)
+static bool ctrl_matches_connectargs(char *name, struct connect_args *args,
+				     bool assume_persistent)
 {
 	struct connect_args cargs;
 	bool found = false;
 	char *path, *addr;
 	int ret;
+	bool persistent = true;
 
 	ret = asprintf(&path, "%s/%s", SYS_NVME, name);
 	if (ret < 0)
@@ -331,17 +333,21 @@ static bool ctrl_matches_connectargs(char *name, struct connect_args *args)
 		 * underneath us as they are owned by another program.
 		 *
 		 * The 'kato' attribute might not be present; assume a
-		 * non-persistent controller for these installations.
+		 * non-persistent controller for these installations,
+		 * unless assume_persistent is set.
 		 */
 		if (kato_str) {
 			kato = strtoul(kato_str, &p, 0);
 			if (p == kato_str)
 				kato = 0;
-		}
-		if (kato == 0)
-			return found;
+			free(kato_str);
+			persistent = (kato != 0);
+		} else if (!assume_persistent)
+			persistent = false;
 	}
-	if (!strcmp(cargs.subsysnqn, args->subsysnqn) &&
+
+	if (persistent &&
+	    !strcmp(cargs.subsysnqn, args->subsysnqn) &&
 	    !strcmp(cargs.transport, args->transport) &&
 	    (!strcmp(cargs.traddr, args->traddr) ||
 	     !strcmp(args->traddr, "none")) &&
@@ -380,7 +386,7 @@ static char *find_ctrl_with_connectargs(struct connect_args *args)
 	}
 
 	for (i = 0; i < n; i++) {
-		if (ctrl_matches_connectargs(devices[i]->d_name, args)) {
+		if (ctrl_matches_connectargs(devices[i]->d_name, args, false)) {
 			devname = strdup(devices[i]->d_name);
 			if (devname == NULL)
 				fprintf(stderr, "no memory for ctrl name %s\n",
@@ -1372,7 +1378,7 @@ static int do_discover(char *argstr, bool connect, enum nvme_print_flags flags)
 	if (!cargs)
 		return -ENOMEM;
 
-	if (!cfg.device || !ctrl_matches_connectargs(cfg.device, cargs))
+	if (!cfg.device || !ctrl_matches_connectargs(cfg.device, cargs, true))
 		cfg.device = find_ctrl_with_connectargs(cargs);
 	free_connect_args(cargs);
 
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer
#6 lore raw

Re: [PATCH 02/10] nvme-discover: assume device given on command line is persistent

Quoted 10 lines

From: Martin Wilck <[email protected]>

After commit "nvme-discover: lookup existing persistent controllers", controllers without the "kato" sysfs attribute will never be used by do_discover(). This makes sense for controllers found while traversing sysfs in find_ctrl_with_connectargs(), but if the user passed a device explicitly, it should be used, even on older kernels that don't support the "kato" attribute.

Quoted 2 lines

Furthermore, make sure allocated memory in ctrl_matches_connectargs() is freed.

This is getting slightly convoluted... what is the motivation again?

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#7 lore raw

Re: [PATCH 02/10] nvme-discover: assume device given on command line is persistent

Quoted 13 lines · Sagi Grimberg
Quoted 13 lines

From: Martin Wilck <[email protected]>

After commit "nvme-discover: lookup existing persistent controllers",
controllers without the "kato" sysfs attribute will never be used by
do_discover(). This makes sense for controllers found while traversing
sysfs in find_ctrl_with_connectargs(), but if the user passed a device explicitly, it should be used, even on older kernels that don't support the "kato" attribute.

Quoted 6 lines
Quoted 3 lines

Furthermore, make sure allocated memory in
ctrl_matches_connectargs()
is freed.

This is getting slightly convoluted... what is the motivation again?

Currently, users need to specify *both* connect args *and* a discovery controller device if they want to reuse an existing discovery controller, which doesn't make much sense from a usability point of view. The idea is to simply check if a matching discovery controller is available.

But if we do that blindly, we may erroneously use a temporary connection that has been set up by some foreign process, and may go away under us. That's why we do the kato check. But as you pointed out yourself in your comment on 01/10, that would completely disable using existing controllers on older kernels that don't have the "kato" attribute. This patch changes the behavior such that if no kato- attribute is found, we trust the user if she specified the controller explicitly, but we don't trust just random controllers found in sysfs.

Thanks,
Martin

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#8 lore raw

PATCH 3/10 do_discover: free cfg.device when resetting it

In reply to [email protected]

From: Martin Wilck <[email protected]>

cfg.device might have been allocated by a previous call to find_ctrl_with_connectargs(), therefore free it. We must make sure that cfg.device is always on the heap, thus change fabrics_discover() accordingly.

---
 fabrics.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
fabrics.c +12 -3
index dcee0c4..223bd04 100644
@@ -1378,7 +1378,11 @@ static int do_discover(char *argstr, bool connect, enum nvme_print_flags flags)
 	if (!cargs)
 		return -ENOMEM;
 
-	if (!cfg.device || !ctrl_matches_connectargs(cfg.device, cargs, true))
+	if (cfg.device && !ctrl_matches_connectargs(cfg.device, cargs, true)) {
+		free(cfg.device);
+		cfg.device = NULL;
+	}
+	if (!cfg.device)
 		cfg.device = find_ctrl_with_connectargs(cargs);
 	free_connect_args(cargs);
 
@@ -1562,8 +1566,13 @@ int fabrics_discover(const char *desc, int argc, char **argv, bool connect)
 	ret = flags = validate_output_format(cfg.output_format);
 	if (ret < 0)
 		goto out;
-	if (cfg.device && !strcmp(cfg.device, "none"))
-		cfg.device = NULL;
+	if (cfg.device && strcmp(cfg.device, "none")) {
+		cfg.device = strdup(cfg.device);
+		if (!cfg.device) {
+			ret = -ENOMEM;
+			goto out;
+		}
+	}
 
 	cfg.nqn = NVME_DISC_SUBSYS_NAME;
 
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#9 lore raw

PATCH 4/10 nvme-connect-all(1): fix documentation for --quiet/-S

In reply to [email protected]

From: Martin Wilck <[email protected]>

---
 Documentation/nvme-connect-all.1    |  8 ++++----
 Documentation/nvme-connect-all.html | 10 +++++-----
 Documentation/nvme-connect-all.txt  |  4 ++--
 3 files changed, 11 insertions(+), 11 deletions(-)
Documentation/nvme-connect-all.1 +4 -4
index 91d6896..a53a017 100644
@@ -2,12 +2,12 @@
 .\"     Title: nvme-connect-all
 .\"    Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
-.\"      Date: 10/20/2020
+.\"      Date: 01/20/2021
 .\"    Manual: NVMe Manual
 .\"    Source: NVMe
 .\"  Language: English
 .\"
-.TH "NVME\-CONNECT\-ALL" "1" "10/20/2020" "NVMe" "NVMe Manual"
+.TH "NVME\-CONNECT\-ALL" "1" "01/20/2021" "NVMe" "NVMe Manual"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -51,7 +51,7 @@ nvme-connect-all \- Discover and Connect to Fabrics controllers\&.
                 [\-\-queue\-size=<#>         | \-Q <#>]
                 [\-\-matching               | \-m]
                 [\-\-persistent             | \-p]
-                [\-\-quiet                  | \-q]
+                [\-\-quiet                  | \-S]
 .fi
 .SH "DESCRIPTION"
 .sp
@@ -193,7 +193,7 @@ If a traddr was specified on the command line or in the configuration file, only
 Don\(cqt remove the discovery controller after retrieving the discovery log page\&.
 .RE
 .PP
-\-q, \-\-quiet
+\-S, \-\-quiet
 .RS 4
 Suppress error messages\&.
 .RE
Documentation/nvme-connect-all.html +5 -5
index beae013..f779c7b 100644
@@ -4,7 +4,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
-<meta name="generator" content="AsciiDoc 8.6.10" />
+<meta name="generator" content="AsciiDoc" />
 <title>nvme-connect-all(1)</title>
 <style type="text/css">
 /* Shared CSS for AsciiDoc xhtml11 and html5 backends */
@@ -436,7 +436,7 @@ thead, p.table.header {
 p.table {
   margin-top: 0;
 }
-/* Because the table frame attribute is overriden by CSS in most browsers. */
+/* Because the table frame attribute is overridden by CSS in most browsers. */
 div.tableblock > table[frame="void"] {
   border-style: none;
 }
@@ -768,7 +768,7 @@ nvme-connect-all(1) Manual Page
                 [--queue-size=&lt;#&gt;         | -Q &lt;#&gt;]
                 [--matching               | -m]
                 [--persistent             | -p]
-                [--quiet                  | -q]</pre>
+                [--quiet                  | -S]</pre>
 <div class="attribution">
 </div></div>
 </div>
@@ -1048,7 +1048,7 @@ cellspacing="0" cellpadding="4">
 </p>
 </dd>
 <dt class="hdlist1">
--q
+-S
 </dt>
 <dt class="hdlist1">
 --quiet
@@ -1115,7 +1115,7 @@ nvme-connect(1)</p></div>
 <div id="footer">
 <div id="footer-text">
 Last updated
- 2020-10-20 16:47:21 PDT
+ 2021-01-20 23:40:57 CET
 </div>
 </div>
 </body>
Documentation/nvme-connect-all.txt +2 -2
index 128f336..820dd6c 100644
@@ -27,7 +27,7 @@ SYNOPSIS
 		[--queue-size=<#>         | -Q <#>]
 		[--matching               | -m]
 		[--persistent             | -p]
-		[--quiet                  | -q]
+		[--quiet                  | -S]
 
 DESCRIPTION
 -----------
@@ -154,7 +154,7 @@ OPTIONS
 	Don't remove the discovery controller after retrieving the discovery
 	log page.
 
--q::
+-S::
 --quiet::
 	Suppress error messages.
 
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v1 4/10
#10 lore raw

Re: [PATCH 04/10] nvme-connect-all(1): fix documentation for --quiet/-S

Quoted 3 lines · On 3/5/21 16:36, [email protected] wrote:

From: Martin Wilck <[email protected]>

Stale date reference 01/20/2021 to current date, otherwise Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#12 lore raw

PATCH 5/10 nvme: add some simplifying macros for __attribute__((cleanup()))

In reply to [email protected]

From: Martin Wilck <[email protected]>

Using __attribute__((cleanup())) is very helpful for writing leak-free code, but it requires lots of awkward boiler plate code. Add some small helpers to make its use more comfortable.

---
 Makefile       |  2 +-
 util/cleanup.c |  4 ++++
 util/cleanup.h | 18 ++++++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 util/cleanup.c
 create mode 100644 util/cleanup.h
Makefile +1 -1
index 3ea17b5..3412e5d 100644
@@ -62,7 +62,7 @@ OBJS := nvme-print.o nvme-ioctl.o nvme-rpmb.o \
 	nvme-lightnvm.o fabrics.o nvme-models.o plugin.o \
 	nvme-status.o nvme-filters.o nvme-topology.o
 
-UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o
+UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/cleanup.o
 
 PLUGIN_OBJS :=					\
 	plugins/intel/intel-nvme.o		\
util/cleanup.c new file +4 -0
new file mode 100644
index 0000000..0d5d910
@@ -0,0 +1,4 @@
+#include <stdlib.h>
+#include "cleanup.h"
+
+DEFINE_CLEANUP_FUNC(cleanup_charp, char *, free);
util/cleanup.h new file +18 -0
new file mode 100644
index 0000000..89a4984
@@ -0,0 +1,18 @@
+#ifndef __CLEANUP_H
+#define __CLEANUP_H
+
+#define __cleanup__(fn) __attribute__((cleanup(fn)))
+
+#define DECLARE_CLEANUP_FUNC(name, type) \
+	void name(type *__p)
+
+#define DEFINE_CLEANUP_FUNC(name, type, free_fn)\
+DECLARE_CLEANUP_FUNC(name, type)		\
+{						\
+	if (*__p)				\
+		free_fn(*__p);			\
+}
+
+DECLARE_CLEANUP_FUNC(cleanup_charp, char *);
+
+#endif
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v1 5/10
#13 lore raw

Re: [PATCH 05/10] nvme: add some simplifying macros for __attribute__((cleanup()))

Quoted 7 lines · On 3/5/21 16:36, [email protected] wrote:

From: Martin Wilck <[email protected]>

Using __attribute__((cleanup())) is very helpful for writing leak-free code, but it requires lots of awkward boiler plate code. Add some small helpers to make its use more comfortable.

Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#15 lore raw

PATCH 6/10 nvme-cli: add generic logging functionality

In reply to [email protected]

From: Martin Wilck <[email protected]>

Add a msg() macro that allows more flexible customization of logging both at build time and at run time. Allow several log levels, using the well-known standard sylog levels. Also optionally allow printing of log timestamps.

Put '#define LOG_FUNCNAME' before '#include "util/log.h"' to enable printing the name of the calling function before the log message.

Use this functionality in the fabrics code for now, wherever fprintf(stderr, ...) had been used.

No functional change except changing the output channel of 554db7d ("print device name when creating a persistent device") from stdout to stderr.

---
 Makefile   |  2 +-
 fabrics.c  | 85 ++++++++++++++++++++++++++++-----------------------
 util/log.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 util/log.h | 34 +++++++++++++++++++++
 4 files changed, 171 insertions(+), 40 deletions(-)
 create mode 100644 util/log.c
 create mode 100644 util/log.h
Makefile +1 -1
index 3412e5d..1fe693c 100644
@@ -62,7 +62,7 @@ OBJS := nvme-print.o nvme-ioctl.o nvme-rpmb.o \
 	nvme-lightnvm.o fabrics.o nvme-models.o plugin.o \
 	nvme-status.o nvme-filters.o nvme-topology.o
 
-UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/cleanup.o
+UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/cleanup.o util/log.o
 
 PLUGIN_OBJS :=					\
 	plugins/intel/intel-nvme.o		\
fabrics.c +46 -39

320 lines not on this page · raw message

util/log.c new file +90 -0

90 lines not on this page · raw message

util/log.h new file +34 -0
new file mode 100644
index 0000000..15107a5
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021 Martin Wilck, SUSE LLC
+ * SPDX-License-Identifier: LGPL-2.1-or-newer
+ */
+#ifndef _LOG_H
+#define _LOG_H
+
+#ifndef MAX_LOGLEVEL
+#  define MAX_LOGLEVEL LOG_DEBUG
+#endif
+#ifndef DEFAULT_LOGLEVEL
+#  define DEFAULT_LOGLEVEL LOG_NOTICE
+#endif
+
+#if (LOG_FUNCNAME == 1)
+#define _log_func __func__
+#else
+#define _log_func NULL
+#endif
+
+extern int log_level;
+extern bool log_timestamp;
+extern bool log_pid;
+
+void __attribute__((format(printf, 3, 4)))
+__msg(int lvl, const char *func, const char *format, ...);
+
+#define msg(lvl, format, ...)						\
+	do {								\
+		if ((lvl) <= MAX_LOGLEVEL)				\
+			__msg(lvl, _log_func, format, ##__VA_ARGS__);	\
+	} while (0)
+
+#endif /* _LOG_H */
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#16 lore raw

Re: [PATCH 06/10] nvme-cli: add generic logging functionality

Makefile OBJS := nvme-print.o nvme-ioctl.o nvme-rpmb.o \ · line 65

Quoted 3 lines · On 3/5/21 16:36, [email protected] wrote:
Makefile +1 -1
-UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/cleanup.o
+UTIL_OBJS := util/argconfig.o util/suffix.o util/json.o util/parser.o util/cleanup.o util/log.o

Line is too long, can you split ?

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

Reviewed-by on v1 6/10
#17 lore raw

Re: [PATCH 06/10] nvme-cli: add generic logging functionality

In reply to [email protected]
Quoted 10 lines · On 3/5/21 16:36, [email protected] wrote:

From: Martin Wilck <[email protected]>

Add a msg() macro that allows more flexible customization of logging both at build time and at run time. Allow several log levels, using the well-known standard sylog levels. Also optionally allow printing of log timestamps.

Put '#define LOG_FUNCNAME' before '#include "util/log.h"' to enable printing the name of the calling function before the log message.

Quoted 7 lines

Use this functionality in the fabrics code for now, wherever fprintf(stderr, ...) had been used.

No functional change except changing the output channel of 554db7d ("print device name when creating a persistent device") from stdout to stderr.

Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#18 lore raw

Re: [PATCH 06/10] nvme-cli: add generic logging functionality

In reply to [email protected]
Quoted 17 lines

From: Martin Wilck <[email protected]>

Add a msg() macro that allows more flexible customization of logging both at build time and at run time. Allow several log levels, using the well-known standard sylog levels. Also optionally allow printing of log timestamps.

Put '#define LOG_FUNCNAME' before '#include "util/log.h"' to enable printing the name of the calling function before the log message.

Use this functionality in the fabrics code for now, wherever fprintf(stderr, ...) had been used.

No functional change except changing the output channel of 554db7d ("print device name when creating a persistent device") from stdout to stderr.

Quoted 6 lines
---
  Makefile   |  2 +-
  fabrics.c  | 85 ++++++++++++++++++++++++++++-----------------------
  util/log.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  util/log.h | 34 +++++++++++++++++++++

I think it makes sense to do a wider sweep also for nvme.c and friends no?

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#19 lore raw

Re: [PATCH 06/10] nvme-cli: add generic logging functionality

Quoted 32 lines · Sagi Grimberg
Quoted 23 lines

From: Martin Wilck <[email protected]>

Add a msg() macro that allows more flexible customization of logging
both at build time and at run time. Allow several log levels, using the well-known standard sylog levels. Also optionally allow printing
of log timestamps.

Put '#define LOG_FUNCNAME' before '#include "util/log.h"' to enable printing
the name of the calling function before the log message.

Use this functionality in the fabrics code for now, wherever fprintf(stderr, ...)
had been used.

No functional change except changing the output channel of 554db7d ("print
device name when creating a persistent device") from stdout to stderr.

Quoted 8 lines

---
  Makefile   |  2 +-
  fabrics.c  | 85 ++++++++++++++++++++++++++++--------------------- --
  util/log.c | 90
++++++++++++++++++++++++++++++++++++++++++++++++++++++
  util/log.h | 34 +++++++++++++++++++++

Quoted 3 lines

I think it makes sense to do a wider sweep also for nvme.c and friends
no?

Yes, but could we do that wider sweep separately, or do you consider it a requirement?

Regards,
Martin

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#20 lore raw

PATCH 7/10 nvme: convert some function arguments from "char *" to "const char *"

In reply to [email protected]

From: Martin Wilck <[email protected]>

Make some function arguments "const char *" where possible. This is generally desirable and allows passing string constants to these function.

---
 fabrics.c       | 25 +++++++++++++++----------
 nvme-topology.c |  2 +-
 nvme.c          |  2 +-
 nvme.h          |  4 ++--
 4 files changed, 19 insertions(+), 14 deletions(-)
fabrics.c +15 -10
index 04f0aef..8a1586f 100644
@@ -234,7 +234,7 @@ static int do_discover(char *argstr, bool connect, enum nvme_print_flags flags);
  * If field found, return string containing field value. If field
  * not found, return an empty string.
  */
-static char *parse_conn_arg(char *conargs, const char delim, const char *field)
+static char *parse_conn_arg(const char *conargs, const char delim, const char *field)
 {
 	char *s, *e;
 	size_t cnt;
@@ -278,17 +278,22 @@ empty_field:
 	return strdup("\0");
 }
 
-static int ctrl_instance(char *device)
+static int ctrl_instance(const char *device)
 {
 	char d[64];
+	const char *p;
 	int ret, instance;
 
-	device = basename(device);
-	ret = sscanf(device, "nvme%d", &instance);
+	p = strrchr(device, '/');
+	if (p == NULL)
+		p = device;
+	else
+		p++;
+	ret = sscanf(p, "nvme%d", &instance);
 	if (ret <= 0)
 		return -EINVAL;
 	if (snprintf(d, sizeof(d), "nvme%d", instance) <= 0 ||
-	    strcmp(device, d))
+	    strcmp(p, d))
 		return -EINVAL;
 	return instance;
 }
@@ -299,7 +304,7 @@ static int ctrl_instance(char *device)
  * given.
  * Return true/false based on whether it matches
  */
-static bool ctrl_matches_connectargs(char *name, struct connect_args *args,
+static bool ctrl_matches_connectargs(const char *name, struct connect_args *args,
 				     bool assume_persistent)
 {
 	struct connect_args cargs;
@@ -927,7 +932,7 @@ add_int_argument(char **argstr, int *max_len, char *arg_str, int arg,
 }
 
 static int
-add_argument(char **argstr, int *max_len, char *arg_str, char *arg)
+add_argument(char **argstr, int *max_len, char *arg_str, const char *arg)
 {
 	int len;
 
@@ -1677,7 +1682,7 @@ static int scan_sys_nvme_filter(const struct dirent *d)
 /*
  * Returns 1 if disconnect occurred, 0 otherwise.
  */
-static int disconnect_subsys(char *nqn, char *ctrl)
+static int disconnect_subsys(const char *nqn, char *ctrl)
 {
 	char *sysfs_nqn_path = NULL, *sysfs_del_path = NULL;
 	char subsysnqn[NVMF_NQN_SIZE] = {};
@@ -1715,7 +1720,7 @@ static int disconnect_subsys(char *nqn, char *ctrl)
 /*
  * Returns the number of controllers successfully disconnected.
  */
-static int disconnect_by_nqn(char *nqn)
+static int disconnect_by_nqn(const char *nqn)
 {
 	struct dirent **devices = NULL;
 	int i, n, ret = 0;
@@ -1737,7 +1742,7 @@ static int disconnect_by_nqn(char *nqn)
 	return ret;
 }
 
-static int disconnect_by_device(char *device)
+static int disconnect_by_device(const char *device)
 {
 	int instance;
 
nvme-topology.c +1 -1
index c0e2d51..fc581f8 100644
@@ -45,7 +45,7 @@ close_fd:
 	return subsysnqn;
 }
 
-char *nvme_get_ctrl_attr(char *path, const char *attr)
+char *nvme_get_ctrl_attr(const char *path, const char *attr)
 {
 	char *attrpath, *value;
 	ssize_t ret;
nvme.c +1 -1
index 2653939..9064e83 100644
@@ -209,7 +209,7 @@ int parse_and_open(int argc, char **argv, const char *desc,
 	return ret;
 }
 
-enum nvme_print_flags validate_output_format(char *format)
+enum nvme_print_flags validate_output_format(const char *format)
 {
 	if (!format)
 		return -EINVAL;
nvme.h +2 -2
index 3fb1060..5b4c1e1 100644
@@ -91,7 +91,7 @@ int parse_and_open(int argc, char **argv, const char *desc,
 extern const char *devicename;
 extern const char *output_format;
 
-enum nvme_print_flags validate_output_format(char *format);
+enum nvme_print_flags validate_output_format(const char *format);
 int __id_ctrl(int argc, char **argv, struct command *cmd,
 	struct plugin *plugin, void (*vs)(__u8 *vs, struct json_object *root));
 char *nvme_char_from_block(char *block);
@@ -109,7 +109,7 @@ int scan_subsystems(struct nvme_topology *t, const char *subsysnqn,
 		    __u32 ns_instance, char *dev_dir);
 void free_topology(struct nvme_topology *t);
 char *get_nvme_subsnqn(char *path);
-char *nvme_get_ctrl_attr(char *path, const char *attr);
+char *nvme_get_ctrl_attr(const char *path, const char *attr);
 
 void *nvme_alloc(size_t len, bool *huge);
 void nvme_free(void *p, bool huge);
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v1 7/10
#21 lore raw

Re: [PATCH 07/10] nvme: convert some function arguments from "char *" to "const char *"

Quoted 7 lines · On 3/5/21 16:36, [email protected] wrote:

From: Martin Wilck <[email protected]>

Make some function arguments "const char *" where possible. This is generally desirable and allows passing string constants to these function.

Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#23 lore raw

PATCH 8/10 fabrics: use "const char *" in struct config

In reply to [email protected]

From: Martin Wilck <[email protected]>

This is easily done, and allows passing in some const char* pointers that would otherwise need to be strdup()d first.

Do not use const char * for cfg->device, because we may need to free() it.

---
 fabrics.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
fabrics.c +9 -9
index 8a1586f..c965beb 100644
@@ -67,13 +67,13 @@ const char *conarg_trsvcid = "trsvcid";
 const char *conarg_host_traddr = "host_traddr";
 
 static struct config {
-	char *nqn;
-	char *transport;
-	char *traddr;
-	char *trsvcid;
-	char *host_traddr;
-	char *hostnqn;
-	char *hostid;
+	const char *nqn;
+	const char *transport;
+	const char *traddr;
+	const char *trsvcid;
+	const char *host_traddr;
+	const char *hostnqn;
+	const char *hostid;
 	int  nr_io_queues;
 	int  nr_write_queues;
 	int  nr_poll_queues;
@@ -82,7 +82,7 @@ static struct config {
 	int  reconnect_delay;
 	int  ctrl_loss_tmo;
 	int  tos;
-	char *raw;
+	const char *raw;
 	char *device;
 	int  duplicate_connect;
 	int  disable_sqflow;
@@ -90,7 +90,7 @@ static struct config {
 	int  data_digest;
 	bool persistent;
 	bool matching_only;
-	char *output_format;
+	const char *output_format;
 } cfg = {
 	.ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO,
 	.output_format = "normal",
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v1 8/10
#24 lore raw

Re: [PATCH 08/10] fabrics: use "const char *" in struct config

Quoted 9 lines · On 3/5/21 16:36, [email protected] wrote:

From: Martin Wilck <[email protected]>

This is easily done, and allows passing in some const char* pointers that would otherwise need to be strdup()d first.

Do not use const char * for cfg->device, because we may need to free() it.

Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#26 lore raw

PATCH 9/10 fabrics: fix some memory leaks

In reply to [email protected]

From: Martin Wilck <[email protected]>

None of these are critical for "nvme discover" or "nvme connect-all". Still, silencing valgrind's error messages by fixing them gives some peace of mind, and a longer-running program like the forthcomint nvme monitor, leak checks are more important.

Use the previously introduced cleanup macros for this purpose.

---
 fabrics.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
fabrics.c +17 -6
index c965beb..f2ff511 100644
@@ -49,6 +49,7 @@
 
 #include "common.h"
 #include "util/log.h"
+#include "util/cleanup.h"
 
 #ifdef HAVE_SYSTEMD
 #include <systemd/sd-id128.h>
@@ -309,7 +310,7 @@ static bool ctrl_matches_connectargs(const char *name, struct connect_args *args
 {
 	struct connect_args cargs;
 	bool found = false;
-	char *path, *addr;
+	char *path = NULL, *addr;
 	int ret;
 	bool persistent = true;
 
@@ -369,6 +370,8 @@ static bool ctrl_matches_connectargs(const char *name, struct connect_args *args
 	free(cargs.traddr);
 	free(cargs.trsvcid);
 	free(cargs.host_traddr);
+	free(addr);
+	free(path);
 
 	return found;
 }
@@ -425,13 +428,18 @@ static struct connect_args *extract_connect_args(char *argstr)
 	return cargs;
 }
 
-static void free_connect_args(struct connect_args *cargs)
+static void destruct_connect_args(struct connect_args *cargs)
 {
 	free(cargs->subsysnqn);
 	free(cargs->transport);
 	free(cargs->traddr);
 	free(cargs->trsvcid);
 	free(cargs->host_traddr);
+}
+
+static void free_connect_args(struct connect_args *cargs)
+{
+	destruct_connect_args(cargs);
 	free(cargs);
 }
 
@@ -1285,7 +1293,7 @@ retry:
 
 static bool cargs_match_found(struct nvmf_disc_rsp_page_entry *entry)
 {
-	struct connect_args cargs = {};
+	struct connect_args cargs __cleanup__(destruct_connect_args) = { NULL, };
 	struct connect_args *c = tracked_ctrls;
 
 	cargs.traddr = strdup(entry->traddr);
@@ -1371,9 +1379,11 @@ static void nvmf_get_host_identifiers(int ctrl_instance)
 	cfg.hostid = nvme_get_ctrl_attr(path, "hostid");
 }
 
+static DEFINE_CLEANUP_FUNC(cleanup_log, struct nvmf_disc_rsp_page_hdr *, free);
+
 static int do_discover(char *argstr, bool connect, enum nvme_print_flags flags)
 {
-	struct nvmf_disc_rsp_page_hdr *log = NULL;
+	struct nvmf_disc_rsp_page_hdr *log __cleanup__(cleanup_log) = NULL;
 	char *dev_name;
 	int instance, numrec = 0, ret, err;
 	int status = 0;
@@ -1457,7 +1467,7 @@ static int discover_from_conf_file(const char *desc, char *argstr,
 		const struct argconfig_commandline_options *opts, bool connect)
 {
 	FILE *f;
-	char line[256], *ptr, *args, **argv;
+	char line[256], *ptr, *all_args, *args, **argv;
 	int argc, err, ret = 0;
 
 	f = fopen(PATH_NVMF_DISC, "r");
@@ -1479,6 +1489,7 @@ static int discover_from_conf_file(const char *desc, char *argstr,
 			ret = -ENOMEM;
 			goto out;
 		}
+		all_args = args;
 
 		argv = calloc(MAX_DISC_ARGS, BUF_SIZE);
 		if (!argv) {
@@ -1522,7 +1533,7 @@ static int discover_from_conf_file(const char *desc, char *argstr,
 			ret = err;
 
 free_and_continue:
-		free(args);
+		free(all_args);
 		free(argv);
 	}
 
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v1 9/10
#27 lore raw

Re: [PATCH 09/10] fabrics: fix some memory leaks

Quoted 10 lines · On 3/5/21 16:36, [email protected] wrote:

From: Martin Wilck <[email protected]>

None of these are critical for "nvme discover" or "nvme connect-all". Still, silencing valgrind's error messages by fixing them gives some peace of mind, and a longer-running program like the forthcomint nvme monitor, leak checks are more important.

Use the previously introduced cleanup macros for this purpose.

Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#29 lore raw

PATCH 10/10 fabrics: fix invalid memory access in discover_from_conf_file()

In reply to [email protected]

From: Martin Wilck <[email protected]>

argconfig_parse() assigns pointers in cfg to point to memory allocated in all_args. If this memory is freed, these pointers become dangling. This is particularly dangerous if discovery.conf contains empty lines, comment lines, or invalid lines.

Fix it by setting all transport parameter to NULL after processing each line, and not proceeding if the basic parameters aren't set.

---
 fabrics.c | 5 +++++
 1 file changed, 5 insertions(+)
fabrics.c +5 -0
index f2ff511..1fcc6cf 100644
@@ -1508,6 +1508,9 @@ static int discover_from_conf_file(const char *desc, char *argstr,
 		if (err)
 			goto free_and_continue;
 
+		if (!cfg.transport || !cfg.traddr)
+			goto free_and_continue;
+
 		err = flags = validate_output_format(cfg.output_format);
 		if (err < 0)
 			goto free_and_continue;
@@ -1535,6 +1538,8 @@ static int discover_from_conf_file(const char *desc, char *argstr,
 free_and_continue:
 		free(all_args);
 		free(argv);
+		cfg.transport = cfg.traddr = cfg.trsvcid =
+			cfg.host_traddr = NULL;
 	}
 
 out:
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme