Skip to content

Commit

Permalink
zram: add size class equals check into recompression
Browse files Browse the repository at this point in the history
Patch series "zsmalloc/zram: configurable zspage size", v4.

Some use-cases and/or data patterns may benefit from larger zspages. 
Currently the limit on the number of physical pages that are linked into a
zspage is hardcoded to 4.  Higher limit changes key characteristics of a
number of the size classes, improving compactness of the pool and redusing
the amount of memory zsmalloc pool uses.  More on this in 0002 commit
message.


This patch (of 9):

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.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Alexey Romanov <[email protected]>
Reviewed-by: Sergey Senozhatsky <[email protected]>
Signed-off-by: Sergey Senozhatsky <[email protected]>
Cc: Minchan Kim <[email protected]>
Cc: Nitin Gupta <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
mRrvz authored and akpm00 committed Nov 5, 2022
1 parent 5a01c73 commit f4a7b76
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions drivers/block/zram/zram_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page,
unsigned long handle_next;
unsigned int comp_len_next;
unsigned int comp_len_prev;
unsigned int class_index_prev;
unsigned int class_index_next;
struct zcomp_strm *zstrm;
void *src, *dst;
int ret;
Expand All @@ -1656,13 +1658,16 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page,
ret = zcomp_compress(zstrm, src, &comp_len_next);
kunmap_atomic(src);

class_index_prev = zs_lookup_class_index(zram->mem_pool, comp_len_prev);
class_index_next = zs_lookup_class_index(zram->mem_pool, comp_len_next);
/*
* Either a compression error or we failed to compressed the object
* in a way that will save us memory. Mark the object so that we
* don't attempt to re-compress it again (RECOMP_SKIP).
*/
if (comp_len_next >= huge_class_size ||
comp_len_next >= comp_len_prev ||
class_index_next >= class_index_prev ||
ret) {
zram_set_flag(zram, index, ZRAM_RECOMP_SKIP);
zram_clear_flag(zram, index, ZRAM_IDLE);
Expand Down
2 changes: 2 additions & 0 deletions include/linux/zsmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ void zs_unmap_object(struct zs_pool *pool, unsigned long handle);
unsigned long zs_get_total_pages(struct zs_pool *pool);
unsigned long zs_compact(struct zs_pool *pool);

unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size);

void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats);
#endif
21 changes: 21 additions & 0 deletions mm/zsmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,27 @@ static bool zspage_full(struct size_class *class, struct zspage *zspage)
return get_zspage_inuse(zspage) == class->objs_per_zspage;
}

/**
* zs_lookup_class_index() - Returns index of the zsmalloc &size_class
* that hold objects of the provided size.
* @pool: zsmalloc pool to use
* @size: object size
*
* Context: Any context.
*
* Return: the index of the zsmalloc &size_class that hold objects of the
* provided size.
*/
unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size)
{
struct size_class *class;

class = pool->size_class[get_size_class_index(size)];

return class->index;
}
EXPORT_SYMBOL_GPL(zs_lookup_class_index);

unsigned long zs_get_total_pages(struct zs_pool *pool)
{
return atomic_long_read(&pool->pages_allocated);
Expand Down

0 comments on commit f4a7b76

Please sign in to comment.