Printbufs
Subsystems (19)
v5 cover Printbufs
[all this code is from Kent, I'm helping him out because he's having trouble with git send-email and OAUTH]
This should (hopefully) be the final iteration of this patch series.
Previous discussion:
https://lore.kernel.org/linux-mm/[email protected]/
Changes since v4:
- %pf(%p) format extension has been dropped for now, since it turned out to be a bit controversial. I think we're going to want it, but I'm leaving it for a future patch series.
- There was a small off by one error in the patch converting trace_events_synth. The original code using seq_buf had to calculate the size of the string to allocate; since printbufs have native support for heap allocating strings, the version of the patch in this series just uses that for a nice cleanup.
What are printbufs, and why?
============================
Printbufs are like the existing seq_buf, with some differences and new features:
- Heap allocation (optional)
Currently, we have no standard mechanism for printing to a heap-allocated
string: code that needs to do this must calculate the size of the buffer to allocate, which is tedious and error prone. IOW, this is a major ergonomic improvement.
- Indent level
Any time we're printing structured multi-line data, proper indenting makes the output considerably more readable. Printbufs have state for the current indent level, controlled by printbuf_indent_add() and printbuf_indent_sub() and obeyed by prt_newline().
In the future this may work with just \n, pending work to do this without performance regressions.
- Tab stops
Printing reports with nicely aligned columns is something we do a _lot_, and printbufs make this a lot easier. After setting tabstops (byte offsets from start of line), prt_tab() will output spaces up to the next tabstop, and pr_tab_rjust() will right justify output since the previous output against the next tabstap.
In the future this may work with just \t, pending work to do this without performance regressions.
Why a new data structure, instead of extending seq_buf?
=======================================================
Some semantic changes were required. The main ones were:
- printbufs have different overflow semantics vs. seq_buf, to support snprintf() which is required to return the number of characters that were written even when an empty buffer is passed
- seq_buf has a readpos member which printbuf drops, since it's only for tracing.
Also, a number of the existing seq_buf users were converted to printbuf's heap allocation functionality, replacing open coded string size calculations and allocations.
Given all this, adding the new thing and converting existing users one by one felt like the more incremental/bisectable approach.
Goals of this patch series:
===========================
- A unified API for functions that output strings (i.e. pretty printers).
Currently, we've got a bit of a mess of different calling conventions for functions that output strings. There's sprintf/snprintf style, seq_buf, and various other variations of passing raw character pointers around.
- Low level helpers for building up strings (prt_char(), prt_str(), etc.).
This replaces a _lot_ of low level pointer arithmetic in vsprintf.c, and elsewhere, with proper helpers.
- Cleanup of pretty-printers in vsprintf.c
Currently, we have a lot of pretty-printers in vsprintf.c invoked by various %p extensions. However, arguments to those pretty printers are passed as additional format string characters, instead of normal function arguments, and we have the code that decodes format strings mixed with pretty-printer code, which is not great for readability and makes those pretty-printers unnecessarily tied to vsprintf.c internal details.
One of my goals is to give those pretty-printers normal function call interfaces, and separate out the format-string parsing: this patch series does some of the prep work. I've got more patches in progress in that vein, but this patch series is big enough as is so I'm leaving them for later.
Kent Overstreet (32):
lib/printbuf: New data structure for printing strings lib/string_helpers: Convert string_escape_mem() to printbuf
vsprintf: Convert to printbuf
lib/hexdump: Convert to printbuf
lib/string_helpers: string_get_size() now returns characters wrote lib/printbuf: Heap allocation
lib/printbuf: Tabstops, indenting
lib/printbuf: Unit specifiers
vsprintf: Improve number()
vsprintf: prt_u64_minwidth(), prt_u64()
test_printf: Drop requirement that sprintf not write past nul
vsprintf: Start consolidating printf_spec handling
vsprintf: Refactor resource_string()
vsprintf: Refactor fourcc_string()
vsprintf: Refactor ip_addr_string()
vsprintf: Refactor mac_address_string()
vsprintf: time_and_date() no longer takes printf_spec
vsprintf: flags_string() no longer takes printf_spec
vsprintf: Refactor device_node_string, fwnode_string
vsprintf: Refactor hex_string, bitmap_string_list, bitmap_string Input/joystick/analog: Convert from seq_buf -> printbuf mm/memcontrol.c: Convert to printbuf
clk: tegra: bpmp: Convert to printbuf tools/testing/nvdimm: Convert to printbuf
powerpc: Convert to printbuf
x86/resctrl: Convert to printbuf
PCI/P2PDMA: Convert to printbuf
tracing: trace_events_synth: Convert to printbuf
d_path: prt_path()
ACPI/APEI: Add missing include
tracing: Convert to printbuf
Delete seq_buf
arch/powerpc/kernel/process.c | 16 +- arch/powerpc/kernel/security.c | 75 +- arch/powerpc/platforms/pseries/papr_scm.c | 34 +- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 +- drivers/acpi/apei/erst-dbg.c | 1 + drivers/clk/tegra/clk-bpmp.c | 21 +- drivers/input/joystick/analog.c | 23 +- drivers/pci/p2pdma.c | 21 +- fs/d_path.c | 35 + include/linux/dcache.h | 1 + include/linux/kernel.h | 12 + include/linux/printbuf.h | 257 ++++ include/linux/seq_buf.h | 162 -- include/linux/string.h | 5 + include/linux/string_helpers.h | 8 +- include/linux/trace_events.h | 2 +- include/linux/trace_seq.h | 17 +- kernel/trace/trace.c | 45 +- kernel/trace/trace_dynevent.c | 34 +- kernel/trace/trace_events_filter.c | 2 +- kernel/trace/trace_events_synth.c | 53 +- kernel/trace/trace_functions_graph.c | 6 +- kernel/trace/trace_kprobe.c | 2 +- kernel/trace/trace_seq.c | 111 +- lib/Makefile | 4 +- lib/hexdump.c | 246 ++-- lib/printbuf.c | 258 ++++ lib/seq_buf.c | 397 ----- lib/string_helpers.c | 224 +-- lib/test_hexdump.c | 30 +- lib/test_printf.c | 6 - lib/vsprintf.c | 1640 ++++++++++----------- mm/memcontrol.c | 54 +- tools/testing/nvdimm/test/ndtest.c | 22 +- 34 files changed, 1855 insertions(+), 1985 deletions(-) create mode 100644 include/linux/printbuf.h delete mode 100644 include/linux/seq_buf.h create mode 100644 lib/printbuf.c delete mode 100644 lib/seq_buf.c
Signature · 1 line
2.35.1
PATCH v5 1/32 lib/printbuf: New data structure for printing strings
From: Kent Overstreet <[email protected]>
This adds printbufs: a printbuf points to a char * buffer and knows the size of the output buffer as well as the current output position.
Future patches will be adding more features to printbuf, but initially printbufs are targeted at refactoring and improving our existing code in lib/vsprintf.c - so this initial printbuf patch has the features required for that.
--- include/linux/printbuf.h | 122 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 include/linux/printbuf.h
include/linux/printbuf.h new file +122 -0
@@ -0,0 +1,122 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +/* Copyright (C) 2022 Kent Overstreet */ + +#ifndef _LINUX_PRINTBUF_H +#define _LINUX_PRINTBUF_H + +#include <linux/kernel.h> +#include <linux/string.h> + +/* +* Printbufs: String buffer for outputting (printing) to, for vsnprintf +*/ + +struct printbuf { +char *buf; +unsigned size; +unsigned pos; +}; + +/* +* Returns size remaining of output buffer: +*/ +static inline unsigned printbuf_remaining_size (struct printbuf *out) +{ +return out->pos < out->size ? out->size - out->pos :0 ; +} + +/* +* Returns number of characters we can print to the output buffer - i.e. +* excluding the terminating nul: +*/ +static inline unsigned printbuf_remaining (struct printbuf *out) +{ +return out->pos < out->size ? out->size - out->pos -1 :0 ; +} + +static inline unsigned printbuf_written (struct printbuf *out) +{ +return out->size ?min (out->pos, out->size -1 ) :0 ; +} + +/* +* Returns true if output was truncated: +*/ +static inline bool printbuf_overflowed (struct printbuf *out) +{ +return out->pos >= out->size; +} + +static inline void printbuf_nul_terminate (struct printbuf *out) +{ +if (out->pos < out->size) + out->buf[out->pos] =0 ; +else if (out->size) + out->buf[out->size -1 ] =0 ; +} + +static inline void __prt_char (struct printbuf *out,char c) +{ +if (printbuf_remaining (out)) + out->buf[out->pos] = c; + out->pos++; +} + +static inline void prt_char (struct printbuf *out,char c) +{ +__prt_char (out, c); +printbuf_nul_terminate (out); +} + +static inline void __prt_chars (struct printbuf *out,char c,unsigned n) +{ +unsigned i, can_print =min (n,printbuf_remaining (out)); + +for (i =0 ; i < can_print; i++) + out->buf[out->pos++] = c; + out->pos += n - can_print; +} + +static inline void prt_chars (struct printbuf *out,char c,unsigned n) +{ +__prt_chars (out, c, n); +printbuf_nul_terminate (out); +} + +static inline void prt_bytes (struct printbuf *out,const void *b,unsigned n) +{ +unsigned i, can_print =min (n,printbuf_remaining (out)); + +for (i =0 ; i < can_print; i++) + out->buf[out->pos++] = ((char *) b)[i]; + out->pos += n - can_print; + +printbuf_nul_terminate (out); +} + +static inline void prt_str (struct printbuf *out,const char *str) +{ +prt_bytes (out, str,strlen (str)); +} + +static inline void prt_hex_byte (struct printbuf *out,u8 byte) +{ +__prt_char (out,hex_asc_hi (byte)); +__prt_char (out,hex_asc_lo (byte)); +printbuf_nul_terminate (out); +} + +static inline void prt_hex_byte_upper (struct printbuf *out,u8 byte) +{ +__prt_char (out,hex_asc_upper_hi (byte)); +__prt_char (out,hex_asc_upper_lo (byte)); +printbuf_nul_terminate (out); +} + +#define PRINTBUF_EXTERN (_buf, _size) \ +((struct printbuf) { \ + .buf = _buf, \ + .size = _size, \ +}) + +#endif /* _LINUX_PRINTBUF_H */
Signature · 1 line
2.35.1
PATCH v5 2/32 lib/string_helpers: Convert string_escape_mem() to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
Like the upcoming vsprintf.c conversion, this converts string_escape_mem to prt_escaped_string(), which uses and outputs to a printbuf, and makes string_escape_mem() a smaller wrapper to support existing users.
The new printbuf helpers greatly simplify the code.
--- include/linux/string_helpers.h | 4 + lib/string_helpers.c | 217 ++++++++++++++++++--------------- 2 files changed, 126 insertions(+), 95 deletions(-)
include/linux/string_helpers.h +4 -0
@@ -10,6 +10,7 @@struct device ;struct file ;struct task_struct ; +struct printbuf ;/* Descriptions of the types of units to * print in */
@@ -62,6 +63,8 @@ static inline int string_unescape_any_inplace(char *buf)#define ESCAPE_ALL_MASK GENMASK(8, 0) +void prt_escaped_string (struct printbuf *out,const char *src,size_t isz, +unsigned int flags,const char *only);int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only);
@@ -71,6 +74,7 @@ static inline int string_escape_mem_any_np(const char *src, size_t isz,return string_escape_mem (src, isz, dst, osz,ESCAPE_ANY_NP , only); } +static inline int string_escape_str (const char *src,char *dst,size_t sz,unsigned int flags,const char *only) {
lib/string_helpers.c +122 -95
309 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH v5 02/32] lib/string_helpers: Convert string_escape_mem() to printbuf
Quoted 8 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
Like the upcoming
vsprintf.cconversion, this convertsstring_escape_memtoprt_escaped_string(), which uses and outputs to a printbuf, and makesstring_escape_mem()a smaller wrapper to support existing users.The new printbuf helpers greatly simplify the code.
...
Quoted 4 lines
struct device;
struct file;
structtask_struct;
+struct printbuf;
Keep it ordered?
...
include/linux/string_helpers.h static inline int string_escape_mem_any_np(const char *src, size_t isz, · line 80
Quoted 8 lines
include/linux/string_helpers.h +1
-0@@ -71,6 +74,7 @@ static inline int string_escape_mem_any_np(const char *src, size_t isz,return string_escape_mem (src, isz, dst, osz,ESCAPE_ANY_NP , only); } +static inline int string_escape_str (const char *src,char *dst,size_t sz,unsigned int flags,const char *only) {
Stray change.
...
lib/string_helpers.c int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, · line 494
Quoted 3 lines
if (!(is_append || in_dict) && is_dict &&lib/string_helpers.c +1
-1- escape_passthrough(c, &p, end)) + escape_passthrough(out, c))
That (a bit strange) indentation is on purpose. Can we keep it?
Quoted 1 line
continue;
...
Not sure if the below was in the original text, but maybe it makes sense to amend.
Quoted 2 lines
+ * Description:
+ * The process of escaping byte buffer includes several parts. They are applied
a byte in the buffer (?)
Quoted 9 lines
+ * in the following sequence.
+ *
+ * 1. The character is not matched to the one from @only string and thus
+ * must go as-is to the output.
+ * 2. The character is matched to the printable and ASCII classes, if asked,
+ * and in case of match it passes through to the output.
+ * 3. The character is matched to the printable or ASCII class, if asked,
+ * and in case of match it passes through to the output.
+ * 4. The character is checked if it falls into the class given by @flags.
Quoted 6 lines
+ * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any + * character. Note that they actually can't go together, otherwise + * %ESCAPE_HEX will be ignored.+ *
+ * Caller must provide valid source and destination pointers. Be aware that
+ * destination buffer will not be NULL-terminated, thus caller have to append
the caller has
Quoted 1 line
+ * it if needs. The supported flags are::
if needed.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v5 02/32] lib/string_helpers: Convert string_escape_mem() to printbuf
Quoted 11 lines · Andy Shevchenko
Quoted 8 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
Like the upcoming
vsprintf.cconversion, this convertsstring_escape_memtoprt_escaped_string(), which uses and outputs to a printbuf, and makesstring_escape_mem()a smaller wrapper to support existing users.The new printbuf helpers greatly simplify the code.
Quoted 8 lines
...
Quoted 4 lines
struct device;
struct file;
structtask_struct;
+struct printbuf;Keep it ordered?
I generally try to keep my #includes alphabetized, missed this :)
Quoted 7 lines
...
Quoted 4 lines
include/linux/string_helpers.h +0
-0@@ -71,6 +74,7 @@ static inline int string_escape_mem_any_np(const char *src, size_t isz,return string_escape_mem (src, isz, dst, osz,ESCAPE_ANY_NP , only); }
Quoted 6 lines
include/linux/string_helpers.h static inline int string_escape_mem_any_np(const char *src, size_t isz, · line 80
Quoted 4 lines
include/linux/string_helpers.h +1
-0+static inline int string_escape_str (const char *src,char *dst,size_t sz,unsigned int flags,const char *only) {Stray change.
Fixed
Quoted 7 lines
...
lib/string_helpers.c int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, · line 494
Quoted 3 lines
if (!(is_append || in_dict) && is_dict &&lib/string_helpers.c +1
-1- escape_passthrough(c, &p, end)) + escape_passthrough(out, c))That (a bit strange) indentation is on purpose. Can we keep it?
Yep, done
Quoted 11 lines
Quoted 1 line
continue;...
Not sure if the below was in the original text, but maybe it makes sense to amend.
Quoted 2 lines
+ * Description:
+ * The process of escaping byte buffer includes several parts. They are applieda byte in the buffer (?)
See below
Quoted 17 lines
Quoted 10 lines
+ * in the following sequence.
+ *
+ * 1. The character is not matched to the one from @only string and thus
+ * must go as-is to the output.
+ * 2. The character is matched to the printable and ASCII classes, if asked,
+ * and in case of match it passes through to the output.
+ * 3. The character is matched to the printable or ASCII class, if asked,
+ * and in case of match it passes through to the output.
+ * 4. The character is checked if it falls into the class given by @flags.Quoted 6 lines
+ * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any + * character. Note that they actually can't go together, otherwise + * %ESCAPE_HEX will be ignored.+ *
+ * Caller must provide valid source and destination pointers. Be aware that
+ * destination buffer will not be NULL-terminated, thus caller have to append
Quoted 1 line
the caller has
Yeah I'm actually not changing the function comment for string_escape_mem(), that's just git getting confused since I reused most of it for prt_escaped_mem(). I'll leave any fixups for that for you if that's ok - the docs look clear enough to me.
Cheers
PATCH v5 3/32 vsprintf: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts vsnprintf() to printbufs: instead of passing around raw char * pointers for current buf position and end of buf, we have a real type!
This makes the calling convention for our existing pretty printers a lot saner and less error prone, plus printbufs add some new helpers that make the code smaller and more readable, with a lot less crazy pointer arithmetic.
There are a lot more refactorings to be done: this patch tries to stick to just converting the calling conventions, as that needs to be done all at once in order to avoid introducing a ton of wrappers that will just be deleted.
Thankfully we have good unit tests for printf, and they have been run and are all passing with this patch.
We have two new exported functions with this patch:
- prt_printf(), which is like snprintf but outputs to a printbuf
- prt_vprintf, like vsnprintf
These are the actual core print routines now - vsnprintf() is a wrapper around prt_vprintf().
--- include/linux/kernel.h | 4 + include/linux/string.h | 5 + lib/vsprintf.c | 1367 +++++++++++++++++++--------------------- 3 files changed, 643 insertions(+), 733 deletions(-)
include/linux/kernel.h +4 -0
@@ -207,6 +207,10 @@ extern int num_to_str(char *buf, int size,/* lib/printf utilities */ +struct printbuf ; +extern __printf(2, 3) void prt_printf(struct printbuf *out, const char *fmt, ...); +extern __printf(2, 0) void prt_vprintf(struct printbuf *out, const char *fmt, va_list); + extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); extern __printf(3, 4)
include/linux/string.h +5 -0
@@ -195,7 +195,12 @@ int __sysfs_match_string(const char * const *array, size_t n, const char *s);*/ #define sysfs_match_string (_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s) +struct printbuf ; +#ifdef CONFIG_BINARY_PRINTF +void prt_vbinprintf (struct printbuf *out,const char *fmt,va_list args); +void prt_bstrprintf (struct printbuf *out,const char *fmt,const u32 *bin_buf); +void prt_bprintf(struct printbuf *out, const char *fmt, ...) __printf(2, 3);int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args);int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf); int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
lib/vsprintf.c +634 -733
2,360 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v5 4/32 lib/hexdump: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts most of the hexdump code to printbufs, along with some significant cleanups and a bit of reorganization. The old non-printbuf functions are mostly left as wrappers around the new printbuf versions.
Big note: byte swabbing behaviour
Previously, hex_dump_to_buffer() would byteswab the groups of bytes being printed on little endian machines. This behaviour is... not standard or typical for a hex dumper, and this behaviour was silently added/changed without documentation (in 2007).
Given that the hex dumpers are just used for debugging output, nothing is likely to break, and hopefully by reverting to more standard behaviour the end result will be _less_ confusion, modulo a few kernel developers who will certainly be annoyed by their tools changing.
--- include/linux/kernel.h | 6 + lib/hexdump.c | 246 ++++++++++++++++++++++++----------------- lib/test_hexdump.c | 30 +---- 3 files changed, 159 insertions(+), 123 deletions(-)
include/linux/kernel.h +6 -0
@@ -293,6 +293,12 @@ extern int hex_to_bin(unsigned char ch);extern int __must_check hex2bin (u8 *dst,const char *src,size_t count);extern char *bin2hex (char *dst,const void *src,size_t count); +struct printbuf ; +void prt_hex_bytes (struct printbuf *,const void *,unsigned ,unsigned ,unsigned ); +void prt_hex_line (struct printbuf *,const void *,size_t ,int ,int ,bool ); +void prt_hex_dump (struct printbuf *,const void *,size_t , +const char *,int ,unsigned ,unsigned ,bool ); +bool mac_pton (const char *s,u8 *mac);/*
lib/hexdump.c +147 -99
297 lines not on this page · raw message
lib/test_hexdump.c +6 -24
@@ -25,36 +25,19 @@ static const char * const test_data_1[] __initconst = { "4c", "d1", "19", "99", "43", "b1", "af", "0c", }; -static const char *const test_data_2_le[]__initconst = { -"32be" ,"7bdb" ,"180a" ,"b293" , -"ba70" ,"24c4" ,"837d" ,"9b34" , -"9ca6" ,"ad31" ,"0f9c" ,"e9ac" , -"d14c" ,"9919" ,"b143" ,"0caf" , -}; - -static const char *const test_data_2_be[]__initconst = { +static const char *const test_data_2[]__initconst = {"be32" ,"db7b" ,"0a18" ,"93b2" ,"70ba" ,"c424" ,"7d83" ,"349b" ,"a69c" ,"31ad" ,"9c0f" ,"ace9" ,"4cd1" ,"1999" ,"43b1" ,"af0c" , }; -static const char *const test_data_4_le[]__initconst = { -"7bdb32be" ,"b293180a" ,"24c4ba70" ,"9b34837d" , -"ad319ca6" ,"e9ac0f9c" ,"9919d14c" ,"0cafb143" , -}; - -static const char *const test_data_4_be[]__initconst = { +static const char *const test_data_4[]__initconst = {"be32db7b" ,"0a1893b2" ,"70bac424" ,"7d83349b" ,"a69c31ad" ,"9c0face9" ,"4cd11999" ,"43b1af0c" , }; -static const char *const test_data_8_le[]__initconst = { -"b293180a7bdb32be" ,"9b34837d24c4ba70" , -"e9ac0f9cad319ca6" ,"0cafb1439919d14c" , -}; - -static const char *const test_data_8_be[]__initconst = { +static const char *const test_data_8[]__initconst = {"be32db7b0a1893b2" ,"70bac4247d83349b" ,"a69c31ad9c0face9" ,"4cd1199943b1af0c" , };
@@ -73,7 +56,6 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize,size_t l = len;int gs = groupsize, rs = rowsize;unsigned int i; -const bool is_be =IS_ENABLED (CONFIG_CPU_BIG_ENDIAN );if (rs !=16 && rs !=32 ) rs =16 ;
@@ -85,11 +67,11 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize, gs =1 ;if (gs ==8 ) - result = is_be ? test_data_8_be : test_data_8_le; + result = test_data_8;else if (gs ==4 ) - result = is_be ? test_data_4_be : test_data_4_le; + result = test_data_4;else if (gs ==2 ) - result = is_be ? test_data_2_be : test_data_2_le; + result = test_data_2;else result = test_data_1;
Signature · 1 line
2.35.1
PATCH v5 5/32 lib/string_helpers: string_get_size() now returns characters wrote
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like snprintf(); this changes string_get_size() to return the the return value of snprintf().
--- include/linux/string_helpers.h | 4 ++-- lib/string_helpers.c | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-)
include/linux/string_helpers.h +2 -2
@@ -19,8 +19,8 @@ enum string_size_units { STRING_UNITS_2, /* use binary powers of 2^10 */ }; -void string_get_size (u64 size,u64 blk_size,enum string_size_units units, -char *buf,int len); +int string_get_size (u64 size,u64 blk_size,enum string_size_units units, +char *buf,int len);#define UNESCAPE_SPACE BIT(0)#define UNESCAPE_OCTAL BIT(1)
lib/string_helpers.c +3 -4
@@ -33,8 +33,8 @@* at least 9 bytes and will always be zero terminated. * */ -void string_get_size (u64 size,u64 blk_size,const enum string_size_units units, -char *buf,int len) +int string_get_size (u64 size,u64 blk_size,const enum string_size_units units, +char *buf,int len) {static const char *const units_10[] = {"B" ,"kB" ,"MB" ,"GB" ,"TB" ,"PB" ,"EB" ,"ZB" ,"YB"
@@ -127,8 +127,7 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,else unit = units_str[units][i]; -snprintf (buf, len,"%u%s %s" , (u32 )size, - tmp, unit); +return snprintf (buf, len,"%u%s %s" , (u32 )size, tmp, unit); }EXPORT_SYMBOL (string_get_size);
Signature · 1 line
2.35.1
Re: [PATCH v5 05/32] lib/string_helpers: string_get_size() now returns characters wrote
Quoted 6 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
printbuf now needs to know the number of characters that would have been written if the buffer was too small, like
snprintf(); this changesstring_get_size()to return the the return value ofsnprintf().
the the --> the
Quoted 50 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Andy Shevchenko <[email protected]>--- include/linux/string_helpers.h | 4 ++-- lib/string_helpers.c | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-)include/linux/string_helpers.h +2
-2@@ -19,8 +19,8 @@ enum string_size_units { STRING_UNITS_2, /* use binary powers of 2^10 */ }; -void string_get_size (u64 size,u64 blk_size,enum string_size_units units, -char *buf,int len); +int string_get_size (u64 size,u64 blk_size,enum string_size_units units, +char *buf,int len);#define UNESCAPE_SPACE BIT(0)#define UNESCAPE_OCTAL BIT(1)lib/string_helpers.c +3
-5@@ -33,8 +33,8 @@* at least 9 bytes and will always be zero terminated. * */ -void string_get_size (u64 size,u64 blk_size,const enum string_size_units units, -char *buf,int len) +int string_get_size (u64 size,u64 blk_size,const enum string_size_units units, +char *buf,int len) {static const char *const units_10[] = {"B" ,"kB" ,"MB" ,"GB" ,"TB" ,"PB" ,"EB" ,"ZB" ,"YB" @@ -127,8 +127,7 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,else unit = units_str[units][i]; -snprintf (buf, len,"%u%s %s" , (u32 )size, - tmp, unit); +return snprintf (buf, len,"%u%s %s" , (u32 )size, tmp, unit); }EXPORT_SYMBOL (string_get_size); --2.35.1
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v5 6/32 lib/printbuf: Heap allocation
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This makes printbufs optionally heap allocated: a printbuf initialized with the PRINTBUF initializer will automatically heap allocate and resize as needed.
Allocations are done with GFP_KERNEL: code should use e.g. memalloc_nofs_save()/restore() as needed. Since we do not currently have memalloc_nowait_save()/restore(), in contexts where it is not safe to block we provide the helpers
printbuf_atomic_inc()
printbuf_atomic_dec()
When the atomic count is nonzero, memory allocations will be done with GFP_NOWAIT.
On memory allocation failure, output will be truncated. Code that wishes to check for memory allocation failure (in contexts where we should return -ENOMEM) should check if printbuf->allocation_failure is set. Since printbufs are expected to be typically used for log messages and on a best effort basis, we don't return errors directly.
Other helpers provided by this patch:
- printbuf_make_room(buf, extra)
Reallocates if necessary to make room for @extra bytes (not including terminating null).
- printbuf_str(buf)
Returns a null terminated string equivalent to the contents of @buf. If @buf was never allocated (or allocation failed), returns a constant empty string.
- printbuf_exit(buf)
Releases memory allocated by a printbuf.
--- include/linux/printbuf.h | 124 +++++++++++++++++++++++++++++++++------ lib/Makefile | 2 +- lib/printbuf.c | 76 ++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 lib/printbuf.c
include/linux/printbuf.h +107 -17
@@ -4,19 +4,73 @@#ifndef _LINUX_PRINTBUF_H#define _LINUX_PRINTBUF_H -#include <linux/kernel.h> -#include <linux/string.h> -/* -* Printbufs: String buffer for outputting (printing) to, for vsnprintf +* Printbufs: Simple strings for printing to, with optional heap allocation +* +* This code has provisions for use in userspace, to aid in making other code +* portable between kernelspace and userspace. +* +* Basic example: +* struct printbuf buf = PRINTBUF; +* +* prt_printf(&buf, "foo="); +* foo_to_text(&buf, foo); +* printk("%s", buf.buf); +* printbuf_exit(&buf); +* +* Or +* struct printbuf buf = PRINTBUF_EXTERN(char_buf, char_buf_size) +* +* We can now write pretty printers instead of writing code that dumps +* everything to the kernel log buffer, and then those pretty-printers can be +* used by other code that outputs to kernel log, sysfs, debugfs, etc. +* +* Memory allocation: Outputing to a printbuf may allocate memory. This +* allocation is done with GFP_KERNEL, by default: use the newer +* memalloc_*_(save|restore) functions as needed. +* +* Since no equivalent yet exists for GFP_ATOMIC/GFP_NOWAIT, memory allocations +* will be done with GFP_NOWAIT if printbuf->atomic is nonzero. +* +* It's allowed to grab the output buffer and free it later with kfree() instead +* of using printbuf_exit(), if the user just needs a heap allocated string at +* the end. +* +* Memory allocation failures: We don't return errors directly, because on +* memory allocation failure we usually don't want to bail out and unwind - we +* want to print what we've got, on a best-effort basis. But code that does want +* to return -ENOMEM may check printbuf.allocation_failure. */ +#include <linux/kernel.h> +#include <linux/string.h> +struct printbuf {char *buf;unsigned size;unsigned pos; +/* +* If nonzero, allocations will be done with GFP_ATOMIC: +*/ +u8 atomic; +bool allocation_failure:1 ; +bool heap_allocated:1 ; }; +int printbuf_make_room (struct printbuf *,unsigned ); +const char *printbuf_str (const struct printbuf *); +void printbuf_exit (struct printbuf *); + +/* Initializer for a heap allocated printbuf: */ +#define PRINTBUF ((struct printbuf) { .heap_allocated = true }) + +/* Initializer a printbuf that points to an external buffer: */ +#define PRINTBUF_EXTERN (_buf, _size) \ +((struct printbuf) { \ + .buf = _buf, \ + .size = _size, \ +}) +/* * Returns size remaining of output buffer: */
@@ -49,26 +103,36 @@ static inline bool printbuf_overflowed(struct printbuf *out)static inline void printbuf_nul_terminate (struct printbuf *out) { +printbuf_make_room (out,1 ); +if (out->pos < out->size) out->buf[out->pos] =0 ;else if (out->size) out->buf[out->size -1 ] =0 ; } -static inline void __prt_char (struct printbuf *out,char c) +/* Doesn't call printbuf_make_room(), doesn't nul terminate: */ +static inline void __prt_char_reserved (struct printbuf *out,char c) {if (printbuf_remaining (out)) out->buf[out->pos] = c; out->pos++; } +/* Doesn't nul terminate: */ +static inline void __prt_char (struct printbuf *out,char c) +{ +printbuf_make_room (out,1 ); +__prt_char_reserved (out, c); +} +static inline void prt_char (struct printbuf *out,char c) {__prt_char (out, c);printbuf_nul_terminate (out); } -static inline void __prt_chars (struct printbuf *out,char c,unsigned n) +static inline void __prt_chars_reserved (struct printbuf *out,char c,unsigned n) {unsigned i, can_print =min (n,printbuf_remaining (out));
@@ -79,13 +143,18 @@ static inline void __prt_chars(struct printbuf *out, char c, unsigned n)static inline void prt_chars (struct printbuf *out,char c,unsigned n) { -__prt_chars (out, c, n); +printbuf_make_room (out, n); +__prt_chars_reserved (out, c, n);printbuf_nul_terminate (out); }static inline void prt_bytes (struct printbuf *out,const void *b,unsigned n) { -unsigned i, can_print =min (n,printbuf_remaining (out)); +unsigned i, can_print; + +printbuf_make_room (out, n); + + can_print =min (n,printbuf_remaining (out));for (i =0 ; i < can_print; i++) out->buf[out->pos++] = ((char *) b)[i];
@@ -101,22 +170,43 @@ static inline void prt_str(struct printbuf *out, const char *str)static inline void prt_hex_byte (struct printbuf *out,u8 byte) { -__prt_char (out,hex_asc_hi (byte)); -__prt_char (out,hex_asc_lo (byte)); +printbuf_make_room (out,2 ); +__prt_char_reserved (out,hex_asc_hi (byte)); +__prt_char_reserved (out,hex_asc_lo (byte));printbuf_nul_terminate (out); }static inline void prt_hex_byte_upper (struct printbuf *out,u8 byte) { -__prt_char (out,hex_asc_upper_hi (byte)); -__prt_char (out,hex_asc_upper_lo (byte)); +printbuf_make_room (out,2 ); +__prt_char_reserved (out,hex_asc_upper_hi (byte)); +__prt_char_reserved (out,hex_asc_upper_lo (byte));printbuf_nul_terminate (out); } -#define PRINTBUF_EXTERN (_buf, _size) \ -((struct printbuf) { \ - .buf = _buf, \ - .size = _size, \ -}) +/** +* printbuf_reset - re-use a printbuf without freeing and re-initializing it: +*/ +static inline void printbuf_reset (struct printbuf *buf) +{ + buf->pos =0 ; + buf->allocation_failure =0 ; +} + +/** +* printbuf_atomic_inc - mark as entering an atomic section +*/ +static inline void printbuf_atomic_inc (struct printbuf *buf) +{ + buf->atomic++; +} + +/** +* printbuf_atomic_inc - mark as leaving an atomic section +*/ +static inline void printbuf_atomic_dec (struct printbuf *buf) +{ + buf->atomic--; +}#endif /* _LINUX_PRINTBUF_H */
lib/Makefile +1 -1
@@ -34,7 +34,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ earlycpio.o seq_buf.o siphash.o dec_and_lock.o \ nmi_backtrace.o nodemask.o win_minmax.o memcat_p.o \ - buildid.o cpumask.o + buildid.o cpumask.o printbuf.o lib-$(CONFIG_PRINTK) += dump_stack.o
lib/printbuf.c new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v5 7/32 lib/printbuf: Tabstops, indenting
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This patch adds two new features to printbuf for structured formatting:
- Indent level: the indent level, as a number of spaces, may be increased with pr_indent_add() and decreased with pr_indent_sub().
Subsequent lines, when started with pr_newline() (not "\n", although that may change) will then be intended according to the current indent level. This helps with pretty-printers that structure a large amonut of data across multiple lines and multiple functions.
- Tabstops: Tabstops may be set by assigning to the printbuf->tabstops array.
Then, pr_tab() may be used to advance to the next tabstop, printing as many spaces as required - leaving previous output left justified to the previous tabstop. pr_tab_rjust() advances to the next tabstop but inserts the spaces just after the previous tabstop - right justifying the previously-outputted text to the next tabstop.
--- include/linux/printbuf.h | 30 ++++++++++ lib/printbuf.c | 125 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+)
include/linux/printbuf.h +30 -0
@@ -40,6 +40,23 @@* memory allocation failure we usually don't want to bail out and unwind - we * want to print what we've got, on a best-effort basis. But code that does want * to return -ENOMEM may check printbuf.allocation_failure. +* +* Indenting, tabstops: +* +* To aid is writing multi-line pretty printers spread across multiple +* functions, printbufs track the current indent level. +* +* printbuf_indent_push() and printbuf_indent_pop() increase and decrease the current indent +* level, respectively. +* +* To use tabstops, set printbuf->tabstops[]; they are in units of spaces, from +* start of line. Once set, prt_tab() will output spaces up to the next tabstop. +* prt_tab_rjust() will also advance the current line of text up to the next +* tabstop, but it does so by shifting text since the previous tabstop up to the +* next tabstop - right justifying it. +* +* Make sure you use prt_newline() instead of \n in the format string for indent +* level and tabstops to work corretly. */ #include <linux/kernel.h>
@@ -49,18 +66,29 @@ struct printbuf {char *buf;unsigned size;unsigned pos; +unsigned last_newline; +unsigned last_field; +unsigned indent;/* * If nonzero, allocations will be done with GFP_ATOMIC: */ u8 atomic; bool allocation_failure:1; bool heap_allocated:1; +u8 tabstop; +u8 tabstops[4 ]; };int printbuf_make_room (struct printbuf *,unsigned );const char *printbuf_str (const struct printbuf *);void printbuf_exit (struct printbuf *); +void prt_newline (struct printbuf *); +void printbuf_indent_add (struct printbuf *,unsigned ); +void printbuf_indent_sub (struct printbuf *,unsigned ); +void prt_tab (struct printbuf *); +void prt_tab_rjust (struct printbuf *); +/* Initializer for a heap allocated printbuf: */ #define PRINTBUF ((struct printbuf) { .heap_allocated = true })
@@ -191,6 +219,8 @@ static inline void printbuf_reset(struct printbuf *buf) { buf->pos =0 ; buf->allocation_failure =0 ; + buf->indent =0 ; + buf->tabstop =0 ; }/**
lib/printbuf.c +125 -0
134 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v5 8/32 lib/printbuf: Unit specifiers
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This adds options to printbuf for specifying whether units should be printed raw (default) or with human readable units, and for controlling whether human-readable units should be base 2 (default), or base 10.
This also adds new helpers that obey these options:
- pr_human_readable_u64
- pr_human_readable_s64
These obey printbuf->si_units
- pr_units_u64
- pr_units_s64
These obey both printbuf-human_readable_units and printbuf->si_units
--- include/linux/printbuf.h | 15 +++++++++++ lib/printbuf.c | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+)
include/linux/printbuf.h +15 -0
@@ -57,11 +57,20 @@* * Make sure you use prt_newline() instead of \n in the format string for indent * level and tabstops to work corretly. +* +* Output units: printbuf->units exists to tell pretty-printers how to output +* numbers: a raw value (e.g. directly from a superblock field), as bytes, or as +* human readable bytes. prt_units() obeys it. */ #include <linux/kernel.h> #include <linux/string.h> +enum printbuf_si { +PRINTBUF_UNITS_2 ,/* use binary powers of 2^10 */ +PRINTBUF_UNITS_10 ,/* use powers of 10^3 (standard SI) */ +}; +struct printbuf {char *buf;unsigned size;
@@ -75,6 +84,8 @@ struct printbuf {u8 atomic; bool allocation_failure:1; bool heap_allocated:1; + enum printbuf_si si_units:1; + bool human_readable_units:1;u8 tabstop;u8 tabstops[4 ]; };
@@ -88,6 +99,10 @@ void printbuf_indent_add(struct printbuf *, unsigned);void printbuf_indent_sub (struct printbuf *,unsigned );void prt_tab (struct printbuf *);void prt_tab_rjust (struct printbuf *); +void prt_human_readable_u64 (struct printbuf *,u64 ); +void prt_human_readable_s64 (struct printbuf *,s64 ); +void prt_units_u64 (struct printbuf *,u64 ); +void prt_units_s64 (struct printbuf *,s64 );/* Initializer for a heap allocated printbuf: */ #define PRINTBUF ((struct printbuf) { .heap_allocated = true })
lib/printbuf.c +57 -0
66 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v5 9/32 vsprintf: Improve number()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This patch refactors number() to make it a bit clearer, and it also changes it to call printbuf_make_room() only once at the start, instead of in the printbuf output helpers.
--- lib/vsprintf.c | 83 +++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 42 deletions(-)
lib/vsprintf.c +41 -42
@@ -457,93 +457,92 @@ void number(struct printbuf *out, unsigned long long num, {/* put_dec requires 2-byte alignment of the buffer. */ char tmp[3 *sizeof (num)]__aligned(2) ; -char sign; -char locase; +char sign =0 ; +/* locase = 0 or 0x20. ORing digits or letters with 'locase' +* produces same digits or (maybe lowercased) letters */ +char locase = (spec.flags &SMALL );int need_pfx = ((spec.flags &SPECIAL ) && spec.base !=10 ); -int i;bool is_zero = num ==0LL ;int field_width = spec.field_width;int precision = spec.precision; +int nr_digits =0 ; +int output_bytes =0 ; -/* locase = 0 or 0x20. ORing digits or letters with 'locase' -* produces same digits or (maybe lowercased) letters */ - locase = (spec.flags &SMALL );if (spec.flags &LEFT ) spec.flags &= ~ZEROPAD ; - sign =0 ;if (spec.flags &SIGN ) {if ((signed long long )num <0 ) { sign ='-' ; num = -(signed long long )num; - field_width--; + output_bytes++; }else if (spec.flags &PLUS ) { sign ='+' ; - field_width--; + output_bytes++; }else if (spec.flags &SPACE ) { sign =' ' ; - field_width--; + output_bytes++; } }if (need_pfx) {if (spec.base ==16 ) - field_width -=2 ; + output_bytes +=2 ;else if (!is_zero) - field_width--; + output_bytes++; }/* generate full string in tmp[], in reverse order */ - i =0 ; -if (num < spec.base) - tmp[i++] = hex_asc_upper[num] | locase; -else if (spec.base !=10 ) {/* 8 or 16 */ +if (spec.base ==10 ) { + nr_digits =put_dec (tmp, num) - tmp; + }else {/* 8 or 16 */ int mask = spec.base -1 ; -int shift =3 ; +int shift =ilog2 ((unsigned ) spec.base); -if (spec.base ==16 ) - shift =4 ;do { - tmp[i++] = (hex_asc_upper[((unsigned char )num) & mask] | locase); + tmp[nr_digits++] = (hex_asc_upper[((unsigned char )num) & mask] | locase); num >>= shift; }while (num); - }else {/* base 10 */ - i =put_dec (tmp, num) - tmp; }/* printing 100 using %2d gives "100", not "00" */ -if (i > precision) - precision = i; + precision =max (nr_digits, precision); + output_bytes += precision; + field_width =max (0 , field_width - output_bytes); + +printbuf_make_room (out, field_width + output_bytes); +/* leading space padding */ - field_width =max (0 , field_width - precision);if (!(spec.flags & (ZEROPAD |LEFT )) && field_width) { -__prt_chars (out,' ' , field_width); +__prt_chars_reserved (out,' ' , field_width); field_width =0 ; } +/* sign */ if (sign) -__prt_char (out, sign); +__prt_char_reserved (out, sign); +/* "0x" / "0" prefix */ if (need_pfx) {if (spec.base ==16 || !is_zero) -__prt_char (out,'0' ); +__prt_char_reserved (out,'0' );if (spec.base ==16 ) -__prt_char (out,'X' | locase); +__prt_char_reserved (out,'X' | locase); } -/* zero or space padding */ -if (!(spec.flags &LEFT ) && field_width) { -char c =' ' + (spec.flags &ZEROPAD ); -__prt_chars (out, c, field_width); - field_width =0 ; - } -/* hmm even more zero padding? */ -if (precision > i) -__prt_chars (out,'0' , precision - i); +/* zero padding */ +if (!(spec.flags &LEFT ) && field_width) +__prt_chars_reserved (out,'0' , field_width); + +/* zero padding from precision */ +if (precision > nr_digits) +__prt_chars_reserved (out,'0' , precision - nr_digits); +/* actual digits of result */ -while (--i >=0 ) -__prt_char (out, tmp[i]); +while (--nr_digits >=0 ) +__prt_char_reserved (out, tmp[nr_digits]); +/* trailing space padding */ -if (field_width) -__prt_chars (out,' ' , field_width); +if ((spec.flags &LEFT ) && field_width) +__prt_chars_reserved (out,' ' , field_width);printbuf_nul_terminate (out); }
Signature · 1 line
2.35.1
PATCH v5 10/32 vsprintf: prt_u64_minwidth(), prt_u64()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This adds two new-style printbuf helpers for printing simple u64s, and converts num_to_str() to be a simple wrapper around prt_u64_minwidth().
--- include/linux/kernel.h | 4 +- lib/vsprintf.c | 94 ++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 50 deletions(-)
include/linux/kernel.h +3 -1
@@ -202,12 +202,14 @@ static inline void might_fault(void) { }void do_exit (long error_code)__noreturn ; +struct printbuf ; +extern void prt_u64_minwidth (struct printbuf *out,u64 num,unsigned width); +extern void prt_u64 (struct printbuf *out,u64 num);extern int num_to_str (char *buf,int size,unsigned long long num,unsigned int width);/* lib/printf utilities */ -struct printbuf ; extern __printf(2, 3) void prt_printf(struct printbuf *out, const char *fmt, ...); extern __printf(2, 0) void prt_vprintf(struct printbuf *out, const char *fmt, va_list);
lib/vsprintf.c +45 -49
146 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v5 11/32 test_printf: Drop requirement that sprintf not write past nul
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
The current test code checks that sprintf never writes past the terminating nul. This is a rather strange requirement, completely separate from writing past the end of the buffer, which of course we can't do: writing anywhere to the buffer passed to snprintf, within size of course, should be perfectly fine.
Since this check has no documentation as to where it comes from or what depends on it, and it's getting in the way of further refactoring (printf_spec handling is right now scattered massively throughout the code, and we'd like to consolidate it) - delete it.
Also, many current pretty-printers building up their output on the stack, and then copy it to the actual output buffer - by eliminating this requirement we can kill those extra buffers.
--- lib/test_printf.c | 6 ------ 1 file changed, 6 deletions(-)
lib/test_printf.c +0 -6
@@ -84,12 +84,6 @@ do_test(int bufsize, const char *expect, int elen,return 1 ; } -if (memchr_inv (test_buffer + written +1 ,FILL_CHAR , bufsize - (written +1 ))) { -pr_warn ("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n" , - bufsize, fmt); -return 1 ; - } -if (memchr_inv (test_buffer + bufsize,FILL_CHAR ,BUF_SIZE +PAD_SIZE - bufsize)) {pr_warn ("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n" , bufsize, fmt);return 1 ;
Signature · 1 line
2.35.1
PATCH v5 12/32 vsprintf: Start consolidating printf_spec handling
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
printf_spec is right now something of a mess - it's a grab-bag of state that's interpreted inconsistently by different code, and it's scattered throughout vsprintf.c.
We'd like to get it out of the pretty-printers, and have it be solely the responsibility of vsprintf()/vpr_buf(), the code that parses and handles format strings.
Most of the code that uses printf_spec is only using it for a minimum & maximum field width - that can be done at the toplevel by checking how much we just printed, and padding or truncating it as necessary. This patch takes those "simple" uses of printf_spec and moves them as far up the call stack as possible.
This patch also renames some helpers and creates new ones that don't take printf_spec:
- do_width_precision: new helper that handles with/precision of printf_spec
- error_string -> error_string_spec
- check_pointer -> check_pointer_spec
- string -> string_spec
Next patches will be reducing/eliminating uses of the *_spec versions.
--- lib/vsprintf.c | 248 ++++++++++++++++++++++++++++--------------------- 1 file changed, 141 insertions(+), 107 deletions(-)
lib/vsprintf.c +141 -107
@@ -616,6 +616,19 @@ void widen_string(struct printbuf *out, int n,prt_chars (out,' ' , spaces); } +static void do_width_precision (struct printbuf *out,unsigned prev_pos, +struct printf_spec spec) +{ +unsigned n = out->pos - prev_pos; + +if (n > spec.precision) { + out->pos -= n - spec.precision; + n = spec.precision; + } + +widen_string (out, n, spec); +} +/* Handle string from a well known address. */ static void string_nocheck(struct printbuf *out, const char *s,
@@ -648,7 +661,7 @@ static void err_ptr(struct printbuf *out, void *ptr, }/* Be careful: error messages must fit into the given buffer. */ -static void error_string (struct printbuf *out,const char *s, +static void error_string_spec (struct printbuf *out,const char *s,struct printf_spec spec) {/*
@@ -678,7 +691,7 @@ static const char *check_pointer_msg(const void *ptr)return NULL ; } -static int check_pointer (struct printbuf *out, +static int check_pointer_spec (struct printbuf *out,const void *ptr,struct printf_spec spec) {
@@ -686,7 +699,7 @@ static int check_pointer(struct printbuf *out, err_msg =check_pointer_msg (ptr);if (err_msg) { -error_string (out, err_msg, spec); +error_string_spec (out, err_msg, spec);return -EFAULT ; }
@@ -694,16 +707,47 @@ static int check_pointer(struct printbuf *out, }static noinline_for_stack -void string(struct printbuf *out, +void string_spec(struct printbuf *out,const char *s,struct printf_spec spec) { -if (check_pointer (out, s, spec)) +if (check_pointer_spec (out, s, spec))return ;string_nocheck (out, s, spec); } +static void error_string (struct printbuf *out,const char *s) +{ +/* +* Hard limit to avoid a completely insane messages. It actually +* works pretty well because most error messages are in +* the many pointer format modifiers. +*/ +prt_bytes (out, s,min (strlen (s),2 *sizeof (void *))); +} + +static int check_pointer (struct printbuf *out,const void *ptr) +{ +const char *err_msg; + + err_msg =check_pointer_msg (ptr); +if (err_msg) { +error_string (out, err_msg); +return -EFAULT ; + } + +return 0 ; +} + +static void string (struct printbuf *out,const char *s) +{ +if (check_pointer (out, s)) +return ; + +prt_str (out, s); +} +static void pointer_string (struct printbuf *out,const void *ptr,struct printf_spec spec)
@@ -805,7 +849,7 @@ static void ptr_to_id(struct printbuf *out,if (ret) { spec.field_width =2 *sizeof (ptr);/* string length must be less than default_width */ -return error_string (out, str, spec); +return error_string_spec (out, str, spec); }pointer_string (out, (const void *)hashval, spec);
@@ -846,7 +890,7 @@ void restricted_pointer(struct printbuf *out,if (in_irq () ||in_serving_softirq () ||in_nmi ()) {if (spec.field_width ==-1 ) spec.field_width =2 *sizeof (ptr); -return error_string (out,"pK-error" , spec); +return error_string_spec (out,"pK-error" , spec); }/*
@@ -876,14 +920,12 @@ void restricted_pointer(struct printbuf *out, }static noinline_for_stack -void dentry_name(struct printbuf *out, -const struct dentry *d,struct printf_spec spec, +void dentry_name(struct printbuf *out, const struct dentry *d,const char *fmt) { -const char *array[4 ], *s; +const char *array[4 ];const struct dentry *p; -int depth; -int i, n; +int i, depth;switch (fmt[1 ]) {case '2' :case '3' :case '4' :
@@ -895,7 +937,7 @@ void dentry_name(struct printbuf *out,rcu_read_lock ();for (i =0 ; i < depth; i++, d = p) { -if (check_pointer (out, d, spec)) { +if (check_pointer (out, d)) {rcu_read_unlock ();return ; }
@@ -909,56 +951,46 @@ void dentry_name(struct printbuf *out,break ; } } - s = array[--i]; -for (n =0 ; n != spec.precision; n++) { -char c = *s++; -if (!c) { -if (!i) -break ; - c ='/' ; - s = array[--i]; - } -prt_char (out, c); +while (1 ) { +prt_str (out, array[--i]); +if (!i) +break ; +prt_char (out,'/' ); }rcu_read_unlock (); - -widen_string (out, n, spec); }static noinline_for_stack -void file_dentry_name(struct printbuf *out, -const struct file *f, -struct printf_spec spec,const char *fmt) +void file_dentry_name(struct printbuf *out, const struct file *f, +const char *fmt) { -if (check_pointer (out, f, spec)) +if (check_pointer (out, f))return ; -return dentry_name (out, f->f_path.dentry, spec, fmt); +return dentry_name (out, f->f_path.dentry, fmt); }#ifdef CONFIG_BLOCK static noinline_for_stack -void bdev_name(struct printbuf *out, -struct block_device *bdev, -struct printf_spec spec,const char *fmt) +void bdev_name(struct printbuf *out, struct block_device *bdev) {struct gendisk *hd; -if (check_pointer (out, bdev, spec)) +if (check_pointer (out, bdev))return ; hd = bdev->bd_disk; -string (out, hd->disk_name, spec); +string (out, hd->disk_name);if (bdev->bd_partno) {if (isdigit (hd->disk_name[strlen (hd->disk_name)-1 ]))prt_char (out,'p' ); -number (out, bdev->bd_partno, spec); +prt_u64 (out, bdev->bd_partno); } }#endif static noinline_for_stack void symbol_string(struct printbuf *out, void *ptr, -struct printf_spec spec,const char *fmt) +const char *fmt) {unsigned long value;#ifdef CONFIG_KALLSYMS
@@ -981,17 +1013,12 @@ void symbol_string(struct printbuf *out, void *ptr, elsesprint_symbol_no_offset (sym ,value ); -string_nocheck (out, sym, spec); +prt_str (out, sym);#else special_hex_number (out, value,sizeof (void *));#endif } -static const struct printf_spec default_str_spec = { - .field_width =-1 , - .precision =-1 , -}; -static const struct printf_spec default_flag_spec = { .base =16 , .precision =-1 ,
@@ -1050,7 +1077,7 @@ void resource_string(struct printbuf *out, struct resource *res,int decode = (fmt[0 ] =='R' ) ?1 :0 ;const struct printf_spec *specp; -if (check_pointer (out, res, spec)) +if (check_pointer_spec (out, res, spec))return ;prt_char (&sym,'[' );
@@ -1114,7 +1141,7 @@ void hex_string(struct printbuf *out, u8 *addr,/* nothing to print */ return ; -if (check_pointer (out, addr, spec)) +if (check_pointer_spec (out, addr, spec))return ;switch (fmt[1 ]) {
@@ -1155,7 +1182,7 @@ void bitmap_string(struct printbuf *out, unsigned long *bitmap,int i, chunksz;bool first = true; -if (check_pointer (out, bitmap, spec)) +if (check_pointer_spec (out, bitmap, spec))return ;/* reused to print numbers */
@@ -1194,7 +1221,7 @@ void bitmap_list_string(struct printbuf *out, unsigned long *bitmap,bool first = true;int rbot, rtop; -if (check_pointer (out, bitmap, spec)) +if (check_pointer_spec (out, bitmap, spec))return ;for_each_set_bitrange (rbot, rtop, bitmap, nr_bits) {
@@ -1221,7 +1248,7 @@ void mac_address_string(struct printbuf *out, u8 *addr,char separator;bool reversed = false; -if (check_pointer (out, addr, spec)) +if (check_pointer_spec (out, addr, spec))return ;switch (fmt[1 ]) {
@@ -1522,7 +1549,7 @@ void ip_addr_string(struct printbuf *out, const void *ptr, {char *err_fmt_msg; -if (check_pointer (out, ptr, spec)) +if (check_pointer_spec (out, ptr, spec))return ;switch (fmt[1 ]) {
@@ -1543,12 +1570,12 @@ void ip_addr_string(struct printbuf *out, const void *ptr,case AF_INET6 :return ip6_addr_string_sa (out, &sa->v6, spec, fmt);default : -return error_string (out,"(einval)" , spec); +return error_string_spec (out,"(einval)" , spec); }} } err_fmt_msg = fmt[0 ] =='i' ?"(%pi?)" :"(%pI?)" ; -return error_string (out, err_fmt_msg, spec); +return error_string_spec (out, err_fmt_msg, spec); } static noinline_for_stack
@@ -1563,7 +1590,7 @@ void escaped_string(struct printbuf *out, u8 *addr,if (spec.field_width ==0 )return ;/* nothing to print */ -if (check_pointer (out, addr, spec)) +if (check_pointer_spec (out, addr, spec))return ; do {
@@ -1608,7 +1635,7 @@ static void va_format(struct printbuf *out, {va_list va; -if (check_pointer (out, va_fmt, spec)) +if (check_pointer_spec (out, va_fmt, spec))return ;va_copy (va, *va_fmt->va);
@@ -1617,16 +1644,13 @@ static void va_format(struct printbuf *out, }static noinline_for_stack -void uuid_string(struct printbuf *out, const u8 *addr, -struct printf_spec spec,const char *fmt) +void uuid_string(struct printbuf *out, const u8 *addr, const char *fmt) { -char uuid_buf[UUID_STRING_LEN +1 ]; -struct printbuf uuid =PRINTBUF_EXTERN (uuid_buf,sizeof (uuid_buf));int i;const u8 *index = uuid_index;bool uc = false; -if (check_pointer (out, addr, spec)) +if (check_pointer (out, addr))return ;switch (*(++fmt)) {
@@ -1643,30 +1667,28 @@ void uuid_string(struct printbuf *out, const u8 *addr,for (i =0 ; i <16 ; i++) {if (uc) -prt_hex_byte_upper (&uuid, addr[index[i]]); +prt_hex_byte_upper (out, addr[index[i]]);else -prt_hex_byte (&uuid, addr[index[i]]); +prt_hex_byte (out, addr[index[i]]);switch (i) {case 3 :case 5 :case 7 :case 9 : -prt_char (&uuid,'-' ); +prt_char (out,'-' );break ; } } - -string_nocheck (out, uuid_buf, spec); }static noinline_for_stack void netdev_bits(struct printbuf *out, const void *addr, -struct printf_spec spec,const char *fmt) +const char *fmt) {unsigned long long num;int size; -if (check_pointer (out, addr, spec)) +if (check_pointer (out, addr))return ;switch (fmt[1 ]) {
@@ -1676,7 +1698,7 @@ void netdev_bits(struct printbuf *out, const void *addr,special_hex_number (out, num, size);break ;default : -error_string (out,"(%pN?)" , spec); +error_string (out,"(%pN?)" );break ; } }
@@ -1691,9 +1713,9 @@ void fourcc_string(struct printbuf *out, const u32 *fourcc,u32 orig, val;if (fmt[1 ] !='c' || fmt[2 ] !='c' ) -return error_string (out,"(%p4?)" , spec); +return error_string_spec (out,"(%p4?)" , spec); -if (check_pointer (out, fourcc, spec)) +if (check_pointer_spec (out, fourcc, spec))return ; orig =get_unaligned (fourcc);
@@ -1714,17 +1736,17 @@ void fourcc_string(struct printbuf *out, const u32 *fourcc,special_hex_number (&output, orig,sizeof (u32));prt_char (&output,')' ); -string (out, output_buf, spec); +string_spec (out, output_buf, spec); }static noinline_for_stack void address_val(struct printbuf *out, const void *addr, -struct printf_spec spec,const char *fmt) +const char *fmt) {unsigned long long num;int size; -if (check_pointer (out, addr, spec)) +if (check_pointer (out, addr))return ;switch (fmt[1 ]) {
@@ -1775,7 +1797,7 @@ void rtc_str(struct printbuf *out, const struct rtc_time *tm,bool found = true;int count =2 ; -if (check_pointer (out, tm, spec)) +if (check_pointer_spec (out, tm, spec))return ;switch (fmt[count]) {
@@ -1845,7 +1867,7 @@ void time_and_date(struct printbuf *out,case 'T' :return time64_str (out, *(const time64_t *)ptr, spec, fmt);default : -return error_string (out,"(%pt?)" , spec); +return error_string_spec (out,"(%pt?)" , spec); } }
@@ -1854,16 +1876,16 @@ void clock(struct printbuf *out, struct clk *clk, struct printf_spec spec, const char *fmt) {if (!IS_ENABLED (CONFIG_HAVE_CLK )) -return error_string (out,"(%pC?)" , spec); +return error_string_spec (out,"(%pC?)" , spec); -if (check_pointer (out, clk, spec)) +if (check_pointer_spec (out, clk, spec))return ;switch (fmt[1 ]) {case 'n' :default :#ifdef CONFIG_COMMON_CLK -return string (out,__clk_get_name (clk), spec); +return string_spec (out,__clk_get_name (clk), spec);#else return ptr_to_id (out, clk, spec);#endif
@@ -1881,7 +1903,7 @@ void format_flags(struct printbuf *out, unsigned long flags,if ((flags & mask) != mask)continue ; -string (out, names->name, default_str_spec); +string (out, names->name); flags &= ~mask;if (flags)
@@ -1939,7 +1961,7 @@ void format_page_flags(struct printbuf *out, unsigned long flags)if (append)prt_char (out,'|' ); -string (out, pff[i].name, default_str_spec); +string (out, pff[i].name);prt_char (out,'=' );number (out, (flags >> pff[i].shift) & pff[i].mask, *pff[i].spec);
@@ -1955,7 +1977,7 @@ void flags_string(struct printbuf *out, void *flags_ptr,unsigned long flags;const struct trace_print_flags *names; -if (check_pointer (out, flags_ptr, spec)) +if (check_pointer_spec (out, flags_ptr, spec))return ;switch (fmt[1 ]) {
@@ -1970,7 +1992,7 @@ void flags_string(struct printbuf *out, void *flags_ptr, names = gfpflag_names;break ;default : -return error_string (out,"(%pG?)" , spec); +return error_string_spec (out,"(%pG?)" , spec); }return format_flags (out, flags, names);
@@ -1987,10 +2009,8 @@ void fwnode_full_name_string(struct printbuf *out,struct fwnode_handle *__fwnode =fwnode_get_nth_parent (fwnode, depth); -string (out,fwnode_get_name_prefix (__fwnode), - default_str_spec); -string (out,fwnode_get_name (__fwnode), - default_str_spec); +string (out,fwnode_get_name_prefix (__fwnode)); +string (out,fwnode_get_name (__fwnode));fwnode_handle_put (__fwnode); }
@@ -2011,12 +2031,12 @@ void device_node_string(struct printbuf *out, struct device_node *dn, str_spec.field_width =-1 ;if (fmt[0 ] !='F' ) -return error_string (out,"(%pO?)" , spec); +return error_string_spec (out,"(%pO?)" , spec);if (!IS_ENABLED (CONFIG_OF )) -return error_string (out,"(%pOF?)" , spec); +return error_string_spec (out,"(%pOF?)" , spec); -if (check_pointer (out, dn, spec)) +if (check_pointer_spec (out, dn, spec))return ;/* simple case without anything any more format specifiers */
@@ -2037,7 +2057,7 @@ void device_node_string(struct printbuf *out, struct device_node *dn, p =fwnode_get_name (of_fwnode_handle (dn)); precision = str_spec.precision; str_spec.precision =strchrnul (p,'@' ) - p; -string (out, p, str_spec); +string_spec (out, p, str_spec); str_spec.precision = precision;break ;case 'p' :/* phandle */
@@ -2047,7 +2067,7 @@ void device_node_string(struct printbuf *out, struct device_node *dn, p =fwnode_get_name (of_fwnode_handle (dn));if (!p[1 ]) p ="/" ; -string (out, p, str_spec); +string_spec (out, p, str_spec);break ;case 'F' :/* flags */ tbuf[0 ] =of_node_check_flag (dn,OF_DYNAMIC ) ?'D' :'-' ;
@@ -2057,18 +2077,18 @@ void device_node_string(struct printbuf *out, struct device_node *dn, tbuf[4 ] =0 ;string_nocheck (out, tbuf, str_spec);break ; -case 'c' :/* major compatible string */ +case 'c' :/* major compatible string_spec */ ret =of_property_read_string (dn,"compatible" , &p);if (!ret) -string (out, p, str_spec); +string_spec (out, p, str_spec);break ; -case 'C' :/* full compatible string */ +case 'C' :/* full compatible string_spec */ has_mult = false;of_property_for_each_string (dn,"compatible" , prop, p) {if (has_mult)string_nocheck (out,"," , str_spec);string_nocheck (out,"\"" , str_spec); -string (out, p, str_spec); +string_spec (out, p, str_spec);string_nocheck (out,"\"" , str_spec); has_mult = true;
@@ -2093,16 +2113,16 @@ void fwnode_string(struct printbuf *out, str_spec.field_width =-1 ;if (*fmt !='w' ) -return error_string (out,"(%pf?)" , spec); +return error_string_spec (out,"(%pf?)" , spec); -if (check_pointer (out, fwnode, spec)) +if (check_pointer_spec (out, fwnode, spec))return ; fmt++;switch (*fmt) {case 'P' :/* name */ -string (out,fwnode_get_name (fwnode), str_spec); +string_spec (out,fwnode_get_name (fwnode), str_spec);break ;case 'f' :/* full_name */ default :
@@ -2269,13 +2289,16 @@ static noinline_for_stackvoid pointer (struct printbuf *out,const char *fmt,void *ptr,struct printf_spec spec) { +unsigned prev_pos = out->pos; +switch (*fmt) {case 'S' :case 's' : ptr =dereference_symbol_descriptor (ptr); fallthrough;case 'B' : -return symbol_string (out, ptr, spec, fmt); +symbol_string (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'R' :case 'r' :return resource_string (out, ptr, spec, fmt);
@@ -2306,28 +2329,34 @@ void pointer(struct printbuf *out, const char *fmt,case 'E' :return escaped_string (out, ptr, spec, fmt);case 'U' : -return uuid_string (out, ptr, spec, fmt); +uuid_string (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'V' :return va_format (out, ptr, spec, fmt);case 'K' :return restricted_pointer (out, ptr, spec);case 'N' : -return netdev_bits (out, ptr, spec, fmt); +netdev_bits (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case '4' :return fourcc_string (out, ptr, spec, fmt);case 'a' : -return address_val (out, ptr, spec, fmt); +address_val (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'd' : -return dentry_name (out, ptr, spec, fmt); +dentry_name (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 't' :return time_and_date (out, ptr, spec, fmt);case 'C' :return clock (out, ptr, spec, fmt);case 'D' : -return file_dentry_name (out, ptr, spec, fmt); +file_dentry_name (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);#ifdef CONFIG_BLOCK case 'g' : -return bdev_name (out, ptr, spec, fmt); +bdev_name (out, ptr); +return do_width_precision (out, prev_pos, spec);#endif case 'G' :
@@ -2347,9 +2376,9 @@ void pointer(struct printbuf *out, const char *fmt,case 'k' :switch (fmt[1 ]) {case 's' : -return string (out, ptr, spec); +return string_spec (out, ptr, spec);default : -return error_string (out,"(einval)" , spec); +return error_string_spec (out,"(einval)" , spec); }default :return default_pointer (out, ptr, spec);
@@ -2627,7 +2656,12 @@ void prt_vprintf(struct printbuf *out, const char *fmt, va_list args)break ;case FORMAT_TYPE_STR : - string(out, va_arg(args, char *), spec); +/* +* we can't use string() then do_width_precision +* afterwards: people use the field width for passing +* non nul terminated strings +*/ + string_spec(out, va_arg(args, char *), spec);break ;case FORMAT_TYPE_PTR :
@@ -3096,7 +3130,7 @@ void prt_bstrprintf(struct printbuf *out, const char *fmt, const u32 *bin_buf)case FORMAT_TYPE_STR : {const char *str_arg = args; args +=strlen (str_arg) +1 ; -string (out, (char *)str_arg, spec); +string_spec (out, (char *)str_arg, spec);break ; }
Signature · 1 line
2.35.1
PATCH v5 13/32 vsprintf: Refactor resource_string()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
Two changes:
- We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes resource_string to not take printf_spec
- With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it.
--- lib/vsprintf.c | 51 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 27 deletions(-)
lib/vsprintf.c +24 -27
@@ -1032,7 +1032,7 @@ static const struct printf_spec default_dec_spec = {static noinline_for_stack void resource_string(struct printbuf *out, struct resource *res, -struct printf_spec spec,const char *fmt) +int decode) {#ifndef IO_RSRC_PRINTK_SIZE #define IO_RSRC_PRINTK_SIZE 6
@@ -1071,62 +1071,58 @@ void resource_string(struct printbuf *out, struct resource *res,#define FLAG_BUF_SIZE (2 * sizeof(res->flags))#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")#define RAW_BUF_SIZE sizeof("[mem - flags 0x]") -char sym_buf[max (2 *RSRC_BUF_SIZE +DECODED_BUF_SIZE , -2 *RSRC_BUF_SIZE +FLAG_BUF_SIZE +RAW_BUF_SIZE )]; -struct printbuf sym =PRINTBUF_EXTERN (sym_buf,sizeof (sym_buf)); -int decode = (fmt[0 ] =='R' ) ?1 :0 ;const struct printf_spec *specp; -if (check_pointer_spec (out, res, spec)) +if (check_pointer (out, res))return ; -prt_char (&sym,'[' ); +prt_char (out,'[' );if (res->flags &IORESOURCE_IO ) { -string_nocheck (&sym,"io " , str_spec); +string_nocheck (out,"io " , str_spec); specp = &io_spec; }else if (res->flags &IORESOURCE_MEM ) { -string_nocheck (&sym,"mem " , str_spec); +string_nocheck (out,"mem " , str_spec); specp = &mem_spec; }else if (res->flags &IORESOURCE_IRQ ) { -string_nocheck (&sym,"irq " , str_spec); +string_nocheck (out,"irq " , str_spec); specp = &default_dec_spec; }else if (res->flags &IORESOURCE_DMA ) { -string_nocheck (&sym,"dma " , str_spec); +string_nocheck (out,"dma " , str_spec); specp = &default_dec_spec; }else if (res->flags &IORESOURCE_BUS ) { -string_nocheck (&sym,"bus " , str_spec); +string_nocheck (out,"bus " , str_spec); specp = &bus_spec; }else { -string_nocheck (&sym,"??? " , str_spec); +string_nocheck (out,"??? " , str_spec); specp = &mem_spec; decode =0 ; }if (decode && res->flags &IORESOURCE_UNSET ) { -string_nocheck (&sym,"size " , str_spec); -number (&sym,resource_size (res), *specp); +string_nocheck (out,"size " , str_spec); +number (out,resource_size (res), *specp); }else { -number (&sym, res->start, *specp); +number (out, res->start, *specp);if (res->start != res->end) { -prt_char (&sym,'-' ); -number (&sym, res->end, *specp); +prt_char (out,'-' ); +number (out, res->end, *specp); } }if (decode) {if (res->flags &IORESOURCE_MEM_64 ) -string_nocheck (&sym," 64bit" , str_spec); +string_nocheck (out," 64bit" , str_spec);if (res->flags &IORESOURCE_PREFETCH ) -string_nocheck (&sym," pref" , str_spec); +string_nocheck (out," pref" , str_spec);if (res->flags &IORESOURCE_WINDOW ) -string_nocheck (&sym," window" , str_spec); +string_nocheck (out," window" , str_spec);if (res->flags &IORESOURCE_DISABLED ) -string_nocheck (&sym," disabled" , str_spec); +string_nocheck (out," disabled" , str_spec); }else { -string_nocheck (&sym," flags " , str_spec); -number (&sym, res->flags, default_flag_spec); +string_nocheck (out," flags " , str_spec); +number (out, res->flags, default_flag_spec); } -prt_char (&sym,']' ); +prt_char (out,']' ); -string_nocheck (out, sym_buf, spec); +printbuf_nul_terminate (out); } static noinline_for_stack
@@ -2301,7 +2297,8 @@ void pointer(struct printbuf *out, const char *fmt,return do_width_precision (out, prev_pos, spec);case 'R' :case 'r' : -return resource_string (out, ptr, spec, fmt); +resource_string (out, ptr, fmt[0 ] =='R' ); +return do_width_precision (out, prev_pos, spec);case 'h' :return hex_string (out, ptr, spec, fmt);case 'b' :
Signature · 1 line
2.35.1
PATCH v5 14/32 vsprintf: Refactor fourcc_string()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
- We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes fourcc_string() to not take printf_spec
- With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it.
--- lib/vsprintf.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
lib/vsprintf.c +12 -15
@@ -1701,17 +1701,15 @@ void netdev_bits(struct printbuf *out, const void *addr,static noinline_for_stack void fourcc_string(struct printbuf *out, const u32 *fourcc, -struct printf_spec spec,const char *fmt) +const char *fmt) { -char output_buf[sizeof ("0123 little-endian (0x01234567)" )]; -struct printbuf output =PRINTBUF_EXTERN (output_buf,sizeof (output_buf));unsigned int i;u32 orig, val;if (fmt[1 ] !='c' || fmt[2 ] !='c' ) -return error_string_spec (out,"(%p4?)" , spec); +return error_string (out,"(%p4?)" ); -if (check_pointer_spec (out, fourcc, spec)) +if (check_pointer (out, fourcc))return ; orig =get_unaligned (fourcc);
@@ -1721,18 +1719,16 @@ void fourcc_string(struct printbuf *out, const u32 *fourcc,unsigned char c = val >> (i *8 );/* Print non-control ASCII characters as-is, dot otherwise */ -prt_char (&output,isascii (c) &&isprint (c) ? c :'.' ); +prt_char (out,isascii (c) &&isprint (c) ? c :'.' ); } -prt_char (&output,' ' ); -prt_str (&output, orig &BIT (31 ) ?"big-endian" :"little-endian" ); - -prt_char (&output,' ' ); -prt_char (&output,'(' ); -special_hex_number (&output, orig,sizeof (u32)); -prt_char (&output,')' ); +prt_char (out,' ' ); +prt_str (out, orig &BIT (31 ) ?"big-endian" :"little-endian" ); -string_spec (out, output_buf, spec); +prt_char (out,' ' ); +prt_char (out,'(' ); +special_hex_number (out, orig,sizeof (u32)); +prt_char (out,')' ); } static noinline_for_stack
@@ -2336,7 +2332,8 @@ void pointer(struct printbuf *out, const char *fmt,netdev_bits (out, ptr, fmt);return do_width_precision (out, prev_pos, spec);case '4' : -return fourcc_string (out, ptr, spec, fmt); +fourcc_string (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'a' :address_val (out, ptr, fmt);return do_width_precision (out, prev_pos, spec);
Signature · 1 line
2.35.1
PATCH v5 15/32 vsprintf: Refactor ip_addr_string()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
- We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes ip_addr_string() to not take printf_spec
- With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it.
--- lib/vsprintf.c | 114 ++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 77 deletions(-)
lib/vsprintf.c +37 -77
@@ -1276,13 +1276,13 @@ void mac_address_string(struct printbuf *out, u8 *addr, }static noinline_for_stack -void ip4_string(struct printbuf *out, -const u8 *addr,const char *fmt) +void ip4_string(struct printbuf *out, const u8 *addr, const char *fmt) { -int i; -bool leading_zeros = (fmt[0 ] =='i' ); -int index; -int step; +struct printf_spec spec = default_dec_spec; +int i, index, step; + +if (fmt[0 ] =='i' ) + spec.precision =3 ;switch (fmt[2 ]) {case 'h' :
@@ -1306,19 +1306,9 @@ void ip4_string(struct printbuf *out,break ; }for (i =0 ; i <4 ; i++) { -char temp[4 ]__aligned(2) ;/* hold each IP quad in reverse order */ -int digits =put_dec_trunc8 (temp, addr[index]) - temp; -if (leading_zeros) { -if (digits <3 ) -prt_char (out,'0' ); -if (digits <2 ) -prt_char (out,'0' ); - } -/* reverse the digits in the quad */ -while (digits--) -prt_char (out, temp[digits]); -if (i <3 ) +if (i)prt_char (out,'.' ); +number (out, addr[index], spec); index += step; } }
@@ -1401,8 +1391,6 @@ void ip6_compressed_string(struct printbuf *out, const char *addr)__prt_char (out,':' );ip4_string (out, &in6.s6_addr[12 ],"I4" ); } - -printbuf_nul_terminate (out); } static noinline_for_stack
@@ -1420,41 +1408,20 @@ void ip6_string(struct printbuf *out, const char *addr, const char *fmt)static noinline_for_stack void ip6_addr_string(struct printbuf *out, const u8 *addr, -struct printf_spec spec,const char *fmt) +const char *fmt) { -char ip6_addr_buf[sizeof ("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255" )]; -struct printbuf ip6_addr =PRINTBUF_EXTERN (ip6_addr_buf,sizeof (ip6_addr_buf)); -if (fmt[0 ] =='I' && fmt[2 ] =='c' ) -ip6_compressed_string (&ip6_addr, addr); +ip6_compressed_string (out, addr);else -ip6_string (&ip6_addr, addr, fmt); - -string_nocheck (out, ip6_addr_buf, spec); -} - -static noinline_for_stack -void ip4_addr_string(struct printbuf *out, const u8 *addr, -struct printf_spec spec,const char *fmt) -{ -char ip4_addr_buf[sizeof ("255.255.255.255" )]; -struct printbuf ip4_addr =PRINTBUF_EXTERN (ip4_addr_buf,sizeof (ip4_addr_buf)); - -ip4_string (&ip4_addr, addr, fmt); - -string_nocheck (out, ip4_addr_buf, spec); +ip6_string (out, addr, fmt); }static noinline_for_stack void ip6_addr_string_sa(struct printbuf *out,const struct sockaddr_in6 *sa, -struct printf_spec spec,const char *fmt) +const char *fmt) {bool have_p = false, have_s = false, have_f = false, have_c = false; -char ip6_addr_buf[sizeof ("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]" ) + -sizeof (":12345" ) +sizeof ("/123456789" ) + -sizeof ("%1234567890" )]; -struct printbuf ip6_addr =PRINTBUF_EXTERN (ip6_addr_buf,sizeof (ip6_addr_buf));const u8 *addr = (const u8 *) &sa->sin6_addr;char fmt6[2 ] = { fmt[0 ],'6' };
@@ -1477,41 +1444,35 @@ void ip6_addr_string_sa(struct printbuf *out, }if (have_p || have_s || have_f) -prt_char (&ip6_addr,'[' ); +prt_char (out,'[' );if (fmt6[0 ] =='I' && have_c) -ip6_compressed_string (&ip6_addr, addr); +ip6_compressed_string (out, addr);else -ip6_string (&ip6_addr, addr, fmt6); +ip6_string (out, addr, fmt6);if (have_p || have_s || have_f) -prt_char (&ip6_addr,']' ); +prt_char (out,']' );if (have_p) { -prt_char (&ip6_addr,':' ); -number (&ip6_addr,ntohs (sa->sin6_port), spec); +prt_char (out,':' ); +prt_u64 (out,ntohs (sa->sin6_port)); }if (have_f) { -prt_char (&ip6_addr,'/' ); -number (&ip6_addr,ntohl (sa->sin6_flowinfo & -IPV6_FLOWINFO_MASK ), spec); +prt_char (out,'/' ); +prt_u64 (out,ntohl (sa->sin6_flowinfo &IPV6_FLOWINFO_MASK )); }if (have_s) { -prt_char (&ip6_addr,'%' ); -number (&ip6_addr, sa->sin6_scope_id, spec); +prt_char (out,'%' ); +prt_u64 (out, sa->sin6_scope_id); } - -string_nocheck (out, ip6_addr_buf, spec); }static noinline_for_stack -void ip4_addr_string_sa(struct printbuf *out, -const struct sockaddr_in *sa, -struct printf_spec spec,const char *fmt) +void ip4_addr_string_sa(struct printbuf *out, const struct sockaddr_in *sa, +const char *fmt) {bool have_p = false; -char ip4_addr_buf[sizeof ("255.255.255.255" ) +sizeof (":12345" )]; -struct printbuf ip4_addr =PRINTBUF_EXTERN (ip4_addr_buf,sizeof (ip4_addr_buf));const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;char fmt4[3 ] = { fmt[0 ],'4' ,0 };
@@ -1530,29 +1491,27 @@ void ip4_addr_string_sa(struct printbuf *out, } } -ip4_string (&ip4_addr, addr, fmt4); +ip4_string (out, addr, fmt4);if (have_p) { -prt_char (&ip4_addr,':' ); -number (&ip4_addr,ntohs (sa->sin_port), spec); +prt_char (out,':' ); +prt_u64 (out,ntohs (sa->sin_port)); } - -string_nocheck (out, ip4_addr_buf, spec); }static noinline_for_stack void ip_addr_string(struct printbuf *out, const void *ptr, -struct printf_spec spec,const char *fmt) +const char *fmt) {char *err_fmt_msg; -if (check_pointer_spec (out, ptr, spec)) +if (check_pointer (out, ptr))return ;switch (fmt[1 ]) {case '6' : -return ip6_addr_string (out, ptr, spec, fmt); +return ip6_addr_string (out, ptr, fmt);case '4' : -return ip4_addr_string (out, ptr, spec, fmt); +return ip4_string (out, ptr, fmt);case 'S' : { const union { struct sockaddr raw;
@@ -1562,16 +1521,16 @@ void ip_addr_string(struct printbuf *out, const void *ptr,switch (sa->raw.sa_family) {case AF_INET : -return ip4_addr_string_sa (out, &sa->v4, spec, fmt); +return ip4_addr_string_sa (out, &sa->v4, fmt);case AF_INET6 : -return ip6_addr_string_sa (out, &sa->v6, spec, fmt); +return ip6_addr_string_sa (out, &sa->v6, fmt);default : -return error_string_spec (out,"(einval)" , spec); +return error_string (out,"(einval)" ); }} } err_fmt_msg = fmt[0 ] =='i' ?"(%pi?)" :"(%pI?)" ; -return error_string_spec (out, err_fmt_msg, spec); +error_string (out, err_fmt_msg); } static noinline_for_stack
@@ -2318,7 +2277,8 @@ void pointer(struct printbuf *out, const char *fmt,* 4: 001.002.003.004 * 6: 000102...0f */ -return ip_addr_string (out, ptr, spec, fmt); +ip_addr_string (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'E' :return escaped_string (out, ptr, spec, fmt);case 'U' :
Signature · 1 line
2.35.1
PATCH v5 16/32 vsprintf: Refactor mac_address_string()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
- We're attempting to consolidate printf_spec and format string handling in the top level ptr_vprintf(), this changesmac_address_string() to not take printf_spec
- With the new printbuf helpers there's no need to use a separate stack allocated buffer, so this patch deletes it.
--- lib/vsprintf.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
lib/vsprintf.c +7 -11
@@ -1236,15 +1236,13 @@ void bitmap_list_string(struct printbuf *out, unsigned long *bitmap,static noinline_for_stack void mac_address_string(struct printbuf *out, u8 *addr, -struct printf_spec spec,const char *fmt) +const char *fmt) { -char mac_addr[sizeof ("xx:xx:xx:xx:xx:xx" )]; -char *p = mac_addr;int i;char separator;bool reversed = false; -if (check_pointer_spec (out, addr, spec)) +if (check_pointer (out, addr))return ;switch (fmt[1 ]) {
@@ -1263,16 +1261,13 @@ void mac_address_string(struct printbuf *out, u8 *addr,for (i =0 ; i <6 ; i++) {if (reversed) - p =hex_byte_pack (p, addr[5 - i]); +prt_hex_byte (out, addr[5 - i]);else - p =hex_byte_pack (p, addr[i]); +prt_hex_byte (out, addr[i]);if (fmt[0 ] =='M' && i !=5 ) - *p++ = separator; +prt_char (out, separator); } - *p ='\0' ; - -string_nocheck (out, mac_addr, spec); } static noinline_for_stack
@@ -2267,7 +2262,8 @@ void pointer(struct printbuf *out, const char *fmt,case 'm' :/* Contiguous: 000102030405 */ /* [mM]F (FDDI) */ /* [mM]R (Reverse order; Bluetooth) */ -return mac_address_string (out, ptr, spec, fmt); +mac_address_string (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'I' :/* Formatted IP supported * 4: 1.2.3.4 * 6: 0001:0203:...:0708
Signature · 1 line
2.35.1
PATCH v5 17/32 vsprintf: time_and_date() no longer takes printf_spec
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes time_and_date() to not take printf_spec.
--- lib/vsprintf.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
lib/vsprintf.c +10 -10
@@ -1736,14 +1736,14 @@ void time_str(struct printbuf *out, const struct rtc_time *tm, bool r)static noinline_for_stack void rtc_str(struct printbuf *out, const struct rtc_time *tm, -struct printf_spec spec,const char *fmt) +const char *fmt) {bool have_t = true, have_d = true;bool raw = false, iso8601_separator = true;bool found = true;int count =2 ; -if (check_pointer_spec (out, tm, spec)) +if (check_pointer (out, tm))return ;switch (fmt[count]) {
@@ -1781,7 +1781,7 @@ void rtc_str(struct printbuf *out, const struct rtc_time *tm,static noinline_for_stack void time64_str(struct printbuf *out, const time64_t time, -struct printf_spec spec,const char *fmt) +const char *fmt) {struct rtc_time rtc_time;struct tm tm;
@@ -1799,21 +1799,20 @@ void time64_str(struct printbuf *out, const time64_t time, rtc_time.tm_isdst =0 ; -rtc_str (out, &rtc_time, spec, fmt); +rtc_str (out, &rtc_time, fmt); }static noinline_for_stack -void time_and_date(struct printbuf *out, -void *ptr,struct printf_spec spec, +void time_and_date(struct printbuf *out, void *ptr,const char *fmt) {switch (fmt[1 ]) {case 'R' : -return rtc_str (out, (const struct rtc_time *)ptr, spec, fmt); +return rtc_str (out, (const struct rtc_time *)ptr, fmt);case 'T' : -return time64_str (out, *(const time64_t *)ptr, spec, fmt); +return time64_str (out, *(const time64_t *)ptr, fmt);default : -return error_string_spec (out,"(%pt?)" , spec); +return error_string (out,"(%pt?)" ); } }
@@ -2297,7 +2296,8 @@ void pointer(struct printbuf *out, const char *fmt,dentry_name (out, ptr, fmt);return do_width_precision (out, prev_pos, spec);case 't' : -return time_and_date (out, ptr, spec, fmt); +time_and_date (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'C' :return clock (out, ptr, spec, fmt);case 'D' :
Signature · 1 line
2.35.1
PATCH v5 18/32 vsprintf: flags_string() no longer takes printf_spec
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
We're attempting to consolidate printf_spec and format string handling in the top level vpr_buf(), this changes time_and_date() to not take printf_spec.
--- lib/vsprintf.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
lib/vsprintf.c +5 -4
@@ -1917,12 +1917,12 @@ void format_page_flags(struct printbuf *out, unsigned long flags)static noinline_for_stack void flags_string(struct printbuf *out, void *flags_ptr, -struct printf_spec spec,const char *fmt) +const char *fmt) {unsigned long flags;const struct trace_print_flags *names; -if (check_pointer_spec (out, flags_ptr, spec)) +if (check_pointer (out, flags_ptr))return ;switch (fmt[1 ]) {
@@ -1937,7 +1937,7 @@ void flags_string(struct printbuf *out, void *flags_ptr, names = gfpflag_names;break ;default : -return error_string_spec (out,"(%pG?)" , spec); +return error_string (out,"(%pG?)" ); }return format_flags (out, flags, names);
@@ -2310,7 +2310,8 @@ void pointer(struct printbuf *out, const char *fmt,#endif case 'G' : -return flags_string (out, ptr, spec, fmt); +flags_string (out, ptr, fmt); +return do_width_precision (out, prev_pos, spec);case 'O' :return device_node_string (out, ptr, spec, fmt +1 );case 'f' :
Signature · 1 line
2.35.1
PATCH v5 19/32 vsprintf: Refactor device_node_string, fwnode_string
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
- eliminate on-stack buffer in device_node_string
- eliminate unnecessary uses of printf_spec, lift format string precision/field width to pointer()
--- lib/vsprintf.c | 73 ++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 44 deletions(-)
lib/vsprintf.c +29 -44
@@ -1963,25 +1963,20 @@ void fwnode_full_name_string(struct printbuf *out,static noinline_for_stack void device_node_string(struct printbuf *out, struct device_node *dn, -struct printf_spec spec,const char *fmt) +const char *fmt) { -char tbuf[sizeof ("xxxx" ) +1 ];const char *p;int ret; -unsigned start = out->pos;struct property *prop;bool has_mult, pass; -struct printf_spec str_spec = spec; - str_spec.field_width =-1 ; -if (fmt[0 ] !='F' ) -return error_string_spec (out,"(%pO?)" , spec); +return error_string (out,"(%pO?)" );if (!IS_ENABLED (CONFIG_OF )) -return error_string_spec (out,"(%pOF?)" , spec); +return error_string (out,"(%pOF?)" ); -if (check_pointer_spec (out, dn, spec)) +if (check_pointer (out, dn))return ;/* simple case without anything any more format specifiers */
@@ -1990,7 +1985,6 @@ void device_node_string(struct printbuf *out, struct device_node *dn, fmt ="f" ;for (pass = false;strspn (fmt,"fnpPFcC" ); fmt++, pass = true) { -int precision;if (pass)prt_char (out,':' );
@@ -1998,43 +1992,41 @@ void device_node_string(struct printbuf *out, struct device_node *dn,case 'f' :/* full_name */ fwnode_full_name_string (out,of_fwnode_handle (dn));break ; -case 'n' :/* name */ - p =fwnode_get_name (of_fwnode_handle (dn)); - precision = str_spec.precision; - str_spec.precision =strchrnul (p,'@' ) - p; -string_spec (out, p, str_spec); - str_spec.precision = precision; +case 'n' : {/* name */ +const char *name =fwnode_get_name (of_fwnode_handle (dn)); +unsigned len =strchrnul (name,'@' ) - name; + +prt_bytes (out, name, len);break ; + }case 'p' :/* phandle */ -prt_u64 (out, (unsigned int )dn->phandle); +prt_u64 (out, dn->phandle);break ;case 'P' :/* path-spec */ p =fwnode_get_name (of_fwnode_handle (dn));if (!p[1 ]) p ="/" ; -string_spec (out, p, str_spec); +string (out, p);break ;case 'F' :/* flags */ - tbuf[0 ] =of_node_check_flag (dn,OF_DYNAMIC ) ?'D' :'-' ; - tbuf[1 ] =of_node_check_flag (dn,OF_DETACHED ) ?'d' :'-' ; - tbuf[2 ] =of_node_check_flag (dn,OF_POPULATED ) ?'P' :'-' ; - tbuf[3 ] =of_node_check_flag (dn,OF_POPULATED_BUS ) ?'B' :'-' ; - tbuf[4 ] =0 ; -string_nocheck (out, tbuf, str_spec); +prt_char (out,of_node_check_flag (dn,OF_DYNAMIC ) ?'D' :'-' ); +prt_char (out,of_node_check_flag (dn,OF_DETACHED ) ?'d' :'-' ); +prt_char (out,of_node_check_flag (dn,OF_POPULATED ) ?'P' :'-' ); +prt_char (out,of_node_check_flag (dn,OF_POPULATED_BUS ) ?'B' :'-' );break ;case 'c' :/* major compatible string_spec */ ret =of_property_read_string (dn,"compatible" , &p);if (!ret) -string_spec (out, p, str_spec); +string (out, p);break ;case 'C' :/* full compatible string_spec */ has_mult = false;of_property_for_each_string (dn,"compatible" , prop, p) {if (has_mult) -string_nocheck (out,"," , str_spec); -string_nocheck (out,"\"" , str_spec); -string_spec (out, p, str_spec); -string_nocheck (out,"\"" , str_spec); +prt_char (out,',' ); +prt_char (out,'\"' ); +string (out, p); +prt_char (out,'\"' ); has_mult = true; }
@@ -2043,39 +2035,30 @@ void device_node_string(struct printbuf *out, struct device_node *dn,break ; } } - -widen_string (out, out->pos - start, spec); }static noinline_for_stack void fwnode_string(struct printbuf *out,struct fwnode_handle *fwnode, -struct printf_spec spec,const char *fmt) +const char *fmt) { -struct printf_spec str_spec = spec; -unsigned start = out->pos; - - str_spec.field_width =-1 ; -if (*fmt !='w' ) -return error_string_spec (out,"(%pf?)" , spec); +return error_string (out,"(%pf?)" ); -if (check_pointer_spec (out, fwnode, spec)) +if (check_pointer (out, fwnode))return ; fmt++;switch (*fmt) {case 'P' :/* name */ -string_spec (out,fwnode_get_name (fwnode), str_spec); +string (out,fwnode_get_name (fwnode));break ;case 'f' :/* full_name */ default :fwnode_full_name_string (out, fwnode);break ; } - -widen_string (out, out->pos - start, spec); }int __init no_hash_pointers_enable (char *str)
@@ -2313,9 +2296,11 @@ void pointer(struct printbuf *out, const char *fmt,flags_string (out, ptr, fmt);return do_width_precision (out, prev_pos, spec);case 'O' : -return device_node_string (out, ptr, spec, fmt +1 ); +device_node_string (out, ptr, fmt +1 ); +return do_width_precision (out, prev_pos, spec);case 'f' : -return fwnode_string (out, ptr, spec, fmt +1 ); +fwnode_string (out, ptr, fmt +1 ); +return do_width_precision (out, prev_pos, spec);case 'x' :return pointer_string (out, ptr, spec);case 'e' :
Signature · 1 line
2.35.1
PATCH v5 20/32 vsprintf: Refactor hex_string, bitmap_string_list, bitmap_string
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This patch cleans up printf_spec handling: these functions only use spec.field_width and they do not interpret it in the normal way - instead it's a number of bits/bytes passed in to print, so these functions are changed to take that parameter directly.
--- lib/vsprintf.c | 60 +++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 32 deletions(-)
lib/vsprintf.c +28 -32
@@ -53,6 +53,7 @@#include <asm/unaligned.h> #include <linux/string_helpers.h> +#include <linux/pretty-printers.h> #include "kstrtox.h" /* Disable pointer hashing if requested */
@@ -1126,18 +1127,23 @@ void resource_string(struct printbuf *out, struct resource *res, }static noinline_for_stack -void hex_string(struct printbuf *out, u8 *addr, -struct printf_spec spec,const char *fmt) +void hex_string(struct printbuf *out, const u8 *addr, +int len,const char *fmt) { -int i, len =1 ;/* if we pass '%ph[CDN]', field width remains -negative value, fallback to the default */ char separator; -if (spec.field_width ==0 ) -/* nothing to print */ +/* nothing to print */ +if (len ==0 )return ; -if (check_pointer_spec (out, addr, spec)) +/* if we pass '%ph[CDN]', field width remains +negative value, fallback to the default */ +if (len <0 ) + len =1 ; + + len =min (len,64 ); + +if (check_pointer (out, addr))return ;switch (fmt[1 ]) {
@@ -1155,34 +1161,21 @@ void hex_string(struct printbuf *out, u8 *addr,break ; } -if (spec.field_width >0 ) - len =min_t (int, spec.field_width,64 ); - -for (i =0 ; i < len; ++i) { -__prt_char (out,hex_asc_hi (addr[i])); -__prt_char (out,hex_asc_lo (addr[i])); - -if (separator && i != len -1 ) -__prt_char (out, separator); - } - -printbuf_nul_terminate (out); +prt_hex_bytes (out, addr, len,1 , separator); }static noinline_for_stack -void bitmap_string(struct printbuf *out, unsigned long *bitmap, -struct printf_spec spec,const char *fmt) +void bitmap_string(struct printbuf *out, unsigned long *bitmap, int nr_bits) { +struct printf_spec spec = { .flags =SMALL |ZEROPAD , .base =16 };const int CHUNKSZ =32 ; -int nr_bits =max_t (int, spec.field_width,0 );int i, chunksz;bool first = true; -if (check_pointer_spec (out, bitmap, spec)) -return ; + nr_bits =max (nr_bits,0 ); -/* reused to print numbers */ - spec = (struct printf_spec ){ .flags =SMALL |ZEROPAD , .base =16 }; +if (check_pointer (out, bitmap)) +return ; chunksz = nr_bits & (CHUNKSZ -1 );if (chunksz ==0 )
@@ -1211,13 +1204,14 @@ void bitmap_string(struct printbuf *out, unsigned long *bitmap,static noinline_for_stack void bitmap_list_string(struct printbuf *out, unsigned long *bitmap, -struct printf_spec spec,const char *fmt) +int nr_bits) { -int nr_bits =max_t (int, spec.field_width,0 );bool first = true;int rbot, rtop; -if (check_pointer_spec (out, bitmap, spec)) + nr_bits =max (nr_bits,0 ); + +if (check_pointer (out, bitmap))return ;for_each_set_bitrange (rbot, rtop, bitmap, nr_bits) {
@@ -2232,13 +2226,15 @@ void pointer(struct printbuf *out, const char *fmt,resource_string (out, ptr, fmt[0 ] =='R' );return do_width_precision (out, prev_pos, spec);case 'h' : -return hex_string (out, ptr, spec, fmt); +/* Uses field_width but _not_ as field size */ +return hex_string (out, ptr, spec.field_width, fmt);case 'b' : +/* Uses field_width but _not_ as field size */ switch (fmt[1 ]) {case 'l' : -return bitmap_list_string (out, ptr, spec, fmt); +return bitmap_list_string (out, ptr, spec.field_width);default : -return bitmap_string (out, ptr, spec, fmt); +return bitmap_string (out, ptr, spec.field_width); }case 'M' :/* Colon separated: 00:01:02:03:04:05 */ case 'm' :/* Contiguous: 000102030405 */
Signature · 1 line
2.35.1
PATCH v5 21/32 Input/joystick/analog: Convert from seq_buf -> printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
seq_buf is being deprecated, this converts to printbuf.
--- drivers/input/joystick/analog.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/input/joystick/analog.c +10 -13
@@ -19,7 +19,7 @@#include <linux/input.h> #include <linux/gameport.h> #include <linux/jiffies.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <linux/timex.h> #include <linux/timekeeping.h>
@@ -339,24 +339,21 @@ static void analog_calibrate_timer(struct analog_port *port)static void analog_name (struct analog *analog) { -struct seq_buf s; +struct printbuf buf =PRINTBUF_EXTERN (analog->name,sizeof (analog->name)); -seq_buf_init (&s, analog->name,sizeof (analog->name)); -seq_buf_printf (&s,"Analog %d-axis %d-button" , -hweight8 (analog->mask &ANALOG_AXES_STD ), -hweight8 (analog->mask &ANALOG_BTNS_STD ) + !!(analog->mask &ANALOG_BTNS_CHF ) *2 + -hweight16 (analog->mask &ANALOG_BTNS_GAMEPAD ) + !!(analog->mask &ANALOG_HBTN_CHF ) *4 ); +prt_printf (&buf,"Analog %d-axis %d-button" , +hweight8 (analog->mask &ANALOG_AXES_STD ), +hweight8 (analog->mask &ANALOG_BTNS_STD ) + !!(analog->mask &ANALOG_BTNS_CHF ) *2 + +hweight16 (analog->mask &ANALOG_BTNS_GAMEPAD ) + !!(analog->mask &ANALOG_HBTN_CHF ) *4 );if (analog->mask &ANALOG_HATS_ALL ) -seq_buf_printf (&s," %d-hat" , -hweight16 (analog->mask &ANALOG_HATS_ALL )); - +prt_printf (&buf," %d-hat" ,hweight16 (analog->mask &ANALOG_HATS_ALL ));if (analog->mask &ANALOG_HAT_FCS ) -seq_buf_printf (&s," FCS" ); +prt_printf (&buf," FCS" );if (analog->mask &ANALOG_ANY_CHF ) -seq_buf_printf (&s, (analog->mask &ANALOG_SAITEK ) ?" Saitek" :" CHF" ); +prt_printf (&buf, (analog->mask &ANALOG_SAITEK ) ?" Saitek" :" CHF" ); -seq_buf_printf (&s, (analog->mask &ANALOG_GAMEPAD ) ?" gamepad" :" joystick" ); +prt_printf (&buf, (analog->mask &ANALOG_GAMEPAD ) ?" gamepad" :" joystick" ); }/*
Signature · 1 line
2.35.1
Re: [PATCH v5 21/32] Input/joystick/analog: Convert from seq_buf -> printbuf
Quoted 5 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
seq_bufis being deprecated, this converts to printbuf.Signed-off-by Kent Overstreet <[email protected]>
Thanks.
Signature · 1 line
Dmitry
PATCH v5 22/32 mm/memcontrol.c: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts memory_stat_format() from seq_buf to printbuf. Printbuf is simalar to seq_buf except that it heap allocates the string buffer:
here, we were already heap allocating the buffer with kmalloc() so the conversion is trivial.
--- mm/memcontrol.c | 54 ++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 27 deletions(-)
mm/memcontrol.c +27 -27
@@ -62,7 +62,7 @@#include <linux/file.h> #include <linux/resume_user_mode.h> #include <linux/psi.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include "internal.h" #include <net/sock.h> #include <net/ip.h>
@@ -1490,13 +1490,10 @@ static const unsigned int memcg_vm_event_stat[] = {#endif }; -static void memory_stat_format (struct mem_cgroup *memcg,char *buf,int bufsize) +static void memory_stat_format (struct printbuf *out,struct mem_cgroup *memcg) { -struct seq_buf s;int i; -seq_buf_init (&s, buf, bufsize); -/* * Provide statistics on the state of the memory subsystem as * well as cumulative event counters that show past behavior.
@@ -1513,30 +1510,30 @@ static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize)u64 size; size =memcg_page_state_output (memcg, memory_stats[i].idx); -seq_buf_printf (&s,"%s %llu\n" , memory_stats[i].name, size); +prt_printf (out,"%s %llu\n" , memory_stats[i].name, size);if (unlikely (memory_stats[i].idx ==NR_SLAB_UNRECLAIMABLE_B )) { size +=memcg_page_state_output (memcg,NR_SLAB_RECLAIMABLE_B ); -seq_buf_printf (&s,"slab %llu\n" , size); +prt_printf (out,"slab %llu\n" , size); } }/* Accumulated memory events */ -seq_buf_printf (&s,"pgscan %lu\n" , -memcg_events (memcg,PGSCAN_KSWAPD ) + -memcg_events (memcg,PGSCAN_DIRECT )); -seq_buf_printf (&s,"pgsteal %lu\n" , -memcg_events (memcg,PGSTEAL_KSWAPD ) + -memcg_events (memcg,PGSTEAL_DIRECT )); +prt_printf (out,"pgscan %lu\n" , +memcg_events (memcg,PGSCAN_KSWAPD ) + +memcg_events (memcg,PGSCAN_DIRECT )); +prt_printf (out,"pgsteal %lu\n" , +memcg_events (memcg,PGSTEAL_KSWAPD ) + +memcg_events (memcg,PGSTEAL_DIRECT ));for (i =0 ; i <ARRAY_SIZE (memcg_vm_event_stat); i++) -seq_buf_printf (&s,"%s %lu\n" , -vm_event_name (memcg_vm_event_stat[i]), -memcg_events (memcg, memcg_vm_event_stat[i])); +prt_printf (out,"%s %lu\n" , +vm_event_name (memcg_vm_event_stat[i]), +memcg_events (memcg, memcg_vm_event_stat[i]));/* The above should easily fit into one page */ -WARN_ON_ONCE (seq_buf_has_overflowed (&s)); +WARN_ON_ONCE (!printbuf_remaining (out)); }#define K (x) ((x) << (PAGE_SHIFT-10))
@@ -1573,7 +1570,8 @@ void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *void mem_cgroup_print_oom_meminfo (struct mem_cgroup *memcg) {/* Use static buffer, for the caller is holding oom_lock. */ -static char buf[PAGE_SIZE ]; +static char _buf[PAGE_SIZE ]; +struct printbuf buf =PRINTBUF_EXTERN (_buf,sizeof (_buf));lockdep_assert_held (&oom_lock);
@@ -1596,8 +1594,8 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)pr_info ("Memory cgroup stats for " );pr_cont_cgroup_path (memcg->css.cgroup);pr_cont (":" ); -memory_stat_format (memcg, buf,sizeof (buf)); -pr_info ("%s" , buf); +memory_stat_format (&buf, memcg); +pr_info ("%s" , buf.buf); }/*
@@ -6401,14 +6399,16 @@ static int memory_events_local_show(struct seq_file *m, void *v)static int memory_stat_show (struct seq_file *m,void *v) {struct mem_cgroup *memcg =mem_cgroup_from_seq (m); -char *buf =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +struct printbuf buf =PRINTBUF ; +int ret; -if (!buf) -return -ENOMEM ; -memory_stat_format (memcg, buf,PAGE_SIZE ); -seq_puts (m, buf); -kfree (buf); -return 0 ; +memory_stat_format (&buf, memcg); + ret = buf.allocation_failure ? -ENOMEM :0 ; +if (!ret) +seq_puts (m, buf.buf); + +printbuf_exit (&buf); +return ret; }#ifdef CONFIG_NUMA
Signature · 1 line
2.35.1
Re: [PATCH v5 22/32] mm/memcontrol.c: Convert to printbuf
Quoted 6 lines · Matthew Wilcox
From: Kent Overstreet <[email protected]>
This converts
memory_stat_format()fromseq_bufto printbuf. Printbuf is simalar toseq_bufexcept that it heap allocates the string buffer:
here, we were already heap allocating the buffer withkmalloc()so the conversion is trivial.
The changelog probably needs an update because the oom path doesn't allocated and for somebody just reading this patch in isolation the PRINTBUF_EXTERN doesn't really seem very obvious.
mm/memcontrol.c static const unsigned int memcg_vm_event_stat[] = { · line 1499
Quoted 13 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Roman Gushchin <[email protected]>mm/memcontrol.c +1
-4-static void memory_stat_format (struct mem_cgroup *memcg,char *buf,int bufsize) +static void memory_stat_format (struct printbuf *out,struct mem_cgroup *memcg) { -struct seq_buf s;int i; -seq_buf_init (&s, buf, bufsize); -
When is the buffer cleared?
Quoted 3 lines
mm/memcontrol.c +0
-0/* * Provide statistics on the state of the memory subsystem as * well as cumulative event counters that show past behavior.
[...]
mm/memcontrol.c void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct * · line 1576
Quoted 10 lines
#define K(x) ((x) << (PAGE_SHIFT-10))
mm/memcontrol.c +2
-1@@ -1573,7 +1570,8 @@ void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *void mem_cgroup_print_oom_meminfo (struct mem_cgroup *memcg) {/* Use static buffer, for the caller is holding oom_lock. */ -static char buf[PAGE_SIZE ]; +static char _buf[PAGE_SIZE ]; +struct printbuf buf =PRINTBUF_EXTERN (_buf,sizeof (_buf));lockdep_assert_held (&oom_lock);
the buffer is static here!
Quoted 1 line
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
Acked-byRe: [PATCH v5 22/32] mm/memcontrol.c: Convert to printbuf
Quoted 41 lines · Michal Hocko
Quoted 6 lines · Matthew Wilcox
From: Kent Overstreet <[email protected]>
This converts
memory_stat_format()fromseq_bufto printbuf. Printbuf is simalar toseq_bufexcept that it heap allocates the string buffer:
here, we were already heap allocating the buffer withkmalloc()so the conversion is trivial.The changelog probably needs an update because the oom path doesn't allocated and for somebody just reading this patch in isolation the
PRINTBUF_EXTERNdoesn't really seem very obvious.mm/memcontrol.c static const unsigned int memcg_vm_event_stat[] = { · line 1499
Quoted 13 lines
Signed-off-by Kent Overstreet <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Roman Gushchin <[email protected]>mm/memcontrol.c +1
-4-static void memory_stat_format (struct mem_cgroup *memcg,char *buf,int bufsize) +static void memory_stat_format (struct printbuf *out,struct mem_cgroup *memcg) { -struct seq_buf s;int i; -seq_buf_init (&s, buf, bufsize); -When is the buffer cleared?
Quoted 3 lines
mm/memcontrol.c +0
-0/* * Provide statistics on the state of the memory subsystem as * well as cumulative event counters that show past behavior. [...]
Quoted 9 lines
#define K(x) ((x) << (PAGE_SHIFT-10))
mm/memcontrol.c +2
-1@@ -1573,7 +1570,8 @@ void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct *void mem_cgroup_print_oom_meminfo (struct mem_cgroup *memcg) {/* Use static buffer, for the caller is holding oom_lock. */ -static char buf[PAGE_SIZE ]; +static char _buf[PAGE_SIZE ]; +struct printbuf buf =PRINTBUF_EXTERN (_buf,sizeof (_buf));
Quoted 3 lines
mm/memcontrol.c void mem_cgroup_print_oom_context(struct mem_cgroup *memcg, struct task_struct * · line 1576
Quoted 1 line
mm/memcontrol.c +0
-0lockdep_assert_held (&oom_lock);the buffer is static here!
And answering to myself. THere is an internal pos field which gets initialized by PRINTBUF_EXTERN even when not mentioned explicitly because it gets a default 0 initialization
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v5 23/32 clk: tegra: bpmp: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts from seq_buf to printbuf, which is similar but heap allocates the string buffer.
Previously in this code the string buffer was allocated on the stack; this means we've added a new potential memory allocation failure. This is fine though since it's only for a dev_printk() message.
Memory allocation context: printbuf doesn't take gfp flags, instead we prefer the new memalloc_no*_(save|restore) interfaces to be used. Here the surrounding code is already allocating with GFP_KERNEL, so everything is fine.
--- drivers/clk/tegra/clk-bpmp.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/clk/tegra/clk-bpmp.c +10 -11
@@ -5,7 +5,7 @@#include <linux/clk-provider.h> #include <linux/device.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <linux/slab.h> #include <soc/tegra/bpmp.h>
@@ -365,39 +365,38 @@ static void tegra_bpmp_clk_info_dump(struct tegra_bpmp *bpmp, const struct tegra_bpmp_clk_info *info) {const char *prefix ="" ; -struct seq_buf buf; +struct printbuf buf =PRINTBUF ;unsigned int i; -char flags[64 ]; - -seq_buf_init (&buf, flags,sizeof (flags));if (info->flags) -seq_buf_printf (&buf,"(" ); +prt_printf (&buf,"(" );if (info->flags &TEGRA_BPMP_CLK_HAS_MUX ) { -seq_buf_printf (&buf,"%smux" , prefix); +prt_printf (&buf,"%smux" , prefix); prefix =", " ; }if ((info->flags &TEGRA_BPMP_CLK_HAS_SET_RATE ) ==0 ) { -seq_buf_printf (&buf,"%sfixed" , prefix); +prt_printf (&buf,"%sfixed" , prefix); prefix =", " ; }if (info->flags &TEGRA_BPMP_CLK_IS_ROOT ) { -seq_buf_printf (&buf,"%sroot" , prefix); +prt_printf (&buf,"%sroot" , prefix); prefix =", " ; }if (info->flags) -seq_buf_printf (&buf,")" ); +prt_printf (&buf,")" );dev_printk (level, bpmp->dev,"%03u: %s\n" , info->id, info->name); -dev_printk (level, bpmp->dev," flags: %lx %s\n" , info->flags, flags); +dev_printk (level, bpmp->dev," flags: %lx %s\n" , info->flags,printbuf_str (&buf));dev_printk (level, bpmp->dev," parents: %u\n" , info->num_parents);for (i =0 ; i < info->num_parents; i++)dev_printk (level, bpmp->dev," %03u\n" , info->parents[i]); + +printbuf_exit (&buf); } static int tegra_bpmp_probe_clocks(struct tegra_bpmp *bpmp,
Signature · 1 line
2.35.1
PATCH v5 24/32 tools/testing/nvdimm: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts from seq_buf to printbuf. Here we're using printbuf with an external buffer, meaning it's a direct conversion.
--- tools/testing/nvdimm/test/ndtest.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
tools/testing/nvdimm/test/ndtest.c +10 -12
@@ -12,7 +12,7 @@#include <linux/ndctl.h> #include <nd-core.h> #include <linux/printk.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include "../watermark.h" #include "nfit_test.h"
@@ -740,32 +740,30 @@ static ssize_t flags_show(struct device *dev, {struct nvdimm *nvdimm =to_nvdimm (dev);struct ndtest_dimm *dimm =nvdimm_provider_data (nvdimm); -struct seq_buf s; +struct printbuf s =PRINTBUF_EXTERN (buf,PAGE_SIZE );u64 flags; flags = dimm->flags; -seq_buf_init (&s, buf,PAGE_SIZE );if (flags &PAPR_PMEM_UNARMED_MASK ) -seq_buf_printf (&s,"not_armed " ); +prt_printf (&s,"not_armed " );if (flags &PAPR_PMEM_BAD_SHUTDOWN_MASK ) -seq_buf_printf (&s,"flush_fail " ); +prt_printf (&s,"flush_fail " );if (flags &PAPR_PMEM_BAD_RESTORE_MASK ) -seq_buf_printf (&s,"restore_fail " ); +prt_printf (&s,"restore_fail " );if (flags &PAPR_PMEM_SAVE_MASK ) -seq_buf_printf (&s,"save_fail " ); +prt_printf (&s,"save_fail " );if (flags &PAPR_PMEM_SMART_EVENT_MASK ) -seq_buf_printf (&s,"smart_notify " ); +prt_printf (&s,"smart_notify " ); +if (printbuf_written (&s)) +prt_printf (&s,"\n" ); -if (seq_buf_used (&s)) -seq_buf_printf (&s,"\n" ); - -return seq_buf_used (&s); +return printbuf_written (&s); }static DEVICE_ATTR_RO (flags);
Signature · 1 line
2.35.1
RE: [PATCH v5 24/32] tools/testing/nvdimm: Convert to printbuf
Quoted 9 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
This converts from
seq_bufto printbuf. Here we're using printbuf with an external buffer, meaning it's a direct conversion.Signed-off-by Kent Overstreet <[email protected]>Cc Dan Williams <[email protected]>Cc Dave Hansen <[email protected]>
My Acked-by still applies:
https://lore.kernel.org/all/[email protected]/
...and Shivaprasad's Tested-by should still apply:
Re: [PATCH v5 24/32] tools/testing/nvdimm: Convert to printbuf
Quoted 11 lines · Dan Williams
Quoted 9 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
This converts from
seq_bufto printbuf. Here we're using printbuf with an external buffer, meaning it's a direct conversion.Signed-off-by Kent Overstreet <[email protected]>Cc Dan Williams <[email protected]>Cc Dave Hansen <[email protected]>
Quoted 7 lines
My Acked-by still applies:
https://lore.kernel.org/all/[email protected]/
...and Shivaprasad's Tested-by should still apply:
Whoops - got them now, thanks!
PATCH v5 25/32 powerpc: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts from seq_buf to printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring in cpu_show_meltdown() to make the code more consistent.
--- arch/powerpc/kernel/process.c | 16 +++-- arch/powerpc/kernel/security.c | 75 ++++++++++------------- arch/powerpc/platforms/pseries/papr_scm.c | 34 +++++----- 3 files changed, 57 insertions(+), 68 deletions(-)
arch/powerpc/kernel/process.c +7 -9
@@ -37,7 +37,7 @@#include <linux/hw_breakpoint.h> #include <linux/uaccess.h> #include <linux/pkeys.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <asm/interrupt.h> #include <asm/io.h>
@@ -1396,32 +1396,30 @@ void show_user_instructions(struct pt_regs *regs) {unsigned long pc;int n =NR_INSN_TO_PRINT ; -struct seq_buf s;char buf[96 ];/* enough for 8 times 9 + 2 chars */ +struct printbuf s =PRINTBUF_EXTERN (buf,sizeof (buf)); pc = regs->nip - (NR_INSN_TO_PRINT *3 /4 *sizeof (int )); -seq_buf_init (&s, buf,sizeof (buf)); -while (n) {int i; -seq_buf_clear (&s); +printbuf_reset (&s);for (i =0 ; i <8 && n; i++, n--, pc +=sizeof (int )) {int instr;if (copy_from_user_nofault (&instr, (void __user *)pc,sizeof (instr))) { -seq_buf_printf (&s,"XXXXXXXX " ); +prt_printf (&s,"XXXXXXXX " );continue ; } -seq_buf_printf (&s, regs->nip == pc ?"<%08x> " :"%08x " , instr); +prt_printf (&s, regs->nip == pc ?"<%08x> " :"%08x " , instr); } -if (!seq_buf_has_overflowed (&s)) +if (printbuf_remaining (&s))pr_info ("%s[%d]: code: %s\n" , current->comm, - current->pid, s.buffer); + current->pid, s.buf); } }
arch/powerpc/kernel/security.c +34 -41
142 lines not on this page · raw message
arch/powerpc/platforms/pseries/papr_scm.c +16 -18
@@ -12,7 +12,7 @@#include <linux/libnvdimm.h> #include <linux/platform_device.h> #include <linux/delay.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <linux/nd.h> #include <asm/plpar_wrappers.h>
@@ -1142,7 +1142,7 @@ static ssize_t perf_stats_show(struct device *dev, {int index;ssize_t rc; -struct seq_buf s; +struct printbuf s =PRINTBUF_EXTERN (buf,PAGE_SIZE );struct papr_scm_perf_stat *stat;struct papr_scm_perf_stats *stats;struct nvdimm *dimm =to_nvdimm (dev);
@@ -1165,18 +1165,17 @@ static ssize_t perf_stats_show(struct device *dev,* values. Since stat_id is essentially a char string of * 8 bytes, simply use the string format specifier to print it. */ -seq_buf_init (&s, buf,PAGE_SIZE );for (index =0 , stat = stats->scm_statistic; index <be32_to_cpu (stats->num_statistics); ++index, ++stat) { -seq_buf_printf (&s,"%.8s = 0x%016llX\n" , - stat->stat_id, -be64_to_cpu (stat->stat_val)); +prt_printf (&s,"%.8s = 0x%016llX\n" , + stat->stat_id, +be64_to_cpu (stat->stat_val)); } free_stats:kfree (stats); -return rc ? rc : (ssize_t )seq_buf_used (&s); +return rc ?:printbuf_written (&s); }static DEVICE_ATTR_ADMIN_RO (perf_stats);
@@ -1185,7 +1184,7 @@ static ssize_t flags_show(struct device *dev, {struct nvdimm *dimm =to_nvdimm (dev);struct papr_scm_priv *p =nvdimm_provider_data (dimm); -struct seq_buf s; +struct printbuf s =PRINTBUF_EXTERN (buf,PAGE_SIZE );u64 health;int rc;
@@ -1196,29 +1195,28 @@ static ssize_t flags_show(struct device *dev,/* Copy health_bitmap locally, check masks & update out buffer */ health =READ_ONCE (p->health_bitmap); -seq_buf_init (&s, buf,PAGE_SIZE );if (health &PAPR_PMEM_UNARMED_MASK ) -seq_buf_printf (&s,"not_armed " ); +prt_printf (&s,"not_armed " );if (health &PAPR_PMEM_BAD_SHUTDOWN_MASK ) -seq_buf_printf (&s,"flush_fail " ); +prt_printf (&s,"flush_fail " );if (health &PAPR_PMEM_BAD_RESTORE_MASK ) -seq_buf_printf (&s,"restore_fail " ); +prt_printf (&s,"restore_fail " );if (health &PAPR_PMEM_ENCRYPTED ) -seq_buf_printf (&s,"encrypted " ); +prt_printf (&s,"encrypted " );if (health &PAPR_PMEM_SMART_EVENT_MASK ) -seq_buf_printf (&s,"smart_notify " ); +prt_printf (&s,"smart_notify " );if (health &PAPR_PMEM_SCRUBBED_AND_LOCKED ) -seq_buf_printf (&s,"scrubbed locked " ); +prt_printf (&s,"scrubbed locked " ); -if (seq_buf_used (&s)) -seq_buf_printf (&s,"\n" ); +if (printbuf_written (&s)) +prt_printf (&s,"\n" ); -return seq_buf_used (&s); +return printbuf_written (&s); }DEVICE_ATTR_RO (flags);
Signature · 1 line
2.35.1
PATCH v5 26/32 x86/resctrl: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts from seq_buf to printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring in cpu_show_meltdown() to make the code more consistent.
--- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +8 -8
@@ -19,7 +19,7 @@#include <linux/fs_parser.h> #include <linux/sysfs.h> #include <linux/kernfs.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <linux/seq_file.h> #include <linux/sched/signal.h> #include <linux/sched/task.h>
@@ -51,7 +51,7 @@ static struct kernfs_node *kn_mongrp;/* Kernel fs node for "mon_data" directory under root */ static struct kernfs_node *kn_mondata; -static struct seq_buf last_cmd_status; +static struct printbuf last_cmd_status;static char last_cmd_status_buf[512 ];struct dentry *debugfs_resctrl;
@@ -59,13 +59,13 @@ struct dentry *debugfs_resctrl;void rdt_last_cmd_clear (void ) {lockdep_assert_held (&rdtgroup_mutex); -seq_buf_clear (&last_cmd_status); +printbuf_reset (&last_cmd_status); }void rdt_last_cmd_puts (const char *s) {lockdep_assert_held (&rdtgroup_mutex); -seq_buf_puts (&last_cmd_status, s); +prt_str (&last_cmd_status, s); }void rdt_last_cmd_printf (const char *fmt, ...)
@@ -74,7 +74,7 @@ void rdt_last_cmd_printf(const char *fmt, ...)va_start (ap, fmt);lockdep_assert_held (&rdtgroup_mutex); -seq_buf_vprintf (&last_cmd_status, fmt, ap); +prt_vprintf (&last_cmd_status, fmt, ap);va_end (ap); }
@@ -833,7 +833,7 @@ static int rdt_last_cmd_status_show(struct kernfs_open_file *of,int len;mutex_lock (&rdtgroup_mutex); - len =seq_buf_used (&last_cmd_status); + len =printbuf_written (&last_cmd_status);if (len)seq_printf (seq,"%.*s" , len, last_cmd_status_buf);else
@@ -3248,8 +3248,8 @@ int __init rdtgroup_init(void) {int ret =0 ; -seq_buf_init (&last_cmd_status, last_cmd_status_buf, -sizeof (last_cmd_status_buf)); + last_cmd_status =PRINTBUF_EXTERN (last_cmd_status_buf, +sizeof (last_cmd_status_buf)); ret =rdtgroup_setup_root ();if (ret)
Signature · 1 line
2.35.1
PATCH v5 27/32 PCI/P2PDMA: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts from seq_buf to printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring in cpu_show_meltdown() to make the code more consistent.
--- drivers/pci/p2pdma.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/pci/p2pdma.c +8 -13
@@ -18,7 +18,7 @@#include <linux/memremap.h> #include <linux/percpu-refcount.h> #include <linux/random.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <linux/xarray.h> struct pci_p2pdma {
@@ -275,12 +275,9 @@ static int pci_bridge_has_acs_redir(struct pci_dev *pdev)return 0 ; } -static void seq_buf_print_bus_devfn (struct seq_buf *buf,struct pci_dev *pdev) +static void prt_bus_devfn (struct printbuf *buf,struct pci_dev *pdev) { -if (!buf) -return ; - -seq_buf_printf (buf,"%s;" ,pci_name (pdev)); +prt_printf (buf,"%s;" ,pci_name (pdev)); }static bool cpu_supports_p2pdma (void )
@@ -454,13 +451,11 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,struct pci_dev *a = provider, *b = client, *bb;bool acs_redirects = false;struct pci_p2pdma *p2pdma; -struct seq_buf acs_list;int acs_cnt =0 ;int dist_a =0 ;int dist_b =0 ;char buf[128 ]; - -seq_buf_init (&acs_list, buf,sizeof (buf)); +struct printbuf acs_list =PRINTBUF_EXTERN (buf,sizeof (buf));/* * Note, we don't need to take references to devices returned by
@@ -471,7 +466,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client, dist_b =0 ;if (pci_bridge_has_acs_redir (a)) { -seq_buf_print_bus_devfn (&acs_list, a); +prt_bus_devfn (&acs_list, a); acs_cnt++; }
@@ -500,7 +495,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,break ;if (pci_bridge_has_acs_redir (bb)) { -seq_buf_print_bus_devfn (&acs_list, bb); +prt_bus_devfn (&acs_list, bb); acs_cnt++; }
@@ -515,11 +510,11 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client, }if (verbose) { - acs_list.buffer[acs_list.len-1 ] =0 ;/* drop final semicolon */ + acs_list.buf[acs_list.pos-1 ] =0 ;/* drop final semicolon */ pci_warn (client,"ACS redirect is set between the client and provider (%s)\n" ,pci_name (provider));pci_warn (client,"to disable ACS redirect for this path, add the kernel parameter: pci=disable_acs_redir=%s\n" , - acs_list.buffer); + acs_list.buf); } acs_redirects = true;
Signature · 1 line
2.35.1
Re: [PATCH v5 27/32] PCI/P2PDMA: Convert to printbuf
Quoted 5 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
This converts from
seq_bufto printbuf. We're using printbuf in external buffer mode, so it's a direct conversion, aside from some trivial refactoring incpu_show_meltdown()to make the code more consistent.
I don't object to the patch, but it would be nice if the commit log hinted at what the advantage is. I assume it's faster/safer/better in some way, but I have no idea what.
Also, cpu_show_meltdown() doesn't appear in this patch, so maybe that's relevant to some other patch but not this one?
Quoted 84 lines
Signed-off-by Kent Overstreet <[email protected]>--- drivers/pci/p2pdma.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)drivers/pci/p2pdma.c +8
-13@@ -18,7 +18,7 @@#include <linux/memremap.h> #include <linux/percpu-refcount.h> #include <linux/random.h> -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <linux/xarray.h> struct pci_p2pdma {@@ -275,12 +275,9 @@ static int pci_bridge_has_acs_redir(struct pci_dev *pdev)return 0 ; } -static void seq_buf_print_bus_devfn (struct seq_buf *buf,struct pci_dev *pdev) +static void prt_bus_devfn (struct printbuf *buf,struct pci_dev *pdev) { -if (!buf) -return ; - -seq_buf_printf (buf,"%s;" ,pci_name (pdev)); +prt_printf (buf,"%s;" ,pci_name (pdev)); }static bool cpu_supports_p2pdma (void )@@ -454,13 +451,11 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,struct pci_dev *a = provider, *b = client, *bb;bool acs_redirects = false;struct pci_p2pdma *p2pdma; -struct seq_buf acs_list;int acs_cnt =0 ;int dist_a =0 ;int dist_b =0 ;char buf[128 ]; - -seq_buf_init (&acs_list, buf,sizeof (buf)); +struct printbuf acs_list =PRINTBUF_EXTERN (buf,sizeof (buf));/* * Note, we don't need to take references to devices returned by @@ -471,7 +466,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client, dist_b =0 ;if (pci_bridge_has_acs_redir (a)) { -seq_buf_print_bus_devfn (&acs_list, a); +prt_bus_devfn (&acs_list, a); acs_cnt++; }@@ -500,7 +495,7 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client,break ;if (pci_bridge_has_acs_redir (bb)) { -seq_buf_print_bus_devfn (&acs_list, bb); +prt_bus_devfn (&acs_list, bb); acs_cnt++; }@@ -515,11 +510,11 @@ calc_map_type_and_dist(struct pci_dev *provider, struct pci_dev *client, }if (verbose) { - acs_list.buffer[acs_list.len-1 ] =0 ;/* drop final semicolon */ + acs_list.buf[acs_list.pos-1 ] =0 ;/* drop final semicolon */ pci_warn (client,"ACS redirect is set between the client and provider (%s)\n" ,pci_name (provider));pci_warn (client,"to disable ACS redirect for this path, add the kernel parameter: pci=disable_acs_redir=%s\n" , - acs_list.buffer); + acs_list.buf); } acs_redirects = true;Signature · 1 line
2.35.1
Re: [PATCH v5 27/32] PCI/P2PDMA: Convert to printbuf
Quoted 3 lines · Bjorn Helgaas
I don't object to the patch, but it would be nice if the commit log hinted at what the advantage is. I assume it's faster/safer/better in some way, but I have no idea what.
Printbufs have some additional features over seq_buf but they're not used here. The main one you might be interested in is heap allocation: that means no need to statically allocate buffers on the stack and no need to calculate the buffer size, printbufs will reallocate as necessary.
I generally haven't been converting code to use that unless it's obvious that we're in a context where it's safe to allocate memory and can deal with allocation failures.
I notice that in calc_map_type_and_dist() you're using xa_store() which can fail, but you're not checking for that or returning errors properly :) perhaps a fix for that could also switch to using printbuf in heap-allocation mode.
Quoted 2 lines
Also,
cpu_show_meltdown()doesn't appear in this patch, so maybe that's relevant to some other patch but not this one?
Whoops, was copying the commit message from another patch, yeah.n
Re: [PATCH v5 27/32] PCI/P2PDMA: Convert to printbuf
Quoted 9 lines · Kent Overstreet
Quoted 3 lines · Bjorn Helgaas
I don't object to the patch, but it would be nice if the commit log hinted at what the advantage is. I assume it's faster/safer/better in some way, but I have no idea what.
Printbufs have some additional features over
seq_bufbut they're not used here. The main one you might be interested in is heap allocation: that means no need to statically allocate buffers on the stack and no need to calculate the buffer size, printbufs will reallocate as necessary.
This doesn't tell me what the advantage of this patch is, since I don't think *this* patch uses heap allocation.
As far as I can tell, this particular patch doesn't improve safety or readability, so "convert X to Y even though we don't use any fancy Y features" is a pointless message.
But if printbufs are better than seq_buf overall, and converting this gets us closer to the goal of removing seq_buf completely, that's a perfectly acceptable reason. Just say that.
Quoted 13 lines
I generally haven't been converting code to use that unless it's obvious that we're in a context where it's safe to allocate memory and can deal with allocation failures.
I notice that in
calc_map_type_and_dist()you're usingxa_store()which can fail, but you're not checking for that or returning errors properly :) perhaps a fix for that could also switch to using printbuf in heap-allocation mode.Quoted 2 lines
Also,
cpu_show_meltdown()doesn't appear in this patch, so maybe that's relevant to some other patch but not this one?Whoops, was copying the commit message from another patch, yeah.n
Re: [PATCH v5 27/32] PCI/P2PDMA: Convert to printbuf
Quoted 10 lines · Bjorn Helgaas
This doesn't tell me what the advantage of this patch is, since I don't think *this* patch uses heap allocation.
As far as I can tell, this particular patch doesn't improve safety or readability, so "convert X to Y even though we don't use any fancy Y features" is a pointless message.
But if printbufs are better than
seq_bufoverall, and converting this gets us closer to the goal of removingseq_bufcompletely, that's a perfectly acceptable reason. Just say that.
Which still brings me back to a point made long time ago: Why are we doing the renaming to start with? Add the new functionality and or changes to seq_buf gradually instead of doing a tree wide sweep. I don't think the new naming has a huge advantage (in fact I think the old one actually is a little better, but the biggest argument here really is to not change something if we don't have to).
That will massively reduce the churn at the cost of Kent having to rework the code a bit, but that seems like a worthwhile tradeoff and one that we've usuall made in the past.
PATCH v5 28/32 tracing: trace_events_synth: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts from seq_buf to printbuf.
This code was using seq_buf for building up dynamically allocated strings; the conversion uses printbuf's heap allocation functionality to simplify things (no longer need to calculate size of the output string).
Also, alphabetize the #includes.
--- kernel/trace/trace_events_synth.c | 51 ++++++++++--------------------- 1 file changed, 16 insertions(+), 35 deletions(-)
kernel/trace/trace_events_synth.c +16 -35
@@ -5,13 +5,14 @@* Copyright (C) 2015, 2020 Tom Zanussi <[email protected]> */ -#include <linux/module.h> #include <linux/kallsyms.h> -#include <linux/security.h> +#include <linux/module.h> #include <linux/mutex.h> +#include <linux/printbuf.h> +#include <linux/rculist.h> +#include <linux/security.h> #include <linux/slab.h> #include <linux/stacktrace.h> -#include <linux/rculist.h> #include <linux/tracefs.h> /* for gfp flag names */
@@ -611,7 +612,7 @@ static struct synth_field *parse_synth_field(int argc, char **argv,const char *prefix =NULL , *field_type = argv[0 ], *field_name, *array;struct synth_field *field;int len, ret = -ENOMEM ; -struct seq_buf s; +struct printbuf buf;ssize_t size;if (!strcmp (field_type,"unsigned" )) {
@@ -654,28 +655,16 @@ static struct synth_field *parse_synth_field(int argc, char **argv, goto free; } - len =strlen (field_type) +1 ; - -if (array) - len +=strlen (array); - -if (prefix) - len +=strlen (prefix); - - field->type =kzalloc (len,GFP_KERNEL ); -if (!field->type) - goto free; - -seq_buf_init (&s, field->type, len); + buf =PRINTBUF ;if (prefix) -seq_buf_puts (&s, prefix); -seq_buf_puts (&s, field_type); +prt_str (&buf, prefix); +prt_str (&buf, field_type);if (array) -seq_buf_puts (&s, array); -if (WARN_ON_ONCE (!seq_buf_buffer_left (&s))) +prt_str (&buf, array); +if (buf.allocation_failure) goto free; - s.buffer[s.len] ='\0' ; + field->type = buf.buf; size =synth_field_size (field->type);if (size <0 ) {
@@ -687,23 +676,15 @@ static struct synth_field *parse_synth_field(int argc, char **argv, goto free; }else if (size ==0 ) {if (synth_field_is_string (field->type)) { -char *type; - - len =sizeof ("__data_loc " ) +strlen (field->type) +1 ; - type =kzalloc (len,GFP_KERNEL ); -if (!type) - goto free; - -seq_buf_init (&s, type, len); -seq_buf_puts (&s,"__data_loc " ); -seq_buf_puts (&s, field->type); + buf =PRINTBUF ; +prt_str (&buf,"__data_loc " ); +prt_str (&buf, field->type); -if (WARN_ON_ONCE (!seq_buf_buffer_left (&s))) +if (buf.allocation_failure) goto free; - s.buffer[s.len] ='\0' ;kfree (field->type); - field->type = type; + field->type = buf.buf; field->is_dynamic = true; size =sizeof (u64);
Signature · 1 line
2.35.1
PATCH v5 29/32 d_path: prt_path()
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This implements a new printbuf version of d_path()/mangle_path(), which will replace the seq_buf version.
Part of what we're trying to do with printbufs is standardizing a calling convention so they don't have to live in lib/vsprintf.c, and can instead with the code for the types they're printing - so this patch adds prt_path() to d_path.c, another patch will switch vsprintf.c to use it.
--- fs/d_path.c | 35 +++++++++++++++++++++++++++++++++++ include/linux/dcache.h | 1 + 2 files changed, 36 insertions(+)
fs/d_path.c +35 -0
@@ -5,6 +5,7 @@#include <linux/fs_struct.h> #include <linux/fs.h> #include <linux/slab.h> +#include <linux/printbuf.h> #include <linux/prefetch.h> #include "mount.h"
@@ -294,6 +295,40 @@ char *d_path(const struct path *path, char *buf, int buflen) }EXPORT_SYMBOL (d_path); +/** +* prt_path - format a path for output +* @out: printbuf to output to +* @path: path to write into the sequence buffer. +* @esc: set of characters to escape in the output +* +* Write a path name into the sequence buffer. +* +* Returns 0 on success, or error code from d_path +*/ +int prt_path (struct printbuf *out,const struct path *path,const char *esc) +{ +char *p, *buf; +size_t size; +again: + buf = out->buf + out->pos; + size =printbuf_remaining_size (out); + + p =d_path (path, buf, size); +if (IS_ERR (p)) { +printbuf_make_room (out,max_t (size_t,64 , size *2 )); +if (printbuf_remaining_size (out) > size) + goto again; + +return PTR_ERR (p); + } + + p =mangle_path (buf, p, esc); +if (p) + out->pos += p - buf; +return 0 ; +} +EXPORT_SYMBOL (prt_path); +/* * Helper function for dentry_operations.d_dname() members */
include/linux/dcache.h +1 -0
@@ -293,6 +293,7 @@ extern char *d_absolute_path(const struct path *, char *, int);extern char *d_path (const struct path *,char *,int );extern char *dentry_path_raw (const struct dentry *,char *,int );extern char *dentry_path (const struct dentry *,char *,int ); +extern int prt_path (struct printbuf *,const struct path *,const char *);/* Allocation counts.. */
Signature · 1 line
2.35.1
Al Viro
maintainerRe: [PATCH v5 29/32] d_path: prt_path()
Quoted 10 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
This implements a new printbuf version of d_path()/mangle_path(), which will replace the
seq_bufversion.Part of what we're trying to do with printbufs is standardizing a calling convention so they don't have to live in
lib/vsprintf.c, and can instead with the code for the types they're printing - so this patch addsprt_path()tod_path.c, another patch will switchvsprintf.cto use it.
To use it for *what*? If you mean replacing %pd with it - forget about that. Not going to happen.
There's a hard requirement for printk - it should be safe to call in any locking environment. d_path() isn't, and that's impossible to avoid.
Re: [PATCH v5 29/32] d_path: prt_path()
Quoted 12 lines · Al Viro
Quoted 10 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
This implements a new printbuf version of d_path()/mangle_path(), which will replace the
seq_bufversion.Part of what we're trying to do with printbufs is standardizing a calling convention so they don't have to live in
lib/vsprintf.c, and can instead with the code for the types they're printing - so this patch addsprt_path()tod_path.c, another patch will switchvsprintf.cto use it.
Quoted 5 lines
To use it for *what*? If you mean replacing %pd with it - forget about that. Not going to happen.
There's a hard requirement for printk - it should be safe to call in any locking environment.
d_path()isn't, and that's impossible to avoid.
Oof, good catch. It was to replace seq_buf_path() in this patch series, and is only used in trace_seq.c. I'll have to investigate what's going on in the tracing code, because now that you mention it that did look suspect.
PATCH v5 30/32 ACPI/APEI: Add missing include
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
The upcoming patch to switch tracing from seq_buf to printbuf means we're no longer pulling in headers that we used to; this adds a missing include so things don't break.
--- drivers/acpi/apei/erst-dbg.c | 1 + 1 file changed, 1 insertion(+)
drivers/acpi/apei/erst-dbg.c +1 -0
@@ -11,6 +11,7 @@* Author: Huang Ying <[email protected]> */ +#include <linux/fs.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/uaccess.h>
Signature · 1 line
2.35.1
Re: [PATCH v5 30/32] ACPI/APEI: Add missing include
Quoted 9 lines · Matthew Wilcox (Oracle)
From: Kent Overstreet <[email protected]>
The upcoming patch to switch tracing from
seq_bufto printbuf means we're no longer pulling in headers that we used to; this adds a missing include so things don't break.Signed-off-by Kent Overstreet <[email protected]>
Quoted 19 lines
--- drivers/acpi/apei/erst-dbg.c | 1 + 1 file changed, 1 insertion(+)drivers/acpi/apei/erst-dbg.c +1
-1@@ -11,6 +11,7 @@* Author: Huang Ying <[email protected]> */ +#include <linux/fs.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/uaccess.h> --2.35.1
PATCH v5 31/32 tracing: Convert to printbuf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
This converts the seq_bufs in dynevent_cmd and trace_seq to printbufs.
- read_pos in seq_buf doesn't exist in printbuf, so is added to trace_seq
- seq_buf_to_user doesn't have a printbuf equivalent, so is inlined into trace_seq_to_user
- seq_buf_putmem_hex currently swabs bytes on little endian, hardcoded to 8 byte units. This patch switches it to prt_hex_bytes(), which does _not_ swab.
Otherwise this is largely a direct conversion, with a few slight refactorings and cleanups.
--- include/linux/trace_events.h | 2 +- include/linux/trace_seq.h | 17 ++-- kernel/trace/trace.c | 45 ++++------- kernel/trace/trace_dynevent.c | 34 ++++---- kernel/trace/trace_events_filter.c | 2 +- kernel/trace/trace_events_synth.c | 2 +- kernel/trace/trace_functions_graph.c | 6 +- kernel/trace/trace_kprobe.c | 2 +- kernel/trace/trace_seq.c | 111 ++++++++++++++------------- 9 files changed, 103 insertions(+), 118 deletions(-)
include/linux/trace_events.h +1 -1
@@ -496,7 +496,7 @@ struct dynevent_cmd;typedef int (*dynevent_create_fn_t )(struct dynevent_cmd *cmd);struct dynevent_cmd { -struct seq_buf seq; +struct printbuf seq;const char *event_name;unsigned int n_fields;enum dynevent_type type;
include/linux/trace_seq.h +11 -6
@@ -2,10 +2,12 @@#ifndef _LINUX_TRACE_SEQ_H#define _LINUX_TRACE_SEQ_H -#include <linux/seq_buf.h> +#include <linux/printbuf.h> #include <asm/page.h> +struct seq_file ; +/* * Trace sequences are used to allow a function to call several other functions * to create a string of data to use (up to a max of PAGE_SIZE).
@@ -13,14 +15,16 @@struct trace_seq {char buffer[PAGE_SIZE ]; -struct seq_buf seq; +struct printbuf seq; +unsigned readpos;int full; };static inline void trace_seq_init (struct trace_seq *s) { -seq_buf_init (&s->seq, s->buffer,PAGE_SIZE ); + s->seq =PRINTBUF_EXTERN (s->buffer,PAGE_SIZE ); + s->readpos =0 ; s->full =0 ; }
@@ -39,7 +43,7 @@ trace_seq_init(struct trace_seq *s)*/ static inline int trace_seq_used (struct trace_seq *s) { -return seq_buf_used (&s->seq); +return printbuf_written (&s->seq); }/**
@@ -54,7 +58,7 @@ static inline int trace_seq_used(struct trace_seq *s)static inline char *trace_seq_buffer_ptr (struct trace_seq *s) { -return s->buffer +seq_buf_used (&s->seq); +return s->buffer +printbuf_written (&s->seq); }/**
@@ -66,7 +70,7 @@ trace_seq_buffer_ptr(struct trace_seq *s)*/ static inline bool trace_seq_has_overflowed (struct trace_seq *s) { -return s->full ||seq_buf_has_overflowed (&s->seq); +return s->full ||printbuf_overflowed (&s->seq); }/*
@@ -87,6 +91,7 @@ extern void trace_seq_putc(struct trace_seq *s, unsigned char c);extern void trace_seq_putmem (struct trace_seq *s,const void *mem,unsigned int len);extern void trace_seq_putmem_hex (struct trace_seq *s,const void *mem,unsigned int len); +struct path ;extern int trace_seq_path (struct trace_seq *s,const struct path *path); extern void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
kernel/trace/trace.c +14 -31
109 lines not on this page · raw message
kernel/trace/trace_dynevent.c +14 -20
@@ -290,21 +290,19 @@ int dynevent_arg_add(struct dynevent_cmd *cmd,struct dynevent_arg *arg, dynevent_check_arg_fn_t check_arg) { - int ret = 0; - if (check_arg) { - ret =check_arg (arg); + int ret = check_arg(arg);if (ret)return ret; } - ret =seq_buf_printf (&cmd->seq," %s%c" , arg->str, arg->separator); -if (ret) { +prt_printf (&cmd->seq," %s%c" , arg->str, arg->separator); +if (printbuf_overflowed (&cmd->seq)) {pr_err ("String is too long: %s%c\n" , arg->str, arg->separator);return -E2BIG ; } -return ret; +return 0 ; }/**
@@ -335,25 +333,23 @@ int dynevent_arg_pair_add(struct dynevent_cmd *cmd,struct dynevent_arg_pair *arg_pair, dynevent_check_arg_fn_t check_arg) { - int ret = 0; - if (check_arg) { - ret =check_arg (arg_pair); + int ret = check_arg(arg_pair);if (ret)return ret; } - ret =seq_buf_printf (&cmd->seq," %s%c%s%c" , arg_pair->lhs, - arg_pair->operator, arg_pair->rhs, - arg_pair->separator); -if (ret) { +prt_printf (&cmd->seq," %s%c%s%c" , arg_pair->lhs, + arg_pair->operator, arg_pair->rhs, + arg_pair->separator); +if (printbuf_overflowed (&cmd->seq)) {pr_err ("field string is too long: %s%c%s%c\n" , arg_pair->lhs, arg_pair->operator, arg_pair->rhs, arg_pair->separator);return -E2BIG ; } -return ret; +return 0 ; }/**
@@ -368,15 +364,13 @@ int dynevent_arg_pair_add(struct dynevent_cmd *cmd,*/ int dynevent_str_add (struct dynevent_cmd *cmd,const char *str) { -int ret =0 ; - - ret =seq_buf_puts (&cmd->seq, str); -if (ret) { +prt_str (&cmd->seq, str); +if (printbuf_overflowed (&cmd->seq)) {pr_err ("String is too long: %s\n" , str);return -E2BIG ; } -return ret; +return 0 ; }/**
@@ -405,7 +399,7 @@ void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen, {memset (cmd,'\0' ,sizeof (*cmd)); -seq_buf_init (&cmd->seq, buf, maxlen); + cmd->seq =PRINTBUF_EXTERN (buf, maxlen); cmd->type = type; cmd->run_command = run_command; }
kernel/trace/trace_events_filter.c +1 -1
@@ -1059,7 +1059,7 @@ static void append_filter_err(struct trace_array *tr, FILT_ERR_ERRNO, 0); }trace_seq_putc (s,0 ); - buf =kmemdup_nul (s->buffer, s->seq.len,GFP_KERNEL ); + buf =kstrdup (printbuf_str (&s->seq),GFP_KERNEL );if (buf) {kfree (filter->filter_string); filter->filter_string = buf;
kernel/trace/trace_events_synth.c +1 -1
@@ -1495,7 +1495,7 @@ static int synth_event_run_command(struct dynevent_cmd *cmd)struct synth_event *se;int ret; - ret =create_or_delete_synth_event (cmd->seq.buffer); + ret =create_or_delete_synth_event (cmd->seq.buf);if (ret)return ret;
kernel/trace/trace_functions_graph.c +3 -3
@@ -1022,9 +1022,9 @@ print_graph_comment(struct trace_seq *s, struct trace_entry *ent, goto out;/* Strip ending newline */ -if (s->buffer[s->seq.len -1 ] =='\n' ) { - s->buffer[s->seq.len -1 ] ='\0' ; - s->seq.len--; +if (s->buffer[s->seq.pos -1 ] =='\n' ) { + s->buffer[s->seq.pos -1 ] ='\0' ; + s->seq.pos--; }trace_seq_puts (s," */\n" );
kernel/trace/trace_kprobe.c +1 -1
@@ -919,7 +919,7 @@ static int create_or_delete_trace_kprobe(const char *raw_command)static int trace_kprobe_run_command (struct dynevent_cmd *cmd) { -return create_or_delete_trace_kprobe (cmd->seq.buffer); +return create_or_delete_trace_kprobe (printbuf_str (&cmd->seq)); }/**
kernel/trace/trace_seq.c +57 -54
261 lines not on this page · raw message
Signature · 1 line
2.35.1
Steven Rostedt
maintainerRe: [PATCH v5 31/32] tracing: Convert to printbuf
Quoted 23 lines · On Mon, 8 Aug 2022 03:41:27 +0100 "Matthew Wilcox (Oracle)"…
kernel/trace/trace.c +1
-13@@ -9826,20 +9821,8 @@ static struct notifier_block trace_die_notifier = {void trace_printk_seq (struct trace_seq *s) { -/* Probably should print a warning here. */ -if (s->seq.len >=TRACE_MAX_PRINT ) - s->seq.len =TRACE_MAX_PRINT ; - -/* -* More paranoid code. Although the buffer size is set to -* PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just -* an extra layer of protection. -*/ -if (WARN_ON_ONCE (s->seq.len >= s->seq.size)) - s->seq.len = s->seq.size -1 ; -/* should be zero ended, but we are paranoid. */ - s->buffer[s->seq.len] =0 ; +printbuf_nul_terminate (&s->seq);printk (KERN_TRACE "%s" , s->buffer);
Quoted 1 line
diff --g
Please remove the two tracing patches and the deletion of the seq_buf from the series.
Thank you.
-- Steve
Re: [PATCH v5 31/32] tracing: Convert to printbuf
Quoted 24 lines · Steven Rostedt
Quoted 21 lines · On Mon, 8 Aug 2022 03:41:27 +0100 "Matthew Wilcox (Oracle)"…
kernel/trace/trace.c +1
-13@@ -9826,20 +9821,8 @@ static struct notifier_block trace_die_notifier = {void trace_printk_seq (struct trace_seq *s) { -/* Probably should print a warning here. */ -if (s->seq.len >=TRACE_MAX_PRINT ) - s->seq.len =TRACE_MAX_PRINT ; - -/* -* More paranoid code. Although the buffer size is set to -* PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just -* an extra layer of protection. -*/ -if (WARN_ON_ONCE (s->seq.len >= s->seq.size)) - s->seq.len = s->seq.size -1 ; -/* should be zero ended, but we are paranoid. */ - s->buffer[s->seq.len] =0 ; +printbuf_nul_terminate (&s->seq);
Quoted 6 lines
Quoted 3 lines
kernel/trace/trace.c +0
-0printk (KERN_TRACE "%s" , s->buffer);diff --g
Please remove the two tracing patches and the deletion of the
seq_buffrom the series.
Well, that's not really an option, as Christoph already (rightly) pointed out.
If you've got actual engineering concerns that you'd care to articulate I'd (still) like to try to work with you - otherwise, I don't think this is something I can accommodate you on.
Steven Rostedt
maintainerRe: [PATCH v5 31/32] tracing: Convert to printbuf
Quoted 5 lines · On Sun, 7 Aug 2022 23:32:01 -0400 Kent Overstreet <[email protected]>…
Quoted 2 lines
Please remove the two tracing patches and the deletion of the
seq_buffrom the series.Well, that's not really an option, as Christoph already (rightly) pointed out.
These are the last patches of the series. There's no dependency on them. You should be able to simply drop them. If others find that these patches are worth their while then by all means, let them have them.
Quoted 4 lines
If you've got actual engineering concerns that you'd care to articulate I'd (still) like to try to work with you - otherwise, I don't think this is something I can accommodate you on.
Here's my technical reason. These are non trivial changes that are replacing code that has been stable for 8 years that the tracing infrastructure highly depends on. I do not have the time to sit down and review this code as it is not a priority. My time is extremely limited (as my wife keeps complaining to me about, as I'm not spending enough time with the family).
This change is likely to cause subtle regressions for no benefit to the tracing subsystem. Hence, when it comes to risk vs reward, I see none.
I'm not NACKing the series, just the changes to the tracing subsystem.
When this code is mature enough, I may reconsider my stance on this.
-- Steve
Re: [PATCH v5 31/32] tracing: Convert to printbuf
Quoted 18 lines · Steven Rostedt
Quoted 5 lines · On Sun, 7 Aug 2022 23:32:01 -0400 Kent Overstreet <[email protected]>…
Quoted 2 lines
Please remove the two tracing patches and the deletion of the
seq_buffrom the series.Well, that's not really an option, as Christoph already (rightly) pointed out.
These are the last patches of the series. There's no dependency on them. You should be able to simply drop them. If others find that these patches are worth their while then by all means, let them have them.
Quoted 4 lines
If you've got actual engineering concerns that you'd care to articulate I'd (still) like to try to work with you - otherwise, I don't think this is something I can accommodate you on.
Quoted 8 lines
Here's my technical reason. These are non trivial changes that are replacing code that has been stable for 8 years that the tracing infrastructure highly depends on. I do not have the time to sit down and review this code as it is not a priority. My time is extremely limited (as my wife keeps complaining to me about, as I'm not spending enough time with the family).
This change is likely to cause subtle regressions for no benefit to the tracing subsystem. Hence, when it comes to risk vs reward, I see none.
It sounds like you're saying you don't have time to maintain your subsystem..? Is there anyone else actively co-maintaining tracing? Part of our jobs is bringing new people in and training them (and not providing a hostile work environment so they'll want to), maybe something to think about.
I'm also not seeing the likelihood of subtle regressions - this isn't my first kernel refactoring and not _nearly_ the biggest or the most invasive. There's definitely some stuff in the tracing code code that is a bit on the unorthodox side, but nothing too crazy. The code's been in my tree for ages where I use tracing on a daily basis, and it passes your test suite (and there was just one bug that made it through to be caught by the tests, as I mentioned in the cover letter).
Anyways, if you've got specific, actionable concerns, I'll be happy to take a look. Otherwise... ¯\_(ツ)_/¯
Cheers
Steven Rostedt
maintainerRe: [PATCH v5 31/32] tracing: Convert to printbuf
Quoted 8 lines · On Mon, 8 Aug 2022 11:15:31 -0400 Kent Overstreet <[email protected]>…
Quoted 2 lines
This change is likely to cause subtle regressions for no benefit to the tracing subsystem. Hence, when it comes to risk vs reward, I see none.
It sounds like you're saying you don't have time to maintain your subsystem..? Is there anyone else actively co-maintaining tracing? Part of our jobs is bringing new people in and training them (and not providing a hostile work environment so they'll want to), maybe something to think about.
No, it sounds like there's nothing here I need. Why do I need to review any code that is not going to improve my subsystem?
Quoted 11 lines
I'm also not seeing the likelihood of subtle regressions - this isn't my first kernel refactoring and not _nearly_ the biggest or the most invasive. There's definitely some stuff in the tracing code code that is a bit on the unorthodox side, but nothing too crazy. The code's been in my tree for ages where I use tracing on a daily basis, and it passes your test suite (and there was just one bug that made it through to be caught by the tests, as I mentioned in the cover letter).
Anyways, if you've got specific, actionable concerns, I'll be happy to take a look. Otherwise... ¯\_(ツ)_/¯
Like I said. I don't see the improvement. I hate changes for changes sake alone. If there was a real improvement to the system, then I would make the time to look at it. But currently, the only thing I get is that you want this code in. And that's not a high enough bar.
As I stated before, and have given talks about. Changes are pulled into Linux, they are never pushed.
-- Steve
PATCH v5 32/32 Delete seq_buf
In reply to Matthew Wilcox (Oracle)From: Kent Overstreet <[email protected]>
All users have now been converted to printbufs.
--- include/linux/seq_buf.h | 162 ---------------- lib/Makefile | 2 +- lib/seq_buf.c | 397 ---------------------------------------- 3 files changed, 1 insertion(+), 560 deletions(-) delete mode 100644 include/linux/seq_buf.h delete mode 100644 lib/seq_buf.c
include/linux/seq_buf.h deleted +0 -162
@@ -1,162 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _LINUX_SEQ_BUF_H -#define _LINUX_SEQ_BUF_H - -#include <linux/fs.h> - -/* -* Trace sequences are used to allow a function to call several other functions -* to create a string of data to use. -*/ - -/** -* seq_buf - seq buffer structure -* @buffer: pointer to the buffer -* @size: size of the buffer -* @len: the amount of data inside the buffer -* @readpos: The next position to read in the buffer. -*/ -struct seq_buf { -char *buffer; -size_t size; -size_t len; -loff_t readpos; -}; - -static inline void seq_buf_clear (struct seq_buf *s) -{ - s->len =0 ; - s->readpos =0 ; -} - -static inline void -seq_buf_init (struct seq_buf *s,char *buf,unsigned int size) -{ - s->buffer = buf; - s->size = size; -seq_buf_clear (s); -} - -/* -* seq_buf have a buffer that might overflow. When this happens -* the len and size are set to be equal. -*/ -static inline bool -seq_buf_has_overflowed (struct seq_buf *s) -{ -return s->len > s->size; -} - -static inline void -seq_buf_set_overflow (struct seq_buf *s) -{ - s->len = s->size +1 ; -} - -/* -* How much buffer is left on the seq_buf? -*/ -static inline unsigned int -seq_buf_buffer_left (struct seq_buf *s) -{ -if (seq_buf_has_overflowed (s)) -return 0 ; - -return s->size - s->len; -} - -/* How much buffer was written? */ -static inline unsigned int seq_buf_used (struct seq_buf *s) -{ -return min (s->len, s->size); -} - -/** -* seq_buf_terminate - Make sure buffer is nul terminated -* @s: the seq_buf descriptor to terminate. -* -* This makes sure that the buffer in @s is nul terminated and -* safe to read as a string. -* -* Note, if this is called when the buffer has overflowed, then -* the last byte of the buffer is zeroed, and the len will still -* point passed it. -* -* After this function is called, s->buffer is safe to use -* in string operations. -*/ -static inline void seq_buf_terminate (struct seq_buf *s) -{ -if (WARN_ON (s->size ==0 )) -return ; - -if (seq_buf_buffer_left (s)) - s->buffer[s->len] =0 ; -else - s->buffer[s->size -1 ] =0 ; -} - -/** -* seq_buf_get_buf - get buffer to write arbitrary data to -* @s: the seq_buf handle -* @bufp: the beginning of the buffer is stored here -* -* Return the number of bytes available in the buffer, or zero if -* there's no space. -*/ -static inline size_t seq_buf_get_buf (struct seq_buf *s,char **bufp) -{ -WARN_ON (s->len > s->size +1 ); - -if (s->len < s->size) { - *bufp = s->buffer + s->len; -return s->size - s->len; - } - - *bufp =NULL ; -return 0 ; -} - -/** -* seq_buf_commit - commit data to the buffer -* @s: the seq_buf handle -* @num: the number of bytes to commit -* -* Commit @num bytes of data written to a buffer previously acquired -* by seq_buf_get. To signal an error condition, or that the data -* didn't fit in the available space, pass a negative @num value. -*/ -static inline void seq_buf_commit (struct seq_buf *s,int num) -{ -if (num <0 ) { -seq_buf_set_overflow (s); - }else { -/* num must be negative on overflow */ -BUG_ON (s->len + num > s->size); - s->len += num; - } -} - -extern __printf(2, 3) -int seq_buf_printf(struct seq_buf *s, const char *fmt, ...); -extern __printf(2, 0) -int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args); -extern int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s); -extern int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, - int cnt); -extern int seq_buf_puts(struct seq_buf *s, const char *str); -extern int seq_buf_putc(struct seq_buf *s, unsigned char c); -extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len); -extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, - unsigned int len); -extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc); -extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, - int prefix_type, int rowsize, int groupsize, - const void *buf, size_t len, bool ascii); - -#ifdef CONFIG_BINARY_PRINTF -extern int -seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary); -#endif - -#endif /* _LINUX_SEQ_BUF_H */
lib/Makefile +1 -1
@@ -32,7 +32,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \ idr.o extable.o irq_regs.o argv_split.o \ flex_proportions.o ratelimit.o show_mem.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ - earlycpio.o seq_buf.o siphash.o dec_and_lock.o \ + earlycpio.o siphash.o dec_and_lock.o \ nmi_backtrace.o nodemask.o win_minmax.o memcat_p.o \ buildid.o cpumask.o printbuf.o
lib/seq_buf.c deleted +0 -397
397 lines not on this page · raw message
Signature · 1 line
2.35.1
Petr Mladek
maintainerQuoted 43 lines · Matthew Wilcox (Oracle)
[all this code is from Kent, I'm helping him out because he's having trouble with git send-email and OAUTH]
This should (hopefully) be the final iteration of this patch series.
Previous discussion:
https://lore.kernel.org/linux-mm/[email protected]/Changes since v4:
- %pf(%p) format extension has been dropped for now, since it turned out to be a bit controversial. I think we're going to want it, but I'm leaving it for a future patch series.
- There was a small off by one error in the patch convertingtrace_events_synth. The original code usingseq_bufhad to calculate the size of the string to allocate; since printbufs have native support for heap allocating strings, the version of the patch in this series just uses that for a nice cleanup.What are printbufs, and why?
============================Printbufs are like the existing
seq_buf, with some differences and new features:- Heap allocation (optional)
Currently, we have no standard mechanism for printing to a heap-allocated
string: code that needs to do this must calculate the size of the buffer to allocate, which is tedious and error prone. IOW, this is a major ergonomic improvement.- Indent level
Any time we're printing structured multi-line data, proper indenting makes the output considerably more readable. Printbufs have state for the current indent level, controlled by
printbuf_indent_add()andprintbuf_indent_sub()and obeyed byprt_newline().In the future this may work with just \n, pending work to do this without performance regressions.
- Tab stops
Quoted 8 lines
Printing reports with nicely aligned columns is something we do a _lot_, and printbufs make this a lot easier. After setting tabstops (byte offsets from start of line),
prt_tab()will output spaces up to the next tabstop, andpr_tab_rjust()will right justify output since the previous output against the next tabstap.In the future this may work with just \t, pending work to do this without performance regressions.
The more I think about this patchset the more doubts I have.
My first reaction was rather positive because
1. __prt_char(out, c)
looks more safe and sane than repeating the following code pattern in vsprintf code
if (buf < end)
*buf = '0';
++buf;
2. printk("%pf", meaningful_function_name(ptr))
is more user friendly, extendable. With a type check, it might be even more safe than
printk("%pxyz", ptr);3. pretty print API would allow to avoid some tricky use of temporary buffers in vsprintf code, for example, see
https://lore.kernel.org/r/[email protected]
What are the concerns?
It seems that the motivation for the pretty print is to allow printing more pretty/fancy multi-line debug messages. The API should be usable inside vsprintf.c and also outside.
1. vsprintf() is very important core API. It is used (not only for) when kernel wants to provide a human readable feedback, for example, via printk(), trace_printk(), procfs, sysfs, debugfs.
If a bug in vsprintf() blocks printk()/trace_printk() then crash_dump might be the only way to debug the kernel.
2. My experience with printk() is that external APIs are big source of problems. Some of them are even solved by hacks, for example:
+ Console drivers have to check oops_in_progress before taking port->lock to prevent a deadlock.
+ printk_deferred() or printk_once() have to be used by code that might be called by printk().
This patchset adds another external API.
The %pf feature allows writing crazy callbacks called inside vsprintf()/printk() without any proper review and self-tests.
People would want to extend the pretty print API for a profs/sysfs/debugfs use-case. They would take it easily. There is a lower risk in this case because only a particular file is affected, the API is called in a well defined context. But it looks like a call for problems if we allow to call the same complicated code also from vsprintf() or printk() in any context.
3. Features usually complicate the code immediately or later.
For example, the record based ring buffer complicated the printk() code a lot. It introduced many regressions. Development of the lockless variant was a real challenge. And it did not solve everything. Peple still complain that pr_cont() is not reliable. Multi-line output might be mixed, ...
The pretty print API is actually designed for multi-line output. But it will not help when used with printk() that uses 1k buffers internally. And adding support for "unlimited" printk() messages would be another challenge. It would bring new problems, for example, one printk() caller might block others for too long, ...
Any opinion is really appreciated.
Best Regards,
Petr
PS: I am sorry that I did not react on this patchset for months. I was
overloaded, sick twice, and had vacation.
A more detailed review of the patchset would help me to have stronger opinion about it. I am not clever and experienced enough to see all the consequences on the first look.
Quoted 10 lines · Petr Mladek
It seems that the motivation for the pretty print is to allow printing more pretty/fancy multi-line debug messages. The API should be usable inside
vsprintf.cand also outside.1.
vsprintf()is very important core API. It is used (not only for) when kernel wants to provide a human readable feedback, for example, viaprintk(),trace_printk(), procfs, sysfs, debugfs.If a bug in
vsprintf()blocks printk()/trace_printk() thencrash_dumpmight be the only way to debug the kernel.
All the more reason for that code to be written with safe APIs, wouldn't you say?
Quoted 10 lines
2. My experience with
printk()is that external APIs are big source of problems. Some of them are even solved by hacks, for example:+ Console drivers have to check
oops_in_progressbefore takingport->lockto prevent a deadlock.+
printk_deferred()orprintk_once()have to be used by code that might be called byprintk().This patchset adds another external API.
We should differentiate between sprintf and printk.
sprintf is _just_ for formatting strings, it doesn't have the same concerns as printk re: locking the output buffer - or any locking concerns whatsover, it's a pure function that doesn't mutate outside system state.
This patch series is about introducing a safe common API for sprintf and other code that outputs to strings, and I'd also note that even Linus agreed on the need for such an API, because it gets rid of the separate stack-allocated buffers that have been a problem in the past.
There's no impact on printk. The extra feature that printbufs add - heap allocation - isn't used by printk.
Quoted 2 lines
The %pf feature allows writing crazy callbacks called inside vsprintf()/printk() without any proper review and self-tests.
I came to similar conclusions about %pf after the discussion with Linus; that part has been dropped from the patch set for now.
I do think we can do it safely in the future, and in the meantime it _may_ be worthwhile for use in a much more limited - probably not, I'd rather hold off and do it right. But since it hasn't been in the patch set the last few times I posted it, let's leave this out of the discussion.
(I think the way to make it safe will be to output to a heap allocated buffer instead of the printk buffer directly; however, that's not an option currently because we'd need to plumb through a gfp flags parameter and obviously we're not going to do that to printk. However, we're looking at switching from gfp flags to the memalloc_*() API, which would make this work).
Quoted 7 lines
People would want to extend the pretty print API for a profs/sysfs/debugfs use-case. They would take it easily. There is a lower risk in this case because only a particular file is affected, the API is called in a well defined context. But it looks like a call for problems if we allow to call the same complicated code also from
vsprintf()orprintk()in any context.
Since %pf isn't getting added, this isn't a concern.
_But_, introducing a common API so that we can use the same code for outputting to the system console, or procfs, or debugfs, is precisely what this is all about! We've got a fair amount of code duplication (some of which this patch series addresses; see the hexdump patches) because of this lack of a common API - often messily, with subtle differences for absolutely no reason.
We _want_ common, reusable, generic code.
Without %pf, to use printbuf code for outputting to the console the standard approach is going to be something like
struct printbuf buf = PRINTBUF;
prt_foo(&buf, foo);printk("%s\n", buf.buf);printbuf_exit(&buf);
Obviously, this isn't the greatest ergonomic-wise, %pf would be better in that respect. But this is still quite a bit better than completely duplicated code - one for outputting to a seq_buf, another set for seq_file, another for printk()...
I also have a patch series in the works for a printbuf based replacement for seq_file - eliminating that API fragmentation as well. I've just been waiting for movement on this patch series...
Quoted 7 lines
3. Features usually complicate the code immediately or later.
For example, the record based ring buffer complicated the
printk()code a lot. It introduced many regressions. Development of the lockless variant was a real challenge. And it did not solve everything. Peple still complain thatpr_cont()is not reliable. Multi-line output might be mixed, ...
I'm not seeing the complications this patch series introduces in its current form. There's heap allocation, which replaces open-coded output buffer allocation that's currently done all over the place. And there's tabstops and indenting; given the amount of columnar data presented in procfs and debugfs, I think those are reasonable additions.
In return, you get a whole ton of code that was previously using raw pointer arithmetic converted to safe APIs, and we have a start on standardizing a lot of different code on a common API.
And, the sprintf code ends up a whole lot more readable and easier to work on.
Quoted 5 lines
The pretty print API is actually designed for multi-line output. But it will not help when used with
printk()that uses 1k buffers internally. And adding support for "unlimited"printk()messages would be another challenge. It would bring new problems, for example, oneprintk()caller might block others for too long, ...
I'm not seeing that this is a real difficulty, since we're not doing %pf at this time.
- add a printk_string_as_lines(), which takes a string buffer (not a
format string) and calls printk() on it one line at a time. This would be an easy way around the current 1k buffer.
- or, if the printk code wants to keep the output lines together and
not have them interspersed with other output, that should be easier when we're presenting you the output already formatted and all you need to do is memcpy().
Basically, we've got broad agreement that calling arbitrary pretty-printers from printk() context is a bad idea, so since we won't be doing that this looks like a non-issue to me as well.
Quoted 3 lines
A more detailed review of the patchset would help me to have stronger opinion about it. I am not clever and experienced enough to see all the consequences on the first look.
There's really not a lot in the way of functional changes, since %pf has been dropped - it's just refactoring and converting to common APIs, and it was pretty mechanical as far as refactorings go.
Besides what we've talked about, the other thing I was doing that might be worth discussing was working on separating the pretty-printers in vsprintf.c from the format string parsing code. What I was seeing was a lot of code mixing parsing of the format string, and I think that code would be _much_ easier to read and work on with the format string parsing confined to pointer(), and having it call pretty-printers with a normal function call interface with well-typed arguments.
I have additional patches finishing this work for around half of the pretty-printers in vsprintf.c, but you were complaining about the patch series growing, so I haven't posted them yet...
Anyways, I hope this addresses some concerns.
Cheers,
Kent