From patchwork Mon Mar 13 12:45:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13172444 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 9D333C6FD19 for ; Mon, 13 Mar 2023 12:45:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2356A6B0075; Mon, 13 Mar 2023 08:45:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E52A6B007B; Mon, 13 Mar 2023 08:45:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D4036B007D; Mon, 13 Mar 2023 08:45:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id F185B6B0075 for ; Mon, 13 Mar 2023 08:45:10 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C9995A0BA5 for ; Mon, 13 Mar 2023 12:45:10 +0000 (UTC) X-FDA: 80563845180.16.3E633AB Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf27.hostedemail.com (Postfix) with ESMTP id B7E454001E for ; Mon, 13 Mar 2023 12:45:08 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=U76QrLbb; spf=pass (imf27.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678711508; a=rsa-sha256; cv=none; b=8m/AZvcCqPxvJnqnwuPXUA/Hfsnq4rzE+LBkyK0lociWd8aUHV26KoLJMW3JP0vfNCbjYP 6Qlh8H+3YkpZ+oGZQElI4K1LMF3jWk+aebxWck1tU976cnisSU9hhKxcLi+3T20Co4JYmv 0WNolUqkHH5Hi6DGlRJvFCboNSrSHms= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=U76QrLbb; spf=pass (imf27.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 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=1678711508; 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=AzV5UcBSaYj6VDh9ha107vVAIwK2e7Ly4U9WmNu6xts=; b=vLtlmvUQ4S/1IcbGuEG/J9Evjhx5089P9Tv3z/OxdMzkT2+PpNGtvM7GprpjL6W+3NthOp /6mhNc92tat0WO90DVa3Jvqao1yqfKblA6L1sq2q0rrS5RW28rIsNWK7203gDp+3sLv/8x n8t9cBND+LWUnJfLEh28AqmDJnvzGog= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678711508; x=1710247508; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WxOc3fPEg3ei7kjqLpPuFsRhUIe1Zw1ZeIqSXy7g7uE=; b=U76QrLbbJv4Uy9D9cYoKcmvPcXTC+f3utlEJ2zpgcUA6Um916lUA7t/s 3Yy2IVIlUjTCIFK3B5NF6xSm7hb4NQhbiPmZvFI2fUtSYaHWQQebEIfqb 2yAtEZmVVi9KmSJwDyvW3ZXpyEHFGBr4JpYlfHeYZdGFdMlxKt/HdW+s2 Uu9hsX/peU9k+lVM8VuL5aYAKS3wHmANHgWgOJalnulxbqQG82LTtSMNX CzRPZbngWZbpI0geqRQ7O4WHPwcYqHkYzFA+wtMhTHxkt/u8upEGhKQ5Q 827G2n2vLhmoid+IuSve4Efuy6fZXypeH/VLoXqqgj/fidLpsbZNDSw2n A==; X-IronPort-AV: E=McAfee;i="6500,9779,10647"; a="399727558" X-IronPort-AV: E=Sophos;i="5.98,256,1673942400"; d="scan'208";a="399727558" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Mar 2023 05:45:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10647"; a="767683536" X-IronPort-AV: E=Sophos;i="5.98,256,1673942400"; d="scan'208";a="767683536" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 13 Mar 2023 05:45:04 -0700 From: Yin Fengwei To: linux-mm@kvack.org, akpm@linux-foundation.org, willy@infradead.org, mike.kravetz@oracle.com, sidhartha.kumar@oracle.com, naoya.horiguchi@nec.com, jane.chu@oracle.com, david@redhat.com Cc: fengwei.yin@intel.com Subject: [PATCH v4 5/5] try_to_unmap_one: batched remove rmap, update folio refcount Date: Mon, 13 Mar 2023 20:45:26 +0800 Message-Id: <20230313124526.1207490-6-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230313124526.1207490-1-fengwei.yin@intel.com> References: <20230313124526.1207490-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: B7E454001E X-Rspamd-Server: rspam01 X-Stat-Signature: bf6iirch4611m8om9gzaur6t9dxc43ed X-HE-Tag: 1678711508-876921 X-HE-Meta: U2FsdGVkX19mpuir1vjEgGfyfFhMauuV0pJoILdwemgLWA1DgcYJTOey2cSh3a8T2ccQ8en6E2dUy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RBrRjMIT7F9mUMqBE6Ke3YtcH0prSaL9siIp5doa/xmU4CrOp7 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 unmap one page fails, or the vma walk will skip next pte, or the vma walk will end on next pte, batched remove map, update folio refcount. Signed-off-by: Yin Fengwei --- include/linux/rmap.h | 1 + mm/page_vma_mapped.c | 30 +++++++++++++++++++++++++++ mm/rmap.c | 48 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index d2569b42e21a..18193d1d5a8e 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -424,6 +424,7 @@ static inline void page_vma_mapped_walk_done(struct page_vma_mapped_walk *pvmw) } bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw); +bool pvmw_walk_skip_or_end_on_next(struct page_vma_mapped_walk *pvmw); /* * Used by swapoff to help locate where page is expected in vma. diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index 4e448cfbc6ef..d5d69d02f08b 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -291,6 +291,36 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) return false; } +/** + * pvmw_walk_skip_or_end_on_next - check if next pte will be skipped or + * end the walk + * @pvmw: pointer to struct page_vma_mapped_walk. + * + * This function can only be called with correct pte lock hold + */ +bool pvmw_walk_skip_or_end_on_next(struct page_vma_mapped_walk *pvmw) +{ + unsigned long address = pvmw->address + PAGE_SIZE; + + if (address >= vma_address_end(pvmw)) + return true; + + if ((address & (PMD_SIZE - PAGE_SIZE)) == 0) + return true; + + pvmw->pte++; + if (pte_none(*pvmw->pte)) + return true; + + if (!check_pte(pvmw)) { + pvmw->pte--; + return true; + } + pvmw->pte--; + + return false; +} + /** * page_mapped_in_vma - check whether a page is really mapped in a VMA * @page: the page to test diff --git a/mm/rmap.c b/mm/rmap.c index bd5331dc9d44..60314c76df59 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1741,6 +1741,26 @@ static bool try_to_unmap_one_page(struct folio *folio, return false; } +static void folio_remove_rmap_and_update_count(struct folio *folio, + struct page *start, struct vm_area_struct *vma, int count) +{ + if (count == 0) + return; + + /* + * No need to call mmu_notifier_invalidate_range() it has be + * done above for all cases requiring it to happen under page + * table lock before mmu_notifier_invalidate_range_end() + * + * See Documentation/mm/mmu_notifier.rst + */ + folio_remove_rmap_range(folio, start, count, vma, + folio_test_hugetlb(folio)); + if (vma->vm_flags & VM_LOCKED) + mlock_drain_local(); + folio_ref_sub(folio, count); +} + /* * @arg: enum ttu_flags will be passed to this argument */ @@ -1748,10 +1768,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, unsigned long address, void *arg) { DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); - struct page *subpage; + struct page *start = NULL; bool ret = true; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; + int count = 0; /* * When racing against e.g. zap_pte_range() on another cpu, @@ -1812,26 +1833,31 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, break; } - subpage = folio_page(folio, + if (!start) + start = folio_page(folio, pte_pfn(*pvmw.pte) - folio_pfn(folio)); ret = try_to_unmap_one_page(folio, vma, range, pvmw, address, flags); if (!ret) { + folio_remove_rmap_and_update_count(folio, + start, vma, count); page_vma_mapped_walk_done(&pvmw); break; } + count++; /* - * No need to call mmu_notifier_invalidate_range() it has be - * done above for all cases requiring it to happen under page - * table lock before mmu_notifier_invalidate_range_end() - * - * See Documentation/mm/mmu_notifier.rst + * If next pte will be skipped in page_vma_mapped_walk() or + * the walk will end at it, batched remove rmap and update + * page refcount. We can't do it after page_vma_mapped_walk() + * return false because the pte lock will not be hold. */ - page_remove_rmap(subpage, vma, false); - if (vma->vm_flags & VM_LOCKED) - mlock_drain_local(); - folio_put(folio); + if (pvmw_walk_skip_or_end_on_next(&pvmw)) { + folio_remove_rmap_and_update_count(folio, + start, vma, count); + count = 0; + start = NULL; + } } mmu_notifier_invalidate_range_end(&range);