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=1796387982; b=oAgcidpU8+uUAhsoPA1pZm12tRF6UKty0wsiHlji553Fzqp2D2yEt6gt8P5CyfDhAw8/ x7Gyivf1CuMziJRO3RR8SLnnbGDCsJ4GnayUa7EGd/RlvLXkhQiEt5EYKiWpbg0Byb87E oRtvyeSPd8B2prMFdsy2YBJwA7LuIZ+oE9qXIhkUsTqZqwCuo/UJ7eC7ohfnL3Fg78neo fozrm+Z+5FMsOnq1rWzTQmegrA5yBLYTTWDGQW6lAINQjbxgr9PPpJqahwilT706hq6Jt lEtZ8XFQgUoUH+FTRxVyNmlBLda1ishF/p9nzQkxwv//l6NOLqPMLl5rzlfP6vGxS/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387982; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=6PJCTer4zsZ7ZJevOE7uPkngR8ICN6ALkjWNcrgftzc=; b=Gib3N0CfO0jXPX6ygsrgHtVois6LsucLw9G5ioETXClVvgfPUu7n3PxBdngbyuSI2vry 20411MSEDIHwZ+USYoJV318FEz55zg3udem3pAzDbZlk2GoQ/4k9QWIFz/NF7ZnqVXVia 4ceYqbl4IQHOP80LqHo7rdOBDQ0xy74e/o2xnphLb6kmNLg2K7jptKftkVM+CtWMeXdZf LdBgTUbYCWgci1t81khpF8TKnN3+FGhV1PCV5qNuo8CA68vUK3edRh0e/HEaFgPvArsMP kvj+dvCrVh+xRMWy89h3xQFdDirs5AeL8dmM01M9XCSiGPzUs6ZsoxSVb9Rb3jf7w8Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=1OBYLKFv; 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 57928606A322; Fri, 4 Dec 2026 12:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=1OBYLKFv Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC0C21F0092C; Fri, 4 Dec 2026 12:39:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387982; bh=6PJCTer4zsZ7ZJevOE7uPkngR8ICN6ALkjWNcrgftzc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=1OBYLKFveAlKNOAGRK/dStAyFz3HLdDbTH3yE60qQyD8lAr2TNZJQvstpht9pWs4c rviBaetHSlz2sg9vlT3atDieIfpT6Nrjwl5cdbcg89W/HLum7vVx9vIhFCdAtFi5/d juYshSUhhoV/ZsqVguwY0EBoB7qw/3hf9xFCBkTg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Pooyan Azad , Andrew Morton , Sergey Senozhatsky , Minchan Kim , Jens Axboe , Sasha Levin Subject: [PATCH 6.1 0437/1059] zram: fix short reads from block_state Date: Fri, 4 Dec 2026 18:59:28 +0900 Message-ID: <20261204100655.637076022@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Pooyan Azad [ Upstream commit 372012202260db088b489ed00059c4571a602552 ] read_block_state() formats each entry directly into the buffer supplied by read(). If the remaining buffer is too small for one complete record, snprintf() returns the full record length and the function stops without copying data or advancing the file position. A read from this debugfs file which is smaller than a record therefore returns zero at a non-EOF position and cannot make progress. Convert block_state to seq_file so formatted records are buffered independently of the userspace read size. Keep dev_lock held across each seq_file iteration and continue to protect individual entries with their slot locks. Link: https://lore.kernel.org/20260929071846.24829-1-pooyan.azadparvar@gmail.com Fixes: c0265342bff4 ("zram: introduce zram memory tracking") Signed-off-by: Pooyan Azad Signed-off-by: Andrew Morton Closes: https://lore.kernel.org/r/CANC3H+LdtoydSp+o2ecErAw7k6R2+gRf9LyxcaoHv_mGhJmyQQ@mail.gmail.com/ Reviewed-by: Sergey Senozhatsky Tested-by: Sergey Senozhatsky Cc: Minchan Kim Cc: Jens Axboe [ sashal: Converted block_state to a seq_file using this tree's init_lock, zram_* slot helpers, ktime_t access time and four flag characters, instead of upstream's names, u32 time and six flags. ] Signed-off-by: Sasha Levin --- drivers/block/zram/zram_drv.c | 100 +++++++++++++++++----------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index b8a2cb2f901cf..ef2f6687eeeea 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -904,69 +905,70 @@ static void zram_accessed(struct zram *zram, u32 index) zram->table[index].ac_time = ktime_get_boottime(); } -static ssize_t read_block_state(struct file *file, char __user *buf, - size_t count, loff_t *ppos) +static void *zram_block_state_start(struct seq_file *seq, loff_t *pos) { - char *kbuf; - ssize_t index, written = 0; - struct zram *zram = file->private_data; + struct zram *zram = seq->private; unsigned long nr_pages; - kbuf = kvmalloc(count, GFP_KERNEL); - if (!kbuf) - return -ENOMEM; - down_read(&zram->init_lock); - if (!init_done(zram)) { - up_read(&zram->init_lock); - kvfree(kbuf); - return -EINVAL; - } + if (!init_done(zram)) + return ERR_PTR(-EINVAL); nr_pages = zram->disksize >> PAGE_SHIFT; + if (*pos >= nr_pages) + return NULL; - for (index = *ppos; index < nr_pages; index++) { - int copied; - struct timespec64 ts; + return pos; +} - zram_slot_lock(zram, index); - if (!zram_allocated(zram, index)) - goto next; +static void *zram_block_state_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct zram *zram = seq->private; + unsigned long nr_pages = zram->disksize >> PAGE_SHIFT; - ts = ktime_to_timespec64(zram->table[index].ac_time); - copied = snprintf(kbuf + written, count, - "%12zd %12lld.%06lu %c%c%c%c\n", - index, (s64)ts.tv_sec, - ts.tv_nsec / NSEC_PER_USEC, - zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.', - zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.', - zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.', - zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.'); - - if (count <= copied) { - zram_slot_unlock(zram, index); - break; - } - written += copied; - count -= copied; -next: - zram_slot_unlock(zram, index); - *ppos += 1; - } + ++*pos; + if (*pos >= nr_pages) + return NULL; + + return pos; +} + +static void zram_block_state_stop(struct seq_file *seq, void *v) +{ + struct zram *zram = seq->private; up_read(&zram->init_lock); - if (copy_to_user(buf, kbuf, written)) - written = -EFAULT; - kvfree(kbuf); +} - return written; +static int zram_block_state_show(struct seq_file *seq, void *v) +{ + struct zram *zram = seq->private; + unsigned long index = *(loff_t *)v; + struct timespec64 ts; + + zram_slot_lock(zram, index); + if (zram_allocated(zram, index)) { + ts = ktime_to_timespec64(zram->table[index].ac_time); + seq_printf(seq, "%12lu %12lld.%06lu %c%c%c%c\n", + index, (s64)ts.tv_sec, + ts.tv_nsec / NSEC_PER_USEC, + zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.', + zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.', + zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.', + zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.'); + } + zram_slot_unlock(zram, index); + + return 0; } -static const struct file_operations proc_zram_block_state_op = { - .open = simple_open, - .read = read_block_state, - .llseek = default_llseek, +static const struct seq_operations zram_block_state_sops = { + .start = zram_block_state_start, + .next = zram_block_state_next, + .stop = zram_block_state_stop, + .show = zram_block_state_show, }; +DEFINE_SEQ_ATTRIBUTE(zram_block_state); static void zram_debugfs_register(struct zram *zram) { @@ -976,7 +978,7 @@ static void zram_debugfs_register(struct zram *zram) zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name, zram_debugfs_root); debugfs_create_file("block_state", 0400, zram->debugfs_dir, - zram, &proc_zram_block_state_op); + zram, &zram_block_state_fops); } static void zram_debugfs_unregister(struct zram *zram) -- 2.53.0