Commit 7c2aff38 authored by Stephen Brennan's avatar Stephen Brennan Committed by Ye Weihua
Browse files

printk: Drop console_sem during panic

mainline inclusion
from mainline-v5.18-rc1
commit 8ebc476f
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I89RYC
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8ebc476fd51e6c0fd3174ec1959a20ba99d4c5e5



-------------------------------------------------

If another CPU is in panic, we are about to be halted. Try to gracefully
abandon the console_sem, leaving it free for the panic CPU to grab.

Suggested-by: default avatarPetr Mladek <pmladek@suse.com>
Signed-off-by: default avatarStephen Brennan <stephen.s.brennan@oracle.com>
Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Reviewed-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20220202171821.179394-5-stephen.s.brennan@oracle.com



Conflict:
	kernel/printk/printk.c

Signed-off-by: default avatarYe Weihua <yeweihua4@huawei.com>
parent 8068817f
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -2444,6 +2444,25 @@ static int have_callable_console(void)
	return 0;
}

/*
 * Return true when this CPU should unlock console_sem without pushing all
 * messages to the console. This reduces the chance that the console is
 * locked when the panic CPU tries to use it.
 */
static bool abandon_console_lock_in_panic(void)
{
	if (!panic_in_progress())
		return false;

	/*
	 * We can use raw_smp_processor_id() here because it is impossible for
	 * the task to be migrated to the panic_cpu, or away from it. If
	 * panic_cpu has already been set, and we're not currently executing on
	 * that CPU, then we never will be.
	 */
	return atomic_read(&panic_cpu) != raw_smp_processor_id();
}

/*
 * Can we actually use the console at this time on this cpu?
 *
@@ -2590,6 +2609,10 @@ void console_unlock(void)

		printk_safe_exit_irqrestore(flags);

		/* Allow panic_cpu to take over the consoles safely */
		if (abandon_console_lock_in_panic())
			break;

		if (do_cond_resched)
			cond_resched();
	}
@@ -2611,7 +2634,7 @@ void console_unlock(void)
	raw_spin_unlock(&logbuf_lock);
	printk_safe_exit_irqrestore(flags);

	if (retry && console_trylock())
	if (retry && !abandon_console_lock_in_panic() && console_trylock())
		goto again;
}
EXPORT_SYMBOL(console_unlock);