Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

vsprintf: add new `%pA` format specifier

This patch adds a format specifier `%pA` to `vsprintf` which formats
a pointer as `core::fmt::Arguments`. Doing so allows us to directly
format to the internal buffer of `printf`, so we do not have to use
a temporary buffer on the stack to pre-assemble the message on
the Rust side.

This specifier is intended only to be used from Rust and not for C, so
`checkpatch.pl` is intentionally unchanged to catch any misuse.

Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com>
Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com>
Co-developed-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com>
Signed-off-by: Gary Guo <gary@garyguo.net>
Co-developed-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Gary Guo and committed by
Miguel Ojeda
787983da 8fcbf024

+23
+10
Documentation/core-api/printk-formats.rst
··· 625 625 %p4cc Y10 little-endian (0x20303159) 626 626 %p4cc NV12 big-endian (0xb231564e) 627 627 628 + Rust 629 + ---- 630 + 631 + :: 632 + 633 + %pA 634 + 635 + Only intended to be used from Rust code to format ``core::fmt::Arguments``. 636 + Do *not* use it from C. 637 + 628 638 Thanks 629 639 ====== 630 640
+13
lib/vsprintf.c
··· 2246 2246 } 2247 2247 early_param("no_hash_pointers", no_hash_pointers_enable); 2248 2248 2249 + /* Used for Rust formatting ('%pA'). */ 2250 + char *rust_fmt_argument(char *buf, char *end, void *ptr); 2251 + 2249 2252 /* 2250 2253 * Show a '%p' thing. A kernel extension is that the '%p' is followed 2251 2254 * by an extra set of alphanumeric characters that are extended format ··· 2375 2372 * 2376 2373 * Note: The default behaviour (unadorned %p) is to hash the address, 2377 2374 * rendering it useful as a unique identifier. 2375 + * 2376 + * There is also a '%pA' format specifier, but it is only intended to be used 2377 + * from Rust code to format core::fmt::Arguments. Do *not* use it from C. 2378 + * See rust/kernel/print.rs for details. 2378 2379 */ 2379 2380 static noinline_for_stack 2380 2381 char *pointer(const char *fmt, char *buf, char *end, void *ptr, ··· 2451 2444 return device_node_string(buf, end, ptr, spec, fmt + 1); 2452 2445 case 'f': 2453 2446 return fwnode_string(buf, end, ptr, spec, fmt + 1); 2447 + case 'A': 2448 + if (!IS_ENABLED(CONFIG_RUST)) { 2449 + WARN_ONCE(1, "Please remove %%pA from non-Rust code\n"); 2450 + return error_string(buf, end, "(%pA?)", spec); 2451 + } 2452 + return rust_fmt_argument(buf, end, ptr); 2454 2453 case 'x': 2455 2454 return pointer_string(buf, end, ptr, spec); 2456 2455 case 'e':