LKML Archive on
help / color / mirror / Atom feed
From: Peter Xu <>
Cc: Alistair Popple <>,
	Tiberiu Georgescu <>,,
	Mike Rapoport <>,
	Hugh Dickins <>,, Matthew Wilcox <>,
	Andrea Arcangeli <>,
	David Hildenbrand <>,
	"Kirill A . Shutemov" <>,
	Andrew Morton <>,
	Mike Kravetz <>
Subject: [PATCH RFC 0/4] mm: Enable PM_SWAP for shmem with PTE_MARKER
Date: Fri,  6 Aug 2021 23:25:17 -0400	[thread overview]
Message-ID: <> (raw)

[Based on v5.14-rc4]\r
This patchset enables PM_SWAP of pagemap on shmem.  IOW userspace will be able\r
to detect whether a shmem page is swapped out, just like anonymous pages.\r
This feature can be enabled with CONFIG_PTE_MARKER_PAGEOUT. When enabled, it\r
brings 0.8% overhead on swap-in performance on a shmem page, so I didn't make\r
it the default yet.  However IMHO 0.8% is still in an acceptable range that we\r
can even make it the default at last.  Comments are welcomed here.\r
There's one previous series that wanted to address the same issue but in\r
another way by Tiberiu A Georgescu <>, here:\r
In that series it's done by looking up page cache for all none ptes.  However I\r
raised concern on 4x performance degradation for all shmem pagemap users.\r
Unlike the other approach, this series has zero overhead on pagemap read\r
because the PM_SWAP info is consolidated into the zapped PTEs directly.\r
One major goal of this series is to add the PM_SWAP support, the reason is as\r
stated by Tiberiu and Ivan in the other patchset:\r
As a summary: for some reason the userspace needs to scan the pages in the\r
background, however that scanning could misguide page reclaim on which page is\r
hot and which is cold.  With correct PM_SWAP information, the userspace can\r
correct the behavior of page reclaim by firstly fetching that info from\r
pagemap, and explicit madvise(MADV_PAGEOUT).  In this case, the pages are for\r
the guest, but it can be any shmem page.\r
Another major goal of this series is to do a proof-of-concept of the PTE marker\r
idea, and that's also the major reason why it's RFC.  So far PTE marker can\r
potentially be the solution for below three problems that I'm aware of:\r
  (a) PM_SWAP on shmem\r
  (b) Userfaultfd-wp on shmem/hugetlbfs\r
  (c) PM_SOFT_DIRTY lost for shmem over swapping\r
This series tries to resolve problem (a) which should be the simplest, ideally\r
it should solve immediate problem for the live migration issue raised by\r
Tiberiu and Ivan on proactive paging out unused guest pages.\r
Both (a) and (c) will be for performance-wise or statistic-wise.\r
Scenario (b) will require pte markers as part of the function to trap writes to\r
uffd-wp protected regions when the pages were e.g. swapped out or zapped for\r
any reason.\r
Currently, uffd-wp shmem work (still during review on the list, latest v5, [1])\r
used another solution called "special swap pte".  It works similarly like PTE\r
markers as both of the approachs are to persist information into zapped pte,\r
but people showed concern about that idea and it's suggested to use a safer\r
(swp-entry level operation, not pte level), and arch-independent approach.\r
Hopefully PTE markers satifsfy these demands.\r
Before I rework the uffd-wp series, I wanted to know whether this approach can\r
be accepted upstream.  So besides the swap part, comments on PTE markers will\r
be extremely welcomed.\r
What is PTE Markers?\r
PTE markers are defined as some special PTEs that works like a "marker" just\r
like in normal life.  Firstly it uses a swap type, IOW it's not a valid/present\r
pte, so processor will trigger a page fault when it's accessed.  Meanwhile, the\r
format of the PTE is well-defined, so as to contain some information that we\r
would like to know before/during the page access happening.\r
In this specific case, when the shmem page is paged out, we set a marker\r
showing that this page was paged out, then when pagemap is read about this pte,\r
we know this is a swapped-out/very-cold page.\r
This use case is not an obvious one but the most simplest.  The uffd-wp use\r
case is more obvious (wr-protect is per-pte, so we can't save into page cache;\r
meanwhile we need that info to persist across zappings e.g. thp split or page\r
out of shmem pages).\r
So in the future, it can contain more information, e.g., whether this pte is\r
wr-protected by userfaultfd; whether this pte was written in this mm context\r
for soft-dirtying.  On 64 bit systems, we have a total of 58 bits (swp_offset).\r
I'm also curious whether it can be further expanded to other mm areas.  E.g.,\r
logically it can work too for non-RAM based memories outside shmem/hugetlbfs,\r
e.g. a common file system like ext4 or btrfs?  As long as there will be a need\r
to store some per-pte information across zapping of the ptes, then maybe it can\r
be considered.\r
Known Issues/Concerns\r
About THP\r
Currently we don't need to worry about THP because paged out shmem pages will\r
be split when shrinking, IOW we only need to consider PTE, and the markers will\r
only be applied to a shmem pte not pmd or bigger.\r
About PM_SWAP Accuracy\r
This is not an "accurate" solution to provide PM_SWAP bit.  Two exmaples:\r
  - When process A & B both map shmem page P somewhere, it can happen that only\r
    one of these ptes got marked with the pte marker.  Imagine below sequence:\r
    0. Process A & B both map shmem page P somewhere\r
    1. Process A zap pte of page P for some reason (e.g. thp split)\r
    2. System decides to recycle page P\r
    3. System replace process B's pte (pointed to P) by PTE marker\r
    4. System _didn't_ replace process A's pte because it was none pte, and\r
       it'll continue to be none pte\r
    5. Only process B's relevant pte has the PTE marker after P swapped out\r
  - When fork, we don't copy shmem vma ptes, including the pte markers.  So\r
    even if page P was swapped out, only the parent process has the pte marker\r
    installed, in child it'll be none pte if fork() happened after pageout.\r
Conclusion: just like it used to be, the PM_SWAP is best-effort.  But it should\r
work in 99.99% cases and it should already start to solve problems.\r
About Performance Impact\r
Due to the special PTE marker, page fault logic needs to understand this pte\r
and there will be some extra logic to handle that.  The overhead is merely\r
non-observable with 0.82% perf drop.\r
For more information, please see the test section below where I wrote a test\r
for it.  When we really care about that small difference, the user can also\r
disable the shmem PM_SWAP support with !CONFIG_PTE_MARKER_PAGEOUT.\r
Test case I used is here:\r
Functional test\r
Run with !CONFIG_PTE_MARKER_PAGEOUT, we'll miss the PM_SWAP when paged out (see\r
swap bit always being zeros):\r
       FAULT1 (expect swap==0): present bit 1, swap bit 0\r
      PAGEOUT (expect swap==1): present bit 0, swap bit 0\r
       FAULT2 (expect swap==0): present bit 1, swap bit 0\r
       REMOVE (expect swap==0): present bit 0, swap bit 0\r
      PAGEOUT (expect swap==1): present bit 0, swap bit 0\r
       REMOVE (expect swap==0): present bit 0, swap bit 0\r
Run with CONFIG_PTE_MARKER_PAGEOUT, we'll be able to observe correct PM_SWAP:\r
       FAULT1 (expect swap==0): present bit 1, swap bit 0\r
      PAGEOUT (expect swap==1): present bit 0, swap bit 1\r
       FAULT2 (expect swap==0): present bit 1, swap bit 0\r
       REMOVE (expect swap==0): present bit 0, swap bit 0\r
      PAGEOUT (expect swap==1): present bit 0, swap bit 1\r
       REMOVE (expect swap==0): present bit 0, swap bit 0\r
Performance test\r
The performance test is not about pagemap reading, because it should be the\r
same as before.  Instead there's indeed extra overhead in the fault path, when\r
the page is swapped in from the disk.  I did some sequential swap-in tests of\r
1GB range (each for 5 times in a loop) to measure the difference.\r
Hardware I used:\r
        Processor: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz\r
        Memory:    32GB memory, 16GB swap (on a PERC H330 Mini 2TBi disk)\r
        Test Size: 1GB shmem\r
I only measured the time to fault-in the pages on the disk, so the measurement\r
does not include pageout time, one can refer to the .c file.  Results:\r
   | Config                            | Time used (us)   | Change (%) |\r
   | !PTE_MARKER                       | 519652 (+-0.73%) |        N/A |\r
   | PTE_MARKER && !PTE_MARKER_PAGEOUT | 519874 (+-0.40%) |     -0.04% |\r
   | PTE_MARKER && PTE_MARKER_PAGEOUT  | 523914 (+-0.71%) |     -0.82% |\r
Any comment would be greatly welcomed.\r
Peter Xu (4):\r
  mm: Introduce PTE_MARKER swap entry\r
  mm: Check against orig_pte for finish_fault()\r
  mm: Handle PTE_MARKER page faults\r
  mm: Install marker pte when page out for shmem pages\r
 fs/proc/task_mmu.c      |  1 +\r
 include/linux/rmap.h    |  1 +\r
 include/linux/swap.h    | 14 ++++++++++++-\r
 include/linux/swapops.h | 45 +++++++++++++++++++++++++++++++++++++++++\r
 mm/Kconfig              | 17 ++++++++++++++++\r
 mm/memory.c             | 43 ++++++++++++++++++++++++++++++++++++++-\r
 mm/rmap.c               | 19 +++++++++++++++++\r
 mm/vmscan.c             |  2 +-\r
 8 files changed, 139 insertions(+), 3 deletions(-)\r
-- \r

             reply	other threads:[~2021-08-07  3:25 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-07  3:25 Peter Xu [this message]
2021-08-07  3:25 ` [PATCH RFC 1/4] mm: Introduce PTE_MARKER swap entry Peter Xu
2021-08-07  3:25 ` [PATCH RFC 2/4] mm: Check against orig_pte for finish_fault() Peter Xu
2021-08-07  3:25 ` [PATCH RFC 3/4] mm: Handle PTE_MARKER page faults Peter Xu
2021-08-07  3:25 ` [PATCH RFC 4/4] mm: Install marker pte when page out for shmem pages Peter Xu
2021-08-13 15:18   ` Tiberiu Georgescu
2021-08-13 16:01     ` Peter Xu
2021-08-18 18:02       ` Tiberiu Georgescu
2021-08-17  9:04 ` [PATCH RFC 0/4] mm: Enable PM_SWAP for shmem with PTE_MARKER David Hildenbrand
2021-08-17 17:09   ` Peter Xu
2021-08-17 18:46     ` David Hildenbrand
2021-08-17 20:24       ` Peter Xu
2021-08-18  8:24         ` David Hildenbrand
2021-08-18 17:52           ` Tiberiu Georgescu
2021-08-18 18:13             ` David Hildenbrand
2021-08-19 14:54               ` Tiberiu Georgescu
2021-08-19 17:26                 ` David Hildenbrand
2021-08-20 16:49                   ` Tiberiu Georgescu
2021-08-20 19:12                     ` Peter Xu
2021-08-25 13:40                       ` Tiberiu Georgescu
2021-08-25 14:59                         ` Peter Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
    --subject='Re: [PATCH RFC 0/4] mm: Enable PM_SWAP for shmem with PTE_MARKER' \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).