From patchwork Mon Jan 16 06:30:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102652 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 A1FB9C54EBE for ; Mon, 16 Jan 2023 06:31:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35B4D6B0073; Mon, 16 Jan 2023 01:31:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 30BBF6B0074; Mon, 16 Jan 2023 01:31:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 186E46B0075; Mon, 16 Jan 2023 01:31:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0AD316B0073 for ; Mon, 16 Jan 2023 01:31:21 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CE6B0140607 for ; Mon, 16 Jan 2023 06:31:20 +0000 (UTC) X-FDA: 80359690320.26.9A9AE28 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id DCBB54001A for ; Mon, 16 Jan 2023 06:31:18 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JPC5Xi0d; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf04.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673850679; 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=X8c7jL0Oyn73WUhbss2ycxAE4wSVztNwmnRNXhVEDMM=; b=4RyDtJAG1FcaG9PvBgkAX2lo5S2FQVmkwqGoqZspZh5qx64ur+9huXKNKHjkGbiLYVGLqQ eLgh+vA2prJCQlxC0KdtspxwBiNiino4XPS+MW7B0V/AvBr21N29MRJeCLd52ZMydxPgRd v1SxScRvHlfEQcNuWkoxYJpNMoMhTEY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JPC5Xi0d; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf04.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673850679; a=rsa-sha256; cv=none; b=iLCNBizdjqdYmAJebwMeRSXlQPU1aDtqH29eOwOk3ot2xJ1Mc3M31Keg/IGMK17ttiwGo8 OsNVKGCOwL5dCzjk7bm+XLEQLbRgcq1c1+ka9KjllBbVaFsliQec2524mW/Pf4vmawx0jH iMs4tetKeoGcRyuuDywH16ZxRLzl7MU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850678; x=1705386678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sxKVJOynzeHGd0H1WzmILRO/OkDsGqFssjhGSWPRnS4=; b=JPC5Xi0dJSu7uHSMNoAsAcQUnLUM6bifrvbvf3URtrIwG+lSw4GD1ubU ldFWCeuobgAN/Wma9cgdk4Sr0Jnthtkj3nQkFild2GashqiBGErLvd7WH fZOD8OytN8N+RNN1gB5pI+7rLKSWbcksdw4djU1pSm9aNghR5S06PwvrE 6T6kJbH6PXw4qFmMMaO4aEO8ZHQe35nuc8u3ft4i5IaZkAVWWx39+HgGz L/TTfmCOd9EFVJHhzKKJq49xCqlNKXOIeY6bAGIRmPC79qT069vHwXhgE WO4WvRQRqQ3JRPdi221Umjs/Wz+o+dFl85+Jb/Z0yUoexk+B6a4CXoegf g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892139" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892139" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2023 22:31:16 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286606" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286606" Received: from tiangeng-mobl.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.220]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2023 22:31:13 -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 , haoxin , Minchan Kim Subject: [PATCH -v3 2/9] migrate_pages: separate hugetlb folios migration Date: Mon, 16 Jan 2023 14:30:50 +0800 Message-Id: <20230116063057.653862-3-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230116063057.653862-1-ying.huang@intel.com> References: <20230116063057.653862-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DCBB54001A X-Stat-Signature: sbscadyf7mfqd1rqfc99raubpxnniwkd X-HE-Tag: 1673850678-447368 X-HE-Meta: U2FsdGVkX18VJbpvI7W/EIGQaXQOx9xdDva+kkgkH90/ROKuFas0sYxPphd1riCTO5b9ck1udmhWer60kTb/xslqmDNnN3o1L5GbV98fyo29emzAMVilO6Fv99DXATSTn10d1CJI8vH/415IIjhmfOaocXhZFZPqsysuFvyBxFOfNoBEj39wTjvxLQXHZyLiAlht/qkn38iyef3IG3Igp22D0e8s1INSt0+NDSQoszWTr9qWan1ysDS/AVYqGDLGN0lrkBPCd/8MA8SEgS1MgG8wZRtYPO5sycLwVAT7ObSoZ2N2Ws7ERo6nw7FP1baqYwOD/a71ngnVDR430K6NP7QnFDU0dUZVaUgkOYUUS09/sh1mAP61pbXrZs3P8s9997C394LYg8tueVCNTs1xHKxb38+uXtYN89FDgcYFmHneXHkUmn8Rf8JthrdHrFROH7I4U9F0kNX/OXk9np0FUZm9aKpLtMdzzrfVBbv68NuPaFirnocZ1YRdi62ayKTLIjh6/CepKkhyaJ+alz0+ZgJVpssdUgxjjwWZG/YgcbBMSWjSVtDaPIzKl4bjN6i9KyqioS97hyLQrRiwZbEtwKkQxUVbUgfChUdt2LCrDbJq1JjVRxtMWMgYwmrz2ZWvhUeJMIlaz+uW0wUPdYaOxl34LIK0W3pgASBfvTA1ZilPwAlFK0IyZoRwzIyLEuk8cQuHWNFSDpuZxE10MbRQI9SfIy293ojGY4juGkOU11eqjEOzX+rAla8g0W9TsTKW3YLuSEKGyxPSgeLpptZ/tz0MnMGeEK5cn7QCi29VY+5OvnS5T0iuOE8FfiwpfUEH0rCf/gcXbWNw65WQTFoSv8G9DpeaKN0uQ23DxMxZKzwIRshI43Key0L/QksP69DM1xIKL7GV5Mub6lyDJmx20gLQHBztdQCZtjEIf4gi640Hpt0F+gwtE3ePxjuQ1bT5rWizj3Oo9fM= 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" Reviewed-by: Baolin Wang Cc: Zi Yan Cc: Yang Shi Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index ef388a9e4747..be7f37523463 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 and large folios 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() + * 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() + * 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);