From patchwork Wed Apr 24 13:59:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13641924 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 EE7C7C4345F for ; Wed, 24 Apr 2024 14:00:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94D0B8D0023; Wed, 24 Apr 2024 09:59:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 80F6A8D0021; Wed, 24 Apr 2024 09:59:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59F808D0020; Wed, 24 Apr 2024 09:59:46 -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 2D2018D0013 for ; Wed, 24 Apr 2024 09:59:46 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EF172160DC0 for ; Wed, 24 Apr 2024 13:59:45 +0000 (UTC) X-FDA: 82044583530.17.46E5139 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf03.hostedemail.com (Postfix) with ESMTP id 31B8420028; Wed, 24 Apr 2024 13:59:41 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 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=1713967183; 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=kNW8OqtsQ7OYIXjhgVws521+WAPaUqPunCADCbHaJbQ=; b=QcGR2v8JPgeIu6KyRVkM0B7X6mVIykRuYZBazeiEJ2riE583pbpmQLNYcrbrynBpodovkC +xoZRx3W52IvpIOyJmkHo5KvwEZZ4mSUoAaLlnbLLTzIXWpE+41JwSyfsZkyIuo5wD+i1I N9Cvwss5tpGZ5l5x7amouMmwDsfCw6Y= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf03.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713967183; a=rsa-sha256; cv=none; b=SNSuInr2Yrup3qBEqjhPJu49OOXwhfh3ag2pVT6wWkCN8gdHsjcMcFghHPLrhS9jnD5Hjq kwbGUBqGkYzZp8bTI321A0V7mtVjZx9kfH9culixfYxMYnDtMfws1hAC5GNapAm8RSCUPs EVT6ss68rLdwFbsw/bwLK/vGP4oHl5c= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VPgZ25zHVzShMx; Wed, 24 Apr 2024 21:58:34 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id 7B3F418006B; Wed, 24 Apr 2024 21:59:39 +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:38 +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 08/10] mm: migrate: support poisoned recover from migrate folio Date: Wed, 24 Apr 2024 21:59:27 +0800 Message-ID: <20240424135929.2847185-9-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-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 31B8420028 X-Stat-Signature: 9m8jcset74ducj8si1kjzhn8bky3kajm X-HE-Tag: 1713967181-17171 X-HE-Meta: U2FsdGVkX1/52oUsXarjacY5AEsWBL9+zDdoO3mJrZyR9xG9Q9FtdmuAhT/FMv/QSVriFFv1OtFx+a+mldiH65VHhzYojUtifvCkNsqBYoRRJ8WuAQPKj0reObx1ee/n4OVKDUDbLJDr0zmGBe2aRNVPB1Mnjm68HNz09mXrOXLyLTIaW6XM6/K3mH1pRCmjErDfGjsD2wIm4N8w9o1KL7r5gBjiFxZGCnQQTWk0fwSIA7m7YAIkXjL5BJj/XtGJOifBesqCRDa1So6oeashbX6bMakVchA7mG51NEMMIQcQzvZ8g/pyh0CWNVd4vApWxIyy75DWV9VSXsA7jyJS3HYZQEgjYicG1YdbkchssB8bCwCMDpb98i/iCD6HOtc6tsKQZ82y7kuBg2/ajc+S9s56OA7LfQ4T6iLF5NanWLgrUO3s0v90y+DAjjCxKVL5QEpKqJWuUZMQLNNHeExyegDNPc951jZ/Zc5XYat0s56LG++dGwqI0QZuT2HrmXfjxnpUm/FGOBwWapAiW8nR8QbZmmmAcOquOFggDT8WEAe7cNy93DMllJIQC5omNDZie1+QcFnbHbewBYVVh9PmxRxzCjAt2owFju8gEfYOv8UmkV/rFmgPWpHgnB7g9yg8Yu1sZd8smD7/GsBA+LWXrl78QA8GrPLS3X0SL9jDirNs/2ufc8qMgPuYDNZKhWrDkIFkZQLuBcu8POYnzwSBoQtt4qCghomDCkmYyYIRg4rxkX6QfZLb5ADR1/oKgGK/TOBRFQsB5rc5YPZQrv/rg47EluZgOtwFUer/cxaNmhqM/TbYJ/opkqe96/wHHr0akEun4YbZvtSUO1cK5Tmaqg1MFgPFojz8Dl2UIAwnp2xdH0Di3WgzubsgelEoe6OCff5ofd9PHoLQRm3U0EYSr+a2pnnvp7tK1my/JZyTawJ/adlR693IklBf5bKEYWU8sqqKXRrfywFm7JeN1eM w0PPGTMG cwLarZstOebNfVTusaL1eUG6i/9AyS3I5PQHncmZIRhyK0JybFRlhteh9Y+wCauAATCIjztGKOBZQc2BJcuNUO0QRvBBmJs2AcOMWY54Th0kZtYAHWaSZjeVOdZea928n7dSxrW0WGpDX3vvWPy0QoIOXV9Aq+zker+gHXx7628ajTsJpIpVoiWSMLRbwIXtFYYcgk5jMmuXzpNf9HRdlcl/AM9/YVx+WuXnJ 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 migration is widely used in kernel, memory compaction, memory hotplug, soft offline page, numa balance, memory demote/promotion, etc, but once access a poisoned source folio when migrating, the kerenl will panic. There is a mechanism in the kernel to recover from uncorrectable memory errors, ARCH_HAS_COPY_MC, which is already used in other core-mm paths, eg, CoW, khugepaged, coredump, ksm copy, see copy_mc_to_{user,kernel}, copy_mc_{user_}highpage callers. In order to support poisoned folio copy recover from migrate folio, we chose to make folio migration tolerant of memory failures and return error for folio migration, because folio migration is no guarantee of success, this could avoid the similar panic shown below. CPU: 1 PID: 88343 Comm: test_softofflin Kdump: loaded Not tainted 6.6.0 pc : copy_page+0x10/0xc0 lr : copy_highpage+0x38/0x50 ... Call trace: copy_page+0x10/0xc0 folio_copy+0x78/0x90 migrate_folio_extra+0x54/0xa0 move_to_new_folio+0xd8/0x1f0 migrate_folio_move+0xb8/0x300 migrate_pages_batch+0x528/0x788 migrate_pages_sync+0x8c/0x258 migrate_pages+0x440/0x528 soft_offline_in_use_page+0x2ec/0x3c0 soft_offline_page+0x238/0x310 soft_offline_page_store+0x6c/0xc0 dev_attr_store+0x20/0x40 sysfs_kf_write+0x4c/0x68 kernfs_fop_write_iter+0x130/0x1c8 new_sync_write+0xa4/0x138 vfs_write+0x238/0x2d8 ksys_write+0x74/0x110 Signed-off-by: Kefeng Wang --- mm/migrate.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index b27c66af385d..60d4e29c5186 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -698,16 +698,25 @@ static int __migrate_folio(struct address_space *mapping, struct folio *dst, struct folio *src, void *src_private, enum migrate_mode mode) { - int rc; + int rc, expected_cnt = folio_expected_refs(mapping, src); - rc = folio_migrate_mapping(mapping, dst, src, 0); - if (rc != MIGRATEPAGE_SUCCESS) + rc = folio_refs_check_and_freeze(mapping, src, expected_cnt); + if (rc) return rc; + rc = folio_mc_copy(dst, src); + if (rc) { + if (mapping) + folio_ref_unfreeze(src, expected_cnt); + return rc; + } + + folio_replace_mapping_and_unfreeze(mapping, dst, src, expected_cnt); + if (src_private) folio_attach_private(dst, folio_detach_private(src)); - folio_migrate_copy(dst, src); + folio_migrate_flags(dst, src); return MIGRATEPAGE_SUCCESS; }