From patchwork Thu Jan 5 12:16:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 39494 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp271612wrt; Thu, 5 Jan 2023 04:18:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXsFAhVen4xjYqFboEYfXZIMAtdUGdq8XBkW36F40dghyon2IeZtXj6mY2U0uJ5pngEOkxs5 X-Received: by 2002:aa7:96f9:0:b0:582:7007:3fec with SMTP id i25-20020aa796f9000000b0058270073fecmr14628700pfq.11.1672921111797; Thu, 05 Jan 2023 04:18:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672921111; cv=none; d=google.com; s=arc-20160816; b=iqgoKxTJrApvpQeR8VEg0jAkOrg1lkAIqEGE01FXPA7jrn1KroxarDv8QbyevJRTlA SsPxXYJpfJwP5gXf2KMFU5r479CtH2zSIsal02/drEi4WZ9p5FLP7Nh439mvDL8b8X5l qNJm/ThIqIPV7g0u13wQLM1gERrWFwOm7RTGOFOJZC1a3GVlvGZMsdE64ffNzFpm/uaS vD5QF9cl6AXvmKyPqoechhXmhLd+0Iz1D2CcUkydysu3TABDiLhUVyRl+5L9eCh9C7yK 8BKakLyGvZAnV+2FwrziY0Ah5Uourpj3jj47FDu7tHd8e+YOvun+NNCbn2QnyAIaj0jI kKmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=5Xh0ofBqr/gGj4H4UF96ZzPTChHhGf7far4SpDmNAdA=; b=QI7Qnj1B5LjUKCqGPsc7dj6G5l46DkPQ7B9gkdzgKGphHf4++fXDbCyjZrsyfl/RV6 vaEZE/rtKcTY+KmSaPCpt8qkCIX3y7njnkz1SgMTWiBP3Gstn82dK0HQ8/oCJ2Dse4ug 0sXdjpGdKKVeEMqnqIz05LMxPs/e18GmnwV7jfg8LZEZqTG3Xtk/Z8al8jlePxZxu5IE ux1vELNa8T8K6NmojD+vYNsRNLuVXO/MFnme4jxLsazakpgljudFlGK+PsdY0Lxf1y6l WffA5zVzSSEu8Ps01ZhMvvtHq6aZ6w4zJ6pFjmagktKoWoJRfq+2qpt6C5zNtoiCkVwU +UpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LyFkvnan; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=VoUI18fQ; 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 j9-20020a056a00130900b00582fa10146esi4082216pfu.217.2023.01.05.04.18.18; Thu, 05 Jan 2023 04:18:31 -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=LyFkvnan; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=VoUI18fQ; 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 S232373AbjAEMRD (ORCPT + 99 others); Thu, 5 Jan 2023 07:17:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231646AbjAEMRB (ORCPT ); Thu, 5 Jan 2023 07:17:01 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9170CBC87 for ; Thu, 5 Jan 2023 04:17:00 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1672921016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5Xh0ofBqr/gGj4H4UF96ZzPTChHhGf7far4SpDmNAdA=; b=LyFkvnan5JmFgaO9HtdeolaPWE1BRvcuxzyvr9aleZQaDG+WQFKvXHrisuhywbSyAypOV6 jhiXPHr984EpDg8vBJCSiKJ5DlqDtv8929zFHuuju1OcPXOmSH9lytjwKgCN7KQxQEXq+L nqBtW34kEoFMHPO4MOKDOqcucJPU5Sm5RdrOREALOnso4NPW2P5o20GW/W1Cm7aPV81EWl KT73W+0IsA3EKe0dfhuqOopVR/llmSnmtphZamKk2zWvbwcgtvmWJgzjR9gah4UuymntGA 6e0TBfTVpPbzowyLHom9dsPmZ4OAvq/qLgfckpnmc5U3ZL5IQf6D/B72Vnelrw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1672921016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5Xh0ofBqr/gGj4H4UF96ZzPTChHhGf7far4SpDmNAdA=; b=VoUI18fQzb/C9y7fXtAxTfnVEjZK/2+EzjZysUpkX/Iz1kel5HN7qz9i2aFWqJ332LaLUf nnkyq9eI2u0fbVAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 7/8 v2] printk: use printk_buffers for devkmsg In-Reply-To: <87fscpch67.fsf@jogness.linutronix.de> References: <20230105103735.880956-1-john.ogness@linutronix.de> <20230105103735.880956-8-john.ogness@linutronix.de> <87fscpch67.fsf@jogness.linutronix.de> Date: Thu, 05 Jan 2023 13:22:16 +0106 Message-ID: <87cz7tch2n.fsf@jogness.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?1754178990143261726?= X-GMAIL-MSGID: =?utf-8?q?1754184927907859272?= Replace the buffers in struct devkmsg_user with a struct printk_buffers. This reduces the number of buffers to track. 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. Signed-off-by: John Ogness --- kernel/printk/printk.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 4fb7d29fb05d..32996b0c5d03 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -695,16 +695,14 @@ 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); + /* /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 +784,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 +797,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)) { + while (!printk_get_next_message(&pmsg, atomic64_read(&user->seq), true)) { if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; @@ -814,36 +814,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) */ + prb_read_valid(prb, atomic64_read(&user->seq), + NULL)); /* 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, pmsg.seq + 1); - atomic64_set(&user->seq, r->info->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 +932,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;