From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793631153; b=h+uEo59hTNSwui6GVUcoFaGaitnsly433/1eAdDzYwVuJGFLgiSKAAfsGWkdAFrgeBxP djtGLW7xR0yJzOuPuF20DX3uYO0u2Jzef7Q8kGFovogwGCME/v07RH6vQoiKB/rYgTY4z l8Xe+d8TKoqtGtYEhhF3sjRwzZlbD0jj/E80e7rXGV5DTNyD6o+SpY8TDYmp/fSMSiWdh /Fr16PAiINfk+UwsK/lVchCONMPFvy5aV63DDEh/DtfvcjS2O7/qhN/sKA3fTqRTL1neq 5xXyWRDRtqG1oh8mmkv8RmpKMJftkdoEA2/K/bKoN67xySTTytnMxYznytXyMVF8nPg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793631153; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=t7CXhIggcklIY3xO5IiO5lA9eZblBCQpzIpV4iqWwIs=; b=pRQpZZ7poRqIHSTWwHC1vApkIGS/GMSxTMaOJcZRMs6qmHsqzcXMxeDhEjsfgn/MpzoZ du3gzB4GwEw9pb2RToGEehM84fNj21a9qDbv+0tMtPMTYsmdTrSP5YdWGwRYjg7oZsTYm 3zLYhi5zgxZlAzFgqrdDyr2/Nvlfj8PD/iquEgvPw6qu+rxtYgViHiMPk3msBv8qv8GEF MbV/B1oZDcy1bftt63W0OXqWTNVNlHPL2+FuI0r6AIj2C1pdvRbN4NKGkyCRSGVdM3kJK iDEddYpMhi8KwT1rNKA/FQtwzWX7wL6Spm8sYm2w/AI6tzFT0RmW9WB8OrXhyqHUI7Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gkL6dVH3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B488B606A32F; Mon, 2 Nov 2026 14:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=gkL6dVH3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 943F01F00A18; Mon, 2 Nov 2026 14:52:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793631152; bh=t7CXhIggcklIY3xO5IiO5lA9eZblBCQpzIpV4iqWwIs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=gkL6dVH3qeZTZTEUa5gJkjb3DNPVL8n5WP7/jOyBtnyh2kByZfYEWHf2I4V1dxI/p MRJ7Cw4L3gZu5jux1vQS4dO9q+KH6eZstOXSKpu3hAjg1a2jYzTILzigEgrHxnw7a+ i+t8SHRQhy99x5vtc69Wd2RooVfSrGTh0BlmU//CeRIHSUWS8Rx6aYeOu7JZwe1YT/ PvpClFllL3x0bHydSz1hKBiSwFXahsk/lqsUAX5OXvNypfMaPi/I/xtwDZZXht4OD1 TjstHlA2Hp9ENKs7XrHGkLPp/n8NK6zuyN2Fg1CTZaD1iWdK7fJJWkqOfluwSEGTxl kopkZdZ+WBiqA== From: Christian Brauner Date: Mon, 02 Nov 2026 15:48:49 +0100 Subject: [PATCH RFC 09/16] seq_file: add seq_preallocate() X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261102-work-procfs-core-v1-9-75df35e4f8b8@kernel.org> References: <20261102-work-procfs-core-v1-0-75df35e4f8b8@kernel.org> In-Reply-To: <20261102-work-procfs-core-v1-0-75df35e4f8b8@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Jann Horn , Josef Bacik , Chris Mason , Jeff Layton , NeilBrown , Eric Dumazet , Thomas Gleixner , Al Viro , Jonathan Corbet , Christoph Hellwig , Greg Kroah-Hartman , Joel Granados , Kees Cook , Jan Kara , Jan Kara , linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-acpi@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org, linux-um@lists.infradead.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, netdev@vger.kernel.org, ntfs3@lists.linux.dev, platform-driver-x86@vger.kernel.org, coreteam@netfilter.org, kexec@lists.infradead.org, linux-pci@vger.kernel.org, linux-sound@vger.kernel.org, netfilter-devel@vger.kernel.org, dri-devel@lists.freedesktop.org, ibm-acpi-devel@lists.sourceforge.net, jfs-discussion@lists.sourceforge.net, linux-alpha@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-cifs@vger.kernel.org, linux-fbdev@vger.kernel.org, linux-hwmon@vger.kernel.org, linux-mm@kvack.org, linux-sh@vger.kernel.org, linux-usb@vger.kernel.org, samba-technical@lists.samba.org, sparclinux@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-doc@vger.kernel.org, linux-serial@vger.kernel.org, linux-atm-general@lists.sourceforge.net, Takashi Iwai , linux-input@vger.kernel.org, linux-raid@vger.kernel.org, linux-ext4@vger.kernel.org, linux-modules@vger.kernel.org, linux-debuggers@vger.kernel.org, "Christian Brauner (Amutable)" , Chuck Lever X-Mailer: b4 0.17-dev single_open_size() allocates the buffer itself because it does the single_open() too. A file whose seq_file is opened by somebody else, like procfs does now, needs to do that after the fact. Add seq_preallocate() for that and use it in single_open_size(). No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/seq_file.c | 36 ++++++++++++++++++++++++++++-------- include/linux/seq_file.h | 1 + 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/fs/seq_file.c b/fs/seq_file.c index 456c78719fd0..a4644cdda7e6 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -590,21 +590,41 @@ int single_open(struct file *file, int (*show)(struct seq_file *, void *), } EXPORT_SYMBOL(single_open); -int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), - void *data, size_t size) +/** + * seq_preallocate - start out with a buffer of @size + * @m: the seq_file, before it is first read + * @size: the size of the buffer + * + * For files whose records don't fit into a page, so that the first read + * doesn't have to work its way up to the right buffer size. + */ +int seq_preallocate(struct seq_file *m, size_t size) { char *buf = seq_buf_alloc(size); - int ret; + if (!buf) return -ENOMEM; + kvfree(m->buf); + m->buf = buf; + m->size = size; + return 0; +} +EXPORT_SYMBOL(seq_preallocate); + +int single_open_size(struct file *file, int (*show)(struct seq_file *, void *), + void *data, size_t size) +{ + int ret; + ret = single_open(file, show, data); - if (ret) { - kvfree(buf); + if (ret) return ret; + ret = seq_preallocate(file->private_data, size); + if (ret) { + single_release(file_inode(file), file); + file->private_data = NULL; } - ((struct seq_file *)file->private_data)->buf = buf; - ((struct seq_file *)file->private_data)->size = size; - return 0; + return ret; } EXPORT_SYMBOL(single_open_size); diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index b813b7e63d89..834daff48b7d 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -175,6 +175,7 @@ int seq_path_root(struct seq_file *m, const struct path *path, void *single_start(struct seq_file *, loff_t *); int single_open(struct file *, int (*)(struct seq_file *, void *), void *); int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t); +int seq_preallocate(struct seq_file *m, size_t size); int single_release(struct inode *, struct file *); void *__seq_open_private(struct file *, const struct seq_operations *, int); int seq_open_private(struct file *, const struct seq_operations *, int); -- 2.53.0