mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yin Fengwei <fengwei.yin@intel.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	akpm@linux-foundation.org, yuzhao@google.com,
	willy@infradead.org, david@redhat.com, ryan.roberts@arm.com,
	shy828301@gmail.com, hughd@google.com
Cc: fengwei.yin@intel.com
Subject: [PATCH 3/3] mm: mlock: update mlock_pte_range to handle large folio
Date: Fri, 28 Jul 2023 15:09:29 +0800	[thread overview]
Message-ID: <20230728070929.2487065-4-fengwei.yin@intel.com> (raw)
In-Reply-To: <20230728070929.2487065-1-fengwei.yin@intel.com>

Current kernel only lock base size folio during mlock syscall.
Add large folio support with following rules:
  - Only mlock large folio when it's in VM_LOCKED VMA range
    and fully mapped to page table.

    fully mapped folio is required to handle the case that
    mremap happens in the middle of large folio and split
    pages of large folio to two different VMA.

  - munlock will apply to the large folio which is in VMA range
    or cross the VMA boundary.

    This is required to handle the case that the large folio is
    mlocked, later the VMA is split in the middle of large folio.

Signed-off-by: Yin Fengwei <fengwei.yin@intel.com>
---
 mm/mlock.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 55 insertions(+), 2 deletions(-)

diff --git a/mm/mlock.c b/mm/mlock.c
index 0a0c996c5c21..8056f9176b70 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -305,6 +305,50 @@ void munlock_folio(struct folio *folio)
 	local_unlock(&mlock_fbatch.lock);
 }
 
+static inline unsigned int folio_mlock_step(struct folio *folio,
+		pte_t *pte, unsigned long addr, unsigned long end)
+{
+	unsigned int count, i, nr = folio_nr_pages(folio);
+	unsigned long pfn = folio_pfn(folio);
+	pte_t ptent = ptep_get(pte);
+
+	if (!folio_test_large(folio))
+		return 1;
+
+	count = pfn + nr - pte_pfn(ptent);
+	count = min_t(unsigned int, count, (end - addr) >> PAGE_SHIFT);
+
+	if (!pte)
+		return count;
+
+	for (i = 0; i < count; i++, pte++) {
+		pte_t entry = ptep_get(pte);
+
+		if (pte_none(entry) || !pte_present(entry))
+			break;
+		if (pte_pfn(entry) - pfn >= nr)
+			break;
+	}
+
+	return i;
+}
+
+static inline bool should_mlock_folio(struct folio *folio,
+				struct vm_area_struct *vma, pte_t *pte)
+{
+	/*
+	 * For unlock, allow munlock large folio which is partially
+	 * mapped to VMA. As it's possible that large folio is
+	 * mlocked and VMA is split later.
+	 *
+	 * During memory pressure, such kind of large folio can
+	 * be split. And the pages are not in VM_LOCKed VMA
+	 * can be reclaimed.
+	 */
+	return !(vma->vm_flags & VM_LOCKED) ||
+			folio_within_vma(folio, vma, pte);
+}
+
 static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
 			   unsigned long end, struct mm_walk *walk)
 
@@ -314,6 +358,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
 	pte_t *start_pte, *pte;
 	pte_t ptent;
 	struct folio *folio;
+	unsigned int step = 1;
 
 	ptl = pmd_trans_huge_lock(pmd, vma);
 	if (ptl) {
@@ -334,6 +379,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
 		walk->action = ACTION_AGAIN;
 		return 0;
 	}
+
 	for (pte = start_pte; addr != end; pte++, addr += PAGE_SIZE) {
 		ptent = ptep_get(pte);
 		if (!pte_present(ptent))
@@ -341,12 +387,19 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr,
 		folio = vm_normal_folio(vma, addr, ptent);
 		if (!folio || folio_is_zone_device(folio))
 			continue;
-		if (folio_test_large(folio))
-			continue;
+
+		step = folio_mlock_step(folio, pte, addr, end);
+		if (!should_mlock_folio(folio, vma, pte))
+			goto next_entry;
+
 		if (vma->vm_flags & VM_LOCKED)
 			mlock_folio(folio);
 		else
 			munlock_folio(folio);
+
+next_entry:
+		pte += step - 1;
+		addr += (step - 1) << PAGE_SHIFT;
 	}
 	pte_unmap(start_pte);
 out:
-- 
2.39.2


      parent reply	other threads:[~2023-07-28  7:12 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-28  7:09 [PATCH 0/3] support large folio for mlock Yin Fengwei
2023-07-28  7:09 ` [PATCH 1/3] mm: add functions folio_in_range() and folio_within_vma() Yin Fengwei
2023-07-28 18:34   ` Andrew Morton
2023-07-29 13:54     ` Yin, Fengwei
2023-08-02 11:14   ` Ryan Roberts
2023-08-02 11:35     ` Ryan Roberts
2023-08-02 13:12       ` Yin, Fengwei
2023-08-02 15:12         ` Ryan Roberts
2023-08-03  1:36           ` Yin Fengwei
2023-08-02 12:50     ` Yin, Fengwei
2023-08-02 13:09       ` Ryan Roberts
2023-08-02 13:46         ` Yin, Fengwei
2023-08-02 14:08           ` Ryan Roberts
2023-08-02 14:14             ` Yin, Fengwei
2023-08-02 14:59               ` Ryan Roberts
2023-08-03  0:24                 ` Yin Fengwei
2023-08-02 15:15   ` Ryan Roberts
2023-08-03  0:41     ` Yin Fengwei
2023-08-03  9:58   ` Ryan Roberts
2023-08-03 10:48     ` Yin Fengwei
2023-08-03 13:20       ` Ryan Roberts
2023-08-03 23:15         ` Yin, Fengwei
2023-08-04  8:46           ` Ryan Roberts
2023-08-04  9:05             ` Yin, Fengwei
2023-07-28  7:09 ` [PATCH 2/3] mm: handle large folio when large folio in VM_LOCKED VMA range Yin Fengwei
2023-07-28  7:09 ` Yin Fengwei [this message]

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=20230728070929.2487065-4-fengwei.yin@intel.com \
    --to=fengwei.yin@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=hughd@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ryan.roberts@arm.com \
    --cc=shy828301@gmail.com \
    --cc=willy@infradead.org \
    --cc=yuzhao@google.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

all inboxes | Powered by JetHome®