From: Sergey Senozhatsky <senozhatsky@chromium.org>
To: Andrew Morton <akpm@linux-foundation.org>,
Richard Chang <richardycc@google.com>,
Minchan Kim <minchan@kernel.org>
Cc: Brian Geffon <bgeffon@google.com>,
David Stevens <stevensd@google.com>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-block@vger.kernel.org,
Sergey Senozhatsky <senozhatsky@chromium.org>
Subject: [PATCHv2 2/7] zram: introduce writeback_compressed device attribute
Date: Mon, 1 Dec 2025 18:47:49 +0900 [thread overview]
Message-ID: <20251201094754.4149975-3-senozhatsky@chromium.org> (raw)
In-Reply-To: <20251201094754.4149975-1-senozhatsky@chromium.org>
From: Richard Chang <richardycc@google.com>
Introduce witeback_compressed device attribute to toggle
compressed writeback (decompression on demand) feature.
[senozhatsky: rewrote original patch, added documentation]
Signed-off-by: Richard Chang <richardycc@google.com>
Co-developed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
---
Documentation/ABI/testing/sysfs-block-zram | 7 ++++
Documentation/admin-guide/blockdev/zram.rst | 13 +++++++
drivers/block/zram/zram_drv.c | 38 +++++++++++++++++++++
3 files changed, 58 insertions(+)
diff --git a/Documentation/ABI/testing/sysfs-block-zram b/Documentation/ABI/testing/sysfs-block-zram
index 36c57de0a10a..ed10c2e4b5c2 100644
--- a/Documentation/ABI/testing/sysfs-block-zram
+++ b/Documentation/ABI/testing/sysfs-block-zram
@@ -150,3 +150,10 @@ Contact: Sergey Senozhatsky <senozhatsky@chromium.org>
Description:
The algorithm_params file is write-only and is used to setup
compression algorithm parameters.
+
+What: /sys/block/zram<id>/writeback_compressed
+Date: Decemeber 2025
+Contact: Richard Chang <richardycc@google.com>
+Description:
+ The writeback_compressed device atrribute toggles compressed
+ writeback feature.
diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst
index 3e273c1bb749..9547e4e95979 100644
--- a/Documentation/admin-guide/blockdev/zram.rst
+++ b/Documentation/admin-guide/blockdev/zram.rst
@@ -214,6 +214,7 @@ mem_limit WO specifies the maximum amount of memory ZRAM can
writeback_limit WO specifies the maximum amount of write IO zram
can write out to backing device as 4KB unit
writeback_limit_enable RW show and set writeback_limit feature
+writeback_compressed RW show and set compressed writeback feature
comp_algorithm RW show and change the compression algorithm
algorithm_params WO setup compression algorithm parameters
compact WO trigger memory compaction
@@ -434,6 +435,18 @@ system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of
writeback happened until you reset the zram to allocate extra writeback
budget in next setting is user's job.
+By default zram stores written back pages in decompressed (raw) form, which
+means that writeback operation involves decompression of the page before
+writing it to the backing device. This behavior can be changed by enabling
+`writeback_compressed` feature, which causes zram to write compressed pages
+to the backing device, thus avoiding decompression overhead. To enable
+this feature, execute::
+
+ $ echo yes > /sys/block/zramX/writeback_compressed
+
+Note that this feature should be configured before the `zramX` device is
+initialized.
+
If admin wants to measure writeback count in a certain period, they could
know it via /sys/block/zram0/bd_stat's 3rd column.
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 6263d300312e..3cc03c3f7389 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -539,6 +539,42 @@ struct zram_rb_req {
u32 index;
};
+static ssize_t writeback_compressed_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len)
+{
+ struct zram *zram = dev_to_zram(dev);
+ bool val;
+
+ if (kstrtobool(buf, &val))
+ return -EINVAL;
+
+ down_write(&zram->init_lock);
+ if (init_done(zram)) {
+ up_write(&zram->init_lock);
+ return -EBUSY;
+ }
+
+ zram->wb_compressed = val;
+ up_write(&zram->init_lock);
+
+ return len;
+}
+
+static ssize_t writeback_compressed_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ bool val;
+ struct zram *zram = dev_to_zram(dev);
+
+ down_read(&zram->init_lock);
+ val = zram->wb_compressed;
+ up_read(&zram->init_lock);
+
+ return sysfs_emit(buf, "%d\n", val);
+}
+
static ssize_t writeback_limit_enable_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
@@ -3048,6 +3084,7 @@ static DEVICE_ATTR_WO(writeback);
static DEVICE_ATTR_RW(writeback_limit);
static DEVICE_ATTR_RW(writeback_limit_enable);
static DEVICE_ATTR_RW(writeback_batch_size);
+static DEVICE_ATTR_RW(writeback_compressed);
#endif
#ifdef CONFIG_ZRAM_MULTI_COMP
static DEVICE_ATTR_RW(recomp_algorithm);
@@ -3070,6 +3107,7 @@ static struct attribute *zram_disk_attrs[] = {
&dev_attr_writeback_limit.attr,
&dev_attr_writeback_limit_enable.attr,
&dev_attr_writeback_batch_size.attr,
+ &dev_attr_writeback_compressed.attr,
#endif
&dev_attr_io_stat.attr,
&dev_attr_mm_stat.attr,
--
2.52.0.487.g5c8c507ade-goog
next prev parent reply other threads:[~2025-12-01 9:48 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-01 9:47 [PATCHv2 0/7] zram: introduce compressed data writeback Sergey Senozhatsky
2025-12-01 9:47 ` [PATCHv2 1/7] " Sergey Senozhatsky
2025-12-01 9:47 ` Sergey Senozhatsky [this message]
2025-12-01 9:47 ` [PATCHv2 3/7] zram: document writeback_batch_size Sergey Senozhatsky
2025-12-01 9:47 ` [PATCHv2 4/7] zram: move bd_stat to writeback section Sergey Senozhatsky
2025-12-01 9:47 ` [PATCHv2 5/7] zram: rename zram_free_page() Sergey Senozhatsky
2025-12-01 9:47 ` [PATCHv2 6/7] zram: switch to guard() for init_lock Sergey Senozhatsky
2025-12-01 9:47 ` [PATCHv2 7/7] zram: consolidate device-attr declarations Sergey Senozhatsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251201094754.4149975-3-senozhatsky@chromium.org \
--to=senozhatsky@chromium.org \
--cc=akpm@linux-foundation.org \
--cc=bgeffon@google.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=minchan@kernel.org \
--cc=richardycc@google.com \
--cc=stevensd@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox