From patchwork Mon Feb 13 12:34:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13138389 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 DEDD1C636CC for ; Mon, 13 Feb 2023 12:35:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C0BA6B0080; Mon, 13 Feb 2023 07:35:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 749126B0081; Mon, 13 Feb 2023 07:35:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59C846B0082; Mon, 13 Feb 2023 07:35:50 -0500 (EST) 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 41DF66B0080 for ; Mon, 13 Feb 2023 07:35:50 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 15E1E12042B for ; Mon, 13 Feb 2023 12:35:50 +0000 (UTC) X-FDA: 80462215260.22.DD65D4E Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf22.hostedemail.com (Postfix) with ESMTP id F035DC0012 for ; Mon, 13 Feb 2023 12:35:47 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XM+rg5ZK; spf=pass (imf22.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=ying.huang@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=1676291748; 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=g5jW1/79Tv0OPDamwb6MdcM8qsaeNohjOfSEKq5e9pg=; b=hye3rQzQ9qOMlVy4OoxygsS/5LRDA3XmUTbuh1rcQReeCI2OUZs8BcTKQNyj0qRxv/3bjW DUiGRg/Q2xDnhw3/l5TbxPW3zYa7+D/ApCrnFHfFteASmfWfjdf5qRU7v1/Ufkg0IADgzm iy6ihNCQoohGqHS44N4+Gjpv2v5Bb04= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=XM+rg5ZK; spf=pass (imf22.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.136 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676291748; a=rsa-sha256; cv=none; b=KsWe1Nd06VEjEaN68wJXGXgstkyo+08q5tBUZRU8kCSgRlaYTp7H0z4dzxgHmqKmu/tfXU fKSmjjEMP6/HY1dfljmqR+zeTFWRQdpf3AU9nm7HJr3MNpcQ3Lq9ImNLFI0y9/5WqUZPtB 1R0aSBK6t9wkmI/DnAeDTHIR2QwlzDY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291748; x=1707827748; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VYkt38A9z8Pq4upkiF6Pp5/5nTEJdBdjh8yFl0FvjyA=; b=XM+rg5ZKTHufyu94tHWEcYZp03nAHVZiLewqM9FgrnrnPoRERIBYXSJ9 dIGQkPpsmkDQ+oe/30UCd6bKTDTVUEWPGnx2es5WLy8XYkfReWJfjen6F 8+c3Lhe7lrMc5OaEojuvqEUo9uYvv4xNs+/mpBTG7+0dg4jyL0udXlY1c ehOggZ/AXeUT80db+VDX9GxhRpjTAt2OaGCRiBPqGrmLgv8gAPlKFSzk0 Wl/JdU5bPy2Nvf4n48QkIAZWkokkz0MSbo7NW/pvYgmRh4PygwP4dGBMH KPHXKQ4VmS6wwknTAfuRvmZ3sdfu+0+LWvtmU9zc8zYfy2tgaMj/ci5is g==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513314" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513314" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 04:35:47 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366663" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366663" Received: from changxin-mobl2.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.171]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 04:35:43 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Xin Hao , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 8/9] migrate_pages: batch flushing TLB Date: Mon, 13 Feb 2023 20:34:43 +0800 Message-Id: <20230213123444.155149-9-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213123444.155149-1-ying.huang@intel.com> References: <20230213123444.155149-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: F035DC0012 X-Stat-Signature: hmfugsquncobtbuudabgmhs8aahebznz X-HE-Tag: 1676291747-411301 X-HE-Meta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cWjdjkQmuhQLEWoRUkU9zFPKEghmbhnoPjGdBMXB622CoiNDkWMaivr4Fq2qY6kcy4ksAcm44tDnrZEs3OR9NXiC7wJmjY2asxOC7VY2p47BLVl3cR+UrAeisd0AS1r3Wpmkhe4oOh8GqXB92AZX9DaF05epEbR9k0GZXF8dmKxMMjadDRNL8tQDtzWCkeT7nUbjnEGCXduA2YZhK46SHMePmlnJMj65AS+zI/S7Abo7zVT/fdfwvd5M6rEv+hdc2reVbvVNSuti4yVhTC9G2eagCKUHYCGtfg9W1+5yrZHD4ui+Gvr13FCzbz4AwMofy8IYvQAAo3sbMOnYeawi3wINoS2z0EX7WCvFwvm2QgK/hFS1Fyp5ruHtIK5QjP6hH8TgvlkVzwou9PMtlYuE4hvdrmXUlZQP1seZyMUJJ19mMzzn+yZiA== 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: The TLB flushing will cost quite some CPU cycles during the folio migration in some situations. For example, when migrate a folio of a process with multiple active threads that run on multiple CPUs. After batching the _unmap and _move in migrate_pages(), the TLB flushing can be batched easily with the existing TLB flush batching mechanism. This patch implements that. We use the following test case to test the patch. On a 2-socket Intel server, - Run pmbench memory accessing benchmark - Run `migratepages` to migrate pages of pmbench between node 0 and node 1 back and forth. With the patch, the TLB flushing IPI reduces 99.1% during the test and the number of pages migrated successfully per second increases 291.7%. Haoxin helped to test the patchset on an ARM64 server with 128 cores, 2 NUMA nodes. Test results show that the page migration performance increases up to 78%. NOTE: TLB flushing is batched only for normal folios, not for THP folios. Because the overhead of TLB flushing for THP folios is much lower than that for normal folios (about 1/512 on x86 platform). Signed-off-by: "Huang, Ying" Tested-by: Xin Hao Reviewed-by: Zi Yan Reviewed-by: Xin Hao Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: Minchan Kim Cc: Mike Kravetz Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- mm/migrate.c | 5 ++++- mm/rmap.c | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 00713ccb6643..2fa420e4f68c 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1248,7 +1248,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page /* Establish migration ptes */ VM_BUG_ON_FOLIO(folio_test_anon(src) && !folio_test_ksm(src) && !anon_vma, src); - try_to_migrate(src, 0); + try_to_migrate(src, TTU_BATCH_FLUSH); page_was_mapped = 1; } @@ -1806,6 +1806,9 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, stats->nr_thp_failed += thp_retry; stats->nr_failed_pages += nr_retry_pages; move: + /* Flush TLBs for all unmapped folios */ + try_to_unmap_flush(); + retry = 1; for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); diff --git a/mm/rmap.c b/mm/rmap.c index 8287f2cc327d..15ae24585fc4 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1952,7 +1952,21 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, } else { flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); /* Nuke the page table entry. */ - pteval = ptep_clear_flush(vma, address, pvmw.pte); + if (should_defer_flush(mm, flags)) { + /* + * We clear the PTE but do not flush so potentially + * a remote CPU could still be writing to the folio. + * If the entry was previously clean then the + * architecture must guarantee that a clear->dirty + * transition on a cached TLB entry is written through + * and traps if the PTE is unmapped. + */ + pteval = ptep_get_and_clear(mm, address, pvmw.pte); + + set_tlb_ubc_flush_pending(mm, pte_dirty(pteval)); + } else { + pteval = ptep_clear_flush(vma, address, pvmw.pte); + } } /* Set the dirty flag on the folio now the pte is gone. */ @@ -2124,10 +2138,10 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) /* * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and - * TTU_SPLIT_HUGE_PMD and TTU_SYNC flags. + * TTU_SPLIT_HUGE_PMD, TTU_SYNC, and TTU_BATCH_FLUSH flags. */ if (WARN_ON_ONCE(flags & ~(TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | - TTU_SYNC))) + TTU_SYNC | TTU_BATCH_FLUSH))) return; if (folio_is_zone_device(folio) &&