From nobody Mon Nov 25 09:34:08 2024 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 D5C8C1DEFC7 for ; Mon, 28 Oct 2024 16:45:34 +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=1730133937; cv=none; b=Ju7zeOy1ImllElt6E4lLJDFTGzO/HY8/Xia0wt6QRZ/G6M2jYLZHEqI7HpwW+1b8hD9apRbFLOFUO87WCdhCqhT5997ZA9fitlFFZ0QilQ+BxbVeuXRP/gUQoCdF417gWw+8l/yi3hUhHoFtPxS2tw7Q3r34l5uzG1vXZ+QVqvI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133937; c=relaxed/simple; bh=ql6Bn+2gNBB4Xey5j1lc75dwIYP+yNqE+DHlZx8kWvY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=UDnk0fzrP08Z1+SZ6h7VTZsILkeGrexNk+5J+KtyAfpWbw46+bNPQorApqLfYmT7nrSWlhxmnn90qWb6IZBKBy70Dkr7vqml0Y8zIHsL+zFWrZ5T+bnAH8zvaVISEbgB5v/kCN3Yr7d/pcs6ysO8uuY3UBDyD52xFJI/dSXI/V8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=V1hIQwYS; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="V1hIQwYS" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-43162cf1eaaso60756755e9.0 for ; Mon, 28 Oct 2024 09:45:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133933; x=1730738733; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=XlHE1AXmr5UIRTY0JExgFknW+MYNUuiW66lPp7YixPs=; b=V1hIQwYSSR2yFmvKh9kjuH9SbCloUts8B0gpDGfnD3C2OK0r6YmsjaL0nSIHqsj8fi VpA1CfyRu8/znE4tFy4u3GbPPF96xyOivnVOB5VfAbsRN5QxTE++POfdc09HHk8yEhBK RClAzDSmeK3K3kJ8FjpwHx2xiWjBM/GXMQCcg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133933; x=1730738733; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XlHE1AXmr5UIRTY0JExgFknW+MYNUuiW66lPp7YixPs=; b=rf5tioWWnhayDYZ2SkI8ksYsBJOy3A+nIqE5NFhLei8jT2Rbr3XByTz7IPkJARHb8K UNJGI99JDdMR7cT+x0iCIWEIRWN4oJHMAMVklnehs0CD/kHBgrygdkNdvVjtPo61EtDf 4CCRsBFCXx13082yzMcJF1cOShZ6m5DF/Aljpa0b39eZbe7jZLQU7cOE1kqNQT4zlCiC f/PSNulOQMxnMx9PcXP6CSHve16kZVgA5RF0iBpCnTUoIyHSwAt4yfn9YqEUOv7GlqpI RWwAZxUFBdb+5LQyedpGf2eK+SwYKXGDFCSqO3fs0+HkNSx8UBQOl+bkhBepXsurtPG5 mdCg== X-Gm-Message-State: AOJu0YwlUh2eHlgDdnjvxfyAEieoLpa5O8kV5exJ7G0J1VNOF6Ue9JvF SkreSsYk/BdtpR/4FC8FwH6+ggFEfqYH38n+QGOv1T1CBD5bBdcLHhRKKZa9xq2wzF9PUm3h8sX C X-Google-Smtp-Source: AGHT+IGrkFPUAi8YEMClr0kIws83LbibtyvRs15RoDLA0esCvnhiOPzpA4qVR0QvhXFABdyG0U/MgA== X-Received: by 2002:a05:6000:b11:b0:37d:4376:6e1d with SMTP id ffacd0b85a97d-38061227b35mr8505267f8f.41.1730133932891; Mon, 28 Oct 2024 09:45:32 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38058b13223sm10001678f8f.1.2024.10.28.09.45.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:32 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:31 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 01/11] printk: Avoid delaying messages that aren't solicited by any console Message-ID: <023621f3c477d6246e27d11b78ae28a2bcb47e8d.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" printk_delay() may introduce delays even when no console wants to emit the message, which is unnecessary and may hold back messages we actually care about. Add a check in printk_delay() to determine if any console will print the message to avoid introducing unnecessary delays. This change aligns with the existing behaviour of boot-delayed printk messages, which already have a similar check. Signed-off-by: Chris Down Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index beb808f4c367..d6159f1c5b29 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1315,15 +1315,13 @@ static int __init boot_delay_setup(char *str) } early_param("boot_delay", boot_delay_setup); =20 -static void boot_delay_msec(int level) +static void boot_delay_msec(void) { unsigned long long k; unsigned long timeout; =20 - if ((boot_delay =3D=3D 0 || system_state >=3D SYSTEM_RUNNING) - || suppress_message_printing(level)) { + if (boot_delay =3D=3D 0 || system_state >=3D SYSTEM_RUNNING) return; - } =20 k =3D (unsigned long long)loops_per_msec * boot_delay; =20 @@ -1342,7 +1340,7 @@ static void boot_delay_msec(int level) } } #else -static inline void boot_delay_msec(int level) +static inline void boot_delay_msec(void) { } #endif @@ -2124,7 +2122,10 @@ int printk_delay_msec __read_mostly; =20 static inline void printk_delay(int level) { - boot_delay_msec(level); + if (suppress_message_printing(level)) + return; + + boot_delay_msec(); =20 if (unlikely(printk_delay_msec)) { int m =3D printk_delay_msec; --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 A867E1DF25B for ; Mon, 28 Oct 2024 16:45:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133940; cv=none; b=DQ8DCzLDbrRWex3zR9+0d0gnfSYC7p6bymkSUiP/+JADnFuuYjHCKGPZhr9i557rzdj6ggmMSTpntmr9ZiuKbxcosRZQGnLhDDtMNPGTLn93E9Po1rIBgnOK9jMTZZJ/x90hseGCEqr2pAWi0e428Z8PC/ZK9uiUhbOynYfgYvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133940; c=relaxed/simple; bh=U+YbVWnIhSxvabD1pM2bJvPXw2gHjIPI4vCrp8Hh14U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Gs1vGDckFZjX6/bw65JwKIQ6jJ1TH3XUKb+RPEIXRS565E6oGzs6aASIhbGCg4uFv71IwEpxCfsnQAefVtMWUpHb1XwQ4btmQR9dUfN/YBV6yW01aBPU/Y+0kW9mc8kgWWryPp9JbZb4OFq8ndl9UmyeTC04+2NTRR6mVUB3M6I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=DQR8Qzq8; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="DQR8Qzq8" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-43168d9c6c9so46718525e9.3 for ; Mon, 28 Oct 2024 09:45:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133936; x=1730738736; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=L2OE+Bj68T4jfh3Vb2Xf6arwPQvwLHizVBSSuQacZWA=; b=DQR8Qzq81nhoD2+qzFWE4Eyshky+fd8BotFbfU73IOMQ9+Nb6NOicJeDm7ex2IhgPJ PI02dHdBovicHIc3oMbWkjLllGcwsIvcr0SzhUGuORF9c6iPP8jT81WXeSZHBUp9otEv BKAp/RoG3siJZdIgD5a/nwi+VA4lRDo749GzY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133936; x=1730738736; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=L2OE+Bj68T4jfh3Vb2Xf6arwPQvwLHizVBSSuQacZWA=; b=GyCyP+iiScW6sTx0VYByfkPE5ngjRw03Oe9UC2O3QulsQzGX5Uy4+QMzgC/T2G8lRj 8kB+l1bp0U2VyFjngzJKMUlZBf/y4z4EOfslYHiuGg2SZHqCO+WWQX4A9XozP1MVu2M8 9hK1mLl3qQCRNtxB+9sadx7ewapH03e7sJf7drZ1t7oPp4TjixPdomtJufg3rKfN6EAS j7vfK6lY1NTc55U7gHubAwcHLUtzWyJiUa9O9Xukw1B1J4j9B0jlFc3Yqlw4SoqU92ie uHHeiiKggMZyfsEhm4XqY6IgTeJqPLJSgUH9lf1UX50C6MN7BG/aRyyx+IRLaWXXwfC/ ob8A== X-Gm-Message-State: AOJu0YwST8t1ouTa5/I6+BT2bHxDnOcxpUvqXxIG2GkXwBBZD+mDuace xMyI8i1QoQRmMuETETl1xiv5cfYY3tLJMOGbuxwJGtTtTzrYdEZR+Fsu/JHINrQ= X-Google-Smtp-Source: AGHT+IFMkr0+rZFHjU0TUe3L2p8vl9KRLvGBk85CApKRdNzYVXf1WhLGnRY0QhCEjrRS2VNCK9WlcA== X-Received: by 2002:a05:600c:1d1c:b0:431:55bf:fe4 with SMTP id 5b1f17b1804b1-431b17365ffmr12406945e9.24.1730133935814; Mon, 28 Oct 2024 09:45:35 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4318b54303fsm145336145e9.7.2024.10.28.09.45.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:35 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:34 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 02/11] printk: Use struct console for suppression and extended console state Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for supporting per-console loglevels, modify printk_get_next_message() to accept the console itself instead of individual arguments that mimic its fields. As part of the upcoming per-console loglevel support we need the console object here anyway, so it makes sense to amortise this now. devkmsg_read() has special behaviour here, but all other consoles follow the same patterns and can have their extension/suppression states determined from their struct console. Signed-off-by: Chris Down Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 4 ++-- kernel/printk/nbcon.c | 2 +- kernel/printk/printk.c | 33 ++++++++++++++++++++------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 3fcb48502adb..58ad209e0310 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -328,8 +328,8 @@ struct printk_message { }; =20 bool other_cpu_in_panic(void); -bool printk_get_next_message(struct printk_message *pmsg, u64 seq, - bool is_extended, bool may_supress); +bool printk_get_next_message(struct printk_message *pmsg, struct console *= con, + u64 seq); =20 #ifdef CONFIG_PRINTK void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped); diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index fd12efcc4aed..5ae1155c34d3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -974,7 +974,7 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt, bool use_a if (!nbcon_context_enter_unsafe(ctxt)) return false; =20 - ctxt->backlog =3D printk_get_next_message(&pmsg, ctxt->seq, is_extended, = true); + ctxt->backlog =3D printk_get_next_message(&pmsg, con, ctxt->seq); if (!ctxt->backlog) return nbcon_context_exit_unsafe(ctxt); =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d6159f1c5b29..dfe7011b863a 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -833,7 +833,7 @@ static ssize_t devkmsg_read(struct file *file, char __u= ser *buf, if (ret) return ret; =20 - if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, fals= e)) { + if (!printk_get_next_message(&pmsg, NULL, atomic64_read(&user->seq))) { if (file->f_flags & O_NONBLOCK) { ret =3D -EAGAIN; goto out; @@ -850,8 +850,8 @@ static ssize_t devkmsg_read(struct file *file, char __u= ser *buf, * This pairs with __wake_up_klogd:A. */ ret =3D wait_event_interruptible(log_wait, - printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, - false)); /* LMM(devkmsg_read:A) */ + printk_get_next_message(&pmsg, NULL, + atomic64_read(&user->seq))); /* LMM(devkmsg_read:A) */ if (ret) goto out; } @@ -2925,20 +2925,19 @@ void console_prepend_replay(struct printk_message *= pmsg) * @pmsg will contain the formatted result. @pmsg->pbufs must point to a * struct printk_buffers. * + * @con is the console in question. Only @con->flags and @con->level are + * guaranteed to be valid at this point. Note especially well that con->se= q is + * not yet guaranteed to be consistent with @seq. + * * @seq is the record to read and format. If it is not available, the next * valid record is read. * - * @is_extended specifies if the message should be formatted for extended - * console output. - * - * @may_supress specifies if records may be skipped based on loglevel. - * * Returns false if no record is available. Otherwise true and all fields * of @pmsg are valid. (See the documentation of struct printk_message * for information about the @pmsg fields.) */ -bool printk_get_next_message(struct printk_message *pmsg, u64 seq, - bool is_extended, bool may_suppress) +bool printk_get_next_message(struct printk_message *pmsg, struct console *= con, + u64 seq) { struct printk_buffers *pbufs =3D pmsg->pbufs; const size_t scratchbuf_sz =3D sizeof(pbufs->scratchbuf); @@ -2948,6 +2947,14 @@ bool printk_get_next_message(struct printk_message *= pmsg, u64 seq, struct printk_info info; struct printk_record r; size_t len =3D 0; + bool is_extended; + + if (con) { + is_extended =3D console_srcu_read_flags(con) & CON_EXTENDED; + } else { + /* Used only by devkmsg_read(). */ + is_extended =3D true; + } =20 /* * Formatting extended messages requires a separate buffer, so use the @@ -2967,8 +2974,8 @@ bool printk_get_next_message(struct printk_message *p= msg, u64 seq, pmsg->seq =3D r.info->seq; pmsg->dropped =3D r.info->seq - seq; =20 - /* Skip record that has level above the console loglevel. */ - if (may_suppress && suppress_message_printing(r.info->level)) + /* Never suppress when used in devkmsg_read() */ + if (con && suppress_message_printing(r.info->level)) goto out; =20 if (is_extended) { @@ -3044,7 +3051,7 @@ static bool console_emit_next_record(struct console *= con, bool *handover, int co =20 *handover =3D false; =20 - if (!printk_get_next_message(&pmsg, con->seq, is_extended, true)) + if (!printk_get_next_message(&pmsg, con, con->seq)) return false; =20 con->dropped +=3D pmsg.dropped; --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 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 8E1F41DF254 for ; Mon, 28 Oct 2024 16:45:40 +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=1730133943; cv=none; b=LExPvxcg6BS5pc3rt3AjTg4ckeAXdFvucAIvOq20TpSbrSY3afW0OEmsPb7nq284skcm8sNh1/5Q7uS7XPUJ96iMRQLm8DedFp1xyOMAtOWLtB5p1T4c+iAwHnMAs4CmUFw1RnOf0dJIHjcmCfG9R0APZXg7LIp0SSEis204NYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133943; c=relaxed/simple; bh=Q/o3IZHmOxdC4QaYJGFQcZogoqnTFgr1GsZTlq7L+XM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=bOLL7zQrHdrifyLyiJb1SiEHLRKLuKlpIsSJ/Q4t1MdIdN81K1aooBfZYHE0ET4r0mbfHSss7fuDGD3JuYe7Hc+4LsXjSK1wsMfkWY0u177tzCQNvPLN3QWw+ez3XK9NRheOUDS4iyd+dhDoOgNCrxKewQV1IDCaCZ0aZ035vHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=ZuopdBb+; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="ZuopdBb+" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-37d3e8d923fso3035005f8f.0 for ; Mon, 28 Oct 2024 09:45:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133939; x=1730738739; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=SRMZS8Ub8dGxMt1dDX1udMSaq6DesVwPyxLbKJJRJUY=; b=ZuopdBb+1wxNi/4pebaoHl9JEFumFHXkojOgkK2gztgyCwYVQxfVg1apxOVUJOU6FL +KFwrJRM1WlXYGaVj6th3UsjFBX7RViYT1N1A9Qhpevbra1smNB7JhA5bDFEZ4XSHZGJ A1HQQih+XrPIk415TXeTBLE3ZEMdV05yh0LvM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133939; x=1730738739; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SRMZS8Ub8dGxMt1dDX1udMSaq6DesVwPyxLbKJJRJUY=; b=Q3q7TspL1SZm66Zan+AtiZVUs/6tsVEYdZjHoNgbBvSeqhqgF9N6sqfxnnUD12V//F PMoQNmuFkxU0olmCWI0iSrqmDoj+U41EcaeR3Auk9unlvaLo/OC670o8rXeIfbUU0bW4 c+A1ExLRlf8F8AwvnXHq5s8qBkro+Krap8Kl5ibiYbzYzichlimAD6c3GsRwDgzzjTTS gB7PgnyMklIBBx2yDbJm61bKE1Rw/BdDOCJBa2gn0zsvxn2RqHjRHkJhTG7K9ji0+BN9 PIrl1tKOmIgnk5uEL21xrvYcehO/384q/aDRvjoptC1SA4gQY23Wr2LXrAd3S7jj4qRk GHDQ== X-Gm-Message-State: AOJu0YwjZkn4quoQ5vmTDULK0h3w21gCkOyZ+TwUIMpVJjhdXi6RcJDd 5w5T6qhnDFd615G57SrvFkd5BSu7UaLPd7wscAdb9rDNoBwMuh7LOFRE8Cx3ImgE2AcrQ3vWaUQ i X-Google-Smtp-Source: AGHT+IGrw1DCQhkyoQGgHJjdT72SOzHagxuMM6asNX8kvnHqUuEpwMT5tyu80InC9imCj+opFAUwNQ== X-Received: by 2002:adf:e28a:0:b0:37d:47b7:f2f5 with SMTP id ffacd0b85a97d-380610f308amr6406254f8f.4.1730133938856; Mon, 28 Oct 2024 09:45:38 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38058b712d6sm9988535f8f.78.2024.10.28.09.45.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:38 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:37 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 03/11] printk: console: Implement core per-console loglevel infrastructure Message-ID: <28d8dff56bc15b2a41f0d2035701ccb11df22610.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Consoles can have vastly different latencies and throughputs. For example, writing a message to the serial console can take on the order of tens of milliseconds to get the UART to successfully write a message. While this might be fine for a single, one-off message, this can cause significant application-level stalls in situations where the kernel writes large amounts of information to the console. This means that while you might want to send at least INFO level messages to (for example) netconsole, which is relatively fast, you may only want to send at least WARN level messages to the serial console. Such an implementation would permit debugging using the serial console in cases that netconsole doesn't receive messages during particularly bad system issues, while still keeping the noise low enough to avoid inducing latency in userspace applications. To mitigate this, add such an interface, extending the existing console loglevel controls to allow each console to have its own loglevel. One can't just disable the serial console, because one may actually need it in situations where the machine is in a bad enough state that nothing is received on netconsole. One also can't just bump the loglevel at runtime after the issue, because usually the machine is already so wedged by this point that it isn't responsive to such requests. The sysfs and kernel command line interfaces to set the per-console loglevel will be added later. For now, simply add the necessary internal infrastructure to be used by later patches. Signed-off-by: Chris Down --- drivers/tty/sysrq.c | 15 ++++++++ include/linux/console.h | 2 ++ include/linux/printk.h | 5 +++ kernel/printk/internal.h | 10 ++++++ kernel/printk/printk.c | 78 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 106 insertions(+), 4 deletions(-) diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index 930b04e3d148..daa9fe7dad54 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -51,6 +51,7 @@ #include #include #include +#include =20 #include #include @@ -101,11 +102,25 @@ __setup("sysrq_always_enabled", sysrq_always_enabled_= setup); static void sysrq_handle_loglevel(u8 key) { u8 loglevel =3D key - '0'; + int cookie; + int warned =3D 0; + struct console *con; =20 console_loglevel =3D CONSOLE_LOGLEVEL_DEFAULT; pr_info("Loglevel set to %u\n", loglevel); console_loglevel =3D loglevel; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + if (!warned && per_console_loglevel_is_set(con)) { + warned =3D 1; + pr_warn("Overriding per-console loglevel from sysrq\n"); + } + WRITE_ONCE(con->level, -1); + } + console_srcu_read_unlock(cookie); } + static const struct sysrq_key_op sysrq_loglevel_op =3D { .handler =3D sysrq_handle_loglevel, .help_msg =3D "loglevel(0-9)", diff --git a/include/linux/console.h b/include/linux/console.h index eba367bf605d..3ff22bfeef2a 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -321,6 +321,7 @@ struct nbcon_write_context { * @dropped: Number of unreported dropped ringbuffer records * @data: Driver private data * @node: hlist node for the console list + * @level: Console-specific loglevel * * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print @@ -349,6 +350,7 @@ struct console { unsigned long dropped; void *data; struct hlist_node node; + int level; =20 /* nbcon console specific members */ =20 diff --git a/include/linux/printk.h b/include/linux/printk.h index eca9bb2ee637..5fbd6b7f1ab4 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -204,6 +204,7 @@ void printk_legacy_allow_panic_sync(void); extern bool nbcon_device_try_acquire(struct console *con); extern void nbcon_device_release(struct console *con); void nbcon_atomic_flush_unsafe(void); +bool per_console_loglevel_is_set(const struct console *con); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -303,6 +304,10 @@ static inline void nbcon_device_release(struct console= *con) static inline void nbcon_atomic_flush_unsafe(void) { } +static inline bool per_console_loglevel_is_set(const struct console *con) +{ + return false; +} =20 #endif =20 diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 58ad209e0310..9303839d0551 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -15,6 +15,16 @@ int devkmsg_sysctl_set_loglvl(const struct ctl_table *ta= ble, int write, #define printk_sysctl_init() do { } while (0) #endif =20 +enum loglevel_source { + LLS_GLOBAL, + LLS_LOCAL, + LLS_IGNORE_LOGLEVEL, +}; + +enum loglevel_source +console_effective_loglevel_source(const struct console *con); +int console_effective_loglevel(const struct console *con); + #define con_printk(lvl, con, fmt, ...) \ printk(lvl pr_fmt("%s%sconsole [%s%d] " fmt), \ (con->flags & CON_NBCON) ? "" : "legacy ", \ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index dfe7011b863a..2e99b63efb46 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1287,9 +1287,62 @@ module_param(ignore_loglevel, bool, S_IRUGO | S_IWUS= R); MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting (prints all kernel messages to the console)"); =20 -static bool suppress_message_printing(int level) +bool per_console_loglevel_is_set(const struct console *con) { - return (level >=3D console_loglevel && !ignore_loglevel); + return con && (READ_ONCE(con->level) > 0); +} + +/* + * Hierarchy of loglevel authority: + * + * 1. con->level. The locally set, console-specific loglevel. Optional, on= ly + * valid if >0. + * 2. console_loglevel. The default global console loglevel, always presen= t. + */ +enum loglevel_source +console_effective_loglevel_source(const struct console *con) +{ + if (WARN_ON_ONCE(!con)) + return LLS_GLOBAL; + + if (ignore_loglevel) + return LLS_IGNORE_LOGLEVEL; + + if (per_console_loglevel_is_set(con)) + return LLS_LOCAL; + + return LLS_GLOBAL; +} + +int console_effective_loglevel(const struct console *con) +{ + enum loglevel_source source; + int level; + + source =3D console_effective_loglevel_source(con); + + switch (source) { + case LLS_IGNORE_LOGLEVEL: + level =3D CONSOLE_LOGLEVEL_MOTORMOUTH; + break; + case LLS_LOCAL: + level =3D READ_ONCE(con->level); + break; + case LLS_GLOBAL: + level =3D console_loglevel; + break; + default: + pr_warn("Unhandled console loglevel source: %d", source); + level =3D console_loglevel; + break; + } + + return level; +} + +static bool suppress_message_printing(int level, struct console *con) +{ + return level >=3D console_effective_loglevel(con); } =20 #ifdef CONFIG_BOOT_PRINTK_DELAY @@ -2122,7 +2175,21 @@ int printk_delay_msec __read_mostly; =20 static inline void printk_delay(int level) { - if (suppress_message_printing(level)) + bool will_emit =3D false; + int cookie; + struct console *con; + + cookie =3D console_srcu_read_lock(); + + for_each_console_srcu(con) { + if (!suppress_message_printing(level, con)) { + will_emit =3D true; + break; + } + } + console_srcu_read_unlock(cookie); + + if (!will_emit) return; =20 boot_delay_msec(); @@ -2975,7 +3042,7 @@ bool printk_get_next_message(struct printk_message *p= msg, struct console *con, pmsg->dropped =3D r.info->seq - seq; =20 /* Never suppress when used in devkmsg_read() */ - if (con && suppress_message_printing(r.info->level)) + if (con && suppress_message_printing(r.info->level, con)) goto out; =20 if (is_extended) { @@ -3789,6 +3856,9 @@ static int try_enable_preferred_console(struct consol= e *newcon, if (newcon->index < 0) newcon->index =3D c->index; =20 + // TODO: Will be configurable in a later patch + newcon->level =3D -1; + if (_braille_register_console(newcon, c)) return 0; =20 --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 4BAA31DF73A for ; Mon, 28 Oct 2024 16:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133946; cv=none; b=cHdIln4pjr+6iazB3eTd2nCkQyaAzyB//JixBhJdf5Tllbu6odXq0oC5QIs5FRxxEHcdp7MmP4yfOuTdE0jGLVxP7rb54gy/vrHHiEg2zgYLcD542urH40tqnfQJMMlABiT00OiO2sR/qEMNmJ2SsmRLnz4XGRQiFvYiSi1Rmmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133946; c=relaxed/simple; bh=cPBkORUG+SEbl8XM/oPwA83ucaxxbizdrui0HaiEyNM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=UiM7ynD3yA9N/byANWHKhHDnBaxA1eg/zhLI9WooCJo3s7nfDn12GQ45imcXEgUJktPZ0dAU5Uj4+alcO8gcK2o8Mw8PiCAvU5PS0eWNi9MKs59BBYkdQe4nU3iT3F+UKAr/LLj9ZvljgQA+/2tNLaCuy3ApYdjiG3ij36jW7Do= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=GuxqCE9M; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="GuxqCE9M" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4315abed18aso43756825e9.2 for ; Mon, 28 Oct 2024 09:45:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133941; x=1730738741; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=u/PxkrM9dRJKpeDPy8VpNPxAo5zWOWUFeNbvL9ub7ws=; b=GuxqCE9M5iIDtM/1lbcEkPRJiHOupqLEXXx5QHeO7sFmoG+c17t+XewUQhjxhlN2Qw e9bTGlhBOB2fGlbhiovJda2GRK4I2OFaM4+lDAESrM+KBdk9l3iZx8dzsdeZa4EiA4Ly M/ddPD25eGbwdQnLn1oIttbqJ8XnlovtjaUr8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133941; x=1730738741; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u/PxkrM9dRJKpeDPy8VpNPxAo5zWOWUFeNbvL9ub7ws=; b=ORtXiQDWpCQKqdH7ousD0miXQnPt65Dg3/tdYKiny0LcVEvdFSuLhVEl6mGfTUlqpf xXlrx1wHTzmSqLdxir2ik9T/ocJWZkZibBlV7T5dysduGAdAZCcvfRLv/oJmnNXA/B47 en7wc3b3JI01cma8nf360ASObEJOQRBNqcicRKszzwnevPCVWEy2wxoCq/qySx76iV2p ani2p8Hv1sx18Zh3ixzC70fBV7tWCdGGqLjioNjp7IpBuEka5/4VW0KSS9QbmZC9psqF B3X1ij5x7jwTFJEXO07WSjumqIeFGjKt7sI4OK9jZ+iq26uLZGjEclNCRkzZ64VF5uVf WaBw== X-Gm-Message-State: AOJu0Yxj4PCfGCjaq3wCDSujaaq6XpwTZfK/SuxjW7dYyeWKZLdcboTY WpEjQwQdq3LRq3dLTi7YRHsACpqZ6HV/ItCogx7WxpiAzft8FxWvOmqvWITDNGM= X-Google-Smtp-Source: AGHT+IGXvtjQ7yxaPQ79ddQagLrkY294O0b3+MI9nvsfLeHQov666vsRoEuG/co909M+5DKZ3zG8hA== X-Received: by 2002:a05:600c:4d21:b0:431:50cb:2398 with SMTP id 5b1f17b1804b1-431a0c3bb43mr58623635e9.2.1730133941499; Mon, 28 Oct 2024 09:45:41 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4318b579613sm144894605e9.38.2024.10.28.09.45.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:41 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:40 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 04/11] printk: Support toggling per-console loglevel via syslog() and cmdline Message-ID: <07141a533c4071c364c4f2eda6d97a9a89797e67.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A new module parameter (ignore_per_console_loglevel) is added, which can be set via the kernel command line or at runtime through /sys/module/printk/parameters/ignore_per_console_loglevel. When set, the per-console loglevels are ignored, and the global console loglevel (console_loglevel) is used for all consoles. During sysrq, we temporarily disable per-console loglevels to ensure all requisite messages are printed to the console. This is necessary because sysrq is often used in dire circumstances where access to /sys/class/console may not be trivially possible. Additionally, the syslog actions SYSLOG_ACTION_CONSOLE_ON and SYSLOG_ACTION_CONSOLE_OFF are augmented to save and restore the state of ignore_per_console_loglevel. This allows administrators to enable or disable per-console loglevels dynamically using the syslog() system call, as supported in userspace by things like dmesg. This is useful when debugging issues with message emission, or when needing to quickly break glass and revert to global loglevel only. Signed-off-by: Chris Down --- Documentation/admin-guide/index.rst | 1 + .../admin-guide/per-console-loglevel.rst | 70 +++++++++++++++++++ MAINTAINERS | 1 + drivers/tty/sysrq.c | 5 ++ include/linux/printk.h | 2 + kernel/printk/printk.c | 39 ++++++++++- 6 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 Documentation/admin-guide/per-console-loglevel.rst diff --git a/Documentation/admin-guide/index.rst b/Documentation/admin-guid= e/index.rst index e85b1adf5908..366a08a1eee2 100644 --- a/Documentation/admin-guide/index.rst +++ b/Documentation/admin-guide/index.rst @@ -119,6 +119,7 @@ configure specific aspects of kernel behavior to your l= iking. namespaces/index numastat parport + per-console-loglevel perf-security pm/index pnp diff --git a/Documentation/admin-guide/per-console-loglevel.rst b/Documenta= tion/admin-guide/per-console-loglevel.rst new file mode 100644 index 000000000000..1ec7608f94b0 --- /dev/null +++ b/Documentation/admin-guide/per-console-loglevel.rst @@ -0,0 +1,70 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _per_console_loglevel: + +Per-console loglevel support +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + +Motivation +---------- + +Consoles can have vastly different latencies and throughputs. For example, +writing a message to the serial console can take on the order of tens of +milliseconds to get the UART to successfully write a message. While this m= ight +be fine for a single, one-off message, this can cause significant +application-level stalls in situations where the kernel writes large amoun= ts of +information to the console. + +This means that while you might want to send at least INFO level messages = to +(for example) netconsole, which is relatively fast, you may only want to s= end +at least WARN level messages to the serial console. This permits debugging +using the serial console in cases that netconsole doesn't receive messages +during particularly bad system issues, while still keeping the noise low e= nough +to avoid inducing latency in userspace applications. + +Loglevel +-------- + +Kernel loglevels are defined thus: + ++---+--------------+-----------------------------------+ +| 0 | KERN_EMERG | system is unusable | ++---+--------------+-----------------------------------+ +| 1 | KERN_ALERT | action must be taken immediately | ++---+--------------+-----------------------------------+ +| 2 | KERN_CRIT | critical conditions | ++---+--------------+-----------------------------------+ +| 3 | KERN_ERR | error conditions | ++---+--------------+-----------------------------------+ +| 4 | KERN_WARNING | warning conditions | ++---+--------------+-----------------------------------+ +| 5 | KERN_NOTICE | normal but significant condition | ++---+--------------+-----------------------------------+ +| 6 | KERN_INFO | informational | ++---+--------------+-----------------------------------+ +| 7 | KERN_DEBUG | debug-level messages | ++---+--------------+-----------------------------------+ + +Tunables +-------- + +In order to allow tuning per-console loglevels, the following controls exi= st: + +Global +~~~~~~ + +The global loglevel is set by the ``kernel.console_loglevel`` sysctl, whic= h can +also be set as ``loglevel=3D`` on the kernel command line. + +The printk module also takes two parameters which modify this behaviour +further: + +* ``ignore_loglevel`` on the kernel command line or set in printk paramete= rs: + Emit all messages. All other controls are ignored if this is present. + +* ``ignore_per_console_loglevel`` on the kernel command line or set in pri= ntk + parameters: Ignore all per-console loglevels and use the global loglevel. + +The default value for ``kernel.console_loglevel`` comes from +``CONFIG_CONSOLE_LOGLEVEL_DEFAULT``, or ``CONFIG_CONSOLE_LOGLEVEL_QUIET`` = if +``quiet`` is passed on the kernel command line. diff --git a/MAINTAINERS b/MAINTAINERS index a27407950242..36490a1fc721 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18449,6 +18449,7 @@ R: John Ogness R: Sergey Senozhatsky S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git +F: Documentation/admin-guide/per-console-loglevel.rst F: include/linux/printk.h F: kernel/printk/ =20 diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index daa9fe7dad54..84befb6d8d82 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -598,6 +598,7 @@ static void __sysrq_put_key_op(u8 key, const struct sys= rq_key_op *op_p) void __handle_sysrq(u8 key, bool check_mask) { const struct sysrq_key_op *op_p; + bool orig_ignore_per_console_loglevel; int orig_log_level; int orig_suppress_printk; int i; @@ -616,6 +617,9 @@ void __handle_sysrq(u8 key, bool check_mask) orig_log_level =3D console_loglevel; console_loglevel =3D CONSOLE_LOGLEVEL_DEFAULT; =20 + orig_ignore_per_console_loglevel =3D ignore_per_console_loglevel; + ignore_per_console_loglevel =3D true; + op_p =3D __sysrq_get_key_op(key); if (op_p) { /* @@ -651,6 +655,7 @@ void __handle_sysrq(u8 key, bool check_mask) rcu_read_unlock(); rcu_sysrq_end(); =20 + ignore_per_console_loglevel =3D orig_ignore_per_console_loglevel; suppress_printk =3D orig_suppress_printk; } =20 diff --git a/include/linux/printk.h b/include/linux/printk.h index 5fbd6b7f1ab4..0053533dcfec 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -73,6 +73,8 @@ extern int console_printk[]; #define minimum_console_loglevel (console_printk[2]) #define default_console_loglevel (console_printk[3]) =20 +extern bool ignore_per_console_loglevel; + extern void console_verbose(void); =20 /* strlen("ratelimit") + 1 */ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 2e99b63efb46..055619c5c7e8 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -103,6 +103,9 @@ DEFINE_STATIC_SRCU(console_srcu); */ int __read_mostly suppress_printk; =20 +/* The sysrq infrastructure needs this even on !CONFIG_PRINTK. */ +bool __read_mostly ignore_per_console_loglevel; + #ifdef CONFIG_LOCKDEP static struct lockdep_map console_lock_dep_map =3D { .name =3D "console_lock" @@ -1287,9 +1290,21 @@ module_param(ignore_loglevel, bool, S_IRUGO | S_IWUS= R); MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting (prints all kernel messages to the console)"); =20 +static int __init ignore_per_console_loglevel_setup(char *str) +{ + ignore_per_console_loglevel =3D true; + return 0; +} + +early_param("ignore_per_console_loglevel", ignore_per_console_loglevel_set= up); +module_param(ignore_per_console_loglevel, bool, 0644); +MODULE_PARM_DESC( + ignore_per_console_loglevel, + "ignore per-console loglevel setting (only respect global console logleve= l)"); + bool per_console_loglevel_is_set(const struct console *con) { - return con && (READ_ONCE(con->level) > 0); + return !ignore_per_console_loglevel && con && (READ_ONCE(con->level) > 0); } =20 /* @@ -1298,6 +1313,16 @@ bool per_console_loglevel_is_set(const struct consol= e *con) * 1. con->level. The locally set, console-specific loglevel. Optional, on= ly * valid if >0. * 2. console_loglevel. The default global console loglevel, always presen= t. + * + * The behaviour can be further changed by the following printk module + * parameters: + * + * 1. ignore_loglevel. Can be set at boot or at runtime with + * /sys/module/printk/parameters/ignore_loglevel. Overrides absolutely + * everything since it's used to debug. + * 2. ignore_per_console_loglevel. Existing per-console loglevel values ar= e left + * intact, but are ignored in favour of console_loglevel as long as thi= s is + * true. Also manipulated through syslog(SYSLOG_ACTION_CONSOLE_{ON,OFF}= ). */ enum loglevel_source console_effective_loglevel_source(const struct console *con) @@ -1796,6 +1821,7 @@ int do_syslog(int type, char __user *buf, int len, in= t source) struct printk_info info; bool clear =3D false; static int saved_console_loglevel =3D LOGLEVEL_DEFAULT; + static int saved_ignore_per_console_loglevel; int error; =20 error =3D check_syslog_permissions(type, source); @@ -1836,19 +1862,28 @@ int do_syslog(int type, char __user *buf, int len, = int source) break; /* Disable logging to console */ case SYSLOG_ACTION_CONSOLE_OFF: - if (saved_console_loglevel =3D=3D LOGLEVEL_DEFAULT) + if (saved_console_loglevel =3D=3D LOGLEVEL_DEFAULT) { saved_console_loglevel =3D console_loglevel; + saved_ignore_per_console_loglevel =3D + ignore_per_console_loglevel; + } console_loglevel =3D minimum_console_loglevel; + ignore_per_console_loglevel =3D true; break; /* Enable logging to console */ case SYSLOG_ACTION_CONSOLE_ON: if (saved_console_loglevel !=3D LOGLEVEL_DEFAULT) { console_loglevel =3D saved_console_loglevel; + ignore_per_console_loglevel =3D + saved_ignore_per_console_loglevel; saved_console_loglevel =3D LOGLEVEL_DEFAULT; } break; /* Set level of messages printed to console */ case SYSLOG_ACTION_CONSOLE_LEVEL: + if (!ignore_per_console_loglevel) + pr_warn_once( + "SYSLOG_ACTION_CONSOLE_LEVEL is ignored by consoles with an explicitly= set per-console loglevel, see Documentation/admin-guide/per-console-loglev= el.rst\n"); if (len < 1 || len > 8) return -EINVAL; if (len < minimum_console_loglevel) --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 CD67D1DF752 for ; Mon, 28 Oct 2024 16:45:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133947; cv=none; b=kd8jOv2/9JgNT4WxEKj1VcdBrVrinyljqdUdYZZaMAkVfhez0QwhsDhkuEjAWZsoglvj13HbT+z4KXNtr8Ob8XY0ex8hwmVZHZBiKHo5KFt/i2yKlbIznxx1mocMIJ2dpsrM3WXz8iVYe5k133TBkxW2nrMHNrwkA8meO/aThz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133947; c=relaxed/simple; bh=ToxVpap89sdmMrlRQDwTP3V9Zux3B7rIoxt1tc4MLys=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=W7yuyv60Eb8CMFULXqsaBLzUQM+Y1c6cvNJMq48oPWKlllipz/oJXvYviANIbTiiN39//5E6/wBCAO6zO1psjEoFd/6v+2v2oCAGGzuuSVyqUj2VyG3EAOFBDBxe9BxgdVD8QtofqRWPt+PjIIZQ2h/+XEdH1jnkRuO3qKsVwNU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=kijhPdzN; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="kijhPdzN" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4314c452180so38206105e9.0 for ; Mon, 28 Oct 2024 09:45:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133944; x=1730738744; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=jVZb7rcW7K/0zR07WvqpGJJQNS5kDJRHk0M0yg+StJM=; b=kijhPdzNxQ7EEJ0Jt4NnlFsqgdSkB6JVYppOQFxkIzuWu8TXTmcU5dxqlC16QEKAsY fE4bVokBcork27NoCSMwp3M5lcohPzvjULD8Gzos4ku+YrsBWbh88gg4PCakwtfueVsB 2bAbWG5EUhrAYCiukPPuRDXCAyICTceEBpeGc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133944; x=1730738744; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jVZb7rcW7K/0zR07WvqpGJJQNS5kDJRHk0M0yg+StJM=; b=E8wNsVaK4vApgyneJRaRTp0nFASvNtWYW6BLSkTxjvwAo9MnK0HvdlLxBGyJcz6O8T M+d969PeQcU+R4u+6pQ0qcRmLaFKDqyiUZm8ED0z1SICzNsp+u1w4hbj5+CiyL61jzTp Ws0g5tWCThsaz6YO4zDlNnyrmLVkwxnyO8XEUWWGUjNc46E1GQU3O3tGhsP4wkDYRmNN gekf4Ihmohc+h245HKb7+q666qFuTpa+EmxjJ133rWFf8i8f4FMnzHr3/uWxSIrlXFA5 Ukn3/tIWmiW19dcJTyOMxpsPawbKbX9BhUnf2NSCbrqFnJkVMTza/ElS49cpeh9v8hSe 30Bg== X-Gm-Message-State: AOJu0YylmDcKbrdQT1Gi3kCHHHKuWWPh6Qr0EhYhsoUdOgYS8W9Uxf+D qtubbatBhr3Kz6M1g9yAfuPtpP/d0jhLe47F9ssrESIaEoUfC6TrXwH/1G5WaY4= X-Google-Smtp-Source: AGHT+IFXK+pe4KdWXxKpJ1VWwSIYQUau5mXRPx7fYGC+9vgU1Sl6HheAYLJs8ocd9CzbesZ3zzbuPA== X-Received: by 2002:adf:f84b:0:b0:37d:321e:ef0c with SMTP id ffacd0b85a97d-380813c9f8dmr181849f8f.11.1730133944115; Mon, 28 Oct 2024 09:45:44 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38058b13223sm10002068f8f.1.2024.10.28.09.45.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:43 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:43 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 05/11] MAINTAINERS: Mark printk-basics.rst as owned by printk subsystem Message-ID: <62e6a36052a1759e7d01669c5c760b6a1760a9c8.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We are going to update this in the next patch, so while we're here, we might as well mark it as owned by us. Signed-off-by: Chris Down Reviewed-by: Petr Mladek --- MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index 36490a1fc721..ea134675669e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18450,6 +18450,7 @@ R: Sergey Senozhatsky S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git F: Documentation/admin-guide/per-console-loglevel.rst +F: Documentation/core-api/printk-basics.rst F: include/linux/printk.h F: kernel/printk/ =20 --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 433C31DF256 for ; Mon, 28 Oct 2024 16:45:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133953; cv=none; b=renGRuz8hn2YvyL+BQo8j81oK+kR3Mf7NE57a/7ZtHtK+mXs5sk/RXMgk2JBseQ+Z/2+aIltiBDOnKjj/xn+SgMmS5mrOQ22A5uev7B3nA+wtJQXhvRqM27EB9C+0OinxS5fOOfHmQqhmt1MMWuGH8XQgBBQM+48ncs2HLpRSR4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133953; c=relaxed/simple; bh=3x6gr9wBt+x6TVoNuvYHGkNSrzFnusUE/PC3JjKWIZc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=R5YlougfKTIMrMRSWiW6Y87w5eaon5yEvITbaMEsc9kZZ9c5VybWnBH58VUy5iCXxcdjz+cpZpcM4KFV49y3ewIGri6qNAU/DYXURK4TpG1sheV3LSX5TWL9g4Jzv1D2uufzgoUbWAGXMWoJjyP1mEnz1FvVmFNePTD0RQ1wYp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=Ic0Ak++W; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="Ic0Ak++W" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-431616c23b5so32363585e9.0 for ; Mon, 28 Oct 2024 09:45:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133947; x=1730738747; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=jLWBrJD3AEjxwad8FLGAsXjK+gMwY4h/fP5eGo4K0TA=; b=Ic0Ak++WaTq3ErqjSmLrcTrdx0kcnwxPqsV86qf1UZjB28ARmrz7OPAPEgDV0e4Rr2 IkR6o0GT4CKfbuvd5SdIqyJ52BCjmpj4uFmXfbXdJg5pha+7VkS5qCO5/LF5gsAdnVx+ gzxr2gaV5eswb8UNCp4MG72MoL70maVRE0Z98= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133947; x=1730738747; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jLWBrJD3AEjxwad8FLGAsXjK+gMwY4h/fP5eGo4K0TA=; b=I6cUuu6ungaCTp0lmeTc4Tx+2VK7kCyhMBMJy8DZK2RqvdtFDXxF5x15di5OQxRpmo +T8Vi4Lrsq4ovwXgr6e67ihSR1OsxWlOgthSqBy3Nu0bGPBixJULMqX24nWLiQgocm9I KOY0xMN/M9BCvn/pIHV24HhtPl4JaD3VERCX4REMiV2M+AlPVxbUYPtW3Vp0UumsLgzF AqXvjyT2TaUyevdV6v641WoOyA0vfXFSsT4zbzdepzjuodWZhQYFgYL9L4B7EYA1/WSC OsFfv+NuiaiLoKSm7FhrvleMKR7LLbLjG/UC4XdyM6FOwqMB17KSCB2riid/tb4NGTVW Jahw== X-Gm-Message-State: AOJu0YxVLPiHvFOLQj8EhRPTh1dKH9crHVrvWTas3PgDd4QK5QrM5p7z NPzrBwPa710dctC9h4jYVAKsZh47qYe1LKCBX/xD5CTFwCL/P+VK113FbgaK91A= X-Google-Smtp-Source: AGHT+IEvj5aZpCxfOd14azbDKVyLNyZgRdGICLEYQLmtIuJBuOrhn1LF6UU/+UOJ93peFTGpI2QMkQ== X-Received: by 2002:a05:600c:1f90:b0:431:136b:8bef with SMTP id 5b1f17b1804b1-431b2ef8745mr2334045e9.7.1730133947111; Mon, 28 Oct 2024 09:45:47 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38058b479a4sm9932061f8f.55.2024.10.28.09.45.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:46 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:46 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 06/11] printk: console: Introduce sysfs interface for per-console loglevels Message-ID: <0312cd1e80e68a1450a194ebce27728cdf497575.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A sysfs interface under /sys/class/console/ is created that permits viewing and configuring per-console attributes. This is the main interface with which we expect users to interact with and configure per-console loglevels. Each console device now has its own directory (for example, /sys/class/console/ttyS0/) containing the following attributes: - effective_loglevel (ro): The effective loglevel for the console after considering all loglevel authorities (e.g., global loglevel, per-console loglevel). - effective_loglevel_source (ro): The source of the effective loglevel (e.g., local, global, ignore_loglevel). - enabled (ro): Indicates whether the console is enabled (1) or disabled (0). - loglevel (rw): The per-console loglevel. Writing a value between 0 (KERN_EMERG) and 8 (KERN_DEBUG + 1) sets the per-console loglevel. Writing -1 disables the per-console loglevel. In terms of technical implementation, we embed a device pointer in the console struct, and register each console using it so we can expose attributes in sysfs. We currently expose the following attributes: % ls -l /sys/class/console/ttyS0/ total 0 lrwxrwxrwx 1 root root 0 Oct 23 13:17 subsystem -> ../../../../class= /console/ -r--r--r-- 1 root root 4096 Oct 23 13:18 effective_loglevel -r--r--r-- 1 root root 4096 Oct 23 13:18 effective_loglevel_source -r--r--r-- 1 root root 4096 Oct 23 13:18 enabled -rw-r--r-- 1 root root 4096 Oct 23 13:18 loglevel -rw-r--r-- 1 root root 4096 Oct 23 13:17 uevent The lifecycle of this classdev looks like this on registration: register_console(con)/printk_late_init() console_register_device(con) device_initialize(con->classdev) # refcount++ device_add(con->classdev) # refcount++ At stable state, the refcount is two. Console unregistration looks like this: [con->classdev refcount drops to 0] console_classdev_release(con->classdev) kfree(con->classdev) unregister_console(con) device_unregister(con->classdev) device_del(con->classdev) # refcount-- device_remove_class_symlinks() kernfs_remove_by_name_ns() kernfs_drain() kernfs_drain_open_files() # wait for close() put_device(con->classdev) # refcount-- Signed-off-by: Chris Down --- Documentation/ABI/testing/sysfs-class-console | 47 +++++ .../admin-guide/per-console-loglevel.rst | 41 ++++ Documentation/core-api/printk-basics.rst | 35 ++-- Documentation/networking/netconsole.rst | 13 ++ MAINTAINERS | 1 + include/linux/console.h | 3 + include/linux/printk.h | 2 + kernel/printk/Makefile | 2 +- kernel/printk/internal.h | 5 + kernel/printk/printk.c | 15 ++ kernel/printk/sysfs.c | 178 ++++++++++++++++++ 11 files changed, 324 insertions(+), 18 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-class-console create mode 100644 kernel/printk/sysfs.c diff --git a/Documentation/ABI/testing/sysfs-class-console b/Documentation/= ABI/testing/sysfs-class-console new file mode 100644 index 000000000000..40b90b190af3 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-class-console @@ -0,0 +1,47 @@ +What: /sys/class/console/ +Date: October 2024 +Contact: Chris Down +Description: Interface for viewing and setting per-console attributes, like + the per-console loglevel. For a high-level document describing + the motivations for this interface and related non-sysfs + controls, see + Documentation/admin-guide/per-console-loglevel.rst. + +What: /sys/class/console//effective_loglevel +Date: October 2024 +Contact: Chris Down +Description: Read only. The currently effective loglevel for this console. + All messages emitted with a loglevel below the effective value + will be emitted to the console. + +What: /sys/class/console//effective_loglevel_source +Date: October 2024 +Contact: Chris Down +Description: Read only. The currently effective loglevel source for this + console -- for example, whether it was set globally, or whether + it was set locally for this console. + + Possible values are: + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + local The loglevel comes from the per-console + loglevel. + global The loglevel comes from the global loglevel. + ignore_loglevel Both the per-console loglevel and global + loglevels are ignored as ignore_loglevel is + present on the kernel command line. + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +What: /sys/class/console//enabled +Date: October 2024 +Contact: Chris Down +Description: Read only. "1" if the console is enabled, "0" otherwise. + +What: /sys/class/console//loglevel +Date: October 2024 +Contact: Chris Down +Description: Read write. The current per-console loglevel, which will take + effect if not overridden by other non-sysfs controls (see + Documentation/admin-guide/per-console-loglevel.rst). Bounds are + 0 (LOGLEVEL_EMERG) to 8 (LOGLEVEL_DEBUG + 1) inclusive. Also + takes the special value "-1" to indicate that no per-console + loglevel is set, and we should defer to the global controls. diff --git a/Documentation/admin-guide/per-console-loglevel.rst b/Documenta= tion/admin-guide/per-console-loglevel.rst index 1ec7608f94b0..41bf3befb2f3 100644 --- a/Documentation/admin-guide/per-console-loglevel.rst +++ b/Documentation/admin-guide/per-console-loglevel.rst @@ -68,3 +68,44 @@ further: The default value for ``kernel.console_loglevel`` comes from ``CONFIG_CONSOLE_LOGLEVEL_DEFAULT``, or ``CONFIG_CONSOLE_LOGLEVEL_QUIET`` = if ``quiet`` is passed on the kernel command line. + +Console attributes +~~~~~~~~~~~~~~~~~~ + +Registered consoles are exposed at ``/sys/class/console``. For example, if= you +are using ``ttyS0``, the console backing it can be viewed at +``/sys/class/console/ttyS0/``. The following files are available: + +* ``effective_loglevel`` (r): The effective loglevel after considering all + loglevel authorities. For example, it shows the value of the console-spe= cific + loglevel when a console-specific loglevel is defined, and shows the glob= al + console loglevel value when the console-specific one is not defined. + +* ``effective_loglevel_source`` (r): The loglevel authority which resulted= in + the effective loglevel being set. The following values can be present: + + * ``local``: The console-specific loglevel is in effect. + + * ``global``: The global loglevel (``kernel.console_loglevel``) is in + effect. Set a console-specific loglevel to override it. + + * ``ignore_loglevel``: ``ignore_loglevel`` was specified on the kernel + command line or at ``/sys/module/printk/parameters/ignore_loglevel``. + Disable it to use level controls. + +* ``enabled`` (r): Whether the console is enabled. + +* ``loglevel`` (rw): The local, console-specific loglevel for this console. + This will be in effect if no other global control overrides it. Look at + ``effective_loglevel`` and ``effective_loglevel_source`` to verify that. + +Deprecated +~~~~~~~~~~ + +* ``kernel.printk`` sysctl: this takes four values, setting + ``kernel.console_loglevel``, ``kernel.default_message_loglevel``, the mi= nimum + console loglevel, and a fourth unused value. The interface is generally + considered to quite confusing, doesn't perform checks on the values give= n, + and is unaware of per-console loglevel semantics. + +Chris Down , 13-October-2024 diff --git a/Documentation/core-api/printk-basics.rst b/Documentation/core-= api/printk-basics.rst index 2dde24ca7d9f..bfad359505bb 100644 --- a/Documentation/core-api/printk-basics.rst +++ b/Documentation/core-api/printk-basics.rst @@ -54,32 +54,33 @@ string, the log level is not a separate argument). The = available log levels are: =20 The log level specifies the importance of a message. The kernel decides wh= ether to show the message immediately (printing it to the current console) depen= ding -on its log level and the current *console_loglevel* (a kernel variable). I= f the -message priority is higher (lower log level value) than the *console_logle= vel* -the message will be printed to the console. +on its log level and the current global *console_loglevel* or local per-co= nsole +loglevel (kernel variables). If the message priority is higher (lower log = level +value) than the effective loglevel the message will be printed to the cons= ole. =20 If the log level is omitted, the message is printed with ``KERN_DEFAULT`` level. =20 -You can check the current *console_loglevel* with:: +You can check the current console's loglevel -- for example if you want to +check the loglevel for serial consoles: =20 - $ cat /proc/sys/kernel/printk - 4 4 1 7 + $ cat /sys/class/console/ttyS0/effective_loglevel + 6 + $ cat /sys/class/console/ttyS0/effective_loglevel_source + local =20 -The result shows the *current*, *default*, *minimum* and *boot-time-defaul= t* log -levels. +To change the default loglevel for all consoles, simply write the desired = level +to ``/proc/sys/kernel/console_loglevel``. For example:: =20 -To change the current console_loglevel simply write the desired level to -``/proc/sys/kernel/printk``. For example, to print all messages to the con= sole:: + # echo 5 > /proc/sys/kernel/console_loglevel =20 - # echo 8 > /proc/sys/kernel/printk +This sets the console_loglevel to print KERN_WARNING (4) or more severe +messages to console. Consoles with a per-console loglevel set will ignore = it +unless ``ignore_per_console_loglevel`` is set on the kernel command line o= r at +``/sys/module/printk/parameters/ignore_per_console_loglevel``. =20 -Another way, using ``dmesg``:: - - # dmesg -n 5 - -sets the console_loglevel to print KERN_WARNING (4) or more severe message= s to -console. See ``dmesg(1)`` for more information. +For more information on per-console loglevels, see +Documentation/admin-guide/per-console-loglevel.rst. =20 As an alternative to printk() you can use the ``pr_*()`` aliases for logging. This family of macros embed the log level in the macro names. For diff --git a/Documentation/networking/netconsole.rst b/Documentation/networ= king/netconsole.rst index d55c2a22ec7a..34419e6fe106 100644 --- a/Documentation/networking/netconsole.rst +++ b/Documentation/networking/netconsole.rst @@ -72,6 +72,19 @@ Built-in netconsole starts immediately after the TCP sta= ck is initialized and attempts to bring up the supplied dev at the supplied address. =20 +You can also set a loglevel at runtime:: + + $ ls -l /sys/class/console/netcon0/ + total 0 + lrwxrwxrwx 1 root root 0 May 18 13:28 subsystem -> ../../../../class/= console/ + -r--r--r-- 1 root root 4096 May 18 13:28 effective_loglevel + -r--r--r-- 1 root root 4096 May 18 13:28 effective_loglevel_source + -r--r--r-- 1 root root 4096 May 18 13:28 enabled + -rw-r--r-- 1 root root 4096 May 18 13:28 loglevel + -rw-r--r-- 1 root root 4096 May 18 13:28 uevent + +See Documentation/admin-guide/per-console-loglevel.rst for more informatio= n. + The remote host has several options to receive the kernel messages, for example: =20 diff --git a/MAINTAINERS b/MAINTAINERS index ea134675669e..003f999e531b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18449,6 +18449,7 @@ R: John Ogness R: Sergey Senozhatsky S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git +F: Documentation/ABI/testing/sysfs-class-console F: Documentation/admin-guide/per-console-loglevel.rst F: Documentation/core-api/printk-basics.rst F: include/linux/printk.h diff --git a/include/linux/console.h b/include/linux/console.h index 3ff22bfeef2a..332eef0f95f7 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -16,6 +16,7 @@ =20 #include #include +#include #include #include #include @@ -322,6 +323,7 @@ struct nbcon_write_context { * @data: Driver private data * @node: hlist node for the console list * @level: Console-specific loglevel + * @classdev: Console class device for /sys/class/console * * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print @@ -351,6 +353,7 @@ struct console { void *data; struct hlist_node node; int level; + struct device *classdev; =20 /* nbcon console specific members */ =20 diff --git a/include/linux/printk.h b/include/linux/printk.h index 0053533dcfec..b7e8411e033d 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -77,6 +77,8 @@ extern bool ignore_per_console_loglevel; =20 extern void console_verbose(void); =20 +int clamp_loglevel(int level); + /* strlen("ratelimit") + 1 */ #define DEVKMSG_STR_MAX_SIZE 10 extern char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE]; diff --git a/kernel/printk/Makefile b/kernel/printk/Makefile index 39a2b61c7232..3c0b6e2a8083 100644 --- a/kernel/printk/Makefile +++ b/kernel/printk/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y =3D printk.o +obj-y =3D printk.o sysfs.o obj-$(CONFIG_PRINTK) +=3D printk_safe.o nbcon.o obj-$(CONFIG_A11Y_BRAILLE_CONSOLE) +=3D braille.o obj-$(CONFIG_PRINTK_INDEX) +=3D index.o diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 9303839d0551..ac607d6907a0 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -21,6 +21,11 @@ enum loglevel_source { LLS_IGNORE_LOGLEVEL, }; =20 +void console_register_device(struct console *new); +void console_setup_class(void); + +int clamp_loglevel(int level); + enum loglevel_source console_effective_loglevel_source(const struct console *con); int console_effective_loglevel(const struct console *con); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 055619c5c7e8..bc456f7624d4 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -199,6 +199,12 @@ static int __init control_devkmsg(char *str) } __setup("printk.devkmsg=3D", control_devkmsg); =20 +int clamp_loglevel(int level) +{ + return clamp(level, minimum_console_loglevel, + CONSOLE_LOGLEVEL_MOTORMOUTH); +} + char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] =3D "ratelimit"; #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL) int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write, @@ -3894,6 +3900,8 @@ static int try_enable_preferred_console(struct consol= e *newcon, // TODO: Will be configurable in a later patch newcon->level =3D -1; =20 + newcon->classdev =3D NULL; + if (_braille_register_console(newcon, c)) return 0; =20 @@ -4170,6 +4178,7 @@ void register_console(struct console *newcon) if (use_device_lock) newcon->device_unlock(newcon, flags); =20 + console_register_device(newcon); console_sysfs_notify(); =20 /* @@ -4264,6 +4273,9 @@ static int unregister_console_locked(struct console *= console) if (console->flags & CON_NBCON) nbcon_free(console); =20 + if (console->classdev) + device_unregister(console->classdev); + console_sysfs_notify(); =20 if (console->exit) @@ -4428,6 +4440,9 @@ static int __init printk_late_init(void) console_cpu_notify, NULL); WARN_ON(ret < 0); printk_sysctl_init(); + + console_setup_class(); + return 0; } late_initcall(printk_late_init); diff --git a/kernel/printk/sysfs.c b/kernel/printk/sysfs.c new file mode 100644 index 000000000000..e24590074861 --- /dev/null +++ b/kernel/printk/sysfs.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include "internal.h" + +#ifdef CONFIG_PRINTK +static struct class *console_class; + +static const char * +console_effective_loglevel_source_str(const struct console *con) +{ + enum loglevel_source source; + const char *str; + + source =3D console_effective_loglevel_source(con); + + switch (source) { + case LLS_IGNORE_LOGLEVEL: + str =3D "ignore_loglevel"; + break; + case LLS_LOCAL: + str =3D "local"; + break; + case LLS_GLOBAL: + str =3D "global"; + break; + default: + pr_warn("Unhandled console loglevel source: %d", source); + str =3D "unknown"; + break; + } + + return str; +} + +static ssize_t loglevel_show(struct device *dev, struct device_attribute *= attr, + char *buf) +{ + struct console *con =3D dev_get_drvdata(dev); + + return sysfs_emit(buf, "%d\n", READ_ONCE(con->level)); +} + +static ssize_t loglevel_store(struct device *dev, struct device_attribute = *attr, + const char *buf, size_t size) +{ + struct console *con =3D dev_get_drvdata(dev); + ssize_t ret; + int level; + + ret =3D kstrtoint(buf, 10, &level); + if (ret < 0) + return ret; + + if (level =3D=3D -1) + goto out; + + if (clamp_loglevel(level) !=3D level) + return -ERANGE; + +out: + WRITE_ONCE(con->level, level); + + return size; +} + +static DEVICE_ATTR_RW(loglevel); + +static ssize_t effective_loglevel_source_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct console *con =3D dev_get_drvdata(dev); + + return sysfs_emit(buf, "%s\n", + console_effective_loglevel_source_str(con)); +} + +static DEVICE_ATTR_RO(effective_loglevel_source); + +static ssize_t effective_loglevel_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct console *con =3D dev_get_drvdata(dev); + + return sysfs_emit(buf, "%d\n", console_effective_loglevel(con)); +} + +static DEVICE_ATTR_RO(effective_loglevel); + +static ssize_t enabled_show(struct device *dev, struct device_attribute *a= ttr, + char *buf) +{ + struct console *con =3D dev_get_drvdata(dev); + int cookie; + bool enabled; + + cookie =3D console_srcu_read_lock(); + enabled =3D console_srcu_read_flags(con) & CON_ENABLED; + console_srcu_read_unlock(cookie); + return sysfs_emit(buf, "%d\n", enabled); +} + +static DEVICE_ATTR_RO(enabled); + +static struct attribute *console_sysfs_attrs[] =3D { + &dev_attr_loglevel.attr, + &dev_attr_effective_loglevel_source.attr, + &dev_attr_effective_loglevel.attr, + &dev_attr_enabled.attr, + NULL, +}; + +ATTRIBUTE_GROUPS(console_sysfs); + +static void console_classdev_release(struct device *dev) +{ + kfree(dev); +} + +void console_register_device(struct console *con) +{ + /* + * We might be called from register_console() before the class is + * registered. If that happens, we'll take care of it in + * printk_late_init. + */ + if (IS_ERR_OR_NULL(console_class)) + return; + + if (WARN_ON(con->classdev)) + return; + + con->classdev =3D kzalloc(sizeof(struct device), GFP_KERNEL); + if (!con->classdev) + return; + + device_initialize(con->classdev); + dev_set_name(con->classdev, "%s%d", con->name, con->index); + dev_set_drvdata(con->classdev, con); + con->classdev->release =3D console_classdev_release; + con->classdev->class =3D console_class; + if (device_add(con->classdev)) + put_device(con->classdev); +} + +void console_setup_class(void) +{ + struct console *con; + int cookie; + + /* + * printk exists for the lifetime of the kernel, it cannot be unloaded, + * so we should never end up back in here. + */ + if (WARN_ON(console_class)) + return; + + console_class =3D class_create("console"); + if (!IS_ERR(console_class)) + console_class->dev_groups =3D console_sysfs_groups; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) + console_register_device(con); + console_srcu_read_unlock(cookie); +} +#else /* CONFIG_PRINTK */ +void console_register_device(struct console *new) +{ +} +void console_setup_class(void) +{ +} +#endif --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.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 90D7C1DFD91 for ; Mon, 28 Oct 2024 16:45:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133953; cv=none; b=J4L2pwkTVou95hxe3zQ3sOhHdSnBsdzkU2cLqUIdPlQAchr3wyE/r1VDK6re7jISNbm8WshBGmbdXNKbIYKWTK4s46bXYXyOScXq0D8UwQ83WagpaaIuoMUY1UJF35i7bAzJzSsbwaYZb+v5ZU/mlGNG1dg/muhjNIRKylkaBlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133953; c=relaxed/simple; bh=Vmt82dwaOjFT6uTwz3tL39RVhl65BBmX7ghox9IjL+k=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hTi8koCJvRlZvTSMy8mL7lWt5iMhvtDUo5G9A69gP9/blUmOXtHa/JSlX4WJq3DNGrLGto+gYgf3fFAgIR5YqG00O1tsslW2yIKHZaKlLCngaM5HkARVDfkA3ysEkO9TPR1gq1EtO3cbWHmkCrMykJVJjqE3ZTB63hVsRxFkmQ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=VXMnEqtb; arc=none smtp.client-ip=209.85.167.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="VXMnEqtb" Received: by mail-lf1-f45.google.com with SMTP id 2adb3069b0e04-539f4d8ef66so5987050e87.1 for ; Mon, 28 Oct 2024 09:45:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133950; x=1730738750; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=wLkfA7TIE3/EUT1Ui9HqWC+wbqjWNmJFUMwyewiF4NA=; b=VXMnEqtbERrYDcpJWsAvv7cd+NVBPAjQ6g8hVbHB49v5nw6OynJXMZlQwmd1AjANGO in+zXXIHJdMmO91VAGyK3zG7Ti37jWHig1qTPsoK8FItMELW0gbNObo8/qysyrmpm7GL 7Oxlv5onExKNJWea5Cbzl5cuPzHs3zQIXYgO4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133950; x=1730738750; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wLkfA7TIE3/EUT1Ui9HqWC+wbqjWNmJFUMwyewiF4NA=; b=Wl6SIfZyOwBrnYbC/d0uW44+rlOJJ/oztb8HslUQEOo7/n9oNMBEQanM1Pbi6pp5mw yfFtMwbrQan3AChtrYKOIkKD8G2nICflLNm9bVCzeTMT9vfb2zTOjo7hFx+IsA7C2Tqy ax3JfTUaX0smMKYyKzuGn8uHEk4bHtm83+WadxLZQgDH1Jiexu9g9vED8NPHtv8UNinn eMrYm/GhvxJWMBOjWADXl3LBkZdWQ+/0ZkdkFth5ixmVfRTrIB0O9IYITs82kxxOnU8/ FFuK72Y9xp+8vKXZBBIQ+SRA/lNgf6srYCPWfIp8o9o7nn/8G0bAjyCNvuIydvRk6C1F gMjA== X-Gm-Message-State: AOJu0YzEM77LphAeGtVCcDV0QbR933wlXEydIwipYQCI241CVYxGuNC8 sR/tR3ykYxiZU4VEgOfV+qOTKYFs9Mrw0z3r4Lh5tsSY/VIDty1vvk3Nupm2UXQ= X-Google-Smtp-Source: AGHT+IHXkIqu82alCxKsn+FjVpdMLj7nsam7bAPuN2BNkxCv+ZuZYKCCNGkZ28Yyml9WXbJLfQDFlw== X-Received: by 2002:a05:6512:3407:b0:539:f67b:b849 with SMTP id 2adb3069b0e04-53b34c8b7e4mr3729760e87.49.1730133949583; Mon, 28 Oct 2024 09:45:49 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4318b567e23sm144787645e9.25.2024.10.28.09.45.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:49 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:48 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 07/11] printk: Constrain hardware-addressed console checks to name position Message-ID: <507dee05e622cc37418b23e5de96b6b449410414.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 7640f1a44eba ("printk: Add match_devname_and_update_preferred_console()") adds support for DEVNAME:n:n style console=3D registration. It determines whether or not this is a hardware-addressed console by looking at whether the console=3D string has a colon in it. However, this interferes with loglevel:n and potentially other future named options, which also make use of the character. In order to avoid this, only search positions before options. Signed-off-by: Chris Down Reviewed-by: Petr Mladek Reviewed-by: Tony Lindgren --- kernel/printk/printk.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index bc456f7624d4..bbd037b84a0d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2670,6 +2670,7 @@ static int __init console_setup(char *str) char *devname =3D NULL; char *options; char *s; + char *first_delim; int idx; =20 /* @@ -2685,8 +2686,13 @@ static int __init console_setup(char *str) if (_braille_console_setup(&str, &brl_options)) return 1; =20 - /* For a DEVNAME:0.0 style console the character device is unknown early = */ - if (strchr(str, ':')) + /* + * For a DEVNAME:0.0 style console the character device is unknown + * early. We must restrict this to before any comma to avoid interfering + * with named options, like "loglevel". + */ + first_delim =3D strpbrk(str, ":,"); + if (first_delim && *first_delim =3D=3D ':') devname =3D buf; else ttyname =3D buf; --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.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 945DD1DFDBC for ; Mon, 28 Oct 2024 16:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133959; cv=none; b=VN1ITVPbV+CO4n+ze75BKPEocoxTfXcn8Ql2Klou3p/GcYdIsofHHzOqyPgD7xgDLlfpjgbql2SBdrunYHMOonZcVwkpI83fHJITFhb6pvEOnUWlc6Awhq4TM+4YGZzN0DdK/67SNSd3ajS47bZAiLRQa2L7wlK5Uoem7Nge0FQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133959; c=relaxed/simple; bh=FJz5ZYHXv2EYMhNVZ1HdE9gJlLVWwx5DQdm0EHjF+Do=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cHWKgKVUGwXhp1/vCSOt4GSFTm6B1rO2ItshijNdyGDQqxt15mf5PJlBFvq6ckdRtksnwn+tULbj+qPA+AORCW5YweZb7DClmnqk7GrgYTRJAc+tmF/T6gxrUnpU7AJY67EDJgjhm9XWj+NjjzMEWBavs75AakAKxQt0me7IHBQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=HmcNA+f0; arc=none smtp.client-ip=209.85.167.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="HmcNA+f0" Received: by mail-lf1-f48.google.com with SMTP id 2adb3069b0e04-539f7606199so4931677e87.0 for ; Mon, 28 Oct 2024 09:45:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133955; x=1730738755; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=gdxmvRxcUCh2wU6MGMITBYWUXVm7PGpvZFKsGRUujMU=; b=HmcNA+f0Xt3f0YZDiNfs4Tmv5U2Pl3tAMc3XBymmalHD1/qZYGfumeyMEU7OoWL6tt xAhDjHK/4lanSZh4MHISKaX3ZwJfAu6FBo564pZ9IjtjhtFKtZjCp9nti0R4dz0xrORM yVQCs/jHf974BchkOSLllLAMR3IE/tNuVQk5Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133955; x=1730738755; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gdxmvRxcUCh2wU6MGMITBYWUXVm7PGpvZFKsGRUujMU=; b=bjVqaLvITPgbfhRNW/t9wa7KbmNJr80auHxkdfBZ3TveBq+KN7bCvgr/34KfVaeGbS +QF3zm5+8kkQ9CPE1hVzyRQWYBB1qXmZWeqDtYS2t4i4zVDRTD6FuM+iWMi9QRYZpZGZ e/QyMra96qGjWkPB9UISFJ0ajRugBrt/9aZQJcF+wvtmu6vKieMbAGI7Uht1u6c0GUnm SVlh8DACDuBsl0pymbWJrciUAXv4qozXBBcbwAZ9HbKrbI7DpJU0OSonNvVa50Ar1Q7b WSXSnSPbEVTq91UwVfTnkYR9T//1Ux5QbfFgHFHFjYnRd5ct4IL9KnfXxKSZlxlo7aY/ 4yAA== X-Gm-Message-State: AOJu0YxINebvZpgB0vjdKCqvkrHrT4ZiFRTmiLb8AfcYoOVhs5hkwDm6 fp9+gxG4JdfxxZJVhkWD4PaZCeZfoQyld0JJPbALtKCrSZZRF5+ts7MPz5p1HYw= X-Google-Smtp-Source: AGHT+IHZvNmhqv9/cwd4qrjDL/P6Dm2CW/EPcLmAMDoaHfhzcy/c184ISjVKoRMKNsJXTY4WqERHBw== X-Received: by 2002:a05:6512:32c6:b0:539:ee0a:4f8f with SMTP id 2adb3069b0e04-53b3491cd35mr3587690e87.44.1730133953804; Mon, 28 Oct 2024 09:45:53 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4317dde77a5sm137445455e9.1.2024.10.28.09.45.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:53 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:52 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 08/11] printk: Support setting initial console loglevel via console= on cmdline Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the console=3D kernel command line parameter to support specifying per-console loglevels at boot time. This is achieved by introducing a new loglevel option that can be passed as a loglevel option within a console=3D stanza. For example, this is an example of how one might configure netconsole devices to print messages with a higher priority than loglevel 3 (KERN_ERR) at startup: console=3Dnetcon0,loglevel:3 Signed-off-by: Chris Down --- .../admin-guide/kernel-parameters.txt | 24 +++-- Documentation/admin-guide/serial-console.rst | 37 +++++++- Documentation/networking/netconsole.rst | 6 +- kernel/printk/console_cmdline.h | 1 + kernel/printk/printk.c | 87 ++++++++++++++++++- 5 files changed, 140 insertions(+), 15 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index 1518343bbe22..d883a851fffc 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -799,13 +799,18 @@ ttyS[,options] ttyUSB0[,options] Use the specified serial port. The options are of - the form "bbbbpnf", where "bbbb" is the baud rate, - "p" is parity ("n", "o", or "e"), "n" is number of - bits, and "f" is flow control ("r" for RTS or - omit it). Default is "9600n8". + the form "bbbbpnf,extra", where "bbbb" is the baud + rate, "p" is parity ("n", "o", or "e"), "n" is + number of bits, and "f" is flow control ("r" for RTS + or omit it). Default is "9600n8". =20 - See Documentation/admin-guide/serial-console.rst for more - information. See + At present the only extra option is "loglevel" to + set the per-console loglevel. For example: + + console=3DttyS0,9600n8,loglevel:3 + + See Documentation/admin-guide/serial-console.rst for + more information. See Documentation/networking/netconsole.rst for an alternative. =20 @@ -3167,8 +3172,11 @@ loglevel=3D [KNL,EARLY] All Kernel Messages with a loglevel smaller than the console loglevel will be printed to the console. It can - also be changed with klogd or other programs. The - loglevels are defined as follows: + also be changed with klogd or other programs. Note that + this can be overridden per-console, see + Documentation/admin-guide/per-console-loglevel.rst. + + The loglevels are defined as follows: =20 0 (KERN_EMERG) system is unusable 1 (KERN_ALERT) action must be taken immediately diff --git a/Documentation/admin-guide/serial-console.rst b/Documentation/a= dmin-guide/serial-console.rst index a3dfc2c66e01..240f7a36379d 100644 --- a/Documentation/admin-guide/serial-console.rst +++ b/Documentation/admin-guide/serial-console.rst @@ -32,6 +32,33 @@ The format of this option is:: and F is flow control ('r' for RTS). Default is 9600n8. The maximum baudrate is 115200. =20 + One can also specify the per-console loglevel for this + console by providing a loglevel parameter, for example + "loglevel:4" to set this console's loglevel to 4. The + value provided can be from 0 (LOGLEVEL_EMERG) to 8 + (LOGLEVEL_DEBUG + 1), and messages below that will be + emitted onto the console as they become available. + +The available loglevels are defined thus: + ++---+--------------+-----------------------------------+ +| 0 | KERN_EMERG | system is unusable | ++---+--------------+-----------------------------------+ +| 1 | KERN_ALERT | action must be taken immediately | ++---+--------------+-----------------------------------+ +| 2 | KERN_CRIT | critical conditions | ++---+--------------+-----------------------------------+ +| 3 | KERN_ERR | error conditions | ++---+--------------+-----------------------------------+ +| 4 | KERN_WARNING | warning conditions | ++---+--------------+-----------------------------------+ +| 5 | KERN_NOTICE | normal but significant condition | ++---+--------------+-----------------------------------+ +| 6 | KERN_INFO | informational | ++---+--------------+-----------------------------------+ +| 7 | KERN_DEBUG | debug-level messages | ++---+--------------+-----------------------------------+ + You can specify multiple console=3D options on the kernel command line. =20 The behavior is well defined when each device type is mentioned only once. @@ -39,11 +66,14 @@ In this case, the output will appear on all requested c= onsoles. And the last device will be used when you open ``/dev/console``. So, for example:: =20 - console=3DttyS1,9600 console=3Dtty0 + console=3DttyS1,9600,loglevel:5 console=3Dtty0 =20 defines that opening ``/dev/console`` will get you the current foreground -virtual console, and kernel messages will appear on both the VGA -console and the 2nd serial port (ttyS1 or COM2) at 9600 baud. +virtual console, and kernel messages will appear on both the VGA console a= nd +the 2nd serial port (ttyS1 or COM2) at 9600 baud. The optional loglevel "5" +indicates that this console will emit messages more serious than +LOGLEVEL_NOTICE (that is, LOGLEVEL_WARNING and below, since more serious +messages have lower ordering). =20 The behavior is more complicated when the same device type is defined more times. In this case, there are the following two rules: @@ -143,3 +173,4 @@ Replace the sample values as needed. the integration of these patches into m68k, ppc and alpha. =20 Miquel van Smoorenburg , 11-Jun-2000 +Chris Down , 23-October-2024 diff --git a/Documentation/networking/netconsole.rst b/Documentation/networ= king/netconsole.rst index 34419e6fe106..a7c7519fe2d0 100644 --- a/Documentation/networking/netconsole.rst +++ b/Documentation/networking/netconsole.rst @@ -72,7 +72,11 @@ Built-in netconsole starts immediately after the TCP sta= ck is initialized and attempts to bring up the supplied dev at the supplied address. =20 -You can also set a loglevel at runtime:: +You can also set a loglevel at boot time on the kernel command line:: + + console=3Dnetcon0,loglevel:2 + +This can also be changed at runtime:: =20 $ ls -l /sys/class/console/netcon0/ total 0 diff --git a/kernel/printk/console_cmdline.h b/kernel/printk/console_cmdlin= e.h index 0ab573b6d4dc..cb3b99d31d80 100644 --- a/kernel/printk/console_cmdline.h +++ b/kernel/printk/console_cmdline.h @@ -7,6 +7,7 @@ struct console_cmdline char name[16]; /* Name of the driver */ int index; /* Minor dev. to use */ char devname[32]; /* DEVNAME:0.0 style device name */ + int level; /* Log level to use */ bool user_specified; /* Specified by command line vs. platform */ char *options; /* Options for the driver */ #ifdef CONFIG_A11Y_BRAILLE_CONSOLE diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index bbd037b84a0d..c47dda23a7d6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2597,12 +2597,84 @@ static void set_user_specified(struct console_cmdli= ne *c, bool user_specified) console_set_on_cmdline =3D 1; } =20 +static bool find_and_remove_console_option(char *options, const char *key, + char *val_buf, size_t val_buf_size) +{ + bool found =3D false, first =3D true; + char *option, *next =3D options; + + while ((option =3D strsep(&next, ","))) { + char *value; + + value =3D strchr(option, ':'); + if (value) + *(value++) =3D '\0'; + + if (strcmp(option, key) =3D=3D 0) { + found =3D true; + if (value) { + if (strlen(value) >=3D val_buf_size) { + pr_warn("Can't copy console option value for %s:%s: not enough space = (%zu)\n", + option, value, val_buf_size); + found =3D false; + } else { + strscpy(val_buf, value, val_buf_size); + } + } else + *val_buf =3D '\0'; + } + + if (found) + break; + + if (next) + *(next - 1) =3D ','; + if (value) + *(value - 1) =3D ':'; + + first =3D false; + } + + if (found) { + if (next) + memmove(option, next, strlen(next) + 1); + else if (first) + *option =3D '\0'; + else + *--option =3D '\0'; + } + + return found; +} + +static int find_and_remove_loglevel_option(char *options) +{ + char val[16]; + int loglevel; + + if (!find_and_remove_console_option(options, "loglevel", val, + sizeof(val))) + return -ENOENT; + + if (kstrtoint(val, 10, &loglevel)) { + pr_warn("Invalid console loglevel, ignoring: %s\n", val); + return -EINVAL; + } + + if (clamp_loglevel(loglevel) !=3D loglevel) { + pr_warn("Per-console loglevel out of range, ignoring: %d\n", loglevel); + return -ERANGE; + } + + return loglevel; +} + static int __add_preferred_console(const char *name, const short idx, const char *devname, char *options, char *brl_options, bool user_specified) { struct console_cmdline *c; - int i; + int i, ret; =20 if (!name && !devname) return -EINVAL; @@ -2639,6 +2711,13 @@ static int __add_preferred_console(const char *name,= const short idx, strscpy(c->name, name); if (devname) strscpy(c->devname, devname); + + ret =3D find_and_remove_loglevel_option(options); + if (ret >=3D 0) + c->level =3D ret; + else + c->level =3D -1; + c->options =3D options; set_user_specified(c, user_specified); braille_set_options(c, brl_options); @@ -3903,8 +3982,10 @@ static int try_enable_preferred_console(struct conso= le *newcon, if (newcon->index < 0) newcon->index =3D c->index; =20 - // TODO: Will be configurable in a later patch - newcon->level =3D -1; + if (c->level > 0) + newcon->level =3D c->level; + else + newcon->level =3D -1; =20 newcon->classdev =3D NULL; =20 --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 539E11DF273 for ; Mon, 28 Oct 2024 16:45:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133961; cv=none; b=CFYshUVlq4PIKd+X+AZUbu1nSItVT/HH7eLR401hDZwemxMlcFoiJqFZRQvU9fScCu11mKMGvokunwKThGR4qGudAmFOw/zDYo96YHZfWkaavrBfkYCVgMe3JQjfDDIyIiAccbeZzYsfAw2q8EBgGrE/OnmUY66MJwKWG0Zdcpo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133961; c=relaxed/simple; bh=hCTeBcjd7iD4npJLTCTrUA2Zs2OYGmA4QQyHBaJpgqc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=U4fJYIBddkfajPxgm6UO3ml2Agc5C1t38qsQihwpppQDlWEFdgWC7SDVzU+rcNWMCkpQlnd5F4XdTxEYaZ64DQU0dw6W12ox/zC5HM+k8eLgoaFid097dmxmPAiYL17pfcLVhz+tJ7t4enI9Fnq6IN3gQS0OFPWgNI1KkJ4Inpw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=WSyBEQs+; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="WSyBEQs+" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-4314c452180so38207735e9.0 for ; Mon, 28 Oct 2024 09:45:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133958; x=1730738758; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=BbVhQ674tMTDDUxWK+JYxDMM69XMs+UNwDYGU3cpCJM=; b=WSyBEQs+kjw9DlYpKgKFY2eHNPcLlB9Qx0fpey3hPiPezt306Nky+irV1u2zoePL+C Vo6Wij9G/S1odxVgMnwwzCMr2ccpqfRCdfQPbCmw4BmsHRJ2LzoesfFCP1gleG0Cb6R8 5BJdPvaEpy3NLj5bLJzjrvyFqQHtuAY4b1hvU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133958; x=1730738758; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BbVhQ674tMTDDUxWK+JYxDMM69XMs+UNwDYGU3cpCJM=; b=eLaQzx91qsC9R/mKKINtk4wm5KmY59FIbOxishzc1KSCfhic9kua6TrX+kiIWLrDXa eNW4AKFrrK0ry0FxjCC09d3XSi4X/75ZuaF1+l7nydzIiGVhhL5LRGPltONdquz/o2Th gmU/k/lhW+1tA9Gub9/mQxf/+jQ4HrIIztLyfpErjzlWjYPWtYLUjLYvwA2P3hiCfiSL 7UTILVk3L8fOOl9P9FhnDXqTkEq1+egN034knFAz59R3f1yNm0tuGYq586eONdMa9+EO l7NqgyaXgxJR/UPYNR9ALOse6FuXFAnseyTxi9m/rqA21ai7F4ZjzFszB22QUK3Dre2x nEqg== X-Gm-Message-State: AOJu0Ywm3/btsyi52b6qyOTO42TfoaqGvCfUwVNSmiyEp1iv7tY82A44 6SGRTvfoaVYio7axYMx0YtuFqAglG6XNgXPSpB0M7bvlkPO18t8DPrnIcEAewXc= X-Google-Smtp-Source: AGHT+IHJvu2pohYpsdBuR+YDwUK3Ikz9/BrbQZ/b8XBcpBP4aAwoWkcwtdALE8PVbKnEIQCZki9wwA== X-Received: by 2002:a05:600c:5490:b0:42c:b991:98bc with SMTP id 5b1f17b1804b1-431b2def197mr2610965e9.0.1730133957668; Mon, 28 Oct 2024 09:45:57 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4318b56ef06sm146260435e9.30.2024.10.28.09.45.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:45:57 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:55 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 09/11] printk: Add sysctl interface to set global loglevels Message-ID: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce two new sysctl interfaces for configuring global loglevels: - kernel.console_loglevel: Sets the global console loglevel, determining the minimum priority of messages printed to consoles. Messages with a loglevel lower than this value will be printed. - kernel.default_message_loglevel: Sets the default loglevel for messages that do not specify an explicit loglevel. The kernel.printk sysctl was previously used to set multiple loglevel parameters simultaneously, but it was confusing and lacked proper validation. By introducing these dedicated sysctl interfaces, we provide a clearer and more granular way to configure the loglevels. Signed-off-by: Chris Down --- Documentation/admin-guide/sysctl/kernel.rst | 17 ++++++++- kernel/printk/sysctl.c | 42 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/ad= min-guide/sysctl/kernel.rst index f8bc1630eba0..8019779b27f6 100644 --- a/Documentation/admin-guide/sysctl/kernel.rst +++ b/Documentation/admin-guide/sysctl/kernel.rst @@ -1044,6 +1044,20 @@ otherwise the 'doze' mode will be used. =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 +Some of these settings may be overridden per-console, see +Documentation/admin-guide/per-console-loglevel.rst. See ``man 2 syslog`` f= or +more information on the different loglevels. + +console_loglevel +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Messages with a higher priority than this will be printed to consoles. + +default_message_loglevel +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Messages without an explicit priority will be printed with this priority. + printk =3D=3D=3D=3D=3D=3D =20 @@ -1052,8 +1066,7 @@ The four values in printk denote: ``console_loglevel`= `, ``default_console_loglevel`` respectively. =20 These values influence printk() behavior when printing or -logging error messages. See '``man 2 syslog``' for more info on -the different loglevels. +logging error messages. =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D console_loglevel messages with a higher priority than diff --git a/kernel/printk/sysctl.c b/kernel/printk/sysctl.c index f5072dc85f7a..3bce8b89dacc 100644 --- a/kernel/printk/sysctl.c +++ b/kernel/printk/sysctl.c @@ -11,6 +11,9 @@ =20 static const int ten_thousand =3D 10000; =20 +static int min_msg_loglevel =3D LOGLEVEL_EMERG; +static int max_msg_loglevel =3D LOGLEVEL_DEBUG; + static int proc_dointvec_minmax_sysadmin(const struct ctl_table *table, in= t write, void *buffer, size_t *lenp, loff_t *ppos) { @@ -20,6 +23,29 @@ static int proc_dointvec_minmax_sysadmin(const struct ct= l_table *table, int writ return proc_dointvec_minmax(table, write, buffer, lenp, ppos); } =20 +static int printk_console_loglevel(const struct ctl_table *table, int writ= e, + void *buffer, size_t *lenp, loff_t *ppos) +{ + struct ctl_table ltable =3D *table; + int ret, level; + + if (!write) + return proc_dointvec(table, write, buffer, lenp, ppos); + + ltable.data =3D &level; + + ret =3D proc_dointvec(<able, write, buffer, lenp, ppos); + if (ret) + return ret; + + if (level !=3D -1 && level !=3D clamp_loglevel(level)) + return -ERANGE; + + console_loglevel =3D level; + + return 0; +} + static struct ctl_table printk_sysctls[] =3D { { .procname =3D "printk", @@ -76,6 +102,22 @@ static struct ctl_table printk_sysctls[] =3D { .extra1 =3D SYSCTL_ZERO, .extra2 =3D SYSCTL_TWO, }, + { + .procname =3D "console_loglevel", + .data =3D &console_loglevel, + .maxlen =3D sizeof(int), + .mode =3D 0644, + .proc_handler =3D printk_console_loglevel, + }, + { + .procname =3D "default_message_loglevel", + .data =3D &default_message_loglevel, + .maxlen =3D sizeof(int), + .mode =3D 0644, + .proc_handler =3D proc_dointvec_minmax, + .extra1 =3D &min_msg_loglevel, + .extra2 =3D &max_msg_loglevel, + }, }; =20 void __init printk_sysctl_init(void) --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 310961DFE1E for ; Mon, 28 Oct 2024 16:46:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133966; cv=none; b=slbJFr/P1yK2jGxLRGXgGX2pWukZI+GGZ6CYQmQ5ikMJGJW5Ylyc8E8dnKXFbSZMlhP6aPRAyo365TnxBLEYaBcRpB64dCnjNGhkoWSwr8lCAOxj+HXLsWcUYNSoE6teuILznos3UqHY7QQkx/nwqYbqXWWeABr0tPtOahUo600= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133966; c=relaxed/simple; bh=sI52s5i9k4pYNrQHBS72EU98OIueE15hSE4KEthCY5Y=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=qGb41gKVFC5Fv3DFh9qGDOtw8INuok4WjW+w/7pbqc2dw5EMBBJoAnpPCWCn2jDD3tzY7GKQGw4770G5uP6/KFEnpm+CVpXefwUoU80mf9Tk4bOoXOLmZEl0tEBsN4Pkw6UTyZcWeBLfYyuWUBixKnir9iDeSIVksHwr0c8ac+Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=Yy/MUa9l; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="Yy/MUa9l" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-431ac30d379so9160745e9.1 for ; Mon, 28 Oct 2024 09:46:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133960; x=1730738760; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=NbATi4LQJSIUmiSf9Q8b/uDqq/jBD5xLfukP+H/32dM=; b=Yy/MUa9l2q67eBZtZoYV0tINKz0i/IlLmYxLOGmiK4a+F2budkEVrzG4skhk8MpVqr ScMrw8XKmBfcylZpPbk8JaQuXMElpCcbfNHjchLe64PoxfGM7loU8NZ7qiukRH/Wr8ZI iK+hKFmHFjkGaESjIaUf13tCUG6FGx+uuAkCM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133960; x=1730738760; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NbATi4LQJSIUmiSf9Q8b/uDqq/jBD5xLfukP+H/32dM=; b=Q9AqS0Ws8246t+RD1M7f+OEQuGb2mKyH9jvhZ5llh4/x3lGVQwcQYDR3YnC4Tw9ip7 pC9Lse5YPO/zKl2u1+aMCHBLnzG/xVu7nytqY9QzRocNdYPDI32xkMItb3T2nemm90PX HID/JvaiX9p0zKvLLMKqA8dE2/7PSQA/ci4+l10gCC5RZLiguTG+M8cTz/Ea/g2wYvTy ZeqlY9cojfogbbv9WPWcGz6YyjoLPlaiLEznndnaB4fIkjzvlGzhbLT5x8wjldvnWnOz trFyuat7/PD2gdrxZFdailrBQwzJoJATYqPFrHVwRGECL9HJuLQnmRtoiuUonr7O3Rw/ Jhvg== X-Gm-Message-State: AOJu0YxspVcIUInjBjkceY1gk4VsDACdTqpM0/PysTiwF5e8w2BhtBXQ haGW9zgWgHVjiIujD0MQp7/W9V4EwJxR6ILqN4lpoJ557BtdH3bQ1g42us7FMHA= X-Google-Smtp-Source: AGHT+IEvvpnLREHjelMIBqHzAmiYJ5zM0d1BpPVE3JPzD4+dRIX/DKHK7f23k+8pfDQQZhz5g38RgQ== X-Received: by 2002:a05:600c:4e8b:b0:431:40ca:ce44 with SMTP id 5b1f17b1804b1-4319ad34a44mr68787325e9.30.1730133960493; Mon, 28 Oct 2024 09:46:00 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38058b929a8sm10083304f8f.103.2024.10.28.09.45.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:46:00 -0700 (PDT) Date: Mon, 28 Oct 2024 16:45:59 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 10/11] printk: Deprecate the kernel.printk sysctl interface Message-ID: <993d978d4a59dd370ac39c6c24c9b72c11f4dc74.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The kernel.printk sysctl interface is deprecated in favour of more granular and clearer sysctl interfaces for controlling loglevels, namely kernel.console_loglevel and kernel.default_message_loglevel. kernel.printk has a number of fairly significant usability problems: - It takes four values in a specific order, which is not intuitive. Speaking from personal experience, even people working on printk sometimes need to look up the order of these values, which doesn't suggest much in the way of perspicuity. - There is no validation on the input values, so users can set them to invalid or nonsensical values without receiving any errors or warnings. This can lead to unpredictable behaviour. - One of the four values, default_console_loglevel, is not used in the kernel (see below), making it redundant and potentially confusing. - Overall, the interface is complex, hard to understand, and combines multiple controls into a single sysctl, which is not ideal. It should be separated into distinct controls for clarity. Setting kernel.printk now calls printk_sysctl_deprecated, which emits a pr_warn. The warning informs users about the deprecation and suggests using the new sysctl interfaces instead. By deprecating kernel.printk, we aim to: - Encourage users to adopt the new, dedicated sysctl interfaces (kernel.console_loglevel and kernel.default_message_loglevel), which are more straightforward and easier to understand. - Improve input validation and error handling, ensuring that users receive appropriate feedback when setting invalid values. - Simplify the configuration of loglevels by exposing only the controls that are necessary and relevant. Signed-off-by: Chris Down Reviewed-by: Petr Mladek --- Documentation/admin-guide/sysctl/kernel.rst | 3 +++ kernel/printk/sysctl.c | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/ad= min-guide/sysctl/kernel.rst index 8019779b27f6..6027f0057ea3 100644 --- a/Documentation/admin-guide/sysctl/kernel.rst +++ b/Documentation/admin-guide/sysctl/kernel.rst @@ -1061,6 +1061,9 @@ Messages without an explicit priority will be printed= with this priority. printk =3D=3D=3D=3D=3D=3D =20 +This sysctl is deprecated and will be removed in future. Please consider u= sing +``kernel.console_loglevel`` or ``kernel.default_message_loglevel`` instead. + The four values in printk denote: ``console_loglevel``, ``default_message_loglevel``, ``minimum_console_loglevel`` and ``default_console_loglevel`` respectively. diff --git a/kernel/printk/sysctl.c b/kernel/printk/sysctl.c index 3bce8b89dacc..42f9e94b1e47 100644 --- a/kernel/printk/sysctl.c +++ b/kernel/printk/sysctl.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "internal.h" =20 static const int ten_thousand =3D 10000; @@ -46,13 +47,24 @@ static int printk_console_loglevel(const struct ctl_tab= le *table, int write, return 0; } =20 +static int printk_sysctl_deprecated(const struct ctl_table *table, int wri= te, + void *buffer, size_t *lenp, loff_t *ppos) +{ + int res =3D proc_dointvec(table, write, buffer, lenp, ppos); + + if (write) + pr_warn_once("printk: The kernel.printk sysctl is deprecated. Consider u= sing kernel.console_loglevel or kernel.default_message_loglevel instead.\n"= ); + + return res; +} + static struct ctl_table printk_sysctls[] =3D { { .procname =3D "printk", .data =3D &console_loglevel, .maxlen =3D 4*sizeof(int), .mode =3D 0644, - .proc_handler =3D proc_dointvec, + .proc_handler =3D printk_sysctl_deprecated, }, { .procname =3D "printk_ratelimit", --=20 2.46.0 From nobody Mon Nov 25 09:34:08 2024 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 A0D941DFE3E for ; Mon, 28 Oct 2024 16:46:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133966; cv=none; b=mKP9T0n6jbyPUE1bf6IqOt1P60DfFcAW3AzU9MeKoOjqe/U0021P5Vfdmgyuc2WR7m6VPKaSjk/vmM/3XyR6mizNCcjlGEui7vKayR23Jo1udsHH3POVfO7WjuRnUgpKQm6gP1xPO8dpyAbUZvnxSglxXjv11v/vR2cCoXpAwfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730133966; c=relaxed/simple; bh=MIAjzwi4cO1uwoYcl3/hokrXBrQxGA0D5qMxMRZRFkQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ToGKcWLsOS7fj5AemEOxzQCf3aiMOJbOSd8gOvAm+hnDeSC/vDllTkIAutgXhx1KxRX4PFpMwAP+nIRohcMXaEmBSw0x267bT13zOrFk4szeSYitQ9+Mf88Q6goGgFLX2+9i0gIO9syaIPZ1QXzHAjFuFrscsOBO8lU23Bv2xA4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b=fQM7gfsH; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.b="fQM7gfsH" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4314c452180so38208345e9.0 for ; Mon, 28 Oct 2024 09:46:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1730133963; x=1730738763; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=Pe3DceTPULKpPQyU6tk9aCv8FZUAKvYc33ASTgoizb4=; b=fQM7gfsH1G4Y8Eht/2eECq5hKnV0RuaD7i1H7O4aZMJF9bUOqXGFbjHMEtmaNTwE5l 3xnla8wY29XpQZtHnDa2lIEK5d7RXSKGam+RxizgDeZGZX9kJ7Ae7QYRmE6tumJAYgFQ cqwDTh0jUE0MCdyTnsNu6VVkDfgOgyTIr9bi0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730133963; x=1730738763; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pe3DceTPULKpPQyU6tk9aCv8FZUAKvYc33ASTgoizb4=; b=SVZ0JlRIFWCHwPHm6CpN74OUisRp5IXSpS2QHzVB+1V1CKx0n0LcI3MAkYxA6127z7 5bIEL2uAbvprUeVPRZMD2G5S3Prpyfm0JPq+DatHRFtdsAJm6wbFVjBPIu9dWnFHpEOA xM5jaoiXbqhDP8BRYPbPbquSAaVkbp79we2cRGCBKRj1BWGE226IwxXGTtFkMx6Qq913 Sll27nVAFk7leA9v10pcd0G2uULa+c5dnKIp8ie7fNWBCxpA/YzDOlOtL2QlW4ovlZim xoVFNhnF3GEjpnbMrdqQ6GePlQckpJyP4f/HTcOjDiJldLpd+ldte1K4SNoD+RfnTFHt t6GA== X-Gm-Message-State: AOJu0Yx/2TEOEFGH4qcaAOUJwILgJ2808+I7nV8IbPvFbEBW4kcRw7MO jLfZS/5WGw136JTHifpObpiSGotpTQpC8MtMVitmbQUoUcfPQnMIhrrovozxMb4= X-Google-Smtp-Source: AGHT+IE0HPKAnm5+enrIloZNvuHS6g3fAvcHJ7RVCpchNz8MLfx2eafTU9darqIcPaMWZv2UPbwrdQ== X-Received: by 2002:a5d:44cc:0:b0:37d:4660:c027 with SMTP id ffacd0b85a97d-3808142da47mr192382f8f.24.1730133962866; Mon, 28 Oct 2024 09:46:02 -0700 (PDT) Received: from localhost ([93.115.193.42]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43193594fe7sm114820565e9.14.2024.10.28.09.46.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 09:46:02 -0700 (PDT) Date: Mon, 28 Oct 2024 16:46:01 +0000 From: Chris Down To: Petr Mladek Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sergey Senozhatsky , Steven Rostedt , John Ogness , Geert Uytterhoeven , Tony Lindgren , kernel-team@fb.com Subject: [PATCH v6 11/11] printk: Purge default_console_loglevel Message-ID: <3326b5ea5d95bd580c6e4dea9f5703d83e3f913a.1730133890.git.chris@chrisdown.name> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.13 (00d56288) (2024-03-09) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" default_console_loglevel (then DEFAULT_LOGLEVEL) was created in Linux 0.99.13 (September 1993) to power what we now call SYSLOG_ACTION_CONSOLE_{ON,OFF}. It was originally hardcoded to 7. In Linux 2.3.12 (March 1997), Chris Horn made it configurable by sysctl through kernel.printk. Its demise came about in July 2009 in commit 1aaad49e856c ("printk: Restore previous console_loglevel when re-enabling logging"), which replaces default_console_loglevel with the previously used console_loglevel. However, the documentation was never updated, and we still kept on telling users that this sets the default value for console_loglevel, even though this hasn't been true for over 15 years. Purge both the documentation and all remaining references to default_console_loglevel. It will still be initialised in the sysctl array. Signed-off-by: Chris Down Reviewed-by: Petr Mladek --- Documentation/admin-guide/sysctl/kernel.rst | 5 ++--- include/linux/printk.h | 1 - kernel/printk/printk.c | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/ad= min-guide/sysctl/kernel.rst index 6027f0057ea3..e1b5124ab383 100644 --- a/Documentation/admin-guide/sysctl/kernel.rst +++ b/Documentation/admin-guide/sysctl/kernel.rst @@ -1065,8 +1065,8 @@ This sysctl is deprecated and will be removed in futu= re. Please consider using ``kernel.console_loglevel`` or ``kernel.default_message_loglevel`` instead. =20 The four values in printk denote: ``console_loglevel``, -``default_message_loglevel``, ``minimum_console_loglevel`` and -``default_console_loglevel`` respectively. +``default_message_loglevel``, ``minimum_console_loglevel`` and an unused l= egacy +value respectively. =20 These values influence printk() behavior when printing or logging error messages. @@ -1078,7 +1078,6 @@ default_message_loglevel messages without an explicit= priority will be printed with this priority minimum_console_loglevel minimum (highest) value to which console_loglevel can be set -default_console_loglevel default value for console_loglevel =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 =20 diff --git a/include/linux/printk.h b/include/linux/printk.h index b7e8411e033d..41ce323b4e77 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -71,7 +71,6 @@ extern int console_printk[]; #define console_loglevel (console_printk[0]) #define default_message_loglevel (console_printk[1]) #define minimum_console_loglevel (console_printk[2]) -#define default_console_loglevel (console_printk[3]) =20 extern bool ignore_per_console_loglevel; =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index c47dda23a7d6..018087b57031 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -65,7 +65,7 @@ int console_printk[4] =3D { CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */ MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */ CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */ - CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */ + /* legacy default console loglevel, unused */ }; EXPORT_SYMBOL_GPL(console_printk); =20 --=20 2.46.0