Re: [PATCH] ksm: Fix unlocked iteration over vmas in cmp_and_merge_page()

From: Andrea Arcangeli
Date: Wed Sep 13 2017 - 09:46:40 EST


On Wed, Sep 13, 2017 at 01:25:09PM +0200, Michal Hocko wrote:
> [CC Claudio and Hugh]

Cc'ed Sioh as well.

>
> On Mon 11-09-17 14:05:05, Kirill Tkhai wrote:
> > In this place mm is unlocked, so vmas or list may change.
> > Down read mmap_sem to protect them from modifications.
> >
> > Signed-off-by: Kirill Tkhai <ktkhai@xxxxxxxxxxxxx>
> > (and compile-tested-by)
>
> Fixes: e86c59b1b12d ("mm/ksm: improve deduplication of zero pages with colouring")
> AFAICS. Maybe even CC: stable as unstable vma can cause large variety of
> issues including memory corruption.
>
> The fix lookds good to me
> Acked-by: Michal Hocko <mhocko@xxxxxxxx>

Reviewed-by: Andrea Arcangeli <aarcange@xxxxxxxxxx>

ksm_use_zero_pages is off by default, this is probably why it went
unnoticed.

Wondering if we should consider enabling ksm_use_zero_pages by default
on those arches that have few physical cache colors.

If we change the jhash2 to crc32c-intel like Sioh suggested (to
speedup the background scan), the chance of false positive and waste
of try_to_merge_one_page here will increase to one every 100k or so if
comparing random data against the zeropage (instead of the current
insignificant amount of false positives provided by jhash2 great
random uniformity).

So the ksm_use_zero_pages branch is currently missing a memcmp against
the ZERO_PAGE zeroes before calling write_protect_page. It's not a
functional bug because there's one last memcmp mandatory to run after
write protection, so this isn't destabilizing anything, but especially
if using crc32c (I suppose crc32cbe-vx is going to be much faster for
the background scan on s390 too) it would be a potential inefficiency
that wrprotects non zero pages by mistake once every 100k or more.

We never care about the cksum actual value, we only care if it changed
since the last pass, this is why ultimately I believe crc32c would
suffice for this purpose, it's extremely unlikely it won't change over
a data change. But it's definitely not suitable to find equality
across million of pages on large systems because it has a not suitable
random uniformity.

In short the "zero_checksum" variable should be dropped and the memcmp
will then materialize naturally after removing it.

Thanks,
Andrea