From patchwork Fri Jul 28 07:09:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13331303 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8CD8EB64DD for ; Fri, 28 Jul 2023 07:11:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 562BD6B0075; Fri, 28 Jul 2023 03:11:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C4856B0078; Fri, 28 Jul 2023 03:11:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33F666B007B; Fri, 28 Jul 2023 03:11:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 22D186B0075 for ; Fri, 28 Jul 2023 03:11:29 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DC096120FBF for ; Fri, 28 Jul 2023 07:11:28 +0000 (UTC) X-FDA: 81060149856.06.F4ABF79 Received: from mgamail.intel.com (unknown [192.55.52.43]) by imf26.hostedemail.com (Postfix) with ESMTP id 870CF140017 for ; Fri, 28 Jul 2023 07:11:26 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QoA93gDN; spf=pass (imf26.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690528287; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EePU6xPznB5l9WtFwzndYBUAVWzLnMkN9vq47y9+zRY=; b=IA+b+szlgy+0Zq/Nw7LckWIv13wD7GY3ze/POyLRtEF6BuTbOoywfSulbwDfea/1Xb++Gn 0nytNYPn1X0dlgUTEHd1HdTeA3F/ytF6aWiZVylCtQcPGVLPMrgF+HSiHNnBCuMS/biJM/ Toml2oDu/Vr0cMpoG9PEd+Pk6AVXO0k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690528287; a=rsa-sha256; cv=none; b=3cP8TYWdkQ0xpNU/N4AWuDzuzGxs8fMp6yJH2qVVYxEN9aV1EX7X3RbMON/OP0f8ncl2Rz lFv69r0HQ4JJjSryy0JIAmEapxIj2klXxw42shmHnmrJno/PyAWdckvSdII8d4MdCaj2sm H7uu4zgOyB65PzVx9H9k9b9MTAnUv+A= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QoA93gDN; spf=pass (imf26.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690528286; x=1722064286; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hSd/zYXrNe6G/TyNoyJX1Y9vNt38Z30KKrdYuOwbs5U=; b=QoA93gDNTs2R27Q9OvJtqUI5bcyZGejODNep5nWoHIpOnKE8Tgc3do26 nzvkfCkmtOaJawScGXKyPNXkyU/dkrbZn8Q4jgjkT1dFF/v8nXoJY0Ivf jdDI08ofx9gpfpi766gShtJaMT7L9SvL4j7bufxE12Yj/hKpt8vDUcLdk 6S3ygnqlBGdz7lR3IzgJ2Li5TmBntRdcqOLfbtkRkmcSkHlRCfTn2u6dh GB2S3OS5RoRgA4YC7hW0fkUyXS/9X90qjtk0BSDoJ8dlTWnJ6Vnbwyrci +wWuqRZawjZB7lIFd85crssdUAtV/638ryokknhlDp94+PQXuvZeZ2FX8 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10784"; a="454888197" X-IronPort-AV: E=Sophos;i="6.01,236,1684825200"; d="scan'208";a="454888197" 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 X-Rspamd-Queue-Id: 870CF140017 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 9b7bkzso97gom43k7djhk4be5rc9t6xu X-HE-Tag: 1690528286-412005 X-HE-Meta: U2FsdGVkX1+iyiV95pNCYGIYM9KTEnAQBRYGRSRAqe+lInUO+7ajDKSwLQryeK15HYJavJnZKuPXzB1pgfOxP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bg9CsctL4YIv3nr9M8KMKga4dZ03tKzFbFgcG0+fBTOlcDGCmygigo+IEoNsxaQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 *folio, * still be set while VM_SPECIAL bits are added: so ignore it then. */ if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED) && - (compound || !folio_test_large(folio))) + folio_within_vma(folio, vma, pte)) mlock_folio(folio); } 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); } 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 = pvmw.address; - 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 |= 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); } - mlock_vma_folio(folio, vma, compound); + mlock_vma_folio(folio, vma, NULL); } /** @@ -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); - mlock_vma_folio(folio, vma, compound); + mlock_vma_folio(folio, vma, NULL); } /** @@ -1383,7 +1396,7 @@ static void __remove_rmap_finish(struct folio *folio, * it's only reliable while mapped. */ - munlock_vma_folio(folio, vma, compound); + munlock_vma_folio(folio, vma); } /** @@ -1557,7 +1570,7 @@ static bool try_to_unmap_one(struct folio *folio, struct 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 = false; break;