From patchwork Tue Jan 10 07:53:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13094777 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21600C46467 for ; Tue, 10 Jan 2023 07:53:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A6D0A8E0005; Tue, 10 Jan 2023 02:53:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F6348E0001; Tue, 10 Jan 2023 02:53:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 824B78E0005; Tue, 10 Jan 2023 02:53:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 717C18E0001 for ; Tue, 10 Jan 2023 02:53:45 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 49C12AED9F for ; Tue, 10 Jan 2023 07:53:45 +0000 (UTC) X-FDA: 80338125210.03.3B75C59 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by imf20.hostedemail.com (Postfix) with ESMTP id 67F101C0014 for ; Tue, 10 Jan 2023 07:53:43 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=L+16QUHT; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673337223; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=35494nDnkMXz4koO2JhR0eCSJtOg4Z8qRFBMG4pROWc=; b=sxCuvOZtOSHky+t8RJb2hrY3NrvsyZk/c/jdE5X15260PibBrdqdl/MAGC77ybhwmgKcMu AieK0vCV/mB09xl1umkJWFmLXGNqdN2zPJGgWTCAjlapR4Cl6twr/hjTXaWbimD+fL128L GGGkbIAi/vty7q9T0vqHdCKqUlUoH6M= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=L+16QUHT; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of ying.huang@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673337223; a=rsa-sha256; cv=none; b=GTTTtcmLDMtws6rbsBTgMVv8mqMqvxxflVbq2A7UzYPU6qOEzkxLmsP+lRTjSkszURlkXU fBIZb27tcr6lytiEC4m6Jrwt/+EklN0cV6YmhNn/HPaPfh+uCePKtzQBoWi+Blj440Zcc+ bXfKrhKmopEDsqVzJTuIJOJ76PAUVRI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673337223; x=1704873223; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rrmNLGIxnXBvmPk6ddHap7Pzun7U4AV1fL0pxDIYoqM=; b=L+16QUHTIa6TPEXmy8nvPwJF79d5gt2Zs9SrFclH+fTj0f9E7FLxO7Z1 HbujGFZxFhm4ZNZkMNsFvvEgh3hVre+31XqwdOvxnrkt6a94tpd6AVNbz hS9wGKJ3wNUSm1p3jFbz50t9Tjj80tMeadg+TyWXwIpLc0REhkMAska3M kRnIzr2DyHRhMVpa1Rr3dMcwogFNhlsAfITJrC2uxl24AVCDp5/JpCk+E FkesFCQ36Nnc4fGoxI62RGxFpjr2o+OfGuN0j0RKkhyPpO4MoGDE8P8dI gs7pCStfmUSIi0YlwqUBQG0YnVi2IT04Zil5AZJ1nmuCjEiLuiF1VxDfB g==; X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="303449282" X-IronPort-AV: E=Sophos;i="5.96,314,1665471600"; d="scan'208";a="303449282" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jan 2023 23:53:41 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10585"; a="902287147" X-IronPort-AV: E=Sophos;i="5.96,314,1665471600"; d="scan'208";a="902287147" Received: from juxinli-mobl.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.254.214.35]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jan 2023 23:53:38 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , haoxin Subject: [PATCH -v2 2/9] migrate_pages: separate hugetlb folios migration Date: Tue, 10 Jan 2023 15:53:20 +0800 Message-Id: <20230110075327.590514-3-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230110075327.590514-1-ying.huang@intel.com> References: <20230110075327.590514-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 67F101C0014 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: om813fiekzjzzhhyyiwbcatf35e3qg4z X-HE-Tag: 1673337223-511398 X-HE-Meta: U2FsdGVkX1+XCnPA0SzOMH8rsvyTmH9eYAUjAVhZUVAV9FnXTpCwQu/E11OVF+NEUqJk00AayEYke/G3mCPIIFR+iyygljbqZAKk1bZgVhff2SH7+RkTyKMf+UyeiiGyTaxidI0UUzZh0pcFacqOBQEizUSk16MQJ7YKGYx/ZVRTwGl29fmJ5PQYAXfcQKI53zrnF1MXnYfFYFiu4KmYpK+C8a5ZE25kATlQDIK8Uq4Aj6bme7JXLfMn9vjtYTwY5421vA3FZRZbqQZY5awpuHhqCLSVGo+2uUBY/UdtxtwIfDYQluQGV/dauYLJoXMQ8w9bNyDuoK8fGd1AWgiQCMEw1SQHApKiqOMSXtLfAUOMCGlGrWEDCyjNPGW2z4SscWnUm0Ltgx3nFqIxeTnwMU2DO7O5DSlNY5N4+I8mEpSrXKRJ+UFcSza9f8ApEA7BHC9v1WUQN3IrP/9KjjzfFH7ADDJaPyeF2u+ZEObudb/0f27QfgY/fShmb4hUpP77kpeRGaNi0hoetHnEYsvL5NoZUk/oYwMldySGNRWbMMdWv5rzK4P30elYl2zdY5Y/yfGcCWGcCyraCOJMrrpwhTvSCO72ncPw91xsktU6jXzTd7bWHjVwrOp2o+AEwZdA5GaqAc+YDdjjA4BDI6q+Ro0QOnHQckZVVtKAqpBLufNpPmsrqsIhFED8i0JCyd9BroXFlYTHNaoYxYGw3AkHXephEXcViPU1Stnyuw3GE2NUVvEENN0+LeaBcuQI4nsklq8tQNbM78gHQ/akftEvtesPgzcHjTHVBVrb85NzgpPRRm6Nnh3CgFsMGCypWLMisDgQQX/0Yt5ygL74erEI2OtwaIexed830AOivSoQma7yg+Mlmqmh4bvRkAPe1LGuSrQ5ugALIPxiPhXbsYYdIoq77Ct/TZsm+r8gbr57m6h8gDD2K9lt96aa8bAidldZ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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" Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Reviewed-by: Baolin Wang --- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index d21de40861a0..04e6802c236c 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1396,6 +1396,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_f return rc; } +#define NR_MAX_MIGRATE_PAGES_RETRY 10 + struct migrate_pages_stats { int nr_succeeded; /* Normal pages and THP migrated successfully, in units of base pages */ @@ -1406,6 +1408,95 @@ struct migrate_pages_stats { int nr_thp_split; /* THP split before migrating */ }; +/* + * Returns the number of hugetlb folios that were not migrated, or an error code + * after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable + * any more because the list has become empty or no retryable hugetlb folios + * exist any more. It is caller's responsibility to call putback_movable_pages() + * to return hugetlb folios to the LRU or free list only if ret != 0. + */ +static int migrate_hugetlbs(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 migrate_pages_stats *stats, + struct list_head *ret_folios) +{ + int retry = 1; + int nr_failed = 0; + int nr_retry_pages = 0; + int pass = 0; + struct folio *folio, *folio2; + int rc, nr_pages; + + for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { + retry = 0; + nr_retry_pages = 0; + + list_for_each_entry_safe(folio, folio2, from, lru) { + if (!folio_test_hugetlb(folio)) + continue; + + nr_pages = folio_nr_pages(folio); + + cond_resched(); + + rc = 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 += 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 += nr_pages + nr_retry_pages; + return -ENOMEM; + case -EAGAIN: + retry++; + nr_retry_pages += nr_pages; + break; + case MIGRATEPAGE_SUCCESS: + stats->nr_succeeded += 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 += 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 += retry; + stats->nr_failed_pages += nr_retry_pages; + + return nr_failed; +} + /* * migrate_pages - migrate the folios specified in a list, to the free folios * supplied as the target for the page migration @@ -1422,10 +1513,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 != 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_pages() + * to return folios to the LRU or free list only if ret != 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 @@ -1439,7 +1530,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, int retry = 1; int large_retry = 1; int thp_retry = 1; - int nr_failed = 0; + int nr_failed; int nr_retry_pages = 0; int nr_large_failed = 0; int pass = 0; @@ -1456,38 +1547,45 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, trace_mm_migrate_pages_start(mode, reason); memset(&stats, 0, sizeof(stats)); + rc = migrate_hugetlbs(from, get_new_page, put_new_page, private, mode, reason, + &stats, &ret_folios); + if (rc < 0) + goto out; + nr_failed = rc; + split_folio_migration: - for (pass = 0; pass < 10 && (retry || large_retry); pass++) { + for (pass = 0; + pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); + pass++) { retry = 0; large_retry = 0; thp_retry = 0; nr_retry_pages = 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; + } + /* * Large folio statistics is based on the source large * folio. Capture required information that might get * lost during migration. */ - is_large = folio_test_large(folio) && !folio_test_hugetlb(folio); + is_large = folio_test_large(folio); is_thp = is_large && folio_test_pmd_mappable(folio); nr_pages = folio_nr_pages(folio); + cond_resched(); - if (folio_test_hugetlb(folio)) - rc = unmap_and_move_huge_page(get_new_page, - put_new_page, private, - &folio->page, pass > 2, mode, - reason, - &ret_folios); - else - rc = unmap_and_move(get_new_page, put_new_page, - private, folio, pass > 2, mode, - reason, &ret_folios); + rc = 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 @@ -1514,7 +1612,6 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, stats.nr_thp_split += is_thp; break; } - /* Hugetlb migration is unsupported */ } else if (!no_split_folio_counting) { nr_failed++; } @@ -1608,8 +1705,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);