[PATCH] zram-swap: enable swap discard

Emil Muratov gpm at hotplug.ru
Thu Jun 25 08:57:01 EDT 2020

Hi Rui,
no, of cource I mean not literally leaked memory
but the memory actually used by zram dev. In case of non-discarding 
operation memory used by zdev will grow up untill 'mem limit' and never 
shrink back again.
But checking for zdev counters in $zdev/mm_stat the following values 
correlates pretty good:
"Compressed data size", "Memory used, total", "Maximum memory ever used".
In particular "memory used" for zram floats pretty close to swap's 
current used capacity and "Maximum memory ever used" bumps to the 
highest observable swap's usage and usually more than or equal to "mem 

Could you, please, describe you test case scenarios and measurements 
where you've hit memory waste for zram dev when swap shrinks?

I want to try and reproduce it for recent and kernels maybe.

Thanks for your help!

On 25.06.2020 12:55, Rui Salvaterra wrote:

> Hi, Emil,
> On Thu, 25 Jun 2020 at 10:05, Emil Muratov <gpm at hotplug.ru> wrote:
>> Hmm... that looks weird at first,
>> I was checking kernel counters at /sys/block/zram0/io_stat,
>> which is zram device for swap. And if there was positive counter for
>> discards for zram device while swap was not sending any discards than
>> what is that counter is about?
> The number you see in io_stat doesn't reflect only pages freed due to
> discard/trim requests. From the zram documentation itself [1]:
> File /sys/block/zram<id>/io_stat
> The stat file represents device's I/O statistics not accounted by block
> layer and, thus, not available in zram<id>/stat file. It consists of a
> single line of text and contains the following stats separated by
> whitespace:
>   failed_reads     the number of failed reads
>   failed_writes    the number of failed writes
>   invalid_io       the number of non-page-size-aligned I/O requests
>   notify_free      Depending on device usage scenario it may account
>                    a) the number of pages freed because of swap slot free
>                    notifications or b) the number of pages freed because of
>                    REQ_OP_DISCARD requests sent by bio. The former ones are
>                    sent to a swap block device when a swap slot is freed,
>                    which implies that this disk is being used as a swap disk.
>                    The latter ones are sent by filesystem mounted with
>                    discard option, whenever some data blocks are getting
>                    discarded.
> What you are seeing is the number of freed pages due to swap slot free
> notifications. This means the swap "filesystem" is marking the swap
> slots as free (metadata), but the underlying zram device is not
> returning the memory to the kernel.
>> Have you checked if stressing swap with some push/pull paging load
>> indeed keeps ram occupied at max level?
>> Analyzing /sys/block/$zdev/mm_stat could also help there. I do not
>> remember mem leaks issues when tested stats script, but who knows...
>> Need to take a closer look into this.
>> Thanks!
> The thing is, there are no memory leaks from the kernel point of view.
> The kernel thinks zram needs the memory it has allocated, the problem
> is that it actually doesn't and also doesn't tell the kernel that it
> can reclaim it.
> So, indeed, we need to explicitly tell swapon we want to issue discard commands.
> [1] https://www.kernel.org/doc/Documentation/blockdev/zram.txt
> Thanks,
> Rui

More information about the openwrt-devel mailing list