Skip to contentSkip to conversation

printk/for-next

PATCH Superseded John Ogness 3 participants 39 messages Apr 19, 2022 – Apr 25, 2022

Subsystems (6)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

John Ogness

author maintainer
#0 lore raw

v3 cover printk/for-next

This is v3 of a series to implement a kthread for each registered console. v2 is here [0]. The kthreads locklessly retrieve the records from the printk ringbuffer and also do not cause any lock contention between each other. This allows consoles to run at full speed. For example, a netconsole is able to dump records much faster than a serial or vt console. Also, during normal operation, printk() callers are completely decoupled from console printing.

There are situations where kthread printing is not sufficient. For example, during panic situations, where the kthreads may not get a chance to schedule. In such cases, the current method of attempting to print directly within the printk() caller context is used. New functions printk_prefer_direct_enter() and printk_prefer_direct_exit() are made available to mark areas of the kernel where direct printing is preferred. (These should only be areas that do not occur during normal operation.)

This series also introduces pr_flush(): a might_sleep() function that will block until all active printing threads have caught up to the latest record at the time of the pr_flush() call. This function is useful, for example, to wait until pending records are flushed to consoles before suspending.

Note that this series does *not* increase the reliability of console printing. Rather it focuses on the non-interference aspect of printk() by decoupling printk() callers from printing (during normal operation). Nonetheless, the reliability aspect should not worsen due to this series.

John Ogness

[0] https://lore.kernel.org/lkml/[email protected]

Changes since v2:

- Threaded printers no longer care about preferred direct printing. As with v1, they will print whenever they are not blocked.

- Provide a separate patch to fix a missing memory barrier in wake_up_klogd() and add memory barrier comments to all appropriate @log_wait usage sites.

- Provide a separate patch to wake all waiters.

- Provide a separate patch to wake waiters for deferred console output and add comments explaining why.

- Introduce console_lock_single_hold() and console_unlock_single_release() to acquire @console_sem and lock a single threaded printer. This allows console start/stop and console unregistration with synchronized con->flags and without disturbing other threaded printers.

- Introduce __console_is_usable() to avoid some redundance between threaded and direct printing code.

- Do not create a printer thread if con->write() is not set. (I do not understand why we even allow registration if con->write() is not set. The checks were added in 2.1.31 for no obvious reason.)

- Only allow handovers between console_trylock() contexts. A console_lock() context cannot handover the console_lock to a console_trylock() context because the blocked kthreads would need to be unblocked via mutex.

- console_flush_all() returns true only if at least one console is usable and all messages to all usable consoles were printed. Otherwise it returns false.

- Remove redundant panic check in console_unlock().

- Rename printk_console_msg() to con_printk() and use syntax similar to dev_printk(). (I did not name it console_printk() because there already exists a symbol with that name.)

- Remove blocked check in register_console() since it is always true.

- In unregister_console(), stop the kthread after the console has been removed from the list. Use the per-console mutex for synchronized kthread stopping.

- Use the console_lock for synchronized activation of the fallback permanent direct printing mode.

- Use the same checks in printer_should_wake() as in printk_kthread_func() to avoid infinite loop danger.

- Rename PRINTK_PENDING_OUTPUT flag to PRINTK_PENDING_DIRECT_OUTPUT.

- Expand commit messages relating to memory barriers, kthreads, and the usage of the per-console mutex.

John Ogness (15):
printk: rename cpulock functions
printk: cpu sync always disable interrupts
printk: add missing memory barrier to wake_up_klogd()
printk: wake up all waiters
printk: wake waiters for safe and NMI contexts
printk: get caller_id/timestamp after migration disable
printk: call boot_delay_msec() in printk_delay()
printk: add con_printk() macro for console details
printk: refactor and rework printing logic
printk: move buffer definitions into console_emit_next_record() caller
printk: add pr_flush()
printk: add functions to prefer direct printing
printk: add kthread console printers
printk: extend console_lock for proper kthread support
printk: remove @console_locked

 drivers/tty/sysrq.c     |    2 +
 include/linux/console.h |   19 +
 include/linux/printk.h  |   82 ++-
 kernel/hung_task.c      |   11 +-
 kernel/panic.c          |    4 +
 kernel/printk/printk.c  | 1197 +++++++++++++++++++++++++++++----------
 kernel/rcu/tree_stall.h |    2 +
 kernel/reboot.c         |   14 +-
 kernel/watchdog.c       |    4 +
 kernel/watchdog_hld.c   |    4 +
 lib/dump_stack.c        |    4 +-
 lib/nmi_backtrace.c     |    4 +-
 12 files changed, 1021 insertions(+), 326 deletions(-)

base-commit: 84d7df104dbab9c3dda8f2c5b46f9a6fc256fe02

Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH v3 1/15 printk: rename cpulock functions

Since the printk cpulock is CPU-reentrant and since it is used in all contexts, its usage must be carefully considered and most likely will require programming locklessly. To avoid mistaking the printk cpulock as a typical lock, rename it to cpu_sync. The main functions then become:

    printk_cpu_sync_get_irqsave(flags);
    printk_cpu_sync_put_irqrestore(flags);

Add extra notes of caution in the function description to help developers understand the requirements for correct usage.

---
 include/linux/printk.h | 54 +++++++++++++++++++-------------
 kernel/printk/printk.c | 71 +++++++++++++++++++++---------------------
 lib/dump_stack.c       |  4 +--
 lib/nmi_backtrace.c    |  4 +--
 4 files changed, 73 insertions(+), 60 deletions(-)
include/linux/printk.h +33 -21
index 1522df223c0f..859323a52985 100644
@@ -277,43 +277,55 @@ static inline void printk_trigger_flush(void)
 #endif
 
 #ifdef CONFIG_SMP
-extern int __printk_cpu_trylock(void);
-extern void __printk_wait_on_cpu_lock(void);
-extern void __printk_cpu_unlock(void);
+extern int __printk_cpu_sync_try_get(void);
+extern void __printk_cpu_sync_wait(void);
+extern void __printk_cpu_sync_put(void);
 
 /**
- * printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning
- *                             lock and disable interrupts.
+ * printk_cpu_sync_get_irqsave() - Acquire the printk cpu-reentrant spinning
+ *                                 lock and disable interrupts.
  * @flags: Stack-allocated storage for saving local interrupt state,
- *         to be passed to printk_cpu_unlock_irqrestore().
+ *         to be passed to printk_cpu_sync_put_irqrestore().
  *
  * If the lock is owned by another CPU, spin until it becomes available.
  * Interrupts are restored while spinning.
+ *
+ * CAUTION: This function must be used carefully. It does not behave like a
+ * typical lock. Here are important things to watch out for...
+ *
+ *     * This function is reentrant on the same CPU. Therefore the calling
+ *       code must not assume exclusive access to data if code accessing the
+ *       data can run reentrant or within NMI context on the same CPU.
+ *
+ *     * If there exists usage of this function from NMI context, it becomes
+ *       unsafe to perform any type of locking or spinning to wait for other
+ *       CPUs after calling this function from any context. This includes
+ *       using spinlocks or any other busy-waiting synchronization methods.
  */
-#define printk_cpu_lock_irqsave(flags)		\
-	for (;;) {				\
-		local_irq_save(flags);		\
-		if (__printk_cpu_trylock())	\
-			break;			\
-		local_irq_restore(flags);	\
-		__printk_wait_on_cpu_lock();	\
+#define printk_cpu_sync_get_irqsave(flags)		\
+	for (;;) {					\
+		local_irq_save(flags);			\
+		if (__printk_cpu_sync_try_get())	\
+			break;				\
+		local_irq_restore(flags);		\
+		__printk_cpu_sync_wait();		\
 	}
 
 /**
- * printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinning
- *                                  lock and restore interrupts.
- * @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave().
+ * printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spinning
+ *                                    lock and restore interrupts.
+ * @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsave().
  */
-#define printk_cpu_unlock_irqrestore(flags)	\
+#define printk_cpu_sync_put_irqrestore(flags)	\
 	do {					\
-		__printk_cpu_unlock();		\
+		__printk_cpu_sync_put();	\
 		local_irq_restore(flags);	\
-	} while (0)				\
+	} while (0)
 
 #else
 
-#define printk_cpu_lock_irqsave(flags) ((void)flags)
-#define printk_cpu_unlock_irqrestore(flags) ((void)flags)
+#define printk_cpu_sync_get_irqsave(flags) ((void)flags)
+#define printk_cpu_sync_put_irqrestore(flags) ((void)flags)
 
 #endif /* CONFIG_SMP */
 
kernel/printk/printk.c +36 -35

148 lines not on this page · raw message

lib/dump_stack.c +2 -2
index 6b7f1bf6715d..83471e81501a 100644
@@ -102,9 +102,9 @@ asmlinkage __visible void dump_stack_lvl(const char *log_lvl)
 	 * Permit this cpu to perform nested stack dumps while serialising
 	 * against other CPUs
 	 */
-	printk_cpu_lock_irqsave(flags);
+	printk_cpu_sync_get_irqsave(flags);
 	__dump_stack(log_lvl);
-	printk_cpu_unlock_irqrestore(flags);
+	printk_cpu_sync_put_irqrestore(flags);
 }
 EXPORT_SYMBOL(dump_stack_lvl);
 
lib/nmi_backtrace.c +2 -2
index 199ab201d501..d01aec6ae15c 100644
@@ -99,7 +99,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)
 		 * Allow nested NMI backtraces while serializing
 		 * against other CPUs.
 		 */
-		printk_cpu_lock_irqsave(flags);
+		printk_cpu_sync_get_irqsave(flags);
 		if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointer(regs))) {
 			pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n",
 				cpu, (void *)instruction_pointer(regs));
@@ -110,7 +110,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)
 			else
 				dump_stack();
 		}
-		printk_cpu_unlock_irqrestore(flags);
+		printk_cpu_sync_put_irqrestore(flags);
 		cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
 		return true;
 	}
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH v3 2/15 printk: cpu sync always disable interrupts

In reply to John Ogness

The CPU sync functions are a NOP for !CONFIG_SMP. But for !CONFIG_SMP they still need to disable interrupts in order to preserve context within the CPU sync sections.

---
 include/linux/printk.h | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/printk.h +9 -9
index 859323a52985..b70a42f94031 100644
@@ -281,9 +281,16 @@ extern int __printk_cpu_sync_try_get(void);
 extern void __printk_cpu_sync_wait(void);
 extern void __printk_cpu_sync_put(void);
 
+#else
+
+#define __printk_cpu_sync_try_get() true
+#define __printk_cpu_sync_wait()
+#define __printk_cpu_sync_put()
+#endif /* CONFIG_SMP */
+
 /**
- * printk_cpu_sync_get_irqsave() - Acquire the printk cpu-reentrant spinning
- *                                 lock and disable interrupts.
+ * printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the printk
+ *                                 cpu-reentrant spinning lock.
  * @flags: Stack-allocated storage for saving local interrupt state,
  *         to be passed to printk_cpu_sync_put_irqrestore().
  *
@@ -322,13 +329,6 @@ extern void __printk_cpu_sync_put(void);
 		local_irq_restore(flags);	\
 	} while (0)
 
-#else
-
-#define printk_cpu_sync_get_irqsave(flags) ((void)flags)
-#define printk_cpu_sync_put_irqrestore(flags) ((void)flags)
-
-#endif /* CONFIG_SMP */
-
 extern int kptr_restrict;
 
 /**
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH v3 3/15 printk: add missing memory barrier to wake_up_klogd()

In reply to John Ogness

It is important that any new records are visible to preparing waiters before the waker checks if the wait queue is empty. Otherwise it is possible that:

- there are new records available
- the waker sees an empty wait queue and does not wake
- the preparing waiter sees no new records and begins to wait

This is exactly the problem that the function description of waitqueue_active() warns about.

Use wq_has_sleeper() instead of waitqueue_active() because it includes the necessary full memory barrier.

---
 kernel/printk/printk.c | 39 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)
kernel/printk/printk.c +36 -3
index 13a1eebe72af..f817dfb4852d 100644
@@ -746,8 +746,19 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
 			goto out;
 		}
 
+		/*
+		 * Guarantee this task is visible on the waitqueue before
+		 * checking the wake condition.
+		 *
+		 * The full memory barrier within set_current_state() of
+		 * prepare_to_wait_event() pairs with the full memory barrier
+		 * within wq_has_sleeper().
+		 *
+		 * This pairs with wake_up_klogd:A.
+		 */
 		ret = wait_event_interruptible(log_wait,
-				prb_read_valid(prb, atomic64_read(&user->seq), r));
+				prb_read_valid(prb,
+					atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */
 		if (ret)
 			goto out;
 	}
@@ -1513,7 +1524,18 @@ static int syslog_print(char __user *buf, int size)
 		seq = syslog_seq;
 
 		mutex_unlock(&syslog_lock);
-		len = wait_event_interruptible(log_wait, prb_read_valid(prb, seq, NULL));
+		/*
+		 * Guarantee this task is visible on the waitqueue before
+		 * checking the wake condition.
+		 *
+		 * The full memory barrier within set_current_state() of
+		 * prepare_to_wait_event() pairs with the full memory barrier
+		 * within wq_has_sleeper().
+		 *
+		 * This pairs with wake_up_klogd:A.
+		 */
+		len = wait_event_interruptible(log_wait,
+				prb_read_valid(prb, seq, NULL)); /* LMM(syslog_print:A) */
 		mutex_lock(&syslog_lock);
 
 		if (len)
@@ -3316,7 +3338,18 @@ void wake_up_klogd(void)
 		return;
 
 	preempt_disable();
-	if (waitqueue_active(&log_wait)) {
+	/*
+	 * Guarantee any new records can be seen by tasks preparing to wait
+	 * before this context checks if the wait queue is empty.
+	 *
+	 * The full memory barrier within wq_has_sleeper() pairs with the full
+	 * memory barrier within set_current_state() of
+	 * prepare_to_wait_event(), which is called after ___wait_event() adds
+	 * the waiter but before it has checked the wait condition.
+	 *
+	 * This pairs with devkmsg_read:A and syslog_print:A.
+	 */
+	if (wq_has_sleeper(&log_wait)) { /* LMM(wake_up_klogd:A) */
 		this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
 		irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
 	}
Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 3/15
#4 lore raw

Re: [PATCH printk v3 03/15] printk: add missing memory barrier to wake_up_klogd()

Quoted 8 lines · John Ogness

It is important that any new records are visible to preparing waiters before the waker checks if the wait queue is empty. Otherwise it is possible that:

- there are new records available
- the waker sees an empty wait queue and does not wake
- the preparing waiter sees no new records and begins to wait

Quoted 7 lines

This is exactly the problem that the function description of waitqueue_active() warns about.

Use wq_has_sleeper() instead of waitqueue_active() because it includes the necessary full memory barrier.

Great catch! Looks good to me.

Best Regards,
Petr

author maintainer
#5 lore raw

PATCH v3 4/15 printk: wake up all waiters

In reply to John Ogness

There can be multiple tasks waiting for new records. They should all be woken. Use wake_up_interruptible_all() instead of wake_up_interruptible().

---
 kernel/printk/printk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
index f817dfb4852d..e23357002648 100644
@@ -3326,7 +3326,7 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)
 	}
 
 	if (pending & PRINTK_PENDING_WAKEUP)
-		wake_up_interruptible(&log_wait);
+		wake_up_interruptible_all(&log_wait);
 }
 
 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) =
Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 4/15
#6 lore raw

Re: [PATCH printk v3 04/15] printk: wake up all waiters

Quoted 5 lines · John Ogness

There can be multiple tasks waiting for new records. They should all be woken. Use wake_up_interruptible_all() instead of wake_up_interruptible().

Another great catch!

Best Regards,
Petr

author maintainer
#7 lore raw

PATCH v3 5/15 printk: wake waiters for safe and NMI contexts

In reply to John Ogness

When printk() is called from safe or NMI contexts, it will directly store the record (vprintk_store()) and then defer the console output. However, defer_console_output() only causes console printing and does not wake any waiters of new records.

Wake waiters from defer_console_output() so that they also are aware of the new records from safe and NMI contexts.

Fixes 03fc7f9c99c1 ("printk/nmi: Prevent deadlock when accessing the main log buffer in NMI")
---
 kernel/printk/printk.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
kernel/printk/printk.c +16 -12
index e23357002648..7bb148a1debb 100644
@@ -754,7 +754,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
 		 * prepare_to_wait_event() pairs with the full memory barrier
 		 * within wq_has_sleeper().
 		 *
-		 * This pairs with wake_up_klogd:A.
+		 * This pairs with __wake_up_klogd:A.
 		 */
 		ret = wait_event_interruptible(log_wait,
 				prb_read_valid(prb,
@@ -1532,7 +1532,7 @@ static int syslog_print(char __user *buf, int size)
 		 * prepare_to_wait_event() pairs with the full memory barrier
 		 * within wq_has_sleeper().
 		 *
-		 * This pairs with wake_up_klogd:A.
+		 * This pairs with __wake_up_klogd:A.
 		 */
 		len = wait_event_interruptible(log_wait,
 				prb_read_valid(prb, seq, NULL)); /* LMM(syslog_print:A) */
@@ -3332,7 +3332,7 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)
 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) =
 	IRQ_WORK_INIT_LAZY(wake_up_klogd_work_func);
 
-void wake_up_klogd(void)
+static void __wake_up_klogd(int val)
 {
 	if (!printk_percpu_data_ready())
 		return;
@@ -3349,22 +3349,26 @@ void wake_up_klogd(void)
 	 *
 	 * This pairs with devkmsg_read:A and syslog_print:A.
 	 */
-	if (wq_has_sleeper(&log_wait)) { /* LMM(wake_up_klogd:A) */
-		this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
+	if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */
+	    (val & PRINTK_PENDING_OUTPUT)) {
+		this_cpu_or(printk_pending, val);
 		irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
 	}
 	preempt_enable();
 }
 
-void defer_console_output(void)
+void wake_up_klogd(void)
 {
-	if (!printk_percpu_data_ready())
-		return;
+	__wake_up_klogd(PRINTK_PENDING_WAKEUP);
+}
 
-	preempt_disable();
-	this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
-	irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
-	preempt_enable();
+void defer_console_output(void)
+{
+	/*
+	 * New messages may have been added directly to the ringbuffer
+	 * using vprintk_store(), so wake any waiters as well.
+	 */
+	__wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT);
 }
 
 void printk_trigger_flush(void)
Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 5/15
#8 lore raw

Re: [PATCH printk v3 05/15] printk: wake waiters for safe and NMI contexts

Quoted 10 lines · John Ogness

When printk() is called from safe or NMI contexts, it will directly store the record (vprintk_store()) and then defer the console output. However, defer_console_output() only causes console printing and does not wake any waiters of new records.

Wake waiters from defer_console_output() so that they also are aware of the new records from safe and NMI contexts.

Fixes 03fc7f9c99c1 ("printk/nmi: Prevent deadlock when accessing the main log buffer in NMI")

Another great catch!

Best Regards,
Petr

author maintainer
#9 lore raw

PATCH v3 6/15 printk: get caller_id/timestamp after migration disable

In reply to John Ogness

Currently the local CPU timestamp and caller_id for the record are collected while migration is enabled. Since this information is CPU-specific, it should be collected with migration disabled.

Migration is disabled immediately after collecting this information anyway, so just move the information collection to after the migration disabling.

---
 kernel/printk/printk.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/printk/printk.c +6 -4
index 7bb148a1debb..82ad3d3d0d4a 100644
@@ -2063,7 +2063,7 @@ static inline void printk_delay(void)
 static inline u32 printk_caller_id(void)
 {
 	return in_task() ? task_pid_nr(current) :
-		0x80000000 + raw_smp_processor_id();
+		0x80000000 + smp_processor_id();
 }
 
 /**
@@ -2145,7 +2145,6 @@ int vprintk_store(int facility, int level,
 		  const struct dev_printk_info *dev_info,
 		  const char *fmt, va_list args)
 {
-	const u32 caller_id = printk_caller_id();
 	struct prb_reserved_entry e;
 	enum printk_info_flags flags = 0;
 	struct printk_record r;
@@ -2155,10 +2154,14 @@ int vprintk_store(int facility, int level,
 	u8 *recursion_ptr;
 	u16 reserve_size;
 	va_list args2;
+	u32 caller_id;
 	u16 text_len;
 	int ret = 0;
 	u64 ts_nsec;
 
+	if (!printk_enter_irqsave(recursion_ptr, irqflags))
+		return 0;
+
 	/*
 	 * Since the duration of printk() can vary depending on the message
 	 * and state of the ringbuffer, grab the timestamp now so that it is
@@ -2167,8 +2170,7 @@ int vprintk_store(int facility, int level,
 	 */
 	ts_nsec = local_clock();
 
-	if (!printk_enter_irqsave(recursion_ptr, irqflags))
-		return 0;
+	caller_id = printk_caller_id();
 
 	/*
 	 * The sprintf needs to come first since the syslog prefix might be
Signature · 1 line
2.30.2
author maintainer
#10 lore raw

PATCH v3 7/15 printk: call boot_delay_msec() in printk_delay()

In reply to John Ogness

boot_delay_msec() is always called immediately before printk_delay() so just call it from within printk_delay().

---
 kernel/printk/printk.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/printk/printk.c +4 -3
index 82ad3d3d0d4a..2f99e0b383b9 100644
@@ -2048,8 +2048,10 @@ static u8 *__printk_recursion_counter(void)
 
 int printk_delay_msec __read_mostly;
 
-static inline void printk_delay(void)
+static inline void printk_delay(int level)
 {
+	boot_delay_msec(level);
+
 	if (unlikely(printk_delay_msec)) {
 		int m = printk_delay_msec;
 
@@ -2274,8 +2276,7 @@ asmlinkage int vprintk_emit(int facility, int level,
 		in_sched = true;
 	}
 
-	boot_delay_msec(level);
-	printk_delay();
+	printk_delay(level);
 
 	printed_len = vprintk_store(facility, level, dev_info, fmt, args);
 
Signature · 1 line
2.30.2
author maintainer
#11 lore raw

PATCH v3 8/15 printk: add con_printk() macro for console details

In reply to John Ogness

It is useful to generate log messages that include details about the related console. Rather than duplicate the code to assemble the details, put that code into a macro con_printk().

Once console printers become threaded, this macro will find more users.

---
 kernel/printk/printk.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
kernel/printk/printk.c +7 -6
index 2f99e0b383b9..e36d3ed41afa 100644
@@ -3015,6 +3015,11 @@ static void try_enable_default_console(struct console *newcon)
 		newcon->flags |= CON_CONSDEV;
 }
 
+#define con_printk(lvl, con, fmt, ...)			\
+	printk(lvl pr_fmt("%sconsole [%s%d] " fmt),	\
+	       (con->flags & CON_BOOT) ? "boot" : "",	\
+	       con->name, con->index, ##__VA_ARGS__)
+
 /*
  * The console driver calls this routine during kernel initialization
  * to register the console printing procedure with printk() and to
@@ -3153,9 +3158,7 @@ void register_console(struct console *newcon)
 	 * users know there might be something in the kernel's log buffer that
 	 * went to the bootconsole (that they do not see on the real console)
 	 */
-	pr_info("%sconsole [%s%d] enabled\n",
-		(newcon->flags & CON_BOOT) ? "boot" : "" ,
-		newcon->name, newcon->index);
+	con_printk(KERN_INFO, newcon, "enabled\n");
 	if (bootcon_enabled &&
 	    ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
 	    !keep_bootcon) {
@@ -3174,9 +3177,7 @@ int unregister_console(struct console *console)
 	struct console *con;
 	int res;
 
-	pr_info("%sconsole [%s%d] disabled\n",
-		(console->flags & CON_BOOT) ? "boot" : "" ,
-		console->name, console->index);
+	con_printk(KERN_INFO, console, "disabled\n");
 
 	res = _braille_unregister_console(console);
 	if (res < 0)
Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 8/15
#12 lore raw

Re: [PATCH printk v3 08/15] printk: add con_printk() macro for console details

Quoted 8 lines · John Ogness

It is useful to generate log messages that include details about the related console. Rather than duplicate the code to assemble the details, put that code into a macro con_printk().

Once console printers become threaded, this macro will find more users.

Macro magic ;-) Looks good to me.

Best Regards,
Petr

author maintainer
#13 lore raw

PATCH v3 9/15 printk: refactor and rework printing logic

In reply to John Ogness

Refactor/rework printing logic in order to prepare for moving to threaded console printing.

- Move @console_seq into struct console so that the current "position" of each console can be tracked individually.

- Move @console_dropped into struct console so that the current drop count of each console can be tracked individually.

- Modify printing logic so that each console independently loads, prepares, and prints its next record.

- Remove exclusive_console logic. Since console positions are handled independently, replaying past records occurs naturally.

- Update the comments explaining why preemption is disabled while printing from printk() context.

With these changes, there is a change in behavior: the console replaying the log (formerly exclusive console) will no longer block other consoles. New messages appear on the other consoles while the newly added console is still replaying.

---
 include/linux/console.h |   2 +
 kernel/printk/printk.c  | 441 +++++++++++++++++++++-------------------
 2 files changed, 230 insertions(+), 213 deletions(-)
include/linux/console.h +2 -0
index 7cd758a4f44e..8c1686e2c233 100644
@@ -151,6 +151,8 @@ struct console {
 	int	cflag;
 	uint	ispeed;
 	uint	ospeed;
+	u64	seq;
+	unsigned long dropped;
 	void	*data;
 	struct	 console *next;
 };
kernel/printk/printk.c +228 -213

556 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 9/15
#14 lore raw

Re: [PATCH printk v3 09/15] printk: refactor and rework printing logic

Quoted 18 lines · John Ogness

Refactor/rework printing logic in order to prepare for moving to threaded console printing.

- Move @console_seq into struct console so that the current "position" of each console can be tracked individually.

- Move @console_dropped into struct console so that the current drop count of each console can be tracked individually.

- Modify printing logic so that each console independently loads, prepares, and prints its next record.

- Remove exclusive_console logic. Since console positions are handled independently, replaying past records occurs naturally.

- Update the comments explaining why preemption is disabled while printing from printk() context.

Quoted 6 lines

With these changes, there is a change in behavior: the console replaying the log (formerly exclusive console) will no longer block other consoles. New messages appear on the other consoles while the newly added console is still replaying.

It might be because I have already seen this code several times. But I really like how it splits the monolithic console_unlock().

It might also mean that I have become blind. But I do not see any problem anymore ;-)

Best Regards,
Petr

author maintainer
#15 lore raw

PATCH v3 10/15 printk: move buffer definitions into console_emit_next_record() caller

In reply to John Ogness

Extended consoles print extended messages and do not print messages about dropped records.

Non-extended consoles print "normal" messages as well as extra messages about dropped records.

Currently the buffers for these various message types are defined within the functions that might use them and their usage is based upon the CON_EXTENDED flag. This will be a problem when moving to kthread printers because each printer must be able to provide its own buffers.

Move all the message buffer definitions outside of console_emit_next_record(). The caller knows if extended or dropped messages should be printed and can specify the appropriate buffers to use. The console_emit_next_record() and call_console_driver() functions can know what to print based on whether specified buffers are non-NULL.

With this change, buffer definition/allocation/specification is separated from the code that does the various types of string printing.

---
 kernel/printk/printk.c | 60 ++++++++++++++++++++++++++++++------------
 1 file changed, 43 insertions(+), 17 deletions(-)
kernel/printk/printk.c +43 -17
index 3dea8bbaf402..dec5355c5b5b 100644
@@ -394,6 +394,9 @@ static struct latched_seq clear_seq = {
 /* the maximum size of a formatted record (i.e. with prefix added per line) */
 #define CONSOLE_LOG_MAX		1024
 
+/* the maximum size for a dropped text message */
+#define DROPPED_TEXT_MAX	64
+
 /* the maximum size allowed to be reserved for a record */
 #define LOG_LINE_MAX		(CONSOLE_LOG_MAX - PREFIX_MAX)
 
@@ -1923,18 +1926,18 @@ static int console_trylock_spinning(void)
 
 /*
  * Call the specified console driver, asking it to write out the specified
- * text and length. For non-extended consoles, if any records have been
+ * text and length. If @dropped_text is non-NULL and any records have been
  * dropped, a dropped message will be written out first.
  */
-static void call_console_driver(struct console *con, const char *text, size_t len)
+static void call_console_driver(struct console *con, const char *text, size_t len,
+				char *dropped_text)
 {
-	static char dropped_text[64];
 	size_t dropped_len;
 
 	trace_console_rcuidle(text, len);
 
-	if (con->dropped && !(con->flags & CON_EXTENDED)) {
-		dropped_len = snprintf(dropped_text, sizeof(dropped_text),
+	if (con->dropped && dropped_text) {
+		dropped_len = snprintf(dropped_text, DROPPED_TEXT_MAX,
 				       "** %lu printk messages dropped **\n",
 				       con->dropped);
 		con->dropped = 0;
@@ -2296,6 +2299,7 @@ EXPORT_SYMBOL(_printk);
 #else /* CONFIG_PRINTK */
 
 #define CONSOLE_LOG_MAX		0
+#define DROPPED_TEXT_MAX	0
 #define printk_time		false
 
 #define prb_read_valid(rb, seq, r)	false
@@ -2319,7 +2323,10 @@ static ssize_t msg_print_ext_body(char *buf, size_t size,
 				  struct dev_printk_info *dev_info) { return 0; }
 static void console_lock_spinning_enable(void) { }
 static int console_lock_spinning_disable_and_check(void) { return 0; }
-static void call_console_driver(struct console *con, const char *text, size_t len) { }
+static void call_console_driver(struct console *con, const char *text, size_t len,
+				char *dropped_text)
+{
+}
 static bool suppress_message_printing(int level) { return false; }
 
 #endif /* CONFIG_PRINTK */
@@ -2644,6 +2651,14 @@ static void __console_unlock(void)
  * Print one record for the given console. The record printed is whatever
  * record is the next available record for the given console.
  *
+ * @text is a buffer of size CONSOLE_LOG_MAX.
+ *
+ * If extended messages should be printed, @ext_text is a buffer of size
+ * CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL.
+ *
+ * If dropped messages should be printed, @dropped_text is a buffer of size
+ * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL.
+ *
  * @handover will be set to true if a printk waiter has taken over the
  * console_lock, in which case the caller is no longer holding the
  * console_lock. Otherwise it is set to false.
@@ -2653,10 +2668,9 @@ static void __console_unlock(void)
  *
  * Requires the console_lock.
  */
-static bool console_emit_next_record(struct console *con, bool *handover)
+static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
+				     char *dropped_text, bool *handover)
 {
-	static char ext_text[CONSOLE_EXT_LOG_MAX];
-	static char text[CONSOLE_LOG_MAX];
 	static int panic_console_dropped;
 	struct printk_info info;
 	struct printk_record r;
@@ -2664,7 +2678,7 @@ static bool console_emit_next_record(struct console *con, bool *handover)
 	char *write_text;
 	size_t len;
 
-	prb_rec_init_rd(&r, &info, text, sizeof(text));
+	prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
 
 	*handover = false;
 
@@ -2686,13 +2700,13 @@ static bool console_emit_next_record(struct console *con, bool *handover)
 		goto skip;
 	}
 
-	if (con->flags & CON_EXTENDED) {
-		write_text = &ext_text[0];
-		len = info_print_ext_header(ext_text, sizeof(ext_text), r.info);
-		len += msg_print_ext_body(ext_text + len, sizeof(ext_text) - len,
+	if (ext_text) {
+		write_text = ext_text;
+		len = info_print_ext_header(ext_text, CONSOLE_EXT_LOG_MAX, r.info);
+		len += msg_print_ext_body(ext_text + len, CONSOLE_EXT_LOG_MAX - len,
 					  &r.text_buf[0], r.info->text_len, &r.info->dev_info);
 	} else {
-		write_text = &text[0];
+		write_text = text;
 		len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time);
 	}
 
@@ -2710,7 +2724,7 @@ static bool console_emit_next_record(struct console *con, bool *handover)
 	console_lock_spinning_enable();
 
 	stop_critical_timings();	/* don't trace print latency */
-	call_console_driver(con, write_text, len);
+	call_console_driver(con, write_text, len, dropped_text);
 	start_critical_timings();
 
 	con->seq++;
@@ -2746,6 +2760,9 @@ static bool console_emit_next_record(struct console *con, bool *handover)
  */
 static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
 {
+	static char dropped_text[DROPPED_TEXT_MAX];
+	static char ext_text[CONSOLE_EXT_LOG_MAX];
+	static char text[CONSOLE_LOG_MAX];
 	bool any_usable = false;
 	struct console *con;
 	bool any_progress;
@@ -2763,7 +2780,16 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
 				continue;
 			any_usable = true;
 
-			progress = console_emit_next_record(con, handover);
+			if (con->flags & CON_EXTENDED) {
+				/* Extended consoles do not print "dropped messages". */
+				progress = console_emit_next_record(con, &text[0],
+								    &ext_text[0], NULL,
+								    handover);
+			} else {
+				progress = console_emit_next_record(con, &text[0],
+								    NULL, &dropped_text[0],
+								    handover);
+			}
 			if (*handover)
 				return false;
 
Signature · 1 line
2.30.2
author maintainer
#16 lore raw

PATCH v3 11/15 printk: add pr_flush()

In reply to John Ogness

Provide a might-sleep function to allow waiting for console printers to catch up to the latest logged message.

Use pr_flush() whenever it is desirable to get buffered messages printed before continuing: suspend_console(), resume_console(), console_stop(), console_start(), console_unblank().

---
 include/linux/printk.h |  7 ++++
 kernel/printk/printk.c | 83 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)
include/linux/printk.h +7 -0
index b70a42f94031..091fba7283e1 100644
@@ -170,6 +170,8 @@ extern void __printk_safe_exit(void);
 #define printk_deferred_enter __printk_safe_enter
 #define printk_deferred_exit __printk_safe_exit
 
+extern bool pr_flush(int timeout_ms, bool reset_on_progress);
+
 /*
  * Please don't use printk_ratelimit(), because it shares ratelimiting state
  * with all other unrelated printk_ratelimit() callsites.  Instead use
@@ -220,6 +222,11 @@ static inline void printk_deferred_exit(void)
 {
 }
 
+static inline bool pr_flush(int timeout_ms, bool reset_on_progress)
+{
+	return true;
+}
+
 static inline int printk_ratelimit(void)
 {
 	return 0;
kernel/printk/printk.c +83 -0

131 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer Reviewed-by on v3 11/15
#17 lore raw

Re: [PATCH printk v3 11/15] printk: add pr_flush()

Quoted 8 lines · John Ogness

Provide a might-sleep function to allow waiting for console printers to catch up to the latest logged message.

Use pr_flush() whenever it is desirable to get buffered messages printed before continuing: suspend_console(), resume_console(), console_stop(), console_start(), console_unblank().

Best Regards,
Petr

author maintainer
#18 lore raw

PATCH v3 12/15 printk: add functions to prefer direct printing

In reply to John Ogness

Once kthread printing is available, console printing will no longer occur in the context of the printk caller. However, there are some special contexts where it is desirable for the printk caller to directly print out kernel messages. Using pr_flush() to wait for threaded printers is only possible if the caller is in a sleepable context and the kthreads are active. That is not always the case.

Introduce printk_prefer_direct_enter() and printk_prefer_direct_exit() functions to explicitly (and globally) activate/deactivate preferred direct console printing. The term "direct console printing" refers to printing to all enabled consoles from the context of the printk caller. The term "prefer" is used because this type of printing is only best effort. If the console is currently locked or other printers are already actively printing, the printk caller will need to rely on the other contexts to handle the printing.

This preferred direct printing is how all printing has been handled until now (unless it was explicitly deferred).

When kthread printing is introduced, there may be some unanticipated problems due to kthreads being unable to flush important messages. In order to minimize such risks, preferred direct printing is activated for the primary important messages when the system experiences general types of major errors. These are:

- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrq

Note that since kthread printing does not yet exist, no behavior changes result from this commit. This is only implementing the counter and marking the various places where preferred direct printing is active.

---
 drivers/tty/sysrq.c     |  2 ++
 include/linux/printk.h  | 11 +++++++++++
 kernel/hung_task.c      | 11 ++++++++++-
 kernel/panic.c          |  4 ++++
 kernel/printk/printk.c  | 28 ++++++++++++++++++++++++++++
 kernel/rcu/tree_stall.h |  2 ++
 kernel/reboot.c         | 14 +++++++++++++-
 kernel/watchdog.c       |  4 ++++
 kernel/watchdog_hld.c   |  4 ++++
 9 files changed, 78 insertions(+), 2 deletions(-)
drivers/tty/sysrq.c +2 -0
index bbfd004449b5..2884cd638d64 100644
@@ -578,6 +578,7 @@ void __handle_sysrq(int key, bool check_mask)
 
 	rcu_sysrq_start();
 	rcu_read_lock();
+	printk_prefer_direct_enter();
 	/*
 	 * Raise the apparent loglevel to maximum so that the sysrq header
 	 * is shown to provide the user with positive feedback.  We do not
@@ -619,6 +620,7 @@ void __handle_sysrq(int key, bool check_mask)
 		pr_cont("\n");
 		console_loglevel = orig_log_level;
 	}
+	printk_prefer_direct_exit();
 	rcu_read_unlock();
 	rcu_sysrq_end();
 
include/linux/printk.h +11 -0
index 091fba7283e1..cd26aab0ab2a 100644
@@ -170,6 +170,9 @@ extern void __printk_safe_exit(void);
 #define printk_deferred_enter __printk_safe_enter
 #define printk_deferred_exit __printk_safe_exit
 
+extern void printk_prefer_direct_enter(void);
+extern void printk_prefer_direct_exit(void);
+
 extern bool pr_flush(int timeout_ms, bool reset_on_progress);
 
 /*
@@ -222,6 +225,14 @@ static inline void printk_deferred_exit(void)
 {
 }
 
+static inline void printk_prefer_direct_enter(void)
+{
+}
+
+static inline void printk_prefer_direct_exit(void)
+{
+}
+
 static inline bool pr_flush(int timeout_ms, bool reset_on_progress)
 {
 	return true;
kernel/hung_task.c +10 -1
index 52501e5f7655..02a65d554340 100644
@@ -127,6 +127,8 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)
 	 * complain:
 	 */
 	if (sysctl_hung_task_warnings) {
+		printk_prefer_direct_enter();
+
 		if (sysctl_hung_task_warnings > 0)
 			sysctl_hung_task_warnings--;
 		pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
@@ -142,6 +144,8 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)
 
 		if (sysctl_hung_task_all_cpu_backtrace)
 			hung_task_show_all_bt = true;
+
+		printk_prefer_direct_exit();
 	}
 
 	touch_nmi_watchdog();
@@ -204,12 +208,17 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
 	}
  unlock:
 	rcu_read_unlock();
-	if (hung_task_show_lock)
+	if (hung_task_show_lock) {
+		printk_prefer_direct_enter();
 		debug_show_all_locks();
+		printk_prefer_direct_exit();
+	}
 
 	if (hung_task_show_all_bt) {
 		hung_task_show_all_bt = false;
+		printk_prefer_direct_enter();
 		trigger_all_cpu_backtrace();
+		printk_prefer_direct_exit();
 	}
 
 	if (hung_task_call_panic)
kernel/panic.c +4 -0
index 55b50e052ec3..7d422597403f 100644
@@ -560,6 +560,8 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 {
 	disable_trace_on_warning();
 
+	printk_prefer_direct_enter();
+
 	if (file)
 		pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n",
 			raw_smp_processor_id(), current->pid, file, line,
@@ -597,6 +599,8 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 
 	/* Just a warning, don't kill lockdep. */
 	add_taint(taint, LOCKDEP_STILL_OK);
+
+	printk_prefer_direct_exit();
 }
 
 #ifndef __WARN_FLAGS
kernel/printk/printk.c +28 -0
index a06999d55278..ed7f738261cc 100644
@@ -362,6 +362,34 @@ static int console_msg_format = MSG_FORMAT_DEFAULT;
 static DEFINE_MUTEX(syslog_lock);
 
 #ifdef CONFIG_PRINTK
+static atomic_t printk_prefer_direct = ATOMIC_INIT(0);
+
+/**
+ * printk_prefer_direct_enter - cause printk() calls to attempt direct
+ *                              printing to all enabled consoles
+ *
+ * Since it is not possible to call into the console printing code from any
+ * context, there is no guarantee that direct printing will occur.
+ *
+ * This globally effects all printk() callers.
+ *
+ * Context: Any context.
+ */
+void printk_prefer_direct_enter(void)
+{
+	atomic_inc(&printk_prefer_direct);
+}
+
+/**
+ * printk_prefer_direct_exit - restore printk() behavior
+ *
+ * Context: Any context.
+ */
+void printk_prefer_direct_exit(void)
+{
+	WARN_ON(atomic_dec_if_positive(&printk_prefer_direct) < 0);
+}
+
 DECLARE_WAIT_QUEUE_HEAD(log_wait);
 /* All 3 protected by @syslog_lock. */
 /* the next printk record to read by syslog(READ) or /proc/kmsg */
kernel/rcu/tree_stall.h +2 -0
index 0c5d8516516a..d612707c2ed0 100644
@@ -619,6 +619,7 @@ static void print_cpu_stall(unsigned long gps)
 	 * See Documentation/RCU/stallwarn.rst for info on how to debug
 	 * RCU CPU stall warnings.
 	 */
+	printk_prefer_direct_enter();
 	trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected"));
 	pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name);
 	raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags);
@@ -656,6 +657,7 @@ static void print_cpu_stall(unsigned long gps)
 	 */
 	set_tsk_need_resched(current);
 	set_preempt_need_resched();
+	printk_prefer_direct_exit();
 }
 
 static void check_cpu_stall(struct rcu_data *rdp)
kernel/reboot.c +13 -1
index 6bcc5d6a6572..4177645e74d6 100644
@@ -447,9 +447,11 @@ static int __orderly_reboot(void)
 	ret = run_cmd(reboot_cmd);
 
 	if (ret) {
+		printk_prefer_direct_enter();
 		pr_warn("Failed to start orderly reboot: forcing the issue\n");
 		emergency_sync();
 		kernel_restart(NULL);
+		printk_prefer_direct_exit();
 	}
 
 	return ret;
@@ -462,6 +464,7 @@ static int __orderly_poweroff(bool force)
 	ret = run_cmd(poweroff_cmd);
 
 	if (ret && force) {
+		printk_prefer_direct_enter();
 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
 
 		/*
@@ -471,6 +474,7 @@ static int __orderly_poweroff(bool force)
 		 */
 		emergency_sync();
 		kernel_power_off();
+		printk_prefer_direct_exit();
 	}
 
 	return ret;
@@ -528,6 +532,8 @@ EXPORT_SYMBOL_GPL(orderly_reboot);
  */
 static void hw_failure_emergency_poweroff_func(struct work_struct *work)
 {
+	printk_prefer_direct_enter();
+
 	/*
 	 * We have reached here after the emergency shutdown waiting period has
 	 * expired. This means orderly_poweroff has not been able to shut off
@@ -544,6 +550,8 @@ static void hw_failure_emergency_poweroff_func(struct work_struct *work)
 	 */
 	pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n");
 	emergency_restart();
+
+	printk_prefer_direct_exit();
 }
 
 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
@@ -582,11 +590,13 @@ void hw_protection_shutdown(const char *reason, int ms_until_forced)
 {
 	static atomic_t allow_proceed = ATOMIC_INIT(1);
 
+	printk_prefer_direct_enter();
+
 	pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason);
 
 	/* Shutdown should be initiated only once. */
 	if (!atomic_dec_and_test(&allow_proceed))
-		return;
+		goto out;
 
 	/*
 	 * Queue a backup emergency shutdown in the event of
@@ -594,6 +604,8 @@ void hw_protection_shutdown(const char *reason, int ms_until_forced)
 	 */
 	hw_failure_emergency_poweroff(ms_until_forced);
 	orderly_poweroff(true);
+out:
+	printk_prefer_direct_exit();
 }
 EXPORT_SYMBOL_GPL(hw_protection_shutdown);
 
kernel/watchdog.c +4 -0
index 9166220457bc..40024e03d422 100644
@@ -424,6 +424,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
 		/* Start period for the next softlockup warning. */
 		update_report_ts();
 
+		printk_prefer_direct_enter();
+
 		pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n",
 			smp_processor_id(), duration,
 			current->comm, task_pid_nr(current));
@@ -442,6 +444,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
 		add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
 		if (softlockup_panic)
 			panic("softlockup: hung tasks");
+
+		printk_prefer_direct_exit();
 	}
 
 	return HRTIMER_RESTART;
kernel/watchdog_hld.c +4 -0
index 247bf0b1582c..701f35f0e2d4 100644
@@ -135,6 +135,8 @@ static void watchdog_overflow_callback(struct perf_event *event,
 		if (__this_cpu_read(hard_watchdog_warn) == true)
 			return;
 
+		printk_prefer_direct_enter();
+
 		pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n",
 			 this_cpu);
 		print_modules();
@@ -155,6 +157,8 @@ static void watchdog_overflow_callback(struct perf_event *event,
 		if (hardlockup_panic)
 			nmi_panic(regs, "Hard LOCKUP");
 
+		printk_prefer_direct_exit();
+
 		__this_cpu_write(hard_watchdog_warn, true);
 		return;
 	}
Signature · 1 line
2.30.2
author maintainer
#19 lore raw

PATCH v3 13/15 printk: add kthread console printers

In reply to John Ogness

Create a kthread for each console to perform console printing. During normal operation (@system_state == SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.

During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irqwork triggered by the printk caller, referred to as direct printing.

Since threaded console printers are responsible for all printing during normal operation, this also includes messages generated via deferred printk calls. If direct printing is in effect during a deferred printk call, the queued irqwork will perform the direct printing. To make it clear that this is the only time that the irqwork will perform direct printing, rename the flag PRINTK_PENDING_OUTPUT to PRINTK_PENDING_DIRECT_OUTPUT.

Threaded console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.

Note that the kthread printers do not care about direct printing. They will always try to print if new records are available. They can be blocked by direct printing, but will be woken again once direct printing is finished.

Console unregistration is a bit tricky because the associated kthread printer cannot be stopped while the console lock is held. A policy is implemented that states: whichever task clears con->thread (under the console lock) is responsible for stopping the kthread. unregister_console() will clear con->thread while the console lock is held and then stop the kthread after releasing the console lock.

For consoles that have implemented the exit() callback, the kthread is stopped before exit() is called.

---
 include/linux/console.h |   2 +
 kernel/printk/printk.c  | 303 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 283 insertions(+), 22 deletions(-)
include/linux/console.h +2 -0
index 8c1686e2c233..9a251e70c090 100644
@@ -153,6 +153,8 @@ struct console {
 	uint	ospeed;
 	u64	seq;
 	unsigned long dropped;
+	struct task_struct *thread;
+
 	void	*data;
 	struct	 console *next;
 };
kernel/printk/printk.c +281 -22

446 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer
#20 lore raw

Re: [PATCH printk v3 13/15] printk: add kthread console printers

Quoted 55 lines · John Ogness

Create a kthread for each console to perform console printing. During normal operation (@system_state == SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.

During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irqwork triggered by the printk caller, referred to as direct printing.

Since threaded console printers are responsible for all printing during normal operation, this also includes messages generated via deferred printk calls. If direct printing is in effect during a deferred printk call, the queued irqwork will perform the direct printing. To make it clear that this is the only time that the irqwork will perform direct printing, rename the flag PRINTK_PENDING_OUTPUT to PRINTK_PENDING_DIRECT_OUTPUT.

Threaded console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.

Note that the kthread printers do not care about direct printing. They will always try to print if new records are available. They can be blocked by direct printing, but will be woken again once direct printing is finished.

Console unregistration is a bit tricky because the associated kthread printer cannot be stopped while the console lock is held. A policy is implemented that states: whichever task clears con->thread (under the console lock) is responsible for stopping the kthread. unregister_console() will clear con->thread while the console lock is held and then stop the kthread after releasing the console lock.

For consoles that have implemented the exit() callback, the kthread is stopped before exit() is called.

kernel/printk/printk.c +8 -0
@@ -390,6 +397,14 @@ void printk_prefer_direct_exit(void)
 	WARN_ON(atomic_dec_if_positive(&printk_prefer_direct) < 0);
 }
 
+static inline bool allow_direct_printing(void)
+{
+	return (!printk_kthreads_available ||
+		system_state > SYSTEM_RUNNING ||
+		oops_in_progress ||
+		atomic_read(&printk_prefer_direct));
+}
+
 DECLARE_WAIT_QUEUE_HEAD(log_wait);
 /* All 3 protected by @syslog_lock. */
 /* the next printk record to read by syslog(READ) or /proc/kmsg */

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

Quoted 5 lines
kernel/printk/printk.c +1 -1
@@ -2280,10 +2295,10 @@ asmlinkage int vprintk_emit(int facility, int level,
 	printed_len = vprintk_store(facility, level, dev_info, fmt, args);
 
 	/* If called from the scheduler, we can not call up(). */
-	if (!in_sched) {
+	if (!in_sched && allow_direct_printing()) {

allow_direct_printing() is racy here. But I think that we could live with it, see below.

Quoted 7 lines
kernel/printk/printk.c +1 -1
 		/*
 		 * The caller may be holding system-critical or
-		 * timing-sensitive locks. Disable preemption during
+		 * timing-sensitive locks. Disable preemption during direct
 		 * printing of all remaining records to all consoles so that
 		 * this context can return as soon as possible. Hopefully
 		 * another printk() caller will take over the printing.

[...]

Quoted 33 lines
kernel/printk/printk.c +8 -3
@@ -3475,10 +3720,14 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)
 {
 	int pending = this_cpu_xchg(printk_pending, 0);
 
-	if (pending & PRINTK_PENDING_OUTPUT) {
+	if (pending & PRINTK_PENDING_DIRECT_OUTPUT) {
+		printk_prefer_direct_enter();
+
 		/* If trylock fails, someone else is doing the printing */
 		if (console_trylock())
 			console_unlock();
+
+		printk_prefer_direct_exit();
 	}
 
 	if (pending & PRINTK_PENDING_WAKEUP)
@@ -3503,10 +3752,11 @@ static void __wake_up_klogd(int val)
 	 * prepare_to_wait_event(), which is called after ___wait_event() adds
 	 * the waiter but before it has checked the wait condition.
 	 *
-	 * This pairs with devkmsg_read:A and syslog_print:A.
+	 * This pairs with devkmsg_read:A, syslog_print:A, and
+	 * printk_kthread_func:A.
 	 */
 	if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */
-	    (val & PRINTK_PENDING_OUTPUT)) {
+	    (val & PRINTK_PENDING_DIRECT_OUTPUT)) {
 		this_cpu_or(printk_pending, val);
 		irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
 	}
@@ -3524,7 +3774,16 @@ void defer_console_output(void)
 	 * New messages may have been added directly to the ringbuffer
 	 * using vprintk_store(), so wake any waiters as well.

kernel/printk/printk.c void defer_console_output(void) · line 3784

Quoted 10 lines
kernel/printk/printk.c +8 -1
 	 */
-	__wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT);
+	int val = PRINTK_PENDING_WAKEUP;
+
+	/*
+	 * If console deferring was called with preferred direct printing,
+	 * make the irqwork perform the direct printing.
+	 */
+	if (atomic_read(&printk_prefer_direct))
+		val |= PRINTK_PENDING_DIRECT_OUTPUT;

We actually need:

	/*
	 * Make sure that someone will handle the messages when direct
	 * printing is allowed. It happens when the kthreads are less
	 * reliable or unusable at all.
	 */
	if (allow_direct_printing())
		val |= PRINTK_PENDING_DIRECT_OUTPUT;

It is racy. But the same race is also in vprintk_emit().

False positive is fine. console_flush_all() will bail out when the direct printing gets disabled in the meantime.

False negative is worse. But we will still queue PRINTK_PENDING_WAKEUP that will try to wake up the kthreads that should still be around.

And it was always problem even with console_trylock() approach. Failure means an expectation that someone else is doing the printing. It might be either a kthread or the current console_lock owner. But it is never guaranteed because both might be sleeping.

We do our best by calling pr_flush() or console_flush_on_panic() on various places. Also PRINTK_PENDING_WAKEUP will always try to wake up the kthreads.

Anyway, we should document this somewhere. At least in the commit message.

My dream is Documentation/core-api/printk-design.rst but I do not want to force you to do it ;-)

kernel/printk/printk.c void defer_console_output(void) · line 3789

Quoted 4 lines
kernel/printk/printk.c +1 -0
+	__wake_up_klogd(val);
 }
 
 void printk_trigger_flush(void)

Otherwise, it looks good.

Best Regards,
Petr

author maintainer
#21 lore raw

Re: [PATCH printk v3 13/15] printk: add kthread console printers

Quoted 10 lines · Petr Mladek

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

Quoted 6 lines · John Ogness
kernel/printk/printk.c +1 -1
@@ -2280,10 +2295,10 @@ asmlinkage int vprintk_emit(int facility, int level,
 	printed_len = vprintk_store(facility, level, dev_info, fmt, args);
 
 	/* If called from the scheduler, we can not call up(). */
-	if (!in_sched) {
+	if (!in_sched && allow_direct_printing()) {

allow_direct_printing() is racy here. But I think that we could live with it, see below.

Well, it is not racy for its intended purpose, which is a context that does:

printk_prefer_direct_enter();
printk();
printk_prefer_direct_exit();

It is only racy for _other_ contexts that might end up direct printing. But since those other contexts don't have a preference, I see no problem with it.

Quoted 16 lines

kernel/printk/printk.c void defer_console_output(void) · line 3784

Quoted 13 lines
kernel/printk/printk.c +8 -1
@@ -3524,7 +3774,16 @@ void defer_console_output(void)
 	 * New messages may have been added directly to the ringbuffer
 	 * using vprintk_store(), so wake any waiters as well.
 	 */
-	__wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT);
+	int val = PRINTK_PENDING_WAKEUP;
+
+	/*
+	 * If console deferring was called with preferred direct printing,
+	 * make the irqwork perform the direct printing.
+	 */
+	if (atomic_read(&printk_prefer_direct))
+		val |= PRINTK_PENDING_DIRECT_OUTPUT;

We actually need:

Quoted 7 lines
	/*
	 * Make sure that someone will handle the messages when direct
	 * printing is allowed. It happens when the kthreads are less
	 * reliable or unusable at all.
	 */
	if (allow_direct_printing())
		val |= PRINTK_PENDING_DIRECT_OUTPUT;

Agreed. I will update the comments appropriately as well.

Quoted 1 line

It is racy. But the same race is also in vprintk_emit().

It is not racy for the intended purpose, so I think it is fine.

Quoted 10 lines

False positive is fine. console_flush_all() will bail out when the direct printing gets disabled in the meantime.

False negative is worse. But we will still queue PRINTK_PENDING_WAKEUP that will try to wake up the kthreads that should still be around.

And it was always problem even with console_trylock() approach. Failure means an expectation that someone else is doing the printing. It might be either a kthread or the current console_lock owner. But it is never guaranteed because both might be sleeping.

By "sleeping" I guess you mean "scheduled out". The console_lock owner or mutex/atomic_t holder will be within printing code. And if a kthread sees new records available, it will continue rather than wait.

Quoted 3 lines

We do our best by calling pr_flush() or console_flush_on_panic() on various places. Also PRINTK_PENDING_WAKEUP will always try to wake up the kthreads.

Yes.

Quoted 5 lines

Anyway, we should document this somewhere. At least in the commit message.

My dream is Documentation/core-api/printk-design.rst but I do not want to force you to do it ;-)

I would be happy to contribute to such a document.

John

maintainer
#22 lore raw

Re: [PATCH printk v3 13/15] printk: add kthread console printers

Quoted 15 lines · John Ogness
Quoted 10 lines · Petr Mladek

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

Quoted 6 lines · John Ogness
kernel/printk/printk.c +1 -1
@@ -2280,10 +2295,10 @@ asmlinkage int vprintk_emit(int facility, int level,
 	printed_len = vprintk_store(facility, level, dev_info, fmt, args);
 
 	/* If called from the scheduler, we can not call up(). */
-	if (!in_sched) {
+	if (!in_sched && allow_direct_printing()) {

allow_direct_printing() is racy here. But I think that we could live with it, see below.

Well, it is not racy for its intended purpose, which is a context that does:

Quoted 7 lines

printk_prefer_direct_enter();
printk();
printk_prefer_direct_exit();

It is only racy for _other_ contexts that might end up direct printing. But since those other contexts don't have a preference, I see no problem with it.

Make sense.

Let me think more about it. To be sure that we see all aspects:

There are also other system wide variables checked by allow_direct_printing() and can be modified asynchronously:

+ printk_kthreads_available
+ system_state
+ oops_in_progress

"oops_in_progress" and "system_state" are similar to "printk_prefer_direct". The context that modifies this variable will see the right value. The other contexts do not care that much and do not need to be strictly synchronized. Also the value means that the direct mode is preferred but it is never guaranteed and never was.

"printk_kthreads_available" is more tricky. __printk_fallback_preferred_direct() causes that printk kthreads will not be used any longer. It should make sure that the pending messages will be printed directly. And it works because it is called under console_lock and the pending messages will be printed by the console_unlock().

Everything looks fine after all. I wonder if we could somehow document it somewhere. I think about adding a comment above allow_direct_printing() definition:

/*
* printk() always wakes printk kthreads so that they could
* flush the new message to the consoles. Also it tries
* the flush the messages directly when it is allowed. *
* The direct priting is allowed in situations when the kthreads
* are not available or the system is in a problematic state. *
* See the implementation about possible races.
*/
static inline bool allow_direct_printing(void)

{
	/*
	 * The kthreads are disabled under console_lock.
	 * Any pending messages will be handled by
	 * console_unlock().
	 */
	if (!printk_kthreads_available)
		return false;

	/*
	 * Prefer direct printing when the system is in a problematic
	 * state. The context that sets this state will always see
	 * the updated value. The other contexts do not care that
	 * much. Anyway, it is just a best effort. The direct output
	 * is possible only when console_lock is not already taken.
	 */
	return (system_state > SYSTEM_RUNNING ||
		oops_in_progress ||
		atomic_read(&printk_prefer_direct));
}

Best Regards,
Petr

author maintainer
#23 lore raw

PATCH v3 14/15 printk: extend console_lock for proper kthread support

In reply to John Ogness

Currently threaded console printers synchronize against each other using console_lock(). However, different console drivers are unrelated and do not require any synchronization between each other. Removing the synchronization between the threaded console printers will allow each console to print at its own speed.

But the threaded consoles printers do still need to synchronize against console_lock() callers. Introduce a per-console mutex and a new console flag CON_THD_BLOCKED to provide this synchronization.

console_lock() is modified so that it must acquire the mutex of each console in order to set the CON_THD_BLOCKED flag. Console printing threads will acquire their mutex while printing a record. If CON_THD_BLOCKED was set, the thread will go back to sleep instead of printing.

The reason for the CON_THD_BLOCKED flag is so that console_lock() callers do not need to acquire multiple console mutexes simultaneously, which would introduce unnecessary complexity due to nested mutex locking.

The per-console mutex is also used to synchronize setting and checking the CON_ENABLED flag. A new console_lock() variant is introduced, console_lock_single_hold(), that allows acquiring @console_sem but only locking (and holding) the mutex of a single console. This allows safely enabling and disabling consoles without disturbing the other kthread printers. The mutex and @console_sem are released with console_unlock_single_release().

Console unregistering now uses console_lock_single_hold() to stop the kthread. Thus con->thread is now synchronized by the per-console mutex. This allows consoles to be unregistered without disturbing the other kthread printers.

Threaded console printers also need to synchronize against console_trylock() callers. Since console_trylock() may be called from any context, the per-console mutex cannot be used for this synchronization. (mutex_trylock() cannot be called from atomic contexts.) Introduce a global atomic counter to identify if any threaded printers are active. The threaded printers will also check the atomic counter to identify if the console has been locked by another task via console_trylock().

Note that @console_sem is still used to provide synchronization between console_lock() and console_trylock() callers.

A locking overview for console_lock(), console_trylock(), and the threaded printers is as follows (pseudo code):

console_lock()

{
        down(&console_sem);
        for_each_console(con) {
                mutex_lock(&con->lock);
                con->flags |= CON_THD_BLOCKED;
                mutex_unlock(&con->lock);
        }
        /* console_lock acquired */
}

console_trylock()

{
        if (down_trylock(&console_sem) == 0) {
                if (atomic_cmpxchg(&console_kthreads_active, 0, -1) == 0) {
                        /* console_lock acquired */
                }
        }
}

threaded_printer()

{
        mutex_lock(&con->lock);
        if (!(con->flags & CON_THD_BLOCKED)) {
		/* console_lock() callers blocked */

                if (atomic_inc_unless_negative(&console_kthreads_active)) {
                        /* console_trylock() callers blocked */

                        con->write();

                        atomic_dec(&console_lock_count);
                }
        }
        mutex_unlock(&con->lock);
}

The console owner and waiter logic now only applies between contexts that have taken the console_lock via console_trylock(). Threaded printers never take the console_lock, so they do not have a console_lock to handover. Tasks that have used console_lock() will block the threaded printers using a mutex and if the console_lock is handed over to an atomic context, it would be unable to unblock the threaded printers. However, the console_trylock() case is really the only scenario that is interesting for handovers anyway.

@panic_console_dropped must change to atomic_t since it is no longer protected exclusively by the console_lock.

Since threaded printers remain asleep if they see that the console is locked, they now must be explicitly woken in __console_unlock(). This means wake_up_klogd() calls following a console_unlock() are no longer necessary and are removed.

Also note that threaded printers no longer need to check @console_suspended. The check for the CON_THD_BLOCKED flag implicitly covers the suspended console case.

---
 include/linux/console.h |  15 ++
 kernel/printk/printk.c  | 295 +++++++++++++++++++++++++++++++---------
 2 files changed, 242 insertions(+), 68 deletions(-)
include/linux/console.h +15 -0
index 9a251e70c090..c1fd4f41c547 100644
@@ -16,6 +16,7 @@
 
 #include <linux/atomic.h>
 #include <linux/types.h>
+#include <linux/mutex.h>
 
 struct vc_data;
 struct console_font_op;
@@ -136,6 +137,7 @@ static inline int con_debug_leave(void)
 #define CON_ANYTIME	(16) /* Safe to call when cpu is offline */
 #define CON_BRL		(32) /* Used for a braille device */
 #define CON_EXTENDED	(64) /* Use the extended output format a la /dev/kmsg */
+#define CON_THD_BLOCKED	(128) /* Thread blocked because console is locked */
 
 struct console {
 	char	name[16];
@@ -155,6 +157,19 @@ struct console {
 	unsigned long dropped;
 	struct task_struct *thread;
 
+	/*
+	 * The per-console lock is used by printing kthreads to synchronize
+	 * this console with callers of console_lock(). This is necessary in
+	 * order to allow printing kthreads to run in parallel to each other,
+	 * while each safely accessing their own @flags and synchronizing
+	 * against direct printing via console_lock/console_unlock.
+	 *
+	 * Note: For synchronizing against direct printing via
+	 *       console_trylock/console_unlock, see the static global
+	 *       variable @console_kthreads_active.
+	 */
+	struct mutex lock;
+
 	void	*data;
 	struct	 console *next;
 };
kernel/printk/printk.c +227 -68

504 lines not on this page · raw message

Signature · 1 line
2.30.2
#24 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

I love your patch! Perhaps something to improve:

Hi John,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 84d7df104dbab9c3dda8f2c5b46f9a6fc256fe02]

url:    https://github.com/intel-lab-lkp/linux/commits/John-Ogness/printk-for-next/20220420-080725
base:   84d7df104dbab9c3dda8f2c5b46f9a6fc256fe02
config: openrisc-randconfig-r022-20220419 (https://download.01.org/0day-ci/archive/20220420/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/5139c5b347088e124f3812a3fa63eb284b14f2dc
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review John-Ogness/printk-for-next/20220420-080725
git checkout 5139c5b347088e124f3812a3fa63eb284b14f2dc
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross W=1 O=build_dir ARCH=openrisc SHELL=/bin/bash kernel/printk/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> kernel/printk/printk.c:2703:6: warning: no previous prototype for 'console_lock_single_hold' [-Wmissing-prototypes]

2703 | void console_lock_single_hold(struct console *con)
| ^~~~~~~~~~~~~~~~~~~~~~~~

vim +/console_lock_single_hold +2703 kernel/printk/printk.c

2697
2698 /*
2699 * Lock the console_lock, but rather than blocking all the kthread printers,
2700 * lock a specified kthread printer and hold the lock. This is useful if
2701 * console flags for a particular console need to be updated.
2702 */

> 2703	void console_lock_single_hold(struct console *con)

2704 {
2705 might_sleep();
2706 down_console_sem();
2707 mutex_lock(&con->lock);
2708 console_locked = 1;
2709 console_may_schedule = 1;
2710 }
2711

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
author maintainer
#25 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 3 lines · kernel test robot
Quoted 1 line
Quoted 1 line

kernel/printk/printk.c:2703:6: warning: no previous prototype for 'console_lock_single_hold' [-Wmissing-prototypes]

2703 | void console_lock_single_hold(struct console *con)
| ^~~~~~~~~~~~~~~~~~~~~~~~

I forgot to make it static. Thanks lkp!

John Ogness

#26 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

In reply to John Ogness
I love your patch! Perhaps something to improve:

Hi John,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 84d7df104dbab9c3dda8f2c5b46f9a6fc256fe02]

url:    https://github.com/intel-lab-lkp/linux/commits/John-Ogness/printk-for-next/20220420-080725
base:   84d7df104dbab9c3dda8f2c5b46f9a6fc256fe02
config: s390-randconfig-r032-20220419 (https://download.01.org/0day-ci/archive/20220420/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project bac6cd5bf85669e3376610cfc4c4f9ca015e7b9b)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install s390 cross compiling tool for clang build
# apt-get install binutils-s390x-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/5139c5b347088e124f3812a3fa63eb284b14f2dc
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review John-Ogness/printk-for-next/20220420-080725
git checkout 5139c5b347088e124f3812a3fa63eb284b14f2dc
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=s390 SHELL=/bin/bash kernel/printk/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

In file included from kernel/printk/printk.c:35:
In file included from include/linux/memblock.h:13:
In file included from arch/s390/include/asm/dma.h:5:
In file included from arch/s390/include/asm/io.h:75:
include/asm-generic/io.h:464:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]

           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:477:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:37:59: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) __swab16((__force __u16)(__le16)(x))
                                                             ^
   include/uapi/linux/swab.h:102:54: note: expanded from macro '__swab16'
   #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
                                                        ^
   In file included from kernel/printk/printk.c:35:
   In file included from include/linux/memblock.h:13:
   In file included from arch/s390/include/asm/dma.h:5:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:490:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/big_endian.h:35:59: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) __swab32((__force __u32)(__le32)(x))
                                                             ^
   include/uapi/linux/swab.h:115:54: note: expanded from macro '__swab32'
   #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
                                                        ^
   In file included from kernel/printk/printk.c:35:
   In file included from include/linux/memblock.h:13:
   In file included from arch/s390/include/asm/dma.h:5:
   In file included from arch/s390/include/asm/io.h:75:
   include/asm-generic/io.h:501:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:511:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:521:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:609:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsb(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:617:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsw(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:625:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           readsl(PCI_IOBASE + addr, buffer, count);
                  ~~~~~~~~~~ ^
   include/asm-generic/io.h:634:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesb(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:643:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesw(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
   include/asm-generic/io.h:652:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           writesl(PCI_IOBASE + addr, buffer, count);
                   ~~~~~~~~~~ ^
>> kernel/printk/printk.c:2703:6: warning: no previous prototype for function 'console_lock_single_hold' [-Wmissing-prototypes]
   void console_lock_single_hold(struct console *con)
        ^

kernel/printk/printk.c:2703:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void console_lock_single_hold(struct console *con)
^
static
13 warnings generated.

vim +/console_lock_single_hold +2703 kernel/printk/printk.c

2697
2698 /*
2699 * Lock the console_lock, but rather than blocking all the kthread printers,
2700 * lock a specified kthread printer and hold the lock. This is useful if
2701 * console flags for a particular console need to be updated.
2702 */

> 2703	void console_lock_single_hold(struct console *con)

2704 {
2705 might_sleep();
2706 down_console_sem();
2707 mutex_lock(&con->lock);
2708 console_locked = 1;
2709 console_may_schedule = 1;
2710 }
2711

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
maintainer
#27 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

In reply to John Ogness
Quoted 113 lines · John Ogness

Currently threaded console printers synchronize against each other using console_lock(). However, different console drivers are unrelated and do not require any synchronization between each other. Removing the synchronization between the threaded console printers will allow each console to print at its own speed.

But the threaded consoles printers do still need to synchronize against console_lock() callers. Introduce a per-console mutex and a new console flag CON_THD_BLOCKED to provide this synchronization.

console_lock() is modified so that it must acquire the mutex of each console in order to set the CON_THD_BLOCKED flag. Console printing threads will acquire their mutex while printing a record. If CON_THD_BLOCKED was set, the thread will go back to sleep instead of printing.

The reason for the CON_THD_BLOCKED flag is so that console_lock() callers do not need to acquire multiple console mutexes simultaneously, which would introduce unnecessary complexity due to nested mutex locking.

The per-console mutex is also used to synchronize setting and checking the CON_ENABLED flag. A new console_lock() variant is introduced, console_lock_single_hold(), that allows acquiring @console_sem but only locking (and holding) the mutex of a single console. This allows safely enabling and disabling consoles without disturbing the other kthread printers. The mutex and @console_sem are released with console_unlock_single_release().

Console unregistering now uses console_lock_single_hold() to stop the kthread. Thus con->thread is now synchronized by the per-console mutex. This allows consoles to be unregistered without disturbing the other kthread printers.

Threaded console printers also need to synchronize against console_trylock() callers. Since console_trylock() may be called from any context, the per-console mutex cannot be used for this synchronization. (mutex_trylock() cannot be called from atomic contexts.) Introduce a global atomic counter to identify if any threaded printers are active. The threaded printers will also check the atomic counter to identify if the console has been locked by another task via console_trylock().

Note that @console_sem is still used to provide synchronization between console_lock() and console_trylock() callers.

A locking overview for console_lock(), console_trylock(), and the threaded printers is as follows (pseudo code):

console_lock()

{
        down(&console_sem);
        for_each_console(con) {
                mutex_lock(&con->lock);
                con->flags |= CON_THD_BLOCKED;
                mutex_unlock(&con->lock);
        }
        /* console_lock acquired */
}

console_trylock()

{
        if (down_trylock(&console_sem) == 0) {
                if (atomic_cmpxchg(&console_kthreads_active, 0, -1) == 0) {
                        /* console_lock acquired */
                }
        }
}

threaded_printer()

{
        mutex_lock(&con->lock);
        if (!(con->flags & CON_THD_BLOCKED)) {
		/* console_lock() callers blocked */

                if (atomic_inc_unless_negative(&console_kthreads_active)) {
                        /* console_trylock() callers blocked */

                        con->write();

                        atomic_dec(&console_lock_count);
                }
        }
        mutex_unlock(&con->lock);
}

The console owner and waiter logic now only applies between contexts that have taken the console_lock via console_trylock(). Threaded printers never take the console_lock, so they do not have a console_lock to handover. Tasks that have used console_lock() will block the threaded printers using a mutex and if the console_lock is handed over to an atomic context, it would be unable to unblock the threaded printers. However, the console_trylock() case is really the only scenario that is interesting for handovers anyway.

@panic_console_dropped must change to atomic_t since it is no longer protected exclusively by the console_lock.

Since threaded printers remain asleep if they see that the console is locked, they now must be explicitly woken in __console_unlock(). This means wake_up_klogd() calls following a console_unlock() are no longer necessary and are removed.

Also note that threaded printers no longer need to check @console_suspended. The check for the CON_THD_BLOCKED flag implicitly covers the suspended console case.

kernel/printk/printk.c static int console_cpu_notify(unsigned int cpu) · line 2671

Quoted 9 lines
kernel/printk/printk.c +3 -3
@@ -2603,9 +2666,10 @@ static int console_cpu_notify(unsigned int cpu)
 		/* If trylock fails, someone else is doing the printing */
 		if (console_trylock())
 			console_unlock();
-
-		/* Wake kthread printers. Some may have become usable. */
-		wake_up_klogd();
+		else {
+			/* Some kthread printers may have become usable. */
+			wake_up_klogd();

Do you have any particular scenario in mind, please? Could CPU hotplug put any printk kthread into a sleep?

Quoted 16 lines
kernel/printk/printk.c +4 -0
+		}
 	}
 	return 0;
 }
@@ -2625,11 +2689,33 @@ void console_lock(void)
 	down_console_sem();
 	if (console_suspended)
 		return;
+	console_kthreads_block();
 	console_locked = 1;
 	console_may_schedule = 1;
 }
 EXPORT_SYMBOL(console_lock);
 
+/*
+ * Lock the console_lock, but rather than blocking all the kthread printers,

kernel/printk/printk.c void console_lock(void) · line 2709

Quoted 10 lines
kernel/printk/printk.c +10 -0
+ * lock a specified kthread printer and hold the lock. This is useful if
+ * console flags for a particular console need to be updated.
+ */
+void console_lock_single_hold(struct console *con)
+{
+	might_sleep();
+	down_console_sem();
+	mutex_lock(&con->lock);
+	console_locked = 1;
+	console_may_schedule = 1;

This looks wrong. It is a global flag that could be modified only when all consoles are blocked. This API blocks only the single console. The other consoles are still allowed to print actively.

Another problem will appear with the 15th patch. It will remove console_locked variable and is_console_locked() will not longer be aware that this console is locked. We will not know that it might cause deadlock in the VT code.

Quoted 15 lines
kernel/printk/printk.c +9 -0
+}
+
+static void console_unlock_single_release(struct console *con)
+{
+	console_locked = 0;
+	mutex_unlock(&con->lock);
+	up_console_sem();
+}
+
 /**
  * console_trylock - try to lock the console system for exclusive use.
  *
@@ -2728,17 +2834,18 @@ static void __console_unlock(void)
  *
  * @handover will be set to true if a printk waiter has taken over the

kernel/printk/printk.c static void __console_unlock(void) · line 2843

Quoted 10 lines
kernel/printk/printk.c +3 -2
  * console_lock, in which case the caller is no longer holding the
- * console_lock. Otherwise it is set to false.
+ * console_lock. Otherwise it is set to false. A NULL pointer may be provided
+ * to disable allowing the console_lock to be taken over by a printk waiter.
  *
  * Returns false if the given console has no next record to print, otherwise
  * true.
  *
- * Requires the console_lock.
+ * Requires the console_lock if @handover is non-NULL.

* Requires con->lock otherwise.

Quoted 12 lines
kernel/printk/printk.c +3 -3
  */
-static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
-				     char *dropped_text, bool *handover)
+static bool __console_emit_next_record(struct console *con, char *text, char *ext_text,
+				       char *dropped_text, bool *handover)
 {
-	static int panic_console_dropped;
+	static atomic_t panic_console_dropped = ATOMIC_INIT(0);
 	struct printk_info info;
 	struct printk_record r;
 	unsigned long flags;

kernel/printk/printk.c void register_console(struct console *newcon) · line 3404

Quoted 4 lines
kernel/printk/printk.c +1 -0
@@ -3261,6 +3401,8 @@ void register_console(struct console *newcon)
 
 	newcon->dropped = 0;
 	newcon->thread = NULL;
+	newcon->flags |= CON_THD_BLOCKED;

Just to show the complexity added by console_lock_single_hold():

It took me some time to realize that it is correct. The flag is needed because the console will be added under console_lock(). The flag would not be needed when it was added under console_lock_single_hold().

kernel/printk/printk.c int unregister_console(struct console *console) · line 3461

Quoted 12 lines
kernel/printk/printk.c +2 -1
+	mutex_init(&newcon->lock);
 
 	if (newcon->flags & CON_PRINTBUFFER) {
 		/* Get a consistent copy of @syslog_seq. */
@@ -3314,7 +3456,7 @@ int unregister_console(struct console *console)
 		return 0;
 
 	res = -ENODEV;
-	console_lock();
+	console_lock_single_hold(console);
 	if (console_drivers == console) {
 		console_drivers=console->next;

Another example of the complexity:

I though that this was not safe. console_drivers is a global list and console_lock_single_hold() is supposed to block only a single console. But it is actually safe because console_lock_single_hold() holds console_sem.

Another question is why console_lock_single_hold() is enough here and why console_lock() is used in register_console(). I think that console_lock_single_hold() will be enough even in register_console().

All this is far from obvious. It shows how the API is confusing and tricky. And it is another motivation to remove console_lock_single_hold().

kernel/printk/printk.c static int printk_kthread_func(void *data) · line 3838

Quoted 7 lines
kernel/printk/printk.c +1 -1
 		res = 0;
@@ -3676,14 +3835,14 @@ static int printk_kthread_func(void *data)
 	kfree(ext_text);
 	kfree(text);
 
-	console_lock();
+	mutex_lock(&con->lock);

This is serialized against unregister_console() but not with register_console() because they use different locking scheme.

kernel/printk/printk.c static int printk_kthread_func(void *data) · line 3848

Quoted 11 lines
kernel/printk/printk.c +1 -1
 	/*
 	 * If this kthread is being stopped by another task, con->thread will
 	 * already be NULL. That is fine. The important thing is that it is
 	 * NULL after the kthread exits.
 	 */
 	con->thread = NULL;
-	console_unlock();
+	mutex_unlock(&con->lock);
 
 	return 0;
 }

Resume:

I would prefer to get rid of console_lock_single_hold() and console_unlock_single_release() API.

It was definitely an interesting experiment. I agree that it would be nice to do not block the other kthreads when it is not really needed. But from my POV, it adds more harm than good at the moment.

It is possible that we will want to do such optimizations in the future. But it must be easier to understand what exactly is serialized which way. At least it should be more documented. Also the same API would need to be used on the related code paths.

Best Regards,
Petr

author maintainer
#28 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 7 lines · Petr Mladek
Quoted 7 lines
kernel/printk/printk.c +0 -1
@@ -2603,9 +2666,10 @@ static int console_cpu_notify(unsigned int cpu)
 		/* If trylock fails, someone else is doing the printing */
 		if (console_trylock())
 			console_unlock();
-
Quoted 8 lines

kernel/printk/printk.c static int console_cpu_notify(unsigned int cpu) · line 2671

Quoted 5 lines
kernel/printk/printk.c +3 -2
-		/* Wake kthread printers. Some may have become usable. */
-		wake_up_klogd();
+		else {
+			/* Some kthread printers may have become usable. */
+			wake_up_klogd();

Do you have any particular scenario in mind, please? Could CPU hotplug put any printk kthread into a sleep?

I do not have a particular scenario. My reasoning was that a CPU coming online would affect the conditions of __console_is_usable() for consoles without CON_ANYTIME. Of course, it would mean that previously a kthread went to sleep because it was trying to print from a CPU that was offline. I am doubtful that such a scenario is possible. But you did uncover some bizarre code paths where task migration could fail during CPU offlining.

Anyway, you suggested to keep the CON_ANYTIME checks for kthreads in there. So it seems correct to wake threads anytime the printer_should_wake() conditions change.

Quoted 10 lines
Quoted 10 lines
kernel/printk/printk.c +1 -0
@@ -2625,11 +2689,33 @@ void console_lock(void)
 	down_console_sem();
 	if (console_suspended)
 		return;
+	console_kthreads_block();
 	console_locked = 1;
 	console_may_schedule = 1;
 }
 EXPORT_SYMBOL(console_lock);
 
Quoted 15 lines

kernel/printk/printk.c void console_lock(void) · line 2709

Quoted 12 lines
kernel/printk/printk.c +12 -0
+/*
+ * Lock the console_lock, but rather than blocking all the kthread printers,
+ * lock a specified kthread printer and hold the lock. This is useful if
+ * console flags for a particular console need to be updated.
+ */
+void console_lock_single_hold(struct console *con)
+{
+	might_sleep();
+	down_console_sem();
+	mutex_lock(&con->lock);
+	console_locked = 1;
+	console_may_schedule = 1;

This looks wrong. It is a global flag that could be modified only when all consoles are blocked.

You are correct. is_console_locked() needs to return false in this scenario. I will leave out the @console_lock setting and insert a comment to clarify why.

Quoted 2 lines

This API blocks only the single console. The other consoles are still allowed to print actively.

That is the point. VT does not care about the other printers. VT is using @console_locked to protect itself against itself.

Quoted 4 lines

Another problem will appear with the 15th patch. It will remove console_locked variable and is_console_locked() will not longer be aware that this console is locked. We will not know that it might cause deadlock in the VT code.

From the perspective of VT code the console is _not_ locked. So is_console_locked() should return false. is_console_locked() is to make sure that the _VT code_ has called console_lock()/console_trylock(). So the 15th patch is still correct.

Quoted 7 lines
Quoted 7 lines
kernel/printk/printk.c +2 -1
@@ -2728,17 +2834,18 @@ static void __console_unlock(void)
  *
  * @handover will be set to true if a printk waiter has taken over the
  * console_lock, in which case the caller is no longer holding the
- * console_lock. Otherwise it is set to false.
+ * console_lock. Otherwise it is set to false. A NULL pointer may be provided
+ * to disable allowing the console_lock to be taken over by a printk waiter.
Quoted 8 lines

kernel/printk/printk.c static void __console_unlock(void) · line 2843

Quoted 6 lines
kernel/printk/printk.c +1 -1
  *
  * Returns false if the given console has no next record to print, otherwise
  * true.
  *
- * Requires the console_lock.
+ * Requires the console_lock if @handover is non-NULL.

* Requires con->lock otherwise.

Right. I will update the comments.

Quoted 19 lines
Quoted 12 lines
kernel/printk/printk.c +3 -3
  */
-static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
-				     char *dropped_text, bool *handover)
+static bool __console_emit_next_record(struct console *con, char *text, char *ext_text,
+				       char *dropped_text, bool *handover)
 {
-	static int panic_console_dropped;
+	static atomic_t panic_console_dropped = ATOMIC_INIT(0);
 	struct printk_info info;
 	struct printk_record r;
 	unsigned long flags;

kernel/printk/printk.c void register_console(struct console *newcon) · line 3404

Quoted 4 lines
kernel/printk/printk.c +1 -0
@@ -3261,6 +3401,8 @@ void register_console(struct console *newcon)
 
 	newcon->dropped = 0;
 	newcon->thread = NULL;
+	newcon->flags |= CON_THD_BLOCKED;

Just to show the complexity added by console_lock_single_hold():

Quoted 4 lines

It took me some time to realize that it is correct. The flag is needed because the console will be added under console_lock(). The flag would not be needed when it was added under console_lock_single_hold().

?? But it is not added under
console_lock_single_hold(). console_lock_single_hold() is not a replacement for console_lock(). Their purpose is very different. console_lock_single_hold() is an internal function to provide synchronization for @flags and @thread updates of a single console.

Maybe we are getting caught in my "bad naming" trap again. :-/

I do not have any ideas for a function that:

- locks @console_sem to prevent console registration/deregistration

- locks con->lock to provide synchronized @flags and/or @thread updates

Quoted 15 lines

kernel/printk/printk.c int unregister_console(struct console *console) · line 3461

Quoted 12 lines
kernel/printk/printk.c +2 -1
+	mutex_init(&newcon->lock);
 
 	if (newcon->flags & CON_PRINTBUFFER) {
 		/* Get a consistent copy of @syslog_seq. */
@@ -3314,7 +3456,7 @@ int unregister_console(struct console *console)
 		return 0;
 
 	res = -ENODEV;
-	console_lock();
+	console_lock_single_hold(console);
 	if (console_drivers == console) {
 		console_drivers=console->next;

Another example of the complexity:

Quoted 4 lines

I though that this was not safe. console_drivers is a global list and console_lock_single_hold() is supposed to block only a single console. But it is actually safe because console_lock_single_hold() holds console_sem.

Yes. It is safe.

Quoted 4 lines

Another question is why console_lock_single_hold() is enough here and why console_lock() is used in register_console(). I think that console_lock_single_hold() will be enough even in register_console().

?? And which console would you want to lock? @newcon? It is not registered yet.

If you want to minimize register_console() locking, it is enough just to down @console_sem.

Quoted 3 lines

All this is far from obvious. It shows how the API is confusing and tricky. And it is another motivation to remove console_lock_single_hold().

We need a method to provide @flags synchronization between the kthreads and console_stop(). Keep in mind that console_lock() does *not* hold the mutexes. So a completed console_lock() call does *not* mean that the kthreads are sleeping. They could still lock their own mutex and keep going. It is not until the kthreads see that CON_THD_BLOCKED is set that they realize they are not supposed to be running and go to sleep. But console_stop() could be performing an update to @flags while that kthread is checking it. It is a data race in code that should be synchronized.

I spent some time trying to find a good solution for this. Here are the ideas that I came up with:

1. Use READ_ONCE(short)/WRITE_ONCE(short) because probably that is enough to guarantee atomic writes/reads on all platforms.

2. Make @flags atomic_t. This guarentees consistence but would require changing how all consoles initialize that field.

3. Create a separate @enabled boolean field in struct console so that data races do not matter. This would also change how all consoles initialize their struct.

4. Provide a new function that uses the mutex to synchronize, since the kthread is already using the mutex.

I ended up choosing #4 because it had the added benefit of allowing console_start(), console_stop(), console_unregister() to avoid affecting the other kthreads.

Quoted 10 lines

kernel/printk/printk.c static int printk_kthread_func(void *data) · line 3838

Quoted 7 lines
kernel/printk/printk.c +1 -1
 		res = 0;
@@ -3676,14 +3835,14 @@ static int printk_kthread_func(void *data)
 	kfree(ext_text);
 	kfree(text);
 
-	console_lock();
+	mutex_lock(&con->lock);

This is serialized against unregister_console() but not with register_console() because they use different locking scheme.

?? In register_console() the thread has not been created yet. There is nothing to synchronize against.

Quoted 8 lines

Resume:

I would prefer to get rid of console_lock_single_hold() and console_unlock_single_release() API.

It was definitely an interesting experiment. I agree that it would be nice to do not block the other kthreads when it is not really needed. But from my POV, it adds more harm than good at the moment.

So we go with option #1 to solve(?) the @flags synchronization issue? Or is there another option I missed?

Quoted 5 lines

It is possible that we will want to do such optimizations in the future. But it must be easier to understand what exactly is serialized which way. At least it should be more documented. Also the same API would need to be used on the related code paths.

AFAICT it is used in all places that it is appropriate.

John

maintainer
#29 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 25 lines · John Ogness
Quoted 7 lines · Petr Mladek
Quoted 7 lines
kernel/printk/printk.c +0 -1
@@ -2603,9 +2666,10 @@ static int console_cpu_notify(unsigned int cpu)
 		/* If trylock fails, someone else is doing the printing */
 		if (console_trylock())
 			console_unlock();
-
Quoted 8 lines

kernel/printk/printk.c static int console_cpu_notify(unsigned int cpu) · line 2671

Quoted 5 lines
kernel/printk/printk.c +3 -2
-		/* Wake kthread printers. Some may have become usable. */
-		wake_up_klogd();
+		else {
+			/* Some kthread printers may have become usable. */
+			wake_up_klogd();

Do you have any particular scenario in mind, please? Could CPU hotplug put any printk kthread into a sleep?

I do not have a particular scenario. My reasoning was that a CPU coming online would affect the conditions of __console_is_usable() for consoles without CON_ANYTIME. Of course, it would mean that previously a kthread went to sleep because it was trying to print from a CPU that was offline. I am doubtful that such a scenario is possible. But you did uncover some bizarre code paths where task migration could fail during CPU offlining.

Quoted 3 lines

Anyway, you suggested to keep the CON_ANYTIME checks for kthreads in there. So it seems correct to wake threads anytime the printer_should_wake() conditions change.

Makes sense. Thanks for updating the comment in v4.

Quoted 41 lines
Quoted 10 lines
Quoted 10 lines
kernel/printk/printk.c +1 -0
@@ -2625,11 +2689,33 @@ void console_lock(void)
 	down_console_sem();
 	if (console_suspended)
 		return;
+	console_kthreads_block();
 	console_locked = 1;
 	console_may_schedule = 1;
 }
 EXPORT_SYMBOL(console_lock);
 
Quoted 15 lines

kernel/printk/printk.c void console_lock(void) · line 2709

Quoted 12 lines
kernel/printk/printk.c +12 -0
+/*
+ * Lock the console_lock, but rather than blocking all the kthread printers,
+ * lock a specified kthread printer and hold the lock. This is useful if
+ * console flags for a particular console need to be updated.
+ */
+void console_lock_single_hold(struct console *con)
+{
+	might_sleep();
+	down_console_sem();
+	mutex_lock(&con->lock);
+	console_locked = 1;
+	console_may_schedule = 1;

This looks wrong. It is a global flag that could be modified only when all consoles are blocked.

You are correct. is_console_locked() needs to return false in this scenario. I will leave out the @console_lock setting and insert a comment to clarify why.

Quoted 2 lines

This API blocks only the single console. The other consoles are still allowed to print actively.

That is the point. VT does not care about the other printers. VT is using @console_locked to protect itself against itself.

Quoted 4 lines

Another problem will appear with the 15th patch. It will remove console_locked variable and is_console_locked() will not longer be aware that this console is locked. We will not know that it might cause deadlock in the VT code.

Quoted 4 lines
Quoted 1 line

From the perspective of VT code the console is _not_ locked. So

is_console_locked() should return false. is_console_locked() is to make sure that the _VT code_ has called console_lock()/console_trylock(). So the 15th patch is still correct.

I see. I was not sure about the exact purpose in the VT code. Anyway, it is clear that the locking is really complicated here.

Quoted 42 lines
Quoted 7 lines
Quoted 7 lines
kernel/printk/printk.c +2 -1
@@ -2728,17 +2834,18 @@ static void __console_unlock(void)
  *
  * @handover will be set to true if a printk waiter has taken over the
  * console_lock, in which case the caller is no longer holding the
- * console_lock. Otherwise it is set to false.
+ * console_lock. Otherwise it is set to false. A NULL pointer may be provided
+ * to disable allowing the console_lock to be taken over by a printk waiter.
Quoted 8 lines

kernel/printk/printk.c static void __console_unlock(void) · line 2843

Quoted 6 lines
kernel/printk/printk.c +1 -1
  *
  * Returns false if the given console has no next record to print, otherwise
  * true.
  *
- * Requires the console_lock.
+ * Requires the console_lock if @handover is non-NULL.

* Requires con->lock otherwise.

Right. I will update the comments.

Quoted 19 lines
Quoted 12 lines
kernel/printk/printk.c +3 -3
  */
-static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
-				     char *dropped_text, bool *handover)
+static bool __console_emit_next_record(struct console *con, char *text, char *ext_text,
+				       char *dropped_text, bool *handover)
 {
-	static int panic_console_dropped;
+	static atomic_t panic_console_dropped = ATOMIC_INIT(0);
 	struct printk_info info;
 	struct printk_record r;
 	unsigned long flags;

kernel/printk/printk.c void register_console(struct console *newcon) · line 3404

Quoted 4 lines
kernel/printk/printk.c +1 -0
@@ -3261,6 +3401,8 @@ void register_console(struct console *newcon)
 
 	newcon->dropped = 0;
 	newcon->thread = NULL;
+	newcon->flags |= CON_THD_BLOCKED;

Just to show the complexity added by console_lock_single_hold():

Quoted 4 lines

It took me some time to realize that it is correct. The flag is needed because the console will be added under console_lock(). The flag would not be needed when it was added under console_lock_single_hold().

Quoted 7 lines

?? But it is not added under
console_lock_single_hold(). console_lock_single_hold() is not a replacement for console_lock(). Their purpose is very different. console_lock_single_hold() is an internal function to provide synchronization for @flags and @thread updates of a single console.

Maybe we are getting caught in my "bad naming" trap again. :-/

From my POV, the problem was that the real purpose was far from clear and it was not mentioned anywhere. If it was mentioned then it was too cryptic for me. The comments focused too much on the possibility to do some changes without disturbing other kthreads.

Quoted 9 lines

We need a method to provide @flags synchronization between the kthreads and console_stop(). Keep in mind that console_lock() does *not* hold the mutexes. So a completed console_lock() call does *not* mean that the kthreads are sleeping. They could still lock their own mutex and keep going. It is not until the kthreads see that CON_THD_BLOCKED is set that they realize they are not supposed to be running and go to sleep. But console_stop() could be performing an update to @flags while that kthread is checking it. It is a data race in code that should be synchronized.

Nice derscription of the problem.

Quoted 13 lines

I spent some time trying to find a good solution for this. Here are the ideas that I came up with:

1. Use READ_ONCE(short)/WRITE_ONCE(short) because probably that is enough to guarantee atomic writes/reads on all platforms.

2. Make @flags atomic_t. This guarentees consistence but would require changing how all consoles initialize that field.

3. Create a separate @enabled boolean field in struct console so that data races do not matter. This would also change how all consoles initialize their struct.

Quoted 6 lines

4. Provide a new function that uses the mutex to synchronize, since the kthread is already using the mutex.

I ended up choosing #4 because it had the added benefit of allowing console_start(), console_stop(), console_unregister() to avoid affecting the other kthreads.

IMHO, it is actually a generic problem of the complex locking scheme when there are too many combinations of the protected data. In the current state, the problem seems to be only with CON_ENABLED flag but there might be other hidden races in the future.

IMHO, it would be much easier when there are the following rules:

+ console_lock() blocks taking con->lock
+ con->lock blocks taking console_lock()
+ Different con->lock might be taken in parallel

The result would be:

+ global variables need to be guarded by the big console_lock()
+ con->lock should be enough to guard per-console variables
+ the big console_lock() would serialize also access to per-console variables.

Of course, it is not that simple. I am not 100% that we could even achieve this. Anyway, I think about the following wrapper:

void single_console_lock(struct console *con)

{
	for (;;) {
		error = wait_event_interruptible(log_wait,
					con->flags & CON_THB_BLOCKED);

		if (error)
			continue;

		mutex_lock(&con->lock);

		if (!con->flags & CON_THB_BLOCKED)
			break;

		mutex_unlock(&con->lock);
	}
}

void single_console_unlock(struct console *con)

{
	mutex_unlock(&con->lock);
}

We should use it everywhere instead of the simple mutex_lock(con->lock) and mutex_lock(con->lock). And we could remove mutex_lock()/unlock() from code called under the big console_lock().

How does it sound, please?

Best Regards,
Petr

author maintainer
#30 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 2 lines · Petr Mladek

IMHO, it is actually a generic problem of the complex locking scheme when there are too many combinations of the protected data.

Sure. We are in a delicate situation of continuing to support the old locking scheme while transitioning to a new one.

Quoted 9 lines

In the current state, the problem seems to be only with CON_ENABLED flag but there might be other hidden races in the future.

IMHO, it would be much easier when there are the following rules:

+ console_lock() blocks taking con->lock
+ con->lock blocks taking console_lock()
+ Different con->lock might be taken in parallel

Quoted 6 lines

The result would be:

+ global variables need to be guarded by the big console_lock()
+ con->lock should be enough to guard per-console variables
+ the big console_lock() would serialize also access to per-console variables.

It looks like you are talking about nested locking. This was my original idea but I had problems relating to kthread stopping. However, the code has changed a lot since then and now when I look at it, it does not look like it would be a problem. Getting rid of CON_THD_BLOCKED would greatly simplify the relationship between console_lock and kthreads.

For this we would need the console list to become a list_head so that it is doubly linked (in order to unlock in reverse order). That probably would be a good idea anyway. It is a bit bizarre that printk implements its own linked list.

Quoted 2 lines

Of course, it is not that simple. I am not 100% that we could even achieve this.

It just might be that simple. I will explore it again.

Quoted 20 lines

Anyway, I think about the following wrapper:

void single_console_lock(struct console *con)

{
	for (;;) {
		error = wait_event_interruptible(log_wait,
					con->flags & CON_THB_BLOCKED);

		if (error)
			continue;

		mutex_lock(&con->lock);

		if (!con->flags & CON_THB_BLOCKED)
			break;

		mutex_unlock(&con->lock);
	}
}
Quoted 8 lines

void single_console_unlock(struct console *con)

{
	mutex_unlock(&con->lock);
}

We should use it everywhere instead of the simple mutex_lock(con->lock) and mutex_lock(con->lock). And we could remove mutex_lock()/unlock() from code called under the big console_lock().

Hmmm. Waiting on @log_wait is not correct. A @log_wait wakeup with the kthread already in the blocked state is unusual. There would need to be a per-console waitqueue for when the kthread unlocks its mutex.

Maybe something like:

void single_console_lock(struct console *con)

{
	for (;;) {
		error = wait_event_interruptible(con->lock_wait,
				!(con->flags & CON_THB_BLOCKED));
		if (error)
			continue;

		mutex_lock(&con->lock);

		if (!(con->flags & CON_THB_BLOCKED))
			break;

		mutex_unlock(&con->lock);
	}
}

And in printk_kthread_func(), after the kthread unlocks its con->lock, it calls:

if (wq_has_sleeper(&con->lock_wait))

	wake_up_interruptible_all(&con->lock_wait);

But single_console_lock() would not be allowed to be called under console_lock(), so I don't see how it is useful. con->flags is always modified under @console_sem to make sure the console does not disappear.

Anyway, I will first look into the nested locking solution. That seems more promising to me and it would go a long way to simplify the locking hierarchy.

John

maintainer
#31 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 29 lines · John Ogness
Quoted 2 lines · Petr Mladek

IMHO, it is actually a generic problem of the complex locking scheme when there are too many combinations of the protected data.

Sure. We are in a delicate situation of continuing to support the old locking scheme while transitioning to a new one.

Quoted 9 lines

In the current state, the problem seems to be only with CON_ENABLED flag but there might be other hidden races in the future.

IMHO, it would be much easier when there are the following rules:

+ console_lock() blocks taking con->lock
+ con->lock blocks taking console_lock()
+ Different con->lock might be taken in parallel

Quoted 6 lines

The result would be:

+ global variables need to be guarded by the big console_lock()
+ con->lock should be enough to guard per-console variables
+ the big console_lock() would serialize also access to per-console variables.

It looks like you are talking about nested locking. This was my original idea but I had problems relating to kthread stopping. However, the code has changed a lot since then and now when I look at it, it does not look like it would be a problem. Getting rid of CON_THD_BLOCKED would greatly simplify the relationship between console_lock and kthreads.

Quoted 4 lines

For this we would need the console list to become a list_head so that it is doubly linked (in order to unlock in reverse order). That probably would be a good idea anyway. It is a bit bizarre that printk implements its own linked list.

Another problem is that the ordering is not stable. The console might come and go.

Quoted 30 lines
Quoted 2 lines

Of course, it is not that simple. I am not 100% that we could even achieve this.

It just might be that simple. I will explore it again.

Quoted 25 lines

Anyway, I think about the following wrapper:

void single_console_lock(struct console *con)

{
	for (;;) {
		error = wait_event_interruptible(log_wait,
					con->flags & CON_THB_BLOCKED);

		if (error)
			continue;

		mutex_lock(&con->lock);

		if (!con->flags & CON_THB_BLOCKED)
			break;

		mutex_unlock(&con->lock);
	}
}

void single_console_unlock(struct console *con)

{
	mutex_unlock(&con->lock);
}
Quoted 7 lines
Quoted 3 lines

We should use it everywhere instead of the simple mutex_lock(con->lock) and mutex_lock(con->lock). And we could remove mutex_lock()/unlock() from code called under the big console_lock().

Hmmm. Waiting on @log_wait is not correct. A @log_wait wakeup with the kthread already in the blocked state is unusual. There would need to be a per-console waitqueue for when the kthread unlocks its mutex.

Yeah, it was a simplification. It would be much better to add extra waitqueue for this purpose.

Quoted 19 lines

Maybe something like:

void single_console_lock(struct console *con)

{
	for (;;) {
		error = wait_event_interruptible(con->lock_wait,
				!(con->flags & CON_THB_BLOCKED));
		if (error)
			continue;

		mutex_lock(&con->lock);

		if (!(con->flags & CON_THB_BLOCKED))
			break;

		mutex_unlock(&con->lock);
	}
}
Quoted 5 lines

And in printk_kthread_func(), after the kthread unlocks its con->lock, it calls:

if (wq_has_sleeper(&con->lock_wait))

	wake_up_interruptible_all(&con->lock_wait);

You are right. It will need to be done in two situations:

+ in __console_unlock() when CON_THB_BLOCKED flag is cleared and the big console_lock is released.

+ in single_console_unlock() because there might be other single_console_lock() waiters.

Quoted 2 lines

But single_console_lock() would not be allowed to be called under console_lock(), so I don't see how it is useful.

Yes. The point is that only console_lock(), __console_unlock(), single_console_lock(), single_console_unlock() will be allowed to call mutex_lock()/mutex_unlock() directly. Any other code will need to use these wrappers to get/release the lock.

I mean that the manipulation of the mutex and CON_THB_BLOCKED flag will be hidden in these wrappers.

We might also want to replace CON_THB_BLOCKED flag with a separate variable (con->locked) to avoid problems with compiler optimizations. Otherwise, we might still need to use WRITE_ONCE()/READ_ONCE() when manipulating con->flags.

Maybe, I should prepare a POC to make it more clear and see if it could work.

Quoted 6 lines

con->flags is always
modified under @console_sem to make sure the console does not disappear.

Anyway, I will first look into the nested locking solution. That seems more promising to me and it would go a long way to simplify the locking hierarchy.

Please, do not spend too much time on this. The solution must be simple in principle. If it gets complicated than it will likely be worse than the current code.

Alternative solution would be to reduce the number of variables affected by the race. I mean:

+ replace CON_THB_BLOCKED flag with con->blocked to avoid the needed of READ_ONCE()/WRITE_ONCE().

+ check con->blocked right after taking con->lock in printk_kthread_func() so that all the other accesses are safe.

Something like:

static int printk_kthread_func(void *data)
{
[...]

	for (;;) {
		error = wait_event_interruptible(log_wait,
				printer_should_wake(con, seq)); /* LMM(printk_kthread_func:A) */

		if (kthread_should_stop() || !printk_kthreads_available)
			break;

		if (error)
			continue;

		error = mutex_lock_interruptible(&con->lock);
		if (error)
			continue;

		if (con->locked) {
			mutex_unlock(&con->lock);
			continue;
		}

		/*
		 * Everything below is safe because we know that the console
		 * is not locked by console_lock();
		 */

		if (!console_is_usable(con)) {
			mutex_unlock(&con->lock);
			continue;
		}

		if ((flags & CON_THD_BLOCKED) ||
		    !console_kthread_printing_tryenter()) {
			mutex_unlock(&con->lock);
			continue;
		}

[...]

	{

	console_lock();
	con->kthread = NULL;
	__console_unlock();

[...]
}

But it is basically open-coding of the single_console_lock() wrapper.

Best Regards,
Petr

author maintainer
#32 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 2 lines · Petr Mladek

Another problem is that the ordering is not stable. The console might come and go.

The console list is protected by @console_sem, so it wouldn't be an actual problem. The real issue is that lockdep would not like it. A new lockdep class would need to be setup for each register_console().

Quoted 7 lines
Quoted 3 lines

Anyway, I will first look into the nested locking solution. That seems more promising to me and it would go a long way to simplify the locking hierarchy.

Please, do not spend too much time on this. The solution must be simple in principle. If it gets complicated than it will likely be worse than the current code.

Sure. The goal is to simplify. The only complexity will be doing in a way that allow lockdep to understand it.

Quoted 9 lines

Alternative solution would be to reduce the number of variables affected by the race. I mean:

+ replace CON_THB_BLOCKED flag with con->blocked to avoid the needed of READ_ONCE()/WRITE_ONCE().

+ check con->blocked right after taking con->lock in printk_kthread_func() so that all the other accesses are safe.

Honestly, I would prefer this to what v4 is doing. The only reason CON_THD_BLOCKED is a flag is to save space. But we are only talking about a few bytes being saved. There aren't that many consoles.

It would be a very simple change. Literally just replacing the 3 lines that set/clear CON_THD_BLOCKED and replacing/reordering the 2 lines that check the flag. Then all the READ_ONCE/WRITE_ONCE to @flags could be removed.

John

maintainer
#33 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 7 lines · John Ogness
Quoted 2 lines · Petr Mladek

Another problem is that the ordering is not stable. The console might come and go.

The console list is protected by @console_sem, so it wouldn't be an actual problem. The real issue is that lockdep would not like it. A new lockdep class would need to be setup for each register_console().

Yeah. I did not mention it explicitely but I meant it as a problem with lockdep.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

Anyway, I will first look into the nested locking solution. That seems more promising to me and it would go a long way to simplify the locking hierarchy.

Please, do not spend too much time on this. The solution must be simple in principle. If it gets complicated than it will likely be worse than the current code.

Sure. The goal is to simplify. The only complexity will be doing in a way that allow lockdep to understand it.

I am not sure how to distinguish intentional and non-intentional ordering change.

Quoted 10 lines
Quoted 9 lines

Alternative solution would be to reduce the number of variables affected by the race. I mean:

+ replace CON_THB_BLOCKED flag with con->blocked to avoid the needed of READ_ONCE()/WRITE_ONCE().

+ check con->blocked right after taking con->lock in printk_kthread_func() so that all the other accesses are safe.

Quoted 8 lines

Honestly, I would prefer this to what v4 is doing. The only reason CON_THD_BLOCKED is a flag is to save space. But we are only talking about a few bytes being saved. There aren't that many consoles.

It would be a very simple change. Literally just replacing the 3 lines that set/clear CON_THD_BLOCKED and replacing/reordering the 2 lines that check the flag. Then all the READ_ONCE/WRITE_ONCE to @flags could be removed.

I agree that it sounds like the easiest solution for now. If you prepare v5 with this change then I push it into linux-next instead of v4.

Well, I think that we need to make con->lock safe to use in the long term. The above workaround in printk_kthread_func() is good enough for now because this is the only location where con->lock is taken without console_sem. But I am sure that we/people will want to do more console-specific operations without console_sem in the future.

IMHO, the only sane approach is to follow the proposed rules:

+ console_lock() will synchronize both global and per-console stuff.

+ con->lock will synchronize per-console stuff.

+ con->lock could not be taken alone when the big console_lock() is taken.

I currently know only about two solutions:

1. The nested locking. console_lock() will take console_sem and all con->lock's and will keep them locked.

It is rather trivial in principle. The problem is lockdep and possible ABBA deadlocks caused by unstable ordering.

2. Create the wrappers around con->lock that will check whether console_sem is taken (con->locked flag).

It will require additional per-console waitqueue. But all the magic will be hidden in the wrappers.

I personally prefer 2nd approach for the long term solution. It might look more complicated but it will not break lockdep.

Best Regards,
Petr

author maintainer
#34 lore raw

Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support

Quoted 4 lines · Petr Mladek
Quoted 4 lines

Honestly, I would prefer this to what v4 is doing. The only reason CON_THD_BLOCKED is a flag is to save space. But we are only talking about a few bytes being saved. There aren't that many consoles.

Quoted 8 lines
Quoted 4 lines

It would be a very simple change. Literally just replacing the 3 lines that set/clear CON_THD_BLOCKED and replacing/reordering the 2 lines that check the flag. Then all the READ_ONCE/WRITE_ONCE to @flags could be removed.

I agree that it sounds like the easiest solution for now. If you prepare v5 with this change then I push it into linux-next instead of v4.

I will send a v5 only for that patch. I will make it a reply to the same v4 patch.

Quoted 29 lines

Well, I think that we need to make con->lock safe to use in the long term. The above workaround in printk_kthread_func() is good enough for now because this is the only location where con->lock is taken without console_sem. But I am sure that we/people will want to do more console-specific operations without console_sem in the future.

IMHO, the only sane approach is to follow the proposed rules:

+ console_lock() will synchronize both global and per-console stuff.

+ con->lock will synchronize per-console stuff.

+ con->lock could not be taken alone when the big console_lock() is taken.

I currently know only about two solutions:

1. The nested locking. console_lock() will take console_sem and all con->lock's and will keep them locked.

It is rather trivial in principle. The problem is lockdep and possible ABBA deadlocks caused by unstable ordering.

2. Create the wrappers around con->lock that will check whether console_sem is taken (con->locked flag).

Quoted 6 lines

It will require additional per-console waitqueue. But all the magic will be hidden in the wrappers.

I personally prefer 2nd approach for the long term solution. It might look more complicated but it will not break lockdep.

The 2nd approach doesn't break lockdep because it is hiding from it. We are basically open coding our own blocking lock mechanism that avoids looking like nested locking.

If using nested locking is the best technical solution, then we should not let lockdep prevent us from using that solution.

You talk about ABBA deadlocks due to unstable ordering, but I still do not see how that is possible with the @console_sem protection. Unless you are talking about some code that is trying to lock multiple consoles at once without taking the console_lock. That would need to be forbidden by the API.

My main concern with nested locking (aside from lockdep complexities) is the console suspension. That is a bizarre state where @console_sem is released even though the console is in a type of frozen state. It is tricky/messy, especially since the kthreads need to remain silent in this state. I suppose the kthreads would also need to be unlocked in suspend_console() and the kthreads would respect @console_suspended and go back to sleep (similar to how CON_THD_BLOCKED is now).

John

author maintainer
#35 lore raw

PATCH v3 15/15 printk: remove @console_locked

In reply to John Ogness

The static global variable @console_locked is used to help debug VT code to make sure that certain code paths are running with the console_lock held. However, this information is also available with the static global variable @console_kthreads_blocked (for locking via console_lock()), and the static global variable @console_kthreads_active (for locking via console_trylock()).

Remove @console_locked and update is_console_locked() to use the alternative variables.

---
 kernel/printk/printk.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
kernel/printk/printk.c +14 -17
index 6d5f57142cd9..c9e0b0abbedd 100644
@@ -340,15 +340,7 @@ static void console_kthreads_unblock(void)
 	console_kthreads_blocked = false;
 }
 
-/*
- * This is used for debugging the mess that is the VT code by
- * keeping track if we have the console semaphore held. It's
- * definitely not the perfect debug tool (we don't know if _WE_
- * hold it and are racing, but it helps tracking those weird code
- * paths in the console code where we end up in places I want
- * locked without the console semaphore held).
- */
-static int console_locked, console_suspended;
+static int console_suspended;
 
 /*
  *	Array of consoles built from command line options (console=)
@@ -2690,7 +2682,6 @@ void console_lock(void)
 	if (console_suspended)
 		return;
 	console_kthreads_block();
-	console_locked = 1;
 	console_may_schedule = 1;
 }
 EXPORT_SYMBOL(console_lock);
@@ -2705,13 +2696,11 @@ void console_lock_single_hold(struct console *con)
 	might_sleep();
 	down_console_sem();
 	mutex_lock(&con->lock);
-	console_locked = 1;
 	console_may_schedule = 1;
 }
 
 static void console_unlock_single_release(struct console *con)
 {
-	console_locked = 0;
 	mutex_unlock(&con->lock);
 	up_console_sem();
 }
@@ -2736,15 +2725,26 @@ int console_trylock(void)
 		up_console_sem();
 		return 0;
 	}
-	console_locked = 1;
 	console_may_schedule = 0;
 	return 1;
 }
 EXPORT_SYMBOL(console_trylock);
 
+/*
+ * This is used to help to make sure that certain paths within the VT code are
+ * running with the console lock held. It is definitely not the perfect debug
+ * tool (it is not known if the VT code is the task holding the console lock),
+ * but it helps tracking those weird code paths in the console code such as
+ * when the console is suspended: where the console is not locked but no
+ * console printing may occur.
+ *
+ * Note: This returns true when the console is suspended but is not locked.
+ *       This is intentional because the VT code must consider that situation
+ *       the same as if the console was locked.
+ */
 int is_console_locked(void)
 {
-	return (console_locked || atomic_read(&console_kthreads_active));
+	return (console_kthreads_blocked || atomic_read(&console_kthreads_active));
 }
 EXPORT_SYMBOL(is_console_locked);
 
@@ -2800,8 +2800,6 @@ static inline bool console_is_usable(struct console *con)
 
 static void __console_unlock(void)
 {
-	console_locked = 0;
-
 	/*
 	 * Depending on whether console_lock() or console_trylock() was used,
 	 * appropriately allow the kthread printers to continue.
@@ -3118,7 +3116,6 @@ void console_unblank(void)
 	} else
 		console_lock();
 
-	console_locked = 1;
 	console_may_schedule = 0;
 	for_each_console(c)
 		if ((c->flags & CON_ENABLED) && c->unblank)
Signature · 1 line
2.30.2
maintainer
#36 lore raw

Re: [PATCH printk v3 15/15] printk: remove @console_locked

Quoted 13 lines · John Ogness

The static global variable @console_locked is used to help debug VT code to make sure that certain code paths are running with the console_lock held. However, this information is also available with the static global variable @console_kthreads_blocked (for locking via console_lock()), and the static global variable @console_kthreads_active (for locking via console_trylock()).

Remove @console_locked and update is_console_locked() to use the alternative variables.

kernel/printk/printk.c void console_lock_single_hold(struct console *con) · line 2708

Quoted 4 lines
kernel/printk/printk.c +0 -1
@@ -2705,13 +2696,11 @@ void console_lock_single_hold(struct console *con)
 	might_sleep();
 	down_console_sem();
 	mutex_lock(&con->lock);
-	console_locked = 1;

is_console_locked() is not able to detect that a given console is locked by console_lock_single_hold().

kernel/printk/printk.c void console_lock_single_hold(struct console *con) · line 2701

Quoted 3 lines
kernel/printk/printk.c +0 -0
 	console_may_schedule = 1;
 }
 

Best Regards,
Petr

maintainer
#37 lore raw
In reply to John Ogness
Quoted 23 lines · John Ogness

This is v3 of a series to implement a kthread for each registered console. v2 is here [0]. The kthreads locklessly retrieve the records from the printk ringbuffer and also do not cause any lock contention between each other. This allows consoles to run at full speed. For example, a netconsole is able to dump records much faster than a serial or vt console. Also, during normal operation, printk() callers are completely decoupled from console printing.

There are situations where kthread printing is not sufficient. For example, during panic situations, where the kthreads may not get a chance to schedule. In such cases, the current method of attempting to print directly within the printk() caller context is used. New functions printk_prefer_direct_enter() and printk_prefer_direct_exit() are made available to mark areas of the kernel where direct printing is preferred. (These should only be areas that do not occur during normal operation.)

This series also introduces pr_flush(): a might_sleep() function that will block until all active printing threads have caught up to the latest record at the time of the pr_flush() call. This function is useful, for example, to wait until pending records are flushed to consoles before suspending.

Quoted 5 lines

Note that this series does *not* increase the reliability of console printing. Rather it focuses on the non-interference aspect of printk() by decoupling printk() callers from printing (during normal operation). Nonetheless, the reliability aspect should not worsen due to this series.

This series looks almost ready for linux-next. The only real problems are:

+ Use allow_direct_printing() instead of atomic_read(&printk_prefer_direct) in defer_console_output()

+ "temporary" remove console_lock_single_hold()/console_lock_single_release() and use the full console_lock()/console_unlock() instead.

The rest are few cosmetic issues.

I would like to push this into linux-next ASAP so that we get some wider testing of this approach. I do not expect that we could find much more issues just by staring into the code ;-)

Now, the question is whether I should wait for v4. Or whether I should put v3 into linux-next with a follow up patch doing the two above suggested changes. They are quite trivial.

Anyway, if I pushed v3+fixup then I would replace it with v4, v5, ... once they are available. I just do not want to block testing because of cosmetic problems.

John, what is your preference, please?
Anybody has another opinion, please?

Best Regards,
Petr

author maintainer
#38 lore raw
Quoted 16 lines · Petr Mladek

This series looks almost ready for linux-next. The only real problems are:

+ Use allow_direct_printing() instead of atomic_read(&printk_prefer_direct) in defer_console_output()

+ "temporary" remove console_lock_single_hold()/console_lock_single_release() and use the full console_lock()/console_unlock() instead.

The rest are few cosmetic issues.

I would like to push this into linux-next ASAP so that we get some wider testing of this approach. I do not expect that we could find much more issues just by staring into the code ;-)

Quoted 7 lines

Now, the question is whether I should wait for v4. Or whether I should put v3 into linux-next with a follow up patch doing the two above suggested changes. They are quite trivial.

Anyway, if I pushed v3+fixup then I would replace it with v4, v5, ... once they are available. I just do not want to block testing because of cosmetic problems.

Even though the fixup may be straight-forward, it would be touching a lot of lines and could potentially introduce new problems. I prefer you wait for a v4 so that there is no mess to clean up.

I can post a v4 tomorrow (using option #1 from [0] as the synchronization alternative).

John

[0] https://lore.kernel.org/r/[email protected]