From patchwork Sun Apr 7 08:54:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 13620081 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 EE373CD11C2 for ; Sun, 7 Apr 2024 08:57:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC8EB6B0082; Sun, 7 Apr 2024 04:57:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C7A266B0083; Sun, 7 Apr 2024 04:57:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B67FB6B0085; Sun, 7 Apr 2024 04:57:10 -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 98C2E6B0082 for ; Sun, 7 Apr 2024 04:57:10 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 424C71A0652 for ; Sun, 7 Apr 2024 08:57:10 +0000 (UTC) X-FDA: 81982131420.05.80E9704 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf01.hostedemail.com (Postfix) with ESMTP id 1132540010 for ; Sun, 7 Apr 2024 08:57:06 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712480228; 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: references; bh=cB6Hy8DizqVTxnTNR4cLeLfpWOtWlZZFePJrkgfI+H4=; b=1Hl7cNbAMmisBUBpQxrp51xFaSc+Fzrc6dpUEQtpaPtobyxUjiVcTDPoN0zZIPUl78YsMR nv1NLuZAWzM/gJs83dRvm1aphi9ZL0YQM7Is0ZVKlPV749cysidhlbed0WrnCrnlNe/Avw tMtKbP5pXmaVPVEUaUZ2MvNq9WWfikI= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf01.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712480228; a=rsa-sha256; cv=none; b=Ht28Zqkw1erlLUKfzkDjE8zA/J8uHslYIEvjy9I6gwreNe8WwI9RaOpaqvmH9Bk765Dpzn KwzzVbcyv+Yd4XGFc9ofCKpEOq2/eGiDTK5iWS/nd+I3B7kPDXjMebGzAMbdoutuslmUYD 5sMdmA3kAo+/EMGVfk+URxUaqJC2+NY= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4VC5cj4VFFz29khB; Sun, 7 Apr 2024 16:54:13 +0800 (CST) Received: from canpemm500002.china.huawei.com (unknown [7.192.104.244]) by mail.maildlp.com (Postfix) with ESMTPS id CE4E71400D3; Sun, 7 Apr 2024 16:57:02 +0800 (CST) Received: from huawei.com (10.173.135.154) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Sun, 7 Apr 2024 16:57:02 +0800 From: Miaohe Lin To: CC: , , , Subject: [PATCH] mm/memory-failure: fix deadlock when hugetlb_optimize_vmemmap is enabled Date: Sun, 7 Apr 2024 16:54:56 +0800 Message-ID: <20240407085456.2798193-1-linmiaohe@huawei.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-Originating-IP: [10.173.135.154] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500002.china.huawei.com (7.192.104.244) X-Rspam-User: X-Stat-Signature: 5xnsanf55emsg9e7noyxe3q8rroy8h5q X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 1132540010 X-HE-Tag: 1712480226-459235 X-HE-Meta: U2FsdGVkX19PmiQ7yQM8XPx9mQk3cCnRJKbVuXhYWofSwSJcq024m5JMNJMgndVfdQxT/rFXGVfmo7sS3vBxhbRdcqhubY+CMfpIrEMh/h5W11nuAzMPrf2LLXXAfNZYBWD22EhMLel7+zOzuvEFRX74WnRq5PNG7ut6vOBxBUXfgx1aKikRh+BFRCug5lGydrNE0cfxFvYiyh2u6ZE2JVARApPGPKDJnvG+gdZjSuA3obn1DZVXMzGPyC6wHUsX/NbgBeubgCQ38XGNfHi7Kl6NwSGFM/dWRaKAGGtoAHmBjUrDXjne7HsZWgGNJEkckA61p7YcdocPVK2DiAvA+BVPSsIugbn0ZQK6uqdlh4xUbG5ehVNxJQqYZ9vPplYCdWtTS07S+jpURlh1CmeXInkab4eqa31HisN9reXjG8Rw5IUXVVQrph0WxCVx2htYLamxVlSdQnCvD/SjeiNuU+dMjbYjP+iInPGBvAp7usS68E9p6SUWKRLogxA/FV5CGRMumUiyXoIP9oy8ExjYEiuGcP1uxL74UAwvcN/PgFemr5EDHgZxpiD8pxXJ9Z+32OShkzqlEi6rnB3knr2jkxk0a940Fuextd4+U2nogR7C/i6UA/E5sFJsq6P9QZCdu5xOiBYCKXKpdtBNSS8RffjeinZ0PEXF7C7/0byS6KSGvvoKyhuPOnVdi8Kfa4Wmc44KCyzxJ2lWlW8lKofysiYcfWlbASnX8lwqRmUg0oK5wi4mLSzdkI0roZHs6L4HpgKfkqBMN1Bi5lll/4G5Yy9aoD+Empntj7V4UjYunfCUVpuYbVYXFjt8QHn1qGSlE6/YY8FsMo7a+veWVBAWL2dYe+K4ahAhk0DAgfDtD9TvTt1KkW0UTqYI55N87CMgTwXKwBtplHaPZZafUdiTNiXUrFiNtgfBV8pIlivQL8FIHWeC0lz7E3h2puafEyqctyPcx7ET21Gj0IxsxIi eaxP3Dag /UkMBi9LOQE2FTfPmoBfL5fFHoiATgWc4MbX2m0VkYh/HIQl2Z6DMCX2GkK8Unwn3XufFBQ1r/u7YAkcvz2nn40BLGORBGK7391HRMxdZ24eVjBX8xTOlZ32y5xUn17fi1wdEz+uyllkR6kKa3Qs1ricb9vaeaAhF3twWquMgdi+S0+t/TQ8wPkG+ldRdXTWqFocpnU29nimP49IiIhoFrL9P57Yp3ElUF1DbSQruEXCgsC1hpwRs2/kQtg== 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: When I did hard offline test with hugetlb pages, below deadlock occurs: ====================================================== WARNING: possible circular locking dependency detected 6.8.0-11409-gf6cef5f8c37f #1 Not tainted ------------------------------------------------------ bash/46904 is trying to acquire lock: ffffffffabe68910 (cpu_hotplug_lock){++++}-{0:0}, at: static_key_slow_dec+0x16/0x60 but task is already holding lock: ffffffffabf92ea8 (pcp_batch_high_lock){+.+.}-{3:3}, at: zone_pcp_disable+0x16/0x40 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (pcp_batch_high_lock){+.+.}-{3:3}: __mutex_lock+0x6c/0x770 page_alloc_cpu_online+0x3c/0x70 cpuhp_invoke_callback+0x397/0x5f0 __cpuhp_invoke_callback_range+0x71/0xe0 _cpu_up+0xeb/0x210 cpu_up+0x91/0xe0 cpuhp_bringup_mask+0x49/0xb0 bringup_nonboot_cpus+0xb7/0xe0 smp_init+0x25/0xa0 kernel_init_freeable+0x15f/0x3e0 kernel_init+0x15/0x1b0 ret_from_fork+0x2f/0x50 ret_from_fork_asm+0x1a/0x30 -> #0 (cpu_hotplug_lock){++++}-{0:0}: __lock_acquire+0x1298/0x1cd0 lock_acquire+0xc0/0x2b0 cpus_read_lock+0x2a/0xc0 static_key_slow_dec+0x16/0x60 __hugetlb_vmemmap_restore_folio+0x1b9/0x200 dissolve_free_huge_page+0x211/0x260 __page_handle_poison+0x45/0xc0 memory_failure+0x65e/0xc70 hard_offline_page_store+0x55/0xa0 kernfs_fop_write_iter+0x12c/0x1d0 vfs_write+0x387/0x550 ksys_write+0x64/0xe0 do_syscall_64+0xca/0x1e0 entry_SYSCALL_64_after_hwframe+0x6d/0x75 other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(pcp_batch_high_lock); lock(cpu_hotplug_lock); lock(pcp_batch_high_lock); rlock(cpu_hotplug_lock); *** DEADLOCK *** 5 locks held by bash/46904: #0: ffff98f6c3bb23f0 (sb_writers#5){.+.+}-{0:0}, at: ksys_write+0x64/0xe0 #1: ffff98f6c328e488 (&of->mutex){+.+.}-{3:3}, at: kernfs_fop_write_iter+0xf8/0x1d0 #2: ffff98ef83b31890 (kn->active#113){.+.+}-{0:0}, at: kernfs_fop_write_iter+0x100/0x1d0 #3: ffffffffabf9db48 (mf_mutex){+.+.}-{3:3}, at: memory_failure+0x44/0xc70 #4: ffffffffabf92ea8 (pcp_batch_high_lock){+.+.}-{3:3}, at: zone_pcp_disable+0x16/0x40 stack backtrace: CPU: 10 PID: 46904 Comm: bash Kdump: loaded Not tainted 6.8.0-11409-gf6cef5f8c37f #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014 Call Trace: dump_stack_lvl+0x68/0xa0 check_noncircular+0x129/0x140 __lock_acquire+0x1298/0x1cd0 lock_acquire+0xc0/0x2b0 cpus_read_lock+0x2a/0xc0 static_key_slow_dec+0x16/0x60 __hugetlb_vmemmap_restore_folio+0x1b9/0x200 dissolve_free_huge_page+0x211/0x260 __page_handle_poison+0x45/0xc0 memory_failure+0x65e/0xc70 hard_offline_page_store+0x55/0xa0 kernfs_fop_write_iter+0x12c/0x1d0 vfs_write+0x387/0x550 ksys_write+0x64/0xe0 do_syscall_64+0xca/0x1e0 entry_SYSCALL_64_after_hwframe+0x6d/0x75 RIP: 0033:0x7fc862314887 Code: 10 00 f7 d8 64 89 02 48 c7 c0 ff ff ff ff eb b7 0f 1f 00 f3 0f 1e fa 64 8b 04 25 18 00 00 00 85 c0 75 10 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 51 c3 48 83 ec 28 48 89 54 24 18 48 89 74 24 RSP: 002b:00007fff19311268 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 RAX: ffffffffffffffda RBX: 000000000000000c RCX: 00007fc862314887 RDX: 000000000000000c RSI: 000056405645fe10 RDI: 0000000000000001 RBP: 000056405645fe10 R08: 00007fc8623d1460 R09: 000000007fffffff R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000c R13: 00007fc86241b780 R14: 00007fc862417600 R15: 00007fc862416a00 In short, below scene breaks the lock dependency chain: memory_failure __page_handle_poison zone_pcp_disable -- lock(pcp_batch_high_lock) dissolve_free_huge_page __hugetlb_vmemmap_restore_folio static_key_slow_dec cpus_read_lock -- rlock(cpu_hotplug_lock) Fix this by calling drain_all_pages() instead. Signed-off-by: Miaohe Lin Acked-by: Oscar Salvador --- mm/memory-failure.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index edd6e114462f..88359a185c5f 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -153,11 +153,17 @@ static int __page_handle_poison(struct page *page) { int ret; - zone_pcp_disable(page_zone(page)); + /* + * zone_pcp_disable() can't be used here. It will hold pcp_batch_high_lock and + * dissolve_free_huge_page() might hold cpu_hotplug_lock via static_key_slow_dec() + * when hugetlb vmemmap optimization is enabled. This will break current lock + * dependency chain and leads to deadlock. + */ ret = dissolve_free_huge_page(page); - if (!ret) + if (!ret) { + drain_all_pages(page_zone(page)); ret = take_page_off_buddy(page); - zone_pcp_enable(page_zone(page)); + } return ret; }