Re: [PATCH 2/4] ksm: skip rmap_item allocation for non-zero pages under only_merge_zero_pages

From: Xu Xin (vivo)

Date: Wed Nov 11 2026 - 04:38:08 EST


> I think we should get rid of any rmap items when merging the shared zeropage
> only. That might require more changes to the ksm code, but it really doesn't
> make any sense to have rmap items.
>
> Maybe we just want a separate KSM infrastrcture that only focuses on
> deduplciating shared zeropages in KSM: get a page, see if it is zero-filled. If
> so, try deduplciating with the shared zeropage. Done.
>
> Mangling this into the existing infrastructure might look easier but it just
> adds things like additional zero-page comparisons and (as Lorenzo points out)
> makes the code harder to understand + maintain.

Makes sense, I agree.

I actually thought about doing it this way at first, but went with the
current approach since it looked simpler back then. Without needing any
rmap_item tracking for zero pages, we can avoid rewriting scan_get_next_rmap_item()
with more complexities.

Splitting them up has two obvious wins:

1. ksm_do_scan() becomes much easier to read:

if (only_merge_zero_pages) {
scan_get_empty_page();
try_to_merge_with_zero_pages();
} else {
/* existing logic */
}

2. We can even add better filtering in scan_get_empty_page() to speed up
empty page scanning, without making scan_get_next_rmap_item() even messier.

That's a very constructive suggestion. I'll rework v3 this way.

Thanks,
Xu