From nobody Thu Oct 2 15:20:02 2025 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 6E4A730B53C for ; Mon, 15 Sep 2025 12:43:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757940204; cv=none; b=jymQ8/Tvz99mL8LQIgV5Il8kXUXiKaY11NJqhBTiRi8oooa3oR3kxC79awJ2rzmrnlCRkt9+gpMQTDwZdTVaSWspW5r7ioSb+ueolm0QTQ/2UiDhWmUCDoEPKK60ykoTovx2Y7lRRXtfnc0aIj1KXvkcDcpyDBbuAK3AxEeDRsA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757940204; c=relaxed/simple; bh=HXi0OiBoVt/MYl+ob4qJzSdv2ZXZgwXc6LkbTDn+0cA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IIqaW8llFJHHSrYusi4mq3cRo1mlon+GT9iefX/7wZmlbG1Y4IpEiLCuhxt0CXCkqkbjlcGGzew74dhQHO0FS1Z8LiOZokrE54mJR3x5rlhe/MwCTyFMIHZl/We8mz756cDI8fjel1JH0c9rRV3py8T3KpBv1Bo10vpAERzjbtI= 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=DgMVzKBt; arc=none smtp.client-ip=209.85.128.50 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="DgMVzKBt" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-45f2c5ef00fso10390295e9.1 for ; Mon, 15 Sep 2025 05:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1757940200; x=1758545000; 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=yslU6u5MFq9rz8jM5cyc8g+IFjlebdZiLBTaCg0Yg3o=; b=DgMVzKBtuHtgc7uyXlnqodJWABhP4C9AoTOy+gzLFJ470F0Uk3Y8yulGkRUSCu1OFW yYAX+XMHcgJ1SH6fBrfocCrA2P5eClZ6319jZhPOnrYvpNrSw7RoFkb4K1OrgyUka+Bq Xi/CYCJ03I75f3cAVVnaA1chquyt7wwRFw97MzY9nxSR8dubzmNmQ7eQ8gh/PuqC96u2 TdOLkEuSMuASH2ekRu2O2kobMjeP1kJXBIt1cZvjTByVXobZDyU1dEVm0pwjWZ/k4r6m TUe7agwpoPsElHatGAkO/3a6UnViDfafAryyVtWtcCP44mncW0vkTvaeuvRKDkkvpIeB DIuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757940200; x=1758545000; 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=yslU6u5MFq9rz8jM5cyc8g+IFjlebdZiLBTaCg0Yg3o=; b=a4XPfWmxbZ+mmI+VOFdElEXxKbjRPT+KXjB3zrEHM+01LQQyZix4neglnJHsarnbJf v1bt7Be+/HFh5vffsHVYn30zcga5SdoNKiyLkcw2FJTkf0j9zuMYTq42g7EL2k2p3UET QgSDZdDZa5kCzguge4fEKDKhUc5XhUq1ojn4Lfc1IQ51hpfOqsJ5elXQQuaskg01WMU8 ibTQ8Hc92Lvqz6Qvkce55R7NvB1bppZeq2V5QPsEJcz2F1zQ298XamttqYBrLtphX6eo SDHOKuDbHQzN2HXtIOwu8aJHP8m1a3QQobaoaq75XPcNFFzbwMyIKF4anGaZ/cI1OkuY bTtw== X-Gm-Message-State: AOJu0YxKeP/qYPx94FevgI+TtlCC2+QPqUitNPiWoLPzUbKOQRgoO30n XlFKxFrzYW2SoFN3JtYk5NLt1W0+bGq1rg2pEamO08b68ypSAB1F2crTGhZFMnSI+cJ44YAawNi 0D+MPA4c= X-Gm-Gg: ASbGnctfhT7VQl2+F3pZ4jOEpkto4Ww3yvYa+vaay2ISEEi8s1j+Kw/jO1eStMlATS5 QAru44rgNWb0HkIZAtMvU5+8TGB4K7tkzSqCxlDmZRT319vDWl2Fi6B/IRLY5qp/oC4r9lbHky2 5uI8T9EAG8GzKlGiKimpLOK0RYbZaixERCmCeC56dwe8WwjiXJwUVKQAXqS/tumB1h4/Qr5Bgx0 OKy6/tI+J20o6D0+FdHm4k/ueaZDedK84X9riAQsKdgGO07MjPD3qojsovwBCxmXl8XaYGZGPdX CvD9Qw21YJSY8GSnd4Zo3d9IKzJBD7TE6dHbVr8F4Bv3FnSfkPaiurqnNjptqzaXgpZaREc9GT0 HWy7Is+HsSTtMcf5Q7EbXzbkDUppYp3nXMhJV5gdAU2BWNws= X-Google-Smtp-Source: AGHT+IGpuI8k+Z7d/BNGCbObra24p5wLy8OlThGhhq7wwrxT5T6D2UkT2IplbxSaN1Hi0Zk1rgFk3Q== X-Received: by 2002:a05:600c:1546:b0:45b:8a0e:cda9 with SMTP id 5b1f17b1804b1-45f211ca9b0mr106143835e9.2.1757940200480; Mon, 15 Sep 2025 05:43:20 -0700 (PDT) Received: from [127.0.1.1] ([2a00:23c5:7815:1301:f27:e3a8:2334:314d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45e017b4222sm178839265e9.20.2025.09.15.05.43.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Sep 2025 05:43:20 -0700 (PDT) From: Andrew Murray Date: Mon, 15 Sep 2025 13:43:05 +0100 Subject: [PATCH RFC 1/2] 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: <20250915-printk_legacy_thread_console_lock-v1-1-f34d42a9bcb3@thegoodpenguin.co.uk> References: <20250915-printk_legacy_thread_console_lock-v1-0-f34d42a9bcb3@thegoodpenguin.co.uk> In-Reply-To: <20250915-printk_legacy_thread_console_lock-v1-0-f34d42a9bcb3@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=1757940198; l=6108; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=HXi0OiBoVt/MYl+ob4qJzSdv2ZXZgwXc6LkbTDn+0cA=; b=0zZ3JNNWzHw6nt6VC3an/qDkCPZt6o0oAwA2ngEYvjCuBluaU+R1GIQNMpYPGN6dqojtZQbS+ GJ5Hj7SlL7AC3fLm/ZE/WXgskxiQrmwU2AIVCfhoSwuvGylk38R+eDw 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. Signed-off-by: Andrew Murray Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 162 +++++++++++++++++++++++++++++++--------------= ---- 1 file changed, 102 insertions(+), 60 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 0efbcdda9aaba9d8d877df5e4f1db002d3a596bc..75a3c47e9c0e645a3198c5f56e4= 7df2a8d1871e6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3161,6 +3161,101 @@ 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_usable =3D false; + 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 +3281,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_usable; 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 Thu Oct 2 15:20:02 2025 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 8244D30C606 for ; Mon, 15 Sep 2025 12:43:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757940205; cv=none; b=c/fFmJ+Bea4OPZl/9mgbLEVAippTVETJttwKIMVi5P7ee9RTWdmScBZ6HdOn1VCNtvCbvEhy+3nGHEbGJ3a+6EYN9739iX+VlDRmNuc91oVMepVxiG0NeuGpThJ+ZlYhykjaRBScAx30tj7ETQlzfSqKAiakm3HaSLaAb3A4YCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757940205; c=relaxed/simple; bh=r6OmmV8VA0qAhD2HZu4eWWUgM5ydSUsLWJ0FF7fhTdo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CtYmOr/gSL1fjtvPI964qv5tqxgtaJ+bLPEb4hO2XQwKPIW2CMWzlxsxa9Kmhmu4y+cqUgSVkyFUKDjPbQ1K4kSKBNgLaM4RQA3TmWF3q2kPhVnl4r9Pz3/YdrVEbvONXCZFDYTqVT2bqwzFvbNCZ17Hmawu+nRfQoRG3lyflvk= 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=TWXolt6E; arc=none smtp.client-ip=209.85.128.51 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="TWXolt6E" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-45dfb8e986aso43921275e9.0 for ; Mon, 15 Sep 2025 05:43:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1757940201; x=1758545001; 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=OGl37ljG3JjOsbZvqjafte0MjItw5JP7BJXGP39K+uk=; b=TWXolt6EB6sOjsN2UL/trRJdOxT+UrlTX42LVpD67LIB33xHAs8wsbzzVbAkiA48k1 VCWPYabkSzN0H4qOIyLF419TaX4dIT5Ur9jd3PKK0oQy05epNPDA2NwPD8/aaq76Ow4e Kh+KsCU3wr1gB1bkq/IDhCJ2tZ0SfqB+GQ2ixqLWDs5vRm9miBqR3w2R8SQJy/a4OaCE jSlRpVchL5fvo3NCfVb0scr+m+ontHXNxnnx82ubGbmyDdO7y4+NRF8+RD74YMRaJK/5 0S83PE0Mfj/xDhY5Cfw708x2ePr3DoK3vTifHSzSWDiM37mgMub3Mli/mGNsjl23f4CD KsOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757940201; x=1758545001; 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=OGl37ljG3JjOsbZvqjafte0MjItw5JP7BJXGP39K+uk=; b=Xds7RR4/8r0BUjS5juYKymYjzlEMO2nqtsCiUMtgyTQKZkVrmbR609XsZvkK+PihUw 9eS7MZjN2VddJNLP+igndNX/uqggP5RmGzm+ugnYIlIAmnxnRu7QARnucvfr68vVy1uJ J/KgKQBfeE2izTruBaFQZteYpT/t5j70sxQZmfq8HwqA8Z+TS3hEWJn0/HAHaEhhKEYb vi0a9PDn0AjPFHsZbi6ubTa5elBbMVnxo1Zw1fPKBDH221AMS4V0XjL8LnszXJuY+oxr d4j9kKVKo++TIvJvtqm7pmUFMY1RaI5F91+aqjUWEYMtJEXxUMVo7GFRroM/K+s9rEP2 VVEQ== X-Gm-Message-State: AOJu0YxnMzI+9dGJ4QqVO2E7atW/pz9SnPDYsJ5piHFh7/SU6hfJrEzM 2hdaR77qYv5J47Yjq+Z7GIhaxz0XNatj3DEuHyphgdwTZmS6+WXFphyTW6Adrd/VwUNX1JixxET kY0NtJo8= X-Gm-Gg: ASbGnctGJLWZp9N7W9cuczj2hsmVj42tdmQV+uYupPHYW9N+MD1x1kCpTHVhsHA9gy4 0IJm9nlcyd5kyNrlGJ9f0CW0uT+DSpybLYTt6U6qSzSOky4qibbkIhqKLegHJhRmc+ufklK/kGf 0GEuHNfspAJ9efsbrfoWBWmR+2yUuyKsMAh+Du7ytFKToolQAniHLcUqckSnHTo5bKsMV/oECTC m436V7xCqCwC229q2bnfbViVVQNa//MbUKjnDM1E6JBIbTXxY4eieeDj/9cPRVt/UGHg0Czdy3y YNkgxJpVougTpVr0AImijaGNTn/ZQVdu14gVQJs5wuudvLf2JGHrBOon6ZMKwo134q3ur7v4U06 lnWZoVWuXElPnkYGhti5zZ3rU6AKcdWmyAoTvNCtw/+cJw2M= X-Google-Smtp-Source: AGHT+IE7+231bNN2PazsCh3VpLR9h1+kfV0XUqdeqv+XnlFeqNxXS6bZAAM3xauIt/wlyb4FrlgNoA== X-Received: by 2002:a05:600c:1f8c:b0:45d:e285:c4c6 with SMTP id 5b1f17b1804b1-45f21205125mr114203805e9.37.1757940201304; Mon, 15 Sep 2025 05:43:21 -0700 (PDT) Received: from [127.0.1.1] ([2a00:23c5:7815:1301:f27:e3a8:2334:314d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45e017b4222sm178839265e9.20.2025.09.15.05.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Sep 2025 05:43:20 -0700 (PDT) From: Andrew Murray Date: Mon, 15 Sep 2025 13:43:06 +0100 Subject: [PATCH RFC 2/2] 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: <20250915-printk_legacy_thread_console_lock-v1-2-f34d42a9bcb3@thegoodpenguin.co.uk> References: <20250915-printk_legacy_thread_console_lock-v1-0-f34d42a9bcb3@thegoodpenguin.co.uk> In-Reply-To: <20250915-printk_legacy_thread_console_lock-v1-0-f34d42a9bcb3@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=1757940198; l=3321; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=r6OmmV8VA0qAhD2HZu4eWWUgM5ydSUsLWJ0FF7fhTdo=; b=5H3EMLmOp53I3myc0TMPerJPHDa933z6CUfN6+AeJ+0Og8aV5HA+LNqUXA94O6SZn1i0uMBmI 0hmo55MlNteA9pAgHVg8cpZ4b1ZLWtgMwz6f5Mgcj57ZIrkcoCoCCV8 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_unlocked which releases and reacquires console_lock between records. Signed-off-by: Andrew Murray --- kernel/printk/printk.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 75a3c47e9c0e645a3198c5f56e47df2a8d1871e6..53daab5cdee537c2ff55702104e= 495005352db1b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3301,6 +3301,46 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove return any_usable; } =20 +/* + * Print out all remaining records to all consoles. + * + * @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. + */ +static bool console_flush_all_unlocked(u64 *next_seq, bool *handover) +{ + bool any_usable; + bool any_progress; + + *next_seq =3D 0; + *handover =3D false; + + do { + console_lock(); + any_progress =3D console_flush_one_record(true, next_seq, handover, &any= _usable); + + if (*handover) + return false; + + __console_unlock(); + + } while (any_progress); + + return any_usable; +} + static void __console_flush_and_unlock(void) { bool do_cond_resched; @@ -3346,6 +3386,17 @@ static void __console_flush_and_unlock(void) } while (prb_read_valid(prb, next_seq, NULL) && console_trylock()); } =20 +static void __console_flush_unlocked(void) +{ + bool handover; + bool flushed; + u64 next_seq; + + do { + flushed =3D console_flush_all_unlocked(&next_seq, &handover); + } while (flushed && !handover && prb_read_valid(prb, next_seq, NULL)); +} + /** * console_unlock - unblock the legacy console subsystem from printing * @@ -3676,8 +3727,7 @@ static int legacy_kthread_func(void *unused) if (kthread_should_stop()) break; =20 - console_lock(); - __console_flush_and_unlock(); + __console_flush_unlocked(); } =20 return 0; --=20 2.34.1