From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CFA9C636CC for ; Mon, 13 Feb 2023 12:35:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231247AbjBMMfZ (ORCPT ); Mon, 13 Feb 2023 07:35:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231221AbjBMMfU (ORCPT ); Mon, 13 Feb 2023 07:35:20 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87D0413DE7 for ; Mon, 13 Feb 2023 04:35:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291719; x=1707827719; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=l+3xR+mdu+KvJS/5Kh2raKgnX+cbZNTvoyGmwX5GiZQ=; b=CaMvGrMqgnuNtHfoh4Fc/aM0MTBBJkTYBvukPSk5olh/W0S9pvwxDzuo ud8zmctUcC0OQbUViryuDgtLr/4J4rp1+jO8j366TrjfC1g8pcfneeBVP CvxpM97vnIK24e9sgSRySmGgLtN7YQM1DrOd/XNAoUl6SxRwmAT9ZCSsk jvdMBYFX6Kc2mtPmT5ouwXSpwSrtz+T3BW7i3Id6TjNzu9AM1UalvkJ9p R7u0eeyH+dPws6VVCArAlMhkanz64vmWtQFjQ/pyteo5rT5fnUHrczkqN n+mqdss4XcI5QwgHdKjFL4dv0IRS1jjFps/W3cv9mJ5Htlopyty0z0Nuw A==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513150" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513150" 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:19 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366593" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366593" 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:15 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Alistair Popple , Zi Yan , Baolin Wang , Xin Hao , Yang Shi , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 1/9] migrate_pages: organize stats with struct migrate_pages_stats Date: Mon, 13 Feb 2023 20:34:36 +0800 Message-Id: <20230213123444.155149-2-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Define struct migrate_pages_stats to organize the various statistics in migrate_pages(). This makes it easier to collect and consume the statistics in multiple functions. This will be needed in the following patches in the series. Signed-off-by: "Huang, Ying" Reviewed-by: Alistair Popple Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Reviewed-by: Xin Hao Cc: Yang Shi Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Minchan Kim Cc: Mike Kravetz Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 60 +++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 5b40b9040ba6..1a9cfcf857d2 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1414,6 +1414,16 @@ static inline int try_split_folio(struct folio *foli= o, struct list_head *split_f return rc; } =20 +struct migrate_pages_stats { + int nr_succeeded; /* Normal and large folios migrated successfully, in + units of base pages */ + int nr_failed_pages; /* Normal and large folios failed to be migrated, in + units of base pages. Untried folios aren't counted */ + int nr_thp_succeeded; /* THP migrated successfully */ + int nr_thp_failed; /* THP failed to be migrated */ + int nr_thp_split; /* THP split before migrating */ +}; + /* * migrate_pages - migrate the folios specified in a list, to the free fol= ios * supplied as the target for the page migration @@ -1448,13 +1458,8 @@ int migrate_pages(struct list_head *from, new_page_t= get_new_page, int large_retry =3D 1; int thp_retry =3D 1; int nr_failed =3D 0; - int nr_failed_pages =3D 0; int nr_retry_pages =3D 0; - int nr_succeeded =3D 0; - int nr_thp_succeeded =3D 0; int nr_large_failed =3D 0; - int nr_thp_failed =3D 0; - int nr_thp_split =3D 0; int pass =3D 0; bool is_large =3D false; bool is_thp =3D false; @@ -1464,9 +1469,11 @@ int migrate_pages(struct list_head *from, new_page_t= get_new_page, LIST_HEAD(split_folios); bool nosplit =3D (reason =3D=3D MR_NUMA_MISPLACED); bool no_split_folio_counting =3D false; + struct migrate_pages_stats stats; =20 trace_mm_migrate_pages_start(mode, reason); =20 + memset(&stats, 0, sizeof(stats)); split_folio_migration: for (pass =3D 0; pass < 10 && (retry || large_retry); pass++) { retry =3D 0; @@ -1520,9 +1527,9 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, /* Large folio migration is unsupported */ if (is_large) { nr_large_failed++; - nr_thp_failed +=3D is_thp; + stats.nr_thp_failed +=3D is_thp; if (!try_split_folio(folio, &split_folios)) { - nr_thp_split +=3D is_thp; + stats.nr_thp_split +=3D is_thp; break; } /* Hugetlb migration is unsupported */ @@ -1530,7 +1537,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, nr_failed++; } =20 - nr_failed_pages +=3D nr_pages; + stats.nr_failed_pages +=3D nr_pages; list_move_tail(&folio->lru, &ret_folios); break; case -ENOMEM: @@ -1540,13 +1547,13 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, */ if (is_large) { nr_large_failed++; - nr_thp_failed +=3D is_thp; + stats.nr_thp_failed +=3D is_thp; /* Large folio NUMA faulting doesn't split to retry. */ if (!nosplit) { int ret =3D try_split_folio(folio, &split_folios); =20 if (!ret) { - nr_thp_split +=3D is_thp; + stats.nr_thp_split +=3D is_thp; break; } else if (reason =3D=3D MR_LONGTERM_PIN && ret =3D=3D -EAGAIN) { @@ -1564,7 +1571,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, nr_failed++; } =20 - nr_failed_pages +=3D nr_pages + nr_retry_pages; + stats.nr_failed_pages +=3D nr_pages + nr_retry_pages; /* * There might be some split folios of fail-to-migrate large * folios left in split_folios list. Move them back to migration @@ -1574,7 +1581,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, list_splice_init(&split_folios, from); /* nr_failed isn't updated for not used */ nr_large_failed +=3D large_retry; - nr_thp_failed +=3D thp_retry; + stats.nr_thp_failed +=3D thp_retry; goto out; case -EAGAIN: if (is_large) { @@ -1586,8 +1593,8 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, nr_retry_pages +=3D nr_pages; break; case MIGRATEPAGE_SUCCESS: - nr_succeeded +=3D nr_pages; - nr_thp_succeeded +=3D is_thp; + stats.nr_succeeded +=3D nr_pages; + stats.nr_thp_succeeded +=3D is_thp; break; default: /* @@ -1598,20 +1605,20 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, */ if (is_large) { nr_large_failed++; - nr_thp_failed +=3D is_thp; + stats.nr_thp_failed +=3D is_thp; } else if (!no_split_folio_counting) { nr_failed++; } =20 - nr_failed_pages +=3D nr_pages; + stats.nr_failed_pages +=3D nr_pages; break; } } } nr_failed +=3D retry; nr_large_failed +=3D large_retry; - nr_thp_failed +=3D thp_retry; - nr_failed_pages +=3D nr_retry_pages; + stats.nr_thp_failed +=3D thp_retry; + stats.nr_failed_pages +=3D nr_retry_pages; /* * Try to migrate split folios of fail-to-migrate large folios, no * nr_failed counting in this round, since all split folios of a @@ -1644,16 +1651,17 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, if (list_empty(from)) rc =3D 0; =20 - count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded); - count_vm_events(PGMIGRATE_FAIL, nr_failed_pages); - count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded); - count_vm_events(THP_MIGRATION_FAIL, nr_thp_failed); - count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split); - trace_mm_migrate_pages(nr_succeeded, nr_failed_pages, nr_thp_succeeded, - nr_thp_failed, nr_thp_split, mode, reason); + count_vm_events(PGMIGRATE_SUCCESS, stats.nr_succeeded); + count_vm_events(PGMIGRATE_FAIL, stats.nr_failed_pages); + count_vm_events(THP_MIGRATION_SUCCESS, stats.nr_thp_succeeded); + count_vm_events(THP_MIGRATION_FAIL, stats.nr_thp_failed); + count_vm_events(THP_MIGRATION_SPLIT, stats.nr_thp_split); + trace_mm_migrate_pages(stats.nr_succeeded, stats.nr_failed_pages, + stats.nr_thp_succeeded, stats.nr_thp_failed, + stats.nr_thp_split, mode, reason); =20 if (ret_succeeded) - *ret_succeeded =3D nr_succeeded; + *ret_succeeded =3D stats.nr_succeeded; =20 return rc; } --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E6E6C636CC for ; Mon, 13 Feb 2023 12:35:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231256AbjBMMfa (ORCPT ); Mon, 13 Feb 2023 07:35:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231221AbjBMMf0 (ORCPT ); Mon, 13 Feb 2023 07:35:26 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A199615567 for ; Mon, 13 Feb 2023 04:35:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291723; x=1707827723; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6XvIwqCGGbQdtVE2GfY6YLHxNbOVZ88E0KPcEuieCq0=; b=H3y23XGm1tBfuOh8IcSTFpD368igPMP6KvwZDORueAo+2V6/4YzKWI8/ Ts/jPFrNkzhQK7w1lEXcYiuuKkUBLAfThNkU+MSZqHlTWTC73pXT4679U L2pM17cmRcT1JvUzniZFDT5FVpTSXse6qqIMEufl+4aOs0+tMP/uY24a9 WTfWi23yASz2ZODczCgHkQOYShrs6BthqweW1uaS7n3BAFqfjEXJk5ZTs xKhaodCMVzdx9SETMEdmD6wK3atSaLwkQE5X+n74LUOxLkvvPqfprO2EL Wj3TrW60o/1EX3NyaTcs+DuSoIvr6l5Pbkbi4weRvWKie2GWsIF6hdoJC Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513179" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513179" 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:23 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366601" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366601" 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:19 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Baolin Wang , Xin Hao , Zi Yan , Yang Shi , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 2/9] migrate_pages: separate hugetlb folios migration Date: Mon, 13 Feb 2023 20:34:37 +0800 Message-Id: <20230213123444.155149-3-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a preparation patch to batch the folio unmapping and moving for the non-hugetlb folios. Based on that we can batch the TLB shootdown during the folio migration and make it possible to use some hardware accelerator for the folio copying. In this patch the hugetlb folios and non-hugetlb folios migration is separated in migrate_pages() to make it easy to change the non-hugetlb folios migration implementation. Signed-off-by: "Huang, Ying" Reviewed-by: Baolin Wang Reviewed-by: Xin Hao Cc: Zi Yan Cc: Yang Shi 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> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 1a9cfcf857d2..586a32bdaa71 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1414,6 +1414,8 @@ static inline int try_split_folio(struct folio *folio= , struct list_head *split_f return rc; } =20 +#define NR_MAX_MIGRATE_PAGES_RETRY 10 + struct migrate_pages_stats { int nr_succeeded; /* Normal and large folios migrated successfully, in units of base pages */ @@ -1424,6 +1426,95 @@ struct migrate_pages_stats { int nr_thp_split; /* THP split before migrating */ }; =20 +/* + * Returns the number of hugetlb folios that were not migrated, or an erro= r code + * after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are m= ovable + * any more because the list has become empty or no retryable hugetlb foli= os + * exist any more. It is caller's responsibility to call putback_movable_p= ages() + * only if ret !=3D 0. + */ +static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_pag= e, + free_page_t put_new_page, unsigned long private, + enum migrate_mode mode, int reason, + struct migrate_pages_stats *stats, + struct list_head *ret_folios) +{ + int retry =3D 1; + int nr_failed =3D 0; + int nr_retry_pages =3D 0; + int pass =3D 0; + struct folio *folio, *folio2; + int rc, nr_pages; + + for (pass =3D 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { + retry =3D 0; + nr_retry_pages =3D 0; + + list_for_each_entry_safe(folio, folio2, from, lru) { + if (!folio_test_hugetlb(folio)) + continue; + + nr_pages =3D folio_nr_pages(folio); + + cond_resched(); + + rc =3D unmap_and_move_huge_page(get_new_page, + put_new_page, private, + &folio->page, pass > 2, mode, + reason, ret_folios); + /* + * The rules are: + * Success: hugetlb folio will be put back + * -EAGAIN: stay on the from list + * -ENOMEM: stay on the from list + * -ENOSYS: stay on the from list + * Other errno: put on ret_folios list + */ + switch(rc) { + case -ENOSYS: + /* Hugetlb migration is unsupported */ + nr_failed++; + stats->nr_failed_pages +=3D nr_pages; + list_move_tail(&folio->lru, ret_folios); + break; + case -ENOMEM: + /* + * When memory is low, don't bother to try to migrate + * other folios, just exit. + */ + stats->nr_failed_pages +=3D nr_pages + nr_retry_pages; + return -ENOMEM; + case -EAGAIN: + retry++; + nr_retry_pages +=3D nr_pages; + break; + case MIGRATEPAGE_SUCCESS: + stats->nr_succeeded +=3D nr_pages; + break; + default: + /* + * Permanent failure (-EBUSY, etc.): + * unlike -EAGAIN case, the failed folio is + * removed from migration folio list and not + * retried in the next outer loop. + */ + nr_failed++; + stats->nr_failed_pages +=3D nr_pages; + break; + } + } + } + /* + * nr_failed is number of hugetlb folios failed to be migrated. After + * NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb + * folios as failed. + */ + nr_failed +=3D retry; + stats->nr_failed_pages +=3D nr_retry_pages; + + return nr_failed; +} + /* * migrate_pages - migrate the folios specified in a list, to the free fol= ios * supplied as the target for the page migration @@ -1440,10 +1531,10 @@ struct migrate_pages_stats { * @ret_succeeded: Set to the number of folios migrated successfully if * the caller passes a non-NULL pointer. * - * The function returns after 10 attempts or if no folios are movable any = more - * because the list has become empty or no retryable folios exist any more. - * It is caller's responsibility to call putback_movable_pages() to return= folios - * to the LRU or free list only if ret !=3D 0. + * The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no= folios + * are movable any more because the list has become empty or no retryable = folios + * exist any more. It is caller's responsibility to call putback_movable_p= ages() + * only if ret !=3D 0. * * Returns the number of {normal folio, large folio, hugetlb} that were not * migrated, or an error code. The number of large folio splits will be @@ -1457,7 +1548,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, int retry =3D 1; int large_retry =3D 1; int thp_retry =3D 1; - int nr_failed =3D 0; + int nr_failed; int nr_retry_pages =3D 0; int nr_large_failed =3D 0; int pass =3D 0; @@ -1474,38 +1565,45 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, trace_mm_migrate_pages_start(mode, reason); =20 memset(&stats, 0, sizeof(stats)); + rc =3D migrate_hugetlbs(from, get_new_page, put_new_page, private, mode, = reason, + &stats, &ret_folios); + if (rc < 0) + goto out; + nr_failed =3D rc; + split_folio_migration: - for (pass =3D 0; pass < 10 && (retry || large_retry); pass++) { + for (pass =3D 0; + pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); + pass++) { retry =3D 0; large_retry =3D 0; thp_retry =3D 0; nr_retry_pages =3D 0; =20 list_for_each_entry_safe(folio, folio2, from, lru) { + /* Retried hugetlb folios will be kept in list */ + if (folio_test_hugetlb(folio)) { + list_move_tail(&folio->lru, &ret_folios); + continue; + } + /* * Large folio statistics is based on the source large * folio. Capture required information that might get * lost during migration. */ - is_large =3D folio_test_large(folio) && !folio_test_hugetlb(folio); + is_large =3D folio_test_large(folio); is_thp =3D is_large && folio_test_pmd_mappable(folio); nr_pages =3D folio_nr_pages(folio); + cond_resched(); =20 - if (folio_test_hugetlb(folio)) - rc =3D unmap_and_move_huge_page(get_new_page, - put_new_page, private, - &folio->page, pass > 2, mode, - reason, - &ret_folios); - else - rc =3D unmap_and_move(get_new_page, put_new_page, - private, folio, pass > 2, mode, - reason, &ret_folios); + rc =3D unmap_and_move(get_new_page, put_new_page, + private, folio, pass > 2, mode, + reason, &ret_folios); /* * The rules are: - * Success: non hugetlb folio will be freed, hugetlb - * folio will be put back + * Success: folio will be freed * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list @@ -1532,7 +1630,6 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, stats.nr_thp_split +=3D is_thp; break; } - /* Hugetlb migration is unsupported */ } else if (!no_split_folio_counting) { nr_failed++; } @@ -1626,8 +1723,8 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, */ if (!list_empty(&split_folios)) { /* - * Move non-migrated folios (after 10 retries) to ret_folios - * to avoid migrating them again. + * Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY + * retries) to ret_folios to avoid migrating them again. */ list_splice_init(from, &ret_folios); list_splice_init(&split_folios, from); --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CA5BC6379F for ; Mon, 13 Feb 2023 12:35:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230074AbjBMMfi (ORCPT ); Mon, 13 Feb 2023 07:35:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231265AbjBMMfb (ORCPT ); Mon, 13 Feb 2023 07:35:31 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACEA11A645 for ; Mon, 13 Feb 2023 04:35:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291727; x=1707827727; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6l4ikwjy8UmsuY4ji+NzBGTMG5CFHZs/zfizs/IbAY0=; b=J9I4AqkpnVKA1QxCa5liF8t90AamB5rskS7VSlvGmjBjnIniSaCLXMoo 1/TsK8bIgAjFn7cWvooHWXj4R8Ghq6VJrbMFHLaazX0W5emxY7SSjpIDb L+q5EWgZBj8+6ePIKcvnQPEF3o0P1LHGYyk4vpUSCrs321LWd79n2cjRM CoIFrA9bQ6F4l3QQGsf4sHMGjJKYCq49cunRvEaaZqI9sauxgosDLSDsx rlh1DtYYD4YL8zA7savuIZjtl8GZ8/OjJWHVPQlUOevUDaXSExrOgI8eh glx4XwDo1Y1W5S43KRsi5UtWPLTjkL9jgC06SUFL3dcIPyyx7Lpgi1hGO w==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513201" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513201" 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:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366614" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366614" 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:23 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Baolin Wang , Zi Yan , Yang Shi , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , Xin Hao , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 3/9] migrate_pages: restrict number of pages to migrate in batch Date: Mon, 13 Feb 2023 20:34:38 +0800 Message-Id: <20230213123444.155149-4-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a preparation patch to batch the folio unmapping and moving for non-hugetlb folios. If we had batched the folio unmapping, all folios to be migrated would be unmapped before copying the contents and flags of the folios. If the folios that were passed to migrate_pages() were too many in unit of pages, the execution of the processes would be stopped for too long time, thus too long latency. For example, migrate_pages() syscall will call migrate_pages() with all folios of a process. To avoid this possible issue, in this patch, we restrict the number of pages to be migrated to be no more than HPAGE_PMD_NR. That is, the influence is at the same level of THP migration. Signed-off-by: "Huang, Ying" Reviewed-by: Baolin Wang Cc: Zi Yan Cc: Yang Shi Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: Xin Hao Cc: Minchan Kim Cc: Mike Kravetz Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 174 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 106 insertions(+), 68 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 586a32bdaa71..d436f35fa145 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1414,6 +1414,11 @@ static inline int try_split_folio(struct folio *foli= o, struct list_head *split_f return rc; } =20 +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#define NR_MAX_BATCHED_MIGRATION HPAGE_PMD_NR +#else +#define NR_MAX_BATCHED_MIGRATION 512 +#endif #define NR_MAX_MIGRATE_PAGES_RETRY 10 =20 struct migrate_pages_stats { @@ -1515,40 +1520,15 @@ static int migrate_hugetlbs(struct list_head *from,= new_page_t get_new_page, return nr_failed; } =20 -/* - * migrate_pages - migrate the folios specified in a list, to the free fol= ios - * supplied as the target for the page migration - * - * @from: The list of folios to be migrated. - * @get_new_page: The function used to allocate free folios to be used - * as the target of the folio migration. - * @put_new_page: The function used to free target folios if migration - * fails, or NULL if no special handling is necessary. - * @private: Private data to be passed on to get_new_page() - * @mode: The migration mode that specifies the constraints for - * folio migration, if any. - * @reason: The reason for folio migration. - * @ret_succeeded: Set to the number of folios migrated successfully if - * the caller passes a non-NULL pointer. - * - * The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no= folios - * are movable any more because the list has become empty or no retryable = folios - * exist any more. It is caller's responsibility to call putback_movable_p= ages() - * only if ret !=3D 0. - * - * Returns the number of {normal folio, large folio, hugetlb} that were not - * migrated, or an error code. The number of large folio splits will be - * considered as the number of non-migrated large folio, no matter how many - * split folios of the large folio are migrated successfully. - */ -int migrate_pages(struct list_head *from, new_page_t get_new_page, +static int migrate_pages_batch(struct list_head *from, new_page_t get_new_= page, free_page_t put_new_page, unsigned long private, - enum migrate_mode mode, int reason, unsigned int *ret_succeeded) + enum migrate_mode mode, int reason, struct list_head *ret_folios, + struct migrate_pages_stats *stats) { int retry =3D 1; int large_retry =3D 1; int thp_retry =3D 1; - int nr_failed; + int nr_failed =3D 0; int nr_retry_pages =3D 0; int nr_large_failed =3D 0; int pass =3D 0; @@ -1556,20 +1536,9 @@ int migrate_pages(struct list_head *from, new_page_t= get_new_page, bool is_thp =3D false; struct folio *folio, *folio2; int rc, nr_pages; - LIST_HEAD(ret_folios); LIST_HEAD(split_folios); bool nosplit =3D (reason =3D=3D MR_NUMA_MISPLACED); bool no_split_folio_counting =3D false; - struct migrate_pages_stats stats; - - trace_mm_migrate_pages_start(mode, reason); - - memset(&stats, 0, sizeof(stats)); - rc =3D migrate_hugetlbs(from, get_new_page, put_new_page, private, mode, = reason, - &stats, &ret_folios); - if (rc < 0) - goto out; - nr_failed =3D rc; =20 split_folio_migration: for (pass =3D 0; @@ -1581,12 +1550,6 @@ int migrate_pages(struct list_head *from, new_page_t= get_new_page, nr_retry_pages =3D 0; =20 list_for_each_entry_safe(folio, folio2, from, lru) { - /* Retried hugetlb folios will be kept in list */ - if (folio_test_hugetlb(folio)) { - list_move_tail(&folio->lru, &ret_folios); - continue; - } - /* * Large folio statistics is based on the source large * folio. Capture required information that might get @@ -1600,15 +1563,14 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, =20 rc =3D unmap_and_move(get_new_page, put_new_page, private, folio, pass > 2, mode, - reason, &ret_folios); + reason, ret_folios); /* * The rules are: * Success: folio will be freed * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list - * Other errno: put on ret_folios list then splice to - * from list + * Other errno: put on ret_folios list */ switch(rc) { /* @@ -1625,17 +1587,17 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, /* Large folio migration is unsupported */ if (is_large) { nr_large_failed++; - stats.nr_thp_failed +=3D is_thp; + stats->nr_thp_failed +=3D is_thp; if (!try_split_folio(folio, &split_folios)) { - stats.nr_thp_split +=3D is_thp; + stats->nr_thp_split +=3D is_thp; break; } } else if (!no_split_folio_counting) { nr_failed++; } =20 - stats.nr_failed_pages +=3D nr_pages; - list_move_tail(&folio->lru, &ret_folios); + stats->nr_failed_pages +=3D nr_pages; + list_move_tail(&folio->lru, ret_folios); break; case -ENOMEM: /* @@ -1644,13 +1606,13 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, */ if (is_large) { nr_large_failed++; - stats.nr_thp_failed +=3D is_thp; + stats->nr_thp_failed +=3D is_thp; /* Large folio NUMA faulting doesn't split to retry. */ if (!nosplit) { int ret =3D try_split_folio(folio, &split_folios); =20 if (!ret) { - stats.nr_thp_split +=3D is_thp; + stats->nr_thp_split +=3D is_thp; break; } else if (reason =3D=3D MR_LONGTERM_PIN && ret =3D=3D -EAGAIN) { @@ -1668,17 +1630,17 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, nr_failed++; } =20 - stats.nr_failed_pages +=3D nr_pages + nr_retry_pages; + stats->nr_failed_pages +=3D nr_pages + nr_retry_pages; /* * There might be some split folios of fail-to-migrate large - * folios left in split_folios list. Move them back to migration + * folios left in split_folios list. Move them to ret_folios * list so that they could be put back to the right list by * the caller otherwise the folio refcnt will be leaked. */ - list_splice_init(&split_folios, from); + list_splice_init(&split_folios, ret_folios); /* nr_failed isn't updated for not used */ nr_large_failed +=3D large_retry; - stats.nr_thp_failed +=3D thp_retry; + stats->nr_thp_failed +=3D thp_retry; goto out; case -EAGAIN: if (is_large) { @@ -1690,8 +1652,8 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, nr_retry_pages +=3D nr_pages; break; case MIGRATEPAGE_SUCCESS: - stats.nr_succeeded +=3D nr_pages; - stats.nr_thp_succeeded +=3D is_thp; + stats->nr_succeeded +=3D nr_pages; + stats->nr_thp_succeeded +=3D is_thp; break; default: /* @@ -1702,20 +1664,20 @@ int migrate_pages(struct list_head *from, new_page_= t get_new_page, */ if (is_large) { nr_large_failed++; - stats.nr_thp_failed +=3D is_thp; + stats->nr_thp_failed +=3D is_thp; } else if (!no_split_folio_counting) { nr_failed++; } =20 - stats.nr_failed_pages +=3D nr_pages; + stats->nr_failed_pages +=3D nr_pages; break; } } } nr_failed +=3D retry; nr_large_failed +=3D large_retry; - stats.nr_thp_failed +=3D thp_retry; - stats.nr_failed_pages +=3D nr_retry_pages; + stats->nr_thp_failed +=3D thp_retry; + stats->nr_failed_pages +=3D nr_retry_pages; /* * Try to migrate split folios of fail-to-migrate large folios, no * nr_failed counting in this round, since all split folios of a @@ -1726,7 +1688,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, * Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY * retries) to ret_folios to avoid migrating them again. */ - list_splice_init(from, &ret_folios); + list_splice_init(from, ret_folios); list_splice_init(&split_folios, from); no_split_folio_counting =3D true; retry =3D 1; @@ -1734,6 +1696,82 @@ int migrate_pages(struct list_head *from, new_page_t= get_new_page, } =20 rc =3D nr_failed + nr_large_failed; +out: + return rc; +} + +/* + * migrate_pages - migrate the folios specified in a list, to the free fol= ios + * supplied as the target for the page migration + * + * @from: The list of folios to be migrated. + * @get_new_page: The function used to allocate free folios to be used + * as the target of the folio migration. + * @put_new_page: The function used to free target folios if migration + * fails, or NULL if no special handling is necessary. + * @private: Private data to be passed on to get_new_page() + * @mode: The migration mode that specifies the constraints for + * folio migration, if any. + * @reason: The reason for folio migration. + * @ret_succeeded: Set to the number of folios migrated successfully if + * the caller passes a non-NULL pointer. + * + * The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no= folios + * are movable any more because the list has become empty or no retryable = folios + * exist any more. It is caller's responsibility to call putback_movable_p= ages() + * only if ret !=3D 0. + * + * Returns the number of {normal folio, large folio, hugetlb} that were not + * migrated, or an error code. The number of large folio splits will be + * considered as the number of non-migrated large folio, no matter how many + * split folios of the large folio are migrated successfully. + */ +int migrate_pages(struct list_head *from, new_page_t get_new_page, + free_page_t put_new_page, unsigned long private, + enum migrate_mode mode, int reason, unsigned int *ret_succeeded) +{ + int rc, rc_gather; + int nr_pages; + struct folio *folio, *folio2; + LIST_HEAD(folios); + LIST_HEAD(ret_folios); + struct migrate_pages_stats stats; + + trace_mm_migrate_pages_start(mode, reason); + + memset(&stats, 0, sizeof(stats)); + + rc_gather =3D migrate_hugetlbs(from, get_new_page, put_new_page, private, + mode, reason, &stats, &ret_folios); + if (rc_gather < 0) + goto out; +again: + nr_pages =3D 0; + list_for_each_entry_safe(folio, folio2, from, lru) { + /* Retried hugetlb folios will be kept in list */ + if (folio_test_hugetlb(folio)) { + list_move_tail(&folio->lru, &ret_folios); + continue; + } + + nr_pages +=3D folio_nr_pages(folio); + if (nr_pages > NR_MAX_BATCHED_MIGRATION) + break; + } + if (nr_pages > NR_MAX_BATCHED_MIGRATION) + list_cut_before(&folios, from, &folio->lru); + else + list_splice_init(from, &folios); + rc =3D migrate_pages_batch(&folios, get_new_page, put_new_page, private, + mode, reason, &ret_folios, &stats); + list_splice_tail_init(&folios, &ret_folios); + if (rc < 0) { + rc_gather =3D rc; + goto out; + } + rc_gather +=3D rc; + if (!list_empty(from)) + goto again; out: /* * Put the permanent failure folio back to migration list, they @@ -1746,7 +1784,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, * are migrated successfully. */ if (list_empty(from)) - rc =3D 0; + rc_gather =3D 0; =20 count_vm_events(PGMIGRATE_SUCCESS, stats.nr_succeeded); count_vm_events(PGMIGRATE_FAIL, stats.nr_failed_pages); @@ -1760,7 +1798,7 @@ int migrate_pages(struct list_head *from, new_page_t = get_new_page, if (ret_succeeded) *ret_succeeded =3D stats.nr_succeeded; =20 - return rc; + return rc_gather; } =20 struct page *alloc_migration_target(struct page *page, unsigned long priva= te) --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C9A0C636D4 for ; Mon, 13 Feb 2023 12:35:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230100AbjBMMfk (ORCPT ); Mon, 13 Feb 2023 07:35:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231304AbjBMMfg (ORCPT ); Mon, 13 Feb 2023 07:35:36 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F2861A956 for ; Mon, 13 Feb 2023 04:35:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291731; x=1707827731; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3XEbG6JD2LdI1CjVNNq0obsv14/SFsm/rzfKm+kf4eA=; b=Pm74s98jQmkqJKLjbHgQZV/Tj3/8F4Z05naZJNZGCZuhngUPPjAheQn2 BJNlg5jtpm8/wADUt1f1kCido3hXdz0yovnqrB2liI4ILCFoQLqn7Ujzo UFi4pZoC83OCcxf0CAKyX6n0uxw6YHJCVhSWCDwEBtizShzMm6brgKgnM TqQRYdX+rA1F4HfyAxRP9sX4u3CcFec48ZcOnc90X+qMCCpwVdXlYRFgd JDLYKBuxIGi6R8NOptMM1xfZXLayWdvBbtxhEdPJFMK5Q3afc3D25yu/U yQGk6ZctyidsqOrvs7cG5JeHyTSWkGt/vGfyxrw3Cdke4+rc6VLKN31rG g==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513221" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513221" 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:31 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366631" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366631" 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:27 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Baolin Wang , Xin Hao , Zi Yan , Yang Shi , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 4/9] migrate_pages: split unmap_and_move() to _unmap() and _move() Date: Mon, 13 Feb 2023 20:34:39 +0800 Message-Id: <20230213123444.155149-5-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a preparation patch to batch the folio unmapping and moving. In this patch, unmap_and_move() is split to migrate_folio_unmap() and migrate_folio_move(). So, we can batch _unmap() and _move() in different loops later. To pass some information between unmap and move, the original unused dst->mapping and dst->private are used. Signed-off-by: "Huang, Ying" Reviewed-by: Baolin Wang Reviewed-by: Xin Hao Cc: Zi Yan Cc: Yang Shi 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> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- include/linux/migrate.h | 1 + mm/migrate.c | 169 ++++++++++++++++++++++++++++++---------- 2 files changed, 129 insertions(+), 41 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index bdff950a8bb4..c88b96b48be7 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -18,6 +18,7 @@ struct migration_target_control; * - zero on page migration success; */ #define MIGRATEPAGE_SUCCESS 0 +#define MIGRATEPAGE_UNMAP 1 =20 /** * struct movable_operations - Driver page migration diff --git a/mm/migrate.c b/mm/migrate.c index d436f35fa145..5fd18a7cce62 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1027,11 +1027,53 @@ static int move_to_new_folio(struct folio *dst, str= uct folio *src, return rc; } =20 -static int __unmap_and_move(struct folio *src, struct folio *dst, +/* + * To record some information during migration, we use some unused + * fields (mapping and private) of struct folio of the newly allocated + * destination folio. This is safe because nobody is using them + * except us. + */ +static void __migrate_folio_record(struct folio *dst, + unsigned long page_was_mapped, + struct anon_vma *anon_vma) +{ + dst->mapping =3D (void *)anon_vma; + dst->private =3D (void *)page_was_mapped; +} + +static void __migrate_folio_extract(struct folio *dst, + int *page_was_mappedp, + struct anon_vma **anon_vmap) +{ + *anon_vmap =3D (void *)dst->mapping; + *page_was_mappedp =3D (unsigned long)dst->private; + dst->mapping =3D NULL; + dst->private =3D NULL; +} + +/* Cleanup src folio upon migration success */ +static void migrate_folio_done(struct folio *src, + enum migrate_reason reason) +{ + /* + * Compaction can migrate also non-LRU pages which are + * not accounted to NR_ISOLATED_*. They can be recognized + * as __PageMovable + */ + if (likely(!__folio_test_movable(src))) + mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON + + folio_is_file_lru(src), -folio_nr_pages(src)); + + if (reason !=3D MR_MEMORY_FAILURE) + /* We release the page in page_handle_poison. */ + folio_put(src); +} + +static int __migrate_folio_unmap(struct folio *src, struct folio *dst, int force, enum migrate_mode mode) { int rc =3D -EAGAIN; - bool page_was_mapped =3D false; + int page_was_mapped =3D 0; struct anon_vma *anon_vma =3D NULL; bool is_lru =3D !__PageMovable(&src->page); =20 @@ -1107,8 +1149,8 @@ static int __unmap_and_move(struct folio *src, struct= folio *dst, goto out_unlock; =20 if (unlikely(!is_lru)) { - rc =3D move_to_new_folio(dst, src, mode); - goto out_unlock_both; + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return MIGRATEPAGE_UNMAP; } =20 /* @@ -1133,11 +1175,42 @@ static int __unmap_and_move(struct folio *src, stru= ct folio *dst, VM_BUG_ON_FOLIO(folio_test_anon(src) && !folio_test_ksm(src) && !anon_vma, src); try_to_migrate(src, 0); - page_was_mapped =3D true; + page_was_mapped =3D 1; } =20 - if (!folio_mapped(src)) - rc =3D move_to_new_folio(dst, src, mode); + if (!folio_mapped(src)) { + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return MIGRATEPAGE_UNMAP; + } + + if (page_was_mapped) + remove_migration_ptes(src, src, false); + +out_unlock_both: + folio_unlock(dst); +out_unlock: + /* Drop an anon_vma reference if we took one */ + if (anon_vma) + put_anon_vma(anon_vma); + folio_unlock(src); +out: + + return rc; +} + +static int __migrate_folio_move(struct folio *src, struct folio *dst, + enum migrate_mode mode) +{ + int rc; + int page_was_mapped =3D 0; + struct anon_vma *anon_vma =3D NULL; + bool is_lru =3D !__PageMovable(&src->page); + + __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); + + rc =3D move_to_new_folio(dst, src, mode); + if (unlikely(!is_lru)) + goto out_unlock_both; =20 /* * When successful, push dst to LRU immediately: so that if it @@ -1160,12 +1233,10 @@ static int __unmap_and_move(struct folio *src, stru= ct folio *dst, =20 out_unlock_both: folio_unlock(dst); -out_unlock: /* Drop an anon_vma reference if we took one */ if (anon_vma) put_anon_vma(anon_vma); folio_unlock(src); -out: /* * If migration is successful, decrease refcount of dst, * which will not free the page because new page owner increased @@ -1177,19 +1248,15 @@ static int __unmap_and_move(struct folio *src, stru= ct folio *dst, return rc; } =20 -/* - * Obtain the lock on folio, remove all ptes and migrate the folio - * to the newly allocated folio in dst. - */ -static int unmap_and_move(new_page_t get_new_page, - free_page_t put_new_page, - unsigned long private, struct folio *src, - int force, enum migrate_mode mode, - enum migrate_reason reason, - struct list_head *ret) +/* Obtain the lock on page, remove all ptes. */ +static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_ne= w_page, + unsigned long private, struct folio *src, + struct folio **dstp, int force, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) { struct folio *dst; - int rc =3D MIGRATEPAGE_SUCCESS; + int rc =3D MIGRATEPAGE_UNMAP; struct page *newpage =3D NULL; =20 if (!thp_migration_supported() && folio_test_transhuge(src)) @@ -1200,20 +1267,49 @@ static int unmap_and_move(new_page_t get_new_page, folio_clear_active(src); folio_clear_unevictable(src); /* free_pages_prepare() will clear PG_isolated. */ - goto out; + list_del(&src->lru); + migrate_folio_done(src, reason); + return MIGRATEPAGE_SUCCESS; } =20 newpage =3D get_new_page(&src->page, private); if (!newpage) return -ENOMEM; dst =3D page_folio(newpage); + *dstp =3D dst; =20 dst->private =3D NULL; - rc =3D __unmap_and_move(src, dst, force, mode); + rc =3D __migrate_folio_unmap(src, dst, force, mode); + if (rc =3D=3D MIGRATEPAGE_UNMAP) + return rc; + + /* + * A folio that has not been unmapped will be restored to + * right list unless we want to retry. + */ + if (rc !=3D -EAGAIN) + list_move_tail(&src->lru, ret); + + if (put_new_page) + put_new_page(&dst->page, private); + else + folio_put(dst); + + return rc; +} + +/* Migrate the folio to the newly allocated folio in dst. */ +static int migrate_folio_move(free_page_t put_new_page, unsigned long priv= ate, + struct folio *src, struct folio *dst, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) +{ + int rc; + + rc =3D __migrate_folio_move(src, dst, mode); if (rc =3D=3D MIGRATEPAGE_SUCCESS) set_page_owner_migrate_reason(&dst->page, reason); =20 -out: if (rc !=3D -EAGAIN) { /* * A folio that has been migrated has all references @@ -1229,20 +1325,7 @@ static int unmap_and_move(new_page_t get_new_page, * we want to retry. */ if (rc =3D=3D MIGRATEPAGE_SUCCESS) { - /* - * Compaction can migrate also non-LRU folios which are - * not accounted to NR_ISOLATED_*. They can be recognized - * as __folio_test_movable - */ - if (likely(!__folio_test_movable(src))) - mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON + - folio_is_file_lru(src), -folio_nr_pages(src)); - - if (reason !=3D MR_MEMORY_FAILURE) - /* - * We release the folio in page_handle_poison. - */ - folio_put(src); + migrate_folio_done(src, reason); } else { if (rc !=3D -EAGAIN) list_add_tail(&src->lru, ret); @@ -1534,7 +1617,7 @@ static int migrate_pages_batch(struct list_head *from= , new_page_t get_new_page, int pass =3D 0; bool is_large =3D false; bool is_thp =3D false; - struct folio *folio, *folio2; + struct folio *folio, *folio2, *dst =3D NULL; int rc, nr_pages; LIST_HEAD(split_folios); bool nosplit =3D (reason =3D=3D MR_NUMA_MISPLACED); @@ -1561,9 +1644,13 @@ static int migrate_pages_batch(struct list_head *fro= m, new_page_t get_new_page, =20 cond_resched(); =20 - rc =3D unmap_and_move(get_new_page, put_new_page, - private, folio, pass > 2, mode, - reason, ret_folios); + rc =3D migrate_folio_unmap(get_new_page, put_new_page, private, + folio, &dst, pass > 2, mode, + reason, ret_folios); + if (rc =3D=3D MIGRATEPAGE_UNMAP) + rc =3D migrate_folio_move(put_new_page, private, + folio, dst, mode, + reason, ret_folios); /* * The rules are: * Success: folio will be freed --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D580C636D4 for ; Mon, 13 Feb 2023 12:35:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbjBMMf6 (ORCPT ); Mon, 13 Feb 2023 07:35:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231336AbjBMMfr (ORCPT ); Mon, 13 Feb 2023 07:35:47 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B372A272B for ; Mon, 13 Feb 2023 04:35:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291735; x=1707827735; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ylCTF0e7vPed9oWJVrpJ/3xILUJhaWMREBCKtLbxdi8=; b=Z8t1xy5hQ5bmARU0mvzSikCYbjq0vkEqz+8IZphpuRhYTh83m/VPhM0/ ImoMx4/vgl9arVZj6eh1ufILoaE4DD/Id/sXK2gtBoNymypS056K6XEua fhGo2c1xAGpu3/jKWa+Y4Cs5M8fW1hi9nHCNp5QA+uWdRIWz8rIDPf+/d ufdivld9XltS9Qkrko5XTNsKB45tXWOWrxCUgwb2/8wwD7FlDknW7btZ0 TY/cUVImov2FzTXR++VWiw0nOGGUqaoQah1Xq/3yzWoTRtu0bLtXkX7Rh QU4C4C0gm9TZjwJKBygdJwj7f7aZ77SRdXY2Mgx0EFaDKEuP0RxvtLduJ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513245" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513245" 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:35 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366645" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366645" 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:31 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , Xin Hao , Minchan Kim , Mike Kravetz Subject: [PATCH -v5 5/9] migrate_pages: batch _unmap and _move Date: Mon, 13 Feb 2023 20:34:40 +0800 Message-Id: <20230213123444.155149-6-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In this patch the _unmap and _move stage of the folio migration is batched. That for, previously, it is, for each folio _unmap() _move() Now, it is, for each folio _unmap() for each folio _move() Based on this, we can batch the TLB flushing and use some hardware accelerator to copy folios between batched _unmap and batched _move stages. Signed-off-by: "Huang, Ying" Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: Xin Hao Cc: Minchan Kim Cc: Mike Kravetz Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 214 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 189 insertions(+), 25 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 5fd18a7cce62..ee3e21f1061c 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1051,6 +1051,33 @@ static void __migrate_folio_extract(struct folio *ds= t, dst->private =3D NULL; } =20 +/* Restore the source folio to the original state upon failure */ +static void migrate_folio_undo_src(struct folio *src, + int page_was_mapped, + struct anon_vma *anon_vma, + struct list_head *ret) +{ + if (page_was_mapped) + remove_migration_ptes(src, src, false); + /* Drop an anon_vma reference if we took one */ + if (anon_vma) + put_anon_vma(anon_vma); + folio_unlock(src); + list_move_tail(&src->lru, ret); +} + +/* Restore the destination folio to the original state upon failure */ +static void migrate_folio_undo_dst(struct folio *dst, + free_page_t put_new_page, + unsigned long private) +{ + folio_unlock(dst); + if (put_new_page) + put_new_page(&dst->page, private); + else + folio_put(dst); +} + /* Cleanup src folio upon migration success */ static void migrate_folio_done(struct folio *src, enum migrate_reason reason) @@ -1069,8 +1096,8 @@ static void migrate_folio_done(struct folio *src, folio_put(src); } =20 -static int __migrate_folio_unmap(struct folio *src, struct folio *dst, - int force, enum migrate_mode mode) +static int __migrate_folio_unmap(struct folio *src, struct folio *dst, int= force, + bool avoid_force_lock, enum migrate_mode mode) { int rc =3D -EAGAIN; int page_was_mapped =3D 0; @@ -1097,6 +1124,17 @@ static int __migrate_folio_unmap(struct folio *src, = struct folio *dst, if (current->flags & PF_MEMALLOC) goto out; =20 + /* + * We have locked some folios and are going to wait to lock + * this folio. To avoid a potential deadlock, let's bail + * out and not do that. The locked folios will be moved and + * unlocked, then we can wait to lock this folio. + */ + if (avoid_force_lock) { + rc =3D -EDEADLOCK; + goto out; + } + folio_lock(src); } =20 @@ -1205,10 +1243,20 @@ static int __migrate_folio_move(struct folio *src, = struct folio *dst, int page_was_mapped =3D 0; struct anon_vma *anon_vma =3D NULL; bool is_lru =3D !__PageMovable(&src->page); + struct list_head *prev; =20 __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); + prev =3D dst->lru.prev; + list_del(&dst->lru); =20 rc =3D move_to_new_folio(dst, src, mode); + + if (rc =3D=3D -EAGAIN) { + list_add(&dst->lru, prev); + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return rc; + } + if (unlikely(!is_lru)) goto out_unlock_both; =20 @@ -1251,7 +1299,7 @@ static int __migrate_folio_move(struct folio *src, st= ruct folio *dst, /* Obtain the lock on page, remove all ptes. */ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_ne= w_page, unsigned long private, struct folio *src, - struct folio **dstp, int force, + struct folio **dstp, int force, bool avoid_force_lock, enum migrate_mode mode, enum migrate_reason reason, struct list_head *ret) { @@ -1279,7 +1327,7 @@ static int migrate_folio_unmap(new_page_t get_new_pag= e, free_page_t put_new_page *dstp =3D dst; =20 dst->private =3D NULL; - rc =3D __migrate_folio_unmap(src, dst, force, mode); + rc =3D __migrate_folio_unmap(src, dst, force, avoid_force_lock, mode); if (rc =3D=3D MIGRATEPAGE_UNMAP) return rc; =20 @@ -1287,7 +1335,7 @@ static int migrate_folio_unmap(new_page_t get_new_pag= e, free_page_t put_new_page * A folio that has not been unmapped will be restored to * right list unless we want to retry. */ - if (rc !=3D -EAGAIN) + if (rc !=3D -EAGAIN && rc !=3D -EDEADLOCK) list_move_tail(&src->lru, ret); =20 if (put_new_page) @@ -1326,9 +1374,8 @@ static int migrate_folio_move(free_page_t put_new_pag= e, unsigned long private, */ if (rc =3D=3D MIGRATEPAGE_SUCCESS) { migrate_folio_done(src, reason); - } else { - if (rc !=3D -EAGAIN) - list_add_tail(&src->lru, ret); + } else if (rc !=3D -EAGAIN) { + list_add_tail(&src->lru, ret); =20 if (put_new_page) put_new_page(&dst->page, private); @@ -1603,12 +1650,16 @@ static int migrate_hugetlbs(struct list_head *from,= new_page_t get_new_page, return nr_failed; } =20 +/* + * migrate_pages_batch() first unmaps folios in the from list as many as + * possible, then move the unmapped folios. + */ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_= page, free_page_t put_new_page, unsigned long private, enum migrate_mode mode, int reason, struct list_head *ret_folios, struct migrate_pages_stats *stats) { - int retry =3D 1; + int retry; int large_retry =3D 1; int thp_retry =3D 1; int nr_failed =3D 0; @@ -1617,13 +1668,19 @@ static int migrate_pages_batch(struct list_head *fr= om, new_page_t get_new_page, int pass =3D 0; bool is_large =3D false; bool is_thp =3D false; - struct folio *folio, *folio2, *dst =3D NULL; - int rc, nr_pages; + struct folio *folio, *folio2, *dst =3D NULL, *dst2; + int rc, rc_saved, nr_pages; LIST_HEAD(split_folios); + LIST_HEAD(unmap_folios); + LIST_HEAD(dst_folios); bool nosplit =3D (reason =3D=3D MR_NUMA_MISPLACED); bool no_split_folio_counting =3D false; + bool avoid_force_lock; =20 -split_folio_migration: +retry: + rc_saved =3D 0; + avoid_force_lock =3D false; + retry =3D 1; for (pass =3D 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); pass++) { @@ -1645,16 +1702,15 @@ static int migrate_pages_batch(struct list_head *fr= om, new_page_t get_new_page, cond_resched(); =20 rc =3D migrate_folio_unmap(get_new_page, put_new_page, private, - folio, &dst, pass > 2, mode, - reason, ret_folios); - if (rc =3D=3D MIGRATEPAGE_UNMAP) - rc =3D migrate_folio_move(put_new_page, private, - folio, dst, mode, - reason, ret_folios); + folio, &dst, pass > 2, avoid_force_lock, + mode, reason, ret_folios); /* * The rules are: * Success: folio will be freed + * Unmap: folio will be put on unmap_folios list, + * dst folio put on dst_folios list * -EAGAIN: stay on the from list + * -EDEADLOCK: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list * Other errno: put on ret_folios list @@ -1689,7 +1745,7 @@ static int migrate_pages_batch(struct list_head *from= , new_page_t get_new_page, case -ENOMEM: /* * When memory is low, don't bother to try to migrate - * other folios, just exit. + * other folios, move unmapped folios, then exit. */ if (is_large) { nr_large_failed++; @@ -1728,7 +1784,19 @@ static int migrate_pages_batch(struct list_head *fro= m, new_page_t get_new_page, /* nr_failed isn't updated for not used */ nr_large_failed +=3D large_retry; stats->nr_thp_failed +=3D thp_retry; - goto out; + rc_saved =3D rc; + if (list_empty(&unmap_folios)) + goto out; + else + goto move; + case -EDEADLOCK: + /* + * The folio cannot be locked for potential deadlock. + * Go move (and unlock) all locked folios. Then we can + * try again. + */ + rc_saved =3D rc; + goto move; case -EAGAIN: if (is_large) { large_retry++; @@ -1742,6 +1810,15 @@ static int migrate_pages_batch(struct list_head *fro= m, new_page_t get_new_page, stats->nr_succeeded +=3D nr_pages; stats->nr_thp_succeeded +=3D is_thp; break; + case MIGRATEPAGE_UNMAP: + /* + * We have locked some folios, don't force lock + * to avoid deadlock. + */ + avoid_force_lock =3D true; + list_move_tail(&folio->lru, &unmap_folios); + list_add_tail(&dst->lru, &dst_folios); + break; default: /* * Permanent failure (-EBUSY, etc.): @@ -1765,12 +1842,95 @@ static int migrate_pages_batch(struct list_head *fr= om, new_page_t get_new_page, nr_large_failed +=3D large_retry; stats->nr_thp_failed +=3D thp_retry; stats->nr_failed_pages +=3D nr_retry_pages; +move: + retry =3D 1; + for (pass =3D 0; + pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); + pass++) { + retry =3D 0; + large_retry =3D 0; + thp_retry =3D 0; + nr_retry_pages =3D 0; + + dst =3D list_first_entry(&dst_folios, struct folio, lru); + dst2 =3D list_next_entry(dst, lru); + list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) { + is_large =3D folio_test_large(folio); + is_thp =3D is_large && folio_test_pmd_mappable(folio); + nr_pages =3D folio_nr_pages(folio); + + cond_resched(); + + rc =3D migrate_folio_move(put_new_page, private, + folio, dst, mode, + reason, ret_folios); + /* + * The rules are: + * Success: folio will be freed + * -EAGAIN: stay on the unmap_folios list + * Other errno: put on ret_folios list + */ + switch(rc) { + case -EAGAIN: + if (is_large) { + large_retry++; + thp_retry +=3D is_thp; + } else if (!no_split_folio_counting) { + retry++; + } + nr_retry_pages +=3D nr_pages; + break; + case MIGRATEPAGE_SUCCESS: + stats->nr_succeeded +=3D nr_pages; + stats->nr_thp_succeeded +=3D is_thp; + break; + default: + if (is_large) { + nr_large_failed++; + stats->nr_thp_failed +=3D is_thp; + } else if (!no_split_folio_counting) { + nr_failed++; + } + + stats->nr_failed_pages +=3D nr_pages; + break; + } + dst =3D dst2; + dst2 =3D list_next_entry(dst, lru); + } + } + nr_failed +=3D retry; + nr_large_failed +=3D large_retry; + stats->nr_thp_failed +=3D thp_retry; + stats->nr_failed_pages +=3D nr_retry_pages; + + if (rc_saved) + rc =3D rc_saved; + else + rc =3D nr_failed + nr_large_failed; +out: + /* Cleanup remaining folios */ + dst =3D list_first_entry(&dst_folios, struct folio, lru); + dst2 =3D list_next_entry(dst, lru); + list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) { + int page_was_mapped =3D 0; + struct anon_vma *anon_vma =3D NULL; + + __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); + migrate_folio_undo_src(folio, page_was_mapped, anon_vma, + ret_folios); + list_del(&dst->lru); + migrate_folio_undo_dst(dst, put_new_page, private); + dst =3D dst2; + dst2 =3D list_next_entry(dst, lru); + } + /* * Try to migrate split folios of fail-to-migrate large folios, no * nr_failed counting in this round, since all split folios of a * large folio is counted as 1 failure in the first round. */ - if (!list_empty(&split_folios)) { + if (rc >=3D 0 && !list_empty(&split_folios)) { /* * Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY * retries) to ret_folios to avoid migrating them again. @@ -1778,12 +1938,16 @@ static int migrate_pages_batch(struct list_head *fr= om, new_page_t get_new_page, list_splice_init(from, ret_folios); list_splice_init(&split_folios, from); no_split_folio_counting =3D true; - retry =3D 1; - goto split_folio_migration; + goto retry; } =20 - rc =3D nr_failed + nr_large_failed; -out: + /* + * We have unlocked all locked folios, so we can force lock now, let's + * try again. + */ + if (rc =3D=3D -EDEADLOCK) + goto retry; + return rc; } =20 --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F04AC636CC for ; Mon, 13 Feb 2023 12:36:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231221AbjBMMgA (ORCPT ); Mon, 13 Feb 2023 07:36:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231270AbjBMMfx (ORCPT ); Mon, 13 Feb 2023 07:35:53 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A3C315CBE for ; Mon, 13 Feb 2023 04:35:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291739; x=1707827739; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K4XDKropeMr7/ju8eX1ZAYnD0sjioz6DP3VREqBtsko=; b=Z5IL4tn+SAjmtk9Cb0hGD6JeBSGVeCuX3kdGF92hkdMoza6R3npUTXiO vVeb47nCxQsa01C4PK8xprXf/wR0pAsmDXVbnD5XUllFhoFVe8up2b1I3 eoQgDARSTFARF2f2XdMVlBzWyRkTM7S2XVXGHBBLjJs05EcweBaVT05Zt xteyeli2vK/1s9slGqlE+i5tgrgj+MpUx1qpWP2M6Tr+PMkdJbIzAOf+n oHlxMv03DHs0dC64lQR6wZXJ7QCkbDZB475ohgAkAp8RgTUMwGNMTlwbc h47bqVIvdxK8jKJzfNMUhvCUX0Ax2TyQMrDSG5fST2I9UsiqD+HEo6Xx1 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513267" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513267" 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:39 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366651" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366651" 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:35 -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 , Xin Hao , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 6/9] migrate_pages: move migrate_folio_unmap() Date: Mon, 13 Feb 2023 20:34:41 +0800 Message-Id: <20230213123444.155149-7-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Just move the position of the functions. There's no any functionality change. This is to make it easier to review the next patch via putting code near its position in the next patch. Signed-off-by: "Huang, Ying" Reviewed-by: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: Xin Hao Cc: Minchan Kim Cc: Mike Kravetz Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 100 +++++++++++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index ee3e21f1061c..0c7488ebe248 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1236,6 +1236,56 @@ static int __migrate_folio_unmap(struct folio *src, = struct folio *dst, int force return rc; } =20 +/* Obtain the lock on page, remove all ptes. */ +static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_ne= w_page, + unsigned long private, struct folio *src, + struct folio **dstp, int force, bool avoid_force_lock, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) +{ + struct folio *dst; + int rc =3D MIGRATEPAGE_UNMAP; + struct page *newpage =3D NULL; + + if (!thp_migration_supported() && folio_test_transhuge(src)) + return -ENOSYS; + + if (folio_ref_count(src) =3D=3D 1) { + /* Folio was freed from under us. So we are done. */ + folio_clear_active(src); + folio_clear_unevictable(src); + /* free_pages_prepare() will clear PG_isolated. */ + list_del(&src->lru); + migrate_folio_done(src, reason); + return MIGRATEPAGE_SUCCESS; + } + + newpage =3D get_new_page(&src->page, private); + if (!newpage) + return -ENOMEM; + dst =3D page_folio(newpage); + *dstp =3D dst; + + dst->private =3D NULL; + rc =3D __migrate_folio_unmap(src, dst, force, avoid_force_lock, mode); + if (rc =3D=3D MIGRATEPAGE_UNMAP) + return rc; + + /* + * A folio that has not been unmapped will be restored to + * right list unless we want to retry. + */ + if (rc !=3D -EAGAIN && rc !=3D -EDEADLOCK) + list_move_tail(&src->lru, ret); + + if (put_new_page) + put_new_page(&dst->page, private); + else + folio_put(dst); + + return rc; +} + static int __migrate_folio_move(struct folio *src, struct folio *dst, enum migrate_mode mode) { @@ -1296,56 +1346,6 @@ static int __migrate_folio_move(struct folio *src, s= truct folio *dst, return rc; } =20 -/* Obtain the lock on page, remove all ptes. */ -static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_ne= w_page, - unsigned long private, struct folio *src, - struct folio **dstp, int force, bool avoid_force_lock, - enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) -{ - struct folio *dst; - int rc =3D MIGRATEPAGE_UNMAP; - struct page *newpage =3D NULL; - - if (!thp_migration_supported() && folio_test_transhuge(src)) - return -ENOSYS; - - if (folio_ref_count(src) =3D=3D 1) { - /* Folio was freed from under us. So we are done. */ - folio_clear_active(src); - folio_clear_unevictable(src); - /* free_pages_prepare() will clear PG_isolated. */ - list_del(&src->lru); - migrate_folio_done(src, reason); - return MIGRATEPAGE_SUCCESS; - } - - newpage =3D get_new_page(&src->page, private); - if (!newpage) - return -ENOMEM; - dst =3D page_folio(newpage); - *dstp =3D dst; - - dst->private =3D NULL; - rc =3D __migrate_folio_unmap(src, dst, force, avoid_force_lock, mode); - if (rc =3D=3D MIGRATEPAGE_UNMAP) - return rc; - - /* - * A folio that has not been unmapped will be restored to - * right list unless we want to retry. - */ - if (rc !=3D -EAGAIN && rc !=3D -EDEADLOCK) - list_move_tail(&src->lru, ret); - - if (put_new_page) - put_new_page(&dst->page, private); - else - folio_put(dst); - - return rc; -} - /* Migrate the folio to the newly allocated folio in dst. */ static int migrate_folio_move(free_page_t put_new_page, unsigned long priv= ate, struct folio *src, struct folio *dst, --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98319C636CC for ; Mon, 13 Feb 2023 12:36:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229668AbjBMMgJ (ORCPT ); Mon, 13 Feb 2023 07:36:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231270AbjBMMgC (ORCPT ); Mon, 13 Feb 2023 07:36:02 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2FD31ABE2 for ; Mon, 13 Feb 2023 04:35:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291743; x=1707827743; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LJBQcFuwCDNelXry1gr5Vxbs1ZO8LcRLXJ+2vWMUK9M=; b=VHnjtZjsqF2UoB4JZ/iM6za1J48H2+D3PL9c5qCdZoWw8IhuJzDcbsLB m2CIjkCxCAFkqndno8glNziyBQPqKFt1LGHvN8+5Tz6Q0mi1fLEEnK5Rn m3KDcNqjl/j9WRZZgrUIuy5CsXU/fnm9qe96SVd6D6rmy5lJPnsZKceMx kLHma9ZdRY62mnVPSWAq7ykI8q+stmX5IusheLKuZ04elFykRDfBItP+N VErDiG9nrnZMjUCYDjYQrRiQzPqy0x1vnxLJf3nGgmDRTKcKt3bZ/e7lt SXkNqH33zcwUPFzfYyC/R+eng6VSudK1IYf8WcWc4jcxu5TkC0m9oCv6N Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513291" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513291" 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:43 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366659" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366659" 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:39 -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 , Xin Hao , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 7/9] migrate_pages: share more code between _unmap and _move Date: Mon, 13 Feb 2023 20:34:42 +0800 Message-Id: <20230213123444.155149-8-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a code cleanup patch to reduce the duplicated code between the _unmap and _move stages of migrate_pages(). No functionality change is expected. 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: Xin Hao Cc: Minchan Kim Cc: Mike Kravetz Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 207 +++++++++++++++++++++------------------------------ 1 file changed, 85 insertions(+), 122 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 0c7488ebe248..00713ccb6643 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1055,6 +1055,7 @@ static void __migrate_folio_extract(struct folio *dst, static void migrate_folio_undo_src(struct folio *src, int page_was_mapped, struct anon_vma *anon_vma, + bool locked, struct list_head *ret) { if (page_was_mapped) @@ -1062,16 +1063,20 @@ static void migrate_folio_undo_src(struct folio *sr= c, /* Drop an anon_vma reference if we took one */ if (anon_vma) put_anon_vma(anon_vma); - folio_unlock(src); - list_move_tail(&src->lru, ret); + if (locked) + folio_unlock(src); + if (ret) + list_move_tail(&src->lru, ret); } =20 /* Restore the destination folio to the original state upon failure */ static void migrate_folio_undo_dst(struct folio *dst, + bool locked, free_page_t put_new_page, unsigned long private) { - folio_unlock(dst); + if (locked) + folio_unlock(dst); if (put_new_page) put_new_page(&dst->page, private); else @@ -1096,13 +1101,42 @@ static void migrate_folio_done(struct folio *src, folio_put(src); } =20 -static int __migrate_folio_unmap(struct folio *src, struct folio *dst, int= force, - bool avoid_force_lock, enum migrate_mode mode) +/* Obtain the lock on page, remove all ptes. */ +static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_ne= w_page, + unsigned long private, struct folio *src, + struct folio **dstp, int force, bool avoid_force_lock, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) { + struct folio *dst; int rc =3D -EAGAIN; + struct page *newpage =3D NULL; int page_was_mapped =3D 0; struct anon_vma *anon_vma =3D NULL; bool is_lru =3D !__PageMovable(&src->page); + bool locked =3D false; + bool dst_locked =3D false; + + if (!thp_migration_supported() && folio_test_transhuge(src)) + return -ENOSYS; + + if (folio_ref_count(src) =3D=3D 1) { + /* Folio was freed from under us. So we are done. */ + folio_clear_active(src); + folio_clear_unevictable(src); + /* free_pages_prepare() will clear PG_isolated. */ + list_del(&src->lru); + migrate_folio_done(src, reason); + return MIGRATEPAGE_SUCCESS; + } + + newpage =3D get_new_page(&src->page, private); + if (!newpage) + return -ENOMEM; + dst =3D page_folio(newpage); + *dstp =3D dst; + + dst->private =3D NULL; =20 if (!folio_trylock(src)) { if (!force || mode =3D=3D MIGRATE_ASYNC) @@ -1137,6 +1171,7 @@ static int __migrate_folio_unmap(struct folio *src, s= truct folio *dst, int force =20 folio_lock(src); } + locked =3D true; =20 if (folio_test_writeback(src)) { /* @@ -1151,10 +1186,10 @@ static int __migrate_folio_unmap(struct folio *src,= struct folio *dst, int force break; default: rc =3D -EBUSY; - goto out_unlock; + goto out; } if (!force) - goto out_unlock; + goto out; folio_wait_writeback(src); } =20 @@ -1184,7 +1219,8 @@ static int __migrate_folio_unmap(struct folio *src, s= truct folio *dst, int force * This is much like races on refcount of oldpage: just don't BUG(). */ if (unlikely(!folio_trylock(dst))) - goto out_unlock; + goto out; + dst_locked =3D true; =20 if (unlikely(!is_lru)) { __migrate_folio_record(dst, page_was_mapped, anon_vma); @@ -1206,7 +1242,7 @@ static int __migrate_folio_unmap(struct folio *src, s= truct folio *dst, int force if (!src->mapping) { if (folio_test_private(src)) { try_to_free_buffers(src); - goto out_unlock_both; + goto out; } } else if (folio_mapped(src)) { /* Establish migration ptes */ @@ -1221,73 +1257,25 @@ static int __migrate_folio_unmap(struct folio *src,= struct folio *dst, int force return MIGRATEPAGE_UNMAP; } =20 - if (page_was_mapped) - remove_migration_ptes(src, src, false); - -out_unlock_both: - folio_unlock(dst); -out_unlock: - /* Drop an anon_vma reference if we took one */ - if (anon_vma) - put_anon_vma(anon_vma); - folio_unlock(src); out: - - return rc; -} - -/* Obtain the lock on page, remove all ptes. */ -static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_ne= w_page, - unsigned long private, struct folio *src, - struct folio **dstp, int force, bool avoid_force_lock, - enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) -{ - struct folio *dst; - int rc =3D MIGRATEPAGE_UNMAP; - struct page *newpage =3D NULL; - - if (!thp_migration_supported() && folio_test_transhuge(src)) - return -ENOSYS; - - if (folio_ref_count(src) =3D=3D 1) { - /* Folio was freed from under us. So we are done. */ - folio_clear_active(src); - folio_clear_unevictable(src); - /* free_pages_prepare() will clear PG_isolated. */ - list_del(&src->lru); - migrate_folio_done(src, reason); - return MIGRATEPAGE_SUCCESS; - } - - newpage =3D get_new_page(&src->page, private); - if (!newpage) - return -ENOMEM; - dst =3D page_folio(newpage); - *dstp =3D dst; - - dst->private =3D NULL; - rc =3D __migrate_folio_unmap(src, dst, force, avoid_force_lock, mode); - if (rc =3D=3D MIGRATEPAGE_UNMAP) - return rc; - /* * A folio that has not been unmapped will be restored to * right list unless we want to retry. */ - if (rc !=3D -EAGAIN && rc !=3D -EDEADLOCK) - list_move_tail(&src->lru, ret); + if (rc =3D=3D -EAGAIN || rc =3D=3D -EDEADLOCK) + ret =3D NULL; =20 - if (put_new_page) - put_new_page(&dst->page, private); - else - folio_put(dst); + migrate_folio_undo_src(src, page_was_mapped, anon_vma, locked, ret); + migrate_folio_undo_dst(dst, dst_locked, put_new_page, private); =20 return rc; } =20 -static int __migrate_folio_move(struct folio *src, struct folio *dst, - enum migrate_mode mode) +/* Migrate the folio to the newly allocated folio in dst. */ +static int migrate_folio_move(free_page_t put_new_page, unsigned long priv= ate, + struct folio *src, struct folio *dst, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) { int rc; int page_was_mapped =3D 0; @@ -1300,12 +1288,8 @@ static int __migrate_folio_move(struct folio *src, s= truct folio *dst, list_del(&dst->lru); =20 rc =3D move_to_new_folio(dst, src, mode); - - if (rc =3D=3D -EAGAIN) { - list_add(&dst->lru, prev); - __migrate_folio_record(dst, page_was_mapped, anon_vma); - return rc; - } + if (rc) + goto out; =20 if (unlikely(!is_lru)) goto out_unlock_both; @@ -1319,70 +1303,49 @@ static int __migrate_folio_move(struct folio *src, = struct folio *dst, * unsuccessful, and other cases when a page has been temporarily * isolated from the unevictable LRU: but this case is the easiest. */ - if (rc =3D=3D MIGRATEPAGE_SUCCESS) { - folio_add_lru(dst); - if (page_was_mapped) - lru_add_drain(); - } + folio_add_lru(dst); + if (page_was_mapped) + lru_add_drain(); =20 if (page_was_mapped) - remove_migration_ptes(src, - rc =3D=3D MIGRATEPAGE_SUCCESS ? dst : src, false); + remove_migration_ptes(src, dst, false); =20 out_unlock_both: folio_unlock(dst); - /* Drop an anon_vma reference if we took one */ - if (anon_vma) - put_anon_vma(anon_vma); - folio_unlock(src); + set_page_owner_migrate_reason(&dst->page, reason); /* * If migration is successful, decrease refcount of dst, * which will not free the page because new page owner increased * refcounter. */ - if (rc =3D=3D MIGRATEPAGE_SUCCESS) - folio_put(dst); - - return rc; -} - -/* Migrate the folio to the newly allocated folio in dst. */ -static int migrate_folio_move(free_page_t put_new_page, unsigned long priv= ate, - struct folio *src, struct folio *dst, - enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) -{ - int rc; - - rc =3D __migrate_folio_move(src, dst, mode); - if (rc =3D=3D MIGRATEPAGE_SUCCESS) - set_page_owner_migrate_reason(&dst->page, reason); - - if (rc !=3D -EAGAIN) { - /* - * A folio that has been migrated has all references - * removed and will be freed. A folio that has not been - * migrated will have kept its references and be restored. - */ - list_del(&src->lru); - } + folio_put(dst); =20 /* - * If migration is successful, releases reference grabbed during - * isolation. Otherwise, restore the folio to right list unless - * we want to retry. + * A folio that has been migrated has all references removed + * and will be freed. */ - if (rc =3D=3D MIGRATEPAGE_SUCCESS) { - migrate_folio_done(src, reason); - } else if (rc !=3D -EAGAIN) { - list_add_tail(&src->lru, ret); + list_del(&src->lru); + /* Drop an anon_vma reference if we took one */ + if (anon_vma) + put_anon_vma(anon_vma); + folio_unlock(src); + migrate_folio_done(src, reason); =20 - if (put_new_page) - put_new_page(&dst->page, private); - else - folio_put(dst); + return rc; +out: + /* + * A folio that has not been migrated will be restored to + * right list unless we want to retry. + */ + if (rc =3D=3D -EAGAIN) { + list_add(&dst->lru, prev); + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return rc; } =20 + migrate_folio_undo_src(src, page_was_mapped, anon_vma, true, ret); + migrate_folio_undo_dst(dst, true, put_new_page, private); + return rc; } =20 @@ -1918,9 +1881,9 @@ static int migrate_pages_batch(struct list_head *from= , new_page_t get_new_page, =20 __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); migrate_folio_undo_src(folio, page_was_mapped, anon_vma, - ret_folios); + true, ret_folios); list_del(&dst->lru); - migrate_folio_undo_dst(dst, put_new_page, private); + migrate_folio_undo_dst(dst, true, put_new_page, private); dst =3D dst2; dst2 =3D list_next_entry(dst, lru); } --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 736A6C636CC for ; Mon, 13 Feb 2023 12:36:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231312AbjBMMgN (ORCPT ); Mon, 13 Feb 2023 07:36:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229918AbjBMMgK (ORCPT ); Mon, 13 Feb 2023 07:36:10 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07E081ADCC for ; Mon, 13 Feb 2023 04:35:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291747; x=1707827747; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VYkt38A9z8Pq4upkiF6Pp5/5nTEJdBdjh8yFl0FvjyA=; b=MIRr8AHBtU/2gb1dDTS+Q7aOJbfE9BfQ7azCh6SZJNMVYZiwmb+mVCYy HxOCzX5ImrODebITp/lP1idDFhnkoA21ClrOJCKbx6liwacP/80ThcP/B KAd7qkBPayVgg7/aNTAJzbVG/zXo0yLDCqtmuQN/Eken5LvABNXIIhGQ9 nzBgI0cdnmMe+S7TTR8C0lasXGsFWSpHYYI1oNFVqSo1K46NCVNFStGwr HMpXpiHEpjg6PNW1ef63o0WK3jgIQ7bDmyt+gjtGGqSl7kP7RnPZ+dhf4 u8iOfBMlvMCiDoiQZ/J+zH2P888t19s3VLlmZymNZ0e48R5vn+VJF+BCH A==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513310" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513310" 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- 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_pag= e, 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 =3D 1; } =20 @@ -1806,6 +1806,9 @@ static int migrate_pages_batch(struct list_head *from= , new_page_t get_new_page, stats->nr_thp_failed +=3D thp_retry; stats->nr_failed_pages +=3D nr_retry_pages; move: + /* Flush TLBs for all unmapped folios */ + try_to_unmap_flush(); + retry =3D 1; for (pass =3D 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 =3D 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 =3D ptep_get_and_clear(mm, address, pvmw.pte); + + set_tlb_ubc_flush_pending(mm, pte_dirty(pteval)); + } else { + pteval =3D ptep_clear_flush(vma, address, pvmw.pte); + } } =20 /* 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_f= lags flags) =20 /* * 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; =20 if (folio_is_zone_device(folio) && --=20 2.35.1 From nobody Fri Sep 12 05:23:43 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0AFCCC636D4 for ; Mon, 13 Feb 2023 12:36:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230360AbjBMMg0 (ORCPT ); Mon, 13 Feb 2023 07:36:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbjBMMgX (ORCPT ); Mon, 13 Feb 2023 07:36:23 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B6D630F9 for ; Mon, 13 Feb 2023 04:35:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676291753; x=1707827753; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dO4m0t0CfUL8xaVC72bSSWz5Mdmx3h1ISiajGHuEsMg=; b=Dqz+Iq6QvFlShoq9lUTXk9kEJFYnPxppIrrDva3iQYz9ftLhP3LNDm2M 4/lh6mctT/GI3rrpFQeG9X8j21AWUYtueCsBEX8DmL1C+hcqz1uIm1AaR B7GGOjiaUv0Ocml0aSPy/DooOiJiDcj5qJc5cRIstJju+mCKdA3quFbB5 fto8S+xQeDOPy2vKmRQW6qfXIDbnvlJpr9GfFD/Ivcw7GgoO10DDJbzyd qyhn6eQAy2zeZDjDZ1CzKAymqjP6q5/vl52vA3uA5/T0kZ/9C9FcE4Y5s upslzSwwjLfACG0SYWpSNf3InapwD+rLwYRlLu1QHXwcDRcUsVPiC1Cu4 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="310513330" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="310513330" 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:51 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="646366670" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="646366670" 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:47 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Alistair Popple , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Xin Hao , Minchan Kim , Mike Kravetz , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH -v5 9/9] migrate_pages: move THP/hugetlb migration support check to simplify code Date: Mon, 13 Feb 2023 20:34:44 +0800 Message-Id: <20230213123444.155149-10-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is a code cleanup patch, no functionality change is expected. After the change, the line number reduces especially in the long migrate_pages_batch(). Signed-off-by: "Huang, Ying" Suggested-by: Alistair Popple Reviewed-by: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Xin Hao Cc: Minchan Kim Cc: Mike Kravetz Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Reported-by: "Xu, Pengfei" Reported-by: Hugh Dickins --- mm/migrate.c | 83 +++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 2fa420e4f68c..ef68a1aff35c 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1117,9 +1117,6 @@ static int migrate_folio_unmap(new_page_t get_new_pag= e, free_page_t put_new_page bool locked =3D false; bool dst_locked =3D false; =20 - if (!thp_migration_supported() && folio_test_transhuge(src)) - return -ENOSYS; - if (folio_ref_count(src) =3D=3D 1) { /* Folio was freed from under us. So we are done. */ folio_clear_active(src); @@ -1380,16 +1377,6 @@ static int unmap_and_move_huge_page(new_page_t get_n= ew_page, struct anon_vma *anon_vma =3D NULL; struct address_space *mapping =3D NULL; =20 - /* - * Migratability of hugepages depends on architectures and their size. - * This check is necessary because some callers of hugepage migration - * like soft offline and memory hotremove don't walk through page - * tables or check whether the hugepage is pmd-based or not before - * kicking migration. - */ - if (!hugepage_migration_supported(page_hstate(hpage))) - return -ENOSYS; - if (folio_ref_count(src) =3D=3D 1) { /* page was freed from under us. So we are done. */ folio_putback_active_hugetlb(src); @@ -1556,6 +1543,20 @@ static int migrate_hugetlbs(struct list_head *from, = new_page_t get_new_page, =20 cond_resched(); =20 + /* + * Migratability of hugepages depends on architectures and + * their size. This check is necessary because some callers + * of hugepage migration like soft offline and memory + * hotremove don't walk through page tables or check whether + * the hugepage is pmd-based or not before kicking migration. + */ + if (!hugepage_migration_supported(folio_hstate(folio))) { + nr_failed++; + stats->nr_failed_pages +=3D nr_pages; + list_move_tail(&folio->lru, ret_folios); + continue; + } + rc =3D unmap_and_move_huge_page(get_new_page, put_new_page, private, &folio->page, pass > 2, mode, @@ -1565,16 +1566,9 @@ static int migrate_hugetlbs(struct list_head *from, = new_page_t get_new_page, * Success: hugetlb folio will be put back * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list - * -ENOSYS: stay on the from list * Other errno: put on ret_folios list */ switch(rc) { - case -ENOSYS: - /* Hugetlb migration is unsupported */ - nr_failed++; - stats->nr_failed_pages +=3D nr_pages; - list_move_tail(&folio->lru, ret_folios); - break; case -ENOMEM: /* * When memory is low, don't bother to try to migrate @@ -1664,6 +1658,28 @@ static int migrate_pages_batch(struct list_head *fro= m, new_page_t get_new_page, =20 cond_resched(); =20 + /* + * Large folio migration might be unsupported or + * the allocation might be failed so we should retry + * on the same folio with the large folio split + * to normal folios. + * + * Split folios are put in split_folios, and + * we will migrate them after the rest of the + * list is processed. + */ + if (!thp_migration_supported() && is_thp) { + nr_large_failed++; + stats->nr_thp_failed++; + if (!try_split_folio(folio, &split_folios)) { + stats->nr_thp_split++; + continue; + } + stats->nr_failed_pages +=3D nr_pages; + list_move_tail(&folio->lru, ret_folios); + continue; + } + rc =3D migrate_folio_unmap(get_new_page, put_new_page, private, folio, &dst, pass > 2, avoid_force_lock, mode, reason, ret_folios); @@ -1675,36 +1691,9 @@ static int migrate_pages_batch(struct list_head *fro= m, new_page_t get_new_page, * -EAGAIN: stay on the from list * -EDEADLOCK: stay on the from list * -ENOMEM: stay on the from list - * -ENOSYS: stay on the from list * Other errno: put on ret_folios list */ switch(rc) { - /* - * Large folio migration might be unsupported or - * the allocation could've failed so we should retry - * on the same folio with the large folio split - * to normal folios. - * - * Split folios are put in split_folios, and - * we will migrate them after the rest of the - * list is processed. - */ - case -ENOSYS: - /* Large folio migration is unsupported */ - if (is_large) { - nr_large_failed++; - stats->nr_thp_failed +=3D is_thp; - if (!try_split_folio(folio, &split_folios)) { - stats->nr_thp_split +=3D is_thp; - break; - } - } else if (!no_split_folio_counting) { - nr_failed++; - } - - stats->nr_failed_pages +=3D nr_pages; - list_move_tail(&folio->lru, ret_folios); - break; case -ENOMEM: /* * When memory is low, don't bother to try to migrate --=20 2.35.1