From nobody Thu Dec 18 05:21:26 2025 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3741E266576 for ; Mon, 20 Oct 2025 15:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760974699; cv=none; b=NDhplcICGgDuxKXENAC1va+ZiVZz/V299BdLiGF7SuRM3xzX01kYebdTwvtrDfx0Fa9s8d0wQ1A4PQu5guity6pawWAcxHV286dH1SkiGVu2YPOhyYf3FVd2OeiNUjPr3/el1ddYnZEQ15HxhBZDAkqeC5/glAlgSkWP1KmroMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760974699; c=relaxed/simple; bh=uGtC4t047/rFjSTLtwDg469HRCpOfVdjh9yBOW3i+JY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YoBa8t2AZ0u9dHpeljo3MHtFvizkhJhQBgBNm+aHs91TLFMMVsezBxoo8vSyX1xLlRS5YfS03iOkVsrRgMVsb3Aehu9KxwaiEtnDr+dA5Do+gDCDorzeEpkqpSxti0AzbS65HIRHtlNt+9L8zi+Sv4BwueSeEijCPWvw2JlD8kE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thegoodpenguin.co.uk; spf=pass smtp.mailfrom=thegoodpenguin.co.uk; dkim=pass (2048-bit key) header.d=thegoodpenguin-co-uk.20230601.gappssmtp.com header.i=@thegoodpenguin-co-uk.20230601.gappssmtp.com header.b=ZRBKGBK0; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thegoodpenguin.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=thegoodpenguin.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=thegoodpenguin-co-uk.20230601.gappssmtp.com header.i=@thegoodpenguin-co-uk.20230601.gappssmtp.com header.b="ZRBKGBK0" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-46b303f7469so35596505e9.1 for ; Mon, 20 Oct 2025 08:38:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1760974695; x=1761579495; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=sEbCrTVr0olcFFDGZoZ0e0aGakadOIG3mUwviakGfVg=; b=ZRBKGBK0EO+7957E2EF1F7UIPdRgIj/vGxwZ9PNzMTE1PVa3z3V9fOU53bj9ivSmtn vzsQFW/vC2y14Kvj9DhwYfu0mcHUPzM2jW3M/N08ohEggCEI5GxTJQA1uW8tAn1PE8mJ s+Chvn8MCBUt2r3B3B1zcFxFDGyoPpi2VadJuHq+kAU8ryk9AFkQqRHHqeu+F58FbC7D stEoilb0qIqDLuXV3x4qQ4KVLJhNuITRH0aDQdIOw8DEx4Qy0P40M9x+oQE0FP2wGcM7 syYbCRGOEtGLZtM37jGk1bZfkAt9SFVQq3sxraybB6QpvNQbflPmyWucSfXhU01UP8bz 1hSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760974695; x=1761579495; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sEbCrTVr0olcFFDGZoZ0e0aGakadOIG3mUwviakGfVg=; b=QpBizE23eX2WQTPoiCc/h2c4VvfPRktkG0zFedPQxubnnPNO7nRNvTffN2XZ+Dr99o IETY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b1-47117874978mr98326265e9.3.1760974694999; Mon, 20 Oct 2025 08:38:14 -0700 (PDT) Received: from [127.0.1.1] ([2a00:23c5:7815:1301:f27:e3a8:2334:314d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4710e8037aasm120631405e9.2.2025.10.20.08.38.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Oct 2025 08:38:14 -0700 (PDT) From: Andrew Murray Date: Mon, 20 Oct 2025 16:38:06 +0100 Subject: [PATCH v3 2/3] printk: console_flush_one_record() code cleanup Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251020-printk_legacy_thread_console_lock-v3-2-00f1f0ac055a@thegoodpenguin.co.uk> References: <20251020-printk_legacy_thread_console_lock-v3-0-00f1f0ac055a@thegoodpenguin.co.uk> In-Reply-To: <20251020-printk_legacy_thread_console_lock-v3-0-00f1f0ac055a@thegoodpenguin.co.uk> To: Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky Cc: linux-kernel@vger.kernel.org, Andrew Murray X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1760974692; l=5716; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=/GOC6iFiS3tZ2dCMgEbMJDcl4Mfq8uze6DfWdcXyQ6M=; b=k0C67lyt9xBYRmBULFh5sYAEQQazyRVf8xLF1fk/C47cfvBJvU4LcrKZvXr/b9kd85xsk4Y+9 jeYGGQfFqIyDS5BH8RC1t3AE1pvGY4vqCaC5HOjsNFb/Tyz3h4UI5/9 X-Developer-Key: i=amurray@thegoodpenguin.co.uk; a=ed25519; pk=0SU0Q8S/uEiCdbXbXS+PvJGUCaBG1nDszD+HPU3Js0Q= From: Petr Mladek 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 =3D> @try_again + when the console is flushed =3D> can_do_the_job(return) && !@try_again + when @next_seq is valid =3D> same as flushed + when lost console_lock =3D> @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 Signed-off-by: Petr Mladek Reviewed-by: Andrew Murray --- kernel/printk/printk.c | 59 ++++++++++++++++++++++++++--------------------= ---- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1c048c66d09919967e57326e1732bd17c10f3c76..6c846d2d37d9d20bad58c6e3a7c= aada3be9552ca 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3142,31 +3142,33 @@ static inline void printk_kthreads_check_locked(voi= d) { } * context. * * @next_seq is set to the sequence number after the last available record. - * The value is valid only when there is at least one usable console and a= ll - * usable consoles were flushed. + * The value is valid only when all usable consoles were flushed. It is + * when the function returns true (can do the job) and @try_again parameter + * is set to false, see below. * * @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. * - * @any_usable will be set to true if there are any usable consoles. + * @try_again will be set to true when it still makes sense to call this + * function again. The function could do the job, see the return value. + * And some consoles still make progress. * - * 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 console= s, - * 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 ag= ain. + * Returns true when the function could do the job. Some consoles are usab= le, + * and there was no takeover and no panic_on_other_cpu(). * * Requires the console_lock. */ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, = bool *handover, - bool *any_usable) + bool *try_again) { struct console_flush_type ft; - bool any_progress =3D false; + int any_usable =3D false; struct console *con; int cookie; =20 + *try_again =3D false; + printk_get_console_flush_type(&ft); =20 cookie =3D console_srcu_read_lock(); @@ -3185,7 +3187,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * =20 if (!console_is_usable(con, flags, !do_cond_resched)) continue; - *any_usable =3D true; + any_usable =3D true; =20 if (flags & CON_NBCON) { progress =3D nbcon_legacy_emit_next_record(con, handover, cookie, @@ -3201,7 +3203,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * * is already released. */ if (*handover) - return false; + goto fail; =20 /* Track the next of the highest seq flushed. */ if (printk_seq > *next_seq) @@ -3209,21 +3211,28 @@ static bool console_flush_one_record(bool do_cond_r= esched, u64 *next_seq, bool * =20 if (!progress) continue; - any_progress =3D true; + + /* + * An usable console made a progress. There might still be + * pending messages. + */ + *try_again =3D true; =20 /* Allow panic_cpu to take over the consoles safely. */ if (panic_on_other_cpu()) - goto abandon; + goto fail_srcu; =20 if (do_cond_resched) cond_resched(); } console_srcu_read_unlock(cookie); =20 - return any_progress; + return any_usable; =20 -abandon: +fail_srcu: console_srcu_read_unlock(cookie); +fail: + *try_again =3D false; return false; } =20 @@ -3252,24 +3261,18 @@ static bool console_flush_one_record(bool do_cond_r= esched, u64 *next_seq, bool * */ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *h= andover) { - bool any_usable =3D false; - bool any_progress; + bool try_again; + bool ret; =20 *next_seq =3D 0; *handover =3D false; =20 do { - any_progress =3D console_flush_one_record(do_cond_resched, next_seq, han= dover, - &any_usable); + ret =3D console_flush_one_record(do_cond_resched, next_seq, + handover, &try_again); + } while (try_again); =20 - if (*handover) - return false; - - if (panic_on_other_cpu()) - return false; - } while (any_progress); - - return any_usable; + return ret; } =20 static void __console_flush_and_unlock(void) --=20 2.34.1