Skip to contentSkip to conversation

fs/seq_file: introduce seq_hex_dump() helper

PATCH Superseded Andy Shevchenko 2 participants 17 messages Sep 4, 2014 – Oct 22, 2014

Subsystems (12)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover fs/seq_file: introduce seq_hex_dump() helper

This introduces a new helper and switches current users to use it. All are compiled tested, hexdump and kmemleak are tested by their test suits.

Changelog v4:
- hexdump is amended to return value from which we can check an overflow (patches 1/12 - 4/12)
- seq_hex_dump pushes bytes directly to buffer and returns an error in case of overflow (addresses Al Viro's comment)
- append Acked-by for patches 8/12 and 9/12
- convert more users (patches 10/12 - 12/12)

Changelog v3:
- append Mauro's Ack
- rebase on top of recent linux-next

Changelog v2:
- append Acked-by and Reviewed-by tags
- update commit messages in patches 3/5. and 5/5
- update line size to be 32 bytes instead of 16 in patch 3/5
- Joe found that output is changed in patch 4/5, thus I update commit message there

Andy Shevchenko (12):
hexdump: introduce test suite
hexdump: fix ascii column for the tail of a dump
hexdump: do few calculations ahead
hexdump: makes it return amount of bytes placed in buffer
seq_file: provide an analogue of print_hex_dump()
saa7164: convert to seq_hex_dump()
crypto: qat - use seq_hex_dump() to dump buffers
parisc: use seq_hex_dump() to dump buffers [S390] zcrypt: use seq_hex_dump() to dump buffers
staging: unisys: use seq_hex_dump() to dump buffers
kmemleak: use seq_hex_dump() to dump buffers
wil6210: use seq_hex_dump() to dump buffers

 .../crypto/qat/qat_common/adf_transport_debug.c    |  16 +-
 drivers/media/pci/saa7164/saa7164-core.c           |  31 +---
 drivers/net/wireless/ath/wil6210/debugfs.c         |  32 +---
 drivers/parisc/ccio-dma.c                          |  14 +-
 drivers/parisc/sba_iommu.c                         |  11 +-
 drivers/s390/crypto/zcrypt_api.c                   |  10 +-
 .../unisys/visorchannel/visorchannel_funcs.c       |  26 +--
 fs/seq_file.c                                      |  50 ++++++
 include/linux/printk.h                             |   6 +-
 include/linux/seq_file.h                           |   4 +
 lib/Kconfig.debug                                  |   3 +
 lib/Makefile                                       |   4 +-
 lib/hexdump.c                                      | 105 +++++++-----
 lib/test-hexdump.c                                 | 180 +++++++++++++++++++++
 mm/kmemleak.c                                      |  21 +--
 15 files changed, 337 insertions(+), 176 deletions(-)
 create mode 100644 lib/test-hexdump.c
Signature · 1 line
2.1.0
#1 lore raw

PATCH v4 1/12 hexdump: introduce test suite

Test different scenarios of function calls located in lib/hexdump.c.

Currently hex_dump_to_buffer() is only tested and test data is provided for little endian CPUs.

---
 lib/Kconfig.debug  |   3 ++
 lib/Makefile       |   4 +-
 lib/test-hexdump.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+), 1 deletion(-)
 create mode 100644 lib/test-hexdump.c
lib/Kconfig.debug +3 -0
index 2870e2e..abcbf88 100644
@@ -1563,6 +1563,9 @@ config ASYNC_RAID6_TEST
 
 	  If unsure, say N.
 
+config TEST_HEXDUMP
+	tristate "Test functions located in the hexdump module at runtime"
+
 config TEST_STRING_HELPERS
 	tristate "Test functions located in the string_helpers module at runtime"
 
lib/Makefile +3 -1
index b73c3c3..06bd377 100644
@@ -23,12 +23,14 @@ lib-y	+= kobject.o klist.o
 obj-y	+= lockref.o
 
 obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
-	 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \
+	 bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \
 	 gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \
 	 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \
 	 percpu-refcount.o percpu_ida.o hash.o rhashtable.o
 obj-y += string_helpers.o
 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
+obj-y += hexdump.o
+obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o
 obj-y += kstrtox.o
 obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
 obj-$(CONFIG_TEST_MODULE) += test_module.o
lib/test-hexdump.c new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
2.1.0
#2 lore raw

Re: [PATCH v4 01/12] hexdump: introduce test suite

Quoted 4 lines · Andy Shevchenko

Test different scenarios of function calls located in lib/hexdump.c.

Currently hex_dump_to_buffer() is only tested and test data is provided for little endian CPUs.

That's a nice one to be amended using Chen's "arch: Kconfig: Let all little endian architectures define CPU_LITTLE_ENDIAN explicitly".

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#3 lore raw

Re: [PATCH v4 01/12] hexdump: introduce test suite

Quoted 10 lines · Geert Uytterhoeven
Quoted 4 lines · Andy Shevchenko

Test different scenarios of function calls located in lib/hexdump.c.

Currently hex_dump_to_buffer() is only tested and test data is provided for little endian CPUs.

That's a nice one to be amended using Chen's "arch: Kconfig: Let all little endian architectures define CPU_LITTLE_ENDIAN explicitly".

I don't see it in the upstream yet, right? Thus, I wouldn't like to depend on it now.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#4 lore raw

PATCH v4 2/12 hexdump: fix ascii column for the tail of a dump

In reply to Andy Shevchenko

In current implementation we have floating ascii column in the tail of the dump.

For example, for row size equal to 16 the ascii column as in following table

group size \ length 8 12 16

	1		50	50	50
	2		22	32	42
	4		20	29	38
	8		19	-	36

This patch makes it the same independently of amount of bytes dumped.

The change is safe since all current users, which use ASCII part of the dump, rely on the group size equal to 1. The patch doesn't change behaviour for such group size (see the table above).

---
 lib/hexdump.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/hexdump.c +3 -3
index 8499c81..4677070 100644
@@ -110,7 +110,7 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 			lx += scnprintf(linebuf + lx, linebuflen - lx,
 					"%s%16.16llx", j ? " " : "",
 					(unsigned long long)*(ptr8 + j));
-		ascii_column = 17 * ngroups + 2;
+		ascii_column = rowsize * 2 + rowsize / 8 + 2;
 		break;
 	}
 
@@ -121,7 +121,7 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 		for (j = 0; j < ngroups; j++)
 			lx += scnprintf(linebuf + lx, linebuflen - lx,
 					"%s%8.8x", j ? " " : "", *(ptr4 + j));
-		ascii_column = 9 * ngroups + 2;
+		ascii_column = rowsize * 2 + rowsize / 4 + 2;
 		break;
 	}
 
@@ -132,7 +132,7 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 		for (j = 0; j < ngroups; j++)
 			lx += scnprintf(linebuf + lx, linebuflen - lx,
 					"%s%4.4x", j ? " " : "", *(ptr2 + j));
-		ascii_column = 5 * ngroups + 2;
+		ascii_column = rowsize * 2 + rowsize / 2 + 2;
 		break;
 	}
 
Signature · 1 line
2.1.0
#5 lore raw

PATCH v4 3/12 hexdump: do few calculations ahead

In reply to Andy Shevchenko

Instead of doing calculations in each case of different groupsize let's do them beforehand. While here, change the switch by if-else-if construction.

---
 lib/hexdump.c | 34 ++++++++++------------------------
 1 file changed, 10 insertions(+), 24 deletions(-)
lib/hexdump.c +10 -24
index 4677070..b82418f 100644
@@ -87,6 +87,7 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 			bool ascii)
 {
 	const u8 *ptr = buf;
+	int ngroups;
 	u8 ch;
 	int j, lx = 0;
 	int ascii_column;
@@ -98,45 +99,33 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 		goto nil;
 	if (len > rowsize)		/* limit to one line at a time */
 		len = rowsize;
+	if (!is_power_of_2(groupsize) || groupsize > 8)
+		groupsize = 1;
 	if ((len % groupsize) != 0)	/* no mixed size output */
 		groupsize = 1;
 
-	switch (groupsize) {
-	case 8: {
+	ngroups = len / groupsize;
+	ascii_column = rowsize * 2 + rowsize / groupsize + 1;
+	if (groupsize == 8) {
 		const u64 *ptr8 = buf;
-		int ngroups = len / groupsize;
 
 		for (j = 0; j < ngroups; j++)
 			lx += scnprintf(linebuf + lx, linebuflen - lx,
 					"%s%16.16llx", j ? " " : "",
 					(unsigned long long)*(ptr8 + j));
-		ascii_column = rowsize * 2 + rowsize / 8 + 2;
-		break;
-	}
-
-	case 4: {
+	} else if (groupsize == 4) {
 		const u32 *ptr4 = buf;
-		int ngroups = len / groupsize;
 
 		for (j = 0; j < ngroups; j++)
 			lx += scnprintf(linebuf + lx, linebuflen - lx,
 					"%s%8.8x", j ? " " : "", *(ptr4 + j));
-		ascii_column = rowsize * 2 + rowsize / 4 + 2;
-		break;
-	}
-
-	case 2: {
+	} else if (groupsize == 2) {
 		const u16 *ptr2 = buf;
-		int ngroups = len / groupsize;
 
 		for (j = 0; j < ngroups; j++)
 			lx += scnprintf(linebuf + lx, linebuflen - lx,
 					"%s%4.4x", j ? " " : "", *(ptr2 + j));
-		ascii_column = rowsize * 2 + rowsize / 2 + 2;
-		break;
-	}
-
-	default:
+	} else {
 		for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
 			ch = ptr[j];
 			linebuf[lx++] = hex_asc_hi(ch);
@@ -145,14 +134,11 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
 		}
 		if (j)
 			lx--;
-
-		ascii_column = 3 * rowsize + 2;
-		break;
 	}
 	if (!ascii)
 		goto nil;
 
-	while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
+	while (lx < (linebuflen - 1) && lx < ascii_column)
 		linebuf[lx++] = ' ';
 	for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
 		ch = ptr[j];
Signature · 1 line
2.1.0
#6 lore raw

PATCH v4 4/12 hexdump: makes it return amount of bytes placed in buffer

In reply to Andy Shevchenko

This patch makes hexdump to return amount of bytes placed in the buffer excluding trailing NUL. In case of overflow it returns desired amount of bytes to place entire dump. Thus, it mimics snprintf().

This will be useful for users that would like to repeat with bigger buffer.

---
 include/linux/printk.h |  6 ++---
 lib/hexdump.c          | 73 +++++++++++++++++++++++++++++++++++++-------------
 lib/test-hexdump.c     | 45 +++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 21 deletions(-)
include/linux/printk.h +3 -3
index d78125f..a6c2771 100644
@@ -407,9 +407,9 @@ enum {
 	DUMP_PREFIX_ADDRESS,
 	DUMP_PREFIX_OFFSET
 };
-extern void hex_dump_to_buffer(const void *buf, size_t len,
-			       int rowsize, int groupsize,
-			       char *linebuf, size_t linebuflen, bool ascii);
+extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
+			      int groupsize, char *linebuf, size_t linebuflen,
+			      bool ascii);
 #ifdef CONFIG_PRINTK
 extern void print_hex_dump(const char *level, const char *prefix_str,
 			   int prefix_type, int rowsize, int groupsize,
lib/hexdump.c +55 -18

118 lines not on this page · raw message

lib/test-hexdump.c +45 -0

57 lines not on this page · raw message

Signature · 1 line
2.1.0
#7 lore raw

PATCH v4 5/12 seq_file: provide an analogue of print_hex_dump()

In reply to Andy Shevchenko

The new seq_hex_dump() is a complete analogue of print_hex_dump().

We have few users of this functionality already. It allows to reduce their codebase.

---
 fs/seq_file.c            | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/seq_file.h |  4 ++++
 2 files changed, 54 insertions(+)
fs/seq_file.c +50 -0
index 3857b72..66c721f4 100644
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/cred.h>
 #include <linux/mm.h>
+#include <linux/printk.h>
 
 #include <asm/uaccess.h>
 #include <asm/page.h>
@@ -794,6 +795,55 @@ void seq_pad(struct seq_file *m, char c)
 }
 EXPORT_SYMBOL(seq_pad);
 
+/* Analogue of print_hex_dump() */
+int seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
+		 int rowsize, int groupsize, const void *buf, size_t len,
+		 bool ascii)
+{
+	const u8 *ptr = buf;
+	int i, linelen, remaining = len;
+	int ret;
+
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
+
+	for (i = 0; i < len; i += rowsize) {
+		linelen = min(remaining, rowsize);
+		remaining -= rowsize;
+
+		/* Prefix string */
+		ret = seq_printf(m, "%s", prefix_str);
+		if (ret < 0)
+			return ret;
+
+		/* Counter if asked */
+		if (prefix_type == DUMP_PREFIX_ADDRESS)
+			ret = seq_printf(m, "%p: ", ptr + i);
+		else if (prefix_type == DUMP_PREFIX_OFFSET)
+			ret = seq_printf(m, "%.8x: ", i);
+		if (ret < 0)
+			return ret;
+
+		/* Hex dump */
+		ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+					 m->buf + m->count, m->size - m->count,
+					 ascii);
+		if (m->count + ret >= m->size) {
+			seq_set_overflow(m);
+			return -1;
+		}
+		m->count += ret;
+
+		/* EOL */
+		ret = seq_putc(m, '\n');
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(seq_hex_dump);
+
 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
 {
 	struct list_head *lh;
include/linux/seq_file.h +4 -0
index 52e0097..5de290c 100644
@@ -107,6 +107,10 @@ int seq_write(struct seq_file *seq, const void *data, size_t len);
 __printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
 __printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
 
+int seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
+		 int rowsize, int groupsize, const void *buf, size_t len,
+		 bool ascii);
+
 int seq_path(struct seq_file *, const struct path *, const char *);
 int seq_dentry(struct seq_file *, struct dentry *, const char *);
 int seq_path_root(struct seq_file *m, const struct path *path,
Signature · 1 line
2.1.0
#8 lore raw

Re: [PATCH v4 05/12] seq_file: provide an analogue of print_hex_dump()

Quoted 4 lines · Andy Shevchenko

The new seq_hex_dump() is a complete analogue of print_hex_dump().

We have few users of this functionality already. It allows to reduce their codebase.

Al, what do you think about this version?

Quoted 90 lines
---
 fs/seq_file.c            | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/seq_file.h |  4 ++++
 2 files changed, 54 insertions(+)
fs/seq_file.c +50 -0
index 3857b72..66c721f4 100644
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/cred.h>
 #include <linux/mm.h>
+#include <linux/printk.h>
 
 #include <asm/uaccess.h>
 #include <asm/page.h>
@@ -794,6 +795,55 @@ void seq_pad(struct seq_file *m, char c)
 }
 EXPORT_SYMBOL(seq_pad);
 
+/* Analogue of print_hex_dump() */
+int seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
+		 int rowsize, int groupsize, const void *buf, size_t len,
+		 bool ascii)
+{
+	const u8 *ptr = buf;
+	int i, linelen, remaining = len;
+	int ret;
+
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
+
+	for (i = 0; i < len; i += rowsize) {
+		linelen = min(remaining, rowsize);
+		remaining -= rowsize;
+
+		/* Prefix string */
+		ret = seq_printf(m, "%s", prefix_str);
+		if (ret < 0)
+			return ret;
+
+		/* Counter if asked */
+		if (prefix_type == DUMP_PREFIX_ADDRESS)
+			ret = seq_printf(m, "%p: ", ptr + i);
+		else if (prefix_type == DUMP_PREFIX_OFFSET)
+			ret = seq_printf(m, "%.8x: ", i);
+		if (ret < 0)
+			return ret;
+
+		/* Hex dump */
+		ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+					 m->buf + m->count, m->size - m->count,
+					 ascii);
+		if (m->count + ret >= m->size) {
+			seq_set_overflow(m);
+			return -1;
+		}
+		m->count += ret;
+
+		/* EOL */
+		ret = seq_putc(m, '\n');
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(seq_hex_dump);
+
 struct list_head *seq_list_start(struct list_head *head, loff_t pos)
 {
 	struct list_head *lh;
include/linux/seq_file.h +4 -0
index 52e0097..5de290c 100644
@@ -107,6 +107,10 @@ int seq_write(struct seq_file *seq, const void *data, size_t len);
 __printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
 __printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
 
+int seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
+		 int rowsize, int groupsize, const void *buf, size_t len,
+		 bool ascii);
+
 int seq_path(struct seq_file *, const struct path *, const char *);
 int seq_dentry(struct seq_file *, struct dentry *, const char *);
 int seq_path_root(struct seq_file *m, const struct path *path,
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#9 lore raw

PATCH v4 6/12 saa7164: convert to seq_hex_dump()

In reply to Andy Shevchenko

Instead of custom approach let's use recently added seq_hex_dump() helper.

---
 drivers/media/pci/saa7164/saa7164-core.c | 31 ++++---------------------------
 1 file changed, 4 insertions(+), 27 deletions(-)
drivers/media/pci/saa7164/saa7164-core.c +4 -27
index 1bf0697..6f81584 100644
@@ -1065,7 +1065,6 @@ static int saa7164_proc_show(struct seq_file *m, void *v)
 	struct saa7164_dev *dev;
 	struct tmComResBusInfo *b;
 	struct list_head *list;
-	int i, c;
 
 	if (saa7164_devcount == 0)
 		return 0;
@@ -1089,35 +1088,13 @@ static int saa7164_proc_show(struct seq_file *m, void *v)
 
 		seq_printf(m, " .m_pdwGetReadPos  = 0x%x (0x%08x)\n",
 			b->m_dwGetWritePos, saa7164_readl(b->m_dwGetWritePos));
-		c = 0;
 		seq_printf(m, "\n  Set Ring:\n");
-		seq_printf(m, "\n addr  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
-		for (i = 0; i < b->m_dwSizeSetRing; i++) {
-			if (c == 0)
-				seq_printf(m, " %04x:", i);
+		seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 16, 1,
+			     b->m_pdwSetRing, b->m_dwSizeSetRing, false);
 
-			seq_printf(m, " %02x", *(b->m_pdwSetRing + i));
-
-			if (++c == 16) {
-				seq_printf(m, "\n");
-				c = 0;
-			}
-		}
-
-		c = 0;
 		seq_printf(m, "\n  Get Ring:\n");
-		seq_printf(m, "\n addr  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
-		for (i = 0; i < b->m_dwSizeGetRing; i++) {
-			if (c == 0)
-				seq_printf(m, " %04x:", i);
-
-			seq_printf(m, " %02x", *(b->m_pdwGetRing + i));
-
-			if (++c == 16) {
-				seq_printf(m, "\n");
-				c = 0;
-			}
-		}
+		seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 16, 1,
+			     b->m_pdwGetRing, b->m_dwSizeGetRing, false);
 
 		mutex_unlock(&b->lock);
 
Signature · 1 line
2.1.0
#10 lore raw

PATCH v4 7/12 crypto: qat - use seq_hex_dump() to dump buffers

In reply to Andy Shevchenko

Instead of custom approach let's use recently introduced seq_hex_dump() helper.

---
 drivers/crypto/qat/qat_common/adf_transport_debug.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
drivers/crypto/qat/qat_common/adf_transport_debug.c +2 -14
index 6b69745..5f438a1 100644
@@ -86,9 +86,7 @@ static int adf_ring_show(struct seq_file *sfile, void *v)
 {
 	struct adf_etr_ring_data *ring = sfile->private;
 	struct adf_etr_bank_data *bank = ring->bank;
-	uint32_t *msg = v;
 	void __iomem *csr = ring->bank->csr_addr;
-	int i, x;
 
 	if (v == SEQ_START_TOKEN) {
 		int head, tail, empty;
@@ -111,18 +109,8 @@ static int adf_ring_show(struct seq_file *sfile, void *v)
 		seq_puts(sfile, "----------- Ring data ------------\n");
 		return 0;
 	}
-	seq_printf(sfile, "%p:", msg);
-	x = 0;
-	i = 0;
-	for (; i < (ADF_MSG_SIZE_TO_BYTES(ring->msg_size) >> 2); i++) {
-		seq_printf(sfile, " %08X", *(msg + i));
-		if ((ADF_MSG_SIZE_TO_BYTES(ring->msg_size) >> 2) != i + 1 &&
-		    (++x == 8)) {
-			seq_printf(sfile, "\n%p:", msg + i + 1);
-			x = 0;
-		}
-	}
-	seq_puts(sfile, "\n");
+	seq_hex_dump(sfile, "", DUMP_PREFIX_ADDRESS, 32, 4,
+		     v, ADF_MSG_SIZE_TO_BYTES(ring->msg_size), false);
 	return 0;
 }
 
Signature · 1 line
2.1.0
#11 lore raw

PATCH v4 8/12 parisc: use seq_hex_dump() to dump buffers

In reply to Andy Shevchenko

Instead of custom approach let's use recently introduced seq_hex_dump() helper.

In one case it changes the output from

	1111111122222222333333334444444455555555666666667777777788888888
to
	11111111 22222222 33333333 44444444 55555555 66666666 77777777 88888888

though it seems it prints same data (by meaning) in both cases. I decide to choose to use the space divided one.

---
 drivers/parisc/ccio-dma.c  | 14 +++-----------
 drivers/parisc/sba_iommu.c | 11 +++--------
 2 files changed, 6 insertions(+), 19 deletions(-)
drivers/parisc/ccio-dma.c +3 -11
index 8b490d7..9d353d2 100644
@@ -1101,20 +1101,12 @@ static const struct file_operations ccio_proc_info_fops = {
 
 static int ccio_proc_bitmap_info(struct seq_file *m, void *p)
 {
-	int len = 0;
 	struct ioc *ioc = ioc_list;
 
 	while (ioc != NULL) {
-		u32 *res_ptr = (u32 *)ioc->res_map;
-		int j;
-
-		for (j = 0; j < (ioc->res_size / sizeof(u32)); j++) {
-			if ((j & 7) == 0)
-				len += seq_puts(m, "\n   ");
-			len += seq_printf(m, "%08x", *res_ptr);
-			res_ptr++;
-		}
-		len += seq_puts(m, "\n\n");
+		seq_hex_dump(m, "   ", DUMP_PREFIX_NONE, 32, 4, ioc->res_map,
+			     ioc->res_size, false);
+		seq_putc(m, '\n');
 		ioc = ioc->next;
 		break; /* XXX - remove me */
 	}
drivers/parisc/sba_iommu.c +3 -8
index 1ff1b67..fbc4db9 100644
@@ -1857,15 +1857,10 @@ sba_proc_bitmap_info(struct seq_file *m, void *p)
 {
 	struct sba_device *sba_dev = sba_list;
 	struct ioc *ioc = &sba_dev->ioc[0];	/* FIXME: Multi-IOC support! */
-	unsigned int *res_ptr = (unsigned int *)ioc->res_map;
-	int i, len = 0;
 
-	for (i = 0; i < (ioc->res_size/sizeof(unsigned int)); ++i, ++res_ptr) {
-		if ((i & 7) == 0)
-			len += seq_printf(m, "\n   ");
-		len += seq_printf(m, " %08x", *res_ptr);
-	}
-	len += seq_printf(m, "\n");
+	seq_hex_dump(m, "   ", DUMP_PREFIX_NONE, 32, 4, ioc->res_map,
+		     ioc->res_size, false);
+	seq_printf(m, "\n");
 
 	return 0;
 }
Signature · 1 line
2.1.0
#12 lore raw

PATCH v4 9/12 zcrypt: use seq_hex_dump() to dump buffers

In reply to Andy Shevchenko

Instead of custom approach let's use recently introduced seq_hex_dump() helper.

---
 drivers/s390/crypto/zcrypt_api.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
drivers/s390/crypto/zcrypt_api.c +1 -9
index 0e18c5d..d1f9983 100644
@@ -1203,16 +1203,8 @@ static void sprinthx(unsigned char *title, struct seq_file *m,
 static void sprinthx4(unsigned char *title, struct seq_file *m,
 		      unsigned int *array, unsigned int len)
 {
-	int r;
-
 	seq_printf(m, "\n%s\n", title);
-	for (r = 0; r < len; r++) {
-		if ((r % 8) == 0)
-			seq_printf(m, "    ");
-		seq_printf(m, "%08X ", array[r]);
-		if ((r % 8) == 7)
-			seq_putc(m, '\n');
-	}
+	seq_hex_dump(m, "    ", DUMP_PREFIX_NONE, 32, 4, array, len, false);
 	seq_putc(m, '\n');
 }
 
Signature · 1 line
2.1.0
#13 lore raw

PATCH v4 10/12 staging: unisys: use seq_hex_dump() to dump buffers

In reply to Andy Shevchenko

Instead of custom approach let's use recently introduced seq_hex_dump() helper.

---
 .../unisys/visorchannel/visorchannel_funcs.c       | 26 ++++------------------
 1 file changed, 4 insertions(+), 22 deletions(-)
drivers/staging/unisys/visorchannel/visorchannel_funcs.c +4 -22
index 947b23c..d8345a6 100644
@@ -633,15 +633,11 @@ void
 visorchannel_dump_section(VISORCHANNEL *chan, char *s,
 			  int off, int len, struct seq_file *seq)
 {
-	char *buf, *tbuf, *fmtbuf;
-	int fmtbufsize = 0;
-	int i;
+	char *buf;
 	int errcode = 0;
 
-	fmtbufsize = 100 * COVQ(len, 16);
 	buf = kmalloc(len, GFP_KERNEL|__GFP_NORETRY);
-	fmtbuf = kmalloc(fmtbufsize, GFP_KERNEL|__GFP_NORETRY);
-	if (buf == NULL || fmtbuf == NULL)
+	if (buf == NULL)
 		goto Away;
 
 	errcode = visorchannel_read(chan, off, buf, len);
@@ -651,23 +647,9 @@ visorchannel_dump_section(VISORCHANNEL *chan, char *s,
 		goto Away;
 	}
 	seq_printf(seq, "channel %s:\n", s);
-	tbuf = buf;
-	while (len > 0) {
-		i = (len < 16) ? len : 16;
-		hex_dump_to_buffer(tbuf, i, 16, 1, fmtbuf, fmtbufsize, TRUE);
-		seq_printf(seq, "%s\n", fmtbuf);
-		tbuf += 16;
-		len -= 16;
-	}
+	seq_hex_dump(seq, "", DUMP_PREFIX_NONE, 16, 1, buf, len, true);
 
 Away:
-	if (buf != NULL) {
-		kfree(buf);
-		buf = NULL;
-	}
-	if (fmtbuf != NULL) {
-		kfree(fmtbuf);
-		fmtbuf = NULL;
-	}
+	kfree(buf);
 }
 EXPORT_SYMBOL_GPL(visorchannel_dump_section);
Signature · 1 line
2.1.0
#14 lore raw

PATCH v4 11/12 kmemleak: use seq_hex_dump() to dump buffers

In reply to Andy Shevchenko

Instead of custom approach let's use recently introduced seq_hex_dump() helper.

---
 mm/kmemleak.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
mm/kmemleak.c +6 -15
index 3cda50c..bc14fdc 100644
@@ -291,23 +291,14 @@ static void hex_dump_object(struct seq_file *seq,
 			    struct kmemleak_object *object)
 {
 	const u8 *ptr = (const u8 *)object->pointer;
-	int i, len, remaining;
-	unsigned char linebuf[HEX_ROW_SIZE * 5];
+	size_t len;
 
 	/* limit the number of lines to HEX_MAX_LINES */
-	remaining = len =
-		min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE));
-
-	seq_printf(seq, "  hex dump (first %d bytes):\n", len);
-	for (i = 0; i < len; i += HEX_ROW_SIZE) {
-		int linelen = min(remaining, HEX_ROW_SIZE);
-
-		remaining -= HEX_ROW_SIZE;
-		hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE,
-				   HEX_GROUP_SIZE, linebuf, sizeof(linebuf),
-				   HEX_ASCII);
-		seq_printf(seq, "    %s\n", linebuf);
-	}
+	len = min(object->size, (size_t)(HEX_MAX_LINES * HEX_ROW_SIZE));
+
+	seq_printf(seq, "  hex dump (first %zu bytes):\n", len);
+	seq_hex_dump(seq, "    ", DUMP_PREFIX_NONE, HEX_ROW_SIZE,
+		     HEX_GROUP_SIZE, ptr, len, HEX_ASCII);
 }
 
 /*
Signature · 1 line
2.1.0
#15 lore raw

PATCH v4 12/12 wil6210: use seq_hex_dump() to dump buffers

In reply to Andy Shevchenko

Instead of custom approach let's use recently introduced seq_hex_dump() helper.

---
 drivers/net/wireless/ath/wil6210/debugfs.c | 32 +++++++-----------------------
 1 file changed, 7 insertions(+), 25 deletions(-)
drivers/net/wireless/ath/wil6210/debugfs.c +7 -25
index b1c6a72..9b28edd 100644
@@ -131,6 +131,12 @@ static const struct file_operations fops_vring = {
 	.llseek		= seq_lseek,
 };
 
+static void wil_seq_hexdump(struct seq_file *s, void *p, int len,
+			    const char *prefix)
+{
+	seq_hex_dump(s, prefix, DUMP_PREFIX_NONE, 16, 1, p, len, false);
+}
+
 static void wil_print_ring(struct seq_file *s, const char *prefix,
 			   void __iomem *off)
 {
@@ -186,8 +192,6 @@ static void wil_print_ring(struct seq_file *s, const char *prefix,
 				   le16_to_cpu(hdr.seq), len,
 				   le16_to_cpu(hdr.type), hdr.flags);
 			if (len <= MAX_MBOXITEM_SIZE) {
-				int n = 0;
-				char printbuf[16 * 3 + 2];
 				unsigned char databuf[MAX_MBOXITEM_SIZE];
 				void __iomem *src = wmi_buffer(wil, d.addr) +
 					sizeof(struct wil6210_mbox_hdr);
@@ -197,15 +201,7 @@ static void wil_print_ring(struct seq_file *s, const char *prefix,
 				 * reading header
 				 */
 				wil_memcpy_fromio_32(databuf, src, len);
-				while (n < len) {
-					int l = min(len - n, 16);
-					hex_dump_to_buffer(databuf + n, l,
-							   16, 1, printbuf,
-							   sizeof(printbuf),
-							   false);
-					seq_printf(s, "      : %s\n", printbuf);
-					n += l;
-				}
+				wil_seq_hexdump(s, databuf, len, "      : ");
 			}
 		} else {
 			seq_printf(s, "\n");
@@ -620,20 +616,6 @@ static const struct file_operations fops_wmi = {
 	.open  = simple_open,
 };
 
-static void wil_seq_hexdump(struct seq_file *s, void *p, int len,
-			    const char *prefix)
-{
-	char printbuf[16 * 3 + 2];
-	int i = 0;
-	while (i < len) {
-		int l = min(len - i, 16);
-		hex_dump_to_buffer(p + i, l, 16, 1, printbuf,
-				   sizeof(printbuf), false);
-		seq_printf(s, "%s%s\n", prefix, printbuf);
-		i += l;
-	}
-}
-
 static void wil_seq_print_skb(struct seq_file *s, struct sk_buff *skb)
 {
 	int i = 0;
Signature · 1 line
2.1.0
#16 lore raw
In reply to Andy Shevchenko
Quoted 3 lines · Andy Shevchenko

This introduces a new helper and switches current users to use it. All are compiled tested, hexdump and kmemleak are tested by their test suits.

Gently ping on this series.
Any comments?

Quoted 51 lines

Changelog v4:
- hexdump is amended to return value from which we can check an overflow (patches 1/12 - 4/12)
- seq_hex_dump pushes bytes directly to buffer and returns an error in case of overflow (addresses Al Viro's comment)
- append Acked-by for patches 8/12 and 9/12
- convert more users (patches 10/12 - 12/12)

Changelog v3:
- append Mauro's Ack
- rebase on top of recent linux-next

Changelog v2:
- append Acked-by and Reviewed-by tags
- update commit messages in patches 3/5. and 5/5
- update line size to be 32 bytes instead of 16 in patch 3/5
- Joe found that output is changed in patch 4/5, thus I update commit message there

Andy Shevchenko (12):
hexdump: introduce test suite
hexdump: fix ascii column for the tail of a dump
hexdump: do few calculations ahead
hexdump: makes it return amount of bytes placed in buffer
seq_file: provide an analogue of print_hex_dump()
saa7164: convert to seq_hex_dump()
crypto: qat - use seq_hex_dump() to dump buffers
parisc: use seq_hex_dump() to dump buffers [S390] zcrypt: use seq_hex_dump() to dump buffers
staging: unisys: use seq_hex_dump() to dump buffers
kmemleak: use seq_hex_dump() to dump buffers
wil6210: use seq_hex_dump() to dump buffers

 .../crypto/qat/qat_common/adf_transport_debug.c    |  16 +-
 drivers/media/pci/saa7164/saa7164-core.c           |  31 +---
 drivers/net/wireless/ath/wil6210/debugfs.c         |  32 +---
 drivers/parisc/ccio-dma.c                          |  14 +-
 drivers/parisc/sba_iommu.c                         |  11 +-
 drivers/s390/crypto/zcrypt_api.c                   |  10 +-
 .../unisys/visorchannel/visorchannel_funcs.c       |  26 +--
 fs/seq_file.c                                      |  50 ++++++
 include/linux/printk.h                             |   6 +-
 include/linux/seq_file.h                           |   4 +
 lib/Kconfig.debug                                  |   3 +
 lib/Makefile                                       |   4 +-
 lib/hexdump.c                                      | 105 +++++++-----
 lib/test-hexdump.c                                 | 180 +++++++++++++++++++++
 mm/kmemleak.c                                      |  21 +--
 15 files changed, 337 insertions(+), 176 deletions(-)
 create mode 100644 lib/test-hexdump.c
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy