Skip to contentSkip to conversation

Introduce per-cgroup compression priority

RFC PATCH In review jinji zhong 6 participants 10 messages Oct 26, 2025 – Oct 30, 2025

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover Introduce per-cgroup compression priority

Hello everyone,

On Android, different applications have varying tolerance for decompression latency. Applications with higher tolerance for decompression latency are better suited for algorithms like ZSTD, which provides high compression ratio but slower decompression speed. Conversely, applications with lower tolerance for decompression latency can use algorithms like LZ4 or LZO that offer faster decompression but lower compression ratios. For example, lightweight applications (with few anonymous pages) or applications without foreground UI typically have higher tolerance for decompression latency.

Similarly, in memory allocation slow paths or under high CPU pressure, using algorithms with faster compression speeds might be more appropriate.

This patch introduces a per-cgroup compression priority mechanism, where different compression priorities map to different algorithms. This allows administrators to select appropriate compression algorithms on a per-cgroup basis.

Currently, this patch is experimental and we would greatly appreciate community feedback. I'm uncertain whether obtaining compression priority via get_cgroup_comp_priority in zram is the best approach. While this implementation is convenient, it seems somewhat unusual. Perhaps the next step should be to pass compression priority through page->private.

jinji zhong (3):
mm/memcontrol: Introduce per-cgroup compression priority
zram: Zram supports per-cgroup compression priority
Doc: Update documentation for per-cgroup compression priority

 Documentation/admin-guide/blockdev/zram.rst | 18 +++--
 Documentation/admin-guide/cgroup-v2.rst     |  7 ++
 drivers/block/zram/zram_drv.c               | 74 ++++++++++++++++++---
 drivers/block/zram/zram_drv.h               |  2 +
 include/linux/memcontrol.h                  | 19 ++++++
 mm/memcontrol.c                             | 31 +++++++++
 6 files changed, 139 insertions(+), 12 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

RFC PATCH 1/3 mm/memcontrol: Introduce per-cgroup compression priority

On Android, applications have varying tolerance for decompression speed. Background and lightweight applications tolerate slower decompression better than large, foreground applications. They are suitable for algorithms like ZSTD, which has a high compression ratio but slower decompression. Other applications may prefer algorithms with faster decompression.

This patch introduces a per-cgroup compression priority mechanism. Different compression priorities map to different algorithms. This allows administrators to select the appropriate compression algorithm on a per-cgroup basis.

---
 include/linux/memcontrol.h | 19 +++++++++++++++++++
 mm/memcontrol.c            | 31 +++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
include/linux/memcontrol.h +19 -0
index 873e510d6f8d..a91670b8c469 100644
@@ -228,6 +228,9 @@ struct mem_cgroup {
 
 	int swappiness;
 
+	/* The priority of the compression algorithm used by the cgroup. */
+	int comp_priority;
+
 	/* memory.events and memory.events.local */
 	struct cgroup_file events_file;
 	struct cgroup_file events_local_file;
@@ -523,6 +526,22 @@ static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *ob
 	return memcg;
 }
 
+#define DEF_COMP_PRIORITY 0
+
+/*
+* get_cgroup_comp_priority - Get the compression priority of the memcg
+* @page: Pointer to the page.
+* Returns the compression priority of the memcg the page belongs to.
+*/
+static inline int get_cgroup_comp_priority(struct page *page)
+{
+	struct mem_cgroup *memcg = folio_memcg(page_folio(page));
+	if (!memcg)
+		return DEF_COMP_PRIORITY;
+
+	return memcg->comp_priority;
+}
+
 /*
  * folio_memcg_kmem - Check if the folio has the memcg_kmem flag set.
  * @folio: Pointer to the folio.
mm/memcontrol.c +31 -0
index 4deda33625f4..436cbc8ddcc2 100644
@@ -5356,6 +5356,31 @@ static int swap_events_show(struct seq_file *m, void *v)
 	return 0;
 }
 
+static int swap_comp_priority_show(struct seq_file *m, void *v)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+
+	seq_printf(m, "%d\n", READ_ONCE(memcg->comp_priority));
+	return 0;
+}
+
+static ssize_t swap_comp_priority_write(struct kernfs_open_file *of,
+					  char *buf, size_t nbytes, loff_t off)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
+	int comp_priority;
+	ssize_t parse_ret = kstrtoint(strstrip(buf), 10, &comp_priority);
+
+	if (parse_ret)
+		return parse_ret;
+
+	if (comp_priority < 0)
+		return -EINVAL;
+
+	WRITE_ONCE(memcg->comp_priority, comp_priority);
+	return nbytes;
+}
+
 static struct cftype swap_files[] = {
 	{
 		.name = "swap.current",
@@ -5388,6 +5413,12 @@ static struct cftype swap_files[] = {
 		.file_offset = offsetof(struct mem_cgroup, swap_events_file),
 		.seq_show = swap_events_show,
 	},
+	{
+		.name = "swap.comp_priority",
+		.flags = CFTYPE_NOT_ON_ROOT,
+		.seq_show = swap_comp_priority_show,
+		.write = swap_comp_priority_write,
+	},
 	{ }	/* terminate */
 };
 
Signature · 1 line
2.48.1
#2 lore raw

RFC PATCH 2/3 zram: Zram supports per-cgroup compression priority

In reply to jinji zhong

This patch allows zram to get the per-cgroup compression priority, enabling administrators to select different compression algorithms for different cgroups.

The feature is enabled by:
echo 1 > /sys/block/zramX/per_cgroup_comp_enable.

---
 drivers/block/zram/zram_drv.c | 74 +++++++++++++++++++++++++++++++----
 drivers/block/zram/zram_drv.h |  2 +
 2 files changed, 68 insertions(+), 8 deletions(-)
drivers/block/zram/zram_drv.c +66 -8
index a43074657531..da79034f2efa 100644
@@ -23,6 +23,7 @@
 #include <linux/buffer_head.h>
 #include <linux/device.h>
 #include <linux/highmem.h>
+#include <linux/memcontrol.h>
 #include <linux/slab.h>
 #include <linux/backing-dev.h>
 #include <linux/string.h>
@@ -1223,6 +1224,7 @@ static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
 		kfree(zram->comp_algs[prio]);
 
 	zram->comp_algs[prio] = alg;
+	zram->comp_algs_flag |= (1 << prio);
 }
 
 static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
@@ -1396,7 +1398,7 @@ static ssize_t comp_algorithm_store(struct device *dev,
 }
 
 #ifdef CONFIG_ZRAM_MULTI_COMP
-static ssize_t recomp_algorithm_show(struct device *dev,
+static ssize_t multi_comp_algorithm_show(struct device *dev,
 				     struct device_attribute *attr,
 				     char *buf)
 {
@@ -1405,7 +1407,7 @@ static ssize_t recomp_algorithm_show(struct device *dev,
 	u32 prio;
 
 	down_read(&zram->init_lock);
-	for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
+	for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
 		if (!zram->comp_algs[prio])
 			continue;
 
@@ -1416,7 +1418,7 @@ static ssize_t recomp_algorithm_show(struct device *dev,
 	return sz;
 }
 
-static ssize_t recomp_algorithm_store(struct device *dev,
+static ssize_t multi_comp_algorithm_store(struct device *dev,
 				      struct device_attribute *attr,
 				      const char *buf,
 				      size_t len)
@@ -1450,12 +1452,43 @@ static ssize_t recomp_algorithm_store(struct device *dev,
 	if (!alg)
 		return -EINVAL;
 
-	if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS)
+	if (prio < ZRAM_PRIMARY_COMP || prio >= ZRAM_MAX_COMPS)
 		return -EINVAL;
 
 	ret = __comp_algorithm_store(zram, prio, alg);
 	return ret ? ret : len;
 }
+
+static ssize_t per_cgroup_comp_enable_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t len)
+{
+	struct zram *zram = dev_to_zram(dev);
+	u64 val;
+	ssize_t ret = -EINVAL;
+
+	if (kstrtoull(buf, 10, &val))
+		return ret;
+
+	down_read(&zram->init_lock);
+	zram->per_cgroup_comp_enable = val;
+	up_read(&zram->init_lock);
+	ret = len;
+
+	return ret;
+}
+
+static ssize_t per_cgroup_comp_enable_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->per_cgroup_comp_enable;
+	up_read(&zram->init_lock);
+
+	return sysfs_emit(buf, "%d\n", val);
+}
 #endif
 
 static ssize_t compact_store(struct device *dev,
@@ -1840,9 +1873,30 @@ static int write_incompressible_page(struct zram *zram, struct page *page,
 	return 0;
 }
 
+static inline bool is_comp_priority_valid(struct zram *zram, int prio)
+{
+	return zram->comp_algs_flag & (1 << prio);
+}
+
+static inline int get_comp_priority(struct zram *zram, struct page *page)
+{
+	int prio;
+
+	if (!zram->per_cgroup_comp_enable)
+		return ZRAM_PRIMARY_COMP;
+
+	prio = get_cgroup_comp_priority(page);
+	if (unlikely(!is_comp_priority_valid(zram, prio))) {
+		WARN_ON_ONCE(1);
+		return ZRAM_PRIMARY_COMP;
+	}
+	return prio;
+}
+
 static int zram_write_page(struct zram *zram, struct page *page, u32 index)
 {
 	int ret = 0;
+	int prio;
 	unsigned long handle;
 	unsigned int comp_len;
 	void *mem;
@@ -1856,9 +1910,10 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
 	if (same_filled)
 		return write_same_filled_page(zram, element, index);
 
-	zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
+	prio = get_comp_priority(zram, page);
+	zstrm = zcomp_stream_get(zram->comps[prio]);
 	mem = kmap_local_page(page);
-	ret = zcomp_compress(zram->comps[ZRAM_PRIMARY_COMP], zstrm,
+	ret = zcomp_compress(zram->comps[prio], zstrm,
 			     mem, &comp_len);
 	kunmap_local(mem);
 
@@ -1894,6 +1949,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
 	zram_free_page(zram, index);
 	zram_set_handle(zram, index, handle);
 	zram_set_obj_size(zram, index, comp_len);
+	zram_set_priority(zram, index, prio);
 	zram_slot_unlock(zram, index);
 
 	/* Update stats */
@@ -2612,7 +2668,8 @@ static DEVICE_ATTR_RW(writeback_limit);
 static DEVICE_ATTR_RW(writeback_limit_enable);
 #endif
 #ifdef CONFIG_ZRAM_MULTI_COMP
-static DEVICE_ATTR_RW(recomp_algorithm);
+static DEVICE_ATTR_RW(multi_comp_algorithm);
+static DEVICE_ATTR_RW(per_cgroup_comp_enable);
 static DEVICE_ATTR_WO(recompress);
 #endif
 static DEVICE_ATTR_WO(algorithm_params);
@@ -2639,8 +2696,9 @@ static struct attribute *zram_disk_attrs[] = {
 #endif
 	&dev_attr_debug_stat.attr,
 #ifdef CONFIG_ZRAM_MULTI_COMP
-	&dev_attr_recomp_algorithm.attr,
+	&dev_attr_multi_comp_algorithm.attr,
 	&dev_attr_recompress.attr,
+	&dev_attr_per_cgroup_comp_enable.attr,
 #endif
 	&dev_attr_algorithm_params.attr,
 	NULL,
drivers/block/zram/zram_drv.h +2 -0
index 6cee93f9c0d0..34ae0c3a9130 100644
@@ -120,11 +120,13 @@ struct zram {
 	 */
 	u64 disksize;	/* bytes */
 	const char *comp_algs[ZRAM_MAX_COMPS];
+	u8 comp_algs_flag;
 	s8 num_active_comps;
 	/*
 	 * zram is claimed so open request will be failed
 	 */
 	bool claim; /* Protected by disk->open_mutex */
+	bool per_cgroup_comp_enable;
 #ifdef CONFIG_ZRAM_WRITEBACK
 	struct file *backing_dev;
 	spinlock_t wb_limit_lock;
Signature · 1 line
2.48.1
#3 lore raw

RFC PATCH 3/3 Doc: Update documentation for per-cgroup compression priority

In reply to jinji zhong

This patch updates the documentation, describing the newly introduced per-cgroup compression priority mechanism.

---
 Documentation/admin-guide/blockdev/zram.rst | 18 ++++++++++++++----
 Documentation/admin-guide/cgroup-v2.rst     |  7 +++++++
 2 files changed, 21 insertions(+), 4 deletions(-)
Documentation/admin-guide/blockdev/zram.rst +14 -4
index 3e273c1bb749..de4ab060f664 100644
@@ -452,12 +452,12 @@ using more effective algorithm and, hence, reduce zsmalloc memory usage.
 With CONFIG_ZRAM_MULTI_COMP, zram supports up to 4 compression algorithms:
 one primary and up to 3 secondary ones. Primary zram compressor is explained
 in "3) Select compression algorithm", secondary algorithms are configured
-using recomp_algorithm device attribute.
+using multi_comp_algorithm device attribute.
 
 Example:::
 
 	#show supported recompression algorithms
-	cat /sys/block/zramX/recomp_algorithm
+	cat /sys/block/zramX/multi_comp_algorithm
 	#1: lzo lzo-rle lz4 lz4hc [zstd]
 	#2: lzo lzo-rle lz4 [lz4hc] zstd
 
@@ -468,10 +468,10 @@ Alternative compression algorithm's priority is provided during algorithms
 configuration:::
 
 	#select zstd recompression algorithm, priority 1
-	echo "algo=zstd priority=1" > /sys/block/zramX/recomp_algorithm
+	echo "algo=zstd priority=1" > /sys/block/zramX/multi_comp_algorithm
 
 	#select deflate recompression algorithm, priority 2
-	echo "algo=deflate priority=2" > /sys/block/zramX/recomp_algorithm
+	echo "algo=deflate priority=2" > /sys/block/zramX/multi_comp_algorithm
 
 Another device attribute that CONFIG_ZRAM_MULTI_COMP enables is recompress,
 which controls recompression.
@@ -524,6 +524,16 @@ This can be achieved by providing a `algo` or `priority` parameter:::
 	#use zstd algorithm only (if zstd was registered under priority 1)
 	echo "type=huge priority=1" > /sys/block/zramX/recompress
 
+per-cgroup compression algorithms
+-------------
+With CONFIG_ZRAM_MULTI_COMP, zram can compress pages using the compression
+algorithm determined by the cgroup. It will get the compression priority from
+the cgroup and use the corresponding compression algorithm to compress the page.
+
+To use the feature, admin should enable per-cgroup compression via::
+
+	echo 1 > /sys/block/zramX/per_cgroup_comp_enable
+
 memory tracking
 ===============
 
Documentation/admin-guide/cgroup-v2.rst +7 -0
index 0e6c67ac585a..1706d8f0d225 100644
@@ -1871,6 +1871,13 @@ The following nested keys are defined.
 	higher than the limit for an extended period of time.  This
 	reduces the impact on the workload and memory management.
 
+  memory.swap.compress_priority
+	A read-write single value file which exists on non-root
+	cgroups.  The default is "0".
+
+	swap compress priority for the cgroup. Different compression
+	priorities mean different compression algorithms.
+
   memory.zswap.current
 	A read-only single value file which exists on non-root
 	cgroups.
Signature · 1 line
2.48.1
maintainer
#4 lore raw
In reply to jinji zhong

Hello,

Quoted 4 lines · jinji zhong

This patch introduces a per-cgroup compression priority mechanism, where different compression priorities map to different algorithms. This allows administrators to select appropriate compression algorithms on a per-cgroup basis.

I don't think it makes sense to tie this to cgroups. Is there something preventing this from following the process hierarchy?

Thanks.

Signature · 1 line
tejun
#5 lore raw
Quoted 11 lines

Hello,

Quoted 4 lines · jinji zhong

This patch introduces a per-cgroup compression priority mechanism, where different compression priorities map to different algorithms. This allows administrators to select appropriate compression algorithms on a per-cgroup basis.

I don't think it makes sense to tie this to cgroups. Is there something preventing this from following the process hierarchy? Thanks.

Hello, Tejun,

There is also a layer of page tables between the process and the page, so making it follow the process hierarchy would be complicated. But you make a good point; it may indeed be unnecessary to introduce a separate per-cgroup compression priority. As Nhat suggested, we could try reusing the per-cgroup swap priority.

Quoted 2 lines
Signature · 1 line
tejun
maintainer
#6 lore raw
In reply to jinji zhong

Hi Jinji,

Quoted 22 lines · jinji zhong

Hello everyone,

On Android, different applications have varying tolerance for decompression latency. Applications with higher tolerance for decompression latency are better suited for algorithms like ZSTD, which provides high compression ratio but slower decompression speed. Conversely, applications with lower tolerance for decompression latency can use algorithms like LZ4 or LZO that offer faster decompression but lower compression ratios. For example, lightweight applications (with few anonymous pages) or applications without foreground UI typically have higher tolerance for decompression latency.

Similarly, in memory allocation slow paths or under high CPU pressure, using algorithms with faster compression speeds might be more appropriate.

This patch introduces a per-cgroup compression priority mechanism, where different compression priorities map to different algorithms. This allows administrators to select appropriate compression algorithms on a per-cgroup basis.

Quoted 7 lines

Currently, this patch is experimental and we would greatly appreciate community feedback. I'm uncertain whether obtaining compression priority via get_cgroup_comp_priority in zram is the best approach. While this implementation is convenient, it seems somewhat unusual. Perhaps the next step should be to pass compression priority through page->private.

Setting aside the issues in the implementation (like changing compression algorithm of a cgroup while it already has some memory compressed using older algo), I don't think memcg interface is the right way to go about it. We usually add interfaces to memcg that have hierarchical semantics.

Anyways if you want to have this feature, I think BPF might be the way to get this flexibility without introducing any stable API and then you can experiment and evaluate if this really helps.

#7 lore raw
Quoted 33 lines

Hi Jinji,

Quoted 22 lines · jinji zhong

Hello everyone,

On Android, different applications have varying tolerance for decompression latency. Applications with higher tolerance for decompression latency are better suited for algorithms like ZSTD, which provides high compression ratio but slower decompression speed. Conversely, applications with lower tolerance for decompression latency can use algorithms like LZ4 or LZO that offer faster decompression but lower compression ratios. For example, lightweight applications (with few anonymous pages) or applications without foreground UI typically have higher tolerance for decompression latency.

Similarly, in memory allocation slow paths or under high CPU pressure, using algorithms with faster compression speeds might be more appropriate.

This patch introduces a per-cgroup compression priority mechanism, where different compression priorities map to different algorithms. This allows administrators to select appropriate compression algorithms on a per-cgroup basis.

Quoted 7 lines

Currently, this patch is experimental and we would greatly appreciate community feedback. I'm uncertain whether obtaining compression priority via get_cgroup_comp_priority in zram is the best approach. While this implementation is convenient, it seems somewhat unusual. Perhaps the next step should be to pass compression priority through page->private.

Quoted 2 lines

Setting aside the issues in the implementation (like changing compression algorithm of a cgroup while it already has some memory

Zram uses flags to track the compression priority of each page, which should be ok when the page is decompressed.

Quoted 3 lines

compressed using older algo), I don't think memcg interface is the right way to go about it. We usually add interfaces to memcg that have hierarchical semantics.

Thanks a lot, Shakeel. I got it.

Quoted 3 lines

Anyways if you want to have this feature, I think BPF might be the way to get this flexibility without introducing any stable API and then you can experiment and evaluate if this really helps.

#8 lore raw
In reply to jinji zhong
Quoted 23 lines · jinji zhong

Hello everyone,

On Android, different applications have varying tolerance for decompression latency. Applications with higher tolerance for decompression latency are better suited for algorithms like ZSTD, which provides high compression ratio but slower decompression speed. Conversely, applications with lower tolerance for decompression latency can use algorithms like LZ4 or LZO that offer faster decompression but lower compression ratios. For example, lightweight applications (with few anonymous pages) or applications without foreground UI typically have higher tolerance for decompression latency.

Similarly, in memory allocation slow paths or under high CPU pressure, using algorithms with faster compression speeds might be more appropriate.

This patch introduces a per-cgroup compression priority mechanism, where different compression priorities map to different algorithms. This allows administrators to select appropriate compression algorithms on a per-cgroup basis.

Quoted 6 lines

Currently, this patch is experimental and we would greatly appreciate community feedback. I'm uncertain whether obtaining compression priority via get_cgroup_comp_priority in zram is the best approach. While this implementation is convenient, it seems somewhat unusual. Perhaps the next step should be to pass compression priority through page->private.

I agree with TJ's and Shakeel's take on this. You (or some other zram/zswap users) will have to present a more compelling case for the necessity of a hierarchical structure for this property :)

The semantics itself is unclear to me - what's the default? How should inheritance be defined? What happens when cgroups are killed etc?

As a side note, seems like there is a proposal for swap device priority (+ Youngjun)

https://lore.kernel.org/all/[email protected]/

Is this something you can leverage?

Another alternative is to make this zram-internal, i.e add knobs to zram sysfs, or extend the recomp parameter. I'll defer to zram maintainers and users to comment on this :)

#9 lore raw
Quoted 3 lines · Nhat Pham

Another alternative is to make this zram-internal, i.e add knobs to zram sysfs, or extend the recomp parameter. I'll defer to zram maintainers and users to comment on this :)

I think this cannot be purely zram-internal, we'd need some "hint" from upper layers which process/cgroup each particular page belongs to and what's its priority.