All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kent Overstreet <kent.overstreet@gmail.com>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, pmladek@suse.com
Cc: Kent Overstreet <kent.overstreet@gmail.com>,
	rostedt@goodmis.org, enozhatsky@chromium.org,
	linux@rasmusvillemoes.dk, willy@infradead.org
Subject: [PATCH v4 00/34] Printbufs - new data structure for building strings
Date: Sun, 19 Jun 2022 20:41:59 -0400	[thread overview]
Message-ID: <20220620004233.3805-1-kent.overstreet@gmail.com> (raw)

Previous discussions:
https://lore.kernel.org/all/20220419203202.2670193-1-kent.overstreet@gmail.com/
https://lore.kernel.org/all/20220519172421.162394-1-kent.overstreet@gmail.com/
https://lore.kernel.org/all/20220604193042.1674951-1-kent.overstreet@gmail.com/

Git repo:
https://evilpiepirate.org/git/bcachefs.git/log/?h=printbuf_v4

Changes since v3:
  Bugfixes and performance improvements, the latest iteration of this patch
  series has been baking in the bcachefs tree and that shook out some bugs.

  Rasmus pointed out that -fno-strict-aliasing is going to cause gcc to generate
  nasty code, and indeed it unfortunately does but according to worst case
  scenario microbenchmarks it's not a problem for actual performance. Using
  memcpy() and memset() in the printbuf helpers _was_ a problem for performance,
  so that's been fixed.

-----------

Core idea: Wouldn't it be nice if we had a common data structure and calling
convention for outputting strings?

The core concept this patch series is aimed at cleaning up and standardizing is
that of a "pretty-printer", which is now a function like prt_foo() or
foo_to_text():

    void foo_to_text(struct printbuf *out, struct foo)

What this patch series does or enables:

 - It becomes quite a bit easier to write composable pretty printers! This is
   huge.

 - A ton of code that works in terms of raw char * pointers and lengths
   (snprintf style, and many weird variations) gets cleaned up, with error prone
   raw pointers arithmetic replaced by proper helpers

 - A ton of code that emits either directly via printk() or to other places
   (sysfs, debugfs) can now output to printbufs, and becomes more reusable and
   composable

 - Countesy of Matthew Wilcox, the new and very cool %pf() format string, which
   allows passing a pretty printer function and its arguments to sprintf() and
   family. This means we can now call type specific pretty-printers without
   adding them to lib/vsprintf.c and writing a bunch of crazy
   parsing-and-dispatch code. For example,

     printk("%pd", dentry);

   becomes

     printk("%pf(%p)", prt_dentry, dentry);

   My OOM debugging & reporting patch series that builds off of this uses this
   to solve a very real problem that Michal Hocko brought up at LSF - with this
   we write shrinkers_to_text(), slab_to_text() which can _also_ now be used for
   reporting in debugfs (which Roman has been working on), as well as in the
   show_mem() report - the "%pf()" syntax lets us print the output of those
   functions without allocating (and having to preallocate) a separate buffer.

 - Some new formatting helpers:
   
   Nicely aligned text is much easier to read, and something that we want a
   _lot, but outputting nicely aligned text with printf() is a pain in the ass.
   Printbufs add tabstops, which can be used for right or left justification -
   simple, easy. prt_tab() emits spaces up to the next tabstop, prt_tab_rjust()
   advances to the next tabstop right justifying text since the previous
   tabstop.

   Printbufs also add an indent level, obeyed by prt_newline() which can be very
   useful for multi line output.

   In the future, \n and \t in format strings may learn to obey these as well.

 - Optional heap allocation - no need to statically allocate buffers on the
   stack and guess at the output size.

 - Lots of consolidating and refactoring
   
   This series replaces seq_buf, which does basically what an earlier version of
   printbufs did.

   A good chunk of lib/string_helpers.c, as well as lib/hexdump.c are converted
   (and simplified!).

   Pretty printers in lib/vsprintf.c previously outputted to buffers on the
   stack and then copied _that_ to the actual output buffer, that's all gone
   (replaced by proper helpers for outputting chars and strings), and they also
   used printf_spec for argument passing in ad-hoc ways. This patch series does
   a lot towards converting them to more standard pretty printers that can be
   called via %pf() instead of having to live in lib/vsprintf.c. Still to do:
   format string decoding for argument passing is a mess that's scattered all
   over the place.

In the course of working on this patch series, I've spotted a _lot_ more
consolidation and refactoring that needs to be done - we've got a ton of API
fragmentation leading to lots of code duplication.

But I'm already really excited about what this patch series enables.

Cheers!

Kent Overstreet (34):
  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
  vsprintf: %pf(%p)
  lib/string_helpers: string_get_size() now returns characters wrote
  lib/printbuf: Heap allocation
  lib/printbuf: Tabstops, indenting
  lib/printbuf: Unit specifiers
  lib/pretty-printers: prt_string_option(), prt_bitflags()
  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

 Documentation/core-api/printk-formats.rst |   22 +
 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/pretty-printers.h           |   10 +
 include/linux/printbuf.h                  |  253 +++
 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         |   32 +-
 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/pretty-printers.c                     |   60 +
 lib/printbuf.c                            |  253 +++
 lib/seq_buf.c                             |  397 -----
 lib/string_helpers.c                      |  224 +--
 lib/test_hexdump.c                        |   30 +-
 lib/test_printf.c                         |   33 +-
 lib/vsprintf.c                            | 1723 ++++++++++-----------
 mm/memcontrol.c                           |   68 +-
 tools/testing/nvdimm/test/ndtest.c        |   22 +-
 37 files changed, 2050 insertions(+), 1976 deletions(-)
 create mode 100644 include/linux/pretty-printers.h
 create mode 100644 include/linux/printbuf.h
 delete mode 100644 include/linux/seq_buf.h
 create mode 100644 lib/pretty-printers.c
 create mode 100644 lib/printbuf.c
 delete mode 100644 lib/seq_buf.c

-- 
2.36.1



             reply	other threads:[~2022-06-20  0:42 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-20  0:41 Kent Overstreet [this message]
2022-06-20  0:42 ` [PATCH v4 01/34] lib/printbuf: New data structure for printing strings Kent Overstreet
2022-06-20  4:44   ` David Laight
2022-06-20 15:30     ` Kent Overstreet
2022-06-20 15:53       ` David Laight
2022-06-20 16:14         ` Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 02/34] lib/string_helpers: Convert string_escape_mem() to printbuf Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 03/34] vsprintf: Convert " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 04/34] lib/hexdump: " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 05/34] vsprintf: %pf(%p) Kent Overstreet
2022-06-21  7:04   ` Rasmus Villemoes
2022-06-21  7:51     ` Kent Overstreet
2022-06-21  8:47       ` Rasmus Villemoes
2022-06-21 11:11     ` David Laight
2022-06-20  0:42 ` [PATCH v4 06/34] lib/string_helpers: string_get_size() now returns characters wrote Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 07/34] lib/printbuf: Heap allocation Kent Overstreet
2022-06-21  7:58   ` Rasmus Villemoes
2022-06-20  0:42 ` [PATCH v4 08/34] lib/printbuf: Tabstops, indenting Kent Overstreet
2022-06-21  8:14   ` Rasmus Villemoes
2022-06-20  0:42 ` [PATCH v4 09/34] lib/printbuf: Unit specifiers Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 10/34] lib/pretty-printers: prt_string_option(), prt_bitflags() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 11/34] vsprintf: Improve number() Kent Overstreet
2022-06-21  8:33   ` Rasmus Villemoes
2022-06-20  0:42 ` [PATCH v4 12/34] vsprintf: prt_u64_minwidth(), prt_u64() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 13/34] test_printf: Drop requirement that sprintf not write past nul Kent Overstreet
2022-06-21  7:19   ` Rasmus Villemoes
2022-06-21  7:52     ` Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 14/34] vsprintf: Start consolidating printf_spec handling Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 15/34] vsprintf: Refactor resource_string() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 16/34] vsprintf: Refactor fourcc_string() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 17/34] vsprintf: Refactor ip_addr_string() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 18/34] vsprintf: Refactor mac_address_string() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 19/34] vsprintf: time_and_date() no longer takes printf_spec Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 20/34] vsprintf: flags_string() " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 21/34] vsprintf: Refactor device_node_string, fwnode_string Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 22/34] vsprintf: Refactor hex_string, bitmap_string_list, bitmap_string Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 23/34] Input/joystick/analog: Convert from seq_buf -> printbuf Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 24/34] mm/memcontrol.c: Convert to printbuf Kent Overstreet
2022-06-20 11:37   ` Michal Hocko
2022-06-20 15:13     ` Kent Overstreet
2022-06-20 15:52       ` Michal Hocko
2022-06-20  0:42 ` [PATCH v4 25/34] clk: tegra: bpmp: " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 26/34] tools/testing/nvdimm: " Kent Overstreet
2022-06-24 19:32   ` Dan Williams
2022-06-24 23:42     ` Santosh Sivaraj
2022-07-01  6:32       ` Shivaprasad G Bhat
2022-06-20  0:42 ` [PATCH v4 27/34] powerpc: " Kent Overstreet
2022-06-20  0:42   ` Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 28/34] x86/resctrl: " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 29/34] PCI/P2PDMA: " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 30/34] tracing: trace_events_synth: " Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 31/34] d_path: prt_path() Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 32/34] ACPI/APEI: Add missing include Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 33/34] tracing: Convert to printbuf Kent Overstreet
2022-06-20  0:42 ` [PATCH v4 34/34] Delete seq_buf Kent Overstreet
2022-06-20  4:19 ` [PATCH v4 00/34] Printbufs - new data structure for building strings David Laight
2022-06-20  4:54   ` Matthew Wilcox
2022-06-20  8:00     ` David Laight
2022-06-20 15:07   ` Kent Overstreet
2022-06-20 15:21     ` David Laight
2022-06-21  0:38     ` Joe Perches
2022-06-21  0:57       ` Kent Overstreet
2022-06-21  1:26         ` Joe Perches
2022-06-21  2:10           ` Joe Perches
2022-06-26 19:53             ` [RFC[ Alloc in vsprintf Joe Perches
2022-06-26 20:06               ` Kent Overstreet
2022-06-26 20:13                 ` Joe Perches
2022-06-26 20:19               ` Linus Torvalds
2022-06-26 20:39                 ` Joe Perches
2022-06-26 20:51                   ` Kent Overstreet
2022-06-26 21:02                     ` Joe Perches
2022-06-26 21:10                       ` Kent Overstreet
2022-06-26 20:54                   ` Linus Torvalds
2022-06-27  8:25                 ` David Laight
2022-06-28  2:56                   ` Kent Overstreet
2022-06-21  2:31           ` [PATCH v4 00/34] Printbufs - new data structure for building strings Kent Overstreet
2022-06-21  3:11   ` Kent Overstreet
2022-06-21  6:11 ` Rasmus Villemoes
2022-06-21  8:01   ` Kent Overstreet
2022-07-19 23:15 ` Steven Rostedt
2022-07-19 23:43   ` Kent Overstreet
2022-07-20  0:05     ` Steven Rostedt
2022-07-20  0:17       ` Kent Overstreet
2022-07-20  1:11         ` Steven Rostedt
2022-07-20  1:31           ` Kent Overstreet
2022-07-20  1:37             ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220620004233.3805-1-kent.overstreet@gmail.com \
    --to=kent.overstreet@gmail.com \
    --cc=enozhatsky@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.