Skip to content

Commit ba00f7c

Browse files
committed
printk: console_flush_one_record() code cleanup
console_flush_one_record() and console_flush_all() duplicate several checks. They both want to tell the caller that consoles are not longer usable in this context because it has lost the lock or the lock has to be reserved for the panic CPU. Remove the duplication by changing the semantic of the function console_flush_one_record() return value and parameters. The function will return true when it is able to do the job. It means that there is at least one usable console. And the flushing was not interrupted by a takeover or panic_on_other_cpu(). Also replace the @any_usable parameter with @try_again. The @try_again parameter will be set to true when the function could do the job and at least one console made a progress. Motivation: The callers need to know when + they should continue flushing => @try_again + when the console is flushed => can_do_the_job(return) && !@try_again + when @next_seq is valid => same as flushed + when lost console_lock => @TakeOver The proposed change makes it clear when the function can do the job. It simplifies the answer for the other questions. Also the return value from console_flush_one_record() can be used as return value from console_flush_all(). Reviewed-by: John Ogness <john.ogness@linutronix.de> Link: https://patch.msgid.link/20251020-printk_legacy_thread_console_lock-v3-2-00f1f0ac055a@thegoodpenguin.co.uk [pmladek@suse.com: Fixed type of any_usable variable reported by John] Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent 741ea7a commit ba00f7c

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

kernel/printk/printk.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3142,31 +3142,33 @@ static inline void printk_kthreads_check_locked(void) { }
31423142
* context.
31433143
*
31443144
* @next_seq is set to the sequence number after the last available record.
3145-
* The value is valid only when there is at least one usable console and all
3146-
* usable consoles were flushed.
3145+
* The value is valid only when all usable consoles were flushed. It is
3146+
* when the function returns true (can do the job) and @try_again parameter
3147+
* is set to false, see below.
31473148
*
31483149
* @handover will be set to true if a printk waiter has taken over the
31493150
* console_lock, in which case the caller is no longer holding the
31503151
* console_lock. Otherwise it is set to false.
31513152
*
3152-
* @any_usable will be set to true if there are any usable consoles.
3153+
* @try_again will be set to true when it still makes sense to call this
3154+
* function again. The function could do the job, see the return value.
3155+
* And some consoles still make progress.
31533156
*
3154-
* Returns true when there was at least one usable console and a record was
3155-
* flushed. A returned false indicates there were no records to flush for any
3156-
* of the consoles. It may also indicate that there were no usable consoles,
3157-
* the context has been lost or there is a panic suitation. Regardless the
3158-
* reason, the caller should assume it is not useful to immediately try again.
3157+
* Returns true when the function could do the job. Some consoles are usable,
3158+
* and there was no takeover and no panic_on_other_cpu().
31593159
*
31603160
* Requires the console_lock.
31613161
*/
31623162
static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *handover,
3163-
bool *any_usable)
3163+
bool *try_again)
31643164
{
31653165
struct console_flush_type ft;
3166-
bool any_progress = false;
3166+
bool any_usable = false;
31673167
struct console *con;
31683168
int cookie;
31693169

3170+
*try_again = false;
3171+
31703172
printk_get_console_flush_type(&ft);
31713173

31723174
cookie = console_srcu_read_lock();
@@ -3185,7 +3187,7 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
31853187

31863188
if (!console_is_usable(con, flags, !do_cond_resched))
31873189
continue;
3188-
*any_usable = true;
3190+
any_usable = true;
31893191

31903192
if (flags & CON_NBCON) {
31913193
progress = nbcon_legacy_emit_next_record(con, handover, cookie,
@@ -3201,29 +3203,36 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
32013203
* is already released.
32023204
*/
32033205
if (*handover)
3204-
return false;
3206+
goto fail;
32053207

32063208
/* Track the next of the highest seq flushed. */
32073209
if (printk_seq > *next_seq)
32083210
*next_seq = printk_seq;
32093211

32103212
if (!progress)
32113213
continue;
3212-
any_progress = true;
3214+
3215+
/*
3216+
* An usable console made a progress. There might still be
3217+
* pending messages.
3218+
*/
3219+
*try_again = true;
32133220

32143221
/* Allow panic_cpu to take over the consoles safely. */
32153222
if (panic_on_other_cpu())
3216-
goto abandon;
3223+
goto fail_srcu;
32173224

32183225
if (do_cond_resched)
32193226
cond_resched();
32203227
}
32213228
console_srcu_read_unlock(cookie);
32223229

3223-
return any_progress;
3230+
return any_usable;
32243231

3225-
abandon:
3232+
fail_srcu:
32263233
console_srcu_read_unlock(cookie);
3234+
fail:
3235+
*try_again = false;
32273236
return false;
32283237
}
32293238

@@ -3252,24 +3261,18 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
32523261
*/
32533262
static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
32543263
{
3255-
bool any_usable = false;
3256-
bool any_progress;
3264+
bool try_again;
3265+
bool ret;
32573266

32583267
*next_seq = 0;
32593268
*handover = false;
32603269

32613270
do {
3262-
any_progress = console_flush_one_record(do_cond_resched, next_seq, handover,
3263-
&any_usable);
3271+
ret = console_flush_one_record(do_cond_resched, next_seq,
3272+
handover, &try_again);
3273+
} while (try_again);
32643274

3265-
if (*handover)
3266-
return false;
3267-
3268-
if (panic_on_other_cpu())
3269-
return false;
3270-
} while (any_progress);
3271-
3272-
return any_usable;
3275+
return ret;
32733276
}
32743277

32753278
static void __console_flush_and_unlock(void)

0 commit comments

Comments
 (0)