LKML Archive on
help / color / mirror / Atom feed
From: Peter Xu <>
To: David Hildenbrand <>
	Alistair Popple <>,
	Tiberiu Georgescu <>,,
	Mike Rapoport <>,
	Hugh Dickins <>,
	Matthew Wilcox <>,
	Andrea Arcangeli <>,
	"Kirill A . Shutemov" <>,
	Andrew Morton <>,
	Mike Kravetz <>
Subject: Re: [PATCH RFC 0/4] mm: Enable PM_SWAP for shmem with PTE_MARKER
Date: Tue, 17 Aug 2021 13:09:18 -0400	[thread overview]
Message-ID: <YRvtPrPmAorX+KY5@t490s> (raw)
In-Reply-To: <>

On Tue, Aug 17, 2021 at 11:04:18AM +0200, David Hildenbrand wrote:
> Hi Peter, a couple of comments, sorry for the late reply.
> > Summary
> > =======
> > 
> > [Based on v5.14-rc4]
> > 
> > This patchset enables PM_SWAP of pagemap on shmem.  IOW userspace will be able
> > to detect whether a shmem page is swapped out, just like anonymous pages.
> > 
> > This feature can be enabled with CONFIG_PTE_MARKER_PAGEOUT. When enabled, it
> > brings 0.8% overhead on swap-in performance on a shmem page, so I didn't make
> > it the default yet.  However IMHO 0.8% is still in an acceptable range that we
> > can even make it the default at last.  Comments are welcomed here.
> Special config option and added complexity for handling a corner case
> feature partially more correct. Hm.
> > 
> > There's one previous series that wanted to address the same issue but in
> > another way by Tiberiu A Georgescu <>, here:
> > 
> >
> > 
> > In that series it's done by looking up page cache for all none ptes.  However I
> > raised concern on 4x performance degradation for all shmem pagemap users.
> Who cares? I am asking because for me, it's hard to imagine a workload that
> actually cares about a 4x performance degradation when querying the pagemap
> in very special cases,

I won't say it's a "special case".  it affects any pagemap reading if there's
shmem, even if they're not looking for PM_SWAP it'll suffer from trying to look
up page cache if the pte is none.

Yes it needs to be a relatively large memory to show an effect, but IMHO it's
not a reason good enough that we can make it 4x slower.

> especially if it involves gigantic shmem ranges. VM
> live migration -- sync will be a bit slower? CRIU sync will be a bit slower?
> I mean, actual page dumping is a lot more expensive. Really a problem?
> I read that CRIU cares about shmem via pagemap [1], at least for anonymous
> shared memory; not sure how memfd is treated, I assume in a similar way. But
> I do wonder how it even works reliably, because it relies on present/swapped
> out and sofrtdirty tracking, which are both essentially broken e.g., when
> swapping out AFAIKT. Looks like this really needs a proper fix.
> [1]

I have not enough knowledge to judge CRIU here, but I can kind of understand
Tiberiu's problem and hopefully I digested it right.  What I'm targetting with
this series is just to see whether it can fix the problem there, assuming that
could be a good summary of what it can also be used elsewhere.

We have the other solution of the page cache lookup, but I think that's slower
than this solution, so when there's the 0.8% overhead possibility I think this
one is much better.  The bad side is it's going to be added into swap-in path,
it's not ideal for fast swap devices, but I have no knowledge on that part too,
as to my understanding most swap devices should be still slow like hard disks
which can take worst case milli-seconds to read in a sector.  I think that
overhead should be mostly not measureable and lost in the white noise.

> > 
> > Unlike the other approach, this series has zero overhead on pagemap read
> > because the PM_SWAP info is consolidated into the zapped PTEs directly.
> > 
> > Goals
> > =====
> > 
> > One major goal of this series is to add the PM_SWAP support, the reason is as
> > stated by Tiberiu and Ivan in the other patchset:
> > 
> >
> > 
> > As a summary: for some reason the userspace needs to scan the pages in the
> > background, however that scanning could misguide page reclaim on which page is
> > hot and which is cold.  With correct PM_SWAP information, the userspace can
> > correct the behavior of page reclaim by firstly fetching that info from
> > pagemap, and explicit madvise(MADV_PAGEOUT).  In this case, the pages are for
> > the guest, but it can be any shmem page.
> > 
> > Another major goal of this series is to do a proof-of-concept of the PTE marker
> > idea, and that's also the major reason why it's RFC.  So far PTE marker can
> > potentially be the solution for below three problems that I'm aware of:
> > 
> >    (a) PM_SWAP on shmem
> > 
> >    (b) Userfaultfd-wp on shmem/hugetlbfs
> > 
> >    (c) PM_SOFT_DIRTY lost for shmem over swapping
> > 
> > This series tries to resolve problem (a) which should be the simplest, ideally
> > it should solve immediate problem for the live migration issue raised by
> > Tiberiu and Ivan on proactive paging out unused guest pages.
> > 
> > Both (a) and (c) will be for performance-wise or statistic-wise.
> > 
> > Scenario (b) will require pte markers as part of the function to trap writes to
> > uffd-wp protected regions when the pages were e.g. swapped out or zapped for
> > any reason.
> > 
> > Currently, uffd-wp shmem work (still during review on the list, latest v5, [1])
> > used another solution called "special swap pte".  It works similarly like PTE
> > markers as both of the approachs are to persist information into zapped pte,
> > but people showed concern about that idea and it's suggested to use a safer
> > (swp-entry level operation, not pte level), and arch-independent approach.
> > 
> > Hopefully PTE markers satifsfy these demands.
> > 
> > Before I rework the uffd-wp series, I wanted to know whether this approach can
> > be accepted upstream.  So besides the swap part, comments on PTE markers will
> > be extremely welcomed.
> For uffd-wp in its current form, it would certainly be the way to go I
> think. AFAIKT, the idea of special swap entries isn't new, just that it's
> limited to anonymous memory for now, which makes things like fork and new
> mappings a lot cheaper.

Thanks for reviewing this series separately; yes I definitely wanted to get
comments on both sides: one on the pte marker idea, the other is whether it's
applicable to this swap+shmem use case.

I don't plan to handle both fork and new mappings for swapping.

For fork(), as we've discussed, and as I've also mentioned below in Known
Issues, that we don't copy these markers in fork().  So children may lose these
hints from pte, but I think that's fine if that's hardly used.

Here I really want to make the pte marker be flexible - it can be strict when
necessary (it will be 100% strict with uffd-wp), then it can also be a hint
just like what we have with available ptes on soft-dirty, idle, accessed bits.
Here the swap bit I wanted to make it that kind, so we add zero overhead to
fork() and we still solve problems.

Same thing to "newly mapped shmem".  Do we have a use case for that?  If that's
a hint bit, can we ignore it?

> > 
> > What is PTE Markers?
> > ====================
> > 
> > PTE markers are defined as some special PTEs that works like a "marker" just
> > like in normal life.  Firstly it uses a swap type, IOW it's not a valid/present
> > pte, so processor will trigger a page fault when it's accessed.  Meanwhile, the
> > format of the PTE is well-defined, so as to contain some information that we
> > would like to know before/during the page access happening.
> > 
> > In this specific case, when the shmem page is paged out, we set a marker
> > showing that this page was paged out, then when pagemap is read about this pte,
> > we know this is a swapped-out/very-cold page.
> > 
> > This use case is not an obvious one but the most simplest.  The uffd-wp use
> > case is more obvious (wr-protect is per-pte, so we can't save into page cache;
> > meanwhile we need that info to persist across zappings e.g. thp split or page
> > out of shmem pages).
> > 
> > So in the future, it can contain more information, e.g., whether this pte is
> > wr-protected by userfaultfd; whether this pte was written in this mm context
> > for soft-dirtying.  On 64 bit systems, we have a total of 58 bits (swp_offset).
> > 
> > I'm also curious whether it can be further expanded to other mm areas.  E.g.,
> > logically it can work too for non-RAM based memories outside shmem/hugetlbfs,
> > e.g. a common file system like ext4 or btrfs?  As long as there will be a need
> > to store some per-pte information across zapping of the ptes, then maybe it can
> > be considered.
> As already expressed, we should try storing as little information in page
> tables as possible if we're dealing with shared memory. The features we
> design around this all seem to over-complicate the actual users,
> over-complicate fork, over-complicate handling on new mappings.

I'll skip the last two "over-complicated" items, because as we've discussed I
don't think we need to take care of them so far.  We can revisit when they
become some kind of requirement.

To me having PM_SWAP 99% right on shmem is still a progress comparing to
completely missing it, even if it's not 100% right.  It's used for performance
reasons on PAGEOUT and doing finer-grained memory control from userspace, it's
not a strict requirement.

So I still cannot strictly follow why storing information in pte is so bad for
file-backed, which I can see you really don't like it.  Could you share some
detailed example?

> For uffd-wp in its current form, there seems to be no way around it, and PTE
> markers seem to be what you want -- but as I already raised, the feature
> itself on shmem is somewhat suboptimal, just like SOFTDIRTY tracking on
> shmem.

Emm I don't know whether we should call it sub-optimal. To me it's still a
clean and self-contained way to do things.  I don't know how to do that from
page cache layer, but I bet there'll be issues coming when we go that way, then
we may figure out "oh this way is 90% beautiful, but that way is 85%".  To me
it's sometimes not easy to figure out the ideal solution for all the problems.

> But I guess I'm biased at this point because the main users of these
> features actually want to query/set such properties for all sharers, not
> individual processes; so the opinion of others would be appreciated.
> > 
> > Known Issues/Concerns
> > =====================
> > 
> > About THP
> > ---------
> > 
> > Currently we don't need to worry about THP because paged out shmem pages will
> > be split when shrinking, IOW we only need to consider PTE, and the markers will
> > only be applied to a shmem pte not pmd or bigger.
> > 
> > About PM_SWAP Accuracy
> > ----------------------
> > 
> > This is not an "accurate" solution to provide PM_SWAP bit.  Two exmaples:
> > 
> >    - When process A & B both map shmem page P somewhere, it can happen that only
> >      one of these ptes got marked with the pte marker.  Imagine below sequence:
> > 
> >      0. Process A & B both map shmem page P somewhere
> >      1. Process A zap pte of page P for some reason (e.g. thp split)
> >      2. System decides to recycle page P
> >      3. System replace process B's pte (pointed to P) by PTE marker
> >      4. System _didn't_ replace process A's pte because it was none pte, and
> >         it'll continue to be none pte
> >      5. Only process B's relevant pte has the PTE marker after P swapped out
> > 
> >    - When fork, we don't copy shmem vma ptes, including the pte markers.  So
> >      even if page P was swapped out, only the parent process has the pte marker
> >      installed, in child it'll be none pte if fork() happened after pageout.
> > 
> > Conclusion: just like it used to be, the PM_SWAP is best-effort.  But it should
> > work in 99.99% cases and it should already start to solve problems.
> At least I don't like these semantics at all. PM_SWAP is a cached value
> which might be under-represented and consequently wrong.

Please have a look at current pagemap impl in pte_to_pagemap_entry().  It's not
accurate from the 1st day, imho.  E.g., when a page is being migrated from numa
node 1 to node 2, we'll mark it PM_SWAP but I think it's not the case.  We can
make it more accurate, but I think it's fine, because it's a hint.

> Take CRIU as an example, it has to be correct even if a process would remap a
> memory region, fork() and unmap in the parent as far as I understand, ...

Are you talking about dirty bit or swap bit?  I'm a bit confused on why swap
bit needs to be accurate.  Maybe you mean the dirty bit?

> If we really care about performance for users with the old semantics,
> introduce some runtime toggle that enables the new behavior (even for a
> single process?) and consequently is a bit slower in corner cases. But I
> really do wonder if we care at all about the performance degradation in
> corner cases.

Yes that's okay.  If we want to go with Tiberiu's approach, we can use an
option to avoid regress users.  However I still prefer the current approach,
and I even want to make it a default option if it can prove itself with some
more real swap workloads somewhere (maybe kernel bot has some?  I didn't check

> If we really care about performance for users with new semantics, then let's
> do it properly and see how we can actually speed it up without per-process
> page table hacks.
> Anyhow, just my 2 cents.

Thanks for the discussion!

Peter Xu

  reply	other threads:[~2021-08-17 17:09 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-07  3:25 Peter Xu
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 [this message]
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 \
    --in-reply-to=YRvtPrPmAorX+KY5@t490s \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
    --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).