From patchwork Wed Apr 24 13:59:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13641921 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 822E8C4345F for ; Wed, 24 Apr 2024 13:59:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 183B78D0012; Wed, 24 Apr 2024 09:59:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 094338D001D; Wed, 24 Apr 2024 09:59:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDD2D8D0013; Wed, 24 Apr 2024 09:59:44 -0400 (EDT) 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 767968D001D for ; Wed, 24 Apr 2024 09:59:44 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3540B1C03FD for ; Wed, 24 Apr 2024 13:59:44 +0000 (UTC) X-FDA: 82044583488.07.E0180A4 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf01.hostedemail.com (Postfix) with ESMTP id BEF4340011; Wed, 24 Apr 2024 13:59:40 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713967182; a=rsa-sha256; cv=none; b=rxz/WWpAhUNemoOZwMP0gHWcV4htowaJTd77NCqoXJn02sKl6chlAaxo3EFQjWKkBHrJC2 M6ZDAnBjndeNwRQXlxf8JpSyBrTLj5yGtPQpKdXpAUul1ipX4kRQpjdQq8bwm8Zyv7kQfk dqr/7F1EjniEn572fSpO24xzl3a8qtg= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713967182; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AuCRYv5qf7jpoyEmEUvAKvvJNQvrHxNIxCpDBspvJfE=; b=tpPJEEYBTn4DxGFOXQDNGTcUPVrxjK4LL9ldoFs9nDc+80D7RJWF9hBaEcHmhLfV715ii8 X8N8TLYzR3JnH+k9iJfhiKoy9u9UGN9LmfHSYDGHi96k3cB/sRdlYffYPm6WV+r1rogBJL D2DBPb80IuNCQBxJGRIQHqjIYsMS2FU= Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VPgZ12htfzShdP; Wed, 24 Apr 2024 21:58:33 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id 0B09318007B; Wed, 24 Apr 2024 21:59:38 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemm100001.china.huawei.com (7.185.36.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Wed, 24 Apr 2024 21:59:37 +0800 From: Kefeng Wang To: Andrew Morton , CC: Tony Luck , Miaohe Lin , Naoya Horiguchi , Matthew Wilcox , David Hildenbrand , Muchun Song , Benjamin LaHaise , , , , Zi Yan , Jiaqi Yan , Hugh Dickins , Vishal Moola , Kefeng Wang Subject: [PATCH v2 06/10] mm: migrate: split folio_migrate_mapping() Date: Wed, 24 Apr 2024 21:59:25 +0800 Message-ID: <20240424135929.2847185-7-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20240424135929.2847185-1-wangkefeng.wang@huawei.com> References: <20240424135929.2847185-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpemm100001.china.huawei.com (7.185.36.93) X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: BEF4340011 X-Stat-Signature: 7pj53gmn4pixnft6t9o5hcrettyjqyqi X-Rspam-User: X-HE-Tag: 1713967180-777942 X-HE-Meta: U2FsdGVkX19Q9hck6UKoTlnUzR0+3fBVousmoqj5jHXCc/d0jT8AZ0AWxK2XX2/TjuTxksjlLAU/Qru6sjSs1WmAvPRpV4824GSH27lRmM7eUx86/gTJjeHlRaNbYLNWJk3bTyPJpzkAKlX0eYqKPQfOLKohveaCjxdv6im3VGwjHvJ3W3dhs17KaaFCv+v1+X89/qMjui8Sh4IpuFQN6OeogzDvj9go/Mu2ZJlYRB3TtE38q3e+p+JXpRY/l581qy2a4eLcB+H3OIRE+bzcD9d7Ply6vyTYzpoubgbCOSmEArkBaiPflHbWvCMcgpILtMGFuJG+3DNyLYU2eJPJDjg3PvMB3FwcTaVzzp2TTiC6qr702cgraEfyTLaK+NmXyl0A9f1uk7fWL79/IQaqx2iRod7m2NKNi1e95nSuDCLtljt9ny2GcLaeJfwFMHz2ffUS/qZrpmvqmM3b8giOZK1YI+mW1MORMEO8Cd4OKAgrTlWJntq2zAXx1eKKh+lJFeS/spFlv/IBYqbyW1PIdMlOaWBKuJlq6MiPtZOuW3cFGMcuEtrO9UvCIQAPHYKvdPiAd9D8nNK/LABfQdls9WmkmTcaK8ePTQQ+ioM2eMgSjHNsWO1BkbJT1CxmPI64aLq5TD+zr20sz5z6174a6YvZPe34vYXdxtPe08ZeAr4aIXkEA3BVXpGuWpdJUj8CXLzt5OaNHSTLc4jNlYuUpqXgc0jpVquef6q/inVIIJ0p/37Kclb3CXLhJn6yiSGvFK/hUmSAC1QVrfW/ve5IV+80BeHv5Fo/AmFNpkd99HDMG7TBsoU+pL7+3ZcmuP12vxHrSh/ODzK2lWsWxy+byDUztn6wgW/pMk4q0/eNr6T42wO6nZK8P3kKF1mQIc1qepY9wHLvnhCAMVCQQdx9CP7aCxZCFakIoGDhoqe2dw8atVWbkSlDHdRQUS8G8H4uBAZ9FtYl7LzeAEO2fnv OkwJ6y5w i1TRuKO0UuUtKx/MtIXbFif0mfYHP8E1Hu1U/ujwbjdwRJYUl2Dslc5xSCzR9HTic7dYl5yrpmr+8suQ9CweF5g7wX0NRtJsaYqBXKjffNGZOS79jpplOVV53HaZJrZI4ynf6Sgz1AyfTcTGBsdNjsw1rkbEx5m95AbHJB9W/n/BItMxihUPwPZti2C2kxSPtrcFCitm2fAp08wjpBPEzgip3k4SOvMjHkUc4lB4T5Zgm0aY4EZFyMqxFzfadHIySpI+njRqlMmMeCtU= 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: List-Subscribe: List-Unsubscribe: The folio_migrate_mapping() function is splitted into two parts, folio_refs_check_and_freeze() and folio_replace_mapping_and_unfreeze(), also update comment from page to folio. Note, the folio_ref_freeze() is moved out of xas_lock_irq(), since the folio is already isolated and locked during migration, so suppose that there is no functional change. Signed-off-by: Kefeng Wang --- mm/migrate.c | 74 +++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 6a9bb4af2595..b27c66af385d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -419,50 +419,49 @@ static int folio_expected_refs(struct address_space *mapping, } /* - * Replace the page in the mapping. - * * The number of remaining references must be: - * 1 for anonymous pages without a mapping - * 2 for pages with a mapping - * 3 for pages with a mapping and PagePrivate/PagePrivate2 set. + * 1 for anonymous folios without a mapping + * 2 for folios with a mapping + * 3 for folios with a mapping and PagePrivate/PagePrivate2 set. */ -int folio_migrate_mapping(struct address_space *mapping, - struct folio *newfolio, struct folio *folio, int extra_count) +static int folio_refs_check_and_freeze(struct address_space *mapping, + struct folio *folio, int expected_cnt) +{ + if (!mapping) { + if (folio_ref_count(folio) != expected_cnt) + return -EAGAIN; + } else { + if (!folio_ref_freeze(folio, expected_cnt)) + return -EAGAIN; + } + + return 0; +} + +/* The folio refcount must be freezed if folio with a mapping */ +static void folio_replace_mapping_and_unfreeze(struct address_space *mapping, + struct folio *newfolio, struct folio *folio, int expected_cnt) { XA_STATE(xas, &mapping->i_pages, folio_index(folio)); struct zone *oldzone, *newzone; - int dirty; - int expected_count = folio_expected_refs(mapping, folio) + extra_count; long nr = folio_nr_pages(folio); long entries, i; + int dirty; if (!mapping) { - /* Anonymous page without mapping */ - if (folio_ref_count(folio) != expected_count) - return -EAGAIN; - - /* No turning back from here */ + /* Anonymous folio without mapping */ newfolio->index = folio->index; newfolio->mapping = folio->mapping; if (folio_test_swapbacked(folio)) __folio_set_swapbacked(newfolio); - - return MIGRATEPAGE_SUCCESS; + return; } oldzone = folio_zone(folio); newzone = folio_zone(newfolio); + /* Now we know that no one else is looking at the folio */ xas_lock_irq(&xas); - if (!folio_ref_freeze(folio, expected_count)) { - xas_unlock_irq(&xas); - return -EAGAIN; - } - - /* - * Now we know that no one else is looking at the folio: - * no turning back from here. - */ newfolio->index = folio->index; newfolio->mapping = folio->mapping; folio_ref_add(newfolio, nr); /* add cache reference */ @@ -478,7 +477,7 @@ int folio_migrate_mapping(struct address_space *mapping, entries = 1; } - /* Move dirty while page refs frozen and newpage not yet exposed */ + /* Move dirty while folio refs frozen and newfolio not yet exposed */ dirty = folio_test_dirty(folio); if (dirty) { folio_clear_dirty(folio); @@ -492,22 +491,22 @@ int folio_migrate_mapping(struct address_space *mapping, } /* - * Drop cache reference from old page by unfreezing - * to one less reference. + * Since old folio's refcount freezed, now drop cache reference from + * old folio by unfreezing to one less reference. * We know this isn't the last reference. */ - folio_ref_unfreeze(folio, expected_count - nr); + folio_ref_unfreeze(folio, expected_cnt - nr); xas_unlock(&xas); /* Leave irq disabled to prevent preemption while updating stats */ /* * If moved to a different zone then also account - * the page for that zone. Other VM counters will be + * the folio for that zone. Other VM counters will be * taken care of when we establish references to the - * new page and drop references to the old page. + * new folio and drop references to the old folio. * - * Note that anonymous pages are accounted for + * Note that anonymous folios are accounted for * via NR_FILE_PAGES and NR_ANON_MAPPED if they * are mapped to swap space. */ @@ -544,7 +543,18 @@ int folio_migrate_mapping(struct address_space *mapping, } } local_irq_enable(); +} + +int folio_migrate_mapping(struct address_space *mapping, struct folio *newfolio, + struct folio *folio, int extra_count) +{ + int ret, expected = folio_expected_refs(mapping, folio) + extra_count; + + ret = folio_refs_check_and_freeze(mapping, folio, expected); + if (ret) + return ret; + folio_replace_mapping_and_unfreeze(mapping, newfolio, folio, expected); return MIGRATEPAGE_SUCCESS; } EXPORT_SYMBOL(folio_migrate_mapping);