From patchwork Mon Jan 16 06:30:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102658 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 063A8C46467 for ; Mon, 16 Jan 2023 06:31:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94EE06B007E; Mon, 16 Jan 2023 01:31:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FFA86B0080; Mon, 16 Jan 2023 01:31:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C6C76B0081; Mon, 16 Jan 2023 01:31:40 -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 6ECA56B007E for ; Mon, 16 Jan 2023 01:31:40 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 43A7B403DA for ; Mon, 16 Jan 2023 06:31:40 +0000 (UTC) X-FDA: 80359691160.04.50367EB Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 1310640016 for ; Mon, 16 Jan 2023 06:31:37 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZVch+r3A; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf04.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673850698; 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=VHED7XLNXdSvIBQE8UtlHoXa2f2MNwI5MeJQhCn0rBk=; b=eYwfbQ93jSZ8ugtc3C3cXCFwJGIE5I55K+XQqKXPKPLK5ayNbPiWeGFUo8Ts4PAbjl7AvI RwbG7ayzVDBHLZoKsZvRErAl844Kt4zek3RGes6aqffJmRCMqcO6fxug8PdqCchmmkCoi8 jw0Om1ERrlXB3qF0Wtk4DK/LhdfSLRg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZVch+r3A; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf04.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673850698; a=rsa-sha256; cv=none; b=vvFC3LOadVSUss/miZ6r0UG8Q4frIDIpa3aIHqt+vO4O/90GV/CVbZ4jFGnXrQUBEE+REy a90mIwtt7S8bjQZmS+rhYJgBAnFvsrGX+fwUZI4c7VUU+U6CJHo4tWj4zpwtknKEMEZGcg 7DnhlKwyoz8iZih8T+tXwJ9yh3kE3tw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850698; x=1705386698; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U4IhIonWVHM0uB5EvKcM6XxvZcENTz+r12d4PtYOG2c=; b=ZVch+r3As2FWyuGdHV5IhCZmtmHXKNnhKhA2eu4oBy6MH91+HAGPZXIi RJB3sLE+41mikK8vsbosGytjeaGmFc7PS+hf7Qy/byEyw5lGWmEclBkbq qeuka8uf8UnYMbguBkcv+v3+5FKAxGVn1EKL7Ce8JmTD4We4nFHwyxp+R LoZjYmmyS/Lbdo+i81jpcyiK60tByH2oZiWSPpqU0Wfn/g/Q928YB0a/B LiMe3NIJMbcpCSsCBInvoLi7ns+lDsgzIRCOaY1hxybVGyAOQRPyjXmcO IOolWhJhsUpbsTt6lmuctNlvDDpC3tKYQqaHVQgoVerY6BWnAa3HnDfD/ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892233" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892233" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2023 22:31:37 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286701" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286701" Received: from tiangeng-mobl.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.220]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2023 22:31:34 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , haoxin , Minchan Kim Subject: [PATCH -v3 8/9] migrate_pages: batch flushing TLB Date: Mon, 16 Jan 2023 14:30:56 +0800 Message-Id: <20230116063057.653862-9-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230116063057.653862-1-ying.huang@intel.com> References: <20230116063057.653862-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1310640016 X-Stat-Signature: hoyogk5w9fy5dhmjcftbfqcthbu65bb8 X-HE-Tag: 1673850697-336176 X-HE-Meta: U2FsdGVkX19ZQGwAq9RgEns67ridgc8h+G7qS53YdKycwHPdpTT/Aa6lyfiaadDao1d8XCcMG8sFSHFcUu3MPSDZWiXQm5jYC2+4XPCtCku53DzwMm7u9kOSsnapFy1nRmos3rQdrH26yjOjHcSx5wcMAdyDUuJCK0JX3OG5Ica5jTcSnnJvFMuhJXzqIunW6yAsjSRltbDrjYXaX/MmTvjJM7VioQBn9zH8LY5tKqLqnn0SCSsq7tYczb4J1HcrM/vI/lKgyw2aInk1eAH4rakPLvIAlpRLpKGeu46ox9BOvzgS/chglZzLaMQpZQUCY4LBntC1TYtdkQmqj9DUteSkfqg2seY1XABbFnw3M+yrgCZgL/TlH2Z67h8LKeEfn2Zy8+zhjF2FzBqHJbKF3wnKHIFTudwbqpLA9tfXGtgupnnonfBwKh4b/0QRnP7IM7n21DYgWx6+2/u+GUeEmJ+Tx2ssyaGGykbXdJj9QQFIZObCieGGkl1l4U9LSKcD6hFowZ06lBPxT0hoEu2RFO/bHKZ2QTBprJjO8h93zfO3m42sezBUnNELuYw39PWBbQTAuljo/lNgOF32gmRy5+bbw5ySfKE3K3ehhffFtnFsBSprfyHCUjfWtx0j8S1pwKZNQN1X+xfZ5q93TQzSRjCNdaZQaEJ4WG8NurgDV9NQHENXEp3fpRTG9MyG2QVcSN2QysGn8sGQD6g+VVgdy3lPWXmtxjX98b/ZjJKzXhw5BVRTrsOuKqIBRNgR9DrDsA25K7tCw+fq1FhVQcdAP7RrxEJ/vqiWMx42kD2PhTqNg/pGOxWB7XoThJSf4dbiUQg7G5L6/I6t6kMh9th/DQi1/FHo4AkIJ8G0Zf1SRZGl/h+9/w3FT3sJtyaXzO1D6rmPTdXaCzgmV55xmdUwpxLHLZX5JMMNRr5fAx+cqWNDc/MnjHBaUzsHvjHFnlydwyjtOI3lMwY= 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%. 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" Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 4 +++- mm/rmap.c | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index ef9f126e21ed..8ccb61c49188 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1230,7 +1230,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; } @@ -1780,6 +1780,8 @@ 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: + 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 b616870a09be..2e125f3e462e 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1976,7 +1976,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. */ @@ -2148,10 +2162,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) &&