zram: use u32 for entry ac_time tracking

We can reduce sizeof(zram_table_entry) on 64-bit systems by converting
flags and ac_time to u32.  Entry flags fit into u32, and for ac_time u32
gives us over a century of entry lifespan (approx 136 years) which is
plenty (zram uses system boot time (seconds)).

In struct zram_table_entry we use bytes aliasing, because bit-wait API
(for slot lock) requires a whole unsigned long word.

Link: https://lkml.kernel.org/r/d7c0b48450c70eeb5fd8acd6ecd23593f30dbf1f.1765775954.git.senozhatsky@chromium.org
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Suggested-by: David Stevens <stevensd@google.com>
Cc: Brian Geffon <bgeffon@google.com>
Cc: Minchan Kim <minchan@google.com>
Cc: Richard Chang <richardycc@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Sergey Senozhatsky 2025-12-15 14:47:11 +09:00 committed by Andrew Morton
parent 0327a86213
commit 2e8ff2f51d
2 changed files with 37 additions and 32 deletions

View File

@ -81,7 +81,7 @@ static void zram_slot_lock_init(struct zram *zram, u32 index)
*/ */
static __must_check bool zram_slot_trylock(struct zram *zram, u32 index) static __must_check bool zram_slot_trylock(struct zram *zram, u32 index)
{ {
unsigned long *lock = &zram->table[index].flags; unsigned long *lock = &zram->table[index].__lock;
if (!test_and_set_bit_lock(ZRAM_ENTRY_LOCK, lock)) { if (!test_and_set_bit_lock(ZRAM_ENTRY_LOCK, lock)) {
mutex_acquire(slot_dep_map(zram, index), 0, 1, _RET_IP_); mutex_acquire(slot_dep_map(zram, index), 0, 1, _RET_IP_);
@ -94,7 +94,7 @@ static __must_check bool zram_slot_trylock(struct zram *zram, u32 index)
static void zram_slot_lock(struct zram *zram, u32 index) static void zram_slot_lock(struct zram *zram, u32 index)
{ {
unsigned long *lock = &zram->table[index].flags; unsigned long *lock = &zram->table[index].__lock;
mutex_acquire(slot_dep_map(zram, index), 0, 0, _RET_IP_); mutex_acquire(slot_dep_map(zram, index), 0, 0, _RET_IP_);
wait_on_bit_lock(lock, ZRAM_ENTRY_LOCK, TASK_UNINTERRUPTIBLE); wait_on_bit_lock(lock, ZRAM_ENTRY_LOCK, TASK_UNINTERRUPTIBLE);
@ -103,7 +103,7 @@ static void zram_slot_lock(struct zram *zram, u32 index)
static void zram_slot_unlock(struct zram *zram, u32 index) static void zram_slot_unlock(struct zram *zram, u32 index)
{ {
unsigned long *lock = &zram->table[index].flags; unsigned long *lock = &zram->table[index].__lock;
mutex_release(slot_dep_map(zram, index), _RET_IP_); mutex_release(slot_dep_map(zram, index), _RET_IP_);
clear_and_wake_up_bit(ZRAM_ENTRY_LOCK, lock); clear_and_wake_up_bit(ZRAM_ENTRY_LOCK, lock);
@ -130,34 +130,33 @@ static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
} }
static bool zram_test_flag(struct zram *zram, u32 index, static bool zram_test_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
return zram->table[index].flags & BIT(flag); return zram->table[index].attr.flags & BIT(flag);
} }
static void zram_set_flag(struct zram *zram, u32 index, static void zram_set_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
zram->table[index].flags |= BIT(flag); zram->table[index].attr.flags |= BIT(flag);
} }
static void zram_clear_flag(struct zram *zram, u32 index, static void zram_clear_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
zram->table[index].flags &= ~BIT(flag); zram->table[index].attr.flags &= ~BIT(flag);
} }
static size_t zram_get_obj_size(struct zram *zram, u32 index) static size_t zram_get_obj_size(struct zram *zram, u32 index)
{ {
return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1); return zram->table[index].attr.flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
} }
static void zram_set_obj_size(struct zram *zram, static void zram_set_obj_size(struct zram *zram, u32 index, size_t size)
u32 index, size_t size)
{ {
unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT; unsigned long flags = zram->table[index].attr.flags >> ZRAM_FLAG_SHIFT;
zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size; zram->table[index].attr.flags = (flags << ZRAM_FLAG_SHIFT) | size;
} }
static inline bool zram_allocated(struct zram *zram, u32 index) static inline bool zram_allocated(struct zram *zram, u32 index)
@ -208,14 +207,14 @@ static inline void zram_set_priority(struct zram *zram, u32 index, u32 prio)
* Clear previous priority value first, in case if we recompress * Clear previous priority value first, in case if we recompress
* further an already recompressed page * further an already recompressed page
*/ */
zram->table[index].flags &= ~(ZRAM_COMP_PRIORITY_MASK << zram->table[index].attr.flags &= ~(ZRAM_COMP_PRIORITY_MASK <<
ZRAM_COMP_PRIORITY_BIT1); ZRAM_COMP_PRIORITY_BIT1);
zram->table[index].flags |= (prio << ZRAM_COMP_PRIORITY_BIT1); zram->table[index].attr.flags |= (prio << ZRAM_COMP_PRIORITY_BIT1);
} }
static inline u32 zram_get_priority(struct zram *zram, u32 index) static inline u32 zram_get_priority(struct zram *zram, u32 index)
{ {
u32 prio = zram->table[index].flags >> ZRAM_COMP_PRIORITY_BIT1; u32 prio = zram->table[index].attr.flags >> ZRAM_COMP_PRIORITY_BIT1;
return prio & ZRAM_COMP_PRIORITY_MASK; return prio & ZRAM_COMP_PRIORITY_MASK;
} }
@ -225,7 +224,7 @@ static void zram_accessed(struct zram *zram, u32 index)
zram_clear_flag(zram, index, ZRAM_IDLE); zram_clear_flag(zram, index, ZRAM_IDLE);
zram_clear_flag(zram, index, ZRAM_PP_SLOT); zram_clear_flag(zram, index, ZRAM_PP_SLOT);
#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
zram->table[index].ac_time = ktime_get_boottime(); zram->table[index].attr.ac_time = (u32)ktime_get_boottime_seconds();
#endif #endif
} }
@ -447,7 +446,7 @@ static void mark_idle(struct zram *zram, ktime_t cutoff)
#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
is_idle = !cutoff || is_idle = !cutoff ||
ktime_after(cutoff, zram->table[index].ac_time); ktime_after(cutoff, zram->table[index].attr.ac_time);
#endif #endif
if (is_idle) if (is_idle)
zram_set_flag(zram, index, ZRAM_IDLE); zram_set_flag(zram, index, ZRAM_IDLE);
@ -461,18 +460,19 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t len) const char *buf, size_t len)
{ {
struct zram *zram = dev_to_zram(dev); struct zram *zram = dev_to_zram(dev);
ktime_t cutoff_time = 0; ktime_t cutoff = 0;
if (!sysfs_streq(buf, "all")) { if (!sysfs_streq(buf, "all")) {
/* /*
* If it did not parse as 'all' try to treat it as an integer * If it did not parse as 'all' try to treat it as an integer
* when we have memory tracking enabled. * when we have memory tracking enabled.
*/ */
u64 age_sec; u32 age_sec;
if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && !kstrtoull(buf, 0, &age_sec)) if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) &&
cutoff_time = ktime_sub(ktime_get_boottime(), !kstrtouint(buf, 0, &age_sec))
ns_to_ktime(age_sec * NSEC_PER_SEC)); cutoff = ktime_sub((u32)ktime_get_boottime_seconds(),
age_sec);
else else
return -EINVAL; return -EINVAL;
} }
@ -482,10 +482,10 @@ static ssize_t idle_store(struct device *dev, struct device_attribute *attr,
return -EINVAL; return -EINVAL;
/* /*
* A cutoff_time of 0 marks everything as idle, this is the * A cutoff of 0 marks everything as idle, this is the
* "all" behavior. * "all" behavior.
*/ */
mark_idle(zram, cutoff_time); mark_idle(zram, cutoff);
return len; return len;
} }
@ -1588,7 +1588,7 @@ static ssize_t read_block_state(struct file *file, char __user *buf,
if (!zram_allocated(zram, index)) if (!zram_allocated(zram, index))
goto next; goto next;
ts = ktime_to_timespec64(zram->table[index].ac_time); ts = ktime_to_timespec64(zram->table[index].attr.ac_time);
copied = snprintf(kbuf + written, count, copied = snprintf(kbuf + written, count,
"%12zd %12lld.%06lu %c%c%c%c%c%c\n", "%12zd %12lld.%06lu %c%c%c%c%c%c\n",
index, (s64)ts.tv_sec, index, (s64)ts.tv_sec,
@ -2013,7 +2013,7 @@ static void zram_slot_free(struct zram *zram, u32 index)
unsigned long handle; unsigned long handle;
#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
zram->table[index].ac_time = 0; zram->table[index].attr.ac_time = 0;
#endif #endif
zram_clear_flag(zram, index, ZRAM_IDLE); zram_clear_flag(zram, index, ZRAM_IDLE);
@ -3286,7 +3286,7 @@ static int __init zram_init(void)
struct zram_table_entry zram_te; struct zram_table_entry zram_te;
int ret; int ret;
BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.flags) * 8); BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.attr.flags) * 8);
ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare", ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
zcomp_cpu_up_prepare, zcomp_cpu_dead); zcomp_cpu_up_prepare, zcomp_cpu_dead);

View File

@ -65,10 +65,15 @@ enum zram_pageflags {
*/ */
struct zram_table_entry { struct zram_table_entry {
unsigned long handle; unsigned long handle;
unsigned long flags; union {
unsigned long __lock;
struct attr {
u32 flags;
#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
ktime_t ac_time; u32 ac_time;
#endif #endif
} attr;
};
struct lockdep_map dep_map; struct lockdep_map dep_map;
}; };