From nobody Sun Feb 8 10:22:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9750BEB64DD for ; Fri, 28 Jul 2023 07:12:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233974AbjG1HL7 (ORCPT ); Fri, 28 Jul 2023 03:11:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233961AbjG1HLc (ORCPT ); Fri, 28 Jul 2023 03:11:32 -0400 Received: from mgamail.intel.com (unknown [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CEF03C2F for ; Fri, 28 Jul 2023 00:11:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690528273; x=1722064273; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hDGtd2tgXIan13dy2PEKZ68DXcw4gSjf8pK8pQRP1gI=; b=Lg3rd+VevH6UWHbFj1yVn/9CjWW7Et/34lbFAdqH9mGZTzT5/xz1muVe qnhPUDWwa3879cXvwmP6fykEMsWRTSHcQOs8qivWB/ivY3UPEJdOpNTqk THRtKEqR8f/XRla9fuBXhtHwfp/kqh9//PihcbjgbHHIOpuX4fvIYx+CN AshUlA3bZM4HIRs96y7S7uxHbd/3dDKRota7O6CJwuchoEJHjJLChgXd7 7BtZPrWu+dYA62kOu1YNSxUNE5I8Yg4Zmd/N68duglT3jTRHPMmKcEvrE N2e56q3QwACogrTBs1DAXLTH+GoESJn/KJMxVVmw6NikvI4igLQTHHlwk g==; X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="454888119" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="454888119" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jul 2023 00:11:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="721161998" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="721161998" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by orsmga007.jf.intel.com with ESMTP; 28 Jul 2023 00:11:08 -0700 From: Yin Fengwei 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 1/3] mm: add functions folio_in_range() and folio_within_vma() Date: Fri, 28 Jul 2023 15:09:27 +0800 Message-Id: <20230728070929.2487065-2-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230728070929.2487065-1-fengwei.yin@intel.com> References: <20230728070929.2487065-1-fengwei.yin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" It will be used to check whether the folio is mapped to specific VMA and whether the mapping address of folio is in the range. Also a helper function folio_within_vma() to check whether folio is in the range of vma based on folio_in_range(). Signed-off-by: Yin Fengwei --- mm/internal.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/mm/internal.h b/mm/internal.h index 5a03bc4782a2..63de32154a48 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -585,6 +585,75 @@ extern long faultin_vma_page_range(struct vm_area_stru= ct *vma, bool write, int *locked); extern bool mlock_future_ok(struct mm_struct *mm, unsigned long flags, unsigned long bytes); + +/* + * Check whether the folio is in specific range + * + * First, check whether the folio is in the range of vma. + * Then, check whether the folio is mapped to the range of [start, end]. + * In the end, check whether the folio is fully mapped to the range. + * + * @pte page table pointer will be checked whether the large folio + * is fully mapped to. Currently, if mremap in the middle of + * large folio, the large folio could be mapped to to different + * VMA and address check can't identify this situation. + */ +static inline bool +folio_in_range(struct folio *folio, struct vm_area_struct *vma, + unsigned long start, unsigned long end, pte_t *pte) +{ + pte_t ptent; + unsigned long i, nr =3D folio_nr_pages(folio); + pgoff_t pgoff, addr; + unsigned long vma_pglen =3D (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; + + VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio); + + if (start < vma->vm_start) + start =3D vma->vm_start; + if (end > vma->vm_end) + end =3D vma->vm_end; + + pgoff =3D folio_pgoff(folio); + /* if folio start address is not in vma range */ + if (pgoff < vma->vm_pgoff || pgoff > vma->vm_pgoff + vma_pglen) + return false; + + addr =3D vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); + if (addr < start || end - addr < folio_size(folio)) + return false; + + /* not necessary to check pte for none large folio */ + if (!folio_test_large(folio)) + return true; + + if (!pte) + return false; + + /* check whether parameter pte is associated with folio */ + ptent =3D ptep_get(pte); + if (pte_none(ptent) || !pte_present(ptent) || + pte_pfn(ptent) - folio_pfn(folio) >=3D nr) + return false; + + pte -=3D pte_pfn(ptent) - folio_pfn(folio); + for (i =3D 0; i < nr; i++, pte++) { + ptent =3D ptep_get(pte); + + if (pte_none(ptent) || !pte_present(ptent) || + pte_pfn(ptent) - folio_pfn(folio) >=3D nr) + return false; + } + + return true; +} + +static inline bool +folio_within_vma(struct folio *folio, struct vm_area_struct *vma, pte_t *p= te) +{ + return folio_in_range(folio, vma, vma->vm_start, vma->vm_end, pte); +} + /* * mlock_vma_folio() and munlock_vma_folio(): * should be called with vma's mmap_lock held for read or write, --=20 2.39.2 From nobody Sun Feb 8 10:22:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1962C0015E for ; Fri, 28 Jul 2023 07:12:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234060AbjG1HMI (ORCPT ); Fri, 28 Jul 2023 03:12:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233894AbjG1HLl (ORCPT ); Fri, 28 Jul 2023 03:11:41 -0400 Received: from mgamail.intel.com (unknown [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8725C422A for ; Fri, 28 Jul 2023 00:11:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690528285; x=1722064285; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hSd/zYXrNe6G/TyNoyJX1Y9vNt38Z30KKrdYuOwbs5U=; b=CmWMzXSya3FiCkiG8mzrdvN0iFs90cJ8ROVs5FRo5iLfz6PWlkgSF5Ib A8I19dv+qsA9SUjiUn/T1VfdW79yARTyWkC7rWIkOE69VMkTWwxdkD2Fh F/5ZYAg7zrfX+tgWuga/5hcKFiO/3RYQDra02LdeVNC3mqYzAZq2hQMxX kuOtJyg71uJ+iq7d7zqpWxKucXifVw2Euh2BqY2EIHpoNOY3E8dHJ15KH DV2Azwp92TzsVZ2olXQ8+CxJvLFokvEqJDNaRh0NhTe+V8xl8ArKz+6yI pENssW5TV5//fIN/7/P7ubIJXiMfiTvhfxqLxr2bk3wKBuAKIiq+YnRMB g==; X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="454888196" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="454888196" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jul 2023 00:11:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="721162102" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="721162102" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by orsmga007.jf.intel.com with ESMTP; 28 Jul 2023 00:11:22 -0700 From: Yin Fengwei 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 2/3] mm: handle large folio when large folio in VM_LOCKED VMA range Date: Fri, 28 Jul 2023 15:09:28 +0800 Message-Id: <20230728070929.2487065-3-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230728070929.2487065-1-fengwei.yin@intel.com> References: <20230728070929.2487065-1-fengwei.yin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If large folio is in the range of VM_LOCKED VMA, it should be mlocked to avoid being picked by page reclaim. Which may split the large folio and then mlock each pages again. Mlock this kind of large folio to prevent them being picked by page reclaim. For the large folio which cross the boundary of VM_LOCKED VMA, we'd better not to mlock it. So if the system is under memory pressure, this kind of large folio will be split and the pages ouf of VM_LOCKED VMA can be reclaimed. for page_add_anon_rmap() and page_add_file_rmap(), we only mlock the folio if it's not large folio. The reason to do so is that these functions can be called couple of times for a large folio and each call just covered piece of large folio. If folio is mlocked multiple time, the folio->mlock_count can be imbalance. Delay the folio mlock to page reclaim phase. As only mlock folio once for sure in page reclaim phase. Signed-off-by: Yin Fengwei --- mm/internal.h | 18 +++++++++--------- mm/rmap.c | 27 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 63de32154a48..6c6fb1f3e4c1 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -662,14 +662,10 @@ folio_within_vma(struct folio *folio, struct vm_area_= struct *vma, pte_t *pte) * mlock is usually called at the end of page_add_*_rmap(), munlock at * the end of page_remove_rmap(); but new anon folios are managed by * folio_add_lru_vma() calling mlock_new_folio(). - * - * @compound is used to include pmd mappings of THPs, but filter out - * pte mappings of THPs, which cannot be consistently counted: a pte - * mapping of the THP head cannot be distinguished by the page alone. */ void mlock_folio(struct folio *folio); static inline void mlock_vma_folio(struct folio *folio, - struct vm_area_struct *vma, bool compound) + struct vm_area_struct *vma, pte_t *pte) { /* * The VM_SPECIAL check here serves two purposes. @@ -680,16 +676,20 @@ static inline void mlock_vma_folio(struct folio *foli= o, * still be set while VM_SPECIAL bits are added: so ignore it then. */ if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) =3D=3D VM_LOCKED) && - (compound || !folio_test_large(folio))) + folio_within_vma(folio, vma, pte)) mlock_folio(folio); } =20 void munlock_folio(struct folio *folio); static inline void munlock_vma_folio(struct folio *folio, - struct vm_area_struct *vma, bool compound) + struct vm_area_struct *vma) { - if (unlikely(vma->vm_flags & VM_LOCKED) && - (compound || !folio_test_large(folio))) + /* + * To handle the case that a mlocked large folio is unmapped from VMA + * piece by piece, allow munlock the large folio which is partially + * mapped to VMA. + */ + if (unlikely(vma->vm_flags & VM_LOCKED)) munlock_folio(folio); } =20 diff --git a/mm/rmap.c b/mm/rmap.c index 54124f18e0e4..1d8f048fbed8 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -798,6 +798,7 @@ struct folio_referenced_arg { unsigned long vm_flags; struct mem_cgroup *memcg; }; + /* * arg: folio_referenced_arg will be passed */ @@ -811,10 +812,22 @@ static bool folio_referenced_one(struct folio *folio, while (page_vma_mapped_walk(&pvmw)) { address =3D pvmw.address; =20 - if ((vma->vm_flags & VM_LOCKED) && - (!folio_test_large(folio) || !pvmw.pte)) { + if (vma->vm_flags & VM_LOCKED) { + if (!folio_within_vma(folio, vma, pvmw.pte)) { + /* + * For large folio cross VMA boundaries, it's + * expected to be picked by page reclaim. But + * should skip reference of pages which are in + * the range of VM_LOCKED vma. As page reclaim + * should just count the reference of pages out + * the range of VM_LOCKED vma. + */ + pra->mapcount--; + continue; + } + /* Restore the mlock which got missed */ - mlock_vma_folio(folio, vma, !pvmw.pte); + mlock_vma_folio(folio, vma, pvmw.pte); page_vma_mapped_walk_done(&pvmw); pra->vm_flags |=3D VM_LOCKED; return false; /* To break the loop */ @@ -1253,7 +1266,7 @@ void page_add_anon_rmap(struct page *page, struct vm_= area_struct *vma, __page_check_anon_rmap(folio, page, vma, address); } =20 - mlock_vma_folio(folio, vma, compound); + mlock_vma_folio(folio, vma, NULL); } =20 /** @@ -1344,7 +1357,7 @@ void page_add_file_rmap(struct page *page, struct vm_= area_struct *vma, if (nr) __lruvec_stat_mod_folio(folio, NR_FILE_MAPPED, nr); =20 - mlock_vma_folio(folio, vma, compound); + mlock_vma_folio(folio, vma, NULL); } =20 /** @@ -1383,7 +1396,7 @@ static void __remove_rmap_finish(struct folio *folio, * it's only reliable while mapped. */ =20 - munlock_vma_folio(folio, vma, compound); + munlock_vma_folio(folio, vma); } =20 /** @@ -1557,7 +1570,7 @@ static bool try_to_unmap_one(struct folio *folio, str= uct vm_area_struct *vma, if (!(flags & TTU_IGNORE_MLOCK) && (vma->vm_flags & VM_LOCKED)) { /* Restore the mlock which got missed */ - mlock_vma_folio(folio, vma, false); + mlock_vma_folio(folio, vma, pvmw.pte); page_vma_mapped_walk_done(&pvmw); ret =3D false; break; --=20 2.39.2 From nobody Sun Feb 8 10:22:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E00A1C0015E for ; Fri, 28 Jul 2023 07:12:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234028AbjG1HMZ (ORCPT ); Fri, 28 Jul 2023 03:12:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233954AbjG1HLz (ORCPT ); Fri, 28 Jul 2023 03:11:55 -0400 Received: from mgamail.intel.com (unknown [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEECD3C03 for ; Fri, 28 Jul 2023 00:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690528299; x=1722064299; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MK9O8/4a3BabvdoGe2yFp5MoF/EC/SVzWLogYQWhbR8=; b=aziYUI/MWVRk6qA2oQFyB8mzvsJDyLXUXsOwfKI5A/gYSNC0sN8A4yAi ZcNLUxvNFj47TDg2dvJo9eaotmayzPKSfnPLThjG1c3zuHn43antlOCIk RLQiL1VNxqxWbswL5WMmh+ZgUbEDGJstQXNB0Xirki+AFBmw83Klgf9k1 xWYlaWHleOUkr9t1WAwCjpLMyx4zYn61l2G5BTFu+G+bOYKcUJqsP+01U Jg0jMBAoGrakU1h7NWEmfg65xdlUutaBfcoDnflJbC17/iAj8F1RU341M woIjFveTSfyuh1xtEpQy7G2wqPZHydro/3nws36F7BlAQslGy95FQTM8E A==; X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="454888240" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="454888240" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jul 2023 00:11:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="721162234" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="721162234" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by orsmga007.jf.intel.com with ESMTP; 28 Jul 2023 00:11:35 -0700 From: Yin Fengwei 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 Message-Id: <20230728070929.2487065-4-fengwei.yin@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230728070929.2487065-1-fengwei.yin@intel.com> References: <20230728070929.2487065-1-fengwei.yin@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- 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); } =20 +static inline unsigned int folio_mlock_step(struct folio *folio, + pte_t *pte, unsigned long addr, unsigned long end) +{ + unsigned int count, i, nr =3D folio_nr_pages(folio); + unsigned long pfn =3D folio_pfn(folio); + pte_t ptent =3D ptep_get(pte); + + if (!folio_test_large(folio)) + return 1; + + count =3D pfn + nr - pte_pfn(ptent); + count =3D min_t(unsigned int, count, (end - addr) >> PAGE_SHIFT); + + if (!pte) + return count; + + for (i =3D 0; i < count; i++, pte++) { + pte_t entry =3D ptep_get(pte); + + if (pte_none(entry) || !pte_present(entry)) + break; + if (pte_pfn(entry) - pfn >=3D 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) =20 @@ -314,6 +358,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long ad= dr, pte_t *start_pte, *pte; pte_t ptent; struct folio *folio; + unsigned int step =3D 1; =20 ptl =3D pmd_trans_huge_lock(pmd, vma); if (ptl) { @@ -334,6 +379,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long ad= dr, walk->action =3D ACTION_AGAIN; return 0; } + for (pte =3D start_pte; addr !=3D end; pte++, addr +=3D PAGE_SIZE) { ptent =3D ptep_get(pte); if (!pte_present(ptent)) @@ -341,12 +387,19 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long = addr, folio =3D vm_normal_folio(vma, addr, ptent); if (!folio || folio_is_zone_device(folio)) continue; - if (folio_test_large(folio)) - continue; + + step =3D 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 +=3D step - 1; + addr +=3D (step - 1) << PAGE_SHIFT; } pte_unmap(start_pte); out: --=20 2.39.2