Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2524152rwl; Mon, 26 Dec 2022 17:00:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXveW9kXitps0Svwq522IrCTPRGaBLbMHGIh6/YGkL4k9ivXfbGWcygHbb7UuhFB2J73jIiE X-Received: by 2002:a17:902:8d8f:b0:191:3b7b:3c2c with SMTP id v15-20020a1709028d8f00b001913b7b3c2cmr22155568plo.22.1672102832020; Mon, 26 Dec 2022 17:00:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672102832; cv=none; d=google.com; s=arc-20160816; b=I9mxJfXFepRowHHEP3xyFSm/efjTvPko0XnaUW9mkImJ32pn6ti1uyubxrau8n3iU6 DOgiAoMRRYMBnoErEwvCO1xL65OmwzerN7vdnMxyyNNVJsvPqVxv1s2xJMA+D6QLFYWw 4evnLquBhO2ysAXdKBwTyohNxWFOB7/JNb0eeMd1QH2IzwsSQ97JTMUSQqEehkGKcGKi TDDJIaYTxcGtv05wCgxtfleX5t/pCtfK82iBDVIlZesmIAwRsboQD4+AtWW1IW+hQzPV Mj9B/RibxDQLqd6lF5z04clKmlrXjBF/WAH3evv4DDv+LuG1OoCuddwZ6RJsd7R72Ydu Y9vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SmzrhIzjhoWM5s5PJ10T2PWwXsxkQGmQGJgyNR100N8=; b=qnw8X4iBMVbxqcPcyMZuk/8VbgprpM4MSbW0kwzQ96sZ3rfHOUmhLF1wWu9Ts1UcEr 3qKhCf+qPjpBshZO9xSmmLJeiXW+9o36PIaBPtAzi3rDDSLqeWSLJVwnxeePo9rhJHtd PQsFIMOoX8ykBBqWEererPru+4S53EQkuMP216jZDNFd2EJEWu2L/KIYwUIXCc02MwQH nI43Pr8dL8PZqNSt3vAlbJ+EPJky0VKkY4dMQoCDvXSck387GlOrm/rBLaAochAV/Zj6 CyHOidZudiwWEIvOwS4juTsyUasqNzwy/5SO71S0XtWao8mwm7wmnV0K2KXthiIGDHFe 0Egg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ih8KNluk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u18-20020a170903125200b00189239e6a29si13531892plh.95.2022.12.26.17.00.21; Mon, 26 Dec 2022 17:00:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ih8KNluk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232625AbiL0Aa4 (ORCPT + 66 others); Mon, 26 Dec 2022 19:30:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232655AbiL0AaC (ORCPT ); Mon, 26 Dec 2022 19:30:02 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8C0A270A for ; Mon, 26 Dec 2022 16:29:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1672100995; x=1703636995; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ba35wqiPwZ1LzOMWrGbZ6/UPey9BiC9eCP+Pk93x5S8=; b=ih8KNlukDsKHZMdppIzuaC83WyoKvunXiIbcFMcuTPnzgGnoJyzbbe7Y OD7+cQ44lb7/5di70irgVQ8cb1emHtV23AkhHVoU817Hfn5fVmRMSGaye p3va+83ltL1ol2yJjiaCKkbuzvq0RgOFrkFNb0p91swLvoKc+NDEXDPby W8kFmhpeE9WRWcblPbdOf/y8I11LYfHP0dYDRovSvhaBgpZhlzORQRnBV r//Uv/8YT43q4I4nC4Un+HE95T2BTmMo4CFze3J7ibQkGWOYnHIZ324d4 Uy/+JSJQhfGQuJDJUC8URAsnlqdnSDtq6TPq/sTnPaqhpX7ZjuRiWqcL+ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10572"; a="322597292" X-IronPort-AV: E=Sophos;i="5.96,277,1665471600"; d="scan'208";a="322597292" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Dec 2022 16:29:55 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10572"; a="760172251" X-IronPort-AV: E=Sophos;i="5.96,277,1665471600"; d="scan'208";a="760172251" Received: from yyang3-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.254.212.104]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Dec 2022 16:29:51 -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 Subject: [PATCH 8/8] migrate_pages: batch flushing TLB Date: Tue, 27 Dec 2022 08:28:59 +0800 Message-Id: <20221227002859.27740-9-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221227002859.27740-1-ying.huang@intel.com> References: <20221227002859.27740-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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%. 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 --- 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 70a40b8fee1f..d7413164e748 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1215,7 +1215,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; } @@ -1732,6 +1732,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 < 10 && (retry || large_retry); pass++) { retry = 0; 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) && -- 2.35.1