[lkml]   [2021]   [Jul]   [4]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
Patch in this message
Subject[PATCH 04/17] vsprintf: add new `%pA` format specifier
From: Miguel Ojeda <>

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: Geoffrey Thomas <>
Signed-off-by: Geoffrey Thomas <>
Co-developed-by: Finn Behrens <>
Signed-off-by: Finn Behrens <>
Co-developed-by: Adam Bratschi-Kaye <>
Signed-off-by: Adam Bratschi-Kaye <>
Co-developed-by: Wedson Almeida Filho <>
Signed-off-by: Wedson Almeida Filho <>
Co-developed-by: Boqun Feng <>
Signed-off-by: Boqun Feng <>
Co-developed-by: Sumera Priyadarsini <>
Signed-off-by: Sumera Priyadarsini <>
Co-developed-by: Michael Ellerman <>
Signed-off-by: Michael Ellerman <>
Co-developed-by: Sven Van Asbroeck <>
Signed-off-by: Sven Van Asbroeck <>
Co-developed-by: Gary Guo <>
Signed-off-by: Gary Guo <>
Co-developed-by: Boris-Chengbiao Zhou <>
Signed-off-by: Boris-Chengbiao Zhou <>
Co-developed-by: Fox Chen <>
Signed-off-by: Fox Chen <>
Co-developed-by: Ayaan Zaidi <>
Signed-off-by: Ayaan Zaidi <>
Co-developed-by: Douglas Su <>
Signed-off-by: Douglas Su <>
Co-developed-by: Yuki Okushi <>
Signed-off-by: Yuki Okushi <>
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 f0c35d9b65b..e7afe954004 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -2182,6 +2182,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;
@@ -2335,6 +2339,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,
@@ -2407,6 +2415,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-07-04 22:29    [W:0.389 / U:0.216 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site