--- zzzz-none-000/linux-5.4.213/drivers/block/zram/zram_drv.c 2022-09-15 10:04:56.000000000 +0000 +++ miami-7690-761/linux-5.4.213/drivers/block/zram/zram_drv.c 2024-05-29 11:19:50.000000000 +0000 @@ -33,6 +33,8 @@ #include #include #include +#include +#include #include "zram_drv.h" @@ -1873,6 +1875,44 @@ NULL, }; +static int oom_info_callback(struct notifier_block *block, unsigned long event, void *_data) +{ + struct zram *zram = container_of(block, struct zram, oom_info_block); + struct seq_file *file = _data; + struct zs_pool_stats pool_stats; + u64 orig_size, mem_used = 0; + long max_used; + + memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats)); + + // Unlik mm_stat we don't use semaphores to lock the access here. This is needed as the + // info_callback is called in an atomic context. + // + // All of the calls made here are either normal reads or atomic reads. Which can be done + // unguarded. The worst is, that we get inconsistent data, which should be easily detectable + // by inspecting the output. + if (init_done(zram)) { + mem_used = zs_get_total_pages(zram->mem_pool); + zs_pool_stats(zram->mem_pool, &pool_stats); + } + + orig_size = atomic64_read(&zram->stats.pages_stored); + max_used = atomic_long_read(&zram->stats.max_used_pages); + + sseq_printf(file, + "%s: orig:%lluKiB compr:%lluKiB used:%lluKiB limit:%luKiB max:%ldKiB same:%llu compact:%lu\n", + zram->disk->disk_name, + orig_size << (PAGE_SHIFT - 10), + (u64)atomic64_read(&zram->stats.compr_data_size) >> 10, + mem_used << (PAGE_SHIFT - 10), + zram->limit_pages << (PAGE_SHIFT - 10), + max_used << (PAGE_SHIFT - 10), + (u64)atomic64_read(&zram->stats.same_pages), + atomic_long_read(&pool_stats.pages_compacted)); + + return NOTIFY_DONE; +} + /* * Allocate and initialize new zram device. the function returns * '>= 0' device_id upon success, and negative value otherwise. @@ -1960,6 +2000,9 @@ strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor)); zram_debugfs_register(zram); + zram->oom_info_block.notifier_call = oom_info_callback; + avm_oom_info_chain_register(&zram->oom_info_block); + pr_info("Added device: %s\n", zram->disk->disk_name); return device_id; @@ -1990,6 +2033,8 @@ zram->claim = true; mutex_unlock(&bdev->bd_mutex); + avm_oom_info_chain_unregister(&zram->oom_info_block); + zram_debugfs_unregister(zram); /* Make sure all the pending I/O are finished */