From patchwork Mon Jan 9 10:07:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40772 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2071264wrt; Mon, 9 Jan 2023 02:11:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXts2uXXdBZu3lx1ewJ32JXvIyxS//Ir8GTCfLXFd1HhcJC50jUWrBsP5mnrCHpuy6xUNCzZ X-Received: by 2002:a17:906:a047:b0:7a9:fc17:eb4c with SMTP id bg7-20020a170906a04700b007a9fc17eb4cmr51277984ejb.40.1673259106113; Mon, 09 Jan 2023 02:11:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259106; cv=none; d=google.com; s=arc-20160816; b=p2Qu5zq5Tw/wbEhzHSQHWOmS2jUxxVONE2NmDbbz9ZS/FEKuz9YhToA4CdU7pJREsC AeuDg6Mwqm/sqjXqrM/VbjtUxNIxqXxkS++w9/Cz/IVeY8nkal38I3IPWZ3FsDw+a7pL DlbdT3hJOyUBePCDotAPYyoZx9jFE+GnsKDjKh0HYxlLA/+4R20qSSEJhSwCwk+/VHXW ejKMZkNXrU563kN5MGjgBTSN/FGrW072UzsboN92uj9jq4JqFzRuZEYjSPa1bBETPPVl HIm0EuB0AlDutFf5K/uc8XdnT8PO/LJuRiX9BMU3SLnTeeVla9APxDsw9p8G2kTsSE15 pavQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=bX02r2I3Rh7nmjgfNifsLAV7ya3446uEg2LFZ7tsWzo=; b=Hj+6ii2NATn6B+v9+t7DKUK3nFvJQYQeTRRfKE9tEyyqMzgn/J6h2CQJH0kPd4nHnb 4uHEH2KwjFU3myap+Q8e+/RMgHO+j01Af2I/8SSL5sU4FkZbnBqmlz4VuUW4KhcEiGJW xAIT1bQHJlbyeo086qgdsA02n7geX1wH0jEIaKjWR5pzEuIhGX3YgxpWYP9AJTcr6+4q ObxdU3gP9pPb3rEXpE2WrQQ88jFPXuyaWVQApptykNyWw2tPJFXAR5NLaOeCBhJLPRxx snzuM2BheKicie36zdCkVPOuIQ37ralcM+mVTUTWJVCLE5utxEdC77R4iSFqX1beDbwR XWUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hT4bV9gm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wv4-20020a170907080400b0084ca5f2b837si8737203ejb.676.2023.01.09.02.11.22; Mon, 09 Jan 2023 02:11:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hT4bV9gm; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234306AbjAIKJS (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236865AbjAIKIs (ORCPT ); Mon, 9 Jan 2023 05:08:48 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A9E310B73 for ; Mon, 9 Jan 2023 02:08:46 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bX02r2I3Rh7nmjgfNifsLAV7ya3446uEg2LFZ7tsWzo=; b=hT4bV9gmwVYUTgBQHgR3eUlb7nWTyaC7+b3/wB0SZ3LdYzCPC0VJjeVPkrCuZVaNX8igF4 0S2EO5xWTixW0lcWpnwDa7PaevzIDjzalqsHjipUMU1v99o4Gox/H3Ynvjg1T41i7LXWbX uCwSmVMY7FmTVV0WfpKSQpCmqSgocl3p/zQopAKY8ZA+kyYc/34Eu1niXyo8n60JiCoNAS Vji2rY8UQnjz6VqrqzUvs3juHRv1TjXE78jVHTYxxPgfuDx+gA9kcUzqAoTifLUPBweNA/ QCrgkhHldjNuPKZqcd0k/eTv+vtRoKuYzmOtsHXYGFZwh1alatNDzU/2X8Nxaw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bX02r2I3Rh7nmjgfNifsLAV7ya3446uEg2LFZ7tsWzo=; b=BqxKVCwzy74Cj0XV5ocO47XuNtxxqiK20w+/ek4KfmTraO0d9Gr/1JjZAMbWhVWputtY5m A1L/JD4Wwh5vEPBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 1/8] printk: move size limit macros into internal.h Date: Mon, 9 Jan 2023 11:13:53 +0106 Message-Id: <20230109100800.1085541-2-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539340222679684?= X-GMAIL-MSGID: =?utf-8?q?1754539340222679684?= The size limit macros are located further down in printk.c and behind ifdef conditionals. This complicates their usage for upcoming changes. Move the macros into internal.h so that they are still invisible outside of printk, but easily accessible for printk. Also, the maximum size of formatted extended messages does not need to be known by any code outside of printk, so move it to internal.h as well. And like CONSOLE_LOG_MAX, for !CONFIG_PRINTK set CONSOLE_EXT_LOG_MAX to 0 to reduce the static memory footprint. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 2 -- kernel/printk/internal.h | 24 ++++++++++++++++++++++++ kernel/printk/printk.c | 17 ----------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 8c81806c2e99..8ef499ab3c1e 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -44,8 +44,6 @@ static inline const char *printk_skip_headers(const char *buffer) return buffer; } -#define CONSOLE_EXT_LOG_MAX 8192 - /* printk's without a loglevel use this.. */ #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index d947ca6c84f9..f394332410c9 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -14,6 +14,24 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, #ifdef CONFIG_PRINTK +#ifdef CONFIG_PRINTK_CALLER +#define PREFIX_MAX 48 +#else +#define PREFIX_MAX 32 +#endif + +/* the maximum size of a formatted record (i.e. with prefix added per line) */ +#define CONSOLE_LOG_MAX 1024 + +/* the maximum size of a formatted extended record */ +#define CONSOLE_EXT_LOG_MAX 8192 + +/* the maximum size for a dropped text message */ +#define DROPPED_TEXT_MAX 64 + +/* the maximum size allowed to be reserved for a record */ +#define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) + /* Flags for a single printk record. */ enum printk_info_flags { LOG_NEWLINE = 2, /* text ended with a newline */ @@ -48,6 +66,12 @@ u16 printk_parse_prefix(const char *text, int *level, enum printk_info_flags *flags); #else +#define PREFIX_MAX 0 +#define CONSOLE_LOG_MAX 0 +#define CONSOLE_EXT_LOG_MAX 0 +#define DROPPED_TEXT_MAX 0 +#define LOG_LINE_MAX 0 + /* * In !PRINTK builds we still export console_sem * semaphore and some of console functions (console_unlock()/etc.), so diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 7decf1e9c486..55bb065de65f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -465,21 +465,6 @@ static struct latched_seq clear_seq = { .val[1] = 0, }; -#ifdef CONFIG_PRINTK_CALLER -#define PREFIX_MAX 48 -#else -#define PREFIX_MAX 32 -#endif - -/* the maximum size of a formatted record (i.e. with prefix added per line) */ -#define CONSOLE_LOG_MAX 1024 - -/* the maximum size for a dropped text message */ -#define DROPPED_TEXT_MAX 64 - -/* the maximum size allowed to be reserved for a record */ -#define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) - #define LOG_LEVEL(v) ((v) & 0x07) #define LOG_FACILITY(v) ((v) >> 3 & 0xff) @@ -2387,8 +2372,6 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre #else /* CONFIG_PRINTK */ -#define CONSOLE_LOG_MAX 0 -#define DROPPED_TEXT_MAX 0 #define printk_time false #define prb_read_valid(rb, seq, r) false From patchwork Mon Jan 9 10:07:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40773 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2071398wrt; Mon, 9 Jan 2023 02:12:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXvPRLCossHR75lslZeVDukwwc+N6kUPpvernNHfXr+WL3XLWwFVavYmZW3tNunRqSEX3jFp X-Received: by 2002:a17:907:c242:b0:7c4:fc02:46a3 with SMTP id tj2-20020a170907c24200b007c4fc0246a3mr63876294ejc.30.1673259130824; Mon, 09 Jan 2023 02:12:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259130; cv=none; d=google.com; s=arc-20160816; b=OgS2vn94ToNfM50SkeslSESUIOtqvP1S2ZF7X1V0I2N/s6DzVtSt6M+OvSo4jI7mLH XwouIHZua4EN8KikTULYpW6kLw37EzpHtiZ9pzvZGyb1dQ7z14Zyq55Wqxy85xlaUK6p 9jhylE4BOUYvc6kLjMFLy2IGtnW2yPQ76DSoZ/EW2QV6G1sHKxQtIW2MhLZnbWMoO9Vi qAIEMwcpElp5kFOjewoQimNPb1gpmdf2xHCx77DJqsMM6z4tzd/Oo7QZZrwynK+brkUi MzBtOMHsBSbP0cEssN304VkI+nAqS5Alx7GrdJGaD09M/Jga/tSAYANosT1hNYWwp+GX 1NoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=XbYaurIRdFG+/mjMZ4Nnsd0tE8OMeEIDH+uE2KSAjBQ=; b=KrLQ368VVOykFXWvUfMzdj8pn+rqlT0twxD/XUV4zYZ7Wqo6rEpk3sgEzFgRaW/L8T VP7H0imSSGIGYQ2XgYV6kn5MsgPlZpKYaTl7URGYRfCQJ1WJONFdn/zSW9rAvEJZJr8q OsNUgRGwmyBDwRdruouaHi9bUGmDQjcyPfV60ihRwJ/CYSXjSxYmfhEK97id5NveHoYH LVD1CXmJmhQXKPjGIiVsC2Q0AmlRGwurpcedgSz0P9TjS8mNLI18lWknjHRJV06csOmT P5DrIH0PwT/CqjRW4YOGN/RC+gsy7DdJ11bMYGscFoAOgRO20f7jDBy8mLosIeNQMSfP skAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=nk9xoyaK; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gs6-20020a1709072d0600b007c170f6b32bsi9353628ejc.266.2023.01.09.02.11.47; Mon, 09 Jan 2023 02:12:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=nk9xoyaK; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234182AbjAIKJR (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236851AbjAIKIr (ORCPT ); Mon, 9 Jan 2023 05:08:47 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65A38EE29 for ; Mon, 9 Jan 2023 02:08:46 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XbYaurIRdFG+/mjMZ4Nnsd0tE8OMeEIDH+uE2KSAjBQ=; b=nk9xoyaKfHsP6LTQQVu+XG/CdducdR0r431heSBnhyIjJ9fMkZy9Qu8hytCRS5+JkkAwwm 1/pgMyVjDASfi1Ffscempghhi0+4iIlt942q2Ciaxu55L7LfBY7VUgwgnyeXP/8BzAK9LG wjaEV6zuiCuIwD71wAd1KBSNcrgVP40vXRCjksZ3cKAYVKjwNtVZrNXylP8l9qtetjEe14 ljkZ4gHS3f3n/2rA5Zq4QrNr3HYHE7oINFg5DlnvAgZEFVGA5Kj15wyz+e95bqS8OoW0Cv IKDAP5RfLDX0UpuEagGIVL9DZhbqoF3AXxieMVPAkieU26GIgFBu95UyKt+/lg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XbYaurIRdFG+/mjMZ4Nnsd0tE8OMeEIDH+uE2KSAjBQ=; b=uVEKsQaLbANPNkuvRmKPiltiGgodI/HUGUzLg17l8c1kxkoNEhqWHr6m3N1eKsTqL9vJDb 7KMyftFzRJpgNoCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 2/8] console: Use BIT() macros for @flags values Date: Mon, 9 Jan 2023 11:13:54 +0106 Message-Id: <20230109100800.1085541-3-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539365920510595?= X-GMAIL-MSGID: =?utf-8?q?1754539365920510595?= From: Thomas Gleixner Rather than manually calculating powers of 2, use the BIT() macros. Also take this opportunatity to cleanup and restructure the value comments into proper kerneldoc comments. Signed-off-by: Thomas Gleixner Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 46 ++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 9cea254b34b8..ed804dd7c2e8 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -15,6 +15,7 @@ #define _LINUX_CONSOLE_H_ 1 #include +#include #include #include @@ -125,18 +126,43 @@ static inline int con_debug_leave(void) /* * The interface for a console, or any other device that wants to capture * console messages (printer driver?) - * - * If a console driver is marked CON_BOOT then it will be auto-unregistered - * when the first real console is registered. This is for early-printk drivers. */ -#define CON_PRINTBUFFER (1) -#define CON_CONSDEV (2) /* Preferred console, /dev/console */ -#define CON_ENABLED (4) -#define CON_BOOT (8) -#define CON_ANYTIME (16) /* Safe to call when cpu is offline */ -#define CON_BRL (32) /* Used for a braille device */ -#define CON_EXTENDED (64) /* Use the extended output format a la /dev/kmsg */ +/** + * cons_flags - General console flags + * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate + * output of messages that were already shown by boot + * consoles or read by userspace via syslog() syscall. + * @CON_CONSDEV: Indicates that the console driver is backing + * /dev/console. + * @CON_ENABLED: Indicates if a console is allowed to print records. If + * false, the console also will not advance to later + * records. + * @CON_BOOT: Marks the console driver as early console driver which + * is used during boot before the real driver becomes + * available. It will be automatically unregistered + * when the real console driver is registered unless + * "keep_bootcon" parameter is used. + * @CON_ANYTIME: A misnomed historical flag which tells the core code + * that the legacy @console::write callback can be invoked + * on a CPU which is marked OFFLINE. That is misleading as + * it suggests that there is no contextual limit for + * invoking the callback. The original motivation was + * readiness of the per-CPU areas. + * @CON_BRL: Indicates a braille device which is exempt from + * receiving the printk spam for obvious reasons. + * @CON_EXTENDED: The console supports the extended output format of + * /dev/kmesg which requires a larger output buffer. + */ +enum cons_flags { + CON_PRINTBUFFER = BIT(0), + CON_CONSDEV = BIT(1), + CON_ENABLED = BIT(2), + CON_BOOT = BIT(3), + CON_ANYTIME = BIT(4), + CON_BRL = BIT(5), + CON_EXTENDED = BIT(6), +}; struct console { char name[16]; From patchwork Mon Jan 9 10:07:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40776 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2071652wrt; Mon, 9 Jan 2023 02:12:49 -0800 (PST) X-Google-Smtp-Source: AMrXdXuUJR3XOyEC2DjJRSXtJ1t7X9DFYU5Z9iqdPMKs54/Rk6muS61qWAR6SqfoHQrDbKcYItib X-Received: by 2002:aa7:d689:0:b0:46c:3f90:f5d9 with SMTP id d9-20020aa7d689000000b0046c3f90f5d9mr54712241edr.5.1673259168878; Mon, 09 Jan 2023 02:12:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259168; cv=none; d=google.com; s=arc-20160816; b=ls092yk91SdTrXvzuyjVJlaIb/oB9VI/PDG1GHMIeTom9Yu4b9Ov87kXKUEjoB4WoJ cjzRBmuz6pkQ57Pe4Q77Ob75rah+D88umCWnV1mFKg/G+rxut3oVOFj2tbS06xrXl/0s FWuAOwmtc3OvQfM8EBb87pRhZkxCi7sphFPQUW+6gUy71GwDYmAhxQlxDNc8XE2AKysq CjQHA0kup/8cF0F6tz2Uz6JG0yIFy58JhqwUgkLS5COEPyOXvmARsOdh0X2isfocQ+9Y AiNdVRL2szBC5kWGOX4lg33P7C7/eXtTet2WKlasuDyxF7237WdsZnvoHIcfTnfbKnTM WImg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=Z+UJBfU/8l7fpxJzETJJj+Hr/lkYjP0wgmBYzutEocw=; b=oL4Rrzrx88EnteHsq+Uy49/Eu3Rqd2jASa7yZ/O/Vla74qZrRD27cJNbbYI/vb3ArV enP8wjL26yMP026uMYET/dVn/ydJ32dPlO3rOnYd9FZe0ApkVmTSbVhsHgHl2Z8hjh3O PbAV+V+T6sdWHxMq5Rzv5+cQ5PY5/qytpZyFi/LI3J4s6JF5QQQhKh2814uyLT/dZ7HO UnTWzJqHGLYImWWsBPUwQ7X5fFLUz0MuuYeWRveXcHIdXb3v/JazHid1HbyzWRnMM7eB 5albqykwFpWM4eFjNgLjzo04BA3Rrjd7GARegjiVAMlWl87vfVqdIRLLOp4ZQYMFWV/1 Ga4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=o+xsj0DT; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sc19-20020a1709078a1300b007ce3aef9498si9402969ejc.628.2023.01.09.02.12.25; Mon, 09 Jan 2023 02:12:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=o+xsj0DT; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233876AbjAIKJk (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236862AbjAIKIs (ORCPT ); Mon, 9 Jan 2023 05:08:48 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FC3210B7F for ; Mon, 9 Jan 2023 02:08:46 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z+UJBfU/8l7fpxJzETJJj+Hr/lkYjP0wgmBYzutEocw=; b=o+xsj0DTLpnugqT9ezmLgLNWgnTCNkkIYxDcBf/DHJnSdw0TmM+P1+bM69x1RHK+N0fiSP gQc//PgyqQWgX+dSbAad/NlcvnNw7JF8lfwvQ//eVwuY2qJLyEiGlcQpOcpnsyd6BgeAsm /oJOn11Z2t5RoVgfzvL921AKPbPVScZXRWrvrBX15YClQlbqWhZ0NVSb3jMeCtcLt2+ClA vAzxezes34pH/f74KXeE7mkEjB8HY80eIQxGrqS4Z9TGIze+lw7zoz0nQvZOJbphhaoAle IvHVkjvM6tWi4CUkOvaK/TfXNkSjsJTZh9CWTVAw/vazsse+u7sXuJjExyU2HA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z+UJBfU/8l7fpxJzETJJj+Hr/lkYjP0wgmBYzutEocw=; b=yEh83H4SphijnQjwG181Gkplaik4ycqGsuEg2bxBiRVNkLT6PBi+TOsYLWto9F0TmYO3U1 8iaIRJ7+CRup0pDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 3/8] console: Document struct console Date: Mon, 9 Jan 2023 11:13:55 +0106 Message-Id: <20230109100800.1085541-4-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539406229611496?= X-GMAIL-MSGID: =?utf-8?q?1754539406229611496?= From: Thomas Gleixner Add kerneldoc comments to struct console. Signed-off-by: Thomas Gleixner Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 54 ++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index ed804dd7c2e8..1e36958aa656 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -164,24 +164,44 @@ enum cons_flags { CON_EXTENDED = BIT(6), }; +/** + * struct console - The console descriptor structure + * @name: The name of the console driver + * @write: Write callback to output messages (Optional) + * @read: Read callback for console input (Optional) + * @device: The underlying TTY device driver (Optional) + * @unblank: Callback to unblank the console (Optional) + * @setup: Callback for initializing the console (Optional) + * @exit: Callback for teardown of the console (Optional) + * @match: Callback for matching a console (Optional) + * @flags: Console flags. See enum cons_flags + * @index: Console index, e.g. port number + * @cflag: TTY control mode flags + * @ispeed: TTY input speed + * @ospeed: TTY output speed + * @seq: Sequence number of the next ringbuffer record to print + * @dropped: Number of unreported dropped ringbuffer records + * @data: Driver private data + * @node: hlist node for the console list + */ struct console { - char name[16]; - void (*write)(struct console *, const char *, unsigned); - int (*read)(struct console *, char *, unsigned); - struct tty_driver *(*device)(struct console *, int *); - void (*unblank)(void); - int (*setup)(struct console *, char *); - int (*exit)(struct console *); - int (*match)(struct console *, char *name, int idx, char *options); - short flags; - short index; - int cflag; - uint ispeed; - uint ospeed; - u64 seq; - unsigned long dropped; - void *data; - struct hlist_node node; + char name[16]; + void (*write)(struct console *co, const char *s, unsigned int count); + int (*read)(struct console *co, char *s, unsigned int count); + struct tty_driver *(*device)(struct console *co, int *index); + void (*unblank)(void); + int (*setup)(struct console *co, char *options); + int (*exit)(struct console *co); + int (*match)(struct console *co, char *name, int idx, char *options); + short flags; + short index; + int cflag; + uint ispeed; + uint ospeed; + u64 seq; + unsigned long dropped; + void *data; + struct hlist_node node; }; #ifdef CONFIG_LOCKDEP From patchwork Mon Jan 9 10:07:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40775 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2071504wrt; Mon, 9 Jan 2023 02:12:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXstVyTxlzwpZXj45GcWVKDh8cR3/b5xyXngA/Uql9E00Jg26YZb5lMTpjGx6LVi/dRdCEUY X-Received: by 2002:a17:907:238e:b0:84d:457a:39d5 with SMTP id vf14-20020a170907238e00b0084d457a39d5mr2666119ejb.44.1673259146240; Mon, 09 Jan 2023 02:12:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259146; cv=none; d=google.com; s=arc-20160816; b=OMksw6tSl9s8s5Usl5XRwssTnIXbmwCUTQ5BXzaRiTVqC3tqswJ2HH6lX769nR1E5X WYKpSd2jRlXODZnUm1ivAGR6Fe6RQTgCmvOZTaqcy85dvFhTeRllJixbdsfm2bEW2SJJ SW0bseS5hBRbAPNKQUfJ8kPo+b1G0sTd+M/9rHsSakg5K1J8jAI48t3CA7uFy+VegQft H8HCFNAhRft/uSr6z9iJQcKs6J+4Se+gT3ZEYXe3T5Mx1Rk68/Pzi7FibuW0bTadFJCC grz95uHIY82lJSFmlMPhM2pYHO+3Gn+lm5ulTmbmsNULhZ9r3aBvPXz/ojTRrupJ4oCs tDZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=8+zAiTv6Y/GvERKIP0KzA27T536AAHA/aopD1bxgvwI=; b=Ap4k2k8rKv4yS8XJGZ9CVtTgtHzS7JbYeen0AghfLk67evBjQHFkIhnWbHlt3k7fVB Of37z4MepQt+oGvkJ+MW4MYlmB77m6PU7wPfIdaAgOPd47bdKachm4w0mFrwLxbb1QJY 9bRX8Rs2scYbBHqTRF7SdzmTg8Lvlok3z3ss1nll1yHJ0JpUdUKjYEGa8cJnyds9udfC 1XQkYEqoMPUIcHgETNdu1b+bvjXi4zNQCnRmt4azxCT3IPMEqbn0ggu2s9cr1Hn2r5md tfeCaV6q8+HarwyOXE+gRu51WylAdNcC4D35ZDgtPiPH05aaaJQJPkb5wAK8FXKMBEJr a5Pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JNT8gIFP; dkim=neutral (no key) header.i=@linutronix.de header.b="Eu92xff/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sz15-20020a1709078b0f00b007c16fb7f164si7193206ejc.943.2023.01.09.02.12.02; Mon, 09 Jan 2023 02:12:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=JNT8gIFP; dkim=neutral (no key) header.i=@linutronix.de header.b="Eu92xff/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233929AbjAIKJO (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236868AbjAIKIs (ORCPT ); Mon, 9 Jan 2023 05:08:48 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08F3E30A for ; Mon, 9 Jan 2023 02:08:47 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8+zAiTv6Y/GvERKIP0KzA27T536AAHA/aopD1bxgvwI=; b=JNT8gIFPAJYoUvQtxs/ab1HfVD7saQv3/plRyYi3APGD2uT/8YbOI9IOKcHWcjk2joFtoU ZXADH03yQMgsh3JU1bHNTwcvfonLlPTHmiiPsVz5/Rcgh2gvqrMtNRl4sgiAkrAT0Mi0Fh Kz3hKq71iqfcEuFYSFrow4tII6i3FgO30AmMuqBw6Sq6N00tbyy7ycLqzXNp67b7CmfrR6 2YASPZ3JfTxXqKwfWGGFKbAyKpU2mb9OX18HEZx7myTjENtPqQcuyk0ebF9glVSCy+Q0xW bkwKp2EQJgnjhsBMqudsBUkCX/eHDSRgm8noxa610/+HG/BcAgWEUpHecztJ4w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8+zAiTv6Y/GvERKIP0KzA27T536AAHA/aopD1bxgvwI=; b=Eu92xff/Ilhy02SZlS7FGKobrMXp5lfXIXz0jn7wQzBzjwMPELXDyqjMvmUEB5HtIOZtsQ nDo1TB0y3nq8olDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 4/8] printk: introduce struct printk_buffers Date: Mon, 9 Jan 2023 11:13:56 +0106 Message-Id: <20230109100800.1085541-5-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539382273264985?= X-GMAIL-MSGID: =?utf-8?q?1754539382273264985?= Introduce a new struct printk_buffers to contain all the buffers needed to read and format a printk message for output. Putting the buffers inside a struct reduces the number of buffer pointers that need to be tracked. Also, it allows usage of the sizeof() macro for the buffer sizes, rather than expecting certain sized buffers being passed in. Note that since the output buffer for normal consoles is now CONSOLE_EXT_LOG_MAX instead of CONSOLE_LOG_MAX, multi-line messages that may have been previously truncated will now be printed in full. This should be considered a feature and not a bug since the CONSOLE_LOG_MAX restriction was about limiting static buffer usage rather than limiting printed text. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 10 +++++++ kernel/printk/printk.c | 57 ++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index f394332410c9..6080d289a342 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -82,3 +82,13 @@ u16 printk_parse_prefix(const char *text, int *level, static inline bool printk_percpu_data_ready(void) { return false; } #endif /* CONFIG_PRINTK */ + +/** + * struct printk_buffers - Buffers to read/format/output printk messages. + * @outbuf: After formatting, contains text to output. + * @scratchbuf: Used as temporary ringbuffer reading and string-print space. + */ +struct printk_buffers { + char outbuf[CONSOLE_EXT_LOG_MAX]; + char scratchbuf[LOG_LINE_MAX]; +}; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 55bb065de65f..bc5d4574c459 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2728,14 +2728,6 @@ static void __console_unlock(void) * Print one record for the given console. The record printed is whatever * record is the next available record for the given console. * - * @text is a buffer of size CONSOLE_LOG_MAX. - * - * If extended messages should be printed, @ext_text is a buffer of size - * CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL. - * - * If dropped messages should be printed, @dropped_text is a buffer of size - * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL. - * * @handover will be set to true if a printk waiter has taken over the * console_lock, in which case the caller is no longer holding both the * console_lock and the SRCU read lock. Otherwise it is set to false. @@ -2747,17 +2739,33 @@ static void __console_unlock(void) * * Requires the console_lock and the SRCU read lock. */ -static bool console_emit_next_record(struct console *con, char *text, char *ext_text, - char *dropped_text, bool *handover, int cookie) +static bool console_emit_next_record(struct console *con, bool *handover, int cookie) { + static char dropped_text[DROPPED_TEXT_MAX]; + static struct printk_buffers pbufs; static int panic_console_dropped; + + bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED; + const size_t scratchbuf_sz = sizeof(pbufs.scratchbuf); + const size_t outbuf_sz = sizeof(pbufs.outbuf); + char *scratchbuf = &pbufs.scratchbuf[0]; + char *outbuf = &pbufs.outbuf[0]; struct printk_info info; struct printk_record r; unsigned long flags; - char *write_text; size_t len; - prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); + /* + * Formatting extended messages requires a separate buffer, so use the + * scratch buffer to read in the ringbuffer text. + * + * Formatting normal messages is done in-place, so read the ringbuffer + * text directly into the output buffer. + */ + if (is_extended) + prb_rec_init_rd(&r, &info, scratchbuf, scratchbuf_sz); + else + prb_rec_init_rd(&r, &info, outbuf, outbuf_sz); *handover = false; @@ -2779,13 +2787,11 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_ goto skip; } - if (ext_text) { - write_text = ext_text; - len = info_print_ext_header(ext_text, CONSOLE_EXT_LOG_MAX, r.info); - len += msg_print_ext_body(ext_text + len, CONSOLE_EXT_LOG_MAX - len, + if (is_extended) { + len = info_print_ext_header(outbuf, outbuf_sz, r.info); + len += msg_print_ext_body(outbuf + len, outbuf_sz - len, &r.text_buf[0], r.info->text_len, &r.info->dev_info); } else { - write_text = text; len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time); } @@ -2803,7 +2809,8 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_ console_lock_spinning_enable(); stop_critical_timings(); /* don't trace print latency */ - call_console_driver(con, write_text, len, dropped_text); + call_console_driver(con, outbuf, len, + is_extended ? NULL : dropped_text); start_critical_timings(); con->seq++; @@ -2839,9 +2846,6 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_ */ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover) { - static char dropped_text[DROPPED_TEXT_MAX]; - static char ext_text[CONSOLE_EXT_LOG_MAX]; - static char text[CONSOLE_LOG_MAX]; bool any_usable = false; struct console *con; bool any_progress; @@ -2861,16 +2865,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove continue; any_usable = true; - if (console_srcu_read_flags(con) & CON_EXTENDED) { - /* Extended consoles do not print "dropped messages". */ - progress = console_emit_next_record(con, &text[0], - &ext_text[0], NULL, - handover, cookie); - } else { - progress = console_emit_next_record(con, &text[0], - NULL, &dropped_text[0], - handover, cookie); - } + progress = console_emit_next_record(con, handover, cookie); /* * If a handover has occurred, the SRCU read lock From patchwork Mon Jan 9 10:07:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40789 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2083914wrt; Mon, 9 Jan 2023 02:47:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXuPOMcStopMjccEoxUoYEOA22sAfjcnPji7X0vJsquUjma7BWfeHnpotXN2jGkjZkMjxiNK X-Received: by 2002:a17:902:dad0:b0:191:3ec0:9166 with SMTP id q16-20020a170902dad000b001913ec09166mr101816346plx.6.1673261232701; Mon, 09 Jan 2023 02:47:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673261232; cv=none; d=google.com; s=arc-20160816; b=zd600FuhZNo0XdgZqmcGEh7IRNuywX/B7GGkV+RPOO55IllTwx9sqhF0nmxliqra69 4USFR2MdFAxNjpBvV+3TgKVTp0VoKtcucaZrVQ6ZRBKUXr7EzmIsPXQwlbYhcdEcBtsh fB8boLD/UMWsAmgsgPgWXeHHW+LD6sepFiQkUgA33YB9Pot+HecI7tiIm1Gq0dswV19+ mookgM5Wq23+waG6Gen8UcTiZonzTp+gYpvbGz8M6hVNIjcZx7bqEvFaVu+43dPWSwUi 2VZByXlzL6U3YE7XFXJ5TVhJtNmpAj/jV5mKDThlF5R6ecfYQJYiFlMkSw7CR+lQ4F6k q4kQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=yqXNx4ZjO+PgrTp+dbVbR/nkqi+9jEGT8axvs9fFi6o=; b=RJZzD2xh9Rrr64DlGaqADZUSUX6MZiO725AVg3A0pnblWhwDvkSLQ52ZDf0Qvyosl+ yDLiIv+8wy+ZVEPZ9QdGP3lP+YKsIHT8Cl5pF3dQJhuYNcvDnXs6zKXhA4c056h61FCI i4mLRsEf5A/1kPzgmHUhkul4hMaWxyiqOtjmOurke66MDnFeQZw80ncPo8KBTpTGUMwg +ya2Zqwwmx6rNkkSSdnX4B7FeMNh0u78mbX3Vh5rmnkP5RN2ZvEhclNHIY6ltcill/wk TIDuIpPitSqxqFTz8dHvZAATNUw2r5AG8R8F1o97ni1+M9d/OnWXjlnHaXES4vq8OIvz xcdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TbZW1AYk; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c5-20020a170902724500b001910b27870esi8347213pll.512.2023.01.09.02.47.00; Mon, 09 Jan 2023 02:47:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TbZW1AYk; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236673AbjAIKYf (ORCPT + 99 others); Mon, 9 Jan 2023 05:24:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236873AbjAIKIu (ORCPT ); Mon, 9 Jan 2023 05:08:50 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C661AFCE for ; Mon, 9 Jan 2023 02:08:48 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yqXNx4ZjO+PgrTp+dbVbR/nkqi+9jEGT8axvs9fFi6o=; b=TbZW1AYkzRXfnxhd4mnm5/MBNwDg+zBjuBLaqk2ckBDX4duSlhbORnYWJxluvCZXe6EswF pVvfP0M5dtKrAjn8LePECMYBrBEuH8xIpL4gqDZRerO1G7/qYoCxULZa4cTGIrOZnvecGP aI09mGTJc60+A/F6k81LaAKtltY0EVkRjt6GemNoeiUqlYq8fjsojutu3yh7DYTNh0kt0A hPVFZfFpi1THen3mghP3GnAhYsssHUB/n1X3m6hIMurswh+wnxoK9P5lN6Ta5nhVBr5EsM sUSAosd4ncVOjP+cm0VmTKoaFv4LpKWwvt6K3pUMkcRyGmePMRfDYsuiUrcwUQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yqXNx4ZjO+PgrTp+dbVbR/nkqi+9jEGT8axvs9fFi6o=; b=YqPYkPhjGtr67J2ezM9YMIYVa0hMdyes5jUxeEJMcU7Ya2rtU5Wt22YYfo5q7iADbunYS0 KEauCMvd6AwCoFBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 5/8] printk: introduce printk_get_next_message() and printk_message Date: Mon, 9 Jan 2023 11:13:57 +0106 Message-Id: <20230109100800.1085541-6-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754541570496199438?= X-GMAIL-MSGID: =?utf-8?q?1754541570496199438?= Code for performing the console output is intermixed with code that is formatting the output for that console. Introduce a new helper function printk_get_next_message() to handle the reading and formatting of the printk text. The helper does not require any locking so that in the future it can be used for other printing contexts as well. This also introduces a new struct printk_message to wrap the struct printk_buffers, adding metadata about its contents. This allows users of printk_get_next_message() to receive all relevant information about the message that was read and formatted. Why is struct printk_message a wrapper struct? It is intentional that a wrapper struct is introduced instead of adding the metadata directly to struct printk_buffers. The upcoming atomic consoles support multiple printing contexts per CPU. This means that while a CPU is formatting a message, it can be interrupted and the interrupting context may also format a (possibly different) message. Since the printk buffers are rather large, there will only be one struct printk_buffers per CPU and it must be shared by the possible contexts of that CPU. If the metadata was part of struct printk_buffers, interrupting contexts would clobber the metadata being prepared by the interrupted context. This could be handled by robustifying the message formatting functions to cope with metadata unexpectedly changing. However, this would require significant amounts of extra data copying, also adding significant complexity to the code. Instead, the metadata can live on the stack of the formatting context and the message formatting functions do not need to be concerned about the metadata changing underneath them. Note that the message formatting functions can handle unexpected text buffer changes. So it is perfectly OK if a shared text buffer is clobbered by an interrupting context. The atomic console implementation will recognize the interruption and avoid printing the (probably garbage) text buffer. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 16 ++++++ kernel/printk/printk.c | 115 +++++++++++++++++++++++++++------------ 2 files changed, 96 insertions(+), 35 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6080d289a342..c9bb0cd86372 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -92,3 +92,19 @@ struct printk_buffers { char outbuf[CONSOLE_EXT_LOG_MAX]; char scratchbuf[LOG_LINE_MAX]; }; + +/** + * struct printk_message - Container for a prepared printk message. + * @pbufs: printk buffers used to prepare the message. + * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This + * does not count the terminator. A value of 0 means there is + * nothing to output and this record should be skipped. + * @seq: The sequence number of the record used for @pbufs->outbuf. + * @dropped: The number of dropped records from reading @seq. + */ +struct printk_message { + struct printk_buffers *pbufs; + unsigned int outbuf_len; + u64 seq; + unsigned long dropped; +}; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index bc5d4574c459..6e2a6d5548e9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2725,35 +2725,35 @@ static void __console_unlock(void) } /* - * Print one record for the given console. The record printed is whatever - * record is the next available record for the given console. + * Read and format the specified record (or a later record if the specified + * record is not available). * - * @handover will be set to true if a printk waiter has taken over the - * console_lock, in which case the caller is no longer holding both the - * console_lock and the SRCU read lock. Otherwise it is set to false. + * @pmsg will contain the formatted result. @pmsg->pbufs must point to a + * struct printk_buffers. * - * @cookie is the cookie from the SRCU read lock. + * @seq is the record to read and format. If it is not available, the next + * valid record is read. * - * Returns false if the given console has no next record to print, otherwise - * true. + * @is_extended specifies if the message should be formatted for extended + * console output. * - * Requires the console_lock and the SRCU read lock. + * 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.) */ -static bool console_emit_next_record(struct console *con, bool *handover, int cookie) +static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, + bool is_extended) { - static char dropped_text[DROPPED_TEXT_MAX]; - static struct printk_buffers pbufs; static int panic_console_dropped; - bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED; - const size_t scratchbuf_sz = sizeof(pbufs.scratchbuf); - const size_t outbuf_sz = sizeof(pbufs.outbuf); - char *scratchbuf = &pbufs.scratchbuf[0]; - char *outbuf = &pbufs.outbuf[0]; + struct printk_buffers *pbufs = pmsg->pbufs; + const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); + const size_t outbuf_sz = sizeof(pbufs->outbuf); + char *scratchbuf = &pbufs->scratchbuf[0]; + char *outbuf = &pbufs->outbuf[0]; struct printk_info info; struct printk_record r; - unsigned long flags; - size_t len; + size_t len = 0; /* * Formatting extended messages requires a separate buffer, so use the @@ -2767,25 +2767,26 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co else prb_rec_init_rd(&r, &info, outbuf, outbuf_sz); - *handover = false; - - if (!prb_read_valid(prb, con->seq, &r)) + if (!prb_read_valid(prb, seq, &r)) return false; - if (con->seq != r.info->seq) { - con->dropped += r.info->seq - con->seq; - con->seq = r.info->seq; - if (panic_in_progress() && panic_console_dropped++ > 10) { - suppress_panic_printk = 1; - pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); - } + pmsg->seq = r.info->seq; + pmsg->dropped = r.info->seq - seq; + + /* + * Check for dropped messages in panic here so that printk + * suppression can occur as early as possible if necessary. + */ + if (pmsg->dropped && + panic_in_progress() && + panic_console_dropped++ > 10) { + suppress_panic_printk = 1; + pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); } /* Skip record that has level above the console loglevel. */ - if (suppress_message_printing(r.info->level)) { - con->seq++; - goto skip; - } + if (suppress_message_printing(r.info->level)) + goto out; if (is_extended) { len = info_print_ext_header(outbuf, outbuf_sz, r.info); @@ -2794,6 +2795,50 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co } else { len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time); } +out: + pmsg->outbuf_len = len; + return true; +} + +/* + * Print one record for the given console. The record printed is whatever + * record is the next available record for the given console. + * + * @handover will be set to true if a printk waiter has taken over the + * console_lock, in which case the caller is no longer holding both the + * console_lock and the SRCU read lock. Otherwise it is set to false. + * + * @cookie is the cookie from the SRCU read lock. + * + * Returns false if the given console has no next record to print, otherwise + * true. + * + * Requires the console_lock and the SRCU read lock. + */ +static bool console_emit_next_record(struct console *con, bool *handover, int cookie) +{ + static char dropped_text[DROPPED_TEXT_MAX]; + static struct printk_buffers pbufs; + + bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED; + char *outbuf = &pbufs.outbuf[0]; + struct printk_message pmsg = { + .pbufs = &pbufs, + }; + unsigned long flags; + + *handover = false; + + if (!printk_get_next_message(&pmsg, con->seq, is_extended)) + return false; + + con->dropped += pmsg.dropped; + + /* Skip messages of formatted length 0. */ + if (pmsg.outbuf_len == 0) { + con->seq = pmsg.seq + 1; + goto skip; + } /* * While actively printing out messages, if another printk() @@ -2809,11 +2854,11 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co console_lock_spinning_enable(); stop_critical_timings(); /* don't trace print latency */ - call_console_driver(con, outbuf, len, + call_console_driver(con, outbuf, pmsg.outbuf_len, is_extended ? NULL : dropped_text); start_critical_timings(); - con->seq++; + con->seq = pmsg.seq + 1; *handover = console_lock_spinning_disable_and_check(cookie); printk_safe_exit_irqrestore(flags); From patchwork Mon Jan 9 10:07:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40774 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2071455wrt; Mon, 9 Jan 2023 02:12:18 -0800 (PST) X-Google-Smtp-Source: AMrXdXvWbxM0eLH8PXWT0T8TCqDlXyLxsWI3+ROBBSFCB2EZbomY58Kumpt7Ae28Zlx/zMiVDvOq X-Received: by 2002:aa7:de08:0:b0:472:7c75:832 with SMTP id h8-20020aa7de08000000b004727c750832mr52782846edv.16.1673259138009; Mon, 09 Jan 2023 02:12:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259137; cv=none; d=google.com; s=arc-20160816; b=R+CwxMwQUhNpK6BG0PBXomKSY/k4eyG87A490QZiVu146Ozk3nQU1qVRDie77ZAbDf 2ZIhPigPAluQTHCLMhueFFs+xJoiAEB2+wf8b57Cmz4LuVfucMKkThmgX4kCwrPdqCE9 /M4LoRz/9aUC7eNonZ+axwnnnMju0bkqmVu6tw+wSIBb8zxC2CvkzJaFi9xaqzPiwHnF rf3R1HZCs0Ze2crYdTW/Mio/fI1ONE5RHD5+9XQla9lzW0wcUZ+5w0ZZBwmkcQ12gVhz dpbhe3U/CHt+EpHBxYJedjPgQwBkZpxnDwDGK44BHhB1DFCyMPuPDMXkF3mTaXl/zZUk Yu3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=KwtlRFi0ucYwgo8zAGimTYO9K8YK26MeFnNklCQOGzw=; b=Iev4nVekVQA0O6iEp2VzKKGWKaiw/QKSrYIu5Qcz0qrGdY+P2Cw+3gfJTdZO33PpRQ EzinSp/T5fqlJOvWMJ/t1k2SFyUuUSf1zxf+RLXoLQwPTB7PjE8iWIe4E0hJCx4hfpkf BifRWaXSieq+wLM4jwj4Fx2xmrzB1Ia3JQG0TvMDK+uxpYhq0OFnmf5Kqd3qXAxkcq37 GqNrZWuWzCcTCn5qvZ4z5fL+XZyjyYI51l9RxXSOPJlc1xmfocElBWMurWQSlQ8kSgZR ZpQh8LegSuzOj/L5Q2gA9Vlondg7rFMEngGKU9SZaIvv14dh93GUiDTLZOK4yn0JQCeB C4kA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3RoQA2aD; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fj14-20020a0564022b8e00b004993ce7f70asi4091053edb.213.2023.01.09.02.11.54; Mon, 09 Jan 2023 02:12:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3RoQA2aD; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233917AbjAIKJ3 (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236871AbjAIKIu (ORCPT ); Mon, 9 Jan 2023 05:08:50 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D09CA10F6 for ; Mon, 9 Jan 2023 02:08:48 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KwtlRFi0ucYwgo8zAGimTYO9K8YK26MeFnNklCQOGzw=; b=3RoQA2aDLx7AlS6Co8IADuTW2f6qTEEXfGZJMrn/9nhav5yaGegA44eq414C5fFHD/ZM5J UrASxwb8wRezckEGyL5BqJ9U2C0Aiv2FCZE8lWEiRNkvD/kIUrf7anBZw/KT0owt0G0qKE h3jF2XIJtY5C5yPlgp9sFw5WMhKZCzcgE2ztl6Dxm2x3fEn83QDN5qYXxKd2ortCyo0krr mhj90TQJUeN7L2hxAtKssv7qIcH7uoBM2NSfiUBDAvYJM9Phdc+noOQUKgMton+VbqmD8b 1/v6qd1y3rCVYJt/zxdIetZeT4abFjBboaTWZblVX8OuyJuF0IYlBPi3aftK1Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KwtlRFi0ucYwgo8zAGimTYO9K8YK26MeFnNklCQOGzw=; b=IXpLv0+901H2N2U63t49w1Ie91aCWYX8F3HksNv9buCqUJQaEx1/MtDDNqdWEQKrLDpZHK Vwc5VVXkijc0hZCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 6/8] printk: introduce console_prepend_dropped() for dropped messages Date: Mon, 9 Jan 2023 11:13:58 +0106 Message-Id: <20230109100800.1085541-7-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539373515127715?= X-GMAIL-MSGID: =?utf-8?q?1754539373515127715?= Currently "dropped messages" are separately printed immediately before printing the printk message. Since normal consoles are now using an output buffer that is much larger than previously, the "dropped message" could be prepended to the printk message and then output everything in a single write() call. Introduce a helper function console_prepend_dropped() to prepend an existing message with a "dropped message". This simplifies the code by allowing all message formatting to be handled together and then only requires a single write() call to output the full message. And since this helper does not require any locking, it can be used in the future for other console printing contexts as well. Note that console_prepend_dropped() is defined as a NOP for !CONFIG_PRINTK. Although the function will never be called for !CONFIG_PRINTK, compiling the function can lead to warnings of "always true" conditionals due to the size macro values used in !CONFIG_PRINTK. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 4 -- kernel/printk/printk.c | 90 +++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index c9bb0cd86372..72df730597f1 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -26,9 +26,6 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, /* the maximum size of a formatted extended record */ #define CONSOLE_EXT_LOG_MAX 8192 -/* the maximum size for a dropped text message */ -#define DROPPED_TEXT_MAX 64 - /* the maximum size allowed to be reserved for a record */ #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) @@ -69,7 +66,6 @@ u16 printk_parse_prefix(const char *text, int *level, #define PREFIX_MAX 0 #define CONSOLE_LOG_MAX 0 #define CONSOLE_EXT_LOG_MAX 0 -#define DROPPED_TEXT_MAX 0 #define LOG_LINE_MAX 0 /* diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6e2a6d5548e9..80a49ec6f42b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1995,27 +1995,6 @@ static int console_trylock_spinning(void) return 1; } -/* - * Call the specified console driver, asking it to write out the specified - * text and length. If @dropped_text is non-NULL and any records have been - * dropped, a dropped message will be written out first. - */ -static void call_console_driver(struct console *con, const char *text, size_t len, - char *dropped_text) -{ - size_t dropped_len; - - if (con->dropped && dropped_text) { - dropped_len = snprintf(dropped_text, DROPPED_TEXT_MAX, - "** %lu printk messages dropped **\n", - con->dropped); - con->dropped = 0; - con->write(con, dropped_text, dropped_len); - } - - con->write(con, text, len); -} - /* * Recursion is tracked separately on each CPU. If NMIs are supported, an * additional NMI context per CPU is also separately tracked. Until per-CPU @@ -2395,10 +2374,6 @@ static ssize_t msg_print_ext_body(char *buf, size_t size, struct dev_printk_info *dev_info) { return 0; } static void console_lock_spinning_enable(void) { } static int console_lock_spinning_disable_and_check(int cookie) { return 0; } -static void call_console_driver(struct console *con, const char *text, size_t len, - char *dropped_text) -{ -} static bool suppress_message_printing(int level) { return false; } static bool pr_flush(int timeout_ms, bool reset_on_progress) { return true; } static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; } @@ -2724,6 +2699,56 @@ static void __console_unlock(void) up_console_sem(); } +/* + * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". This + * is achieved by shifting the existing message over and inserting the dropped + * message. + * + * @pmsg is the printk message to prepend. + * + * @dropped is the dropped count to report in the dropped message. + * + * If the message text in @pmsg->pbufs->outbuf does not have enough space for + * the dropped message, the message text will be sufficiently truncated. + * + * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated. + */ +#ifdef CONFIG_PRINTK +static void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped) +{ + struct printk_buffers *pbufs = pmsg->pbufs; + const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); + const size_t outbuf_sz = sizeof(pbufs->outbuf); + char *scratchbuf = &pbufs->scratchbuf[0]; + char *outbuf = &pbufs->outbuf[0]; + size_t len; + + len = snprintf(scratchbuf, scratchbuf_sz, + "** %lu printk messages dropped **\n", dropped); + + /* + * Make sure outbuf is sufficiently large before prepending. + * Keep at least the prefix when the message must be truncated. + * It is a rather theoretical problem when someone tries to + * use a minimalist buffer. + */ + if (WARN_ON_ONCE(len + PREFIX_MAX >= outbuf_sz)) + return; + + if (pmsg->outbuf_len + len >= outbuf_sz) { + /* Truncate the message, but keep it terminated. */ + pmsg->outbuf_len = outbuf_sz - (len + 1); + outbuf[pmsg->outbuf_len] = 0; + } + + memmove(outbuf + len, outbuf, pmsg->outbuf_len + 1); + memcpy(outbuf, scratchbuf, len); + pmsg->outbuf_len += len; +} +#else +#define console_prepend_dropped(pmsg, dropped) +#endif /* CONFIG_PRINTK */ + /* * Read and format the specified record (or a later record if the specified * record is not available). @@ -2817,7 +2842,6 @@ static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, */ static bool console_emit_next_record(struct console *con, bool *handover, int cookie) { - static char dropped_text[DROPPED_TEXT_MAX]; static struct printk_buffers pbufs; bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED; @@ -2840,6 +2864,11 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co goto skip; } + if (con->dropped && !is_extended) { + console_prepend_dropped(&pmsg, con->dropped); + con->dropped = 0; + } + /* * While actively printing out messages, if another printk() * were to occur on another CPU, it may wait for this one to @@ -2853,9 +2882,12 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co printk_safe_enter_irqsave(flags); console_lock_spinning_enable(); - stop_critical_timings(); /* don't trace print latency */ - call_console_driver(con, outbuf, pmsg.outbuf_len, - is_extended ? NULL : dropped_text); + /* Do not trace print latency. */ + stop_critical_timings(); + + /* Write everything out to the hardware. */ + con->write(con, outbuf, pmsg.outbuf_len); + start_critical_timings(); con->seq = pmsg.seq + 1; From patchwork Mon Jan 9 10:07:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40778 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2072072wrt; Mon, 9 Jan 2023 02:14:02 -0800 (PST) X-Google-Smtp-Source: AMrXdXsq/rrtt5vzwAY4OrDeYnxRO6b3Asy2+rN6Ukok5Z42aZ/LNFz6/t0UXP5K2mXKIDDtSRCT X-Received: by 2002:a05:6a20:bf19:b0:af:dc62:8abd with SMTP id gc25-20020a056a20bf1900b000afdc628abdmr69918426pzb.0.1673259241878; Mon, 09 Jan 2023 02:14:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259241; cv=none; d=google.com; s=arc-20160816; b=m5Egvis62AzPxbYkT7RfDyIFFE+JwPHaGdBfgsCuMG2EBCxwesxbrXCgJdB901yrlJ xCqouabf1PpZtAtgZwxx11Ute/l38LYsQHboTK0b5+yYYU2FFY/fEVkQITtYRr4IwT5d pdzumLjsZXnJVl54KbH+byvIHTh2Fo/8YAMwJADZLobuPHcMuVlUn7upAiuXvg4wV0hH W7lp8qJy3WJ4w5q2BCuQ+JKawP9P8hC4lVsaRqu/Lq/GJr7eGT/DA6kDgzidIXIpcshv mxyeVTWi17p0bS2uGsouOWE8Z4NjlRj3JGqswRrEfM2DAnWAlGhwsrZ3TDI9rzO1eTUs w5OA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=PJqnYxHgAm5zPWQPXQ8w2S1sD7jcnMKX3VIFczAZH38=; b=SzryS1jG/mNkV1d1KN4zo+8daN5PPt14i19IDS8Gp4KPB75kKMlva+MNZ32FaD+yw0 jeyCvY8cvkatqD0MBf+AiLMcH6FSONaCSAv42fJyyVDMdNzUmaJdXUUUOFpEcwYl15Yq wwToEQ24aNpwxnqewbo7ygoGbvgtmGt3Ip3bIP0iCu5+KeSDZ05QVXmk9bMNvtLRQTKX 9dMBc9b9t4nIlokOpHWVuteNiqjnKbpm3EsPaP2h30rc+AT8oq3y+gGV1NJAmCwWGsm1 A2EdWI3QJ+UobV1GQk3hN/RAGlZqzAVTaO3omSF+A4YyVLj+m3QS6N79yGc/6iuNDw3k KXZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=1EByRzQz; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e188-20020a6369c5000000b004a03cdc3f29si8913587pgc.140.2023.01.09.02.13.48; Mon, 09 Jan 2023 02:14:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=1EByRzQz; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236133AbjAIKJV (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236885AbjAIKIv (ORCPT ); Mon, 9 Jan 2023 05:08:51 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D5B3B1C1 for ; Mon, 9 Jan 2023 02:08:49 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PJqnYxHgAm5zPWQPXQ8w2S1sD7jcnMKX3VIFczAZH38=; b=1EByRzQzhb8tbRlrbFCbzD5+nrEU7ae34iNdqti1AKJsCvff+F8EJIdQKnShLXRjNtCyMk pzFgLfbQe4s268Hu/bbClmRcSoW2mxIet/PgIqMGHRJUDKPe7xwiMgRgtDCXo93ugKUoCA x4U2kVPQ/Yhzu6YHEmMl+cr7mv+TGcQmzIynuOic3tuyJtS7hX+mrEmZUAIvW2kCPTi6x8 cYSC2gWk61+8r8gddLm9JBPOQtbuqkdoEzNt2WE5uWfXqnl5h+ibCj00NQLwk94J8Ueb+I gvB37aNHy802lVSChD96146Ln3NCZcd+MdfejcBvRTU9HfaIZd0yOa6KGW3vBA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258926; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PJqnYxHgAm5zPWQPXQ8w2S1sD7jcnMKX3VIFczAZH38=; b=IAQ1RJhglgrijsFvOo0DaKvloLt3um1/8neEecwhxvE1oNmhiHOEx7HpZl4r7r/RwkrLS3 jZivOpfaIOa5lyCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 7/8] printk: use printk_buffers for devkmsg Date: Mon, 9 Jan 2023 11:13:59 +0106 Message-Id: <20230109100800.1085541-8-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539482791114940?= X-GMAIL-MSGID: =?utf-8?q?1754539482791114940?= Replace the buffers in struct devkmsg_user with a struct printk_buffers. This reduces the number of buffers to keep track of. As a side-effect, @text_buf was 8kB large, even though it only needed to be the max size of a ringbuffer record. By switching to struct printk_buffers, ~7kB less memory is allocated when opening /dev/kmsg. And since struct printk_buffers will be used now, reduce duplicate code by calling printk_get_next_message() to handle the record reading and formatting. Note that since /dev/kmsg never suppresses records based on loglevel, printk_get_next_message() is extended with an extra bool argument to specify if suppression is allowed. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 49 +++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 80a49ec6f42b..a72904890a5f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -695,16 +695,15 @@ static ssize_t msg_print_ext_body(char *buf, size_t size, return len; } +static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, + bool is_extended, bool may_supress); + /* /dev/kmsg - userspace message inject/listen interface */ struct devkmsg_user { atomic64_t seq; struct ratelimit_state rs; struct mutex lock; - char buf[CONSOLE_EXT_LOG_MAX]; - - struct printk_info info; - char text_buf[CONSOLE_EXT_LOG_MAX]; - struct printk_record record; + struct printk_buffers pbufs; }; static __printf(3, 4) __cold @@ -786,8 +785,10 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct devkmsg_user *user = file->private_data; - struct printk_record *r = &user->record; - size_t len; + char *outbuf = &user->pbufs.outbuf[0]; + struct printk_message pmsg = { + .pbufs = &user->pbufs, + }; ssize_t ret; if (!user) @@ -797,7 +798,7 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, if (ret) return ret; - if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) { + if (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, false)) { if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; @@ -814,36 +815,31 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, * This pairs with __wake_up_klogd:A. */ ret = wait_event_interruptible(log_wait, - prb_read_valid(prb, - atomic64_read(&user->seq), r)); /* LMM(devkmsg_read:A) */ + printk_get_next_message(&pmsg, atomic64_read(&user->seq), true, + false)); /* LMM(devkmsg_read:A) */ if (ret) goto out; } - if (r->info->seq != atomic64_read(&user->seq)) { + if (pmsg.dropped) { /* our last seen message is gone, return error and reset */ - atomic64_set(&user->seq, r->info->seq); + atomic64_set(&user->seq, pmsg.seq); ret = -EPIPE; goto out; } - len = info_print_ext_header(user->buf, sizeof(user->buf), r->info); - len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len, - &r->text_buf[0], r->info->text_len, - &r->info->dev_info); - - atomic64_set(&user->seq, r->info->seq + 1); + atomic64_set(&user->seq, pmsg.seq + 1); - if (len > count) { + if (pmsg.outbuf_len > count) { ret = -EINVAL; goto out; } - if (copy_to_user(buf, user->buf, len)) { + if (copy_to_user(buf, outbuf, pmsg.outbuf_len)) { ret = -EFAULT; goto out; } - ret = len; + ret = pmsg.outbuf_len; out: mutex_unlock(&user->lock); return ret; @@ -937,9 +933,6 @@ static int devkmsg_open(struct inode *inode, struct file *file) mutex_init(&user->lock); - prb_rec_init_rd(&user->record, &user->info, - &user->text_buf[0], sizeof(user->text_buf)); - atomic64_set(&user->seq, prb_first_valid_seq(prb)); file->private_data = user; @@ -2762,12 +2755,14 @@ static void console_prepend_dropped(struct printk_message *pmsg, unsigned long d * @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.) */ static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, - bool is_extended) + bool is_extended, bool may_suppress) { static int panic_console_dropped; @@ -2810,7 +2805,7 @@ static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, } /* Skip record that has level above the console loglevel. */ - if (suppress_message_printing(r.info->level)) + if (may_suppress && suppress_message_printing(r.info->level)) goto out; if (is_extended) { @@ -2853,7 +2848,7 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co *handover = false; - if (!printk_get_next_message(&pmsg, con->seq, is_extended)) + if (!printk_get_next_message(&pmsg, con->seq, is_extended, true)) return false; con->dropped += pmsg.dropped; From patchwork Mon Jan 9 10:08:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 40777 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2071659wrt; Mon, 9 Jan 2023 02:12:50 -0800 (PST) X-Google-Smtp-Source: AMrXdXuxjOv+OmjrEsadr04iLAK6flhTz8XvyUtqiR3kmM9vZ0orpsvhi7fe9ur0lGLJqIvTmRB7 X-Received: by 2002:a17:907:a707:b0:7d8:2488:4074 with SMTP id vw7-20020a170907a70700b007d824884074mr56365239ejc.77.1673259170514; Mon, 09 Jan 2023 02:12:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673259170; cv=none; d=google.com; s=arc-20160816; b=UPJCT+pGmvagoj27MS0Hd/8/mcdAmpO2K0ivmDgL0OKlrYD/oeB3o74s8LLMgFoiPP B+gvQrEJbB/mb0nqEQnB+6LJcwhRNG+DjDTw136o7FWhAVP9zi0Wc5n/DWybP1voTfBh FxjNg4HfdgyJk60ZLrDxPj5rxpdyD3KyNkT799B4HYEOX2RO5mOFMxhkaKgGwCHPJzRT eOWposedFmxBS3IOmUsSIHnm83GoaIAs56GiZP8gvg3GBCmQpgNlWh1kw77LMXIL3BYV JQv7ofs1oGQrtURokan4xtOw++sayawxrRq8EtXhH5ffCoC/v73M0/c6/QfS7j+JzpbV lV+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=Be9tRmuwGMWyEdusml6s/K1qdWm2ngTw+Ga411aox2M=; b=XOZSqbcKyKbCMzH8C4gKBnqS6S+SZgSE61ZpXxu6XSSCEJa/wvXAG7N6by+zfylKKQ bINuTUcv/WmOVVZZhdUq/zH80cHmXwrEeFgQ3J4dkKVIZJKSjFokosfgez5pHpZGUQAk mCipim35KPEQ/rpfN0xyt/l/w5/tNd6h/ntimUjCtkDKbgffJuIYNRRQzHPaUc+08W89 vZMUSpFJ+cZowilXYl5imxUJ5uJcgsiYNHDDpaUaK1HYhk2MtKMBKYwrpWTMwAOydR5x QskNdf4bJgU1E5625hrBISjmlEFKRjhKZzH3uHAVE8oPidYvhZWGNjK6H4g+tex0wCLn DxwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=pZzVouh4; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa41-20020a17090786a900b00836b1c40dd8si8911001ejc.202.2023.01.09.02.12.26; Mon, 09 Jan 2023 02:12:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=pZzVouh4; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234224AbjAIKJp (ORCPT + 99 others); Mon, 9 Jan 2023 05:09:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236879AbjAIKIv (ORCPT ); Mon, 9 Jan 2023 05:08:51 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D4C9BDF for ; Mon, 9 Jan 2023 02:08:49 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673258927; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Be9tRmuwGMWyEdusml6s/K1qdWm2ngTw+Ga411aox2M=; b=pZzVouh4ELzKMgwSHhPeMKTj3YCtRyYqBKOwZJZ5FUOjcbZUjYMDCdxEjfpWTzrPxhKMrF mqvJDj/M21N460cwCkmHj4PAsy7Nzd4eS5PFPhk8U40nQk8I/o7afniOxL27LW1iE/r8cO 9e3uuOln65Uf8q5bc6lSO38piC8y5tH51Cvu5bpfFHDerEJTzjUGQqeLK8F3a2wmuFC4Ud nImGEodfoizoL/9OOAJ2bg+CK3Srhva0onvfeg7SqoKB5oP2hehWzZkLp5dywiAa2Dzxcc hq0N4a083V58JUEHwgnLJ8BT+zg2EYXx7z0K+z3TkROF0J30wmYa/N53IroFSw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673258927; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Be9tRmuwGMWyEdusml6s/K1qdWm2ngTw+Ga411aox2M=; b=4E/OKkerVIvdINGIf9yPD//fehJOAQq35kb9jeQ1ftC4xVNQVRtEADdiLevYaTWMOoWXSE NQG+/LKc9PVRYSDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 8/8] printk: adjust string limit macros Date: Mon, 9 Jan 2023 11:14:00 +0106 Message-Id: <20230109100800.1085541-9-john.ogness@linutronix.de> In-Reply-To: <20230109100800.1085541-1-john.ogness@linutronix.de> References: <20230109100800.1085541-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754539407931924286?= X-GMAIL-MSGID: =?utf-8?q?1754539407931924286?= The various internal size limit macros have names and/or values that do not fit well to their current usage. Rename the macros so that their purpose is clear and, if needed, provide a more appropriate value. In general, the new macros and values will lead to less memory usage. The new macros are... PRINTK_MESSAGE_MAX: This is the maximum size for a formatted message on a console, devkmsg, or syslog. It does not matter which format the message has (normal or extended). It replaces the use of CONSOLE_EXT_LOG_MAX for console and devkmsg. It replaces the use of CONSOLE_LOG_MAX for syslog. Historically, normal messages have been allowed to print up to 1kB, whereas extended messages have been allowed to print up to 8kB. However, the difference in lengths of these message types is not significant and in multi-line records, normal messages are probably larger. Also, because 1kB is only slightly above the allowed record size, multi-line normal messages could be easily truncated during formatting. This new macro should be significantly larger than the allowed record size to allow sufficient space for extended or multi-line prefix text. A value of 2kB should be plenty of space. For normal messages this represents a doubling of the historically allowed amount. For extended messages it reduces the excessive 8kB size, thus reducing memory usage needed for message formatting. PRINTK_PREFIX_MAX: This is the maximum size allowed for a record prefix (used by console and syslog). It replaces PREFIX_MAX. The value is left unchanged. PRINTKRB_RECORD_MAX: This is the maximum size allowed to be reserved for a record in the ringbuffer. It is used by all readers and writers with the printk ringbuffer. It replaces LOG_LINE_MAX. Previously this was set to "1kB - PREFIX_MAX", which makes some sense if 1kB is the limit for normal message output and prefixes are enabled. However, with the allowance of larger output and the existence of multi-line records, the value is rather bizarre. Round the value up to 1kB. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 27 +++++++++++++-------------- kernel/printk/printk.c | 24 ++++++++++++------------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 72df730597f1..2a17704136f1 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -15,19 +15,19 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, #ifdef CONFIG_PRINTK #ifdef CONFIG_PRINTK_CALLER -#define PREFIX_MAX 48 +#define PRINTK_PREFIX_MAX 48 #else -#define PREFIX_MAX 32 +#define PRINTK_PREFIX_MAX 32 #endif -/* the maximum size of a formatted record (i.e. with prefix added per line) */ -#define CONSOLE_LOG_MAX 1024 - -/* the maximum size of a formatted extended record */ -#define CONSOLE_EXT_LOG_MAX 8192 +/* + * the maximum size of a formatted record (i.e. with prefix added + * per line and dropped messages or in extended message format) + */ +#define PRINTK_MESSAGE_MAX 2048 /* the maximum size allowed to be reserved for a record */ -#define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) +#define PRINTKRB_RECORD_MAX 1024 /* Flags for a single printk record. */ enum printk_info_flags { @@ -63,10 +63,9 @@ u16 printk_parse_prefix(const char *text, int *level, enum printk_info_flags *flags); #else -#define PREFIX_MAX 0 -#define CONSOLE_LOG_MAX 0 -#define CONSOLE_EXT_LOG_MAX 0 -#define LOG_LINE_MAX 0 +#define PRINTK_PREFIX_MAX 0 +#define PRINTK_MESSAGE_MAX 0 +#define PRINTKRB_RECORD_MAX 0 /* * In !PRINTK builds we still export console_sem @@ -85,8 +84,8 @@ static inline bool printk_percpu_data_ready(void) { return false; } * @scratchbuf: Used as temporary ringbuffer reading and string-print space. */ struct printk_buffers { - char outbuf[CONSOLE_EXT_LOG_MAX]; - char scratchbuf[LOG_LINE_MAX]; + char outbuf[PRINTK_MESSAGE_MAX]; + char scratchbuf[PRINTKRB_RECORD_MAX]; }; /** diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index a72904890a5f..55338bfd3b55 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -729,7 +729,7 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from) size_t len = iov_iter_count(from); ssize_t ret = len; - if (!user || len > LOG_LINE_MAX) + if (!user || len > PRINTKRB_RECORD_MAX) return -EINVAL; /* Ignore when user logging is disabled. */ @@ -1127,7 +1127,7 @@ static unsigned int __init add_to_rb(struct printk_ringbuffer *rb, return prb_record_text_space(&e); } -static char setup_text_buf[LOG_LINE_MAX] __initdata; +static char setup_text_buf[PRINTKRB_RECORD_MAX] __initdata; void __init setup_log_buf(int early) { @@ -1393,7 +1393,7 @@ static size_t record_print_text(struct printk_record *r, bool syslog, size_t text_len = r->info->text_len; size_t buf_size = r->text_buf_size; char *text = r->text_buf; - char prefix[PREFIX_MAX]; + char prefix[PRINTK_PREFIX_MAX]; bool truncated = false; size_t prefix_len; size_t line_len; @@ -1492,7 +1492,7 @@ static size_t get_record_print_text_size(struct printk_info *info, unsigned int line_count, bool syslog, bool time) { - char prefix[PREFIX_MAX]; + char prefix[PRINTK_PREFIX_MAX]; size_t prefix_len; prefix_len = info_print_prefix(info, syslog, time, prefix); @@ -1558,11 +1558,11 @@ static int syslog_print(char __user *buf, int size) int len = 0; u64 seq; - text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL); + text = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL); if (!text) return -ENOMEM; - prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); + prb_rec_init_rd(&r, &info, text, PRINTK_MESSAGE_MAX); mutex_lock(&syslog_lock); @@ -1663,7 +1663,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear) u64 seq; bool time; - text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL); + text = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL); if (!text) return -ENOMEM; @@ -1675,7 +1675,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear) seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1, size, true, time); - prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); + prb_rec_init_rd(&r, &info, text, PRINTK_MESSAGE_MAX); len = 0; prb_for_each_record(seq, prb, seq, &r) { @@ -2198,8 +2198,8 @@ int vprintk_store(int facility, int level, reserve_size = vsnprintf(&prefix_buf[0], sizeof(prefix_buf), fmt, args2) + 1; va_end(args2); - if (reserve_size > LOG_LINE_MAX) - reserve_size = LOG_LINE_MAX; + if (reserve_size > PRINTKRB_RECORD_MAX) + reserve_size = PRINTKRB_RECORD_MAX; /* Extract log level or control flags. */ if (facility == 0) @@ -2213,7 +2213,7 @@ int vprintk_store(int facility, int level, if (flags & LOG_CONT) { prb_rec_init_wr(&r, reserve_size); - if (prb_reserve_in_last(&e, prb, &r, caller_id, LOG_LINE_MAX)) { + if (prb_reserve_in_last(&e, prb, &r, caller_id, PRINTKRB_RECORD_MAX)) { text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size, facility, &flags, fmt, args); r.info->text_len += text_len; @@ -2725,7 +2725,7 @@ static void console_prepend_dropped(struct printk_message *pmsg, unsigned long d * It is a rather theoretical problem when someone tries to * use a minimalist buffer. */ - if (WARN_ON_ONCE(len + PREFIX_MAX >= outbuf_sz)) + if (WARN_ON_ONCE(len + PRINTK_PREFIX_MAX >= outbuf_sz)) return; if (pmsg->outbuf_len + len >= outbuf_sz) {