From patchwork Wed Oct 18 13:04:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13427030 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 D0041CDB483 for ; Wed, 18 Oct 2023 13:04:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E7AD8D0016; Wed, 18 Oct 2023 09:04:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4982B8D00AD; Wed, 18 Oct 2023 09:04:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38AA58D0016; Wed, 18 Oct 2023 09:04:46 -0400 (EDT) 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 277B38D0016 for ; Wed, 18 Oct 2023 09:04:46 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0083B140203 for ; Wed, 18 Oct 2023 13:04:45 +0000 (UTC) X-FDA: 81358601730.13.AAD32EC Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) by imf09.hostedemail.com (Postfix) with ESMTP id F3C73140051 for ; Wed, 18 Oct 2023 13:04:41 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697634283; a=rsa-sha256; cv=none; b=SU6VBZCrNaiIYpJsdKkLs8z5VO+1jlkLuS/mpc6fzN1rYUfXrsjZMXLzK4fTgGyI5wluqc tAXin35MSykaZZ9EPDG+jY7wwJHTX8gNRvYXMP+lclzhLcGDz+oY2uRHPIuEE7LfXrySV6 4ycOfonXZF5fId1jXuP2UGRIg0Yni7M= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697634283; 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:references; bh=F4JlYDoyYrXpWINTyrX3YfF45mGlBOQzVjEtg1OC3nI=; b=oy7voAGPOyuwi3jcITwELj4DuykJED4dhgHjsCsXag4MvtwxGADsH05Yos3d/M65s+tNKw PqpqGsJ8c/Y2eOHA0nusBTXPEL1JTj6gcAk/NrAuBCIjdMsLaVumF1tPHYX5ruF6sGz7eI 2uLfH5VHRuWgG3t98xTprS6YAMHh6cs= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0VuR0XGI_1697634275; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VuR0XGI_1697634275) by smtp.aliyun-inc.com; Wed, 18 Oct 2023 21:04:36 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: mgorman@techsingularity.net, hughd@google.com, vbabka@suse.cz, ying.huang@intel.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: migrate: record the mlocked page status to remove unnecessary lru drain Date: Wed, 18 Oct 2023 21:04:32 +0800 Message-Id: <64899ad0bb78cde88b52abed1a5a5abbc9919998.1697632761.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: F3C73140051 X-Stat-Signature: 418ps7yfiichci9gcx8w6zm9997t8k85 X-HE-Tag: 1697634281-929978 X-HE-Meta: U2FsdGVkX18mRt+0qMQlQ4dMgyHBVecV3jU1mXuK93TcknRJxf+s+bYn9pnH+zb0GvFrhKHoD0ux0SEk32IjcR157TrQowodn1GoK2zrF2aqc3lvXUbFE2i3fvqfi481RrBetwiy2cDZRzo4iU0HGPq3rQnyMfci4Eem2cEVenrnL6O2Fwq2ll7+qv1fwCOESMUtDu3HS6EnGunxRklY49CZ+Hv8/Ny0LES9bwZySrp+JBAnoa9Ixvh6lYHEjQaxdjCI7PDYFmk71kMP8arqy8GxXMIt3j7/BTTP+/nD3eJDobraqpPS47ovPhJfm7zZ6rk+UE72jj9THYenolnmXJgd4lSf3QfFuNg/2lhKO0s1vij7XxmySdlyENSCVQLmULUP3v+RR5duTl5zo814nZuEDJ3CburoRYfq8LvuDAfz5hojcKpcXn4xWwllkqYqpYceVFlDBE15iVzwmY43XtNS5GIqPK0T11UqEy9GVOJk9zzRRYBPsX18qLLx8SxkrH2d6TwkyeygEhFeXeTTdwfUQSJl5PPmkT3/dzNg0WTEW7DzG2IDTB4kaiH/tHBNi5bljIyhpogeDvHnyHYyRdXhcNqGABYydIVql+9VERWKAevo1lJBjsUzrIsOr2jH/nz76Leqe1jV5zvWaJNHrtHSqsUFpdIjERSLNTQ6Kb5qOnR5A8q/yVPNsAn+cmAzO+Sjk/t8QphB3hHXgs4Kz1luzJuvB3vTqtvnn2/KtSDdLeZBBtGFgpiZbyIlKLWvFrfhXR6VZBGG7CHM+I2Bzr1cKKGJtLPHQhUaxvY/ixoFgHIf+X+ELWlnZbqS3B0ySg7bdDMqCrIV6ZWzyWsnjXPibaidVaNDrJSDzjhZgb9qM44iQ5OwKxKRJ4uKiHRpFc6Y/XfgswSWcmNE5gaPDyR7WsVXDPcm40msZQdP0XZyx47IlEDa1QNSwa6D7iSJVOppVCJyubMwWLZgIMZ YK4TiCKX 80Ejb1HvnMjbja/9810PfxEfn6zzUkdj8eoZ7RDie547kt4wIXAG1b47TcmUfZ3sSW75De6DLWdRLDfM= 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: When doing compaction, I found the lru_add_drain() is an obvious hotspot when migrating pages. The distribution of this hotspot is as follows: - 18.75% compact_zone - 17.39% migrate_pages - 13.79% migrate_pages_batch - 11.66% migrate_folio_move - 7.02% lru_add_drain + 7.02% lru_add_drain_cpu + 3.00% move_to_new_folio 1.23% rmap_walk + 1.92% migrate_folio_unmap + 3.20% migrate_pages_sync + 0.90% isolate_migratepages The lru_add_drain() was added by commit c3096e6782b7 ("mm/migrate: __unmap_and_move() push good newpage to LRU") to drain the newpage to LRU immediately, to help to build up the correct newpage->mlock_count in remove_migration_ptes() for mlocked pages. However, if there are no mlocked pages are migrating, then we can avoid this lru drain operation, especailly for the heavy concurrent scenarios. So we can record the source pages' mlocked status in migrate_folio_unmap(), and only drain the lru list when the mlocked status is set in migrate_folio_move(). In addition, the page was already isolated from lru when migrating, so we check the mlocked status is stable by folio_test_mlocked() in migrate_folio_unmap(). After this patch, I can see the hotpot of the lru_add_drain() is gone: - 9.41% migrate_pages_batch - 6.15% migrate_folio_move - 3.64% move_to_new_folio + 1.80% migrate_folio_extra + 1.70% buffer_migrate_folio + 1.41% rmap_walk + 0.62% folio_add_lru + 3.07% migrate_folio_unmap Meanwhile, the compaction latency shows some improvements when running thpscale: base patched Amean fault-both-1 1131.22 ( 0.00%) 1112.55 * 1.65%* Amean fault-both-3 2489.75 ( 0.00%) 2324.15 * 6.65%* Amean fault-both-5 3257.37 ( 0.00%) 3183.18 * 2.28%* Amean fault-both-7 4257.99 ( 0.00%) 4079.04 * 4.20%* Amean fault-both-12 6614.02 ( 0.00%) 6075.60 * 8.14%* Amean fault-both-18 10607.78 ( 0.00%) 8978.86 * 15.36%* Amean fault-both-24 14911.65 ( 0.00%) 11619.55 * 22.08%* Amean fault-both-30 14954.67 ( 0.00%) 14925.66 * 0.19%* Amean fault-both-32 16654.87 ( 0.00%) 15580.31 * 6.45%* Signed-off-by: Baolin Wang --- mm/migrate.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 4caf405b6504..32c96f89710f 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1027,22 +1027,32 @@ union migration_ptr { struct anon_vma *anon_vma; struct address_space *mapping; }; + +enum { + PAGE_WAS_MAPPED = 1 << 0, + PAGE_WAS_MLOCKED = 1 << 1, +}; + static void __migrate_folio_record(struct folio *dst, - unsigned long page_was_mapped, + unsigned long page_flags, struct anon_vma *anon_vma) { union migration_ptr ptr = { .anon_vma = anon_vma }; dst->mapping = ptr.mapping; - dst->private = (void *)page_was_mapped; + dst->private = (void *)page_flags; } static void __migrate_folio_extract(struct folio *dst, int *page_was_mappedp, + int *page_was_mlocked, struct anon_vma **anon_vmap) { union migration_ptr ptr = { .mapping = dst->mapping }; + unsigned long page_flags = (unsigned long)dst->private; + *anon_vmap = ptr.anon_vma; - *page_was_mappedp = (unsigned long)dst->private; + *page_was_mappedp = page_flags & PAGE_WAS_MAPPED ? 1 : 0; + *page_was_mlocked = page_flags & PAGE_WAS_MLOCKED ? 1 : 0; dst->mapping = NULL; dst->private = NULL; } @@ -1103,7 +1113,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, { struct folio *dst; int rc = -EAGAIN; - int page_was_mapped = 0; + int page_was_mapped = 0, page_was_mlocked = 0; struct anon_vma *anon_vma = NULL; bool is_lru = !__folio_test_movable(src); bool locked = false; @@ -1157,6 +1167,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, folio_lock(src); } locked = true; + page_was_mlocked = folio_test_mlocked(src); if (folio_test_writeback(src)) { /* @@ -1206,7 +1217,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, dst_locked = true; if (unlikely(!is_lru)) { - __migrate_folio_record(dst, page_was_mapped, anon_vma); + __migrate_folio_record(dst, 0, anon_vma); return MIGRATEPAGE_UNMAP; } @@ -1236,7 +1247,13 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, } if (!folio_mapped(src)) { - __migrate_folio_record(dst, page_was_mapped, anon_vma); + unsigned int page_flags = 0; + + if (page_was_mapped) + page_flags |= PAGE_WAS_MAPPED; + if (page_was_mlocked) + page_flags |= PAGE_WAS_MLOCKED; + __migrate_folio_record(dst, page_flags, anon_vma); return MIGRATEPAGE_UNMAP; } @@ -1261,12 +1278,13 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private, struct list_head *ret) { int rc; - int page_was_mapped = 0; + int page_was_mapped = 0, page_was_mlocked = 0; struct anon_vma *anon_vma = NULL; bool is_lru = !__folio_test_movable(src); struct list_head *prev; - __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); + __migrate_folio_extract(dst, &page_was_mapped, + &page_was_mlocked, &anon_vma); prev = dst->lru.prev; list_del(&dst->lru); @@ -1287,7 +1305,7 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private, * isolated from the unevictable LRU: but this case is the easiest. */ folio_add_lru(dst); - if (page_was_mapped) + if (page_was_mlocked) lru_add_drain(); if (page_was_mapped) @@ -1321,8 +1339,15 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private, * right list unless we want to retry. */ if (rc == -EAGAIN) { + unsigned int page_flags = 0; + + if (page_was_mapped) + page_flags |= PAGE_WAS_MAPPED; + if (page_was_mlocked) + page_flags |= PAGE_WAS_MLOCKED; + list_add(&dst->lru, prev); - __migrate_folio_record(dst, page_was_mapped, anon_vma); + __migrate_folio_record(dst, page_flags, anon_vma); return rc; } @@ -1799,10 +1824,11 @@ static int migrate_pages_batch(struct list_head *from, 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; + int page_was_mapped = 0, page_was_mlocked = 0; struct anon_vma *anon_vma = NULL; - __migrate_folio_extract(dst, &page_was_mapped, &anon_vma); + __migrate_folio_extract(dst, &page_was_mapped, + &page_was_mlocked, &anon_vma); migrate_folio_undo_src(folio, page_was_mapped, anon_vma, true, ret_folios); list_del(&dst->lru);