From: Barry Song <21cnbao@gmail.com>
To: "Huang, Ying" <ying.huang@intel.com>
Cc: Khalid Aziz <khalid.aziz@oracle.com>,
akpm@linux-foundation.org, linux-mm@kvack.org,
baolin.wang@linux.alibaba.com, chrisl@kernel.org,
david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org,
hughd@google.com, kasong@tencent.com, ryan.roberts@arm.com,
surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org,
xiang@kernel.org, yosryahmed@google.com, yuzhao@google.com,
ziy@nvidia.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 4/5] mm: swap: entirely map large folios found in swapcache
Date: Tue, 16 Apr 2024 16:40:08 +1200 [thread overview]
Message-ID: <CAGsJ_4x5FJrpGLzvrEZ-mgWiSUVaO-41h6Dcy7VGyaMMC8Tmng@mail.gmail.com> (raw)
In-Reply-To: <87pluq0zdr.fsf@yhuang6-desk2.ccr.corp.intel.com>
On Tue, Apr 16, 2024 at 3:19 PM Huang, Ying <ying.huang@intel.com> wrote:
>
> Barry Song <21cnbao@gmail.com> writes:
>
> > On Tue, Apr 16, 2024 at 2:41 PM Huang, Ying <ying.huang@intel.com> wrote:
> >>
> >> Barry Song <21cnbao@gmail.com> writes:
> >>
> >> > On Tue, Apr 16, 2024 at 2:27 PM Huang, Ying <ying.huang@intel.com> wrote:
> >> >>
> >> >>
> >> >> Added Khalid for arch_do_swap_page().
> >> >>
> >> >> Barry Song <21cnbao@gmail.com> writes:
> >> >>
> >> >> > On Mon, Apr 15, 2024 at 8:39 PM Huang, Ying <ying.huang@intel.com> wrote:
> >> >> >>
> >> >> >> Barry Song <21cnbao@gmail.com> writes:
> >> >>
> >> >> [snip]
> >> >>
> >> >> >>
> >> >> >> > + bool any_swap_shared = false;
> >> >> >> >
> >> >> >> > if (!pte_unmap_same(vmf))
> >> >> >> > goto out;
> >> >> >> > @@ -4137,6 +4141,35 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> >> >> >> > */
> >> >> >> > vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, vmf->address,
> >> >> >> > &vmf->ptl);
> >> >> >>
> >> >> >> We should move pte check here. That is,
> >> >> >>
> >> >> >> if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
> >> >> >> goto out_nomap;
> >> >> >>
> >> >> >> This will simplify the situation for large folio.
> >> >> >
> >> >> > the plan is moving the whole code block
> >> >> >
> >> >> > if (start_pte && folio_test_large(folio) && folio_test_swapcache(folio))
> >> >> >
> >> >> > after
> >> >> > if (unlikely(!folio_test_uptodate(folio))) {
> >> >> > ret = VM_FAULT_SIGBUS;
> >> >> > goto out_nomap;
> >> >> > }
> >> >> >
> >> >> > though we couldn't be !folio_test_uptodate(folio)) for hitting
> >> >> > swapcache but it seems
> >> >> > logically better for future use.
> >> >>
> >> >> LGTM, Thanks!
> >> >>
> >> >> >>
> >> >> >> > +
> >> >> >> > + /* We hit large folios in swapcache */
> >> >> >>
> >> >> >> The comments seems unnecessary because the code tells that already.
> >> >> >>
> >> >> >> > + if (start_pte && folio_test_large(folio) && folio_test_swapcache(folio)) {
> >> >> >> > + int nr = folio_nr_pages(folio);
> >> >> >> > + int idx = folio_page_idx(folio, page);
> >> >> >> > + unsigned long folio_start = vmf->address - idx * PAGE_SIZE;
> >> >> >> > + unsigned long folio_end = folio_start + nr * PAGE_SIZE;
> >> >> >> > + pte_t *folio_ptep;
> >> >> >> > + pte_t folio_pte;
> >> >> >> > +
> >> >> >> > + if (unlikely(folio_start < max(vmf->address & PMD_MASK, vma->vm_start)))
> >> >> >> > + goto check_pte;
> >> >> >> > + if (unlikely(folio_end > pmd_addr_end(vmf->address, vma->vm_end)))
> >> >> >> > + goto check_pte;
> >> >> >> > +
> >> >> >> > + folio_ptep = vmf->pte - idx;
> >> >> >> > + folio_pte = ptep_get(folio_ptep);
> >> >> >>
> >> >> >> It's better to construct pte based on fault PTE via generalizing
> >> >> >> pte_next_swp_offset() (may be pte_move_swp_offset()). Then we can find
> >> >> >> inconsistent PTEs quicker.
> >> >> >
> >> >> > it seems your point is getting the pte of page0 by pte_next_swp_offset()
> >> >> > unfortunately pte_next_swp_offset can't go back. on the other hand,
> >> >> > we have to check the real pte value of the 0nd entry right now because
> >> >> > swap_pte_batch() only really reads pte from the 1st entry. it assumes
> >> >> > pte argument is the real value for the 0nd pte entry.
> >> >> >
> >> >> > static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
> >> >> > {
> >> >> > pte_t expected_pte = pte_next_swp_offset(pte);
> >> >> > const pte_t *end_ptep = start_ptep + max_nr;
> >> >> > pte_t *ptep = start_ptep + 1;
> >> >> >
> >> >> > VM_WARN_ON(max_nr < 1);
> >> >> > VM_WARN_ON(!is_swap_pte(pte));
> >> >> > VM_WARN_ON(non_swap_entry(pte_to_swp_entry(pte)));
> >> >> >
> >> >> > while (ptep < end_ptep) {
> >> >> > pte = ptep_get(ptep);
> >> >> >
> >> >> > if (!pte_same(pte, expected_pte))
> >> >> > break;
> >> >> >
> >> >> > expected_pte = pte_next_swp_offset(expected_pte);
> >> >> > ptep++;
> >> >> > }
> >> >> >
> >> >> > return ptep - start_ptep;
> >> >> > }
> >> >>
> >> >> Yes. You are right.
> >> >>
> >> >> But we may check whether the pte of page0 is same as "vmf->orig_pte -
> >> >> folio_page_idx()" (fake code).
> >> >
> >> > right, that is why we are reading and checking PTE0 before calling
> >> > swap_pte_batch()
> >> > right now.
> >> >
> >> > folio_ptep = vmf->pte - idx;
> >> > folio_pte = ptep_get(folio_ptep);
> >> > if (!is_swap_pte(folio_pte) || non_swap_entry(pte_to_swp_entry(folio_pte)) ||
> >> > swap_pte_batch(folio_ptep, nr, folio_pte, &any_swap_shared) != nr)
> >> > goto check_pte;
> >> >
> >> > So, if I understand correctly, you're proposing that we should directly check
> >> > PTE0 in swap_pte_batch(). Personally, I don't have any objections to this idea.
> >> > However, I'd also like to hear the feedback from Ryan and David :-)
> >>
> >> I mean that we can replace
> >>
> >> !is_swap_pte(folio_pte) || non_swap_entry(pte_to_swp_entry(folio_pte))
> >>
> >> in above code with pte_same() with constructed expected first pte.
> >
> > Got it. It could be quite tricky, especially with considerations like
> > pte_swp_soft_dirty, pte_swp_exclusive, and pte_swp_uffd_wp. We might
> > require a helper function similar to pte_next_swp_offset() but capable of
> > moving both forward and backward. For instance:
> >
> > pte_move_swp_offset(pte_t pte, long delta)
> >
> > pte_next_swp_offset can insteadly call it by:
> > pte_move_swp_offset(pte, 1);
> >
> > Is it what you are proposing?
>
> Yes. Exactly.
Great. I agree that this appears to be much cleaner than the current code.
>
> --
> Best Regards,
> Huang, Ying
>
> >>
> >> >>
> >> >> You need to check the pte of page 0 anyway.
> >> >>
> >> >> >>
> >> >> >> > + if (!is_swap_pte(folio_pte) || non_swap_entry(pte_to_swp_entry(folio_pte)) ||
> >> >> >> > + swap_pte_batch(folio_ptep, nr, folio_pte, &any_swap_shared) != nr)
> >> >> >> > + goto check_pte;
> >> >> >> > +
> >> >> >> > + start_address = folio_start;
> >> >> >> > + start_pte = folio_ptep;
> >> >> >> > + nr_pages = nr;
> >> >> >> > + entry = folio->swap;
> >> >> >> > + page = &folio->page;
> >> >> >> > + }
> >> >> >> > +
> >> >> >> > +check_pte:
> >> >> >> > if (unlikely(!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte)))
> >> >> >> > goto out_nomap;
> >> >> >> >
> >> >> >> > @@ -4190,6 +4223,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> >> >> >> > */
> >> >> >> > exclusive = false;
> >> >> >> > }
> >> >> >> > +
> >> >> >> > + /* Reuse the whole large folio iff all entries are exclusive */
> >> >> >> > + if (nr_pages > 1 && any_swap_shared)
> >> >> >> > + exclusive = false;
> >> >> >> > }
> >> >> >> >
> >>
> >> [snip]
> >>
> >> --
> >> Best Regards,
> >> Huang, Ying
next prev parent reply other threads:[~2024-04-16 4:40 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-09 8:26 [PATCH v2 0/5] large folios swap-in: handle refault cases first Barry Song
2024-04-09 8:26 ` [PATCH v2 1/5] mm: swap: introduce swap_free_nr() for batched swap_free() Barry Song
2024-04-10 23:37 ` SeongJae Park
2024-04-11 1:27 ` Barry Song
2024-04-11 14:30 ` Ryan Roberts
2024-04-12 2:07 ` Chuanhua Han
2024-04-12 11:28 ` Ryan Roberts
2024-04-12 11:38 ` Chuanhua Han
2024-04-15 6:17 ` Huang, Ying
2024-04-15 7:04 ` Barry Song
2024-04-15 8:06 ` Barry Song
2024-04-15 8:19 ` Huang, Ying
2024-04-15 8:34 ` Barry Song
2024-04-15 8:51 ` Huang, Ying
2024-04-15 9:01 ` Barry Song
2024-04-16 1:40 ` Huang, Ying
2024-04-16 2:08 ` Barry Song
2024-04-16 3:11 ` Huang, Ying
2024-04-16 4:32 ` Barry Song
2024-04-17 0:32 ` Huang, Ying
2024-04-17 1:35 ` Barry Song
2024-04-18 5:27 ` Barry Song
2024-04-18 8:55 ` Huang, Ying
2024-04-18 9:14 ` Barry Song
2024-05-02 23:05 ` Barry Song
2024-04-09 8:26 ` [PATCH v2 2/5] mm: swap: make should_try_to_free_swap() support large-folio Barry Song
2024-04-15 7:11 ` Huang, Ying
2024-04-09 8:26 ` [PATCH v2 3/5] mm: swap_pte_batch: add an output argument to reture if all swap entries are exclusive Barry Song
2024-04-11 14:54 ` Ryan Roberts
2024-04-11 15:00 ` David Hildenbrand
2024-04-11 15:36 ` Ryan Roberts
2024-04-09 8:26 ` [PATCH v2 4/5] mm: swap: entirely map large folios found in swapcache Barry Song
2024-04-11 15:33 ` Ryan Roberts
2024-04-11 23:30 ` Barry Song
2024-04-12 11:31 ` Ryan Roberts
2024-04-15 8:37 ` Huang, Ying
2024-04-15 8:53 ` Barry Song
2024-04-16 2:25 ` Huang, Ying
2024-04-16 2:36 ` Barry Song
2024-04-16 2:39 ` Huang, Ying
2024-04-16 2:52 ` Barry Song
2024-04-16 3:17 ` Huang, Ying
2024-04-16 4:40 ` Barry Song [this message]
2024-04-18 9:55 ` Barry Song
2024-04-09 8:26 ` [PATCH v2 5/5] mm: add per-order mTHP swpin_refault counter Barry Song
2024-04-10 23:15 ` SeongJae Park
2024-04-11 1:46 ` Barry Song
2024-04-11 16:14 ` SeongJae Park
2024-04-11 15:53 ` Ryan Roberts
2024-04-11 23:01 ` Barry Song
2024-04-17 0:45 ` Huang, Ying
2024-04-17 1:16 ` Barry Song
2024-04-17 1:38 ` Huang, Ying
2024-04-17 1:48 ` Barry Song
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:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAGsJ_4x5FJrpGLzvrEZ-mgWiSUVaO-41h6Dcy7VGyaMMC8Tmng@mail.gmail.com \
--to=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=chrisl@kernel.org \
--cc=david@redhat.com \
--cc=hanchuanhua@oppo.com \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=kasong@tencent.com \
--cc=khalid.aziz@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryan.roberts@arm.com \
--cc=surenb@google.com \
--cc=v-songbaohua@oppo.com \
--cc=willy@infradead.org \
--cc=xiang@kernel.org \
--cc=ying.huang@intel.com \
--cc=yosryahmed@google.com \
--cc=yuzhao@google.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox