From patchwork Thu Mar 21 03:27:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13598369 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 6E402C54E68 for ; Thu, 21 Mar 2024 03:29:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15DB06B00A1; Wed, 20 Mar 2024 23:29:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DB68C6B009B; Wed, 20 Mar 2024 23:28:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6ABE96B009B; Wed, 20 Mar 2024 23:28:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 319586B00A3 for ; Wed, 20 Mar 2024 23:28:59 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E20DA12154A for ; Thu, 21 Mar 2024 03:28:58 +0000 (UTC) X-FDA: 81919614756.05.CBD312A Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf10.hostedemail.com (Postfix) with ESMTP id 51D4AC001C; Thu, 21 Mar 2024 03:28:54 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710991736; 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=5OqRxMEgnmfvIfvFYvfWsTNphKRXKVmHm/AtAv4Ye5c=; b=0iOgatXxKO8byuwkHkyQkFT0rKP0WgALGrymrbMLUTHYs/9/FEEvchAO0Ubi27tjkMaQQS wQvOSW3RZ+KWkn+OjMCDmNcCqK3Qfrd5pR13zwxLgr7Pg1jFOwFX2ocIGo3lDrFaJUXKo1 GNWARmI4/bgk1ILGyVX405Nz5np6TSc= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf10.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710991736; a=rsa-sha256; cv=none; b=gNSaZh9dOjPUKyGjP15wyQ/Z2Q+tsLVSpLeDwHXuoXl/vJB27XIyIJ5dypG7aZU7Z6oFWv jbK+pGhWupPRYx6vQzQgIyhdqMSgODsffIUzOUZH7OH432nf5DUq+06xj7yvP8LxqgAqTN bgHYhF+q39OiNWEkr+hauUqsc9yjN+0= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4V0W8W07vkztQWk; Thu, 21 Mar 2024 11:26:35 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id 22A3714011F; Thu, 21 Mar 2024 11:28:52 +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; Thu, 21 Mar 2024 11:28:51 +0800 From: Kefeng Wang To: Andrew Morton , CC: Tony Luck , Naoya Horiguchi , Miaohe Lin , Matthew Wilcox , David Hildenbrand , Muchun Song , Benjamin LaHaise , , , , Zi Yan , Jiaqi Yan , Hugh Dickins , Kefeng Wang Subject: [PATCH v1 06/11] mm: migrate: split folio_migrate_mapping() Date: Thu, 21 Mar 2024 11:27:42 +0800 Message-ID: <20240321032747.87694-7-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20240321032747.87694-1-wangkefeng.wang@huawei.com> References: <20240321032747.87694-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm100001.china.huawei.com (7.185.36.93) X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 51D4AC001C X-Stat-Signature: 4bkj9rekazwq9395pbjx81imighgn9ub X-HE-Tag: 1710991734-319350 X-HE-Meta: U2FsdGVkX19M/mxAkp5YTjpY1oSpcuJpIiybXpL/SiHCvWQt18S4pJkiF45cH2sCj+fHGtsVYhqxdSf+uy3uq02FTH09Mpf+KPEEWv9hCwZiYwhUajimGJAY/xhlz6FG1Tm7t1z5RvPAICH+mpRwl62Hhr74lJs/v8ZPAhiatLWtARzGiEQk1aM3JRPPizJ5w6qrvUYFJ3TF5xQVfHLpz6MYnmJMQtdgk52UgTahSzSS3U+d1Jvx/R96h5pmNU0qleoFf39oggrPOx+Cek23qhMDudInh2RW4Xr2yCYO8acOylSL1hF4jLwW4XObLx2ZAyGv9dqTlcRR3AyQFRZYRLZFmS+JTT+qszxZUkfyDCOAifJjtfBEnZcShppbStcHrgwXg+Slu65GyAoPLEeBqD+AeO3Ijb4PoACY1sejPUfeZocm6P+oZWSbswTryVgv0pYS3A+xcX0r+co8iiRtSrT6EkRCUB0EBFeFutOd7dsp5SjrlCVTba+SP0k07u8hwxygNwqVB7eUjY3ZxggtABmB0Itz6Uk/F84/MDXAgjXmKPG90nEh1F0uiz9l9LVKEMxzLWlocybRyHYmKXM/JnuZrUKYS3LebKdSESCyRSC3llbhHDYKbFDlJ6NUakCz4TVHppfjni5232Jkq190H8lSI7NDYHqOQC216sXNCIoH2w0zRDVNNQfbGolge7WqPco1rfBY3+2MxIOoObObAg0A8X2WQfNP5O7iphfJml+iSVfyxDRhtqrBFkvPaNx0Ubn/TmeBv8zY+KQehPq9I+RkNc5orpHfN8aOQfa9MbFZYccWrT1re5yGrxp51Xk2b0Pmv/yabPRv2WnmQQZzaeqVgZTRGJRVbCNs9Y57VCAuYutOfXcUhkBwHRWwcwDoL0SFU56AIAoViJmrTWSuRzpajN+OVYzRYY6bWry/Vq3W0E7Ia+lMKnQV72q4PHWspOHUja3Kb1IFYOqSxSa cgqZTcWe vLk131lNRK6jLSACs3rvXxuxscl3aLsb7BKnBvtiESdFHpNZgmHi+SDa8wiOH/Tq2c9DTSXpOZchFqcBb5vWhgBjyjZIfBAFF7kiYMVUaxHhpuZZgMXZpLV2zaNzrw1l8knAQBUWEmsEEpyui2oMDg+BxFAZ8AozP7QyM6KNLQaXyVMAbhn4FmElULFwZ0rJC1YBuTkw1bzM5PpM= 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 669c6c2a1868..59c7d66aacba 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -393,50 +393,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 */ @@ -452,7 +451,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); @@ -466,22 +465,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. */ @@ -518,7 +517,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);