From patchwork Mon Jan 16 06:30:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102651 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 39CFDC46467 for ; Mon, 16 Jan 2023 06:31:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B4AAD6B0072; Mon, 16 Jan 2023 01:31:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AD4636B0073; Mon, 16 Jan 2023 01:31:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99BDF6B0074; Mon, 16 Jan 2023 01:31:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8C5D36B0072 for ; Mon, 16 Jan 2023 01:31:16 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 54CF780759 for ; Mon, 16 Jan 2023 06:31:16 +0000 (UTC) X-FDA: 80359690152.21.880BA7B Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf15.hostedemail.com (Postfix) with ESMTP id 85CEBA000C for ; Mon, 16 Jan 2023 06:31:14 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kMOzXywZ; spf=pass (imf15.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673850674; 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=Wpg4lyBb2eMdjSDaJn6katrpTx+/pyJbQHtmI0KRGwI=; b=09ZYNTtL/IAoqC2GQydXBIyL4M3PK/hgevyIqXjy70yHYal4FIJYxQujlCLPf93Bvhg8RE AkRI1n0KuiqBs90cGwTUMlhAG0RDnf5WlN/1gZQj5+/g0aZ2Rdo0vDHN9szV+cH/GbQT+b us7XadllH27hF6Z65ghF7b7ATVwpby0= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=kMOzXywZ; spf=pass (imf15.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673850674; a=rsa-sha256; cv=none; b=DjzMDlfMmXDOS383nIPBgdw3uo8MFqEaSJHBk3yZvpwY9LsAB9HRkJJROm76Q9fOyO5EqP J4Mdk7PDuwwFLjB6Vbu4H8vMVgx6w30grGm1AiJa/cLTJShlEnNbW99k3Fjz02fJP9jqNY TYJBNcH2DI5xTkwXaaur5dV+fp21vKs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850674; x=1705386674; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kyA2nwu/iDvX1f1gFBqUt2qOO7JRIRhIbEkDSdv9gqU=; b=kMOzXywZOcUsEWmNZac/vtinN21sKWhA5z8yXWaSljqUaFOf1rLrHmR6 A/yJlEax9TROZdWgeiNcbwnNZHG01j7JMS87QKawLbsEFC6C9Ujo9nX9s eGDdM9+ReAswnY3s1KDEtk1brI+g/I/phjfIMmMBi4mNym59dOX4zbOKK Up97LXfedwdJCZSwCOQYrHJ4Z8fl25Xc19VDLaKcTvXvE2Cc/9WwlVUrq d7lZTHUCkTACCPFM91d3OSvIdAaPXSAfDrkgzrNk397dPZ0XCG76PtWzI SJ4u+XiS5y3DnkVJkekKkiD5XADPDA5eX4tH4tjef7C+51581Omspx7gS g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892125" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892125" 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:13 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286591" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286591" 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:10 -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 , Yang Shi , Oscar Salvador , Matthew Wilcox , Bharata B Rao , haoxin , Minchan Kim Subject: [PATCH -v3 1/9] migrate_pages: organize stats with struct migrate_pages_stats Date: Mon, 16 Jan 2023 14:30:49 +0800 Message-Id: <20230116063057.653862-2-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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 85CEBA000C X-Stat-Signature: 149tu7niukw5jscju1befpn4womzd4mh X-Rspam-User: X-HE-Tag: 1673850674-305145 X-HE-Meta: U2FsdGVkX1+mvRC1QB3iXa7dAg/fsj4C9UpZYYz/ARNudYP9eSyGZJDSl8jFnQgR+56wYTyV0IFVMUNmOCuBEykkB9jIwIii8s/SrnKdJGujAU1HwSyltWKv6OcXta4FvlSNuXfykrxUx05WkV9l/nKBsAHES+Lo/oYanXOhwRywEjHNN1n7/rIbo/cZdRhFkpinTTCr6CguuUzeGTC8noog9VRhab7mj3f7IQoiHsRXKO9KImzWTkWUc97VA4dpvbYEwtZaEHIc7bglOkzYld7+Ay7Vj6+969I5jsz2UY/byfUH4LT+8+d9v8srEapv4W2Y2qinfIsjCmj69Uj9bZ2scj1HFjgvWNuHEExRt/Dt9Tc32s3Xrdpa1Dv6/cI3JEDTWnNA/W3lqWG3Vna2/jiR9t3OWabtb/KPtJNv2K090XaWfiiFUv410Qs3XZadi4IKco2NVW4zu0HWHnrT+Hn/RBJglIuN1rf/gcE+ZvRCxT8pvMDXyesZeyQSCXEVs76Y9pHfXAkGdVw7GZtaVv3H8rpQ1SOdXQZVczjQz+F3mdt9VkvklCmzP7tYnayoFNPbRraTpCXicU3vGxwmpe2GoBl/mEP6zdkkATGBBfrejdwzcioSxkua6KP8a9kNUt5ysCMEw3ViCnH/XMoaGjZPRrHkVRwfXUQK9BVVAIQMuelUR0hZMWVW2xSW8fGMkK9iKe4gP5AE8MUIwMH/Dywvgq2MIX7//SQhg48kHfl5+tfYqNdB3rlSAT0iENogoCF3hcDU+4mZACwlomVfZuJMlIJVTC4RNaGi1Z/ihJC8ZWti/pCJn8VrdTUUR3K1SKPIYKT8sS/rj6WxSAjifKc/xo1ERti9jLzM49IJUP8pBidYSl9HVyEZX5qgZO/2ki5Z3FoPaW5b8WpAG1+LP4cWeam9eH1drtMZx9R3jHMKeaFoYQ+JZu3Xtg2oDrwp 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: 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 Cc: Yang Shi Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 60 +++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index a4d3fc65085f..ef388a9e4747 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1396,6 +1396,16 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_f return rc; } +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 folios * supplied as the target for the page migration @@ -1430,13 +1440,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, int large_retry = 1; int thp_retry = 1; int nr_failed = 0; - int nr_failed_pages = 0; int nr_retry_pages = 0; - int nr_succeeded = 0; - int nr_thp_succeeded = 0; int nr_large_failed = 0; - int nr_thp_failed = 0; - int nr_thp_split = 0; int pass = 0; bool is_large = false; bool is_thp = false; @@ -1446,9 +1451,11 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, LIST_HEAD(split_folios); bool nosplit = (reason == MR_NUMA_MISPLACED); bool no_split_folio_counting = false; + struct migrate_pages_stats stats; trace_mm_migrate_pages_start(mode, reason); + memset(&stats, 0, sizeof(stats)); split_folio_migration: for (pass = 0; pass < 10 && (retry || large_retry); pass++) { retry = 0; @@ -1502,9 +1509,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 += is_thp; + stats.nr_thp_failed += is_thp; if (!try_split_folio(folio, &split_folios)) { - nr_thp_split += is_thp; + stats.nr_thp_split += is_thp; break; } /* Hugetlb migration is unsupported */ @@ -1512,7 +1519,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++; } - nr_failed_pages += nr_pages; + stats.nr_failed_pages += nr_pages; list_move_tail(&folio->lru, &ret_folios); break; case -ENOMEM: @@ -1522,13 +1529,13 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, */ if (is_large) { nr_large_failed++; - nr_thp_failed += is_thp; + stats.nr_thp_failed += is_thp; /* Large folio NUMA faulting doesn't split to retry. */ if (!nosplit) { int ret = try_split_folio(folio, &split_folios); if (!ret) { - nr_thp_split += is_thp; + stats.nr_thp_split += is_thp; break; } else if (reason == MR_LONGTERM_PIN && ret == -EAGAIN) { @@ -1546,7 +1553,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++; } - nr_failed_pages += nr_pages + nr_retry_pages; + stats.nr_failed_pages += 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 @@ -1556,7 +1563,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 += large_retry; - nr_thp_failed += thp_retry; + stats.nr_thp_failed += thp_retry; goto out; case -EAGAIN: if (is_large) { @@ -1568,8 +1575,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_retry_pages += nr_pages; break; case MIGRATEPAGE_SUCCESS: - nr_succeeded += nr_pages; - nr_thp_succeeded += is_thp; + stats.nr_succeeded += nr_pages; + stats.nr_thp_succeeded += is_thp; break; default: /* @@ -1580,20 +1587,20 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, */ if (is_large) { nr_large_failed++; - nr_thp_failed += is_thp; + stats.nr_thp_failed += is_thp; } else if (!no_split_folio_counting) { nr_failed++; } - nr_failed_pages += nr_pages; + stats.nr_failed_pages += nr_pages; break; } } } nr_failed += retry; nr_large_failed += large_retry; - nr_thp_failed += thp_retry; - nr_failed_pages += nr_retry_pages; + stats.nr_thp_failed += thp_retry; + stats.nr_failed_pages += 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 @@ -1626,16 +1633,17 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, if (list_empty(from)) rc = 0; - 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); if (ret_succeeded) - *ret_succeeded = nr_succeeded; + *ret_succeeded = stats.nr_succeeded; return rc; } 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); From patchwork Mon Jan 16 06:30:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102653 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 B0BD1C46467 for ; Mon, 16 Jan 2023 06:31:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 530D16B0074; Mon, 16 Jan 2023 01:31:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E0546B0075; Mon, 16 Jan 2023 01:31:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 381226B0078; Mon, 16 Jan 2023 01:31:23 -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 295D76B0074 for ; Mon, 16 Jan 2023 01:31:23 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 06B3A1C3789 for ; Mon, 16 Jan 2023 06:31:23 +0000 (UTC) X-FDA: 80359690446.07.9E06BC3 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 20E564000A for ; Mon, 16 Jan 2023 06:31:20 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=AtaqkLxt; 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=1673850681; 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=syKxat5Riidat0fYzK2CyNeqwv4aQF3+v5l2SGALSnE=; b=3U+n3+BiajaGL0iWQo0WDNXtk1NjJV54LqyHcdj8vjX59IIkuCVVmdILmjLu5rDkn1w0GJ LxJRs+RjkGXCrQifm6C1Rk946ca1CBUF+mapoj8O/4HaZPSP1APdv6FAgKkBAMv5f9Rcga r/l+FCFshyQKMQD4Lu/DoCgu1gDEJow= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=AtaqkLxt; 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=1673850681; a=rsa-sha256; cv=none; b=2Y3UkqBY8yolDToSymmIA0EbGOPJhg2dwPNdZB7CgXL65TLMGpTqdpCKhi3JhCgBxsJjIk LKowtCCDa565OIez2iB4CWp5g9fz4lUpo5cYuoI86NSn66wF8S2hCxMrf/By9rNdZ0S0Su v8NjYTKx7izS2tj7I9gSQcIPP+qwwZg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850681; x=1705386681; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uhl+HDmcN3nt7HY4v6GOQD1fVaDhzluLCdJxO2iis+Y=; b=AtaqkLxtkjw2DtTt3e+GYkTdjOyOP3TPFX1i5vexVFVGjIq+AmbUJ/Jd cokowyj0NcLpk2gJ2DFHdK3PkeOimbhf3zLzQHXNkgC4Fjb4dk3NQsZXy TzuQZ4UkdNDVbM+ObU0YhbpQmMx7piE5kI1Vf5BRmnbtPNAVGVhmUSViL zpxQc5zj7j7ux7eZQ1vkl/iKh8jODNfP2x01SRAvZxQaHfweusKGazDvL LooNhu91NoQ5wJrU1kZFrUIf5QbyDewx/bowvh7H04JsI5kPW3N2utH5q 979UWfiKsR9ksaZXVLTVlbsZX0Dlz2BzZ85dT+DEHAvpNF2F6Yq7PvIZP A==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892155" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892155" 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:20 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286645" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286645" 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:17 -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 3/9] migrate_pages: restrict number of pages to migrate in batch Date: Mon, 16 Jan 2023 14:30:51 +0800 Message-Id: <20230116063057.653862-4-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: 20E564000A X-Stat-Signature: 8gr3wb3ftm9kb7fut3j551xd53o98jj6 X-HE-Tag: 1673850680-693133 X-HE-Meta: U2FsdGVkX1+ZmOOF4QYcS1UA93LzyRNhqcmATqVON9TZX/m2HeGXUpz7dlPpON5kst5vS7S+UdfysYDfQ2Jgr9hQQVIbVmkVJzEegm35Xq6jBwwTpPzJz/Oi5ej6QY1SJBcHq9oB1Zf6xwrY6lHLfUkJa35PMlHWgqoVmHFejdkv5u+/LZhWYTnZhcz5Ni0l7/lnbV06ANlPZ/vYuOSarcQihYIHALxMaEVDRVDtsfxxtKpjk4L/REQLg4afwC/mfntOvF67NQ1ktJx1Env9AjELpjeFOPVheC+mOFb6psyolVaDyoTi9w6RVbSN5ZhvJzO5FlVaz+KbEVGFn4b1R37VMfLDl5VU/hYO8kJ/KoQdjphAkSJZiexEeMKJ5AHfWwijJr8Rj5tdV+gLhgV8h5yal9N6AjjDSWIiDW1AaYsKt63WQrUgjrRXemGBqJ4ZyUfZJKTU1GITgq1qKcba19jDWswHElytiHU2OZqpxXnUWcis2Rutbe6sEqu7eIrbki/NIvqqkntJC9joPgAzmveEbpbpO8g+Q7FLZcZkaWBwjKJkY1voGGve/yVNmwnXTNBtVBkQslR5HQWTO2va8gnRVMOuG8G7Ws0NN4Lhb2Pg3GC6jPIV6Tx4TRTg7Sg8/2JsfY2sxsMhtBl7RDoepwIMBF7tUHNSaI3WB3AN051Wqj3glMqqn5aLK2pHBuYT2vnME8dcl/+TQSA+zzhWAiZ5vaOLrVhH7Ztqz7yOODZVOmuiV9Bc13dr2bOLhUoFQ1nbCyXWllLWmoOfjRtD5lMEczQMgIC7NmgA3ba61EHygjpamkOO1oaUX6englz9ZicXcOr2odIyezOiO5bZO6Wnw0eLnxoJx/UTTzlLxxnNipmLQwM4k29n7pNT5pfppT1XLu/E3V6viqdBEw8mLKhsAW7iUEHB 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 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: haoxin Cc: Minchan Kim --- mm/migrate.c | 174 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 106 insertions(+), 68 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index be7f37523463..9a667039c34c 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1396,6 +1396,11 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_f return rc; } +#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 struct migrate_pages_stats { @@ -1497,40 +1502,15 @@ static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page, return nr_failed; } -/* - * migrate_pages - migrate the folios specified in a list, to the free folios - * 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_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 - * 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 = 1; int large_retry = 1; int thp_retry = 1; - int nr_failed; + int nr_failed = 0; int nr_retry_pages = 0; int nr_large_failed = 0; int pass = 0; @@ -1538,20 +1518,9 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, bool is_thp = false; struct folio *folio, *folio2; int rc, nr_pages; - LIST_HEAD(ret_folios); LIST_HEAD(split_folios); bool nosplit = (reason == MR_NUMA_MISPLACED); bool no_split_folio_counting = false; - struct migrate_pages_stats stats; - - 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; @@ -1563,12 +1532,6 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, 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 @@ -1582,15 +1545,14 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, rc = 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) { /* @@ -1607,17 +1569,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 += is_thp; + stats->nr_thp_failed += is_thp; if (!try_split_folio(folio, &split_folios)) { - stats.nr_thp_split += is_thp; + stats->nr_thp_split += is_thp; break; } } else if (!no_split_folio_counting) { nr_failed++; } - stats.nr_failed_pages += nr_pages; - list_move_tail(&folio->lru, &ret_folios); + stats->nr_failed_pages += nr_pages; + list_move_tail(&folio->lru, ret_folios); break; case -ENOMEM: /* @@ -1626,13 +1588,13 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, */ if (is_large) { nr_large_failed++; - stats.nr_thp_failed += is_thp; + stats->nr_thp_failed += is_thp; /* Large folio NUMA faulting doesn't split to retry. */ if (!nosplit) { int ret = try_split_folio(folio, &split_folios); if (!ret) { - stats.nr_thp_split += is_thp; + stats->nr_thp_split += is_thp; break; } else if (reason == MR_LONGTERM_PIN && ret == -EAGAIN) { @@ -1650,17 +1612,17 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++; } - stats.nr_failed_pages += nr_pages + nr_retry_pages; + stats->nr_failed_pages += 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 += large_retry; - stats.nr_thp_failed += thp_retry; + stats->nr_thp_failed += thp_retry; goto out; case -EAGAIN: if (is_large) { @@ -1672,8 +1634,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_retry_pages += nr_pages; break; case MIGRATEPAGE_SUCCESS: - stats.nr_succeeded += nr_pages; - stats.nr_thp_succeeded += is_thp; + stats->nr_succeeded += nr_pages; + stats->nr_thp_succeeded += is_thp; break; default: /* @@ -1684,20 +1646,20 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, */ if (is_large) { nr_large_failed++; - stats.nr_thp_failed += is_thp; + stats->nr_thp_failed += is_thp; } else if (!no_split_folio_counting) { nr_failed++; } - stats.nr_failed_pages += nr_pages; + stats->nr_failed_pages += nr_pages; break; } } } nr_failed += retry; nr_large_failed += large_retry; - stats.nr_thp_failed += thp_retry; - stats.nr_failed_pages += nr_retry_pages; + stats->nr_thp_failed += thp_retry; + stats->nr_failed_pages += 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 @@ -1708,7 +1670,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 = true; retry = 1; @@ -1716,6 +1678,82 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, } rc = nr_failed + nr_large_failed; +out: + return rc; +} + +/* + * migrate_pages - migrate the folios specified in a list, to the free folios + * 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_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 + * 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 = migrate_hugetlbs(from, get_new_page, put_new_page, private, + mode, reason, &stats, &ret_folios); + if (rc_gather < 0) + goto out; +again: + nr_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; + } + + nr_pages += 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 = 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 = rc; + goto out; + } + rc_gather += rc; + if (!list_empty(from)) + goto again; out: /* * Put the permanent failure folio back to migration list, they @@ -1728,7 +1766,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, * are migrated successfully. */ if (list_empty(from)) - rc = 0; + rc_gather = 0; count_vm_events(PGMIGRATE_SUCCESS, stats.nr_succeeded); count_vm_events(PGMIGRATE_FAIL, stats.nr_failed_pages); @@ -1742,7 +1780,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, if (ret_succeeded) *ret_succeeded = stats.nr_succeeded; - return rc; + return rc_gather; } struct page *alloc_migration_target(struct page *page, unsigned long private) From patchwork Mon Jan 16 06:30:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102654 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 CF167C46467 for ; Mon, 16 Jan 2023 06:31:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 705126B0075; Mon, 16 Jan 2023 01:31:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B5276B0078; Mon, 16 Jan 2023 01:31:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 555FB6B007B; Mon, 16 Jan 2023 01:31:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4879A6B0075 for ; Mon, 16 Jan 2023 01:31:27 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1C60E1A06A1 for ; Mon, 16 Jan 2023 06:31:27 +0000 (UTC) X-FDA: 80359690614.19.2D261AB Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 6FA8B4000F for ; Mon, 16 Jan 2023 06:31:24 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=HJeYEezS; 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=1673850685; 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=/ON5pZMoLOY2bojAgs8m7XjxP9OdY4WXNYx4q9Unl9k=; b=FrCkm3NPH4zZRbC+AoJPUiSMciSmL3ZJywFlGk17u1E3u9FYwSB6ONw+ahuOfszmien6A7 UR7JIrjiYlBP6vjTSDVzw6xj+iaCsu0hIYvi77EiTSRBXEVQi59iz35jlIOSnhUyS89bG6 LKaSRfFQviRyNi3FtdXhLPKdZWpr0KI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=HJeYEezS; 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=1673850685; a=rsa-sha256; cv=none; b=MBo0s56F6EGiQYIEej81haWVBB4Hu/mVNdlmMGflQ/KvZN3FfOf55YlZjMmATMEUhEc2jX fRp1KctHSRdOZYMkou6L3xRJ3QjGhgzoxT9yd5cZDqLfAVk7CZp/kyB+jZps64X+pTpOJ3 EWWAI+LZdfYcECybsGzd+w9Rjxy7ZW0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850685; x=1705386685; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=owp2StX07FtXCpFgHDVBdQMId5O0fSuu5EeccMqJEM0=; b=HJeYEezSbw0NZYcVQZEEa7MmdiHpOzvTCO1R/tXksUx4JYppvmrMV5jb CPf81CXqWtKmAZcazQIYkPM31EoNR/5sxzxf+I7cCoC24/xmcJYTl69nu lfDItsyg65u8D6cOc1eSBeJrgDmdYcYCzPMKLATYwuuvtYs7CxA94QRuJ ffZic/Rfgs7fmOBJDbX9w0CnoOD1Gi1Sz6d4vKzHC6j1lkW0+PPOJV992 Iw1+7xHo6HEJfg6SAPyhmTrqxNmT+I9bhx7Td+5CkJx5/y8Q+l8vzroKP 8K8uzqPAvrNlfUmz0zlVtjGwLDjTilyJNa+8kGEHUHqy7wvj1D0/telbL g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892164" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892164" 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:23 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286654" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286654" 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:20 -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 4/9] migrate_pages: split unmap_and_move() to _unmap() and _move() Date: Mon, 16 Jan 2023 14:30:52 +0800 Message-Id: <20230116063057.653862-5-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: 6FA8B4000F X-Stat-Signature: omjqgtcrmzkz7fby1h3r7w7639tmfnkq X-HE-Tag: 1673850684-260626 X-HE-Meta: U2FsdGVkX19JRl1j/BlOJtKkgbSN3qmfN8hTjqIs9PX+3P8Ymq8vNV/IrQX+e5rFuEvPGRlhHH/EmMIStI4hl1V3U99WRHH5d0Fn+upZQYUCFJesfccn9JqD/OSvdpdwwzUUwFmmbUMYINtzICZLW0bknIIe+s5n0A7RmW1bq4y8nCjY5pY+UYce/fq7QJJyUkOsbGa6HcfYnjcPIathz97+WwA5zFxoRAf8LNLciIieCNIHjvEyEPHt+hj0K1ARAabtnXBBKIQWczSHAXFQNd09vf2n8TUc0YsY53M6g6EG3BJrJk8W51WgAtP+YQTzPrXY8EFOo0ie7nBYxr6mMN/teiN3mkSTQe/PCzvkaZpIKA9oCRWXfzPVRKMvzFiP4XwEM6O/u8yQh4obKdvU11kV8kqm6yq+WoKGmL/xHLzOkqvuPG8ilpR7a9KwduybYxNycFlntLJRgkZR3Lie7uAjZuFXI8tqjOKX6+EnWCUy4adIFxspceP/pP7IJDaaiOWSQ3aOlBwLVytEDpo09PtDwuWpIWh+E2FPVj/Qx8kLI1cKbaKMWdiVMT/o9tjVYL444VBcm62KwcR/IhB9MFdyysTV+iHjIST5uilzP1URUoYXc1rMxKMIoBoeLTktqNgyxnnYg4omuYnrftdQEgSqZqDL98/9Lva/EXU4fhWt7GxQUw+b/XV1CkZmNGxE8hXlLW6lmpYdkm0c9ktkDjUOQdm6HkZZf/WW1Rm9IKRm68joIQGp404Oiz3t7iAzIuwfRAuWy2+WxxU+9PCxgkIGu2Y9GqKIiVT8lpEcAvgOamzcWGUkPJ5fmckp0q6ghxY2Uz6O5gi0pYtUbue6odukwaDX+yIPWXAHOgbYeZ6M/qWi9ThQHTthrvG8ZOwW59I9uLh1u0o7+I+ObDFjWM24RX7zNEN+6a82bEmxYE21iyctz396KnAxXwfl+h6N5vioKMU2yzM= 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. 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 Cc: Zi Yan Cc: Yang Shi Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Cc: Minchan Kim --- include/linux/migrate.h | 1 + mm/migrate.c | 170 ++++++++++++++++++++++++++++++---------- 2 files changed, 130 insertions(+), 41 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 3ef77f52a4f0..7376074f2e1e 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 /** * struct movable_operations - Driver page migration diff --git a/mm/migrate.c b/mm/migrate.c index 9a667039c34c..0428449149f4 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1009,11 +1009,53 @@ static int move_to_new_folio(struct folio *dst, struct folio *src, return rc; } -static int __unmap_and_move(struct folio *src, struct folio *dst, +/* + * To record some information during migration, we uses 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 = (void *)anon_vma; + dst->private = (void *)page_was_mapped; +} + +static void __migrate_folio_extract(struct folio *dst, + int *page_was_mappedp, + struct anon_vma **anon_vmap) +{ + *anon_vmap = (void *)dst->mapping; + *page_was_mappedp = (unsigned long)dst->private; + dst->mapping = NULL; + dst->private = 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 != 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 = -EAGAIN; - bool page_was_mapped = false; + int page_was_mapped = 0; struct anon_vma *anon_vma = NULL; bool is_lru = !__PageMovable(&src->page); @@ -1089,8 +1131,8 @@ static int __unmap_and_move(struct folio *src, struct folio *dst, goto out_unlock; if (unlikely(!is_lru)) { - rc = move_to_new_folio(dst, src, mode); - goto out_unlock_both; + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return MIGRATEPAGE_UNMAP; } /* @@ -1115,11 +1157,42 @@ static int __unmap_and_move(struct folio *src, struct 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 = true; + page_was_mapped = 1; } - if (!folio_mapped(src)) - rc = 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 = 0; + struct anon_vma *anon_vma = NULL; + bool is_lru = !__PageMovable(&src->page); + + __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); + + rc = move_to_new_folio(dst, src, mode); + if (unlikely(!is_lru)) + goto out_unlock_both; /* * When successful, push dst to LRU immediately: so that if it @@ -1142,12 +1215,10 @@ static int __unmap_and_move(struct folio *src, struct folio *dst, 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 @@ -1159,19 +1230,15 @@ static int __unmap_and_move(struct folio *src, struct folio *dst, return rc; } -/* - * 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_new_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 = MIGRATEPAGE_SUCCESS; + int rc = MIGRATEPAGE_UNMAP; struct page *newpage = NULL; if (!thp_migration_supported() && folio_test_transhuge(src)) @@ -1182,20 +1249,50 @@ 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; } newpage = get_new_page(&src->page, private); if (!newpage) return -ENOMEM; dst = page_folio(newpage); + *dstp = dst; dst->private = NULL; - rc = __unmap_and_move(src, dst, force, mode); + rc = __migrate_folio_unmap(src, dst, force, mode); + if (rc == MIGRATEPAGE_UNMAP) + return rc; + + /* + * A page that has not been migrated will have kept its + * references and be restored. + */ + /* restore the folio to right list. */ + if (rc != -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 private, + struct folio *src, struct folio *dst, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) +{ + int rc; + + rc = __migrate_folio_move(src, dst, mode); if (rc == MIGRATEPAGE_SUCCESS) set_page_owner_migrate_reason(&dst->page, reason); -out: if (rc != -EAGAIN) { /* * A folio that has been migrated has all references @@ -1211,20 +1308,7 @@ static int unmap_and_move(new_page_t get_new_page, * we want to retry. */ if (rc == 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 != MR_MEMORY_FAILURE) - /* - * We release the folio in page_handle_poison. - */ - folio_put(src); + migrate_folio_done(src, reason); } else { if (rc != -EAGAIN) list_add_tail(&src->lru, ret); @@ -1516,7 +1600,7 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, int pass = 0; bool is_large = false; bool is_thp = false; - struct folio *folio, *folio2; + struct folio *folio, *folio2, *dst = NULL; int rc, nr_pages; LIST_HEAD(split_folios); bool nosplit = (reason == MR_NUMA_MISPLACED); @@ -1543,9 +1627,13 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, cond_resched(); - rc = unmap_and_move(get_new_page, put_new_page, - private, folio, pass > 2, mode, - reason, ret_folios); + rc = migrate_folio_unmap(get_new_page, put_new_page, private, + folio, &dst, pass > 2, mode, + reason, ret_folios); + if (rc == MIGRATEPAGE_UNMAP) + rc = migrate_folio_move(put_new_page, private, + folio, dst, mode, + reason, ret_folios); /* * The rules are: * Success: folio will be freed From patchwork Mon Jan 16 06:30:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102655 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 AAE08C54EBE for ; Mon, 16 Jan 2023 06:31:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 493806B0078; Mon, 16 Jan 2023 01:31:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4443A6B007B; Mon, 16 Jan 2023 01:31:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E5336B007D; Mon, 16 Jan 2023 01:31:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 216366B0078 for ; Mon, 16 Jan 2023 01:31:30 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 050131404FC for ; Mon, 16 Jan 2023 06:31:30 +0000 (UTC) X-FDA: 80359690740.03.50D50CD Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 0F6874000F for ; Mon, 16 Jan 2023 06:31:27 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=A+JM8W2S; 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=1673850688; 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=kFwrZXUdLMJGOSywHTHPFO4EIXk5oPcW9R7hHGQH1OQ=; b=TrXEVAp8Op+PeE9KdiTxA54iv1XqN1GG9wqKOtcVCW+sibVtF3Smz5i5jfID4ww14chf3S 5/uhzgZe2NhWj7nqyu2BTQRoUjXPp5flkJ8iVqkDZV0aR/p4kYJ9lLmTyccwCsygt/Vwz5 uksvC+AmoypN6mVXkrCWluoTLptqNes= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=A+JM8W2S; 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=1673850688; a=rsa-sha256; cv=none; b=BJZWG+q+w/SjNy8mGPl/JkoYznVNdeZXbjsH3gmcOF2whibpOImQ4mqVwY1ADlEuIcDosb 2PxY/3d9hJHqSkDIu9dNmxgJWyV6gLGPGbSgScx8aBmw4+q9x4HoBVk6YPQqR6hAKKhBIg Mo2v2nTwZQDTUgekMyiHuWsYhrO7B4c= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850688; x=1705386688; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2La8Np+Radhjz09NHg913SidfNnKYzsWmL+WkrdLqG4=; b=A+JM8W2SaGGcUmblZDRK1YFemrUmGocemzNq007xMONwymBmtRSu6Eig 86SkXt5a7lryYlcfqyOp6ZC46A6cgAPuG+sX2rLPYJexy4AJc9p1U6PPm scEZ6kOProM9e2aPKs3vPzhVeNAuXhxyZ7RWiC3WRuZOLuDK55gwi+XWX eA0P0YAbARKmsPs6KHUvVJpGMfHNPsFmhvFrZyY1ZqWUUkDShaC47X6ai WtQQ1ztz0rcOUlv85PPGqqyTp8SxUOpNXg6jJCAW8+kzsX2KW0IZOp10l pYQjV70/7Nuk2QBSKiQV/cKTpr+dBh+SR8Wr1OIuZyznp90NfFnmyGZHb g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892176" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892176" 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:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286664" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286664" 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:24 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , haoxin , Minchan Kim Subject: [PATCH -v3 5/9] migrate_pages: batch _unmap and _move Date: Mon, 16 Jan 2023 14:30:53 +0800 Message-Id: <20230116063057.653862-6-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: 0F6874000F X-Stat-Signature: p6dj8qg6gmonigzqqpu81gya8diishx8 X-HE-Tag: 1673850687-139661 X-HE-Meta: U2FsdGVkX1+AuLD7aziKjTQLq9YDywnUYFQocVJxqMc3yg2GpLllpC1YL2b1f6at3+39itiPiUqwNA2Bokr6ybYstwjZMxvS7D8i9T1fHX+w9XyxanE1fI71xdOzkMNr6wjGNmFgmlXJYRoQJXzoQP7ZC10bYSJCM8CcDg4FOnywfkJMzpTJ8A0XZ34XDmFQjfU8LohuP5qNcEDFcgDsOWATeIf0urP4igVtrM/Z+SfNWkgTupIVLzI+IyHfbk2Zoplotr7Bj2JU0WmWNMLPSQ4zloE56EHgARkzlmWc2/dOd7rrC7XtgUdqVvJFHcyv7acQ6Pcp5qfeRszxWKlmaJn1ZSDIjRy+hXoCXOcgfw66Vbe5qQ+u/Fa7PDbDxLFqy9u5x93fh/RiIiS+FKDi6Q8GVTAPeM5O4nGFb3ID0SQZpGzmpGvkiJd6LrXdsWSX/RKHDczrebgo6pKtncratqXb9qqyyO3EoMgkn6ZWV4tZeElzHos/kFgZEJ43VDtMtFJpJMtc1odV+NQjHU6TAYt1uA37SpGR/txqTTaNWlk+IFgPnhmsxlce3Ie32w6ObRAyBRl5iazsW7VARBGI/T1Q5365GMyVKD6BrHps3KuSAO/b7hE235DNnki5B9QHY5SDS3c9ci3dM7/MoBJuImuvuNLigXzBgm5tkJ+oXmFXTEz603mSuU6gclwyCVQcnGUc/26YpXRpY5ktn/fmbUVcd0rNgvmnvABXQ4998Bst7HFeGVkaIiY4q4uxPWxcNUUXoswjbHREWyrKdhGpro4yoPWnm3dF1SNXoAOvdjf4D6XG0ZByB2MO5QI1b/CTe1Z3RL7MJwc4SS65gXKnhFKbTiHWpp9lw8X6t/Vle9L7ppe/R49+WX+1jRCL+vBlIadZ/twsB6Sw99wfW4XOkBxZxhRnObga7wSMjPT5kxtI0yltBF7I0ZfJvFhnQDCRG2v/bk92elk= 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: 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" Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 207 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 183 insertions(+), 24 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 0428449149f4..143d96775b4d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1033,6 +1033,33 @@ static void __migrate_folio_extract(struct folio *dst, dst->private = NULL; } +/* 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) @@ -1052,7 +1079,7 @@ static void migrate_folio_done(struct folio *src, } static int __migrate_folio_unmap(struct folio *src, struct folio *dst, - int force, enum migrate_mode mode) + int force, bool force_lock, enum migrate_mode mode) { int rc = -EAGAIN; int page_was_mapped = 0; @@ -1079,6 +1106,17 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, if (current->flags & PF_MEMALLOC) goto out; + /* + * We have locked some folios, to avoid deadlock, we cannot + * lock the folio synchronously. Go out to process (and + * unlock) all the locked folios. Then we can lock the folio + * synchronously. + */ + if (!force_lock) { + rc = -EDEADLOCK; + goto out; + } + folio_lock(src); } @@ -1191,6 +1229,10 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst, __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); rc = move_to_new_folio(dst, src, mode); + + if (rc != -EAGAIN) + list_del(&dst->lru); + if (unlikely(!is_lru)) goto out_unlock_both; @@ -1209,6 +1251,11 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst, lru_add_drain(); } + if (rc == -EAGAIN) { + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return rc; + } + if (page_was_mapped) remove_migration_ptes(src, rc == MIGRATEPAGE_SUCCESS ? dst : src, false); @@ -1233,7 +1280,7 @@ static int __migrate_folio_move(struct folio *src, struct 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_new_page, unsigned long private, struct folio *src, - struct folio **dstp, int force, + struct folio **dstp, int force, bool force_lock, enum migrate_mode mode, enum migrate_reason reason, struct list_head *ret) { @@ -1261,7 +1308,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page *dstp = dst; dst->private = NULL; - rc = __migrate_folio_unmap(src, dst, force, mode); + rc = __migrate_folio_unmap(src, dst, force, force_lock, mode); if (rc == MIGRATEPAGE_UNMAP) return rc; @@ -1270,7 +1317,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page * references and be restored. */ /* restore the folio to right list. */ - if (rc != -EAGAIN) + if (rc != -EAGAIN && rc != -EDEADLOCK) list_move_tail(&src->lru, ret); if (put_new_page) @@ -1309,9 +1356,8 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private, */ if (rc == MIGRATEPAGE_SUCCESS) { migrate_folio_done(src, reason); - } else { - if (rc != -EAGAIN) - list_add_tail(&src->lru, ret); + } else if (rc != -EAGAIN) { + list_add_tail(&src->lru, ret); if (put_new_page) put_new_page(&dst->page, private); @@ -1591,7 +1637,7 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, enum migrate_mode mode, int reason, struct list_head *ret_folios, struct migrate_pages_stats *stats) { - int retry = 1; + int retry; int large_retry = 1; int thp_retry = 1; int nr_failed = 0; @@ -1600,13 +1646,19 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, int pass = 0; bool is_large = false; bool is_thp = false; - struct folio *folio, *folio2, *dst = NULL; - int rc, nr_pages; + struct folio *folio, *folio2, *dst = NULL, *dst2; + int rc, rc_saved, nr_pages; LIST_HEAD(split_folios); + LIST_HEAD(unmap_folios); + LIST_HEAD(dst_folios); bool nosplit = (reason == MR_NUMA_MISPLACED); bool no_split_folio_counting = false; + bool force_lock; -split_folio_migration: +retry: + rc_saved = 0; + force_lock = true; + retry = 1; for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); pass++) { @@ -1628,16 +1680,15 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, cond_resched(); rc = migrate_folio_unmap(get_new_page, put_new_page, private, - folio, &dst, pass > 2, mode, - reason, ret_folios); - if (rc == MIGRATEPAGE_UNMAP) - rc = migrate_folio_move(put_new_page, private, - folio, dst, mode, - reason, ret_folios); + folio, &dst, pass > 2, 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 @@ -1672,7 +1723,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++; @@ -1711,7 +1762,19 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, /* nr_failed isn't updated for not used */ nr_large_failed += large_retry; stats->nr_thp_failed += thp_retry; - goto out; + rc_saved = 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 = rc; + goto move; case -EAGAIN: if (is_large) { large_retry++; @@ -1725,6 +1788,15 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, stats->nr_succeeded += nr_pages; stats->nr_thp_succeeded += is_thp; break; + case MIGRATEPAGE_UNMAP: + /* + * We have locked some folios, don't force lock + * to avoid deadlock. + */ + force_lock = false; + list_move_tail(&folio->lru, &unmap_folios); + list_add_tail(&dst->lru, &dst_folios); + break; default: /* * Permanent failure (-EBUSY, etc.): @@ -1748,12 +1820,95 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, nr_large_failed += large_retry; stats->nr_thp_failed += thp_retry; stats->nr_failed_pages += nr_retry_pages; +move: + retry = 1; + 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; + + dst = list_first_entry(&dst_folios, struct folio, lru); + dst2 = list_next_entry(dst, lru); + list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) { + is_large = folio_test_large(folio); + is_thp = is_large && folio_test_pmd_mappable(folio); + nr_pages = folio_nr_pages(folio); + + cond_resched(); + + rc = 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 += is_thp; + } else if (!no_split_folio_counting) { + retry++; + } + nr_retry_pages += nr_pages; + break; + case MIGRATEPAGE_SUCCESS: + stats->nr_succeeded += nr_pages; + stats->nr_thp_succeeded += is_thp; + break; + default: + if (is_large) { + nr_large_failed++; + stats->nr_thp_failed += is_thp; + } else if (!no_split_folio_counting) { + nr_failed++; + } + + stats->nr_failed_pages += nr_pages; + break; + } + dst = dst2; + dst2 = list_next_entry(dst, lru); + } + } + nr_failed += retry; + nr_large_failed += large_retry; + stats->nr_thp_failed += thp_retry; + stats->nr_failed_pages += nr_retry_pages; + + if (rc_saved) + rc = rc_saved; + else + rc = nr_failed + nr_large_failed; +out: + /* Cleanup remaining folios */ + dst = list_first_entry(&dst_folios, struct folio, lru); + dst2 = list_next_entry(dst, lru); + list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) { + int page_was_mapped = 0; + struct anon_vma *anon_vma = 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 = dst2; + dst2 = 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 >= 0 && !list_empty(&split_folios)) { /* * Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY * retries) to ret_folios to avoid migrating them again. @@ -1761,12 +1916,16 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, list_splice_init(from, ret_folios); list_splice_init(&split_folios, from); no_split_folio_counting = true; - retry = 1; - goto split_folio_migration; + goto retry; } - rc = nr_failed + nr_large_failed; -out: + /* + * We have unlocked all locked folios, so we can force lock now, let's + * try again. + */ + if (rc == -EDEADLOCK) + goto retry; + return rc; } From patchwork Mon Jan 16 06:30:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102656 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 3C79FC54EBE for ; Mon, 16 Jan 2023 06:31:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B25566B007B; Mon, 16 Jan 2023 01:31:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AD3696B007D; Mon, 16 Jan 2023 01:31:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C2B16B007E; Mon, 16 Jan 2023 01:31:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8DACE6B007B for ; Mon, 16 Jan 2023 01:31:34 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 56855C06D0 for ; Mon, 16 Jan 2023 06:31:34 +0000 (UTC) X-FDA: 80359690908.11.D7B4CD0 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 75B374000A for ; Mon, 16 Jan 2023 06:31:31 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SFWFKENI; 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=1673850691; 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=B4Ib9iv/tkOqjnr8m1P8kQJ3evZVUAW+mGbms/QMtjs=; b=Otrtik2CnkBQk4IOM5/1utETdI/RoPISryNoSrOHt40Q2+wtFk/YhiH1TluwPV+tS4diSr aDxMEsMXBVY1CiwM3NuOsYd3gQUMboKEu1xarEdVXQEIs+Lbtq2wjJn76lofV0K7tx0KY0 wnM+huTjNArP9sEZMFyMPB6iKF37XZY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SFWFKENI; 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=1673850691; a=rsa-sha256; cv=none; b=ctQ1ct0nGz/cBi4G+YNTmA6CkNpC7OzL9jMDgTSz9jBNKCMGUhKLqDuMJdQ9eO9IU6iy4b 2YRN8sosReYwB561w3eUSOIAmIKLHF8PeyFYIHUzHidJBjsiRaOzblYfyOmRQWn9xrPYCe pS8tor3H+Wm+SR225sdr89w1SgY+4bU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850691; x=1705386691; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IWllhTG0GdPmuukl+DFwxPlkS3OUSn/9WM3n7s2+yfw=; b=SFWFKENIEL3uQKI9o5Fen69fU3t59O6a+kK41TKS/9OrxkD+DI/SnLZW ptVwnxXXD+vQZgtVzOyzfr0Rn5AZiRRgQgtGXNjlcnCva/e4Pe6PEDK54 09QbHXZVi0J81e80unhnp+tRVu7qjHliVtB6xR8EbQJT6qwNoRkY8W+/C W6Qm2ioC6PPLi4zdryhEDHjkL6diN3hqbYfngwoix/LG6qmaah/BECb/J FhFrzI0BUs2uk6SI8Kvmn/B2XoVTTqsBBsPSXUS+GAEcWBRtXzGILeOKo Lw76Y+zOQ50x1xoU4349MPepuqzZLu2ZhFLorV25Tjf30AO7+T6xQpdU4 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892193" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892193" 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:30 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286676" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286676" 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:27 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , haoxin , Minchan Kim Subject: [PATCH -v3 6/9] migrate_pages: move migrate_folio_unmap() Date: Mon, 16 Jan 2023 14:30:54 +0800 Message-Id: <20230116063057.653862-7-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: 75B374000A X-Stat-Signature: txas7kc83ksftwq8gegfmdhkqjatb8hq X-HE-Tag: 1673850691-575962 X-HE-Meta: U2FsdGVkX19bPGrm9mD39MOS0uOZexPu+kDw97cdWcPZXaJO1h1hx4QzazajgBr+nP9R7rkDtFxngn6/+tFdqhWuQ3U1s2mD/wj50pEeINzHvyeQ26/QFGpKmdEcEjZg6CCb2zVVgREMbTxPSZQZdxOmHTZCNVvKQ4EVSwPGAIFCfxd3V2xTN7tC0kBlxf44uHwdthrJ40FvfJVXkf+3Yh6DYKVhYpsQKUcfrCo/FR+xes+6mqhsaHtfy15Fs54tdBpKY4I0ti/01x4Sn7t2dfpgV8Hxn4zqKcDjbrnndJto8Evie8ivk5mrv3/HSAzuYAC8QM3SuRc8xKtneyEQWWvOc6szr+uXquN2b4fENatJdqKHsIM7wj2vN6K0PX9twkkeRRFTPKU6db8bPeopdMetDvh2jsaKjp8yMBWMVPGfcnpCHnsxgZgx6BmttDmDAIX4VEu7kU85GaqbvIFwqX5r5Ybg+goSFqMhVhUKc5hF9dqS8VxnqmMetrHE6myT/nsro+rsxKcGhtQ8AXf44eo/omx1mOEsRAh2hav7ekCEzHRP2CTccLgP9n2nHU5v2uhmtRtcCF2oBArJkVbLD1hyH6edRCUTyRTcHcNowuBapGkSFYCHUEzlKRlKXJDYBfUV7Lzyu0LYW4GrrjKss5V1ahYywnMlc4b2+O0PSk6RFbOukIX1HNb/QVb6pGHtVNJwymNdzohsK/H/ZypOwhQCxa1HO8EqUapqfqzUhWcnf/ty/4SyKVRm3giC7SmKYNFwrJHiTd+qf7I+pXG6RA+WhOxwDcbmBDjBdP3YpEVBd3MSi8v07aM8oj+u/WWH+jYJ4tSIsqpDcHZ+vugANRpKSOhbo/rc2FGSKDSR52jNLy1d58yULGkfFJcQKU8wrkDm3GAXaupOYM2fdrWOACHODoltTmj7Nj/1HsdRCc9me6Z8aGfVSRhhefQXL0OVkl9MDlynmfY= 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: 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" Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 102 +++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 143d96775b4d..a25966233923 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1218,6 +1218,57 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, 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_new_page, + unsigned long private, struct folio *src, + struct folio **dstp, int force, bool force_lock, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) +{ + struct folio *dst; + int rc = MIGRATEPAGE_UNMAP; + struct page *newpage = NULL; + + if (!thp_migration_supported() && folio_test_transhuge(src)) + return -ENOSYS; + + if (folio_ref_count(src) == 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 = get_new_page(&src->page, private); + if (!newpage) + return -ENOMEM; + dst = page_folio(newpage); + *dstp = dst; + + dst->private = NULL; + rc = __migrate_folio_unmap(src, dst, force, force_lock, mode); + if (rc == MIGRATEPAGE_UNMAP) + return rc; + + /* + * A page that has not been migrated will have kept its + * references and be restored. + */ + /* restore the folio to right list. */ + if (rc != -EAGAIN && rc != -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) { @@ -1277,57 +1328,6 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst, 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_new_page, - unsigned long private, struct folio *src, - struct folio **dstp, int force, bool force_lock, - enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) -{ - struct folio *dst; - int rc = MIGRATEPAGE_UNMAP; - struct page *newpage = NULL; - - if (!thp_migration_supported() && folio_test_transhuge(src)) - return -ENOSYS; - - if (folio_ref_count(src) == 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 = get_new_page(&src->page, private); - if (!newpage) - return -ENOMEM; - dst = page_folio(newpage); - *dstp = dst; - - dst->private = NULL; - rc = __migrate_folio_unmap(src, dst, force, force_lock, mode); - if (rc == MIGRATEPAGE_UNMAP) - return rc; - - /* - * A page that has not been migrated will have kept its - * references and be restored. - */ - /* restore the folio to right list. */ - if (rc != -EAGAIN && rc != -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 private, struct folio *src, struct folio *dst, From patchwork Mon Jan 16 06:30:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102657 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 812C7C67871 for ; Mon, 16 Jan 2023 06:31:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 249E26B007D; Mon, 16 Jan 2023 01:31:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 222236B007E; Mon, 16 Jan 2023 01:31:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0EA416B0080; Mon, 16 Jan 2023 01:31:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 01FBC6B007D for ; Mon, 16 Jan 2023 01:31:37 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B5FF31C183E for ; Mon, 16 Jan 2023 06:31:36 +0000 (UTC) X-FDA: 80359690992.20.0CB0249 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id BE2B84000C for ; Mon, 16 Jan 2023 06:31:34 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JEyOnmkC; 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=1673850695; 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=SrqhyEiXuvquif5eZBtqifnyHUVKqHgw6sp8tu3F0S0=; b=01ozOJchNDwlsIUE6edLaui1UaoJX1lTCXa2a/+KAZFmMDofl5aDCVXhHWgn2XioIeWI/d QxmushOIhVRBPtY6apW1LybsViXYGbtfr2heKFIDImJZzMux3+nyFew6MHnuI2543q5Jhk jwWoXQtAAhjsvSSs4iScCk8xh5HCPRU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JEyOnmkC; 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=1673850695; a=rsa-sha256; cv=none; b=XY5Mg/99vzm1XvhlJ+Yker5DIIrlcv5BVQxLD66pzOio/vZGrNIy4xVseMf2Dr1z7MHwO4 xnZ/1pPpSyBRxDTOn0ft3cLzk7TbEQmD2imAviZNYibTP1eZ8UPCmz10D2MljxW8KOEQsF D4dDldN/bzh2XdMphictrVNa54EeuMo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850694; x=1705386694; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=c08JPciVGbfSOCD62Qp15TGPybC907KLoZLpS/qN1Zg=; b=JEyOnmkCuANGG2LCuyl4NVOstnYxK0llZZoVMb+WERDBqvMDYwSvAp9U 7rYwKsT+iWuqBt3uOCUsgosJmB7Dl0AXnjcvAOWo8+BAbIUaFUgse5jZ0 NX54hBWk+rzKRVtFBbhK8VCYzpl6QtUta0+FQtIzxcZiSyUDJid7YF3U3 dFnrsPou2WcB2w8d7xA9ccSJMWL7PbmrfimKv5r81MaO312DMNwxvBVh4 s3xUVJ3lrgebe4bp/ggCwELvdMOeunUQ4w4ZSNhYi3+EnzLtzsFgq3thT abm0uRZXSn1twfiEUDQk9dGr8wG3NxPZkwaXe76AnJ8CoEdBqP5IE3/7j g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892211" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892211" 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:34 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286692" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286692" 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:30 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , haoxin , Minchan Kim Subject: [PATCH -v3 7/9] migrate_pages: share more code between _unmap and _move Date: Mon, 16 Jan 2023 14:30:55 +0800 Message-Id: <20230116063057.653862-8-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: BE2B84000C X-Stat-Signature: xyidcmkf6xd1eixgrr6pkxwjen14z8dg X-HE-Tag: 1673850694-561445 X-HE-Meta: U2FsdGVkX18oXgn0QlocRZtNM8qpceZ6xKzBfbGaiSa+ywK338avArn2ia0CKe7Fewg7cAKICvj8WjftgVddEV1nLhBFmIDaQOAHnfR4iOBvXF75BYxDzXB8afwZWWm2FZpx2Kxd2tbMVqEuToTvHbflsiqzZzSeeohJF5q2eREWBI8HhYVS5a5nAOrWZFKwsaIPZqtqf3S2zV8DJvZwAEq5+1nPsSKWVCC1TPNpBFPpm7d45F4cIMV/0gMKZ+9Ar0/YdJxNemivSmA76btxaQpxrrTX1hPnkC4HBPt8G2kNfIqOYq6zEfwAiRsjnljlJbu98NTfmSgldb19aq5hR+OTDZEo1cMGshzEevUreOmGV4vPf8BLUh1x2WRAPA81v0+mPx5Q63jRYbG+g9wjnjTjbPpBvNVHf6NfR9XwbxIIY4IYljDvGFCN0Oxl6NbS5xCrczq5hFZjaeGQcGTo5hsU705++6yKHH5DELe06uoyWF56LoIjBpj2MHJBiDiPB3hZky/VSacnD2KJWh6EbvteMUwWVoCvlSzwoeeKNa6ZFvrx8+mAe1DdkvIdunMaIKWNFV/BOTIBn4DVodNQFInTy4Ai5Yh9W31suLE79VLQyoBroXmIC0/TpFpV69aiwxeXUwPbM+Nsife74jOkf/1d/YmZQgIpIrQXp4pH0MPY+BC24uffc/P4+sTFJd9jsWyKJvpDHN59xYGTN5Y7Br83q+RQmZyvmOLYEd3qDCrKpEPuZ9NEC+H//HfJKZYyTzeEAdiqgORYusSgVlXp0+X/mSBgAuo72e4XJRRZ0Q+H4p4/2cr8KnwWjjxGe541okVwK9HS6mufhez13xjBLAB0oSyava2mahdr3jCXjxaqXv0+XuDfPrX3k+o3/POy//uDVZNpH84LNbDSIBzCebkvr8gINAy4kQIjwhq9Z1Eu87106o36tQeRY0adwwmklgcMZhutSbk= 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 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: haoxin Cc: Minchan Kim --- mm/migrate.c | 205 +++++++++++++++++++++------------------------------ 1 file changed, 82 insertions(+), 123 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index a25966233923..ef9f126e21ed 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1037,6 +1037,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) @@ -1044,16 +1045,20 @@ static void migrate_folio_undo_src(struct folio *src, /* 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); } /* 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 @@ -1078,13 +1083,42 @@ static void migrate_folio_done(struct folio *src, folio_put(src); } -static int __migrate_folio_unmap(struct folio *src, struct folio *dst, - int force, bool 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_new_page, + unsigned long private, struct folio *src, + struct folio **dstp, int force, bool force_lock, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) { + struct folio *dst; int rc = -EAGAIN; + struct page *newpage = NULL; int page_was_mapped = 0; struct anon_vma *anon_vma = NULL; bool is_lru = !__PageMovable(&src->page); + bool locked = false; + bool dst_locked = false; + + if (!thp_migration_supported() && folio_test_transhuge(src)) + return -ENOSYS; + + if (folio_ref_count(src) == 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 = get_new_page(&src->page, private); + if (!newpage) + return -ENOMEM; + dst = page_folio(newpage); + *dstp = dst; + + dst->private = NULL; if (!folio_trylock(src)) { if (!force || mode == MIGRATE_ASYNC) @@ -1119,6 +1153,7 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, folio_lock(src); } + locked = true; if (folio_test_writeback(src)) { /* @@ -1133,10 +1168,10 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, break; default: rc = -EBUSY; - goto out_unlock; + goto out; } if (!force) - goto out_unlock; + goto out; folio_wait_writeback(src); } @@ -1166,7 +1201,8 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, * 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 = true; if (unlikely(!is_lru)) { __migrate_folio_record(dst, page_was_mapped, anon_vma); @@ -1188,7 +1224,7 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, 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 */ @@ -1203,74 +1239,26 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst, 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; -} - -/* Obtain the lock on page, remove all ptes. */ -static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page, - unsigned long private, struct folio *src, - struct folio **dstp, int force, bool force_lock, - enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) -{ - struct folio *dst; - int rc = MIGRATEPAGE_UNMAP; - struct page *newpage = NULL; - - if (!thp_migration_supported() && folio_test_transhuge(src)) - return -ENOSYS; - - if (folio_ref_count(src) == 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 = get_new_page(&src->page, private); - if (!newpage) - return -ENOMEM; - dst = page_folio(newpage); - *dstp = dst; - - dst->private = NULL; - rc = __migrate_folio_unmap(src, dst, force, force_lock, mode); - if (rc == MIGRATEPAGE_UNMAP) - return rc; - /* * A page that has not been migrated will have kept its * references and be restored. */ /* restore the folio to right list. */ - if (rc != -EAGAIN && rc != -EDEADLOCK) - list_move_tail(&src->lru, ret); + if (rc == -EAGAIN || rc == -EDEADLOCK) + ret = NULL; - 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); return rc; } -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 private, + struct folio *src, struct folio *dst, + enum migrate_mode mode, enum migrate_reason reason, + struct list_head *ret) { int rc; int page_was_mapped = 0; @@ -1280,9 +1268,10 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst, __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); rc = move_to_new_folio(dst, src, mode); + if (rc) + goto out; - if (rc != -EAGAIN) - list_del(&dst->lru); + list_del(&dst->lru); if (unlikely(!is_lru)) goto out_unlock_both; @@ -1296,75 +1285,45 @@ 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 == MIGRATEPAGE_SUCCESS) { - folio_add_lru(dst); - if (page_was_mapped) - lru_add_drain(); - } - - if (rc == -EAGAIN) { - __migrate_folio_record(dst, page_was_mapped, anon_vma); - return rc; - } + folio_add_lru(dst); + if (page_was_mapped) + lru_add_drain(); if (page_was_mapped) - remove_migration_ptes(src, - rc == MIGRATEPAGE_SUCCESS ? dst : src, false); + remove_migration_ptes(src, dst, false); 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 == 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 private, - struct folio *src, struct folio *dst, - enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) -{ - int rc; - - rc = __migrate_folio_move(src, dst, mode); - if (rc == MIGRATEPAGE_SUCCESS) - set_page_owner_migrate_reason(&dst->page, reason); - - if (rc != -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); /* - * If migration is successful, releases reference grabbed during - * isolation. Otherwise, restore the folio to right list unless - * we want to retry. + * A page that has been migrated has all references removed + * and will be freed. */ - if (rc == MIGRATEPAGE_SUCCESS) { - migrate_folio_done(src, reason); - } else if (rc != -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); - if (put_new_page) - put_new_page(&dst->page, private); - else - folio_put(dst); + return rc; +out: + if (rc == -EAGAIN) { + __migrate_folio_record(dst, page_was_mapped, anon_vma); + return rc; } + migrate_folio_undo_src(src, page_was_mapped, anon_vma, true, ret); + list_del(&dst->lru); + migrate_folio_undo_dst(dst, true, put_new_page, private); + return rc; } @@ -1896,9 +1855,9 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, __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 = dst2; dst2 = list_next_entry(dst, lru); } From patchwork Mon Jan 16 06:30:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102658 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 063A8C46467 for ; Mon, 16 Jan 2023 06:31:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94EE06B007E; Mon, 16 Jan 2023 01:31:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FFA86B0080; Mon, 16 Jan 2023 01:31:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C6C76B0081; Mon, 16 Jan 2023 01:31:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6ECA56B007E for ; Mon, 16 Jan 2023 01:31:40 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 43A7B403DA for ; Mon, 16 Jan 2023 06:31:40 +0000 (UTC) X-FDA: 80359691160.04.50367EB Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 1310640016 for ; Mon, 16 Jan 2023 06:31:37 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZVch+r3A; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf04.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673850698; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VHED7XLNXdSvIBQE8UtlHoXa2f2MNwI5MeJQhCn0rBk=; b=eYwfbQ93jSZ8ugtc3C3cXCFwJGIE5I55K+XQqKXPKPLK5ayNbPiWeGFUo8Ts4PAbjl7AvI RwbG7ayzVDBHLZoKsZvRErAl844Kt4zek3RGes6aqffJmRCMqcO6fxug8PdqCchmmkCoi8 jw0Om1ERrlXB3qF0Wtk4DK/LhdfSLRg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZVch+r3A; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf04.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673850698; a=rsa-sha256; cv=none; b=vvFC3LOadVSUss/miZ6r0UG8Q4frIDIpa3aIHqt+vO4O/90GV/CVbZ4jFGnXrQUBEE+REy a90mIwtt7S8bjQZmS+rhYJgBAnFvsrGX+fwUZI4c7VUU+U6CJHo4tWj4zpwtknKEMEZGcg 7DnhlKwyoz8iZih8T+tXwJ9yh3kE3tw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850698; x=1705386698; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U4IhIonWVHM0uB5EvKcM6XxvZcENTz+r12d4PtYOG2c=; b=ZVch+r3As2FWyuGdHV5IhCZmtmHXKNnhKhA2eu4oBy6MH91+HAGPZXIi RJB3sLE+41mikK8vsbosGytjeaGmFc7PS+hf7Qy/byEyw5lGWmEclBkbq qeuka8uf8UnYMbguBkcv+v3+5FKAxGVn1EKL7Ce8JmTD4We4nFHwyxp+R LoZjYmmyS/Lbdo+i81jpcyiK60tByH2oZiWSPpqU0Wfn/g/Q928YB0a/B LiMe3NIJMbcpCSsCBInvoLi7ns+lDsgzIRCOaY1hxybVGyAOQRPyjXmcO IOolWhJhsUpbsTt6lmuctNlvDDpC3tKYQqaHVQgoVerY6BWnAa3HnDfD/ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892233" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892233" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2023 22:31:37 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286701" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286701" Received: from tiangeng-mobl.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.220]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2023 22:31:34 -0800 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox , Bharata B Rao , Alistair Popple , haoxin , Minchan Kim Subject: [PATCH -v3 8/9] migrate_pages: batch flushing TLB Date: Mon, 16 Jan 2023 14:30:56 +0800 Message-Id: <20230116063057.653862-9-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230116063057.653862-1-ying.huang@intel.com> References: <20230116063057.653862-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1310640016 X-Stat-Signature: hoyogk5w9fy5dhmjcftbfqcthbu65bb8 X-HE-Tag: 1673850697-336176 X-HE-Meta: U2FsdGVkX19ZQGwAq9RgEns67ridgc8h+G7qS53YdKycwHPdpTT/Aa6lyfiaadDao1d8XCcMG8sFSHFcUu3MPSDZWiXQm5jYC2+4XPCtCku53DzwMm7u9kOSsnapFy1nRmos3rQdrH26yjOjHcSx5wcMAdyDUuJCK0JX3OG5Ica5jTcSnnJvFMuhJXzqIunW6yAsjSRltbDrjYXaX/MmTvjJM7VioQBn9zH8LY5tKqLqnn0SCSsq7tYczb4J1HcrM/vI/lKgyw2aInk1eAH4rakPLvIAlpRLpKGeu46ox9BOvzgS/chglZzLaMQpZQUCY4LBntC1TYtdkQmqj9DUteSkfqg2seY1XABbFnw3M+yrgCZgL/TlH2Z67h8LKeEfn2Zy8+zhjF2FzBqHJbKF3wnKHIFTudwbqpLA9tfXGtgupnnonfBwKh4b/0QRnP7IM7n21DYgWx6+2/u+GUeEmJ+Tx2ssyaGGykbXdJj9QQFIZObCieGGkl1l4U9LSKcD6hFowZ06lBPxT0hoEu2RFO/bHKZ2QTBprJjO8h93zfO3m42sezBUnNELuYw39PWBbQTAuljo/lNgOF32gmRy5+bbw5ySfKE3K3ehhffFtnFsBSprfyHCUjfWtx0j8S1pwKZNQN1X+xfZ5q93TQzSRjCNdaZQaEJ4WG8NurgDV9NQHENXEp3fpRTG9MyG2QVcSN2QysGn8sGQD6g+VVgdy3lPWXmtxjX98b/ZjJKzXhw5BVRTrsOuKqIBRNgR9DrDsA25K7tCw+fq1FhVQcdAP7RrxEJ/vqiWMx42kD2PhTqNg/pGOxWB7XoThJSf4dbiUQg7G5L6/I6t6kMh9th/DQi1/FHo4AkIJ8G0Zf1SRZGl/h+9/w3FT3sJtyaXzO1D6rmPTdXaCzgmV55xmdUwpxLHLZX5JMMNRr5fAx+cqWNDc/MnjHBaUzsHvjHFnlydwyjtOI3lMwY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The TLB flushing will cost quite some CPU cycles during the folio migration in some situations. For example, when migrate a folio of a process with multiple active threads that run on multiple CPUs. After batching the _unmap and _move in migrate_pages(), the TLB flushing can be batched easily with the existing TLB flush batching mechanism. This patch implements that. We use the following test case to test the patch. On a 2-socket Intel server, - Run pmbench memory accessing benchmark - Run `migratepages` to migrate pages of pmbench between node 0 and node 1 back and forth. With the patch, the TLB flushing IPI reduces 99.1% during the test and the number of pages migrated successfully per second increases 291.7%. NOTE: TLB flushing is batched only for normal folios, not for THP folios. Because the overhead of TLB flushing for THP folios is much lower than that for normal folios (about 1/512 on x86 platform). Signed-off-by: "Huang, Ying" Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: Alistair Popple Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 4 +++- mm/rmap.c | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index ef9f126e21ed..8ccb61c49188 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1230,7 +1230,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page /* Establish migration ptes */ VM_BUG_ON_FOLIO(folio_test_anon(src) && !folio_test_ksm(src) && !anon_vma, src); - try_to_migrate(src, 0); + try_to_migrate(src, TTU_BATCH_FLUSH); page_was_mapped = 1; } @@ -1780,6 +1780,8 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, stats->nr_thp_failed += thp_retry; stats->nr_failed_pages += nr_retry_pages; move: + try_to_unmap_flush(); + retry = 1; for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); diff --git a/mm/rmap.c b/mm/rmap.c index b616870a09be..2e125f3e462e 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1976,7 +1976,21 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, } else { flush_cache_page(vma, address, pte_pfn(*pvmw.pte)); /* Nuke the page table entry. */ - pteval = ptep_clear_flush(vma, address, pvmw.pte); + if (should_defer_flush(mm, flags)) { + /* + * We clear the PTE but do not flush so potentially + * a remote CPU could still be writing to the folio. + * If the entry was previously clean then the + * architecture must guarantee that a clear->dirty + * transition on a cached TLB entry is written through + * and traps if the PTE is unmapped. + */ + pteval = ptep_get_and_clear(mm, address, pvmw.pte); + + set_tlb_ubc_flush_pending(mm, pte_dirty(pteval)); + } else { + pteval = ptep_clear_flush(vma, address, pvmw.pte); + } } /* Set the dirty flag on the folio now the pte is gone. */ @@ -2148,10 +2162,10 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) /* * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and - * TTU_SPLIT_HUGE_PMD and TTU_SYNC flags. + * TTU_SPLIT_HUGE_PMD, TTU_SYNC, and TTU_BATCH_FLUSH flags. */ if (WARN_ON_ONCE(flags & ~(TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | - TTU_SYNC))) + TTU_SYNC | TTU_BATCH_FLUSH))) return; if (folio_is_zone_device(folio) && From patchwork Mon Jan 16 06:30:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13102659 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 1083DC54EBE for ; Mon, 16 Jan 2023 06:31:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A126B6B0080; Mon, 16 Jan 2023 01:31:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C2866B0081; Mon, 16 Jan 2023 01:31:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88AD66B0082; Mon, 16 Jan 2023 01:31:44 -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 7C1676B0080 for ; Mon, 16 Jan 2023 01:31:44 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8ED87C0661 for ; Mon, 16 Jan 2023 06:31:43 +0000 (UTC) X-FDA: 80359691286.12.B4FA095 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id 9AA5040018 for ; Mon, 16 Jan 2023 06:31:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UOZiXzIF; 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=1673850701; 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=vdsTK1bdcdezS1W6DaCq42k2ayF2YQCiNcKK08kjjLM=; b=kumpo0tfmPrYFQKxCJWIl1ylJE3jdIz4RgJOx1EmwU06pzjUYtMdtP1nd0RPpxQEtCJTbW TmhynylmZwedcD043/WXkopAsPcshwlrHQp67GUUEgh3isRMxc34/LU3pfx2h6fmvQnW35 3V/O1HYxNmvJcYpy4R6msjybpgvamM0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UOZiXzIF; 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=1673850701; a=rsa-sha256; cv=none; b=BbUVfIHxZ2dGygkMpFfluh05f7NyPouk5x/qV59KlA78wZkMjUsUnBrqXpD+0bdQM3cyeQ OieBzYuR1U72Fa7Ymeic47Wso1EggkMLBs20HTVS5xlJkoZRReWaqFmNg9Phhs7wzlwmoR SZ1y/jOqa2BHF3pLr4NzIjCHOUFK0l0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673850701; x=1705386701; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3J9a3YKiHmKyIA98tevHEicLgJhEATSgMLt7yTqcfnY=; b=UOZiXzIFZmarHiv1iwJrS7zikIqIYdQmT34BJQx3BBhU92M59ZG99/ns MF8YB7fhQmh5xePrEqaBzb6QrZ4JAc4KqDdubkQ6pIjPeniw2K+n7kHij Ezam8xh8Ry0FfYgs40xN97bnTI8NxPVv9MT74x/PGcXBGwwDuSpsQVknv 5okv4ehJHuiPmmyi6C2wVqdrK2yTGnqRDKnG6hwQEDQWhEs/SjsRax2+x vnWeHC8Wq/O14f74aWBE81kthdVbxL0FFyQ0nKe+rthU+MtWwQFcGk4rV 4xO5Ujujvpf7NYfX4rdbP6PzfSvThv1aTslKtOcvgNeRz/Kl8MqeLTJIn w==; X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="388892245" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="388892245" 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:40 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10591"; a="801286715" X-IronPort-AV: E=Sophos;i="5.97,220,1669104000"; d="scan'208";a="801286715" 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:37 -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 , haoxin , Minchan Kim Subject: [PATCH -v3 9/9] migrate_pages: move THP/hugetlb migration support check to simplify code Date: Mon, 16 Jan 2023 14:30:57 +0800 Message-Id: <20230116063057.653862-10-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: 9AA5040018 X-Stat-Signature: cj5q88w1qwbhbmgfh4wwfwnz386w6gz1 X-HE-Tag: 1673850701-730699 X-HE-Meta: U2FsdGVkX1/fNZd6q/6wwWwt+NtN100vf8kKOEUcgcGdgG/iJe/1h48Rmj22RMazTcIEHd5Nf+Q1mjjfZc0WmL1uQFkkHuPTUYwV8/kfzLphWwF0vKSKB7DgMpIUjmOJFELwZeRyFJ4ILY4zlvM8YBq8K8rPY2j2XcNugewoKfxnflEUgpusqOJMR6ZMxoFv2KI9CN525izpBFV8ICTJ8G3Sq5O3FfdHh9bd9q1RFVORep0mpxcZy18/vas2t3LCZB9ouwaV/pFai583XKmhlXsRwmV5KLOrDltDU+7/E99iUhGG02yngCx8ntUXeTh5vW+QY4tlcoYRUYX9YEKxw1M0h4nvXTVIxGS7c1yeLSS7eIsq0UpyN6c4c8j02N0ku7H3jctfT/qWzaxZu0HsWwpKWIXXSbMs1MW3t900+9KLB/mATaRWLcKEXEBrV8bi8VQ/h+r36/auxSs2PfCET34BzGta1Wlf0TWZqKuQO+cx/TyN7CYXTCt8R4Y7qhTRknKimXGEQw4F9pNOGhjMVu0ZJPm7gCpt0Wzz/BitCurHcg3O9223h5Uaz2hnbL03RHi8z1JGsyfhHK06I7rv1A7F3KTFvmbY7567b58W3MDb0GORPGd4zUPLJWdIMXsSe5dle9+sheW7wf9qD07BWK9Hop05KNFXdimekkR3h/1CUEWUrFkltaHGnKfjHj1uDQwgv3PFU5G/HlOyGpxBJfpY02GmKC4ktqcqAOEuHehfRDmEYWb+bAMIVPMWYeKI7ffIYhaXvbcaS7hYj9eFpKe7RSVz9uKqKSpH8GvUxfUEBZCYtXQ9AB0w8rfbsL5oKkPmuesiiqqFbDgihmWNAa9MKeOlXE+yy/SUgCU2EPsXhevjA51VBd37a7Y/BHrWEeeP/yCW/sufT7Oda5jqOBx2USvFQr2vvr8ZTxahICN+afbFni5Zbn66thRx0Ks2lvk5wPLuGxE= 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 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 Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox Cc: Bharata B Rao Cc: haoxin Cc: Minchan Kim --- mm/migrate.c | 83 +++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 8ccb61c49188..9d390368cf3f 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1099,9 +1099,6 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page bool locked = false; bool dst_locked = false; - if (!thp_migration_supported() && folio_test_transhuge(src)) - return -ENOSYS; - if (folio_ref_count(src) == 1) { /* Folio was freed from under us. So we are done. */ folio_clear_active(src); @@ -1358,16 +1355,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page, struct anon_vma *anon_vma = NULL; struct address_space *mapping = NULL; - /* - * 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) == 1) { /* page was freed from under us. So we are done. */ putback_active_hugepage(hpage); @@ -1534,6 +1521,20 @@ static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page, cond_resched(); + /* + * 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 += nr_pages; + list_move_tail(&folio->lru, ret_folios); + continue; + } + rc = unmap_and_move_huge_page(get_new_page, put_new_page, private, &folio->page, pass > 2, mode, @@ -1543,16 +1544,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 += nr_pages; - list_move_tail(&folio->lru, ret_folios); - break; case -ENOMEM: /* * When memory is low, don't bother to try to migrate @@ -1638,6 +1632,28 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, cond_resched(); + /* + * 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 += nr_pages; + list_move_tail(&folio->lru, ret_folios); + continue; + } + rc = migrate_folio_unmap(get_new_page, put_new_page, private, folio, &dst, pass > 2, force_lock, mode, reason, ret_folios); @@ -1649,36 +1665,9 @@ static int migrate_pages_batch(struct list_head *from, 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 += is_thp; - if (!try_split_folio(folio, &split_folios)) { - stats->nr_thp_split += is_thp; - break; - } - } else if (!no_split_folio_counting) { - 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