Skip to contentSkip to conversation

scsi: core: Optimize the SCSI printk() variants

PATCH In review Bart Van Assche 3 participants 9 messages Aug 31 – Sep 10 SCSI

lore
Share & tools
lore
#0 lore raw

v2 cover scsi: core: Optimize the SCSI printk() variants

Hi Martin,

The SCSI logging functions currently allocate a temporary 128-byte buffer via kmalloc(..., GFP_ATOMIC). This design has several drawbacks:
- Runtime memory allocation and freeing overhead on logging paths.
- Truncation of log messages to 127 characters.
- Discarded log messages if the buffer allocation fails under memory pressure.

This patch series eliminates temporary buffer allocations from scsi_logging.c by formatting messages directly using struct va_format (%pV) and dev_printk(), as well as formatting hex buffers directly with %*ph.

To ensure formatting and logging behavior remain free of regressions, the first patch introduces a comprehensive KUnit test suite for the SCSI logging functions. The second patch refactors scsi_logging.c to use the direct formatting helpers.

Please consider applying this series.

Thanks,

Bart.

Changes compared to v1:
- Expanded a single patch into a series of two patches by adding a patch with unit tests.
- Restored a newline in a format string that was missing from v1.

Bart Van Assche (2):
scsi: core: Add KUnit tests for scsi_logging.c
scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

 drivers/scsi/Kconfig             |   9 +
 drivers/scsi/scsi_logging.c      | 285 +++++++++------------
 drivers/scsi/scsi_logging_test.c | 422 +++++++++++++++++++++++++++++++
 3 files changed, 547 insertions(+), 169 deletions(-)
 create mode 100644 drivers/scsi/scsi_logging_test.c
#1 lore raw

PATCH v2 1/2 scsi: core: Add KUnit tests for scsi_logging.c

Add regression unit tests for the functions in drivers/scsi/scsi_logging.c that call dev_printk().

Implement a macro redefinition approach where dev_printk() is intercepted when CONFIG_SCSI_LOGGING_KUNIT_TEST is enabled, allowing the test suite to capture the emitted log levels, target device references, and formatted log strings into a test buffer without side effects.

Add test cases covering:
- sdev_prefix_printk() with and without device name prefix.
- scmd_printk() with tag only, disk name and tag, and untagged commands.
- scsi_print_command() with 6, 10, 16, and multi-line 32-byte CDBs, as well as vendor-specific and reserved opcodes.
- scsi_print_sense_hdr() with standard, descriptor, and deferred formats.
- __scsi_print_sense() with normalized sense data and raw hex dumps.
- scsi_print_sense() with command-attached sense buffers.
- scsi_print_result() with custom messages, default messages, unknown dispositions, and unknown hostbyte statuses.

---
 drivers/scsi/Kconfig             |   9 +
 drivers/scsi/scsi_logging.c      |  30 +++
 drivers/scsi/scsi_logging_test.c | 422 +++++++++++++++++++++++++++++++
 3 files changed, 461 insertions(+)
 create mode 100644 drivers/scsi/scsi_logging_test.c
drivers/scsi/Kconfig +9 -0
index 4a2af0f702e1..8c2a44b55900 100644
@@ -76,6 +76,15 @@ config SCSI_LIB_KUNIT_TEST
 
 	  If unsure say N.
 
+config SCSI_LOGGING_KUNIT_TEST
+	bool "KUnit tests for SCSI logging functions" if !KUNIT_ALL_TESTS
+	depends on SCSI && KUNIT=y && SCSI_CONSTANTS
+	default KUNIT_ALL_TESTS
+	help
+	  Run SCSI Mid Layer's KUnit tests for scsi_logging.
+
+	  If unsure say N.
+
 comment "SCSI support type (disk, tape, CD-ROM)"
 	depends on SCSI
 
drivers/scsi/scsi_logging.c +30 -0
index 3cd0d3074085..3bff4dea4c6a 100644
@@ -15,6 +15,32 @@
 #include <scsi/scsi_eh.h>
 #include <scsi/scsi_dbg.h>
 
+#if IS_ENABLED(CONFIG_SCSI_LOGGING_KUNIT_TEST)
+static void (*scsi_logging_test_dev_printk)(const char *level,
+					    const struct device *dev,
+					    const char *fmt, va_list args);
+
+static void scsi_logging_dev_printk(const char *level, const struct device *dev,
+				    const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	if (unlikely(scsi_logging_test_dev_printk)) {
+		scsi_logging_test_dev_printk(level, dev, fmt, args);
+	} else {
+		struct va_format vaf = { .fmt = fmt, .va = &args };
+
+		_dev_printk(level, dev, "%pV", &vaf);
+	}
+	va_end(args);
+}
+
+#undef dev_printk
+#define dev_printk(level, dev, fmt, ...) \
+	scsi_logging_dev_printk(level, dev, fmt, ##__VA_ARGS__)
+#endif
+
 static char *scsi_log_reserve_buffer(size_t *len)
 {
 	*len = 128;
@@ -436,3 +462,7 @@ void scsi_print_result(struct scsi_cmnd *cmd, const char *msg, int disposition)
 	scsi_log_release_buffer(logbuf);
 }
 EXPORT_SYMBOL(scsi_print_result);
+
+#if IS_ENABLED(CONFIG_SCSI_LOGGING_KUNIT_TEST)
+#include "scsi_logging_test.c"
+#endif
drivers/scsi/scsi_logging_test.c new file +422 -0

422 lines not on this page · raw message

#2 lore raw

Re: [PATCH v2 1/2] scsi: core: Add KUnit tests for scsi_logging.c

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] Data race and out-of-bounds memory write in SCSI logging test capture. - [Medium] NULL pointer…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] Data race and out-of-bounds memory write in SCSI logging test capture.
- [Medium] NULL pointer dereference due to a TOCTOU data race on the global scsi_logging_test_dev_printk pointer.
- [Low] Global suppression of actual SCSI system logs during KUnit test execution.
--

commit 33967e88f5091c2f971d2edac33840afe7e6e925
Author: Bart Van Assche <[email protected]>

scsi: core: Add KUnit tests for scsi_logging.c

This commit introduces KUnit tests for SCSI logging functions by intercepting dev_printk() when CONFIG_SCSI_LOGGING_KUNIT_TEST is enabled. It captures log outputs into a buffer to verify standard and formatted SCSI command outputs.

Quoted 11 lines
drivers/scsi/scsi_logging.c +3 -0
index 3cd0d30740855..3bff4dea4c6ac 100644
@@ -15,6 +15,32 @@
 #include <scsi/scsi_eh.h>
 #include <scsi/scsi_dbg.h>
 
+#if IS_ENABLED(CONFIG_SCSI_LOGGING_KUNIT_TEST)
+static void (*scsi_logging_test_dev_printk)(const char *level,
+					    const struct device *dev,

drivers/scsi/scsi_logging.c line 30

Quoted 10 lines
drivers/scsi/scsi_logging.c +10 -0
+					    const char *fmt, va_list args);
+
+static void scsi_logging_dev_printk(const char *level, const struct device *dev,
+				    const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	if (unlikely(scsi_logging_test_dev_printk)) {
+		scsi_logging_test_dev_printk(level, dev, fmt, args);

[Severity: Medium]
Could this result in a NULL pointer dereference? The global pointer scsi_logging_test_dev_printk is checked for truthiness and then invoked without synchronization or READ_ONCE(). If a background SCSI event triggers a log concurrently with the test teardown in scsi_logging_test_exit() (which clears the pointer), a race window exists where the pointer might be evaluated as non-NULL but cleared before invocation.

drivers/scsi/scsi_logging.c line 35

Quoted 5 lines
drivers/scsi/scsi_logging.c +5 -0
+	} else {
+		struct va_format vaf = { .fmt = fmt, .va = &args };
+
+		_dev_printk(level, dev, "%pV", &vaf);
+	}

[Severity: Low]
Does this branch inadvertently suppress all actual SCSI system logs during the test execution? Because the hook intercepts all dev_printk() calls in scsi_logging.c and doesn't filter for the test-specific dev structure, background hardware logs might be swallowed while the test is running.

Quoted 2 lines
drivers/scsi/scsi_logging.c +2 -0
+	va_end(args);
+}

[ ... ]

Quoted 6 lines
drivers/scsi/scsi_logging_test.c new file +0 -0
new file mode 100644
index 0000000000000..1dcebc383052a
@@ -0,0 +1,422 @@

[ ... ]

drivers/scsi/scsi_logging_test.c line 33

Quoted 9 lines
drivers/scsi/scsi_logging_test.c new file +9 -0
+static struct captured_dev_printk captured_logs[MAX_CAPTURED_LINES];
+static int captured_count;
+
+static void test_capture_dev_printk(const char *level, const struct device *dev,
+				    const char *fmt, va_list args)
+{
+	if (captured_count < MAX_CAPTURED_LINES) {
+		captured_logs[captured_count].level = level;
+		captured_logs[captured_count].dev = dev;

[Severity: Medium]
Is there a risk of an out-of-bounds memory write here if background SCSI events occur concurrently? The bounds check on the shared captured_count variable is made locklessly, creating a TOCTOU race window. Multiple threads or an interrupt on the same CPU could evaluate the bounds check simultaneously and then proceed to write into the array.

drivers/scsi/scsi_logging_test.c line 37

Quoted 4 lines
drivers/scsi/scsi_logging_test.c new file +4 -0
+		vscnprintf(captured_logs[captured_count].msg,
+			   sizeof(captured_logs[captured_count].msg), fmt,
+			   args);
+		captured_count++;

[Severity: Medium]
This increment further contributes to the race, as concurrent executions would increment captured_count beyond MAX_CAPTURED_LINES after writing out of bounds.

Quoted 20 lines
drivers/scsi/scsi_logging_test.c new file +20 -0
+	}
+}
+
+static void scsi_logging_test_reset(void)
+{
+	captured_count = 0;
+	memset(captured_logs, 0, sizeof(captured_logs));
+}
+
+static int scsi_logging_test_init(struct kunit *test)
+{
+	scsi_logging_test_reset();
+	scsi_logging_test_dev_printk = test_capture_dev_printk;
+	return 0;
+}
+
+static void scsi_logging_test_exit(struct kunit *test)
+{
+	scsi_logging_test_dev_printk = NULL;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/2 scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

In reply to Bart Van Assche

The SCSI logging functions allocate a temporary buffer. This approach has multiple disadvantages:
- Runtime overhead.
- Truncation of log messages to 127 characters.
- Messages are discarded if buffer allocation fails.

Hence this patch that eliminates temporary buffer allocations. Instead, use struct va_format (%pV) and format directly with dev_printk().

Introduce sdev_tag_prefix_vprintk() and sdev_tag_prefix_printk() helper functions that emit dev_printk() messages with optional device name ([<name>]) and request tag (tag#<tag>) prefixes.

Convert sdev_prefix_printk(), scmd_printk(), scsi_print_command(), scsi_log_dump_sense(), scsi_log_print_sense_hdr(), and scsi_print_result() to use these helpers. Use the %*ph format specifier to format CDB and sense hex buffers directly.

Remove scsi_log_reserve_buffer(), scsi_log_release_buffer(), and sdev_format_header().

This patch has been generated by asking Gemini 3.7 to eliminate the scsi_log_*_buffer() functions from the scsi_logging.c file, followed by some minor manual improvements.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_logging.c | 263 ++++++++++++------------------------
 1 file changed, 90 insertions(+), 173 deletions(-)
drivers/scsi/scsi_logging.c +90 -173
index 3bff4dea4c6a..0991e5cb305a 100644
@@ -41,17 +41,6 @@ static void scsi_logging_dev_printk(const char *level, const struct device *dev,
 	scsi_logging_dev_printk(level, dev, fmt, ##__VA_ARGS__)
 #endif
 
-static char *scsi_log_reserve_buffer(size_t *len)
-{
-	*len = 128;
-	return kmalloc(*len, GFP_ATOMIC);
-}
-
-static void scsi_log_release_buffer(char *bufptr)
-{
-	kfree(bufptr);
-}
-
 static inline const char *scmd_name(struct scsi_cmnd *scmd)
 {
 	const struct request *rq = scsi_cmd_to_rq(scmd);
@@ -61,48 +50,57 @@ static inline const char *scmd_name(struct scsi_cmnd *scmd)
 	return rq->q->disk->disk_name;
 }
 
-static size_t sdev_format_header(char *logbuf, size_t logbuf_len,
-				 const char *name, int tag)
+static void __printf(5, 0)
+sdev_tag_prefix_vprintk(const char *level, const struct scsi_device *sdev,
+			const char *name, int tag, const char *fmt,
+			va_list *args)
 {
-	size_t off = 0;
+	const struct device *dev;
+	struct va_format vaf = {
+		.fmt = fmt,
+		.va = args,
+	};
 
-	if (name)
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "[%s] ", name);
+	if (!sdev)
+		return;
 
-	if (WARN_ON(off >= logbuf_len))
-		return off;
+	dev = &sdev->sdev_gendev;
+	if (name) {
+		if (tag >= 0)
+			dev_printk(level, dev, "[%s] tag#%d %pV", name, tag,
+				   &vaf);
+		else
+			dev_printk(level, dev, "[%s] %pV", name, &vaf);
+	} else {
+		if (tag >= 0)
+			dev_printk(level, dev, "tag#%d %pV", tag, &vaf);
+		else
+			dev_printk(level, dev, "%pV", &vaf);
+	}
+}
 
-	if (tag >= 0)
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "tag#%d ", tag);
-	return off;
+static void __printf(5, 6)
+sdev_tag_prefix_printk(const char *level, const struct scsi_device *sdev,
+		       const char *name, int tag, const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	sdev_tag_prefix_vprintk(level, sdev, name, tag, fmt, &args);
+	va_end(args);
 }
 
 void sdev_prefix_printk(const char *level, const struct scsi_device *sdev,
 			const char *name, const char *fmt, ...)
 {
 	va_list args;
-	char *logbuf;
-	size_t off = 0, logbuf_len;
 
 	if (!sdev)
 		return;
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
-		return;
-
-	if (name)
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "[%s] ", name);
-	if (!WARN_ON(off >= logbuf_len)) {
-		va_start(args, fmt);
-		off += vscnprintf(logbuf + off, logbuf_len - off, fmt, args);
-		va_end(args);
-	}
-	dev_printk(level, &sdev->sdev_gendev, "%s", logbuf);
-	scsi_log_release_buffer(logbuf);
+	va_start(args, fmt);
+	sdev_tag_prefix_vprintk(level, sdev, name, -1, fmt, &args);
+	va_end(args);
 }
 EXPORT_SYMBOL(sdev_prefix_printk);
 
@@ -110,24 +108,14 @@ void scmd_printk(const char *level, struct scsi_cmnd *scmd, const char *fmt,
 		 ...)
 {
 	va_list args;
-	char *logbuf;
-	size_t off = 0, logbuf_len;
 
 	if (!scmd)
 		return;
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
-		return;
-	off = sdev_format_header(logbuf, logbuf_len, scmd_name(scmd),
-				 scsi_cmd_to_rq(scmd)->tag);
-	if (off < logbuf_len) {
-		va_start(args, fmt);
-		off += vscnprintf(logbuf + off, logbuf_len - off, fmt, args);
-		va_end(args);
-	}
-	dev_printk(level, &scmd->device->sdev_gendev, "%s", logbuf);
-	scsi_log_release_buffer(logbuf);
+	va_start(args, fmt);
+	sdev_tag_prefix_vprintk(level, scmd->device, scmd_name(scmd),
+				scsi_cmd_to_rq(scmd)->tag, fmt, &args);
+	va_end(args);
 }
 EXPORT_SYMBOL(scmd_printk);
 
@@ -205,60 +193,34 @@ EXPORT_SYMBOL(__scsi_format_command);
 
 void scsi_print_command(struct scsi_cmnd *cmd)
 {
+	char opcode_name[64];
 	int k;
-	char *logbuf;
-	size_t off, logbuf_len;
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
+	if (!cmd)
 		return;
 
-	off = sdev_format_header(logbuf, logbuf_len,
-				 scmd_name(cmd), scsi_cmd_to_rq(cmd)->tag);
-	if (off >= logbuf_len)
-		goto out_printk;
-	off += scnprintf(logbuf + off, logbuf_len - off, "CDB: ");
-	if (WARN_ON(off >= logbuf_len))
-		goto out_printk;
-
-	off += scsi_format_opcode_name(logbuf + off, logbuf_len - off,
-				       cmd->cmnd);
-	if (off >= logbuf_len)
-		goto out_printk;
+	scsi_format_opcode_name(opcode_name, sizeof(opcode_name), cmd->cmnd);
 
-	/* print out all bytes in cdb */
 	if (cmd->cmd_len > 16) {
 		/* Print opcode in one line and use separate lines for CDB */
-		off += scnprintf(logbuf + off, logbuf_len - off, "\n");
-		dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf);
+		sdev_tag_prefix_printk(KERN_INFO, cmd->device, scmd_name(cmd),
+				       scsi_cmd_to_rq(cmd)->tag, "CDB: %s\n",
+				       opcode_name);
 		for (k = 0; k < cmd->cmd_len; k += 16) {
-			size_t linelen = min(cmd->cmd_len - k, 16);
-
-			off = sdev_format_header(logbuf, logbuf_len,
-						 scmd_name(cmd),
-						 scsi_cmd_to_rq(cmd)->tag);
-			if (!WARN_ON(off > logbuf_len - 58)) {
-				off += scnprintf(logbuf + off, logbuf_len - off,
-						 "CDB[%02x]: ", k);
-				hex_dump_to_buffer(&cmd->cmnd[k], linelen,
-						   16, 1, logbuf + off,
-						   logbuf_len - off, false);
-			}
-			dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s",
-				   logbuf);
+			size_t linelen = min_t(size_t, cmd->cmd_len - k, 16);
+
+			sdev_tag_prefix_printk(KERN_INFO, cmd->device,
+					       scmd_name(cmd),
+					       scsi_cmd_to_rq(cmd)->tag,
+					       "CDB[%02x]: %*ph", k,
+					       (int)linelen, &cmd->cmnd[k]);
 		}
-		goto out;
-	}
-	if (!WARN_ON(off > logbuf_len - 49)) {
-		off += scnprintf(logbuf + off, logbuf_len - off, " ");
-		hex_dump_to_buffer(cmd->cmnd, cmd->cmd_len, 16, 1,
-				   logbuf + off, logbuf_len - off,
-				   false);
+	} else {
+		sdev_tag_prefix_printk(KERN_INFO, cmd->device, scmd_name(cmd),
+				       scsi_cmd_to_rq(cmd)->tag, "CDB: %s %*ph",
+				       opcode_name, (int)cmd->cmd_len,
+				       cmd->cmnd);
 	}
-out_printk:
-	dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf);
-out:
-	scsi_log_release_buffer(logbuf);
 }
 EXPORT_SYMBOL(scsi_print_command);
 
@@ -318,51 +280,29 @@ static void
 scsi_log_dump_sense(const struct scsi_device *sdev, const char *name, int tag,
 		    const unsigned char *sense_buffer, int sense_len)
 {
-	char *logbuf;
-	size_t logbuf_len;
 	int i;
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
-		return;
-
 	for (i = 0; i < sense_len; i += 16) {
 		int len = min(sense_len - i, 16);
-		size_t off;
-
-		off = sdev_format_header(logbuf, logbuf_len,
-					 name, tag);
-		hex_dump_to_buffer(&sense_buffer[i], len, 16, 1,
-				   logbuf + off, logbuf_len - off,
-				   false);
-		dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf);
+
+		sdev_tag_prefix_printk(KERN_INFO, sdev, name, tag, "%*ph", len,
+				       &sense_buffer[i]);
 	}
-	scsi_log_release_buffer(logbuf);
 }
 
 static void
 scsi_log_print_sense_hdr(const struct scsi_device *sdev, const char *name,
 			 int tag, const struct scsi_sense_hdr *sshdr)
 {
-	char *logbuf;
-	size_t off, logbuf_len;
+	char sense_hdr[64];
+	char extd_sense[96];
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
-		return;
-	off = sdev_format_header(logbuf, logbuf_len, name, tag);
-	off += scsi_format_sense_hdr(logbuf + off, logbuf_len - off, sshdr);
-	dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf);
-	scsi_log_release_buffer(logbuf);
+	scsi_format_sense_hdr(sense_hdr, sizeof(sense_hdr), sshdr);
+	sdev_tag_prefix_printk(KERN_INFO, sdev, name, tag, "%s", sense_hdr);
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
-		return;
-	off = sdev_format_header(logbuf, logbuf_len, name, tag);
-	off += scsi_format_extd_sense(logbuf + off, logbuf_len - off,
-				      sshdr->asc, sshdr->ascq);
-	dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf);
-	scsi_log_release_buffer(logbuf);
+	scsi_format_extd_sense(extd_sense, sizeof(extd_sense), sshdr->asc,
+			       sshdr->ascq);
+	sdev_tag_prefix_printk(KERN_INFO, sdev, name, tag, "%s", extd_sense);
 }
 
 static void
@@ -407,59 +347,36 @@ EXPORT_SYMBOL(scsi_print_sense);
 
 void scsi_print_result(struct scsi_cmnd *cmd, const char *msg, int disposition)
 {
-	char *logbuf;
-	size_t off, logbuf_len;
 	const char *mlret_string = scsi_mlreturn_string(disposition);
 	const char *hb_string = scsi_hostbyte_string(cmd->result);
 	unsigned long cmd_age = (jiffies - cmd->jiffies_at_alloc) / HZ;
+	char mlret_buf[32];
+	char hb_buf[40];
 
-	logbuf = scsi_log_reserve_buffer(&logbuf_len);
-	if (!logbuf)
-		return;
-
-	off = sdev_format_header(logbuf, logbuf_len, scmd_name(cmd),
-				 scsi_cmd_to_rq(cmd)->tag);
-
-	if (off >= logbuf_len)
-		goto out_printk;
-
-	if (msg) {
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "%s: ", msg);
-		if (WARN_ON(off >= logbuf_len))
-			goto out_printk;
-	}
 	if (mlret_string)
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "%s ", mlret_string);
+		snprintf(mlret_buf, sizeof(mlret_buf), "%s", mlret_string);
 	else
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "UNKNOWN(0x%02x) ", disposition);
-	if (WARN_ON(off >= logbuf_len))
-		goto out_printk;
-
-	off += scnprintf(logbuf + off, logbuf_len - off, "Result: ");
-	if (WARN_ON(off >= logbuf_len))
-		goto out_printk;
+		snprintf(mlret_buf, sizeof(mlret_buf), "UNKNOWN(0x%02x)",
+			 disposition);
 
 	if (hb_string)
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "hostbyte=%s ", hb_string);
+		snprintf(hb_buf, sizeof(hb_buf), "hostbyte=%s", hb_string);
 	else
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "hostbyte=0x%02x ", host_byte(cmd->result));
-	if (WARN_ON(off >= logbuf_len))
-		goto out_printk;
-
-	off += scnprintf(logbuf + off, logbuf_len - off,
-			 "driverbyte=DRIVER_OK ");
-
-	off += scnprintf(logbuf + off, logbuf_len - off,
-			 "cmd_age=%lus", cmd_age);
-
-out_printk:
-	dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf);
-	scsi_log_release_buffer(logbuf);
+		snprintf(hb_buf, sizeof(hb_buf), "hostbyte=0x%02x",
+			 host_byte(cmd->result));
+
+	if (msg)
+		sdev_tag_prefix_printk(
+			KERN_INFO, cmd->device, scmd_name(cmd),
+			scsi_cmd_to_rq(cmd)->tag,
+			"%s: %s Result: %s driverbyte=DRIVER_OK cmd_age=%lus",
+			msg, mlret_buf, hb_buf, cmd_age);
+	else
+		sdev_tag_prefix_printk(
+			KERN_INFO, cmd->device, scmd_name(cmd),
+			scsi_cmd_to_rq(cmd)->tag,
+			"%s Result: %s driverbyte=DRIVER_OK cmd_age=%lus",
+			mlret_buf, hb_buf, cmd_age);
 }
 EXPORT_SYMBOL(scsi_print_result);
 
#4 lore raw

Re: [PATCH v2 2/2] scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

Quoted 85 lines · Bart Van Assche

The SCSI logging functions allocate a temporary buffer. This approach has multiple disadvantages:
- Runtime overhead.
- Truncation of log messages to 127 characters.
- Messages are discarded if buffer allocation fails.

Hence this patch that eliminates temporary buffer allocations. Instead, use struct va_format (%pV) and format directly with dev_printk().

Introduce sdev_tag_prefix_vprintk() and sdev_tag_prefix_printk() helper functions that emit dev_printk() messages with optional device name ([<name>]) and request tag (tag#<tag>) prefixes.

Convert sdev_prefix_printk(), scmd_printk(), scsi_print_command(), scsi_log_dump_sense(), scsi_log_print_sense_hdr(), and scsi_print_result() to use these helpers. Use the %*ph format specifier to format CDB and sense hex buffers directly.

Remove scsi_log_reserve_buffer(), scsi_log_release_buffer(), and sdev_format_header().

This patch has been generated by asking Gemini 3.7 to eliminate the scsi_log_*_buffer() functions from the scsi_logging.c file, followed by some minor manual improvements.

Cc Hannes Reinecke <[email protected]>
Cc John Garry <[email protected]>
---
  drivers/scsi/scsi_logging.c | 263 ++++++++++++------------------------
  1 file changed, 90 insertions(+), 173 deletions(-)
drivers/scsi/scsi_logging.c +16 -19
index 3bff4dea4c6a..0991e5cb305a 100644
@@ -41,17 +41,6 @@ static void scsi_logging_dev_printk(const char *level, const struct device *dev,
  	scsi_logging_dev_printk(level, dev, fmt, ##__VA_ARGS__)
  #endif
 
-static char *scsi_log_reserve_buffer(size_t *len)
-{
-	*len = 128;
-	return kmalloc(*len, GFP_ATOMIC);
-}
-
-static void scsi_log_release_buffer(char *bufptr)
-{
-	kfree(bufptr);
-}
-
  static inline const char *scmd_name(struct scsi_cmnd *scmd)
  {
  	const struct request *rq = scsi_cmd_to_rq(scmd);
@@ -61,48 +50,57 @@ static inline const char *scmd_name(struct scsi_cmnd *scmd)
  	return rq->q->disk->disk_name;
  }
 
-static size_t sdev_format_header(char *logbuf, size_t logbuf_len,
-				 const char *name, int tag)
+static void __printf(5, 0)
+sdev_tag_prefix_vprintk(const char *level, const struct scsi_device *sdev,
+			const char *name, int tag, const char *fmt,
+			va_list *args)
  {
-	size_t off = 0;
+	const struct device *dev;
+	struct va_format vaf = {
+		.fmt = fmt,
+		.va = args,
+	};
 
-	if (name)
-		off += scnprintf(logbuf + off, logbuf_len - off,
-				 "[%s] ", name);
+	if (!sdev)
+		return;
 
-	if (WARN_ON(off >= logbuf_len))
-		return off;
+	dev = &sdev->sdev_gendev;
+	if (name) {
+		if (tag >= 0)
+			dev_printk(level, dev, "[%s] tag#%d %pV", name, tag,
+				   &vaf);

drivers/scsi/scsi_logging.c static inline const char *scmd_name(struct scsi_cmnd *scmd) · line 81

Quoted 10 lines
drivers/scsi/scsi_logging.c +9 -0
+		else
+			dev_printk(level, dev, "[%s] %pV", name, &vaf);
+	} else {
+		if (tag >= 0)
+			dev_printk(level, dev, "tag#%d %pV", tag, &vaf);
+		else
+			dev_printk(level, dev, "%pV", &vaf);
+	}
+}
 

But that would completely revert the idea of this patch; 'dev_printk' will always insert a newline after the string argument, and the whole point of this patch was that do _not_ want to have a newline here as we want to print out several things before terminating the log message.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#5 lore raw

Re: [PATCH v2 2/2] scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

Quoted 2 lines · Hannes Reinecke

But that would completely revert the idea of this patch; 'dev_printk' will always insert a newline after the string argument, and the whole point of this patch was that do _not_ want to have a newline here as we want to print out several things before terminating the log message.

KERN_CONT should be avoided whenever possible because it causes log lines to be mixed up if a context switch happens in the middle of multiple printk() statements that use KERN_CONT. Do you perhaps want me to include patches that eliminate the use of KERN_CONT from SCSI code (NCR5380, aic7xxx, ch, mac53c94, megaraid, qla1280 and sd)?

Thanks,

Bart.

#6 lore raw

Re: [PATCH v2 2/2] scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

Quoted 10 lines · Bart Van Assche
Quoted 2 lines · Hannes Reinecke

But that would completely revert the idea of this patch; 'dev_printk' will always insert a newline after the string argument, and the whole point of this patch was that do _not_ want to have a newline here as we want to print out several things before terminating the log message.

KERN_CONT should be avoided whenever possible because it causes log lines to be mixed up if a context switch happens in the middle of multiple printk() statements that use KERN_CONT. Do you perhaps want me to include patches that eliminate the use of KERN_CONT from SCSI code (NCR5380, aic7xxx, ch, mac53c94, megaraid, qla1280 and sd)?

I _know_ about KERN_CONT.
And the patchset introducing the scsi_log was _precisely_ to avoid having to use KERN_CONT.
But in removing scsi_log we're back to square one, namely that we cannot assemble a single line of logging with several calls (eg as required when dumping a hex buffer or a SCSI command).

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#7 lore raw

Re: [PATCH v2 2/2] scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

Quoted 3 lines · Hannes Reinecke

But in removing scsi_log we're back to square one, namely that we cannot assemble a single line of logging with several calls (eg as required when dumping a hex buffer or a SCSI command).

This patch uses %*ph to dump hex buffers. No temporary buffers are required to dump a hex buffer. The temporary buffers introduced by this patch are as follows:

+ char opcode_name[64];
+ char sense_hdr[64];
+ char extd_sense[96];
+ char mlret_buf[32];
+ char hb_buf[40];

Personally I prefer the introduction of these temporary buffers rather than making every sdev_prefix_printk() call perform a fixed-size GFP_ATOMIC allocation. This patch eliminates the risk of log line suppression because of GFP_ATOMIC memory allocation failure and also eliminates the 128 byte line limit length.

Thanks,

Bart.

#8 lore raw

Re: [PATCH v2 2/2] scsi: core: Eliminate scsi_log_{reserve,release}_buffer()

In reply to Hannes Reinecke
Quoted 30 lines · Hannes Reinecke
Quoted 29 lines · Bart Van Assche
drivers/scsi/scsi_logging.c +2 -2
-static size_t sdev_format_header(char *logbuf, size_t logbuf_len,
-                 const char *name, int tag)
+static void __printf(5, 0)
+sdev_tag_prefix_vprintk(const char *level, const struct scsi_device 

*sdev,
+            const char *name, int tag, const char *fmt, +            va_list *args)
  {
-    size_t off = 0;
+    const struct device *dev;
+    struct va_format vaf = {
+        .fmt = fmt,
+        .va = args,
+    };
-    if (name)
-        off += scnprintf(logbuf + off, logbuf_len - off, -                 "[%s] ", name);
+    if (!sdev)
+        return;
-    if (WARN_ON(off >= logbuf_len))
-        return off;
+    dev = &sdev->sdev_gendev;
+    if (name) {
+        if (tag >= 0)
+            dev_printk(level, dev, "[%s] tag#%d %pV", name, tag, +                   &vaf);
+        else
+            dev_printk(level, dev, "[%s] %pV", name, &vaf); +    } else {

Quoted 8 lines
Quoted 6 lines

+        if (tag >= 0)
+            dev_printk(level, dev, "tag#%d %pV", tag, &vaf); +        else
+            dev_printk(level, dev, "%pV", &vaf);
+    }
+}

But that would completely revert the idea of this patch; 'dev_printk' will always insert a newline after the string argument, and the whole point of this patch was that do _not_ want to have a newline here as we want to print out several things before terminating the log message.

No direct nor any indirect callers of this function use KERN_CONT, isn't it? Please let me know if I overlooked something.

Bart.