From nobody Wed Oct 1 21:24:56 2025 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 6010C223DFF for ; Sat, 27 Sep 2025 22:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759010757; cv=none; b=P91JsZWw9qn2beOjy/DCXkCkcsMy3lebGFBqGsed83+1CrpD/ldHHUnM7M5iqvxQm8hmxpuU2oP0PeNqRTSm8ZmY3R+gcl8TEGr/iwZTx83dtd5kfuUQ9SWEIpw6hsq+iaTgl8OfDfdH9w3+aGEd4lv/sbnvmsmso4y0GMIkrWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759010757; c=relaxed/simple; bh=6Go1DY++PRJ2gacF9TNbqcngs9mSCg4eSNAIAeCDEnQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jZU2S7yf7jgwUN8k/N3p0LQJaeSd7aZ+47qW6ClgxpzoO65h7rC5m6TCNdD4P42eKLP4DD1YB4HGlju0dTy2yFxYPGUQQaFbh/blo1X5ARRd3L0vf8qCzxnEAgCY5zk3pNPK/w93Vj7O3h9pcBFnmMAkn5hu6+Pw38u+DBtykhE= 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=Li9oEjdH; arc=none smtp.client-ip=209.85.128.48 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="Li9oEjdH" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-46e48d6b95fso5704125e9.3 for ; Sat, 27 Sep 2025 15:05:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1759010754; x=1759615554; 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=DNNxigzaCtAeUeqgqnx1pzvYQdhOYm8sMlkgdnR3ZUE=; b=Li9oEjdHggT+97UP9/PuxY9KxIK/1h0N9t3PEsbLEuUAkzqOr6HUa4CHAH9Mo4zAkr gvuPZKE0xM+cJLQA9vbkl+4Nn7d4JGz9EdsyhSURpkh6IWMGFxnZSgFdJRoAWXL96wJk EXZzjMqZZnToN5LbiKyqawsBckjG77tJB+fi2OeFBfewB8epLRw7/G6cXRoYWrIBUk/6 kn8vmsdfCfxjm6Wus2QpFTLVwArOdVujopCIaJd9N8t8awX3NTQxpM+jAUTLi1rxmER9 8avGNAqj1sstyKKNTPDUn3EeHvLaT0cwI2tr9talkAxjDpb5JWHQAM+oZcOBbHGq1aTm 2oSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759010754; x=1759615554; 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=DNNxigzaCtAeUeqgqnx1pzvYQdhOYm8sMlkgdnR3ZUE=; b=fJ4mAlIdUASu2KGBUmA2NcNv+PCiZUUBx9SA4S0mgAAjupmwTp3FlezdzVIFV6/JYI nbTfTkjPlkqPQRcqYjbziI3TT1HP+J7b7s2NfZ1n0OK/QHYRdUx5U0iIBhKjXH6yjomw w+NCslyyMAzii8r2xfB5BkZbXZobPV7w9WSmk3eslxVhO+LA6lF/i2eLhTSZeMZIz/V+ DyS0qISdgeGtT8fsl+x0ZC+iZ7wLeUNKdtA00j8uPzZed/O34s+8NX/pfzMqUYvXJYrw jMcXeOACKHnFrsEis6qbRb8uPOIyv//+7Oh8U7B2iesu45l36VKiZnV6FWyOMQb0Hxuv rm8w== X-Gm-Message-State: AOJu0YwC67g2rIhFiK4Q3qpeU3EXuLxTnKFsluWRyXg9rm/lN6yZkYvo XwhfDGPI4L6C+TVlbweAi10MEfF+v5VSUXw6j5zhzIu6Os1Yes4ilsBRZOdIxu3F36NSCh/IBwG AH7Us03NZmw== X-Gm-Gg: ASbGncv4ua+dSqNpsgXeTxzKdgNvOnEdZpsgy7sepDKPdIK8jAKg2Jo6oby8LQB+2qt r9ZvIBG5vzB4ovicWcg3OpNLYM3UdaFb0kPjxjsNUdWcbS8nuYaaIM/z3NwjbAJKvZOmhWZqlTq JsCjLcXIxWkZjc4jBxp4TQhaMuK1YKaIrhDpcRlhHnEFkNTaUrG560Aqm1P1J+TPp0+3tSdU5GA rJP1vlT5YgANJQaNJbEv8AYUQTcE11NVExzWg9mcqI3lkiEvDF5ulYJ2Hv7UXLGtef/CFwQg6gC PDzn7j+mGThpc6ingJQ8u7MeUUoq1vJ/R+RQyJoeo2JhmMGrfunbeD7FTlmth1Lye9MFI82UeqL 5+hQRG7X4BL6GUSZsPajo2v+XQJ1Wbv9kYroOAKcsQPJfKaY= X-Google-Smtp-Source: AGHT+IHVfwdUQZduTyYp0srLmKz2EUN2TeLie06yX3Ae8RYG5ycu94VRa35YSBwNyngA8kmofg3blw== X-Received: by 2002:a05:600c:3153:b0:46d:27b7:e7ff with SMTP id 5b1f17b1804b1-46e32a58b6amr108455435e9.36.1759010753594; Sat, 27 Sep 2025 15:05:53 -0700 (PDT) Received: from [127.0.1.1] ([2a00:23c5:7815:1301:f27:e3a8:2334:314d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-40fb89fb264sm12463730f8f.20.2025.09.27.15.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Sep 2025 15:05:52 -0700 (PDT) From: Andrew Murray Date: Sat, 27 Sep 2025 23:05:35 +0100 Subject: [PATCH v2 1/3] printk: Introduce console_flush_one_record 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: <20250927-printk_legacy_thread_console_lock-v2-1-cff9f063071a@thegoodpenguin.co.uk> References: <20250927-printk_legacy_thread_console_lock-v2-0-cff9f063071a@thegoodpenguin.co.uk> In-Reply-To: <20250927-printk_legacy_thread_console_lock-v2-0-cff9f063071a@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=1759010750; l=6109; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=6Go1DY++PRJ2gacF9TNbqcngs9mSCg4eSNAIAeCDEnQ=; b=2oYoZx7JraZ8aiZyWar9PVp+wPe3khVT9VGchN1Ca690MX8lPCiKH3DdiSxhssogCG1Uzju+F 3ILZhUe1BxzC/RO786MKrlaNblb1EUq9wqDgcJvrTsh0kYXWS1rLRd+ X-Developer-Key: i=amurray@thegoodpenguin.co.uk; a=ed25519; pk=0SU0Q8S/uEiCdbXbXS+PvJGUCaBG1nDszD+HPU3Js0Q= 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: Petr Mladek Signed-off-by: Andrew Murray --- kernel/printk/printk.c | 159 +++++++++++++++++++++++++++++++--------------= ---- 1 file changed, 100 insertions(+), 59 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 0efbcdda9aaba9d8d877df5e4f1db002d3a596bc..060d4919de320fe21fd7aca73ba= 497e27c4ff334 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3161,6 +3161,100 @@ static inline void printk_kthreads_check_locked(voi= d) { } =20 #endif /* CONFIG_PRINTK */ =20 + +/* + * Print out one record for each console. + * + * @do_cond_resched is set by the caller. It can be true only in schedulab= le + * context. + * + * @next_seq is set to the sequence number after the last available record. + * The value is valid only when this function returns true. + * + * @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. + * + * 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. + * + * Requires the console_lock. + */ +static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, = bool *handover, + bool *any_usable) +{ + struct console_flush_type ft; + struct console *con; + bool any_progress; + int cookie; + + any_progress =3D false; + + printk_get_console_flush_type(&ft); + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + u64 printk_seq; + bool progress; + + /* + * 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)) + continue; + + if (!console_is_usable(con, flags, !do_cond_resched)) + continue; + *any_usable =3D true; + + if (flags & CON_NBCON) { + progress =3D nbcon_legacy_emit_next_record(con, handover, cookie, + !do_cond_resched); + printk_seq =3D nbcon_seq_read(con); + } else { + progress =3D console_emit_next_record(con, handover, cookie); + printk_seq =3D con->seq; + } + + /* + * If a handover has occurred, the SRCU read lock + * is already released. + */ + if (*handover) + return false; + + /* Track the next of the highest seq flushed. */ + if (printk_seq > *next_seq) + *next_seq =3D printk_seq; + + if (!progress) + continue; + any_progress =3D true; + + /* Allow panic_cpu to take over the consoles safely. */ + if (other_cpu_in_panic()) + goto abandon; + + if (do_cond_resched) + cond_resched(); + } + console_srcu_read_unlock(cookie); + + return any_progress; + +abandon: + console_srcu_read_unlock(cookie); + return false; +} + /* * Print out all remaining records to all consoles. * @@ -3186,77 +3280,24 @@ static inline void printk_kthreads_check_locked(voi= d) { } */ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *h= andover) { - struct console_flush_type ft; bool any_usable =3D false; - struct console *con; bool any_progress; - int cookie; =20 *next_seq =3D 0; *handover =3D false; =20 do { - any_progress =3D false; - - printk_get_console_flush_type(&ft); - - cookie =3D console_srcu_read_lock(); - for_each_console_srcu(con) { - short flags =3D console_srcu_read_flags(con); - u64 printk_seq; - bool progress; + any_progress =3D console_flush_one_record(do_cond_resched, next_seq, han= dover, + &any_usable); =20 - /* - * console_flush_all() 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)) - continue; - - if (!console_is_usable(con, flags, !do_cond_resched)) - continue; - any_usable =3D true; - - if (flags & CON_NBCON) { - progress =3D nbcon_legacy_emit_next_record(con, handover, cookie, - !do_cond_resched); - printk_seq =3D nbcon_seq_read(con); - } else { - progress =3D console_emit_next_record(con, handover, cookie); - printk_seq =3D con->seq; - } - - /* - * If a handover has occurred, the SRCU read lock - * is already released. - */ - if (*handover) - return false; - - /* Track the next of the highest seq flushed. */ - if (printk_seq > *next_seq) - *next_seq =3D printk_seq; - - if (!progress) - continue; - any_progress =3D true; - - /* Allow panic_cpu to take over the consoles safely. */ - if (other_cpu_in_panic()) - goto abandon; + if (*handover) + return false; =20 - if (do_cond_resched) - cond_resched(); - } - console_srcu_read_unlock(cookie); + if (other_cpu_in_panic()) + return false; } while (any_progress); =20 return any_usable; - -abandon: - console_srcu_read_unlock(cookie); - return false; } =20 static void __console_flush_and_unlock(void) --=20 2.34.1 From nobody Wed Oct 1 21:24:56 2025 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 EC63D23B605 for ; Sat, 27 Sep 2025 22:05:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759010758; cv=none; b=Nu5oDfwZ3pq4Rulwszby7V6biLyPVBfudc0I8aLMj0RCRQPXVsREkydaRb8q7rtLAXZiPCV52g5HleLrJDyqw38r3nLjpI0VQopAUNIZdEx0cvgS0UWgGRIPU9sZJ8WlSuaYn1e0FO+i8wr8dPkxmVFRCK3g5LqPZeYAcXgedRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759010758; c=relaxed/simple; bh=t+Bxs6xl3hHAyQJNynT1qHHn6ausucEiLn8yQOE7E/c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=syjSdBvXYr7YlDd9HIaqnIu5YEgx2ygX18RXHCq919jz/mmQ3Zu7FKmy2g9yejqQMgyUKIfN4rKyqtMxSKOSJnrklDOeRZFwMWS+PAmJ1mXKEbrPhQJ5+jGLC/j9zPZCd8XDtrodoKMfQAAPsl/WiXGWbaOIFouQPmcINwP2q30= 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=ACEhUV92; arc=none smtp.client-ip=209.85.221.48 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="ACEhUV92" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-3ecdf2b1751so2114462f8f.0 for ; Sat, 27 Sep 2025 15:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1759010755; x=1759615555; 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=yU5Wj1Qjx2YiF/m2kQaKUtVAL4Tj3fTykAgifo+j3OI=; b=ACEhUV92PoGYWS9MZW+urdoIUGcYtAiWQT+mhg7v8qtjfdqJZSTUmej/0XHthSSaQL /sM4oYbz14e1YRAdVML0WTUPU9Wo5Ej1cNwyUpKdHbKW98rIu3cA2tKSD6DWccz57HZ+ lN80VFKGCETw4eFSGO4WN0VGXz7y2yuk3sUsj6iNGI4YkH3xkfBzXDfbIG7D/7p1o9MQ FjLUPWdkiizVTSL+Goc+F9cTPywkXVUC8E9Fl50RbEdzH2qRJNP24Ays8kYXxPfAEeLu 3atdNZXFWOImw7zIb5xzig0V6IFZ7WBg0LNkUoiP9I824wRIKBtoEQX384ooQK384ls8 icaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759010755; x=1759615555; 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=yU5Wj1Qjx2YiF/m2kQaKUtVAL4Tj3fTykAgifo+j3OI=; b=KtSDgcPF0FUr+wQoiXVM6qS3Uq5UIZ9Suun3k7yc8qykJ2bZSrIK0LEjUjegHsemG5 kBACnifmMpSNMTsN910K5ZDgdHQ1dtf0/jtM7n2uSkCrAf403XcTPhdJdWJKY7MkPiFi p4cs8irzYuRazkAiYx0abkbpGReCM6SfHMkDKf6rGkxDmoQfxiN5R0fdt6PMYF/uU3qp w8G4JcVwS193nE0HGHRMY9vx+oVyDvMh2WVz7YUrENN/mGTE/SO5LHtznQ5qbe5Pk6er XVFZmhibxivljX8a77sN5vyTkBXo728r5QH8cyNTc80SfYuPBYo5/BaMRJvDn6bQMVwp llMQ== X-Gm-Message-State: AOJu0YzJy+4MF/EMQfKT5hME5P/OXVpTEMUXX40XO9McRxlORnKVjjUG GJ4H/0LjO8//6HQULquSxGtAqMJJcaap8eyEjoPgsilMxLithHopRtQYNYgJKR+Xp06v2WTFGXY t+y40PXZBNw== X-Gm-Gg: ASbGncsOQ/HqapWVPWkGHvbQYcx9/iCzMK92wt7APmPxwZri5MDs5yGfFi6jpZntUPp wPjG5LlxhLbbysIgO9o79cHrw5xYFEgceR0InJiTJKengC/ni04IE/J3h5BIw/UEUZw3caaikYk C9RRwxRMegfOf8zW44QcKbwqgo5CKdspfqjGpkLmpZYhq8NTjTh/rAj72dFcewJaEPSjLjpiSh8 ka+ftEqO1exyOGYQHnToixxN13O3etxUmJ0MOyJevjhRpRX9lRVpUdqzp2RqWudd7YoCjFMCn2z 1KMx3D4XkKVHqcVbR6Oc4qvtXIoroOa8CdAqqbP47uQJwAzKIhVUl5nmu9i2XmbK9xCkof6zXNT JHKC5o0rFDI+ekzNZulsWREL3hBaslBD5Wsx2 X-Google-Smtp-Source: AGHT+IHw58eMb61DlUkRD25LT2hGFnvuBmbeG5KtaLpO4n9Z9cmPxTjYvekEsASiE6F4HO0TiiG/+Q== X-Received: by 2002:a05:6000:601:b0:40e:604:8e29 with SMTP id ffacd0b85a97d-40e4a7113a4mr10980448f8f.15.1759010755030; Sat, 27 Sep 2025 15:05:55 -0700 (PDT) Received: from [127.0.1.1] ([2a00:23c5:7815:1301:f27:e3a8:2334:314d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-40fb89fb264sm12463730f8f.20.2025.09.27.15.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Sep 2025 15:05:54 -0700 (PDT) From: Andrew Murray Date: Sat, 27 Sep 2025 23:05:36 +0100 Subject: [PATCH v2 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: <20250927-printk_legacy_thread_console_lock-v2-2-cff9f063071a@thegoodpenguin.co.uk> References: <20250927-printk_legacy_thread_console_lock-v2-0-cff9f063071a@thegoodpenguin.co.uk> In-Reply-To: <20250927-printk_legacy_thread_console_lock-v2-0-cff9f063071a@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=1759010750; l=3077; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=pFb1dkVqvBhpN05SNX7LSOGgtYq0als+P5lj9Ap+u+4=; b=t1zB4bpIJ+lBohL3EsvL39EXEhzeQh/sUWplcfhOONZynAeqoA/ubMDqnLhQAo/9RZkXDrEMS H2gUS5EE5e+Cv8JVJIm7a2XbsnhP7U1PAj/W0GhLOv7vluQvzxggx0m 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. Pass this information by clearing the @any_usable parameter value which has the same effect. Reviewed-by: Andrew Murray Signed-off-by: Petr Mladek --- kernel/printk/printk.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 060d4919de320fe21fd7aca73ba497e27c4ff334..e2c1cacdb4164489c60fe38f1e2= 837eb838107d6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3175,7 +3175,8 @@ static inline void printk_kthreads_check_locked(void)= { } * 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. + * @any_usable will be set to true if there are any usable consoles + * in this context. * * 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 @@ -3193,6 +3194,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * bool any_progress; int cookie; =20 + *any_usable =3D false; any_progress =3D false; =20 printk_get_console_flush_type(&ft); @@ -3229,7 +3231,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * * is already released. */ if (*handover) - return false; + goto unusable; =20 /* Track the next of the highest seq flushed. */ if (printk_seq > *next_seq) @@ -3241,7 +3243,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * =20 /* Allow panic_cpu to take over the consoles safely. */ if (other_cpu_in_panic()) - goto abandon; + goto unusable_srcu; =20 if (do_cond_resched) cond_resched(); @@ -3250,8 +3252,10 @@ static bool console_flush_one_record(bool do_cond_re= sched, u64 *next_seq, bool * =20 return any_progress; =20 -abandon: +unusable_srcu: console_srcu_read_unlock(cookie); +unusable: + *any_usable =3D false; return false; } =20 @@ -3280,21 +3284,16 @@ 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_usable; bool any_progress; =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); + any_progress =3D console_flush_one_record(do_cond_resched, next_seq, + handover, &any_usable); =20 - if (*handover) - return false; - - if (other_cpu_in_panic()) - return false; } while (any_progress); =20 return any_usable; --=20 2.34.1 From nobody Wed Oct 1 21:24:56 2025 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 86FF723D290 for ; Sat, 27 Sep 2025 22:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759010759; cv=none; b=V9DaTloRvFSDODoZlE4xsK8qrEVXpY0NfaMTLoDUx3FS8DaiR6Bf0uwbp817JfqDjUGvWeVuP8VxoO0vhcAeF47kqK9VM6Ix+lfNYPou4wyDxCu4T4FczVK4+8eDnGa0P4JG1nxgAiTG8FeQBQDxe/9m517/Q2X1vJwrBQ1arv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759010759; c=relaxed/simple; bh=iRRIRcCO6to7StUlEIsN+miyvowwCexvmjuZd0Cvz0w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SZ9wgPoVHDZ7e5vB/ZXyjVX/lcU5vheosSeoVHxh9gfLxbm8LG3LbWzz/LvKhvCn+Psbaz6NwZupzpN+4G15yCJtNFVvccECW+u1VOgLZK8xU0pW8/lypFSQv+Pa4kNaT3uHImf93hEF1HC3ViJjeVSO8kAuWwanyQRHBTqx0f4= 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=qx+gbvql; arc=none smtp.client-ip=209.85.128.45 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="qx+gbvql" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-45dd513f4ecso20130725e9.3 for ; Sat, 27 Sep 2025 15:05:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1759010756; x=1759615556; 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=OepS14psY5rWOXHn3HlFtsint7dVhh7F+68SEkY7ueU=; b=qx+gbvqlx4RtNlrh0vnTAvC6KaHmT7D1kskVz9PbUVwTzdOsGPmFT0pYkv8Jm6CC1m 4+tbHE+uTbK3FuzkZBqtVKghXpS/C50OMZCZRzRb8uQj3xwrhO03NNe428U1Ffw5W81E UEL5MLiCkuYJsOuKyvIL0buSqsK9U+KJCjboV8P0sA2s8cnLNu6Zjd2NLKwxzgZ+Tavb ktj7JasXF/y/WsF8XW9G+FO8SpjhDZlqejrnepAgzOjtJLYqDyzhe1yh9zvsurdyocdM 7ewDPrjF4A1znyoibpSbTx54XN7py1oaOmw5KprNgrco2fK4lMRKuZvcYUYpJWWw98Q9 tEBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759010756; x=1759615556; 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=OepS14psY5rWOXHn3HlFtsint7dVhh7F+68SEkY7ueU=; b=EZHNZeLkWQzSIfqHSR1NP+5IUZ5Zpb5DLutsUuv/DewSvpyzbm3ZvgkrApG/D/Qew/ SMJW3mN46szLdhQ7bx+eX8bZ9ftR0ZnshTjZPy0mn/+me1vrNiWowq8RG6iCQyfMieh+ jc/hU/MFMUEmZ/SyQ2FyQX3e9k3VzyTc8ghxKmLP4tJBCJdQo2MLvfEKT0kLl7+b6+VE 3KKA2tg6HV/Hk2LQBKBq+YmW0valD29JXIuHAvdM9FHe6kDJFPlOlfIeMY+UwOgxPQXF NS0dkMPofUhmc4vsFocKa10wQLuAN8OZKZwOXZcrMTxcNf6fHl3Dqvx20UgcLkhBUMpG 3i3Q== X-Gm-Message-State: AOJu0Yy448QYbALmFrTE9fz6ko3gdO7RtDzh/Sq4MD93+0OHN9Lyl6Ug DSzvUVHBvfdw0OMmMp2sDro8Tz3qvNRUwpa8rsAsVdAfo6+/i3rs3D6ueCwSmhM2wvY= X-Gm-Gg: ASbGnct2lI1CuLWn0yQ7JwZwRDufXpt3wO64ndbausf+LxHCMS09Ppvuqw3nmMUwcpQ BdpbOEWb07Bfo09eHIk57gbFdAF6TrTd/QgVgxYt6/TzZN500yeFBDNmuGbm7CLSwVdvFtcOriw 927uasyEa1ixkyDJyYlNQjeKG5my7GEPPtO/h1DFphGAv9lRyMuM8JBHc4K09o6GyQFVuoVuVMi MVs7/HcQa+RYx22/YLgqtXEjyMZXAJwzG7O9nua+PybI19832ZaZ49uacxFYQHDFPAcpWFw7BFQ hbWp5w/zz+MTBqVcGIg/rvbcWRvWsgZZLesmC9+Cu6AfJ8SRPDuaXc9ssRGp1Ht5QOKUDFTQPBT iEqlnRvE3zzYOa5hCj7IVSfW2Aj5AZ7TIPgnM X-Google-Smtp-Source: AGHT+IFuCZkH4xeasbCOTD5CcnTc6ihgAt5Zdq3ygLNU9i8ZplsZptkliPo9c4TLjgw2kFqHys5MQQ== X-Received: by 2002:a05:600c:4ec6:b0:46c:7097:6363 with SMTP id 5b1f17b1804b1-46e329b441cmr101671235e9.13.1759010755894; Sat, 27 Sep 2025 15:05:55 -0700 (PDT) Received: from [127.0.1.1] ([2a00:23c5:7815:1301:f27:e3a8:2334:314d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-40fb89fb264sm12463730f8f.20.2025.09.27.15.05.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Sep 2025 15:05:55 -0700 (PDT) From: Andrew Murray Date: Sat, 27 Sep 2025 23:05:37 +0100 Subject: [PATCH v2 3/3] printk: Use console_flush_one_record for legacy printer kthread 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: <20250927-printk_legacy_thread_console_lock-v2-3-cff9f063071a@thegoodpenguin.co.uk> References: <20250927-printk_legacy_thread_console_lock-v2-0-cff9f063071a@thegoodpenguin.co.uk> In-Reply-To: <20250927-printk_legacy_thread_console_lock-v2-0-cff9f063071a@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=1759010750; l=4038; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=iRRIRcCO6to7StUlEIsN+miyvowwCexvmjuZd0Cvz0w=; b=DN9wdv7NhTa8M+k1N+NFp4j/1vsnnKnat2zU0oGYNgaJVWHU7TuH6TNFB9zxZdnMWEm9YhOnU twNfh54iPwWDU9KdnkTIAHIWEjs5y9uFC56ZHAS/zekM+2QqZz+nr4H X-Developer-Key: i=amurray@thegoodpenguin.co.uk; a=ed25519; pk=0SU0Q8S/uEiCdbXbXS+PvJGUCaBG1nDszD+HPU3Js0Q= The legacy printer kthread uses console_lock and __console_flush_and_unlock to flush records to the console. This approach results in the console_lock being held for the entire duration of a flush. 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). This contention is observed during boot, as the call to filp_open in console_on_rootfs will delay progression to userspace until any in-flight flush is completed. Let's instead use console_flush_one_record and release/reacquire the console_lock between records. On a PocketBeagle 2, with the following boot args: "console=3DttyS2,9600 initcall_debug=3D1 loglevel=3D10" Without this patch: [ 5.613166] +console_on_rootfs/filp_open [ 5.643473] mmc1: SDHCI controller on fa00000.mmc [fa00000.mmc] using AD= MA 64-bit [ 5.643823] probe of fa00000.mmc returned 0 after 258244 usecs [ 5.710520] mmc1: new UHS-I speed SDR104 SDHC card at address 5048 [ 5.721976] mmcblk1: mmc1:5048 SD32G 29.7 GiB [ 5.747258] mmcblk1: p1 p2 [ 5.753324] probe of mmc1:5048 returned 0 after 40002 usecs [ 15.595240] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to 30040000.pruss [ 15.595282] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to e010000.watchdog [ 15.595297] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to e000000.watchdog [ 15.595437] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to 30300000.crc [ 146.275961] -console_on_rootfs/filp_open ... and with: [ 5.477122] +console_on_rootfs/filp_open [ 5.595814] mmc1: SDHCI controller on fa00000.mmc [fa00000.mmc] using AD= MA 64-bit [ 5.596181] probe of fa00000.mmc returned 0 after 312757 usecs [ 5.662813] mmc1: new UHS-I speed SDR104 SDHC card at address 5048 [ 5.674367] mmcblk1: mmc1:5048 SD32G 29.7 GiB [ 5.699320] mmcblk1: p1 p2 [ 5.705494] probe of mmc1:5048 returned 0 after 39987 usecs [ 6.418682] -console_on_rootfs/filp_open ... ... [ 15.593509] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to 30040000.pruss [ 15.593551] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to e010000.watchdog [ 15.593566] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to e000000.watchdog [ 15.593704] ti_sci_pm_domains 44043000.system-controller:power-controlle= r: sync_state() pending due to 30300000.crc Where I've added a printk surrounding the call in console_on_rootfs to filp= _open. Suggested-by: Petr Mladek Signed-off-by: Andrew Murray --- kernel/printk/printk.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e2c1cacdb4164489c60fe38f1e2837eb838107d6..2c9b9383df76de956a052115372= 64fd6e06366da 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3668,17 +3668,29 @@ static bool legacy_kthread_should_wakeup(void) =20 static int legacy_kthread_func(void *unused) { - for (;;) { - wait_event_interruptible(legacy_wait, legacy_kthread_should_wakeup()); + bool any_progress; + +wait_for_event: + wait_event_interruptible(legacy_wait, + legacy_kthread_should_wakeup()); + + do { + bool any_usable; + bool handover =3D false; + u64 next_seq; =20 if (kthread_should_stop()) - break; + return 0; =20 console_lock(); - __console_flush_and_unlock(); - } + any_progress =3D console_flush_one_record(true, &next_seq, + &handover, &any_usable); + if (!handover) + __console_unlock(); =20 - return 0; + } while (any_progress); + + goto wait_for_event; } =20 static bool legacy_kthread_create(void) --=20 2.34.1