From nobody Wed Dec 17 09:00:13 2025 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 776591A316E for ; Mon, 20 Oct 2025 15:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760974698; cv=none; b=B4MbtFN1gfeoLK5uNig5lKrAXIBcdb1jUSlEaYGve0zdu6WRuu83uDT9dMjdQg8kE5ztRbuFY+0ecYWE/9XVZdII5xF+lSVknesjWigJdmHDz1Y/7JHoexFMFyLCapCF1lSo6RofKYDzx/tm/I+P2KMsDgHjCNItCpvnkReu1n8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760974698; c=relaxed/simple; bh=aGybOktdhkgeSQs+rX+F7ywh/2yjyEU3TPcl+RFqWQg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SpRBLChtKqHHkr9NJ0FyTTOU3vlni+xOkrIzmQZzW4va2j4zTWgQGcR5ypkzLF0hcpDAyTCZZdfkj+FcZQEgF2hjSXyUvMK2NRdGASHqT52PhwCa+arwJRbPVKF7Y3p1IdVn00b0V3DWpg0yy8lyM5UlYXd8/f1LeYPVMLHzsXE= 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=vK3ATPo5; arc=none smtp.client-ip=209.85.221.54 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="vK3ATPo5" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-426fc536b5dso2429522f8f.3 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=1760974694; x=1761579494; 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=cyuJNWxnHafy+PPLjmM+EIGmoAJm4KiwAsY+kP7vYUw=; b=vK3ATPo5VB3IVnqsPhU49vScodlYrb4LSrd/M5WWhwz3zvE29M9zQDeWjOINPz5Xci CZLr3lhLxdxk/Qv/CNyAJShHq04fx1EL6PSmSDQP2bWgTcqyMxv5JQfgNLtHJIg/Yvwu H4FyuXiNYnyz6l8/AgE9pjp+Mxg79etLcDcVKDFllMKLrCVVTJMNwHPXu/U7jz75UPzV l8EK79197TT1ox1hOeknWvCQ5wkXfN1CEMSUp9DtlrpYWNwwtiFqd9jD4PnHx7VdVIhu /fQmErK2L2MJLLoliSRGKqNfXvozYe0+G1lfjWNm5Nl2KJ0pjqi0LdonEIvqbQkTgaCs 04BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760974694; x=1761579494; 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=cyuJNWxnHafy+PPLjmM+EIGmoAJm4KiwAsY+kP7vYUw=; b=e4mbfB6pjk1+/6quxOknrKmrImVA09HZdUZMozfT6DzWlrqgrMpnKhZZFMFiMcM9hf PjPMEwnzln4h4nhgDng+eYbNBVGfTnlLltlb85ckTuwFNWsE0FeBp40gtuWLoHgOlrIZ zPxKwam+pUS9fdqMDKYQOXqb5IfipXirpZpfIQI6VqcmDG2le8bHG11hw6hF+PPWeoVo iT2XYTLJrYgis1QdhQWffZRZ1F4hEfQsDY4//Td+K1DkCyuF0PQn9UsOl8iGPMNuG0DB 1M7667o5x+jPgSskzL40/zQ3YX7zNBaPWSk26BOlfp6ahSwMdOCMxS4Oz1GwQpd1c1Qx Gedw== X-Gm-Message-State: AOJu0YzHE0iZI0AHO10V8R4IcCHfFXTtNvSDMe6Df5+KV6827/k2nmby CR4jh5qKSCg5/7z10/kz7h1t23Wbxs6r+ZbiMfDxyZfDj5mWFLseYG+HEhF7vxBrj2urAXHl1i2 tSsML762Oez9i X-Gm-Gg: ASbGncvul0fbVrTnYMRtcMbgSTBg6v+TRP1CXUQ9b5FXJAbqBIq2aDjV1tvWDOU2G2I oGhML5eGdYaresr2rXq45W1uLMiyqIj/6iYqFSUTWfEKLv2/3YzB5p0mlikyxsngnas3d4LmCBz yrnoFchWgEIcb7Rsyjg2Nv0t7cI9KWFwsUUjKnusX/Lq9ToDa2z6Z8xf3FkQ5t9pDkNb+Zlg2w0 VAZguH+GuHG6Rgz/BXmBPmKZjzXv3fXxVNsL0+q54mCHBOFya2paFMTVc5tViRcRNMYWrtGnbIR TsZZBw4D0rSXEetDtjWEN1ZklENWkNiyrEpkdP/KiACyJrLc9MB9+TCQSG67UpkjdQorhl0vNey 4yYOwxKV4ZIfypI/CGW0w0G0ZcQkjo/3TxW81wMPabiYnaUASxTW+FB3e5OVEvbeT7ENhTwrime MnJosaiy+cO12tNfp1pDxzbOwb X-Google-Smtp-Source: AGHT+IGkV4ZKtP7KkxgQHB3S9drQol6zdtqvnkbisQIINbPnlfhd87f1zuu14TLOrLcDBclV2LIrCA== X-Received: by 2002:a05:6000:4010:b0:425:8502:f8c3 with SMTP id ffacd0b85a97d-42704d7e9e0mr9233918f8f.1.1760974694303; 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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Oct 2025 08:38:13 -0700 (PDT) From: Andrew Murray Date: Mon, 20 Oct 2025 16:38:05 +0100 Subject: [PATCH v3 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: <20251020-printk_legacy_thread_console_lock-v3-1-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=6139; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=aGybOktdhkgeSQs+rX+F7ywh/2yjyEU3TPcl+RFqWQg=; b=6EMW4r3t5Yx8fh4/AX1u3HHYz+8W9zfUKZ7J5NsH2z6Yf7QzNRSoc9bOumYnAwSkJp5Y04O2X CFnM0e8BgjFBAPUICqnJw1bcE7dkTS23HFj9OzL8zQJilsA7ZWA1xWz 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 Reviewed-by: John Ogness --- kernel/printk/printk.c | 158 +++++++++++++++++++++++++++++++--------------= ---- 1 file changed, 99 insertions(+), 59 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 5aee9ffb16b9a5e7bfadb0266a77bfa569e50e51..1c048c66d09919967e57326e173= 2bd17c10f3c76 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3134,6 +3134,99 @@ static inline void printk_kthreads_check_locked(void= ) { } =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 there is at least one usable console and a= ll + * usable consoles were 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. + * + * @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; + bool any_progress =3D false; + struct console *con; + int cookie; + + 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 (panic_on_other_cpu()) + 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. * @@ -3159,77 +3252,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; + any_progress =3D console_flush_one_record(do_cond_resched, next_seq, han= dover, + &any_usable); =20 - 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; + if (*handover) + return false; =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 (panic_on_other_cpu()) - goto abandon; - - if (do_cond_resched) - cond_resched(); - } - console_srcu_read_unlock(cookie); + if (panic_on_other_cpu()) + 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 Dec 17 09:00:13 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 IETY0uRdc12PWoY4en5A7LxGc1tq6vdW3BiGatjKbnW2QYiy+tDZ9xlOCIgi8n/Qi5Kn 1Hf/h7flHnM3kmIRUdUo+wAXAfovMdYJEDwnLC4aINLGgZRyVbHcqmLc4BABQRMdpU66 20n/5CS44i9nHpi8e3BFMppLpA1Fg6dKWwzZau9/ovblK6ncweB79K3SwA1O8Hv+mwdk 6SwlGAlxQ8kHXdNvuUmAKHBlr63q0LSTizZsnyoge426oNAFzebaHkWNM7kZ++Fpy5kT Y0Bw== X-Gm-Message-State: AOJu0YzCt/9w+a8zbrXBcoLZQcuS+BLz3lwyN6/sz8Z8McrvW8nS4XaC Nz64rTJ8ELqBUUb+Rbj8VxdrXUa81XJtFipS5QpnHxeXR5pbDwyRtAfCT93gZG+GK2RX3MviO80 uqze6YKEsbisJ X-Gm-Gg: ASbGncuTfOrZ6g6c76MVu5imO7z7HVd/BGFJV9h3D9YKrYb4SIPLrg6qJYqFN2btvJT O0TwL7UzfgHyyxOZKMuQrBAw0hqgB210jaTVsUIuWaw6iZyZMu81yKKuWoW5gkqgB/ffGQ6t/OX 3FrFYnlssKD+y2HG7dweR2PCGs9C16K6CnZu3w1JMh+u3LVoQvkaDznqlfv5jQoQRfXffol/GJK rpg0xMqMVfla2Jne5iaqC0MBg9QPnXcqna4ixIWCJN9NYjkvn92ZGPPPKOUQZYVKcaTdbAg9ubD 7UKo2MLnmPigTvZeZQ+zR24FFgGzaJRvCl9UJabIvloN/QrMfQv5QBVw/qqK8OiUoFkDRHn+aqj seORso+3NNkUF8WxpsInQ4cUKiXwSISbRs/ECOZ5zQPmXFk7NBcY8rCMBg9p8xS4UNpKGtOyAl5 MeZ3riG+YdzTr0cFZAOPorkdx1GcDthjZQMVE= X-Google-Smtp-Source: AGHT+IEseEwR9zmogG0q83EyxVyrhwIyDQNXmGRLK6zRRXD9i/0l4M2Pgeqd9EobHiaWVHycYQt9Hw== X-Received: by 2002:a05:600c:3b03:b0:471:14af:c715 with SMTP id 5b1f17b1804b1-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 From nobody Wed Dec 17 09:00:13 2025 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 C99A626C3AE for ; Mon, 20 Oct 2025 15:38:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760974699; cv=none; b=nkI88KNkr3HEya8WxcvHiV41Z4lTDBC8x4d7+UN/cU3zWcHxhT6/EJLDH/9r118CHGL3I5YFZS7JJBOZ6WwBD0ND+eXY5xmJQ5rhh5hz0qg5onMY8vf1Xpkd109+8hLw5ezRBCb2ehsVl2ZkVd8a4MJympmzoiwJlQgAVhvs0kU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760974699; c=relaxed/simple; bh=BOim9W/NsB/OSI7iw8/JuwQcBd0s6PyDRJRBxyl0CJQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aR92JZEX1hpPxkvb+a9qRwh/4CAcgS+RI1sXNANM/K4rRy9N+9uMPK4pERfnQgIzDVswSHGbyDjexUUtcI/tPy19kHT5TwMkjo6VPtocTaHfwujPSp1UKKGbKS9klKWrKrJPGLhtezmzAoGZzmcaNbPrO1rUo57RL2EOdLROzeU= 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=1wfIh5dD; arc=none smtp.client-ip=209.85.128.54 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="1wfIh5dD" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-47117f92e32so28388165e9.1 for ; Mon, 20 Oct 2025 08:38:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thegoodpenguin-co-uk.20230601.gappssmtp.com; s=20230601; t=1760974696; x=1761579496; 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=l1WpPsLi2xewBAoEF0YUxT2xy9Y1Dmcopa55uXgVTvg=; b=1wfIh5dD8QQv5JW19honM7z3UHWbZWLRjXCS1Qo33CwGCxiuULX5PxjVnlxNjYeqHn kLGwmijJggK9FNedN2+oMfE4LoMMlG3RfyCdmzPPWquKdpjInD8Dn8HUeaKdlPhPUq3V mcQ71oJ2mNV5yztbaH4Aatfx8Rgn2Ax2EiaXr4koLKqgBfQoQzhdS9GL2mhUKSIJ1/fz pj6/2Iywt8bvd+lfQdhL7AZv2WsicwfY8gTI1RKhVLiA3Jo/iJH5snsQJHrjj7SWqS0O 7Jx1uK+L8Jb/CpEC6pZ2WY1ydxpU7MQ+cUHdefsthCAjlEUGKhQ4iGhcHb6RJpH5JjS+ SrbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760974696; x=1761579496; 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=l1WpPsLi2xewBAoEF0YUxT2xy9Y1Dmcopa55uXgVTvg=; b=f57I35wp63r1Pn1XzLNybxYa/WOoXZIhIflvAMqLM60JBnY8ICv35dekq9MTNPVzut 0QU/3cL1h6yQLitjE6o44WUfiSChKmPySUuJvfSkYJq8z6/SrfQpmCViV1IqvwlGpi9D /9y7OipHsJs2/2BznR0/cPinURdhPZiGUeJ8wwkArIoA2/wsrL1JGDZUyQxCbwGj1pLB a1pEfUe3vtlGu4D47UCW+A9Y4dFSXwx+4D9En43oGcf3fx1XixXiLjV2Q2YUOQnljmvF x7TwmKS+NfXkLz5qCGCJhExAE11iib/d3ShSTpyBhLRZX/Lzn56ZqeE4ClTLbLj/Hkk8 xSJA== X-Gm-Message-State: AOJu0YyLWR4cGFgTg8hoo9PmEAuMMKaR2CMK+EtnKx89zlUKDYcS4xsr GMDqY+moMuNMF14CCdtNAzVtnAlJ2coyuuHa39aLQuOqNIWMWwPPhrydSaezpKzS9pq6P4vwSTA DYn4PRm9jc/IG X-Gm-Gg: ASbGncvLn6MpCh0IETUvBFDNeHN/HMptNUmv4VQCL2b/vyzgIVgspRjJeld7YyyfrpL 96RvTVSgzYQhgrpYGlvOoRuSMjM/Hr+ytIkYte5Yoh81Ep9jjDXTj+AgTzgyW3e57SCTBrb1wxl DCgRopSDL0DtlEwPIiHJ8SC+O45PLC7OGUNA6yFH5C+k6RedeJNRjwaPD5YDRBvg8icOxORvFDh JyO7j7mB15R09sVuwQGVrvV2iJMMRcPyMa6nBQA+1thsksyjukBXM4/IV4rlb0GCtfGkdA2vSrc CDIa/qYbyJtSapvDkhJ+ntnU3v6ZWb8KCphv3ddGRxPiGZ1gGdbiOdi7uVki2DLiXAzTNCn5dWQ ycPdDe1RgvPpZda2/BXyXprYtRrjGCzQIdAUwWlyiKzvGz21favs5SFf6m78xiFL6DpAJRnJnSA AkhbfGBvEKKzu9HA== X-Google-Smtp-Source: AGHT+IGdf9VV2wGEvh9arjsxEQ2H3l1LgoR2fW9Wez0KU4UhOZkl4cqnX5kXbHcJxMdREo16G8z+mA== X-Received: by 2002:a05:600c:828d:b0:471:11a3:a289 with SMTP id 5b1f17b1804b1-4711791c5f5mr92703615e9.27.1760974695705; Mon, 20 Oct 2025 08:38:15 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Oct 2025 08:38:15 -0700 (PDT) From: Andrew Murray Date: Mon, 20 Oct 2025 16:38:07 +0100 Subject: [PATCH v3 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: <20251020-printk_legacy_thread_console_lock-v3-3-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=3981; i=amurray@thegoodpenguin.co.uk; s=20250914; h=from:subject:message-id; bh=BOim9W/NsB/OSI7iw8/JuwQcBd0s6PyDRJRBxyl0CJQ=; b=PHdBALW8Ed7QxtF1Gs4JmXk44Hg+amGSJpEzlUJgM6Jr7zdViAOLR/BIAT5L7/Tw9lVtpVsrd CSXkrRGzr3kCBLr9tQrEv0GrvcZeUlyBwvYRpplfvW4JSGwGCfiSz2b 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 Reviewed-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6c846d2d37d9d20bad58c6e3a7caada3be9552ca..2665a7a59f03b3a357b3346de17= 35606e77c3496 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3644,17 +3644,26 @@ 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 try_again; + +wait_for_event: + wait_event_interruptible(legacy_wait, legacy_kthread_should_wakeup()); + + do { + u64 next_seq =3D 0; + bool handover =3D false; =20 if (kthread_should_stop()) - break; + return 0; =20 console_lock(); - __console_flush_and_unlock(); - } + console_flush_one_record(true, &next_seq, &handover, &try_again); + if (!handover) + __console_unlock(); =20 - return 0; + } while (try_again); + + goto wait_for_event; } =20 static bool legacy_kthread_create(void) --=20 2.34.1