From patchwork Tue Feb 6 11:21:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13547061 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 D3CE2C4829A for ; Tue, 6 Feb 2024 11:22:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5AB036B0093; Tue, 6 Feb 2024 06:21:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E49F6B008C; Tue, 6 Feb 2024 06:21:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED0366B0081; Tue, 6 Feb 2024 06:21:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 83D066B0087 for ; Tue, 6 Feb 2024 06:21:53 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 60139A0235 for ; Tue, 6 Feb 2024 11:21:53 +0000 (UTC) X-FDA: 81761139306.10.192C2E8 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf07.hostedemail.com (Postfix) with ESMTP id E12B84000E; Tue, 6 Feb 2024 11:21:49 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.191 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=1707218511; 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=FInqeyBKBdRx6e9pEyelk5QnPcq0EKHsr7Xo1WvbmuQ=; b=EgNv/SPAn7FNCNl0cXRUn4lKXEPaNN0y/wYOpQR40a2++HtgqK1vZ9lvNtr85Zm9ret2AB mp+XmowqAy371EGKuk3hCBKfmxZ1yLyJ7+IrryQL+W7X3zHB7ox3RdqpFgFNu7e6EnrD5E CGGVgxZsCzK3to/LuuLojRWh+CcrsaE= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.191 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=1707218511; a=rsa-sha256; cv=none; b=xKBc4i3XoRMW9riEcOi37EtWG2561tCRrU6Ff0Z40yOBoOvbLaZUILHHTRTr04prbWpyl8 igt9MPnyqNu/Y1T1Oz99rXXCwQDwyAT2rKyVGkwvjdzCT8GHYZRJPmm1UVyNR+INedDuqM c/jmYPpx0ZGgxAmCQz4YoEDZKBZPqJw= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4TTgkt69Stz1gyfQ; Tue, 6 Feb 2024 19:19:50 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id 6B9D2140412; Tue, 6 Feb 2024 19:21:46 +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_128_GCM_SHA256) id 15.1.2507.35; Tue, 6 Feb 2024 19:21:45 +0800 From: Kefeng Wang To: Andrew Morton , CC: Tony Luck , Naoya Horiguchi , Miaohe Lin , Matthew Wilcox , David Hildenbrand , Muchun Song , Benjamin LaHaise , , , , Kefeng Wang Subject: [PATCH rfcv2 06/11] mm: migrate: split folio_migrate_mapping() Date: Tue, 6 Feb 2024 19:21:29 +0800 Message-ID: <20240206112134.1479464-7-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20240206112134.1479464-1-wangkefeng.wang@huawei.com> References: <20240206112134.1479464-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm100001.china.huawei.com (7.185.36.93) X-Rspamd-Queue-Id: E12B84000E X-Rspam-User: X-Stat-Signature: pd9robkaskdruixxkfudhaw7wybm45kt X-Rspamd-Server: rspam01 X-HE-Tag: 1707218509-308268 X-HE-Meta: U2FsdGVkX1/0qPs9L2mZO3WrxlDsx5RcZh2HFnkpnsZdrd9cCr5EoYBKDrJ8pNc4GPid6pmMV9O5lPhvhhXb6eyhvQJJ5N4xcayA+L9PWIfv2baxcBB+Eh0gNQ8CXjBar0HATTZwgRvWicFy/20RxJ8eXiyt51ofcpWNMiHYl4QpMq+vcuR60vdiPcv6aaC9k1sfx+FgKFZm9LNw1ePqxznuHughMrPzLi6VxNQg0PoRV20t2ysBsNsirBKt855DqgSNBw32TFl6dxcrTVgkiLXHCC9bbS7zPDyrcxPpCCcwT77wxt0znWx57fe80mAa/1HBnh6GjjRfSRsjQjI9Mo62o5uib5B06kFZTW0777KVPoMddHm7h43E/R78mvKnQbnJXSBBMcKWpDl4i7h6qEVT5hcpaYQDytdnDFyuWsZXZUPHiIeepQt67r6okcb5mPVUrigSUQAM1W1Pejt+VeQ3tsUiHDuDmcrJ40JlkJGwtOby5OKZvxVh4OFW9E/W1PR9GzO4iTp4S+EcqCY0JDPd4WCGHIS322xKDF1ZTiyZmj9hAwlOeu5vUMGcvfYBqFjXkY/rTllcPnZiJlZtX/AFrSYMvCrFt3kIaT6L1w+qIcyP4jgw54r5mskyAZ58xnJPaFEJjj0sPUUgbaOJHwU0mHjshuu4eF6bFIIN/KkFOqSD4w6WyCmsf1j9hG7maWzHmkskh8G7ghc20zUehUZshtpwsvOsNYuRboX8ADmM3iYN6rk7CkkbVCUaRdNROXMNHOThgUInDyNMO3upgGXf8TXlQ7mftZFvrmEXLBnDmTnznnoVFLiZMwvZAIMM9htqDFq4A7DnMckz0wukT6NKEB2fIqZxkXCHqow5VimtBSAwA6T9RKbbaFO7nhUy+/Cokva1yaoxqW2vVmvxsCOAkv0xpOzGX5WZYVCMSMoeoslh0P/O8ntCGxT5ACX7nJIpuhtbB5TsrwHUicp uArF9tBq Cyj+Qbt0/5dNiGiJNuZQJEkL3XG6KkJO56GY3e1rxh69shHrrgblnSfKnp+fN67tjhHb/NbrXH0p6GChw59WOZgZAilHGloGe4lGpN7DkGdbsbZCpLuy2JHvT9ys5L5IkE4f5/i3Pl1rbLTbxA8Wjxt10QQbM03M/cTa3tqy4N8430gRmwfSxENAsOX2kOjmIW4zm 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 2dcd0d422056..1db93b5eb819 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -390,50 +390,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 */ @@ -449,7 +448,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); @@ -463,22 +462,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. */ @@ -515,7 +514,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);