Skip to contentSkip to conversation

printk: wake up klogd in vprintk_emit

PATCH In review Sergey Senozhatsky 4 participants 14 messages Apr 14, 2018 – Apr 26, 2018 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 printk: wake up klogd in vprintk_emit

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

---
 kernel/printk/printk.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
kernel/printk/printk.c +2 -12
index 2f4af216bd6e..86f0b337cbf6 100644
@@ -1888,6 +1888,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 
 	printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
 
+	wake_up_klogd();
 	logbuf_unlock_irqrestore(flags);
 
 	/* If called from the scheduler, we can not call up(). */
@@ -2289,9 +2290,7 @@ void console_unlock(void)
 {
 	static char ext_text[CONSOLE_EXT_LOG_MAX];
 	static char text[LOG_LINE_MAX + PREFIX_MAX];
-	static u64 seen_seq;
 	unsigned long flags;
-	bool wake_klogd = false;
 	bool do_cond_resched, retry;
 
 	if (console_suspended) {
@@ -2335,11 +2334,6 @@ void console_unlock(void)
 
 		printk_safe_enter_irqsave(flags);
 		raw_spin_lock(&logbuf_lock);
-		if (seen_seq != log_next_seq) {
-			wake_klogd = true;
-			seen_seq = log_next_seq;
-		}
-
 		if (console_seq < log_first_seq) {
 			len = sprintf(text, "** %u printk messages dropped **\n",
 				      (unsigned)(log_first_seq - console_seq));
@@ -2397,7 +2391,7 @@ void console_unlock(void)
 
 		if (console_lock_spinning_disable_and_check()) {
 			printk_safe_exit_irqrestore(flags);
-			goto out;
+			return;
 		}
 
 		printk_safe_exit_irqrestore(flags);
@@ -2429,10 +2423,6 @@ void console_unlock(void)
 
 	if (retry && console_trylock())
 		goto again;
-
-out:
-	if (wake_klogd)
-		wake_up_klogd();
 }
 EXPORT_SYMBOL(console_unlock);
 
Signature · 1 line
2.17.0
maintainer
#1 lore raw
Quoted 21 lines · Sergey Senozhatsky

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

---
 kernel/printk/printk.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
kernel/printk/printk.c +0 -0
index 2f4af216bd6e..86f0b337cbf6 100644

kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1892

Quoted 5 lines
kernel/printk/printk.c +1 -0
@@ -1888,6 +1888,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 
 	printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
 
+	wake_up_klogd();
 	logbuf_unlock_irqrestore(flags);

The change makes perfect sense and I am fine with the idea. I just wonder if there is a strong reason to do the wake_up before releasing the logbuf_lock. It makes an assumption that it needs to be synchronized by logbuf_lock.

In fact, I would feel more comfortable if we move this to the end of vprintk_emit() right before return printk_len. This will be more close to the current behavior (console first). But it will still wakeup klogd much earlier and regularly if there is a flood of messages.

Best Regards,
Petr

#2 lore raw

On (04/18/18 16:04), Petr Mladek wrote:
[..]

Quoted 8 lines
Quoted 8 lines
kernel/printk/printk.c +0 -0
index 2f4af216bd6e..86f0b337cbf6 100644
@@ -1888,6 +1888,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 
 	printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
 
Quoted 7 lines

kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1892

Quoted 2 lines
kernel/printk/printk.c +1 -0
+	wake_up_klogd();
 	logbuf_unlock_irqrestore(flags);

The change makes perfect sense and I am fine with the idea. I just wonder if there is a strong reason to do the wake_up before releasing the logbuf_lock. It makes an assumption that it needs to be synchronized by logbuf_lock.

No, not really, just wanted to wakeup klogd from the same CPU which called printk().

Quoted 5 lines

In fact, I would feel more comfortable if we move this to the end of vprintk_emit() right before return printk_len. This will be more close to the current behavior (console first). But it will still wakeup klogd much earlier and regularly if there is a flood of messages.

Hm, the idea of the patch is that the existing "push everything to slow consoles first, then wakeup syslog" is not very robust. But probably we can do what you suggested, yes.

	-ss
maintainer
#3 lore raw
Quoted 19 lines · Sergey Senozhatsky

On (04/18/18 16:04), Petr Mladek wrote:
[..]

Quoted 8 lines
Quoted 8 lines
kernel/printk/printk.c +0 -0
index 2f4af216bd6e..86f0b337cbf6 100644
@@ -1888,6 +1888,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 
 	printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
 
Quoted 8 lines

kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1892

Quoted 2 lines
kernel/printk/printk.c +1 -0
+	wake_up_klogd();
 	logbuf_unlock_irqrestore(flags);

In fact, I would feel more comfortable if we move this to the end of vprintk_emit() right before return printk_len. This will be more close to the current behavior (console first). But it will still wakeup klogd much earlier and regularly if there is a flood of messages.

Quoted 2 lines

Hm, the idea of the patch is that the existing "push everything to slow consoles first, then wakeup syslog" is not very robust.

I guess that you know why I suggested what I suggested ;-) Just for record, I thought the following way:

The obsession about getting messages on consoles ASAP blocked any softlockup prevention for years. There are surely situations where this matter and we need to keep this in mind.

Loggers might preempt the current task because of priority inversion. It is rather a corner case. The priority inversion is problem on its own that probably do not have a good solution.

Our main target is to increase the chance to see the messages. In theory userspace loggers might be faster than a slow console. But they are not much reliable by definition. They need to get scheduled, copy the message, store it into a file, the file need to get synced to the disk. There are many things that might get wrong there.

All in all, neither solution would work better in all situations. I would just feel more comfortable if we do not go to the other extreme, ...

Quoted 1 line

But probably we can do what you suggested, yes.

Thanks a lot for using this in v2.

Best Regards,
Petr

#4 lore raw
In reply to Sergey Senozhatsky

kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1892

Quoted 8 lines · On Sat, 14 Apr 2018 12:01:45 +0900 Sergey Senozhatsky…
kernel/printk/printk.c +1 -0
@@ -1888,6 +1888,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 
 	printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
 
+	wake_up_klogd();
 	logbuf_unlock_irqrestore(flags);

You can't do this, because the scheduler can call printk_deferred() with the rq lock held, and printk_deferred() will grab the logbuf lock.

Calling wake_up_klogd() will grab the rq lock and give us a A-B<->B-A locking order.

-- Steve

maintainer
#5 lore raw
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Sat, 14 Apr 2018 12:01:45 +0900 Sergey Senozhatsky…
kernel/printk/printk.c +0 -0
@@ -1888,6 +1888,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 
 	printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
 
Quoted 8 lines

kernel/printk/printk.c asmlinkage int vprintk_emit(int facility, int level, · line 1892

Quoted 2 lines
kernel/printk/printk.c +1 -0
+	wake_up_klogd();
 	logbuf_unlock_irqrestore(flags);

You can't do this, because the scheduler can call printk_deferred() with the rq lock held, and printk_deferred() will grab the logbuf lock.

Calling wake_up_klogd() will grab the rq lock and give us a A-B<->B-A locking order.

wake_up_klogd() uses the lockless irq_work_queue(). So it is actually safe.

But the name is confusing. We should rename it.

Best Regards,
Petr

#6 lore raw
Quoted 5 lines · On Wed, 18 Apr 2018 17:02:14 +0200 Petr Mladek <[email protected]> wrote:
Quoted 2 lines

Calling wake_up_klogd() will grab the rq lock and give us a A-B<->B-A locking order.

wake_up_klogd() uses the lockless irq_work_queue(). So it is actually safe.

I didn't look at the code. OK then we don't need to worry about that.

Quoted 2 lines

But the name is confusing. We should rename it.

Yes, I would because the old wake_up_klogd() did do a wakeup. Perhaps we should name it: kick_klogd().

-- Steve

#7 lore raw
Quoted 8 lines · Steven Rostedt
Quoted 6 lines
Quoted 3 lines

Calling wake_up_klogd() will grab the rq lock and give us a A-B<->B-A locking order.

wake_up_klogd() uses the lockless irq_work_queue(). So it is actually safe.

I didn't look at the code. OK then we don't need to worry about that.

OK.

Quoted 5 lines
Quoted 2 lines

But the name is confusing. We should rename it.

Yes, I would because the old wake_up_klogd() did do a wakeup. Perhaps we should name it: kick_klogd().

Agreed.

	-ss
#8 lore raw

PATCH v2 1/1 printk: wake up klogd in vprintk_emit

In reply to Sergey Senozhatsky

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

---
 kernel/printk/printk.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
kernel/printk/printk.c +2 -12
index 2f4af216bd6e..247808333ba4 100644
@@ -1908,6 +1908,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 		preempt_enable();
 	}
 
+	wake_up_klogd();
 	return printed_len;
 }
 EXPORT_SYMBOL(vprintk_emit);
@@ -2289,9 +2290,7 @@ void console_unlock(void)
 {
 	static char ext_text[CONSOLE_EXT_LOG_MAX];
 	static char text[LOG_LINE_MAX + PREFIX_MAX];
-	static u64 seen_seq;
 	unsigned long flags;
-	bool wake_klogd = false;
 	bool do_cond_resched, retry;
 
 	if (console_suspended) {
@@ -2335,11 +2334,6 @@ void console_unlock(void)
 
 		printk_safe_enter_irqsave(flags);
 		raw_spin_lock(&logbuf_lock);
-		if (seen_seq != log_next_seq) {
-			wake_klogd = true;
-			seen_seq = log_next_seq;
-		}
-
 		if (console_seq < log_first_seq) {
 			len = sprintf(text, "** %u printk messages dropped **\n",
 				      (unsigned)(log_first_seq - console_seq));
@@ -2397,7 +2391,7 @@ void console_unlock(void)
 
 		if (console_lock_spinning_disable_and_check()) {
 			printk_safe_exit_irqrestore(flags);
-			goto out;
+			return;
 		}
 
 		printk_safe_exit_irqrestore(flags);
@@ -2429,10 +2423,6 @@ void console_unlock(void)
 
 	if (retry && console_trylock())
 		goto again;
-
-out:
-	if (wake_klogd)
-		wake_up_klogd();
 }
 EXPORT_SYMBOL(console_unlock);
 
Signature · 1 line
2.17.0
maintainer Reviewed-by on v2 1/1
#9 lore raw
Quoted 10 lines · Sergey Senozhatsky

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

The last two lines need an update. What about?

"right after the attempt to handle consoles. As a result, klog will get waken either after flushing the new message to consoles or immediately when consoles are still busy with older messages."

Otherwise, it looks nice:

Best Regards,
Petr

#10 lore raw
Quoted 7 lines · Petr Mladek
Quoted 6 lines · Sergey Senozhatsky

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

Quoted 6 lines
Quoted 4 lines

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

The last two lines need an update. What about?

Ah. Indeed!

Quoted 3 lines

"right after the attempt to handle consoles. As a result, klog will get waken either after flushing the new message to consoles or immediately when consoles are still busy with older messages."

Looks good. Do you want me to resend the patch?

Quoted 3 lines

Otherwise, it looks nice:

Thanks.

	-ss
maintainer
#11 lore raw
Quoted 15 lines · Sergey Senozhatsky
Quoted 7 lines · Petr Mladek
Quoted 6 lines · Sergey Senozhatsky

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

Quoted 6 lines
Quoted 4 lines

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

The last two lines need an update. What about?

Quoted 7 lines

Ah. Indeed!

Quoted 3 lines

"right after the attempt to handle consoles. As a result, klog will get waken either after flushing the new message to consoles or immediately when consoles are still busy with older messages."

Looks good. Do you want me to resend the patch?

No need to resend. I could update the patch when pushing.

I'll wait few more days to give people time to respond.

Best Regards,
Petr

maintainer
#12 lore raw
In reply to Sergey Senozhatsky
Quoted 15 lines · Sergey Senozhatsky
Quoted 7 lines · Petr Mladek
Quoted 6 lines · Sergey Senozhatsky

We wake up klogd very late - only when current console_sem owner is done pushing pending kernel messages to the serial/net consoles. In some cases this results in lost syslog messages, because kernel log buffer is a circular buffer and if we don't wakeup syslog long enough there are chances that logbuf simply will wrap around.

Quoted 6 lines
Quoted 4 lines

The patch moves the klog wake up call to vprintk_emit(), which is the only legit way for a kernel message to appear in the logbuf, right before we attempt to grab the console_sem (possibly spinning on it waiting for the hand off) and call console drivers.

The last two lines need an update. What about?

Quoted 7 lines

Ah. Indeed!

Quoted 3 lines

"right after the attempt to handle consoles. As a result, klog will get waken either after flushing the new message to consoles or immediately when consoles are still busy with older messages."

Looks good. Do you want me to resend the patch?

JFYI, I have updated the commit message and pushed the patch into printk.git, for-4.18 branch.

Best Regards,
Petr

#13 lore raw
Quoted 5 lines · Petr Mladek
Quoted 2 lines

Looks good. Do you want me to resend the patch?

JFYI, I have updated the commit message and pushed the patch into printk.git, for-4.18 branch.

Thanks!

	-ss