Commit 2bb2b7b5 authored by John Ogness's avatar John Ogness Committed by Petr Mladek
Browse files

printk: add functions to prefer direct printing



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.

Signed-off-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Acked-by: Paul E. McKenney <paulmck@kernel.org> # for RCU
Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20220421212250.565456-13-john.ogness@linutronix.de
parent 3b604ca8
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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();

+11 −0
Original line number Diff line number Diff line
@@ -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;
+10 −1
Original line number Diff line number Diff line
@@ -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)
+4 −0
Original line number Diff line number Diff line
@@ -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
+28 −0
Original line number Diff line number Diff line
@@ -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 */
Loading