Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp659486rwl; Thu, 5 Jan 2023 02:44:41 -0800 (PST) X-Google-Smtp-Source: AMrXdXszR7/TjeHwS9UTR0oJ3LFq5/787mMXGA9ElTRvE2xIdA6m8h87zc7UHK8UUmnaq6/JH/nO X-Received: by 2002:a05:6a20:3b27:b0:ad:5747:a507 with SMTP id c39-20020a056a203b2700b000ad5747a507mr55657456pzh.3.1672915481342; Thu, 05 Jan 2023 02:44:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672915481; cv=none; d=google.com; s=arc-20160816; b=AvilJZa2E2hpE25s/O/tNHhreUQPt97AzbuOghvsS1ezPKe7RjKK/esuN+lL4y3oft t+triP8QLNSu9nFJQRYbJddRq9QDvx8jr+KCTw6plKsbAha6gFfaClRxO+IN5LOlgykM CyshKZNEDMKO6ZJnbXkf4hTHbUgUYmVQzW9/DN/jyb6Z5dHO/UIxIFQw+g9n0j9BXN13 llcwXtxDn/keUHzG8DRZ2ct9Ucn60Xmtb5iNJNpNwYjdb3wLF9627cWxjvJHPeMuObwA FlywrzCnEr2Vfpp8djyJ0lUfec+Z/MMRCFr20mp33D4qSCx0U5+CJmYKxTGUIabpyzaK nPQA== 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=DFngVp9MQgIEtplZg2ethuU38QZaV6s9fRh+JEWhx2k=; b=Bxi9fuq9ae1Q4k7x0XliPdl037PZf3WYqtoKm0VZppDrELXRCif0GbqCgVjMboffMq 11xc2QjouNs4sPdVoqPmbZzD9kyNUL7l5BrmU0ZEkv8dXnnrU6xWCFHpO21cq1c/TsKJ +/rofC17ra3vToFucaMWyWET5/UAZzI884vZE50Y5wA4vhvgSo9GDlzgvMk1bs9E31bg DEX9HpHhklPUa+YeKbF5zm9b9VaeJoa5jMiCmzqlRLjAixQwvm4r+JTlw6w+kXPzDYph +N9oB8AMUDNumWdMdNtOLi42lDxpmHWxNa9F9bdF/0EjAOmUHjoHfuUlsem4r9C9m9Q/ e8og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wE2fq0It; dkim=neutral (no key) header.i=@linutronix.de header.b=NwCHHW9S; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t71-20020a63814a000000b0049ceae025b7si22413126pgd.334.2023.01.05.02.44.34; Thu, 05 Jan 2023 02:44:41 -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=wE2fq0It; dkim=neutral (no key) header.i=@linutronix.de header.b=NwCHHW9S; 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 S232115AbjAEKjL (ORCPT + 55 others); Thu, 5 Jan 2023 05:39:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233076AbjAEKiU (ORCPT ); Thu, 5 Jan 2023 05:38:20 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00F9D3F100 for ; Thu, 5 Jan 2023 02:38:18 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1672915097; 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=DFngVp9MQgIEtplZg2ethuU38QZaV6s9fRh+JEWhx2k=; b=wE2fq0ItOYe/T63jHnr6N1j++OFCPzFcALhGwI/EE9JbgRzDtO3PTPJuBp8atCCQ0UxD3m 8EYGIZni4kWumz0580TFQX5hWUeQANCq0c4STJoFbWI7uKtuiIqtaOHRxsGLvy9wLzvblA lLZVIoCBeyog7iW9ojMXBn4DRrQJTp6TD/25/wMj6NfodF4EgQ0tJCG4M4OZMZRq3ZPcEK 9yKPqC80FTgepLyFscb5ChwmfH4uTeET9SoaRd1LHl/TrE6MELzpaCUaQ1eVIweGvcgOev Efs9GTblMD5yM4cfrDxDoeAJFR5rIDJPfC8hGyw4XAxJuUCaiQGbse4BYmW56A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1672915097; 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=DFngVp9MQgIEtplZg2ethuU38QZaV6s9fRh+JEWhx2k=; b=NwCHHW9SBE74wrBVwYMTUb/6Ahvf0gJUnQM7cIa7NMnMmIUiEFoxu596f84Op59if2f6hB SoATAqxLlcgzqmCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 7/8] printk: use printk_buffers for devkmsg Date: Thu, 5 Jan 2023 11:43:34 +0106 Message-Id: <20230105103735.880956-8-john.ogness@linutronix.de> In-Reply-To: <20230105103735.880956-1-john.ogness@linutronix.de> References: <20230105103735.880956-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Replace the individual @text_buf and @buf buffers in struct devkmsg_user with a struct printk_buffers. This reduces the number of buffers to keep track of. Also, since @text_buf is only used for ringbuffer reading and @buf is only used for output, it maps perfectly to the purpose and fields of struct printk_buffers. 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 | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 4fb7d29fb05d..e28a3736a399 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -695,15 +695,16 @@ 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_buffers pbufs; struct printk_info info; - char text_buf[CONSOLE_EXT_LOG_MAX]; struct printk_record record; }; @@ -787,7 +788,10 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, { 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 +801,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)) { if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; @@ -820,30 +824,25 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf, 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 +936,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; -- 2.30.2