printk: remove logbuf_lock
Subsystems (8)
John Ogness
maintainerv4 cover printk: remove logbuf_lock
Hello,
Here is v4 of a series to remove @logbuf_lock, exposing the ringbuffer locklessly to both readers and writers. v3 is here [0].
Since @logbuf_lock was protecting much more than just the ringbuffer, this series clarifies and cleans up the various protections using comments, lockless accessors, atomic types, and a new finer-grained @syslog_lock.
Removing @logbuf_lock required changing the semantics of the kmsg_dumper callback in order to work locklessly. This series adjusts all kmsg_dumpers and users of the kmsg_dump_get_*() functions for the new semantics.
This series is based on next-20210303.
Changes since v3:
- disable interrupts in the arch/um kmsg_dumper
- reduce CONSOLE_LOG_MAX value from 4096 back to 1024 to revert the increasd 3KiB static memory footprint
- change the kmsg_dumper() callback prototype back to how it was because some dumpers need the registered object for container_of() usage
- for kmsg_dump_get_line()/kmsg_dump_get_buffer() restrict the minimal allowed sequence number to the cleared sequence number
John Ogness
[0] https://lore.kernel.org/lkml/[email protected]/
John Ogness (15):
um: synchronize kmsg_dumper
mtd: mtdoops: synchronize kmsg_dumper
printk: limit second loop of syslog_print_all
printk: kmsg_dump: remove unused fields
printk: refactor kmsg_dump_get_buffer()
printk: consolidate kmsg_dump_get_buffer/syslog_print_all code
printk: introduce CONSOLE_LOG_MAX
printk: use seqcount_latch for clear_seq
printk: use atomic64_t for devkmsg_user.seq
printk: add syslog_lock
printk: kmsg_dumper: remove @active field
printk: introduce a kmsg_dump iterator
printk: remove logbuf_lock
printk: kmsg_dump: remove _nolock() variants
printk: console: remove unnecessary safe buffer usage
arch/powerpc/kernel/nvram_64.c | 8 +- arch/powerpc/xmon/xmon.c | 6 +- arch/um/kernel/kmsg_dump.c | 13 +- drivers/hv/vmbus_drv.c | 4 +- drivers/mtd/mtdoops.c | 17 +- fs/pstore/platform.c | 5 +- include/linux/kmsg_dump.h | 47 ++-- kernel/debug/kdb/kdb_main.c | 10 +- kernel/printk/internal.h | 4 +- kernel/printk/printk.c | 464 +++++++++++++++++---------------- kernel/printk/printk_safe.c | 27 +- 11 files changed, 310 insertions(+), 295 deletions(-)
Signature · 6 lines
2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
John Ogness
maintainerPATCH v4 1/15 um: synchronize kmsg_dumper
The kmsg_dumper can be called from any context and CPU, possibly from multiple CPUs simultaneously. Since a static buffer is used to retrieve the kernel logs, this buffer must be protected against simultaneous dumping. Skip dumping if another context is already dumping.
--- arch/um/kernel/kmsg_dump.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/um/kernel/kmsg_dump.c +8 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/kmsg_dump.h> +#include <linux/spinlock.h> #include <linux/console.h> #include <linux/string.h> #include <shared/init.h>
@@ -9,8 +10,10 @@static void kmsg_dumper_stdout (struct kmsg_dumper *dumper,enum kmsg_dump_reason reason) { +static DEFINE_SPINLOCK (lock);static char line[1024 ];struct console *con; +unsigned long flags;size_t len =0 ;/* only dump kmsg when no console is available */
@@ -29,11 +32,16 @@ static void kmsg_dumper_stdout(struct kmsg_dumper *dumper,if (con)return ; +if (!spin_trylock_irqsave (&lock, flags)) +return ; +printf ("kmsg_dump:\n" );while (kmsg_dump_get_line (dumper, true, line,sizeof (line), &len)) { line[len] ='\0' ;printf ("%s" , line); } + +spin_unlock_irqrestore (&lock, flags); }static struct kmsg_dumper kmsg_dumper = {
Signature · 7 lines
2.20.1 _______________________________________________ linux-um mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-um
John Ogness
maintainerPATCH v4 2/15 mtd: mtdoops: synchronize kmsg_dumper
In reply to John OgnessThe kmsg_dumper can be called from any context and CPU, possibly from multiple CPUs simultaneously. Since the writing of the buffer can occur from a later scheduled work queue, the oops buffer must be protected against simultaneous dumping.
Use an atomic bit to mark when the buffer is protected. Release the protection in between setting the buffer and the actual writing in order for a possible panic (immediate write) to be written during the scheduling of a previous oops (delayed write).
An atomic bit (rather than a spinlock) was chosen so that no scheduling or preemption side-effects would be introduced. The MTD kmsg_dumper may dump directly or it may be delayed (via scheduled work). Depending on the context, different MTD callbacks are used. For example, mtd_write() expects to be called in a non-atomic context and may take a mutex.
--- drivers/mtd/mtdoops.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/mtd/mtdoops.c +11 -1
@@ -52,6 +52,7 @@ static struct mtdoops_context {int nextcount;unsigned long *oops_page_used; +unsigned long oops_buf_busy;void *oops_buf; } oops_cxt;
@@ -180,6 +181,9 @@ static void mtdoops_write(struct mtdoops_context *cxt, int panic)u32 *hdr;int ret; +if (test_and_set_bit (0 , &cxt->oops_buf_busy)) +return ; +/* Add mtdoops header to the buffer */ hdr = cxt->oops_buf; hdr[0 ] = cxt->nextcount;
@@ -190,7 +194,7 @@ static void mtdoops_write(struct mtdoops_context *cxt, int panic) record_size, &retlen, cxt->oops_buf);if (ret == -EOPNOTSUPP ) {printk (KERN_ERR "mtdoops: Cannot write from panic without panic_write\n" ); -return ; + goto out; } }else ret = mtd_write(mtd, cxt->nextpage * record_size,
@@ -203,6 +207,8 @@ static void mtdoops_write(struct mtdoops_context *cxt, int panic)memset (cxt->oops_buf,0xff , record_size);mtdoops_inc_counter (cxt); +out: +clear_bit (0 , &cxt->oops_buf_busy); }static void mtdoops_workfunc_write (struct work_struct *work)
@@ -276,8 +282,11 @@ static void mtdoops_do_dump(struct kmsg_dumper *dumper,if (reason ==KMSG_DUMP_OOPS && !dump_oops)return ; +if (test_and_set_bit (0 , &cxt->oops_buf_busy)) +return ;kmsg_dump_get_buffer (dumper, true, cxt->oops_buf +MTDOOPS_HEADER_SIZE , record_size -MTDOOPS_HEADER_SIZE ,NULL ); +clear_bit (0 , &cxt->oops_buf_busy);if (reason !=KMSG_DUMP_OOPS ) {/* Panics must be written immediately */
@@ -394,6 +403,7 @@ static int __init mtdoops_init(void)return -ENOMEM ; }memset (cxt->oops_buf,0xff , record_size); + cxt->oops_buf_busy =0 ;INIT_WORK (&cxt->work_erase, mtdoops_workfunc_erase);INIT_WORK (&cxt->work_write, mtdoops_workfunc_write);
Signature · 1 line
2.20.1
Re: [PATCH next v4 02/15] mtd: mtdoops: synchronize kmsg_dumper
Hello,
John Ogness <[email protected]> wrote on Wed, 3 Mar 2021 11:15:15 +0100:
Quoted 17 lines
The
kmsg_dumpercan be called from any context and CPU, possibly from multiple CPUs simultaneously. Since the writing of the buffer can occur from a later scheduled work queue, the oops buffer must be protected against simultaneous dumping.Use an atomic bit to mark when the buffer is protected. Release the protection in between setting the buffer and the actual writing in order for a possible panic (immediate write) to be written during the scheduling of a previous oops (delayed write).
An atomic bit (rather than a spinlock) was chosen so that no scheduling or preemption side-effects would be introduced. The MTD
kmsg_dumpermay dump directly or it may be delayed (via scheduled work). Depending on the context, different MTD callbacks are used. For example,mtd_write()expects to be called in a non-atomic context and may take a mutex.
Quoted 2 lines
Signed-off-by John Ogness <[email protected]>Reviewed-by Petr Mladek <[email protected]>
Thanks,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
John Ogness
maintainerPATCH v4 3/15 printk: limit second loop of syslog_print_all
In reply to John OgnessThe second loop of syslog_print_all() subtracts lengths that were added in the first loop. With commit b031a684bfd0 ("printk: remove logbuf_lock writer-protection of ringbuffer") it is possible that records are (over)written during syslog_print_all(). This allows the possibility of the second loop subtracting lengths that were never added in the first loop.
This situation can result in syslog_print_all() filling the buffer starting from a later record, even though there may have been room to fit the earlier record(s) as well.
--- kernel/printk/printk.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/printk/printk.c +8 -1
@@ -1494,6 +1494,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)struct printk_info info;unsigned int line_count;struct printk_record r; +u64 max_seq;char *text;int len =0 ;u64 seq;
@@ -1512,9 +1513,15 @@ static int syslog_print_all(char __user *buf, int size, bool clear)prb_for_each_info (clear_seq, prb, seq, &info, &line_count) len +=get_record_print_text_size (&info, line_count, true, time); +/* +* Set an upper bound for the next loop to avoid subtracting lengths +* that were never added. +*/ + max_seq = seq; +/* move first record forward until length fits into the buffer */ prb_for_each_info (clear_seq, prb, seq, &info, &line_count) { -if (len <= size) +if (len <= size || info.seq >= max_seq)break ; len -=get_record_print_text_size (&info, line_count, true, time); }
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 4/15 printk: kmsg_dump: remove unused fields
In reply to John Ognessstruct kmsg_dumper still contains some fields that were used to iterate the old ringbuffer. They are no longer used. Remove them and update the struct documentation.
--- include/linux/kmsg_dump.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/kmsg_dump.h +3 -2
@@ -36,6 +36,9 @@ enum kmsg_dump_reason {* through the record iterator * @max_reason: filter for highest reason number that should be dumped * @registered: Flag that specifies if this is already registered +* @active: Flag that specifies if this is currently dumping +* @cur_seq: Points to the oldest message to dump +* @next_seq: Points after the newest message to dump */ struct kmsg_dumper {struct list_head list;
@@ -45,8 +48,6 @@ struct kmsg_dumper {bool registered;/* private state of the kmsg iterator */ -u32 cur_idx; -u32 next_idx;u64 cur_seq;u64 next_seq; };
Signature · 1 line
2.20.1
Petr Mladek
maintainerlkml delivery: was: Re: [PATCH next v4 00/15] printk: remove logbuf_lock
In reply to John OgnessHi John,
Quoted 5 lines · John Ogness
Hello,
Here is v4 of a series to remove @logbuf_lock, exposing the ringbuffer locklessly to both readers and writers. v3 is here [0].
Have you got some reply from lkml that it has not delivered there, please?
I am not able to get the patchset using b4 tool:
$> b4 am -o test [email protected]
Looking up https://lore.kernel.org/r/20210303101528.29901-1-john.ogness%40linutronix.de
Grabbing thread from lore.kernel.org/linux-hyperv
Analyzing 2 messages in the thread
---
Thread incomplete, attempting to backfill
Grabbing thread from lore.kernel.org/lkml
Server returned an error: 404
Grabbing thread from lore.kernel.org/linux-mtd
Server returned an error: 404
Grabbing thread from lore.kernel.org/linuxppc-dev
Loaded 2 messages from https://lore.kernel.org/linuxppc-dev/
---
Writing test/v4_20210303_john_ogness_printk_remove_logbuf_lock.mbx
ERROR: missing [1/15]!
ERROR: missing [2/15]!
ERROR: missing [3/15]!
ERROR: missing [4/15]!
ERROR: missing [5/15]!
ERROR: missing [6/15]!
ERROR: missing [7/15]!
ERROR: missing [8/15]!
ERROR: missing [9/15]!
ERROR: missing [10/15]!
[PATCH next v4 11/15] printk: kmsg_dumper: remove @active field
✓ [PATCH next v4 12/15] printk: introduce a kmsg_dump iterator
ERROR: missing [13/15]!
[PATCH next v4 14/15] printk: kmsg_dump: remove _nolock() variants
ERROR: missing [15/15]!
---
Total patches: 3
---
WARNING: Thread incomplete!
Cover: test/v4_20210303_john_ogness_printk_remove_logbuf_lock.cover
Base: not found
git am test/v4_20210303_john_ogness_printk_remove_logbuf_lock.mbx
and I do not see it at lore. It has only found copies in linux-hyperv and linux-ppcdev mailing lists,
see https://lore.kernel.org/lkml/[email protected]/
Best Regards,
Petr
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Steven Rostedt
reviewerRe: lkml delivery: was: Re: [PATCH next v4 00/15] printk: remove logbuf_lock
Quoted 11 lines · On Wed, 3 Mar 2021 14:18:29 +0100 Petr Mladek <[email protected]> wrote:
Hi John,
Quoted 5 lines · John Ogness
Hello,
Here is v4 of a series to remove @logbuf_lock, exposing the ringbuffer locklessly to both readers and writers. v3 is here [0].
Have you got some reply from lkml that it has not delivered there, please?
vger has been having some issues as of late, and emails have been coming in slowly. I just received emails I sent more than 24 hours a head of time. Those in charge are trying to work things out.
-- Steve
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
John Ogness
maintainerPATCH v4 5/15 printk: refactor kmsg_dump_get_buffer()
In reply to John Ognesskmsg_dump_get_buffer() requires nearly the same logic as syslog_print_all(), but uses different variable names and does not make use of the ringbuffer loop macros. Modify kmsg_dump_get_buffer() so that the implementation is as similar to syslog_print_all() as possible.
A follow-up commit will move this common logic into a separate helper function.
--- include/linux/kmsg_dump.h | 2 +- kernel/printk/printk.c | 62 +++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 30 deletions(-)
include/linux/kmsg_dump.h +1 -1
@@ -62,7 +62,7 @@ bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, char *line, size_t size, size_t *len);bool kmsg_dump_get_buffer (struct kmsg_dumper *dumper,bool syslog, -char *buf,size_t size,size_t *len); +char *buf,size_t size,size_t *len_out);void kmsg_dump_rewind_nolock (struct kmsg_dumper *dumper);
kernel/printk/printk.c +33 -29
107 lines not on this page · raw message
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 6/15 printk: consolidate kmsg_dump_get_buffer/syslog_print_all code
In reply to John OgnessThe logic for finding records to fit into a buffer is the same for kmsg_dump_get_buffer() and syslog_print_all(). Introduce a helper function find_first_fitting_seq() to handle this logic.
--- kernel/printk/printk.c | 87 ++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 37 deletions(-)
kernel/printk/printk.c +50 -37
@@ -1421,6 +1421,50 @@ static size_t get_record_print_text_size(struct printk_info *info,return ((prefix_len * line_count) + info->text_len +1 ); } +/* +* Beginning with @start_seq, find the first record where it and all following +* records up to (but not including) @max_seq fit into @size. +* +* @max_seq is simply an upper bound and does not need to exist. If the caller +* does not require an upper bound, -1 can be used for @max_seq. +*/ +static u64 find_first_fitting_seq (u64 start_seq,u64 max_seq,size_t size, +bool syslog,bool time) +{ +struct printk_info info; +unsigned int line_count; +size_t len =0 ; +u64 seq; + +/* Determine the size of the records up to @max_seq. */ +prb_for_each_info (start_seq, prb, seq, &info, &line_count) { +if (info.seq >= max_seq) +break ; + len +=get_record_print_text_size (&info, line_count, syslog, time); + } + +/* +* Adjust the upper bound for the next loop to avoid subtracting +* lengths that were never added. +*/ +if (seq < max_seq) + max_seq = seq; + +/* +* Move first record forward until length fits into the buffer. Ignore +* newest messages that were not counted in the above cycle. Messages +* might appear and get lost in the meantime. This is a best effort +* that prevents an infinite loop that could occur with a retry. +*/ +prb_for_each_info (start_seq, prb, seq, &info, &line_count) { +if (len <= size || info.seq >= max_seq) +break ; + len -=get_record_print_text_size (&info, line_count, syslog, time); + } + +return seq; +} +static int syslog_print (char __user *buf,int size) {struct printk_info info;
@@ -1492,9 +1536,7 @@ static int syslog_print(char __user *buf, int size)static int syslog_print_all (char __user *buf,int size,bool clear) {struct printk_info info; -unsigned int line_count;struct printk_record r; -u64 max_seq;char *text;int len =0 ;u64 seq;
@@ -1510,21 +1552,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)* Find first record that fits, including all following records, * into the user-provided buffer for this dump. */ -prb_for_each_info (clear_seq, prb, seq, &info, &line_count) - len +=get_record_print_text_size (&info, line_count, true, time); - -/* -* Set an upper bound for the next loop to avoid subtracting lengths -* that were never added. -*/ - max_seq = seq; - -/* move first record forward until length fits into the buffer */ -prb_for_each_info (clear_seq, prb, seq, &info, &line_count) { -if (len <= size || info.seq >= max_seq) -break ; - len -=get_record_print_text_size (&info, line_count, true, time); - } + seq =find_first_fitting_seq (clear_seq,-1 , size, true, time);prb_rec_init_rd (&r, &info, text,LOG_LINE_MAX +PREFIX_MAX );
@@ -3427,7 +3455,6 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, char *buf, size_t size, size_t *len_out) { struct printk_info info; -unsigned int line_count;struct printk_record r;unsigned long flags;u64 seq;
@@ -3455,26 +3482,12 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,/* * Find first record that fits, including all following records, -* into the user-provided buffer for this dump. +* into the user-provided buffer for this dump. Pass in size-1 +* because this function (by way of record_print_text()) will +* not write more than size-1 bytes of text into @buf. */ - -prb_for_each_info (dumper->cur_seq, prb, seq, &info, &line_count) { -if (info.seq >= dumper->next_seq) -break ; - len +=get_record_print_text_size (&info, line_count, syslog, time); - } - -/* -* Move first record forward until length fits into the buffer. Ignore -* newest messages that were not counted in the above cycle. Messages -* might appear and get lost in the meantime. This is the best effort -* that prevents an infinite loop. -*/ -prb_for_each_info (dumper->cur_seq, prb, seq, &info, &line_count) { -if (len < size || info.seq >= dumper->next_seq) -break ; - len -=get_record_print_text_size (&info, line_count, syslog, time); - } + seq =find_first_fitting_seq (dumper->cur_seq, dumper->next_seq, + size -1 , syslog, time);/* * Next kmsg_dump_get_buffer() invocation will dump block of
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 7/15 printk: introduce CONSOLE_LOG_MAX
In reply to John OgnessInstead of using "LOG_LINE_MAX + PREFIX_MAX" for temporary buffer sizes, introduce CONSOLE_LOG_MAX. This represents the maximum size that is allowed to be printed to the console for a single record.
--- kernel/printk/printk.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
kernel/printk/printk.c +12 -8
@@ -410,7 +410,12 @@ static u64 clear_seq;#else #define PREFIX_MAX 32#endif -#define LOG_LINE_MAX (1024 - PREFIX_MAX) + +/* the maximum size of a formatted record (i.e. with prefix added per line) */ +#define CONSOLE_LOG_MAX 1024 + +/* the maximum size allowed to be reserved for a record */ +#define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX)#define LOG_LEVEL (v) ((v) & 0x07)#define LOG_FACILITY (v) ((v) >> 3 & 0xff)
@@ -1472,11 +1477,11 @@ static int syslog_print(char __user *buf, int size)char *text;int len =0 ; - text =kmalloc (LOG_LINE_MAX +PREFIX_MAX ,GFP_KERNEL ); + text =kmalloc (CONSOLE_LOG_MAX ,GFP_KERNEL );if (!text)return -ENOMEM ; -prb_rec_init_rd (&r, &info, text,LOG_LINE_MAX +PREFIX_MAX ); +prb_rec_init_rd (&r, &info, text,CONSOLE_LOG_MAX );while (size >0 ) {size_t n;
@@ -1542,7 +1547,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)u64 seq;bool time; - text =kmalloc (LOG_LINE_MAX +PREFIX_MAX ,GFP_KERNEL ); + text =kmalloc (CONSOLE_LOG_MAX ,GFP_KERNEL );if (!text)return -ENOMEM ;
@@ -1554,7 +1559,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)*/ seq =find_first_fitting_seq (clear_seq,-1 , size, true, time); -prb_rec_init_rd (&r, &info, text,LOG_LINE_MAX +PREFIX_MAX ); +prb_rec_init_rd (&r, &info, text,CONSOLE_LOG_MAX ); len =0 ;prb_for_each_record (seq, prb, seq, &r) {
@@ -2187,8 +2192,7 @@ EXPORT_SYMBOL(printk);#else /* CONFIG_PRINTK */ -#define LOG_LINE_MAX 0 -#define PREFIX_MAX 0 +#define CONSOLE_LOG_MAX 0#define printk_time false#define prb_read_valid (rb, seq, r) false
@@ -2506,7 +2510,7 @@ static inline int can_use_console(void)void console_unlock (void ) {static char ext_text[CONSOLE_EXT_LOG_MAX ]; -static char text[LOG_LINE_MAX +PREFIX_MAX ]; +static char text[CONSOLE_LOG_MAX ];unsigned long flags;bool do_cond_resched, retry;struct printk_info info;
Signature · 1 line
2.20.1
Re: [PATCH next v4 07/15] printk: introduce CONSOLE_LOG_MAX
Quoted 5 lines · John Ogness
Instead of using "
LOG_LINE_MAX+PREFIX_MAX" for temporary buffer sizes, introduceCONSOLE_LOG_MAX. This represents the maximum size that is allowed to be printed to the console for a single record.Signed-off-by John Ogness <[email protected]>
Best Regards,
Petr
John Ogness
maintainerPATCH v4 8/15 printk: use seqcount_latch for clear_seq
In reply to John Ognesskmsg_dump_rewind_nolock() locklessly reads @clear_seq. However, this is not done atomically. Since @clear_seq is 64-bit, this cannot be an atomic operation for all platforms. Therefore, use a seqcount_latch to allow readers to always read a consistent value.
--- kernel/printk/printk.c | 58 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 8 deletions(-)
kernel/printk/printk.c +50 -8
@@ -402,8 +402,21 @@ static u64 console_seq;static u64 exclusive_console_stop_seq;static unsigned long console_dropped; -/* the next printk record to read after the last 'clear' command */ -static u64 clear_seq; +struct latched_seq { +seqcount_latch_t latch; +u64 val[2 ]; +}; + +/* +* The next printk record to read after the last 'clear' command. There are +* two copies (updated with seqcount_latch) so that reads can locklessly +* access a valid value. Writers are synchronized by @logbuf_lock. +*/ +static struct latched_seq clear_seq = { + .latch =SEQCNT_LATCH_ZERO (clear_seq.latch), + .val[0 ] =0 , + .val[1 ] =0 , +};#ifdef CONFIG_PRINTK_CALLER #define PREFIX_MAX 48
@@ -457,6 +470,31 @@ bool printk_percpu_data_ready(void)return __printk_percpu_data_ready; } +/* Must be called under logbuf_lock. */ +static void latched_seq_write (struct latched_seq *ls,u64 val) +{ +raw_write_seqcount_latch (&ls->latch); + ls->val[0 ] = val; +raw_write_seqcount_latch (&ls->latch); + ls->val[1 ] = val; +} + +/* Can be called from any context. */ +static u64 latched_seq_read_nolock (struct latched_seq *ls) +{ +unsigned int seq; +unsigned int idx; +u64 val; + +do { + seq =raw_read_seqcount_latch (&ls->latch); + idx = seq &0x1 ; + val = ls->val[idx]; + }while (read_seqcount_latch_retry (&ls->latch, seq)); + +return val; +} +/* Return log buffer address */ char *log_buf_addr_get (void ) {
@@ -801,7 +839,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)* like issued by 'dmesg -c'. Reading /dev/kmsg itself * changes no global state, and does not clear anything. */ - user->seq = clear_seq; + user->seq =latched_seq_read_nolock (&clear_seq);break ;case SEEK_END :/* after the last record */
@@ -960,6 +998,9 @@ void log_buf_vmcoreinfo_setup(void)VMCOREINFO_SIZE (atomic_long_t);VMCOREINFO_TYPE_OFFSET (atomic_long_t, counter); + +VMCOREINFO_STRUCT_SIZE (latched_seq); + VMCOREINFO_OFFSET(latched_seq, val); }#endif
@@ -1557,7 +1598,8 @@ static int syslog_print_all(char __user *buf, int size, bool clear)* Find first record that fits, including all following records, * into the user-provided buffer for this dump. */ - seq =find_first_fitting_seq (clear_seq,-1 , size, true, time); + seq =find_first_fitting_seq (latched_seq_read_nolock (&clear_seq),-1 , + size, true, time);prb_rec_init_rd (&r, &info, text,CONSOLE_LOG_MAX );
@@ -1584,7 +1626,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear) }if (clear) - clear_seq = seq; +latched_seq_write (&clear_seq, seq);logbuf_unlock_irq ();kfree (text);
@@ -1594,7 +1636,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)static void syslog_clear (void ) {logbuf_lock_irq (); - clear_seq =prb_next_seq (prb); +latched_seq_write (&clear_seq,prb_next_seq (prb));logbuf_unlock_irq (); }
@@ -3336,7 +3378,7 @@ void kmsg_dump(enum kmsg_dump_reason reason) dumper->active = true;logbuf_lock_irqsave (flags); - dumper->cur_seq = clear_seq; + dumper->cur_seq =latched_seq_read_nolock (&clear_seq); dumper->next_seq =prb_next_seq (prb);logbuf_unlock_irqrestore (flags);
@@ -3534,7 +3576,7 @@ EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);*/ void kmsg_dump_rewind_nolock (struct kmsg_dumper *dumper) { - dumper->cur_seq = clear_seq; + dumper->cur_seq =latched_seq_read_nolock (&clear_seq); dumper->next_seq =prb_next_seq (prb); }
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 9/15 printk: use atomic64_t for devkmsg_user.seq
In reply to John Ogness@user->seq is indirectly protected by @logbuf_lock. Once @logbuf_lock is removed, @user->seq will be no longer safe from an atomicity point of view.
In preparation for the removal of @logbuf_lock, change it to atomic64_t to provide this safety.
--- kernel/printk/printk.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
kernel/printk/printk.c +12 -12
@@ -662,7 +662,7 @@ static ssize_t msg_print_ext_body(char *buf, size_t size,/* /dev/kmsg - userspace message inject/listen interface */ struct devkmsg_user { -u64 seq; +atomic64_t seq;struct ratelimit_state rs;struct mutex lock;char buf[CONSOLE_EXT_LOG_MAX ];
@@ -763,7 +763,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,return ret;logbuf_lock_irq (); -if (!prb_read_valid (prb, user->seq, r)) { +if (!prb_read_valid (prb,atomic64_read (&user->seq), r)) {if (file->f_flags &O_NONBLOCK ) { ret = -EAGAIN ;logbuf_unlock_irq ();
@@ -772,15 +772,15 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,logbuf_unlock_irq (); ret =wait_event_interruptible (log_wait, -prb_read_valid (prb, user->seq, r)); +prb_read_valid (prb,atomic64_read (&user->seq), r));if (ret) goto out;logbuf_lock_irq (); } -if (r->info->seq != user->seq) { +if (r->info->seq !=atomic64_read (&user->seq)) {/* our last seen message is gone, return error and reset */ - user->seq = r->info->seq; +atomic64_set (&user->seq, r->info->seq); ret = -EPIPE ;logbuf_unlock_irq (); goto out;
@@ -791,7 +791,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, &r->text_buf[0 ], r->info->text_len, &r->info->dev_info); - user->seq = r->info->seq +1 ; +atomic64_set (&user->seq, r->info->seq +1 );logbuf_unlock_irq ();if (len > count) {
@@ -831,7 +831,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)switch (whence) {case SEEK_SET :/* the first record */ - user->seq =prb_first_valid_seq (prb); +atomic64_set (&user->seq,prb_first_valid_seq (prb));break ;case SEEK_DATA :/*
@@ -839,11 +839,11 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)* like issued by 'dmesg -c'. Reading /dev/kmsg itself * changes no global state, and does not clear anything. */ - user->seq =latched_seq_read_nolock (&clear_seq); +atomic64_set (&user->seq,latched_seq_read_nolock (&clear_seq));break ;case SEEK_END :/* after the last record */ - user->seq =prb_next_seq (prb); +atomic64_set (&user->seq,prb_next_seq (prb));break ;default : ret = -EINVAL ;
@@ -864,9 +864,9 @@ static __poll_t devkmsg_poll(struct file *file, poll_table *wait)poll_wait (file, &log_wait, wait);logbuf_lock_irq (); -if (prb_read_valid_info (prb, user->seq, &info,NULL )) { +if (prb_read_valid_info (prb,atomic64_read (&user->seq), &info,NULL )) {/* return error when data has vanished underneath us */ -if (info.seq != user->seq) +if (info.seq !=atomic64_read (&user->seq)) ret =EPOLLIN |EPOLLRDNORM |EPOLLERR |EPOLLPRI ;else ret =EPOLLIN |EPOLLRDNORM ;
@@ -905,7 +905,7 @@ static int devkmsg_open(struct inode *inode, struct file *file) &user->text_buf[0], sizeof(user->text_buf));logbuf_lock_irq (); - user->seq =prb_first_valid_seq (prb); +atomic64_set (&user->seq,prb_first_valid_seq (prb));logbuf_unlock_irq (); file->private_data = user;
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 10/15 printk: add syslog_lock
In reply to John OgnessThe global variables @syslog_seq, @syslog_partial, @syslog_time and write access to @clear_seq are protected by @logbuf_lock. Once @logbuf_lock is removed, these variables will need their own synchronization method. Introduce @syslog_lock for this purpose.
@syslog_lock is a raw_spin_lock for now. This simplifies the transition to removing @logbuf_lock. Once @logbuf_lock and the safe buffers are removed, @syslog_lock can change to spin_lock.
--- kernel/printk/printk.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
kernel/printk/printk.c +37 -4
@@ -390,8 +390,12 @@ DEFINE_RAW_SPINLOCK(logbuf_lock);printk_safe_exit_irqrestore (flags); \ }while (0 ) +/* syslog_lock protects syslog_* variables and write access to clear_seq. */ +static DEFINE_RAW_SPINLOCK(syslog_lock); +#ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD (log_wait); +/* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */ static u64 syslog_seq;static size_t syslog_partial;
@@ -410,7 +414,7 @@ struct latched_seq {/* * The next printk record to read after the last 'clear' command. There are * two copies (updated with seqcount_latch) so that reads can locklessly -* access a valid value. Writers are synchronized by @logbuf_lock. +* access a valid value. Writers are synchronized by @syslog_lock. */ static struct latched_seq clear_seq = { .latch =SEQCNT_LATCH_ZERO (clear_seq.latch),
@@ -470,7 +474,7 @@ bool printk_percpu_data_ready(void)return __printk_percpu_data_ready; } -/* Must be called under logbuf_lock. */ +/* Must be called under syslog_lock. */ static void latched_seq_write (struct latched_seq *ls,u64 val) {raw_write_seqcount_latch (&ls->latch);
@@ -1529,7 +1533,9 @@ static int syslog_print(char __user *buf, int size)size_t skip;logbuf_lock_irq (); +raw_spin_lock (&syslog_lock);if (!prb_read_valid (prb, syslog_seq, &r)) { +raw_spin_unlock (&syslog_lock);logbuf_unlock_irq ();break ; }
@@ -1559,6 +1565,7 @@ static int syslog_print(char __user *buf, int size) syslog_partial += n; }else n =0 ; +raw_spin_unlock (&syslog_lock);logbuf_unlock_irq ();if (!n)
@@ -1625,8 +1632,11 @@ static int syslog_print_all(char __user *buf, int size, bool clear)break ; } -if (clear) +if (clear) { +raw_spin_lock (&syslog_lock);latched_seq_write (&clear_seq, seq); +raw_spin_unlock (&syslog_lock); + }logbuf_unlock_irq ();kfree (text);
@@ -1636,10 +1646,24 @@ static int syslog_print_all(char __user *buf, int size, bool clear)static void syslog_clear (void ) {logbuf_lock_irq (); +raw_spin_lock (&syslog_lock);latched_seq_write (&clear_seq,prb_next_seq (prb)); +raw_spin_unlock (&syslog_lock);logbuf_unlock_irq (); } +/* Return a consistent copy of @syslog_seq. */ +static u64 read_syslog_seq_irq (void ) +{ +u64 seq; + +raw_spin_lock_irq (&syslog_lock); + seq = syslog_seq; +raw_spin_unlock_irq (&syslog_lock); + +return seq; +} +int do_syslog (int type,char __user *buf,int len,int source) {struct printk_info info;
@@ -1663,8 +1687,9 @@ int do_syslog(int type, char __user *buf, int len, int source)return 0 ;if (!access_ok (buf, len))return -EFAULT ; + error =wait_event_interruptible (log_wait, -prb_read_valid (prb, syslog_seq,NULL )); +prb_read_valid (prb,read_syslog_seq_irq (),NULL ));if (error)return error; error =syslog_print (buf, len);
@@ -1713,8 +1738,10 @@ int do_syslog(int type, char __user *buf, int len, int source)/* Number of chars in the log buffer */ case SYSLOG_ACTION_SIZE_UNREAD :logbuf_lock_irq (); +raw_spin_lock (&syslog_lock);if (!prb_read_valid_info (prb, syslog_seq, &info,NULL )) {/* No unread messages. */ +raw_spin_unlock (&syslog_lock);logbuf_unlock_irq ();return 0 ; }
@@ -1743,6 +1770,7 @@ int do_syslog(int type, char __user *buf, int len, int source) } error -= syslog_partial; } +raw_spin_unlock (&syslog_lock);logbuf_unlock_irq ();break ;/* Size of the log buffer */
@@ -2992,7 +3020,12 @@ void register_console(struct console *newcon)*/ exclusive_console = newcon; exclusive_console_stop_seq = console_seq; + +/* Get a consistent copy of @syslog_seq. */ +raw_spin_lock (&syslog_lock); console_seq = syslog_seq; +raw_spin_unlock (&syslog_lock); +logbuf_unlock_irqrestore (flags); }console_unlock ();
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 11/15 printk: kmsg_dumper: remove @active field
In reply to John OgnessAll 6 kmsg_dumpers do not benefit from the @active flag:
(provide their own synchronization)
- arch/powerpc/kernel/nvram_64.c
- arch/um/kernel/kmsg_dump.c
- drivers/mtd/mtdoops.c
- fs/pstore/platform.c
(only dump on KMSG_DUMP_PANIC, which does not require synchronization)
- arch/powerpc/platforms/powernv/opal-kmsg.c
- drivers/hv/vmbus_drv.c
The other 2 kmsg_dump users also do not rely on @active:
(hard-code @active to always be true)
- arch/powerpc/xmon/xmon.c
- kernel/debug/kdb/kdb_main.c
Therefore, @active can be removed.
--- arch/powerpc/xmon/xmon.c | 2 +- include/linux/kmsg_dump.h | 2 -- kernel/debug/kdb/kdb_main.c | 2 +- kernel/printk/printk.c | 10 +--------- 4 files changed, 3 insertions(+), 13 deletions(-)
arch/powerpc/xmon/xmon.c +1 -1
@@ -3001,7 +3001,7 @@ print_address(unsigned long addr)static void dump_log_buf (void ) { -struct kmsg_dumper dumper = { .active =1 }; +struct kmsg_dumper dumper;unsigned char buf[128 ];size_t len;
include/linux/kmsg_dump.h +0 -2
@@ -36,7 +36,6 @@ enum kmsg_dump_reason {* through the record iterator * @max_reason: filter for highest reason number that should be dumped * @registered: Flag that specifies if this is already registered -* @active: Flag that specifies if this is currently dumping * @cur_seq: Points to the oldest message to dump * @next_seq: Points after the newest message to dump */
@@ -44,7 +43,6 @@ struct kmsg_dumper {struct list_head list;void (*dump)(struct kmsg_dumper *dumper,enum kmsg_dump_reason reason);enum kmsg_dump_reason max_reason; -bool active;bool registered;/* private state of the kmsg iterator */
kernel/debug/kdb/kdb_main.c +1 -1
@@ -2101,7 +2101,7 @@ static int kdb_dmesg(int argc, const char **argv)int adjust =0 ;int n =0 ;int skip =0 ; -struct kmsg_dumper dumper = { .active =1 }; +struct kmsg_dumper dumper;size_t len;char buf[201 ];
kernel/printk/printk.c +1 -9
@@ -3408,8 +3408,6 @@ void kmsg_dump(enum kmsg_dump_reason reason)continue ;/* initialize iterator with data about the stored records */ - dumper->active = true; -logbuf_lock_irqsave (flags); dumper->cur_seq =latched_seq_read_nolock (&clear_seq); dumper->next_seq =prb_next_seq (prb);
@@ -3417,9 +3415,6 @@ void kmsg_dump(enum kmsg_dump_reason reason)/* invoke dumper which will iterate over records */ dumper->dump (dumper, reason); - -/* reset iterator */ - dumper->active = false; }rcu_read_unlock (); }
@@ -3454,9 +3449,6 @@ bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,prb_rec_init_rd (&r, &info, line, size); -if (!dumper->active) - goto out; -/* Read text or count text lines? */ if (line) {if (!prb_read_valid (prb, dumper->cur_seq, &r))
@@ -3542,7 +3534,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,bool ret = false;bool time = printk_time; -if (!dumper->active || !buf || !size) +if (!buf || !size) goto out;logbuf_lock_irqsave (flags);
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 12/15 printk: introduce a kmsg_dump iterator
In reply to John OgnessRather than storing the iterator information in the registered kmsg_dumper structure, create a separate iterator structure. The kmsg_dump_iter structure can reside on the stack of the caller, thus allowing lockless use of the kmsg_dump functions.
Update code that accesses the kernel logs using the kmsg_dumper structure to use the new kmsg_dump_iter structure. For kmsg_dumpers, this also means adding a call to kmsg_dump_rewind() to initialize the iterator.
All this is in preparation for removal of @logbuf_lock.
--- arch/powerpc/kernel/nvram_64.c | 8 +++-- arch/powerpc/xmon/xmon.c | 6 ++-- arch/um/kernel/kmsg_dump.c | 5 ++- drivers/hv/vmbus_drv.c | 4 ++- drivers/mtd/mtdoops.c | 5 ++- fs/pstore/platform.c | 5 ++- include/linux/kmsg_dump.h | 36 ++++++++++--------- kernel/debug/kdb/kdb_main.c | 10 +++--- kernel/printk/printk.c | 63 +++++++++++++++++----------------- 9 files changed, 80 insertions(+), 62 deletions(-)
arch/powerpc/kernel/nvram_64.c +5 -3
@@ -647,6 +647,7 @@ static void oops_to_nvram(struct kmsg_dumper *dumper, {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 +682,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); +kmsg_dump_get_buffer (&iter, false, big_oops_buf, big_oops_buf_sz, &text_len); rc =zip_oops (text_len); }if (rc !=0 ) { -kmsg_dump_rewind (dumper); -kmsg_dump_get_buffer (dumper, false, +kmsg_dump_rewind (&iter); +kmsg_dump_get_buffer (&iter, false, oops_data, oops_data_sz, &text_len); err_type =ERR_TYPE_KERNEL_PANIC ; oops_hdr->version =cpu_to_be16 (OOPS_HDR_VERSION );
arch/powerpc/xmon/xmon.c +3 -3
@@ -3001,7 +3001,7 @@ print_address(unsigned long addr)static void dump_log_buf (void ) { -struct kmsg_dumper dumper; +struct kmsg_dump_iter iter;unsigned char buf[128 ];size_t len;
@@ -3013,9 +3013,9 @@ dump_log_buf(void) catch_memory_errors =1 ;sync (); -kmsg_dump_rewind_nolock (&dumper); +kmsg_dump_rewind_nolock (&iter);xmon_start_pagination (); -while (kmsg_dump_get_line_nolock (&dumper, false, buf,sizeof (buf), &len)) { +while (kmsg_dump_get_line_nolock (&iter, false, buf,sizeof (buf), &len)) { buf[len] ='\0' ;printf ("%s" , buf); }
arch/um/kernel/kmsg_dump.c +4 -1
@@ -10,6 +10,7 @@static void kmsg_dumper_stdout (struct kmsg_dumper *dumper,enum kmsg_dump_reason reason) { +static struct kmsg_dump_iter iter;static DEFINE_SPINLOCK (lock);static char line[1024 ];struct console *con;
@@ -35,8 +36,10 @@ static void kmsg_dumper_stdout(struct kmsg_dumper *dumper,if (!spin_trylock_irqsave (&lock, flags))return ; +kmsg_dump_rewind (&iter); +printf ("kmsg_dump:\n" ); -while (kmsg_dump_get_line (dumper, true, line,sizeof (line), &len)) { +while (kmsg_dump_get_line (&iter, true, line,sizeof (line), &len)) { line[len] ='\0' ;printf ("%s" , line); }
drivers/hv/vmbus_drv.c +3 -1
@@ -1391,6 +1391,7 @@ static void vmbus_isr(void)static void hv_kmsg_dump (struct kmsg_dumper *dumper,enum kmsg_dump_reason reason) { +struct kmsg_dump_iter iter;size_t bytes_written;phys_addr_t panic_pa;
@@ -1404,7 +1405,8 @@ static void hv_kmsg_dump(struct kmsg_dumper *dumper,* Write dump contents to the page. No need to synchronize; panic should * be single-threaded. */ -kmsg_dump_get_buffer (dumper, false, hv_panic_page,HV_HYP_PAGE_SIZE , +kmsg_dump_rewind (&iter); +kmsg_dump_get_buffer (&iter, false, hv_panic_page,HV_HYP_PAGE_SIZE , &bytes_written);if (bytes_written)hyperv_report_panic_msg (panic_pa, bytes_written);
drivers/mtd/mtdoops.c +4 -1
@@ -277,14 +277,17 @@ static void mtdoops_do_dump(struct kmsg_dumper *dumper, {struct mtdoops_context *cxt =container_of (dumper, struct mtdoops_context, dump); +struct kmsg_dump_iter iter;/* Only dump oopses if dump_oops is set */ if (reason ==KMSG_DUMP_OOPS && !dump_oops)return ; +kmsg_dump_rewind (&iter); +if (test_and_set_bit (0 , &cxt->oops_buf_busy))return ; -kmsg_dump_get_buffer (dumper, true, cxt->oops_buf +MTDOOPS_HEADER_SIZE , +kmsg_dump_get_buffer (&iter, true, cxt->oops_buf +MTDOOPS_HEADER_SIZE , record_size -MTDOOPS_HEADER_SIZE ,NULL );clear_bit (0 , &cxt->oops_buf_busy);
fs/pstore/platform.c +4 -1
@@ -385,6 +385,7 @@ void pstore_record_init(struct pstore_record *record,static void pstore_dump (struct kmsg_dumper *dumper,enum kmsg_dump_reason reason) { +struct kmsg_dump_iter iter;unsigned long total =0 ;const char *why;unsigned int part =1 ;
@@ -405,6 +406,8 @@ static void pstore_dump(struct kmsg_dumper *dumper, } } +kmsg_dump_rewind (&iter); + oopscount++;while (total < kmsg_bytes) {char *dst;
@@ -435,7 +438,7 @@ static void pstore_dump(struct kmsg_dumper *dumper, dst_size -= header_size;/* Write dump contents. */ -if (!kmsg_dump_get_buffer (dumper, true, dst + header_size, +if (!kmsg_dump_get_buffer (&iter, true, dst + header_size, dst_size, &dump_size))break ;
include/linux/kmsg_dump.h +20 -16
@@ -29,6 +29,16 @@ enum kmsg_dump_reason { KMSG_DUMP_MAX }; +/** +* struct kmsg_dump_iter - iterator for retrieving kernel messages +* @cur_seq: Points to the oldest message to dump +* @next_seq: Points after the newest message to dump +*/ +struct kmsg_dump_iter { +u64 cur_seq; +u64 next_seq; +}; +/** * struct kmsg_dumper - kernel crash message dumper structure * @list: Entry in the dumper list (private)
@@ -36,35 +46,29 @@ enum kmsg_dump_reason {* through the record iterator * @max_reason: filter for highest reason number that should be dumped * @registered: Flag that specifies if this is already registered -* @cur_seq: Points to the oldest message to dump -* @next_seq: Points after the newest message to dump */ struct kmsg_dumper {struct list_head list;void (*dump)(struct kmsg_dumper *dumper,enum kmsg_dump_reason reason);enum kmsg_dump_reason max_reason;bool registered; - -/* private state of the kmsg iterator */ -u64 cur_seq; -u64 next_seq; };#ifdef CONFIG_PRINTK void kmsg_dump (enum kmsg_dump_reason reason); -bool kmsg_dump_get_line_nolock (struct kmsg_dumper *dumper,bool syslog, +bool kmsg_dump_get_line_nolock (struct kmsg_dump_iter *iter,bool syslog,char *line,size_t size,size_t *len); -bool kmsg_dump_get_line (struct kmsg_dumper *dumper,bool syslog, +bool kmsg_dump_get_line (struct kmsg_dump_iter *iter,bool syslog,char *line,size_t size,size_t *len); -bool kmsg_dump_get_buffer (struct kmsg_dumper *dumper,bool syslog, +bool kmsg_dump_get_buffer (struct kmsg_dump_iter *iter,bool syslog,char *buf,size_t size,size_t *len_out); -void kmsg_dump_rewind_nolock (struct kmsg_dumper *dumper); +void kmsg_dump_rewind_nolock (struct kmsg_dump_iter *iter); -void kmsg_dump_rewind (struct kmsg_dumper *dumper); +void kmsg_dump_rewind (struct kmsg_dump_iter *iter);int kmsg_dump_register (struct kmsg_dumper *dumper);
@@ -76,30 +80,30 @@ static inline void kmsg_dump(enum kmsg_dump_reason reason) { } -static inline bool kmsg_dump_get_line_nolock (struct kmsg_dumper *dumper, +static inline bool kmsg_dump_get_line_nolock (struct kmsg_dump_iter *iter,bool syslog,const char *line,size_t size,size_t *len) {return false; } -static inline bool kmsg_dump_get_line (struct kmsg_dumper *dumper,bool syslog, +static inline bool kmsg_dump_get_line (struct kmsg_dump_iter *iter,bool syslog,const char *line,size_t size,size_t *len) {return false; } -static inline bool kmsg_dump_get_buffer (struct kmsg_dumper *dumper,bool syslog, +static inline bool kmsg_dump_get_buffer (struct kmsg_dump_iter *iter,bool syslog,char *buf,size_t size,size_t *len) {return false; } -static inline void kmsg_dump_rewind_nolock (struct kmsg_dumper *dumper) +static inline void kmsg_dump_rewind_nolock (struct kmsg_dump_iter *iter) { } -static inline void kmsg_dump_rewind (struct kmsg_dumper *dumper) +static inline void kmsg_dump_rewind (struct kmsg_dump_iter *iter) { }
kernel/debug/kdb/kdb_main.c +5 -5
@@ -2101,7 +2101,7 @@ static int kdb_dmesg(int argc, const char **argv)int adjust =0 ;int n =0 ;int skip =0 ; -struct kmsg_dumper dumper; +struct kmsg_dump_iter iter;size_t len;char buf[201 ];
@@ -2126,8 +2126,8 @@ static int kdb_dmesg(int argc, const char **argv)kdb_set (2 , setargs); } -kmsg_dump_rewind_nolock (&dumper); -while (kmsg_dump_get_line_nolock (&dumper,1 ,NULL ,0 ,NULL )) +kmsg_dump_rewind_nolock (&iter); +while (kmsg_dump_get_line_nolock (&iter,1 ,NULL ,0 ,NULL )) n++;if (lines <0 ) {
@@ -2159,8 +2159,8 @@ static int kdb_dmesg(int argc, const char **argv)if (skip >= n || skip <0 )return 0 ; -kmsg_dump_rewind_nolock (&dumper); -while (kmsg_dump_get_line_nolock (&dumper,1 , buf,sizeof (buf), &len)) { +kmsg_dump_rewind_nolock (&iter); +while (kmsg_dump_get_line_nolock (&iter,1 , buf,sizeof (buf), &len)) {if (skip) { skip--;continue ;
kernel/printk/printk.c +32 -31
196 lines not on this page · raw message
Signature · 6 lines
2.20.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH next v4 12/15] printk: introduce a kmsg_dump iterator
Quoted 10 lines · John Ogness
Rather than storing the iterator information in the registered
kmsg_dumperstructure, create a separate iterator structure. Thekmsg_dump_iterstructure can reside on the stack of the caller, thus allowing lockless use of thekmsg_dumpfunctions.Update code that accesses the kernel logs using the
kmsg_dumperstructure to use the newkmsg_dump_iterstructure. Forkmsg_dumpers, this also means adding a call tokmsg_dump_rewind()to initialize the iterator.
Quoted 4 lines
All this is in preparation for removal of @logbuf_lock.
Signed-off-by John Ogness <[email protected]>Reviewed-by Kees Cook <[email protected]> # pstore
Best Regards,
Petr
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
John Ogness
maintainerPATCH v4 13/15 printk: remove logbuf_lock
In reply to John OgnessSince the ringbuffer is lockless, there is no need for it to be protected by @logbuf_lock. Remove @logbuf_lock.
@console_seq, @exclusive_console_stop_seq, @console_dropped are protected by @console_lock.
--- kernel/printk/internal.h | 4 +- kernel/printk/printk.c | 112 ++++++++++++------------------------ kernel/printk/printk_safe.c | 27 +++------ 3 files changed, 46 insertions(+), 97 deletions(-)
kernel/printk/internal.h +1 -3
@@ -12,8 +12,6 @@#define PRINTK_NMI_CONTEXT_OFFSET 0x010000000 -extern raw_spinlock_t logbuf_lock; - __printf(4, 0) int vprintk_store(int facility, int level, const struct dev_printk_info *dev_info,
@@ -59,7 +57,7 @@ void defer_console_output(void); __printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return 0; }/* -* In !PRINTK builds we still export logbuf_lock spin_lock, console_sem +* In !PRINTK builds we still export console_sem * semaphore and some of console functions (console_unlock()/etc.), so * printk-safe must preserve the existing local IRQ guarantees. */
kernel/printk/printk.c +36 -76
321 lines not on this page · raw message
kernel/printk/printk_safe.c +9 -18
@@ -16,7 +16,7 @@#include "internal.h" /* -* printk() could not take logbuf_lock in NMI context. Instead, +* In NMI and safe mode, printk() avoids taking locks. Instead, * it uses an alternative implementation that temporary stores * the strings into a per-CPU buffer. The content of the buffer * is later flushed into the main ring buffer via IRQ work.
@@ -267,17 +267,9 @@ void printk_safe_flush(void)void printk_safe_flush_on_panic (void ) {/* -* Make sure that we could access the main ring buffer. +* Make sure that we could access the safe buffers. * Do not risk a double release when more CPUs are up. */ -if (raw_spin_is_locked (&logbuf_lock)) { -if (num_online_cpus () >1 ) -return ; - -debug_locks_off (); -raw_spin_lock_init (&logbuf_lock); - } -if (raw_spin_is_locked (&safe_read_lock)) {if (num_online_cpus () >1 )return ;
@@ -319,9 +311,7 @@ void noinstr printk_nmi_exit(void)* reordering. * * It has effect only when called in NMI context. Then printk() -* will try to store the messages into the main logbuf directly -* and use the per-CPU buffers only as a fallback when the lock -* is not available. +* will store the messages into the main logbuf directly. */ void printk_nmi_direct_enter (void ) {
@@ -376,20 +366,21 @@ __printf(1, 0) int vprintk_func(const char *fmt, va_list args)#endif /* -* Try to use the main logbuf even in NMI. But avoid calling console +* Use the main logbuf even in NMI. But avoid calling console * drivers that might have their own locks. */ -if ((this_cpu_read (printk_context) &PRINTK_NMI_DIRECT_CONTEXT_MASK ) && -raw_spin_trylock (&logbuf_lock)) { +if ((this_cpu_read (printk_context) &PRINTK_NMI_DIRECT_CONTEXT_MASK )) { +unsigned long flags;int len; +printk_safe_enter_irqsave (flags); len =vprintk_store (0 ,LOGLEVEL_DEFAULT ,NULL , fmt, args); -raw_spin_unlock (&logbuf_lock); +printk_safe_exit_irqrestore (flags);defer_console_output ();return len; } -/* Use extra buffer in NMI when logbuf_lock is taken or in safe mode. */ +/* Use extra buffer in NMI. */ if (this_cpu_read (printk_context) &PRINTK_NMI_CONTEXT_MASK )return vprintk_nmi (fmt, args);
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 14/15 printk: kmsg_dump: remove _nolock() variants
In reply to John Ognesskmsg_dump_rewind() and kmsg_dump_get_line() are lockless, so there is no need for _nolock() variants. Remove these functions and switch all callers of the _nolock() variants.
The functions without _nolock() were chosen because they are already exported to kernel modules.
--- arch/powerpc/xmon/xmon.c | 4 +-- include/linux/kmsg_dump.h | 16 ---------- kernel/debug/kdb/kdb_main.c | 8 ++--- kernel/printk/printk.c | 60 +++++-------------------------------- 4 files changed, 14 insertions(+), 74 deletions(-)
arch/powerpc/xmon/xmon.c +2 -2
@@ -3013,9 +3013,9 @@ dump_log_buf(void) catch_memory_errors =1 ;sync (); -kmsg_dump_rewind_nolock (&iter); +kmsg_dump_rewind (&iter);xmon_start_pagination (); -while (kmsg_dump_get_line_nolock (&iter, false, buf,sizeof (buf), &len)) { +while (kmsg_dump_get_line (&iter, false, buf,sizeof (buf), &len)) { buf[len] ='\0' ;printf ("%s" , buf); }
include/linux/kmsg_dump.h +0 -16
@@ -57,17 +57,12 @@ struct kmsg_dumper {#ifdef CONFIG_PRINTK void kmsg_dump (enum kmsg_dump_reason reason); -bool kmsg_dump_get_line_nolock (struct kmsg_dump_iter *iter,bool syslog, -char *line,size_t size,size_t *len); -bool kmsg_dump_get_line (struct kmsg_dump_iter *iter,bool syslog,char *line,size_t size,size_t *len);bool kmsg_dump_get_buffer (struct kmsg_dump_iter *iter,bool syslog,char *buf,size_t size,size_t *len_out); -void kmsg_dump_rewind_nolock (struct kmsg_dump_iter *iter); -void kmsg_dump_rewind (struct kmsg_dump_iter *iter);int kmsg_dump_register (struct kmsg_dumper *dumper);
@@ -80,13 +75,6 @@ static inline void kmsg_dump(enum kmsg_dump_reason reason) { } -static inline bool kmsg_dump_get_line_nolock (struct kmsg_dump_iter *iter, -bool syslog,const char *line, -size_t size,size_t *len) -{ -return false; -} -static inline bool kmsg_dump_get_line (struct kmsg_dump_iter *iter,bool syslog,const char *line,size_t size,size_t *len) {
@@ -99,10 +87,6 @@ static inline bool kmsg_dump_get_buffer(struct kmsg_dump_iter *iter, bool syslogreturn false; } -static inline void kmsg_dump_rewind_nolock (struct kmsg_dump_iter *iter) -{ -} -static inline void kmsg_dump_rewind (struct kmsg_dump_iter *iter) { }
kernel/debug/kdb/kdb_main.c +4 -4
@@ -2126,8 +2126,8 @@ static int kdb_dmesg(int argc, const char **argv)kdb_set (2 , setargs); } -kmsg_dump_rewind_nolock (&iter); -while (kmsg_dump_get_line_nolock (&iter,1 ,NULL ,0 ,NULL )) +kmsg_dump_rewind (&iter); +while (kmsg_dump_get_line (&iter,1 ,NULL ,0 ,NULL )) n++;if (lines <0 ) {
@@ -2159,8 +2159,8 @@ static int kdb_dmesg(int argc, const char **argv)if (skip >= n || skip <0 )return 0 ; -kmsg_dump_rewind_nolock (&iter); -while (kmsg_dump_get_line_nolock (&iter,1 , buf,sizeof (buf), &len)) { +kmsg_dump_rewind (&iter); +while (kmsg_dump_get_line (&iter,1 , buf,sizeof (buf), &len)) {if (skip) { skip--;continue ;
kernel/printk/printk.c +8 -52
106 lines not on this page · raw message
Signature · 1 line
2.20.1
John Ogness
maintainerPATCH v4 15/15 printk: console: remove unnecessary safe buffer usage
In reply to John OgnessUpon registering a console, safe buffers are activated when setting up the sequence number to replay the log. However, these are already protected by @console_sem and @syslog_lock. Remove the unnecessary safe buffer usage.
--- kernel/printk/printk.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
kernel/printk/printk.c +3 -7
@@ -2967,9 +2967,7 @@ void register_console(struct console *newcon)/* * console_unlock(); will print out the buffered messages * for us. -*/ -printk_safe_enter_irqsave (flags); -/* +* * We're about to replay the log buffer. Only do this to the * just-registered console to avoid excessive message spam to * the already-registered consoles.
@@ -2982,11 +2980,9 @@ void register_console(struct console *newcon) exclusive_console_stop_seq = console_seq;/* Get a consistent copy of @syslog_seq. */ -raw_spin_lock (&syslog_lock); +raw_spin_lock_irqsave (&syslog_lock, flags); console_seq = syslog_seq; -raw_spin_unlock (&syslog_lock); - -printk_safe_exit_irqrestore (flags); +raw_spin_unlock_irqrestore (&syslog_lock, flags); }console_unlock ();console_sysfs_notify ();
Signature · 1 line
2.20.1
Petr Mladek
maintainerQuoted 5 lines · John Ogness
Hello,
Here is v4 of a series to remove @logbuf_lock, exposing the ringbuffer locklessly to both readers and writers. v3 is here [0].
The series look ready. I am going to push it into printk/linux.git the following week unless anyone speaks against it in the meantime.
Best Regards,
Petr
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Petr Mladek
maintainerQuoted 9 lines · Petr Mladek
Quoted 5 lines · John Ogness
Hello,
Here is v4 of a series to remove @logbuf_lock, exposing the ringbuffer locklessly to both readers and writers. v3 is here [0].
The series look ready. I am going to push it into printk/linux.git the following week unless anyone speaks against it in the meantime.
The series is committed in printk/linux.git, branch printk-rework.
Best Regards,
Petr
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/