Skip to content

Commit

Permalink
zram: do not waste zram_table_entry flags bits
Browse files Browse the repository at this point in the history
zram_table_entry::flags stores object size in the lower bits and zram
pageflags in the upper bits.  However, for some reason, we use 24 lower
bits, while maximum zram object size is PAGE_SIZE, which requires
PAGE_SHIFT bits (up to 16 on arm64).  This wastes 24 - PAGE_SHIFT bits
that we can use for additional zram pageflags instead.

Also add a BUILD_BUG_ON() to alert us should we run out of bits in
zram_table_entry::flags.

Link: https://lkml.kernel.org/r/20220912152744.527438-1-senozhatsky@chromium.org
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reviewed-by: Brian Geffon <bgeffon@google.com>
Acked-by: Minchan Kim <minchan@kernel.org>
Cc: Nitin Gupta <ngupta@vflare.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
sergey-senozhatsky authored and akpm00 committed Oct 3, 2022
1 parent a187094 commit f635725
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 2 additions & 0 deletions drivers/block/zram/zram_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,8 @@ static int __init zram_init(void)
{
int ret;

BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > BITS_PER_LONG);

ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
zcomp_cpu_up_prepare, zcomp_cpu_dead);
if (ret < 0)
Expand Down
15 changes: 7 additions & 8 deletions drivers/block/zram/zram_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@


/*
* The lower ZRAM_FLAG_SHIFT bits of table.flags is for
* object size (excluding header), the higher bits is for
* zram_pageflags.
* ZRAM is mainly used for memory efficiency so we want to keep memory
* footprint small and thus squeeze size and zram pageflags into a flags
* member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding
* header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT
* bits), the higher bits are for zram_pageflags.
*
* zram is mainly used for memory efficiency so we want to keep memory
* footprint small so we can squeeze size and flags into a field.
* The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
* the higher bits is for zram_pageflags.
* We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.
*/
#define ZRAM_FLAG_SHIFT 24
#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)

/* Flags for zram pages (table[page_no].flags) */
enum zram_pageflags {
Expand Down

0 comments on commit f635725

Please sign in to comment.