Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4882595rwb; Tue, 20 Sep 2022 23:13:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4FPbacST+x4NUoBf//zjTwo1zjMmpvLdQFlFm6YkXd9ZI1+O/ddQFhSg/WOjX7ArwA7JR+ X-Received: by 2002:a17:906:5d0e:b0:77c:ecf8:904c with SMTP id g14-20020a1709065d0e00b0077cecf8904cmr19320398ejt.716.1663740818260; Tue, 20 Sep 2022 23:13:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663740818; cv=none; d=google.com; s=arc-20160816; b=ZkGlWEeewGZCQ9ulup7EvIWwzfAOoN0PQxR9p+BFD8OA60LgWo/YgbOucNQGqt/4CT 8QiIXayG8mxcXoakVzNzQgi+bSxDcEWXDkjxOGcho2Gye9DHbGly9B7VYqAu/KPiQxFU Mpl5QoXO5cJ2BJ+oNDYISFU4aMJtGPP9AhKbZwvNVPObTSiELqbGWWredVBxDUVXXKVM i9mica0gp+HFknMHti1MDzX9ZSWosxgS4k4u7dTKets5PP2eRE7fZhbdZbJ+0Mww7BOi /JlL6rjtRuEQ1wvyjtQ8PXft3xngFsulh7KGq3T28Eg0MLRt7Gn44Nmq3rdppRTO5Oj5 aqpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6sI578zPO31SGuIvnfv9ruk/rXq1w1XVZD9mdppxNr0=; b=wKNywn8tmTZE0uQuc+esHthGlOuDilMNNa5p1t6J9YeXaP6brZigT000p7FMAsNkhL 8g9bRK1+ja+DCQbmvplFWFqYFxNYJ48EeUYq/lO9GsJcrd5KsJUGEa/2aNp4kps1Xysp FcQHQWvh+YLMp95ZvXu3G0s3jS6QAKhER9xzvfpZiTXvfXSnGPn67gQyy5xq43wlS8o+ 4vlBYKFhjpNk+RwJbJ88tjPjaVex7xnQb7IYL8/gCkT5hjFCTwRYr8WNMy6lD/wtAY0v YpmErZUxV9n5nm7A26Dc3wrWCFGshBQWmkx0Om41a1znWQsCUo123DLP7Q3WCBT8OeWQ X3gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=m+YMZEJA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g12-20020a056402428c00b0043d84f9413dsi1636294edc.604.2022.09.20.23.13.11; Tue, 20 Sep 2022 23:13:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=m+YMZEJA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229733AbiIUGHN (ORCPT + 99 others); Wed, 21 Sep 2022 02:07:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229667AbiIUGHG (ORCPT ); Wed, 21 Sep 2022 02:07:06 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 563CF7C1CA for ; Tue, 20 Sep 2022 23:06:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663740419; x=1695276419; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vMOxlk+IH0y/jJD/DO08c91l+llx8vnqZ8FEqWsnej4=; b=m+YMZEJA4Et195RUlu9WpNWq7bHU9pVayHONR0AwDaZdFFvFVmRb/IJI DTn4Pm2PW5U4sIJK8TnRKpppEY0+j6DTtqLREUjUmhOdrsMVEyiPBTXGB 22QEzVdM3KuErVO37s/kayGCWEMUZgCQ9gutb6EOSWCmcKsAEgVqqE32E tpycN1djDqJsqsjVWhgTvJaZ3oHvhhJPmwlo50Xyu9E5Po4CzY6uvDLVm VfgCpFZQCVV8nEoXkNkPs0qcKA+UZcrgs4eMljX27i0BQfnlUtik0xAgQ BJOSbjNHyMWtUiag8utm8TAZJY9a5rPxUP8A7bL+WK0H/pWE3FxhYlo05 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10476"; a="280284848" X-IronPort-AV: E=Sophos;i="5.93,332,1654585200"; d="scan'208";a="280284848" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Sep 2022 23:06:59 -0700 X-IronPort-AV: E=Sophos;i="5.93,332,1654585200"; d="scan'208";a="649913874" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.245]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Sep 2022 23:06:56 -0700 From: Huang Ying To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Huang Ying , Zi Yan , Yang Shi , Baolin Wang , Oscar Salvador , Matthew Wilcox Subject: [RFC 3/6] mm/migrate_pages: restrict number of pages to migrate in batch Date: Wed, 21 Sep 2022 14:06:13 +0800 Message-Id: <20220921060616.73086-4-ying.huang@intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220921060616.73086-1-ying.huang@intel.com> References: <20220921060616.73086-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a preparation patch to batch the page unmapping and moving for the normal pages and THP. If we had batched the page unmapping, all pages to be migrated would be unmapped before copying the contents and flags of the pages. If the number of pages that were passed to migrate_pages() was too large, too many pages would be unmapped. Then, the execution of their processes would be stopped for too long time. For example, migrate_pages() syscall will call migrate_pages() with all pages 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" Cc: Zi Yan Cc: Yang Shi Cc: Baolin Wang Cc: Oscar Salvador Cc: Matthew Wilcox --- mm/migrate.c | 93 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 4a81e0bfdbcd..1077af858e36 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1439,32 +1439,7 @@ static inline int try_split_thp(struct page *page, struct list_head *split_pages return rc; } -/* - * migrate_pages - migrate the pages specified in a list, to the free pages - * supplied as the target for the page migration - * - * @from: The list of pages to be migrated. - * @get_new_page: The function used to allocate free pages to be used - * as the target of the page migration. - * @put_new_page: The function used to free target pages 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 - * page migration, if any. - * @reason: The reason for page migration. - * @ret_succeeded: Set to the number of normal pages migrated successfully if - * the caller passes a non-NULL pointer. - * - * The function returns after 10 attempts or if no pages are movable any more - * because the list has become empty or no retryable pages exist any more. - * It is caller's responsibility to call putback_movable_pages() to return pages - * to the LRU or free list only if ret != 0. - * - * Returns the number of {normal page, THP, hugetlb} that were not migrated, or - * an error code. The number of THP splits will be considered as the number of - * non-migrated THP, no matter how many subpages of the THP 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) { @@ -1709,6 +1684,72 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, return rc; } +/* + * migrate_pages - migrate the pages specified in a list, to the free pages + * supplied as the target for the page migration + * + * @from: The list of pages to be migrated. + * @get_new_page: The function used to allocate free pages to be used + * as the target of the page migration. + * @put_new_page: The function used to free target pages 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 + * page migration, if any. + * @reason: The reason for page migration. + * @ret_succeeded: Set to the number of normal pages migrated successfully if + * the caller passes a non-NULL pointer. + * + * The function returns after 10 attempts or if no pages are movable any more + * because the list has become empty or no retryable pages exist any more. + * It is caller's responsibility to call putback_movable_pages() to return pages + * to the LRU or free list only if ret != 0. + * + * Returns the number of {normal page, THP, hugetlb} that were not migrated, or + * an error code. The number of THP splits will be considered as the number of + * non-migrated THP, no matter how many subpages of the THP 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 *pret_succeeded) +{ + int rc, rc_gether = 0; + int ret_succeeded, ret_succeeded_gether = 0; + int nr_pages; + struct page *page; + LIST_HEAD(pagelist); + LIST_HEAD(ret_pages); + +again: + nr_pages = 0; + list_for_each_entry(page, from, lru) { + nr_pages += compound_nr(page); + if (nr_pages > HPAGE_PMD_NR) + break; + } + if (nr_pages > HPAGE_PMD_NR) + list_cut_before(&pagelist, from, &page->lru); + else + list_splice_init(from, &pagelist); + rc = migrate_pages_batch(&pagelist, get_new_page, put_new_page, private, + mode, reason, &ret_succeeded); + ret_succeeded_gether += ret_succeeded; + list_splice_tail_init(&pagelist, &ret_pages); + if (rc == -ENOMEM) { + rc_gether = rc; + goto out; + } + rc_gether += rc; + if (!list_empty(from)) + goto again; +out: + if (pret_succeeded) + *pret_succeeded = ret_succeeded_gether; + list_splice(&ret_pages, from); + + return rc_gether; +} + struct page *alloc_migration_target(struct page *page, unsigned long private) { struct folio *folio = page_folio(page); -- 2.35.1