Skip to contentSkip to conversation

lib/vsprintf: Add support for printing V4L2 and DRM fourccs

PATCH Superseded Sakari Ailus 3 participants 7 messages Nov 3, 2020 – Nov 13, 2020

Subsystems (4)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 lib/vsprintf: Add support for printing V4L2 and DRM fourccs

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

---
Hi folks,

I believe I've addressed comments from Rasmus, Joe and Andy --- variables that don't need to be negative have remained unsigned though. Thanks for the suggestions, I believe this is much cleaner than v3.

since v3:

- Remove use of WARN_ON, assume the code is correct instead. A side effect of this is the code is much easier to understand.

- Check the modifier first before validating the buf pointer.

- Use isascii() and isprint() functions to weed out non-printable characters.

- Use hex_byte_pack() for printing 8-bit hexadecimal numbers.

- Remove macros, and instead use plain strings.

- Use strcpy() to copy the endianness string, and then strlen() to add its length.

- Strip the MSB (endianness bit), then use the value as such.

- Assign characters to the buffer and increment active pointer at the same time.

- Drop __ from variable names.

- Add example to documentation.

 Documentation/core-api/printk-formats.rst | 16 +++++++
 lib/test_printf.c                         | 17 ++++++++
 lib/vsprintf.c                            | 52 +++++++++++++++++++++++
 3 files changed, 85 insertions(+)
Documentation/core-api/printk-formats.rst +16 -0
index 6d26c5c6ac48..080262d2e030 100644
@@ -565,6 +565,22 @@ For printing netdev_features_t.
 
 Passed by reference.
 
+V4L2 and DRM FourCC code (pixel format)
+---------------------------------------
+
+::
+
+	%p4cc
+
+Print a FourCC code used by V4L2 or DRM, including format endianness and
+its numerical value as hexadecimal.
+
+Passed by reference.
+
+Examples::
+
+	%p4cc	BG12 little-endian (0x32314742)
+
 Thanks
 ======
 
lib/test_printf.c +17 -0
index 7ac87f18a10f..7fb042542660 100644
@@ -649,6 +649,22 @@ static void __init fwnode_pointer(void)
 	software_node_unregister(&softnodes[0]);
 }
 
+static void __init fourcc_pointer(void)
+{
+	struct {
+		u32 code;
+		char *str;
+	} const try[] = {
+		{ 0x20104646, "FF(10) little-endian (0x20104646)", },
+		{ 0xa0104646, "FF(10) big-endian (0xa0104646)", },
+		{ 0x10111213, "(13)(12)(11)(10) little-endian (0x10111213)", },
+	};
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(try); i++)
+		test(try[i].str, "%p4cc", &try[i].code);
+}
+
 static void __init
 errptr(void)
 {
@@ -694,6 +710,7 @@ test_pointer(void)
 	flags();
 	errptr();
 	fwnode_pointer();
+	fourcc_pointer();
 }
 
 static void __init selftest(void)
lib/vsprintf.c +52 -0

70 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw
Quoted 3 lines · Sakari Ailus

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

...

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1740

Quoted 5 lines
lib/vsprintf.c +5 -0
+static noinline_for_stack
+char *fourcc_string(char *buf, char *end, const u32 *fourcc,
+		    struct printf_spec spec, const char *fmt)
+{
+	char output[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")];

I would add a comment that there is another possibility, i.e. big-endian, but it occupies less space.

Quoted 19 lines
lib/vsprintf.c +19 -0
+	char *p = output;
+	unsigned int i;
+	u32 val;
+
+	if (fmt[1] != 'c' || fmt[2] != 'c')
+		return error_string(output, end, "(%p4?)", spec);
+
+	if (check_pointer(&buf, end, fourcc, spec))
+		return buf;
+
+	val = *fourcc & ~BIT(31);
+
+	for (i = 0; i < sizeof(*fourcc); i++) {
+		unsigned char c = val >> (i * 8);
+
+		/* Weed out spaces */
+		if (c == ' ')
+			continue;
+

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1769

Quoted 10 lines
lib/vsprintf.c +10 -0
+		/* Print non-control ASCII characters as-is */
+		if (isascii(c) && isprint(c)) {
+			*p++ = c;
+			continue;
+		}
+
+		*p++ = '(';
+		p = hex_byte_pack(p, c);
+		*p++ = ')';
+	}

I guess above loop can be still optimized, but I have to think about it.

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1776

Quoted 6 lines
lib/vsprintf.c +6 -0
+	strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian");
+	p += strlen(p);
+
+	*p++ = ' ';
+	*p++ = '(';
+	/* subtract parenthesis and the space from the size */

This comment misleading. What you are subtracting is a room for closing parenthesis and NUL.

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1778

Quoted 2 lines
lib/vsprintf.c +2 -0
+	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc,
+			       sizeof(u32));

I would go with one line here.

The (theoretical) problem is here that the case when buffer size is not enough to print a value will be like '(0xabc)' but should be rather '(0xabcd' like snprintf() does in general.

Quoted 5 lines
lib/vsprintf.c +5 -0
+	*p++ = ')';
+	*p = '\0';
+
+	return string(buf, end, output, spec);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi Andy,

Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Sakari Ailus

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

...

Quoted 8 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1740

Quoted 5 lines
lib/vsprintf.c +5 -0
+static noinline_for_stack
+char *fourcc_string(char *buf, char *end, const u32 *fourcc,
+		    struct printf_spec spec, const char *fmt)
+{
+	char output[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")];

I would add a comment that there is another possibility, i.e. big-endian, but it occupies less space.

This string is here to represent the longest possible output of the function. Most of the time it's less. Saying that might make sense but it's fairly clear already. Either way works for me though.

Quoted 33 lines
Quoted 20 lines
lib/vsprintf.c +19 -0
+	char *p = output;
+	unsigned int i;
+	u32 val;
+
+	if (fmt[1] != 'c' || fmt[2] != 'c')
+		return error_string(output, end, "(%p4?)", spec);
+
+	if (check_pointer(&buf, end, fourcc, spec))
+		return buf;
+
+	val = *fourcc & ~BIT(31);
+
+	for (i = 0; i < sizeof(*fourcc); i++) {
+		unsigned char c = val >> (i * 8);
+
+		/* Weed out spaces */
+		if (c == ' ')
+			continue;
+

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1769

Quoted 10 lines
lib/vsprintf.c +10 -0
+		/* Print non-control ASCII characters as-is */
+		if (isascii(c) && isprint(c)) {
+			*p++ = c;
+			continue;
+		}
+
+		*p++ = '(';
+		p = hex_byte_pack(p, c);
+		*p++ = ')';
+	}

I guess above loop can be still optimized, but I have to think about it.

Quoted 9 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1776

Quoted 6 lines
lib/vsprintf.c +6 -0
+	strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian");
+	p += strlen(p);
+
+	*p++ = ' ';
+	*p++ = '(';
+	/* subtract parenthesis and the space from the size */

This comment misleading. What you are subtracting is a room for closing parenthesis and NUL.

Agreed, I'll update it for v5.

Quoted 5 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1778

Quoted 3 lines
lib/vsprintf.c +2 -0
+	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc,
+			       sizeof(u32));

I would go with one line here.

It's wrapped since the result would be over 80 otherwise.

Quoted 11 lines

The (theoretical) problem is here that the case when buffer size is not enough to print a value will be like '(0xabc)' but should be rather '(0xabcd' like snprintf() does in general.

Quoted 5 lines
lib/vsprintf.c +5 -0
+	*p++ = ')';
+	*p = '\0';
+
+	return string(buf, end, output, spec);
+}
Signature · 7 lines
Regards,

Sakari Ailus
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw
Quoted 5 lines · Sakari Ailus
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Sakari Ailus

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

...

Quoted 8 lines
Quoted 4 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1740

Quoted 1 line
lib/vsprintf.c +1 -0
+	char output[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")];

I would add a comment that there is another possibility, i.e. big-endian, but it occupies less space.

This string is here to represent the longest possible output of the function. Most of the time it's less. Saying that might make sense but it's fairly clear already. Either way works for me though.

It's not known by reading the above line. I would add a comment.

...

Quoted 6 lines
Quoted 4 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1778

Quoted 2 lines
lib/vsprintf.c +2 -0
+	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc,
+			       sizeof(u32));

I would go with one line here.

It's wrapped since the result would be over 80 otherwise.

It will be not the first one breaking the limit in this module for the sake of readability.

...

Quoted 3 lines
Quoted 3 lines

The (theoretical) problem is here that the case when buffer size is not enough to print a value will be like '(0xabc)' but should be rather '(0xabcd' like snprintf() does in general.

Any comments here?
Perhaps you need to add a comment to explain that the overflow is impossible.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
In reply to Sakari Ailus
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Sakari Ailus

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

...

Quoted 8 lines
Quoted 8 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1740

Quoted 5 lines
lib/vsprintf.c +5 -0
+static noinline_for_stack
+char *fourcc_string(char *buf, char *end, const u32 *fourcc,
+		    struct printf_spec spec, const char *fmt)
+{
+	char output[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")];

I would add a comment that there is another possibility, i.e. big-endian, but it occupies less space.

I think it's unnecessary as it's obvious and similarly done in other <foo>_string type functions.

Quoted 6 lines
Quoted 4 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1778

Quoted 2 lines
lib/vsprintf.c +2 -0
+	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc,
+			       sizeof(u32));

I would go with one line here.

It's wrapped since the result would be over 80 otherwise.

Perhaps simpler as

	p = special_hex_number(p, p + 10, *fourcc, sizeof(u32));
Quoted 3 lines
Quoted 3 lines

The (theoretical) problem is here that the case when buffer size is not enough to print a value will be like '(0xabc)' but should be rather '(0xabcd' like snprintf() does in general.

Isn't the stack buffer known to be large enough?

Quoted 4 lines
Quoted 4 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1782

Quoted 4 lines
lib/vsprintf.c +4 -0
+	*p++ = ')';
+	*p = '\0';
+
+	return string(buf, end, output, spec);

Isn't the actual output buffer used here truncating output?

If the general problem is someone using a limited length pointer output like %10p4cc, then all the output is getting truncated no?

#5 lore raw

Hi Joe,

Quoted 26 lines · Joe Perches
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Sakari Ailus

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

...

Quoted 8 lines
Quoted 8 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1740

Quoted 5 lines
lib/vsprintf.c +5 -0
+static noinline_for_stack
+char *fourcc_string(char *buf, char *end, const u32 *fourcc,
+		    struct printf_spec spec, const char *fmt)
+{
+	char output[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")];

I would add a comment that there is another possibility, i.e. big-endian, but it occupies less space.

I think it's unnecessary as it's obvious and similarly done in other <foo>_string type functions.

Quoted 5 lines
Quoted 4 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1778

Quoted 2 lines
lib/vsprintf.c +2 -0
+	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc,
+			       sizeof(u32));

I would go with one line here.

Quoted 5 lines
Quoted 1 line

It's wrapped since the result would be over 80 otherwise.

Perhaps simpler as

	p = special_hex_number(p, p + 10, *fourcc, sizeof(u32));

Yes. But having bugs elsewhere would have a magnified effect. I wouldn't be afraid of a newline here.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

The (theoretical) problem is here that the case when buffer size is not enough to print a value will be like '(0xabc)' but should be rather '(0xabcd' like snprintf() does in general.

Isn't the stack buffer known to be large enough?

Yes. If there are no bugs, that is.

Quoted 10 lines
Quoted 5 lines
Quoted 5 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1782

Quoted 5 lines
lib/vsprintf.c +4 -0
+	*p++ = ')';
+	*p = '\0';
+
+	return string(buf, end, output, spec);

Isn't the actual output buffer used here truncating output?

If the general problem is someone using a limited length pointer output like %10p4cc, then all the output is getting truncated no?

Correct. But I'd also be surprised if anyone ever used this feature.

Signature · 7 lines
Kind regards,

Sakari Ailus
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw
Quoted 29 lines · Sakari Ailus

Hi Joe,

Quoted 26 lines · Joe Perches
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Sakari Ailus

Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM pixel formats denoted by fourccs. The fourcc encoding is the same for both so the same implementation can be used.

...

Quoted 8 lines
Quoted 8 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1740

Quoted 5 lines
lib/vsprintf.c +5 -0
+static noinline_for_stack
+char *fourcc_string(char *buf, char *end, const u32 *fourcc,
+		    struct printf_spec spec, const char *fmt)
+{
+	char output[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")];

I would add a comment that there is another possibility, i.e. big-endian, but it occupies less space.

I think it's unnecessary as it's obvious and similarly done in other <foo>_string type functions.

Quoted 5 lines
Quoted 4 lines

lib/vsprintf.c char *netdev_bits(char *buf, char *end, const void *addr, · line 1778

Quoted 2 lines
lib/vsprintf.c +2 -0
+	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc,
+			       sizeof(u32));

I would go with one line here.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

It's wrapped since the result would be over 80 otherwise.

Perhaps simpler as

	p = special_hex_number(p, p + 10, *fourcc, sizeof(u32));

Yes. But having bugs elsewhere would have a magnified effect.

How's that? Where would "elsewhere" be?

Quoted 1 line

I wouldn't be afraid of a newline here.

I'd prefer obvious code instead of indirected p vs output and having to lookup whatever output is again.

special_hex_number is already known to fit in the buffer.