Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

zram: add size class equals check into recompression

It makes no sense for us to recompress the object if it will be in the
same size class. We anyway don't get any memory gain. But, at the same
time, we get a CPU time overhead when inserting this object into zspage
and decompressing it afterwards.

[senozhatsky: rebased and fixed conflicts]
Link: https://lkml.kernel.org/r/20221109115047.2921851-9-senozhatsky@chromium.org
Signed-off-by: Alexey Romanov <avromanov@sberdevices.ru>
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Nitin Gupta <ngupta@vflare.org>
Cc: Suleiman Souhlal <suleiman@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alexey Romanov and committed by
Andrew Morton
7c2af309 f24ee92c

+33 -1
+10 -1
drivers/block/zram/zram_drv.c
··· 1672 1672 unsigned long handle_new; 1673 1673 unsigned int comp_len_old; 1674 1674 unsigned int comp_len_new; 1675 + unsigned int class_index_old; 1676 + unsigned int class_index_new; 1675 1677 void *src, *dst; 1676 1678 int ret; 1677 1679 ··· 1692 1690 if (ret) 1693 1691 return ret; 1694 1692 1693 + class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old); 1695 1694 /* 1696 1695 * Iterate the secondary comp algorithms list (in order of priority) 1697 1696 * and try to recompress the page. ··· 1718 1715 return ret; 1719 1716 } 1720 1717 1718 + class_index_new = zs_lookup_class_index(zram->mem_pool, 1719 + comp_len_new); 1720 + 1721 1721 /* Continue until we make progress */ 1722 1722 if (comp_len_new >= huge_class_size || 1723 1723 comp_len_new >= comp_len_old || 1724 + class_index_new >= class_index_old || 1724 1725 (threshold && comp_len_new >= threshold)) { 1725 1726 zcomp_stream_put(zram->comps[prio]); 1726 1727 continue; ··· 1747 1740 * that would save memory, mark the object as incompressible so that 1748 1741 * we will not try to compress it again. 1749 1742 */ 1750 - if (comp_len_new >= huge_class_size || comp_len_new >= comp_len_old) { 1743 + if (comp_len_new >= huge_class_size || 1744 + comp_len_new >= comp_len_old || 1745 + class_index_new >= class_index_old) { 1751 1746 zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE); 1752 1747 return 0; 1753 1748 }
+2
include/linux/zsmalloc.h
··· 55 55 unsigned long zs_get_total_pages(struct zs_pool *pool); 56 56 unsigned long zs_compact(struct zs_pool *pool); 57 57 58 + unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size); 59 + 58 60 void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats); 59 61 #endif
+21
mm/zsmalloc.c
··· 1205 1205 return get_zspage_inuse(zspage) == class->objs_per_zspage; 1206 1206 } 1207 1207 1208 + /** 1209 + * zs_lookup_class_index() - Returns index of the zsmalloc &size_class 1210 + * that hold objects of the provided size. 1211 + * @pool: zsmalloc pool to use 1212 + * @size: object size 1213 + * 1214 + * Context: Any context. 1215 + * 1216 + * Return: the index of the zsmalloc &size_class that hold objects of the 1217 + * provided size. 1218 + */ 1219 + unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size) 1220 + { 1221 + struct size_class *class; 1222 + 1223 + class = pool->size_class[get_size_class_index(size)]; 1224 + 1225 + return class->index; 1226 + } 1227 + EXPORT_SYMBOL_GPL(zs_lookup_class_index); 1228 + 1208 1229 unsigned long zs_get_total_pages(struct zs_pool *pool) 1209 1230 { 1210 1231 return atomic_long_read(&pool->pages_allocated);