From nobody Wed Oct 1 22:19:18 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