Commit 741ea7aa authored by Andrew Murray's avatar Andrew Murray Committed by Petr Mladek
Browse files

printk: Introduce console_flush_one_record



console_flush_all prints all remaining records to all usable consoles
whilst its caller holds console_lock. This can result in large waiting
times for those waiting for console_lock especially where there is a
large volume of records or where the console is slow (e.g. serial).

Let's extract the parts of this function which print a single record
into a new function named console_flush_one_record. This can later
be used for functions that will release and reacquire console_lock
between records.

This commit should not change existing functionality.

Reviewed-by: default avatarPetr Mladek <pmladek@suse.com>
Signed-off-by: default avatarAndrew Murray <amurray@thegoodpenguin.co.uk>
Reviewed-by: default avatarJohn Ogness <john.ogness@linutronix.de>
Link: https://patch.msgid.link/20251020-printk_legacy_thread_console_lock-v3-1-00f1f0ac055a@thegoodpenguin.co.uk


Signed-off-by: default avatarPetr Mladek <pmladek@suse.com>
parent 48e3694a
Loading
Loading
Loading
Loading
+99 −59
Original line number Diff line number Diff line
@@ -3134,43 +3134,39 @@ static inline void printk_kthreads_check_locked(void) { }

#endif /* CONFIG_PRINTK */


/*
 * Print out all remaining records to all consoles.
 * Print out one record for each console.
 *
 * @do_cond_resched is set by the caller. It can be true only in schedulable
 * context.
 *
 * @next_seq is set to the sequence number after the last available record.
 * The value is valid only when this function returns true. It means that all
 * usable consoles are completely flushed.
 * The value is valid only when there is at least one usable console and all
 * usable consoles were flushed.
 *
 * @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.
 *
 * Returns true when there was at least one usable console and all messages
 * were flushed to all usable consoles. A returned false informs the caller
 * that everything was not flushed (either there were no usable consoles or
 * another context has taken over printing or it is a panic situation and this
 * is not the panic CPU). Regardless the reason, the caller should assume it
 * is not useful to immediately try again.
 * @any_usable will be set to true if there are any usable consoles.
 *
 * Returns true when there was at least one usable console and a record was
 * flushed. A returned false indicates there were no records to flush for any
 * of the consoles. It may also indicate that there were no usable consoles,
 * the context has been lost or there is a panic suitation. Regardless the
 * reason, the caller should assume it is not useful to immediately try again.
 *
 * Requires the console_lock.
 */
static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *handover,
				     bool *any_usable)
{
	struct console_flush_type ft;
	bool any_usable = false;
	bool any_progress = false;
	struct console *con;
	bool any_progress;
	int cookie;

	*next_seq = 0;
	*handover = false;

	do {
		any_progress = false;

	printk_get_console_flush_type(&ft);

	cookie = console_srcu_read_lock();
@@ -3180,8 +3176,8 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
		bool progress;

		/*
			 * console_flush_all() is only responsible for nbcon
			 * consoles when the nbcon consoles cannot print via
		 * console_flush_one_record() is only responsible for
		 * nbcon consoles when the nbcon consoles cannot print via
		 * their atomic or threaded flushing.
		 */
		if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
@@ -3189,7 +3185,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove

		if (!console_is_usable(con, flags, !do_cond_resched))
			continue;
			any_usable = true;
		*any_usable = true;

		if (flags & CON_NBCON) {
			progress = nbcon_legacy_emit_next_record(con, handover, cookie,
@@ -3223,15 +3219,59 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
			cond_resched();
	}
	console_srcu_read_unlock(cookie);
	} while (any_progress);

	return any_usable;
	return any_progress;

abandon:
	console_srcu_read_unlock(cookie);
	return false;
}

/*
 * Print out all remaining records to all consoles.
 *
 * @do_cond_resched is set by the caller. It can be true only in schedulable
 * context.
 *
 * @next_seq is set to the sequence number after the last available record.
 * The value is valid only when this function returns true. It means that all
 * usable consoles are completely flushed.
 *
 * @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.
 *
 * Returns true when there was at least one usable console and all messages
 * were flushed to all usable consoles. A returned false informs the caller
 * that everything was not flushed (either there were no usable consoles or
 * another context has taken over printing or it is a panic situation and this
 * is not the panic CPU). Regardless the reason, the caller should assume it
 * is not useful to immediately try again.
 *
 * Requires the console_lock.
 */
static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
{
	bool any_usable = false;
	bool any_progress;

	*next_seq = 0;
	*handover = false;

	do {
		any_progress = console_flush_one_record(do_cond_resched, next_seq, handover,
							&any_usable);

		if (*handover)
			return false;

		if (panic_on_other_cpu())
			return false;
	} while (any_progress);

	return any_usable;
}

static void __console_flush_and_unlock(void)
{
	bool do_cond_resched;