[lkml]   [2021]   [Dec]   [6]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH 11/19] vsprintf: add new `%pA` format specifier
From: Gary Guo <>

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
`` is intentionally unchanged to catch any misuse.

Co-developed-by: Alex Gaynor <>
Signed-off-by: Alex Gaynor <>
Co-developed-by: Wedson Almeida Filho <>
Signed-off-by: Wedson Almeida Filho <>
Signed-off-by: Gary Guo <>
Co-developed-by: Miguel Ojeda <>
Signed-off-by: Miguel Ojeda <>
lib/vsprintf.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 58d5e567f836..bc9c05427d9a 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -2233,6 +2233,10 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
return widen_string(buf, buf - buf_start, end, spec);

+char *rust_fmt_argument(char* buf, char* end, void *ptr);
/* Disable pointer hashing if requested */
bool no_hash_pointers __ro_after_init;
@@ -2388,6 +2392,10 @@ early_param("no_hash_pointers", no_hash_pointers_enable);
* Note: The default behaviour (unadorned %p) is to hash the address,
* rendering it useful as a unique identifier.
+ *
+ * There is also a '%pA' format specifier, but it is only intended to be used
+ * from Rust code to format core::fmt::Arguments. Do *not* use it from C.
+ * See rust/kernel/ for details.
static noinline_for_stack
char *pointer(const char *fmt, char *buf, char *end, void *ptr,
@@ -2460,6 +2468,10 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
return device_node_string(buf, end, ptr, spec, fmt + 1);
case 'f':
return fwnode_string(buf, end, ptr, spec, fmt + 1);
+ case 'A':
+ return rust_fmt_argument(buf, end, ptr);
case 'x':
return pointer_string(buf, end, ptr, spec);
case 'e':
 \ /
  Last update: 2021-12-06 15:07    [W:0.234 / U:0.508 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site