Skip to content

ksm: perform a range-walk to jump over holes in break_ksm

Pedro Demarchi Gomes linux-mm lkml 4 versions – Applied

Status from this message: [merged mm-stable] ksm-replace-function-unmerge_ksm_pages-with-break_ksm.patch removed from -mm tree

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest3 patches 0 repliesApplied 3/3
  2. v33 patches 3 repliesSuperseded
  3. v23 patches 6 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v13 patches6 repliesSuperseded

Changes in v4 from the cover letter

- Fix few nits

Patches in v4 All 3 applied

#Subject and filesSizeRepliesApplied
1/3Revert "mm/ksm: convert break_ksm() from walk_page_range_vma() to folio_walk"mm/ksm.c+48 -160 repliesApplied
2/3ksm: perform a range-walk in break_ksmmm/ksm.c+43 -400 repliesApplied
3/3ksm: replace function unmerge_ksm_pages with break_ksmmm/ksm.c+16 -240 repliesApplied

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchDavid Hildenbrand (Red Hat)
1/3A-b
2/3A-b
3/3A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v4 thread

Cover letter [PATCH v4 0/3] ksm: perform a range-walk to jump over holes in break_ksm

When unmerging an address range, unmerge_ksm_pages function walks every page address in the specified range to locate ksm pages. This becomes highly inefficient when scanning large virtual memory areas that contain mostly unmapped regions, causing the process to get blocked for several minutes.

This patch makes break_ksm, function called by unmerge_ksm_pages for every page in an address range, perform a range walk, allowing it to skip over entire unmapped holes in a VMA, avoiding unnecessary lookups.

As pointed by David Hildenbrand in [1], unmerge_ksm_pages() is called from:

* ksm_madvise() through madvise(MADV_UNMERGEABLE). There are not a lot of users of that function.

* __ksm_del_vma() through ksm_del_vmas(). Effectively called when disabling KSM for a process either through the sysctl or from s390x gmap code when enabling storage keys for a VM.

Consider the following test program which creates a 32 TiB mapping in the virtual address space but only populates a single page:

#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>

/* 32 TiB */
const size_t size = 32ul * 1024 * 1024 * 1024 * 1024;

int main() {
        char *area = mmap(NULL, size, PROT_READ | PROT_WRITE,
                          MAP_NORESERVE | MAP_PRIVATE | MAP_ANON, -1, 0);

        if (area == MAP_FAILED) {
                perror("mmap() failed\n");
                return -1;
        }

        /* Populate a single page such that we get an anon_vma. */
        *area = 0;

        /* Enable KSM. */
        madvise(area, size, MADV_MERGEABLE);
        madvise(area, size, MADV_UNMERGEABLE);
        return 0;
}

Without this patch, this program takes 9 minutes to finish, while with this patch it finishes in less then 5 seconds.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Changelog:

v4:
- Fix few nits

v3: https://lore.kernel.org/all/[email protected]/
- Remove leftover code from previous version
- Call is_ksm_zero_pte() only if the folio is present
- Rename variable ret to found in first patch

v2: https://lore.kernel.org/all/[email protected]/
- Use folios in break_ksm_pmd_entry
- Pass address pointer to walk_page_range_vma private parameter instead of struct break_ksm_arg
- Do not unnecessarily initialize start_ptep
- Order local variable declarations from longest to shortest line
- Improve patch 2 description (do not reference the cover letter)
- Quite a few styling fixes and improvements as suggested by reviewer

v1: https://lore.kernel.org/all/[email protected]/

Pedro Demarchi Gomes (3):
Revert "mm/ksm: convert break_ksm() from walk_page_range_vma() to
folio_walk"
ksm: perform a range-walk in break_ksm
ksm: replace function unmerge_ksm_pages with break_ksm

 mm/ksm.c | 137 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 82 insertions(+), 55 deletions(-)
Signature · 1 line
2.43.0