From: John Ogness <john.ogness@linutronix.de>
To: Sreenath Vijayan <sreenath.vijayan@sony.com>,
corbet@lwn.net, gregkh@linuxfoundation.org, jirislaby@kernel.org,
rdunlap@infradead.org, pmladek@suse.com
Cc: rostedt@goodmis.org, senozhatsky@chromium.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-serial@vger.kernel.org, taichi.shimoyashiki@sony.com,
daniel.palmer@sony.com, anandakumar.balasubramaniam@sony.com,
sreenath.vijayan@sony.com
Subject: Re: [PATCH v3 1/2] printk: Add function to dump printk buffer directly to consoles
Date: Thu, 18 Jan 2024 10:55:20 +0106 [thread overview]
Message-ID: <87le8nas4f.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <402f0cbc3a573503c7cc794113aa5137ed7f276c.1705331453.git.sreenath.vijayan@sony.com>
On 2024-01-17, Sreenath Vijayan <sreenath.vijayan@sony.com> wrote:
> It is useful to be able to dump the printk buffer directly to
> consoles in some situations so as to not flood the buffer.
> This needs access to private items of printk like PRINTK_MESSAGE_MAX.
> Add function in printk.c to accomplish this.
>
> Suggested-by: John Ogness <john.ogness@linutronix.de>
> Signed-off-by: Sreenath Vijayan <sreenath.vijayan@sony.com>
> Signed-off-by: Shimoyashiki Taichi <taichi.shimoyashiki@sony.com>
> ---
> include/linux/printk.h | 4 ++++
> kernel/printk/printk.c | 33 +++++++++++++++++++++++++++++++++
> 2 files changed, 37 insertions(+)
>
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index 8ef499ab3c1e..0896745f31e2 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -192,6 +192,7 @@ void show_regs_print_info(const char *log_lvl);
> extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
> extern asmlinkage void dump_stack(void) __cold;
> void printk_trigger_flush(void);
> +void dump_printk_buffer(void);
> #else
> static inline __printf(1, 0)
> int vprintk(const char *s, va_list args)
> @@ -271,6 +272,9 @@ static inline void dump_stack(void)
> static inline void printk_trigger_flush(void)
> {
> }
> +static inline void dump_printk_buffer(void)
> +{
> +}
> #endif
>
> #ifdef CONFIG_SMP
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index f2444b581e16..5b11fb377f8f 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -4259,6 +4259,39 @@ void kmsg_dump_rewind(struct kmsg_dump_iter *iter)
> }
> EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
>
> +/**
> + * Dump the printk ring buffer directly to consoles
> + */
> +void dump_printk_buffer(void)
> +{
> + struct kmsg_dump_iter iter;
> + struct console *con;
> + char *buf;
> + size_t len;
> + int cookie;
> +
> + buf = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL);
> + if (!buf)
> + return;
> +
> + kmsg_dump_rewind(&iter);
> + while (kmsg_dump_get_line(&iter, 1, buf, PRINTK_MESSAGE_MAX, &len)) {
Although using the kmsg_dump interface will provide you the messages,
they will not necessarily be in the correct format. Consoles can be set
to use extended format.
We probably should respect that console setting.
> + /*
> + * Since using printk() or pr_*() will append the message to the
> + * printk ring buffer, they cannot be used to display the retrieved
> + * message. Hence console_write() of serial drivers is used.
> + */
> + console_lock();
> + cookie = console_srcu_read_lock();
> + for_each_console_srcu(con) {
> + if ((console_srcu_read_flags(con) & CON_ENABLED) && con->write)
console_is_usable() should be used instead. It makes the correct checks.
> + con->write(con, buf, len);
> + }
> + console_srcu_read_unlock(cookie);
> + console_unlock();
> + }
> + kfree(buf);
> +}
We could do something like this:
void dump_printk_buffer(void)
{
console_lock();
console_flush_on_panic(CONSOLE_REPLAY_ALL);
console_unlock();
}
This version respects all the console features (formatting, handovers),
but console_flush_on_panic() does not to allow cond_resched(), which we
would want in this case.
We could take the console sequence-resetting code out into its own
helper function. Then it would look like this (comments removed to keep
things short):
static void console_rewind_all(void)
{
struct console *c;
short flags;
int cookie;
u64 seq;
seq = prb_first_valid_seq(prb);
cookie = console_srcu_read_lock();
for_each_console_srcu(c) {
flags = console_srcu_read_flags(c);
if (flags & CON_NBCON)
nbcon_seq_force(c, seq);
else
c->seq = seq;
}
console_srcu_read_unlock(cookie);
}
void console_flush_on_panic(enum con_flush_mode mode)
{
bool handover;
u64 next_seq;
console_may_schedule = 0;
if (mode == CONSOLE_REPLAY_ALL)
console_rewind_all();
console_flush_all(false, &next_seq, &handover);
}
void dump_printk_buffer(void)
{
bool handover;
u64 next_seq;
console_lock();
console_rewind_all();
console_flush_all(true, &next_seq, &handover);
console_unlock();
}
Any thoughts?
John
next prev parent reply other threads:[~2024-01-18 9:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-17 10:12 [PATCH v3 0/2] Add support to dump printk buffer to console via sysrq Sreenath Vijayan
2024-01-17 10:12 ` [PATCH v3 1/2] printk: Add function to dump printk buffer directly to consoles Sreenath Vijayan
2024-01-18 9:49 ` John Ogness [this message]
2024-01-18 10:14 ` John Ogness
2024-01-20 8:41 ` Sreenath Vijayan
2024-01-17 11:13 ` [PATCH v3 2/2] tty/sysrq: Dump printk ring buffer messages via sysrq Sreenath Vijayan
2024-01-17 10:12 ` Sreenath Vijayan
2024-01-18 10:05 ` John Ogness
2024-01-18 22:56 ` David Laight
2024-01-19 5:47 ` gregkh
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=87le8nas4f.fsf@jogness.linutronix.de \
--to=john.ogness@linutronix.de \
--cc=anandakumar.balasubramaniam@sony.com \
--cc=corbet@lwn.net \
--cc=daniel.palmer@sony.com \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=pmladek@suse.com \
--cc=rdunlap@infradead.org \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=sreenath.vijayan@sony.com \
--cc=taichi.shimoyashiki@sony.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).