public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()
@ 2026-04-16 12:01 Yihan Ding
  2026-04-16 12:01 ` [PATCH bpf v3 1/2] " Yihan Ding
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Yihan Ding @ 2026-04-16 12:01 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, shuah, alan.maguire, paul.chaignon,
	linux-kernel, Yihan Ding

bpf_bprintf_prepare() currently rejects any non-ASCII byte in format
strings, so helpers such as bpf_trace_printk() fail to emit UTF-8
literal text even when those bytes are not part of a format specifier.

Keep plain text permissive while continuing to parse '%' sequences as
ASCII-only. Patch 1 updates snprintf_negative() at the same time so the
selftests stay consistent during bisection. Patch 2 then extends
trace_printk coverage for both the valid UTF-8 literal case and the
invalid non-ASCII-after-'%' case.

Changes in v3:
- drop Suggested-by trailers and move review credit into this changelog
- update test_snprintf_negative() in patch 1/2 so plain non-ASCII text is
  accepted while non-ASCII after '%' is still rejected, keeping
  ./test_progs -t snprintf aligned with the new behavior.
- clarify the trace_printk negative case with an explicit invalid format
  string and comment
- address Paul Chaignon's review feedback and keep the negative coverage
  requested earlier by Alan Maguire

Changes in v2:
- split the core change and selftest updates into two patches
- drop unnecessary isspace()/ispunct() casts
- add comments to clarify plain-text vs format-specifier handling
- add a negative selftest for non-ASCII bytes inside '%' sequences

Testing:
- Reproduced on x86_64 without the core fix: ASCII trace output works,
  while UTF-8 literal text in bpf_trace_printk() is rejected and
  produces no trace output
- Verified with tools/testing/selftests/bpf: ./test_progs -t trace_printk
- Verified with tools/testing/selftests/bpf: ./test_progs -t snprintf

Yihan Ding (2):
  bpf: allow UTF-8 literals in bpf_bprintf_prepare()
  selftests/bpf: cover UTF-8 trace_printk output

 kernel/bpf/helpers.c                          | 17 ++++++++++-
 .../selftests/bpf/prog_tests/snprintf.c       |  3 +-
 .../selftests/bpf/prog_tests/trace_printk.c   | 28 +++++++++++++++----
 .../selftests/bpf/progs/trace_printk.c        | 10 +++++++
 4 files changed, 50 insertions(+), 8 deletions(-)

-- 
2.20.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH bpf v3 1/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()
  2026-04-16 12:01 [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() Yihan Ding
@ 2026-04-16 12:01 ` Yihan Ding
  2026-04-16 13:03   ` sashiko-bot
  2026-04-16 22:32   ` Paul Chaignon
  2026-04-16 12:01 ` [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output Yihan Ding
  2026-04-16 23:00 ` [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() patchwork-bot+netdevbpf
  2 siblings, 2 replies; 7+ messages in thread
From: Yihan Ding @ 2026-04-16 12:01 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, shuah, alan.maguire, paul.chaignon,
	linux-kernel, Yihan Ding

bpf_bprintf_prepare() only needs ASCII parsing for conversion
specifiers. Plain text can safely carry bytes >= 0x80, so allow
UTF-8 literals outside '%' sequences while keeping ASCII control
bytes rejected and format specifiers ASCII-only.

This keeps existing parsing rules for format directives unchanged,
while allowing helpers such as bpf_trace_printk() to emit UTF-8
literal text.

Update test_snprintf_negative() in the same commit so selftests keep
matching the new plain-text vs format-specifier split during bisection.

Fixes: 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")
Signed-off-by: Yihan Ding <dingyihan@uniontech.com>
---
 kernel/bpf/helpers.c                            | 17 ++++++++++++++++-
 .../testing/selftests/bpf/prog_tests/snprintf.c |  3 ++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 6eb6c82ed2ee..d51f1b612f1d 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		data->buf = buffers->buf;
 
 	for (i = 0; i < fmt_size; i++) {
-		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
+		unsigned char c = fmt[i];
+
+		/*
+		 * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
+		 * through unchanged, while still rejecting ASCII control bytes.
+		 */
+		if (isascii(c) && !isprint(c) && !isspace(c)) {
 			err = -EINVAL;
 			goto out;
 		}
@@ -867,6 +873,15 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
 		 * always access fmt[i + 1], in the worst case it will be a 0
 		 */
 		i++;
+		c = fmt[i];
+		/*
+		 * The format parser below only understands ASCII conversion
+		 * specifiers and modifiers, so reject non-ASCII after '%'.
+		 */
+		if (!isascii(c)) {
+			err = -EINVAL;
+			goto out;
+		}
 
 		/* skip optional "[0 +-][num]" width formatting field */
 		while (fmt[i] == '0' || fmt[i] == '+'  || fmt[i] == '-' ||
diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c b/tools/testing/selftests/bpf/prog_tests/snprintf.c
index 594441acb707..4e4a82d54f79 100644
--- a/tools/testing/selftests/bpf/prog_tests/snprintf.c
+++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c
@@ -114,7 +114,8 @@ static void test_snprintf_negative(void)
 	ASSERT_ERR(load_single_snprintf("%--------"), "invalid specifier 5");
 	ASSERT_ERR(load_single_snprintf("%lc"), "invalid specifier 6");
 	ASSERT_ERR(load_single_snprintf("%llc"), "invalid specifier 7");
-	ASSERT_ERR(load_single_snprintf("\x80"), "non ascii character");
+	ASSERT_OK(load_single_snprintf("\x80"), "non ascii plain text");
+	ASSERT_ERR(load_single_snprintf("%\x80"), "non ascii in specifier");
 	ASSERT_ERR(load_single_snprintf("\x1"), "non printable character");
 	ASSERT_ERR(load_single_snprintf("%p%"), "invalid specifier 8");
 	ASSERT_ERR(load_single_snprintf("%s%"), "invalid specifier 9");
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output
  2026-04-16 12:01 [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() Yihan Ding
  2026-04-16 12:01 ` [PATCH bpf v3 1/2] " Yihan Ding
@ 2026-04-16 12:01 ` Yihan Ding
  2026-04-16 22:35   ` Paul Chaignon
  2026-04-16 23:00 ` [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() patchwork-bot+netdevbpf
  2 siblings, 1 reply; 7+ messages in thread
From: Yihan Ding @ 2026-04-16 12:01 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, shuah, alan.maguire, paul.chaignon,
	linux-kernel, Yihan Ding

Extend trace_printk coverage to verify that UTF-8 literal text is
emitted successfully and that '%' parsing still rejects non-ASCII
bytes once format parsing starts.

Use an explicitly invalid format string for the negative case so the
ASCII-only parser expectation is visible from the test code itself.

Signed-off-by: Yihan Ding <dingyihan@uniontech.com>
---
 .../selftests/bpf/prog_tests/trace_printk.c   | 28 +++++++++++++++----
 .../selftests/bpf/progs/trace_printk.c        | 10 +++++++
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/trace_printk.c b/tools/testing/selftests/bpf/prog_tests/trace_printk.c
index e56e88596d64..a5a8104c1ddd 100644
--- a/tools/testing/selftests/bpf/prog_tests/trace_printk.c
+++ b/tools/testing/selftests/bpf/prog_tests/trace_printk.c
@@ -6,18 +6,21 @@
 #include "trace_printk.lskel.h"
 
 #define SEARCHMSG	"testing,testing"
+#define SEARCHMSG_UTF8	"中文,测试"
 
 static void trace_pipe_cb(const char *str, void *data)
 {
 	if (strstr(str, SEARCHMSG) != NULL)
-		(*(int *)data)++;
+		((int *)data)[0]++;
+	if (strstr(str, SEARCHMSG_UTF8))
+		((int *)data)[1]++;
 }
 
 void serial_test_trace_printk(void)
 {
 	struct trace_printk_lskel__bss *bss;
 	struct trace_printk_lskel *skel;
-	int err = 0, found = 0;
+	int err = 0, found[2] = {};
 
 	skel = trace_printk_lskel__open();
 	if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
@@ -46,11 +49,24 @@ void serial_test_trace_printk(void)
 	if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
 		goto cleanup;
 
-	/* verify our search string is in the trace buffer */
-	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, &found, 1000),
-		 "read_trace_pipe_iter");
+	if (!ASSERT_GT(bss->trace_printk_utf8_ran, 0, "bss->trace_printk_utf8_ran"))
+		goto cleanup;
+
+	if (!ASSERT_GT(bss->trace_printk_utf8_ret, 0, "bss->trace_printk_utf8_ret"))
+		goto cleanup;
+
+	if (!ASSERT_LT(bss->trace_printk_invalid_spec_ret, 0,
+		       "bss->trace_printk_invalid_spec_ret"))
+		goto cleanup;
+
+	/* verify our search strings are in the trace buffer */
+	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, found, 1000),
+		  "read_trace_pipe_iter");
+
+	if (!ASSERT_EQ(found[0], bss->trace_printk_ran, "found"))
+		goto cleanup;
 
-	if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
+	if (!ASSERT_EQ(found[1], bss->trace_printk_utf8_ran, "found_utf8"))
 		goto cleanup;
 
 cleanup:
diff --git a/tools/testing/selftests/bpf/progs/trace_printk.c b/tools/testing/selftests/bpf/progs/trace_printk.c
index 6695478c2b25..f4c538ec3ebd 100644
--- a/tools/testing/selftests/bpf/progs/trace_printk.c
+++ b/tools/testing/selftests/bpf/progs/trace_printk.c
@@ -10,13 +10,23 @@ char _license[] SEC("license") = "GPL";
 
 int trace_printk_ret = 0;
 int trace_printk_ran = 0;
+int trace_printk_invalid_spec_ret = 0;
+int trace_printk_utf8_ret = 0;
+int trace_printk_utf8_ran = 0;
 
 const char fmt[] = "Testing,testing %d\n";
+static const char utf8_fmt[] = "中文,测试 %d\n";
+/* Non-ASCII bytes after '%' must still be rejected. */
+static const char invalid_spec_fmt[] = "%\x80\n";
 
 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
 int sys_enter(void *ctx)
 {
 	trace_printk_ret = bpf_trace_printk(fmt, sizeof(fmt),
 					    ++trace_printk_ran);
+	trace_printk_utf8_ret = bpf_trace_printk(utf8_fmt, sizeof(utf8_fmt),
+						 ++trace_printk_utf8_ran);
+	trace_printk_invalid_spec_ret = bpf_trace_printk(invalid_spec_fmt,
+							 sizeof(invalid_spec_fmt));
 	return 0;
 }
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf v3 1/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()
  2026-04-16 12:01 ` [PATCH bpf v3 1/2] " Yihan Ding
@ 2026-04-16 13:03   ` sashiko-bot
  2026-04-16 22:32   ` Paul Chaignon
  1 sibling, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-04-16 13:03 UTC (permalink / raw)
  To: Yihan Ding; +Cc: bpf

Sashiko AI review found 1 potential issue(s):
- [Medium] UTF-8 literals are incorrectly rejected when placed immediately after '%s' or '%p' format specifiers.
--

commit c7ffc79748019b47230d65f7c4646b2bba78f020
Author: Yihan Ding <dingyihan@uniontech.com>

bpf: allow UTF-8 literals in bpf_bprintf_prepare()

This commit modifies bpf_bprintf_prepare() to permit bytes >= 0x80 in
plain text, allowing UTF-8 literals to pass through unchanged while still
rejecting ASCII control bytes.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index bb95e287b0dc8..2bb60200c2665 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
>  		data->buf = buffers->buf;
>  
>  	for (i = 0; i < fmt_size; i++) {
> -		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
> +		unsigned char c = fmt[i];
> +
> +		/*
> +		 * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
> +		 * through unchanged, while still rejecting ASCII control bytes.
> +		 */
> +		if (isascii(c) && !isprint(c) && !isspace(c)) {
>  			err = -EINVAL;
>  			goto out;
>  		}

Does this change overlook the lookahead checks performed on characters
immediately following format specifiers like %s and %p?

For example, looking at the parsing logic for %s:

kernel/bpf/helpers.c:bpf_bprintf_prepare() {
    ...
		} else if (fmt[i] == 's') {
			fmt_ptype = fmt[i];
fmt_str:
			if (fmt[i + 1] != 0 &&
			    !isspace(fmt[i + 1]) &&
			    !ispunct(fmt[i + 1])) {
				err = -EINVAL;
				goto out;
			}
    ...
}

Because the initial loop now permits bytes >= 0x80, these bytes can appear
immediately after a format specifier. If a UTF-8 literal is placed immediately
after the specifier, such as in the string "%s你好", fmt[i + 1] will be the
first byte of the UTF-8 character.

Since isspace() and ispunct() only match ASCII characters, both will
evaluate to false. Will this cause the parser to incorrectly reject valid
UTF-8 strings with -EINVAL?

A similar lookahead check exists for %p:

kernel/bpf/helpers.c:bpf_bprintf_prepare() {
    ...
		if (fmt[i] == 'p') {
			sizeof_cur_arg = sizeof(long);

			if (fmt[i + 1] == 0 || isspace(fmt[i + 1]) ||
			    ispunct(fmt[i + 1])) {
				if (tmp_buf)
					cur_arg = raw_args[num_spec];
				goto nocopy_fmt;
			}
    ...
}

If fmt[i + 1] is a UTF-8 character, it won't match isspace() or ispunct().
It will then fall through to the %pI4/%pi4 checks and also be rejected with
-EINVAL.

Can the lookahead checks be updated to also permit bytes >= 0x80, matching
the new plain text parsing rules?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260416120142.1420646-1-dingyihan@uniontech.com?part=1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf v3 1/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()
  2026-04-16 12:01 ` [PATCH bpf v3 1/2] " Yihan Ding
  2026-04-16 13:03   ` sashiko-bot
@ 2026-04-16 22:32   ` Paul Chaignon
  1 sibling, 0 replies; 7+ messages in thread
From: Paul Chaignon @ 2026-04-16 22:32 UTC (permalink / raw)
  To: Yihan Ding; +Cc: bpf, ast, daniel, andrii, shuah, alan.maguire, linux-kernel

On Thu, Apr 16, 2026 at 08:01:41PM +0800, Yihan Ding wrote:
> bpf_bprintf_prepare() only needs ASCII parsing for conversion
> specifiers. Plain text can safely carry bytes >= 0x80, so allow
> UTF-8 literals outside '%' sequences while keeping ASCII control
> bytes rejected and format specifiers ASCII-only.
> 
> This keeps existing parsing rules for format directives unchanged,
> while allowing helpers such as bpf_trace_printk() to emit UTF-8
> literal text.
> 
> Update test_snprintf_negative() in the same commit so selftests keep
> matching the new plain-text vs format-specifier split during bisection.
> 
> Fixes: 48cac3f4a96d ("bpf: Implement formatted output helpers with bstr_printf")
> Signed-off-by: Yihan Ding <dingyihan@uniontech.com>
> ---
>  kernel/bpf/helpers.c                            | 17 ++++++++++++++++-
>  .../testing/selftests/bpf/prog_tests/snprintf.c |  3 ++-
>  2 files changed, 18 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 6eb6c82ed2ee..d51f1b612f1d 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -845,7 +845,13 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
>  		data->buf = buffers->buf;
>  
>  	for (i = 0; i < fmt_size; i++) {
> -		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i])) {
> +		unsigned char c = fmt[i];

I'm a bit unsure this extra variable is worth it, but it's probably not
worth sending a v4 just for that.

> +
> +		/*
> +		 * Permit bytes >= 0x80 in plain text so UTF-8 literals can pass
> +		 * through unchanged, while still rejecting ASCII control bytes.
> +		 */
> +		if (isascii(c) && !isprint(c) && !isspace(c)) {
>  			err = -EINVAL;
>  			goto out;
>  		}
> @@ -867,6 +873,15 @@ int bpf_bprintf_prepare(const char *fmt, u32 fmt_size, const u64 *raw_args,
>  		 * always access fmt[i + 1], in the worst case it will be a 0
>  		 */
>  		i++;
> +		c = fmt[i];
> +		/*
> +		 * The format parser below only understands ASCII conversion
> +		 * specifiers and modifiers, so reject non-ASCII after '%'.
> +		 */
> +		if (!isascii(c)) {
> +			err = -EINVAL;
> +			goto out;
> +		}
>  
>  		/* skip optional "[0 +-][num]" width formatting field */
>  		while (fmt[i] == '0' || fmt[i] == '+'  || fmt[i] == '-' ||
> diff --git a/tools/testing/selftests/bpf/prog_tests/snprintf.c b/tools/testing/selftests/bpf/prog_tests/snprintf.c
> index 594441acb707..4e4a82d54f79 100644
> --- a/tools/testing/selftests/bpf/prog_tests/snprintf.c
> +++ b/tools/testing/selftests/bpf/prog_tests/snprintf.c
> @@ -114,7 +114,8 @@ static void test_snprintf_negative(void)
>  	ASSERT_ERR(load_single_snprintf("%--------"), "invalid specifier 5");
>  	ASSERT_ERR(load_single_snprintf("%lc"), "invalid specifier 6");
>  	ASSERT_ERR(load_single_snprintf("%llc"), "invalid specifier 7");
> -	ASSERT_ERR(load_single_snprintf("\x80"), "non ascii character");
> +	ASSERT_OK(load_single_snprintf("\x80"), "non ascii plain text");
> +	ASSERT_ERR(load_single_snprintf("%\x80"), "non ascii in specifier");

Acked-by: Paul Chaignon <paul.chaignon@gmail.com>

>  	ASSERT_ERR(load_single_snprintf("\x1"), "non printable character");
>  	ASSERT_ERR(load_single_snprintf("%p%"), "invalid specifier 8");
>  	ASSERT_ERR(load_single_snprintf("%s%"), "invalid specifier 9");
> -- 
> 2.20.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output
  2026-04-16 12:01 ` [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output Yihan Ding
@ 2026-04-16 22:35   ` Paul Chaignon
  0 siblings, 0 replies; 7+ messages in thread
From: Paul Chaignon @ 2026-04-16 22:35 UTC (permalink / raw)
  To: Yihan Ding; +Cc: bpf, ast, daniel, andrii, shuah, alan.maguire, linux-kernel

On Thu, Apr 16, 2026 at 08:01:42PM +0800, Yihan Ding wrote:
> Extend trace_printk coverage to verify that UTF-8 literal text is
> emitted successfully and that '%' parsing still rejects non-ASCII
> bytes once format parsing starts.
> 
> Use an explicitly invalid format string for the negative case so the
> ASCII-only parser expectation is visible from the test code itself.
> 
> Signed-off-by: Yihan Ding <dingyihan@uniontech.com>

The test makes sense and I verified it fails as expected without the
fix.

Acked-by: Paul Chaignon <paul.chaignon@gmail.com>

> ---
>  .../selftests/bpf/prog_tests/trace_printk.c   | 28 +++++++++++++++----
>  .../selftests/bpf/progs/trace_printk.c        | 10 +++++++
>  2 files changed, 32 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/trace_printk.c b/tools/testing/selftests/bpf/prog_tests/trace_printk.c
> index e56e88596d64..a5a8104c1ddd 100644
> --- a/tools/testing/selftests/bpf/prog_tests/trace_printk.c
> +++ b/tools/testing/selftests/bpf/prog_tests/trace_printk.c
> @@ -6,18 +6,21 @@
>  #include "trace_printk.lskel.h"
>  
>  #define SEARCHMSG	"testing,testing"
> +#define SEARCHMSG_UTF8	"中文,测试"
>  
>  static void trace_pipe_cb(const char *str, void *data)
>  {
>  	if (strstr(str, SEARCHMSG) != NULL)
> -		(*(int *)data)++;
> +		((int *)data)[0]++;
> +	if (strstr(str, SEARCHMSG_UTF8))
> +		((int *)data)[1]++;
>  }
>  
>  void serial_test_trace_printk(void)
>  {
>  	struct trace_printk_lskel__bss *bss;
>  	struct trace_printk_lskel *skel;
> -	int err = 0, found = 0;
> +	int err = 0, found[2] = {};
>  
>  	skel = trace_printk_lskel__open();
>  	if (!ASSERT_OK_PTR(skel, "trace_printk__open"))
> @@ -46,11 +49,24 @@ void serial_test_trace_printk(void)
>  	if (!ASSERT_GT(bss->trace_printk_ret, 0, "bss->trace_printk_ret"))
>  		goto cleanup;
>  
> -	/* verify our search string is in the trace buffer */
> -	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, &found, 1000),
> -		 "read_trace_pipe_iter");
> +	if (!ASSERT_GT(bss->trace_printk_utf8_ran, 0, "bss->trace_printk_utf8_ran"))
> +		goto cleanup;
> +
> +	if (!ASSERT_GT(bss->trace_printk_utf8_ret, 0, "bss->trace_printk_utf8_ret"))
> +		goto cleanup;
> +
> +	if (!ASSERT_LT(bss->trace_printk_invalid_spec_ret, 0,
> +		       "bss->trace_printk_invalid_spec_ret"))
> +		goto cleanup;
> +
> +	/* verify our search strings are in the trace buffer */
> +	ASSERT_OK(read_trace_pipe_iter(trace_pipe_cb, found, 1000),
> +		  "read_trace_pipe_iter");
> +
> +	if (!ASSERT_EQ(found[0], bss->trace_printk_ran, "found"))
> +		goto cleanup;
>  
> -	if (!ASSERT_EQ(found, bss->trace_printk_ran, "found"))
> +	if (!ASSERT_EQ(found[1], bss->trace_printk_utf8_ran, "found_utf8"))
>  		goto cleanup;
>  
>  cleanup:
> diff --git a/tools/testing/selftests/bpf/progs/trace_printk.c b/tools/testing/selftests/bpf/progs/trace_printk.c
> index 6695478c2b25..f4c538ec3ebd 100644
> --- a/tools/testing/selftests/bpf/progs/trace_printk.c
> +++ b/tools/testing/selftests/bpf/progs/trace_printk.c
> @@ -10,13 +10,23 @@ char _license[] SEC("license") = "GPL";
>  
>  int trace_printk_ret = 0;
>  int trace_printk_ran = 0;
> +int trace_printk_invalid_spec_ret = 0;
> +int trace_printk_utf8_ret = 0;
> +int trace_printk_utf8_ran = 0;
>  
>  const char fmt[] = "Testing,testing %d\n";
> +static const char utf8_fmt[] = "中文,测试 %d\n";
> +/* Non-ASCII bytes after '%' must still be rejected. */
> +static const char invalid_spec_fmt[] = "%\x80\n";
>  
>  SEC("fentry/" SYS_PREFIX "sys_nanosleep")
>  int sys_enter(void *ctx)
>  {
>  	trace_printk_ret = bpf_trace_printk(fmt, sizeof(fmt),
>  					    ++trace_printk_ran);
> +	trace_printk_utf8_ret = bpf_trace_printk(utf8_fmt, sizeof(utf8_fmt),
> +						 ++trace_printk_utf8_ran);
> +	trace_printk_invalid_spec_ret = bpf_trace_printk(invalid_spec_fmt,
> +							 sizeof(invalid_spec_fmt));
>  	return 0;
>  }
> -- 
> 2.20.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()
  2026-04-16 12:01 [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() Yihan Ding
  2026-04-16 12:01 ` [PATCH bpf v3 1/2] " Yihan Ding
  2026-04-16 12:01 ` [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output Yihan Ding
@ 2026-04-16 23:00 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-04-16 23:00 UTC (permalink / raw)
  To: Yihan Ding
  Cc: bpf, ast, daniel, andrii, shuah, alan.maguire, paul.chaignon,
	linux-kernel

Hello:

This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Thu, 16 Apr 2026 20:01:40 +0800 you wrote:
> bpf_bprintf_prepare() currently rejects any non-ASCII byte in format
> strings, so helpers such as bpf_trace_printk() fail to emit UTF-8
> literal text even when those bytes are not part of a format specifier.
> 
> Keep plain text permissive while continuing to parse '%' sequences as
> ASCII-only. Patch 1 updates snprintf_negative() at the same time so the
> selftests stay consistent during bisection. Patch 2 then extends
> trace_printk coverage for both the valid UTF-8 literal case and the
> invalid non-ASCII-after-'%' case.
> 
> [...]

Here is the summary with links:
  - [bpf,v3,1/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare()
    https://git.kernel.org/bpf/bpf/c/b960430ea886
  - [bpf,v3,2/2] selftests/bpf: cover UTF-8 trace_printk output
    https://git.kernel.org/bpf/bpf/c/4198ff31edb1

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-04-16 23:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-16 12:01 [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() Yihan Ding
2026-04-16 12:01 ` [PATCH bpf v3 1/2] " Yihan Ding
2026-04-16 13:03   ` sashiko-bot
2026-04-16 22:32   ` Paul Chaignon
2026-04-16 12:01 ` [PATCH bpf v3 2/2] selftests/bpf: cover UTF-8 trace_printk output Yihan Ding
2026-04-16 22:35   ` Paul Chaignon
2026-04-16 23:00 ` [PATCH bpf v3 0/2] bpf: allow UTF-8 literals in bpf_bprintf_prepare() patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox