Re: [PATCH next v3 12/15] printk: introduce a kmsg_dump iterator
From: John Ogness <john.ogness@linutronix.de>
Date: 2021-03-02 15:57:30
Also in:
linux-um, linuxppc-dev, lkml
On 2021-03-01, Petr Mladek [off-list ref] wrote:
quoted
diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c index 532f22637783..5a64b24a91c2 100644 --- a/arch/powerpc/kernel/nvram_64.c +++ b/arch/powerpc/kernel/nvram_64.c@@ -72,8 +72,7 @@ static const char *nvram_os_partitions[] = { NULL }; -static void oops_to_nvram(struct kmsg_dumper *dumper, - enum kmsg_dump_reason reason); +static void oops_to_nvram(enum kmsg_dump_reason reason); static struct kmsg_dumper nvram_kmsg_dumper = { .dump = oops_to_nvram@@ -642,11 +641,11 @@ void __init nvram_init_oops_partition(int rtas_partition_exists) * that we think will compress sufficiently to fit in the lnx,oops-log * partition. If that's too much, go back and capture uncompressed text. */ -static void oops_to_nvram(struct kmsg_dumper *dumper, - enum kmsg_dump_reason reason) +static void oops_to_nvram(enum kmsg_dump_reason reason) { struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; static unsigned int oops_count = 0; + static struct kmsg_dump_iter iter; static bool panicking = false; static DEFINE_SPINLOCK(lock); unsigned long flags;@@ -681,13 +680,14 @@ static void oops_to_nvram(struct kmsg_dumper *dumper, return; if (big_oops_buf) { - kmsg_dump_get_buffer(dumper, false, + kmsg_dump_rewind(&iter);It would be nice to get rid of the kmsg_dump_rewind(&iter) calls in all callers. A solution might be to create the following in include/linux/kmsg_dump.h #define KMSG_DUMP_ITER_INIT(iter) { \ .cur_seq = 0, \ .next_seq = U64_MAX, \ } #define DEFINE_KMSG_DUMP_ITER(iter) \ struct kmsg_dump_iter iter = KMSG_DUMP_ITER_INIT(iter)
For this caller (arch/powerpc/kernel/nvram_64.c) and for (kernel/debug/kdb/kdb_main.c), kmsg_dump_rewind() is called twice within the dumper. So rewind will still be used there.
Then we could do the following at the beginning of both kmsg_dump_get_buffer() and kmsg_dump_get_line(): u64 clear_seq = latched_seq_read_nolock(&clear_seq); if (iter->cur_seq < clear_seq) cur_seq = clear_seq;
I suppose we need to add this part anyway, if we want to enforce that records before @clear_seq are not to be available for dumpers.
I am not completely sure about next_seq:
+ kmsg_dump_get_buffer() will set it for the next call anyway.
It reads the blocks of messages from the newest.
+ kmsg_dump_get_line() wants to read the entire buffer anyway.
But there is a small risk of an infinite loop when new messages
are printed when dumping each line.
It might be better to avoid the infinite loop. We could do the following:
static void check_and_set_iter(struct kmsg_dump_iter)
{
if (iter->cur_seq == 0 && iter->next_seq == U64_MAX) {
kmsg_dump_rewind(iter);
}
and call this at the beginning of both kmsg_dump_get_buffer()
and kmsg_dump_get_line()
What do you think?
On a technical level, it does not make any difference. It is pure
cosmetic.
Personally, I prefer the rewind directly before the kmsg_dump_get calls
because it puts the initializer directly next to the user.
As an example to illustrate my view, I prefer:
for (i = 0; i < n; i++)
...;
instead of:
int i = 0;
...
for (; i < n; i++)
...;
Also, I do not really like the special use of 0/U64_MAX to identify
special actions of the kmsg_dump_get functions.
Note that I do not resist on it. But it might make the API easier to use from my POV.
Since you do not resist, I will keep the API the same for v4. But I will add the @clear_seq check to the kmsg_dump_get functions. John Ogness