From mboxrd@z Thu Jan 1 00:00:00 1970 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 43D44C3DA41 for ; Wed, 10 Jul 2024 03:26:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AA846B009D; Tue, 9 Jul 2024 23:26:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 959366B009E; Tue, 9 Jul 2024 23:26:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8206B6B009F; Tue, 9 Jul 2024 23:26:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 639536B009D for ; Tue, 9 Jul 2024 23:26:03 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 03748121AC2 for ; Wed, 10 Jul 2024 03:26:02 +0000 (UTC) X-FDA: 82322404206.11.F1B652C Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by imf12.hostedemail.com (Postfix) with ESMTP id 4DBDF4000C for ; Wed, 10 Jul 2024 03:25:58 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf12.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.188 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720581937; a=rsa-sha256; cv=none; b=ETC2WW7tvuH9pUoZaLDJvB/9AbU/iF3IX+XzW2OCGmCCTc943u31LCnRZjHy7gAzk1H2Ug v21lumBZpK7K9rIEFM28vnGU1HMrRB53nf/1E076kAqjrrpeIpoqFV1SQJUuKYrfYq88dh ubmF7EGLtu2eA+3VLyfadRpC+7MEBNw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf12.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.188 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=1720581937; 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=j+sfGXgGTSSIF9NZ7h8bns+yel6PupThicCXVaeXTKI=; b=VrR3TJgKVpGofNHm/Hupal34tB4TBkUvbUN8xMe3jaDktLt3xcSSGph+E0PKfrD3YY1iWJ nss3szmObj5+Am83v+oSJgZItAMPb+E6hSxBcYsFXNNF9t5o/yMCUPhauGuXRnwVmImc0q AlYqRfLyY66KskktImxsPN47pN/duE0= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4WJjrY6Rj1zdgvg; Wed, 10 Jul 2024 11:24:13 +0800 (CST) Received: from kwepemd200019.china.huawei.com (unknown [7.221.188.193]) by mail.maildlp.com (Postfix) with ESMTPS id 2FE6514041B; Wed, 10 Jul 2024 11:25:54 +0800 (CST) Received: from [10.173.127.72] (10.173.127.72) by kwepemd200019.china.huawei.com (7.221.188.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 10 Jul 2024 11:25:53 +0800 Subject: Re: [PATCH RESEND] mm/hugetlb: fix kernel NULL pointer dereference when migrating hugetlb folio To: Hugh Dickins , Andrew Morton CC: , , , References: <20240709120433.4136700-1-linmiaohe@huawei.com> From: Miaohe Lin Message-ID: <070b6acd-e60f-b2c8-18c7-9a9d3806f273@huawei.com> Date: Wed, 10 Jul 2024 11:25:52 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.173.127.72] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To kwepemd200019.china.huawei.com (7.221.188.193) X-Rspamd-Queue-Id: 4DBDF4000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 7gaergxzftgaudhuett9psbxagjk91m4 X-HE-Tag: 1720581958-466444 X-HE-Meta: U2FsdGVkX19fz2dhIoPHnufi7b8gN5i2PbUPlV1gHdnWMIcuznGPQ6Xf6/s0KUCQrYJejggvu9lIwoWyHKMn5JAxAnaLWIHsXlZQoZ9BJH5Uow/h4nBC9FnFmAGSYEWqZR4OLDPrTbibdW5DJv0XC/KwZ6ZovuRCkEOhi9Ha13vkBdFTpoYfabJ33ruPCYg3JjesN0qS3PS9l0G+PL6UWWUFG2sYa3tAHCdHONHBKwItZAWS70lm1ZHnxarBNP6qZvelsCJnFSzZrkHqpUHZ6JTebdr/KcYtKQ0ij4C/hJUR15dszxhGsfEFrMBTJ4kLgTPYROo3MXbr8sEVq+bDCI5eIqgT4QZ7dYIj3XjJjZfIl+0JdLJ8PWHowLubjp8Gq9RFsvSP+CiQwv9/3SD6CPLT6vWXen9Uu1V0VESmyR0co6sxjdT49nIcV1H6+So9aASAzElNmpexLabLUh3BMCJrNqmTA2zoQ4MJUcG9GwNly82sxVPFusIrbYpmvVqacbv6GbIAV69OubK73oolgHY9WT8i3vOxeHzbM+HC/V4IPIZ9YJVcnommnKQNMsbbSnEM2JjWnfOIh/Wh7Yb0DaxNFf+uJEdZHdDQHGqUGjlagR28KD0TiUeopjgUcEcmIw/EQwHDXQ07nJ+fHKfppPTamuen7nY7Y7WTYKknG6tLFTjaz3Rwn87THyb5b/uXLxm4spCrVjpJ//540z75bHJgCMloo6OHYyKDjGK14ENawPiPAEMtv90W75c5qZ7iTMg3xNtsrYiVku3hGWqtbZSu+G1BEmHF99HrOVBh/UEHtl+bnf0tUPs5CEzCmcf/mfickbbJzi5DfrvtiIVFaZ4cpAVVfg2Auj7qfG/ptI4B39R7RnupPDHaA9YyjCycAYvr8oOsVq/GA9A7LSbajUP3P54FLmYdYc2iMJwsV8DrXD0jWQQP+xLsEqPtRevqktWJiep7nHUWUTqlllI VSwtS+kT 9jDTvp6pseAFgn1vU7myDi2WfOdP3osreBP5J+nwY2kfOO1M+ZBubyC5FNc6sOby8tQWpl0kvx1GCcfk34sueN4mNoIhTH41v6ZuQFfZL+qS5PKpoUruxdLdyPKX18Lbqi4bGb+yoHYjYWsDp2i9svvNH71iks+q7ojyo5Tu7CeXVLK6pIsa1TMglk5xlkSKNyLs0xZ4ky5OK5FSEF5+nG8l1x0OUE9UzDoTi43KcUmSpFKqoHh1VjTk7z8agFhsnmtQ5uOCy1YQkISw= 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: On 2024/7/10 8:14, Hugh Dickins wrote: > On Tue, 9 Jul 2024, Miaohe Lin wrote: > >> A kernel crash was observed when migrating hugetlb folio: >> >> BUG: kernel NULL pointer dereference, address: 0000000000000008 >> PGD 0 P4D 0 >> Oops: Oops: 0002 [#1] PREEMPT SMP NOPTI >> CPU: 0 PID: 3435 Comm: bash Not tainted 6.10.0-rc6-00450-g8578ca01f21f #66 >> RIP: 0010:__folio_undo_large_rmappable+0x70/0xb0 >> RSP: 0018:ffffb165c98a7b38 EFLAGS: 00000097 >> RAX: fffffbbc44528090 RBX: 0000000000000000 RCX: 0000000000000000 >> RDX: ffffa30e000a2800 RSI: 0000000000000246 RDI: ffffa3153ffffcc0 >> RBP: fffffbbc44528000 R08: 0000000000002371 R09: ffffffffbe4e5868 >> R10: 0000000000000001 R11: 0000000000000001 R12: ffffa3153ffffcc0 >> R13: fffffbbc44468000 R14: 0000000000000001 R15: 0000000000000001 >> FS: 00007f5b3a716740(0000) GS:ffffa3151fc00000(0000) knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: 0000000000000008 CR3: 000000010959a000 CR4: 00000000000006f0 >> Call Trace: >> >> __folio_migrate_mapping+0x59e/0x950 >> __migrate_folio.constprop.0+0x5f/0x120 >> move_to_new_folio+0xfd/0x250 >> migrate_pages+0x383/0xd70 >> soft_offline_page+0x2ab/0x7f0 >> soft_offline_page_store+0x52/0x90 >> kernfs_fop_write_iter+0x12c/0x1d0 >> vfs_write+0x380/0x540 >> ksys_write+0x64/0xe0 >> do_syscall_64+0xb9/0x1d0 >> entry_SYSCALL_64_after_hwframe+0x77/0x7f >> RIP: 0033:0x7f5b3a514887 >> RSP: 002b:00007ffe138fce68 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 >> RAX: ffffffffffffffda RBX: 000000000000000c RCX: 00007f5b3a514887 >> RDX: 000000000000000c RSI: 0000556ab809ee10 RDI: 0000000000000001 >> RBP: 0000556ab809ee10 R08: 00007f5b3a5d1460 R09: 000000007fffffff >> R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000c >> R13: 00007f5b3a61b780 R14: 00007f5b3a617600 R15: 00007f5b3a616a00 >> >> It's because hugetlb folio is passed to __folio_undo_large_rmappable() >> unexpectedly. large_rmappable flag is imperceptibly set to hugetlb folio >> since commit f6a8dd98a2ce ("hugetlb: convert alloc_buddy_hugetlb_folio to >> use a folio"). Then commit be9581ea8c05 ("mm: fix crashes from deferred >> split racing folio migration") makes folio_migrate_mapping() call >> folio_undo_large_rmappable() triggering the bug. Fix this issue by >> clearing large_rmappable flag for hugetlb folios. They don't need that >> flag set anyway. > > Gosh, thanks a lot for catching this: it had not crossed my mind that > a folio which passes (folio_test_large and) folio_test_large_rmappable > might not be suitable for folio_undo_large_rmappable. > >> >> Fixes: f6a8dd98a2ce ("hugetlb: convert alloc_buddy_hugetlb_folio to use a folio") > > That's in 6.10-rc, isn't it? > >> Fixes: be9581ea8c05 ("mm: fix crashes from deferred split racing folio migration") > > And that's in mm-hotfixes-stable intended for 6.10 final. > >> Signed-off-by: Miaohe Lin >> Cc: > > So if all goes to plan, this shouldn't need the Cc stable. I think you are right. Cc stable should be removed. But this patch has been merged into mm-hotfixes-stable branch, so might Andrew can kindly help modify this? > > I certainly deserve blame for not thinking of this possibility: but how > was it working before my commit, when the folio_undo_large_rmappable() > was being called from mem_cgroup_migrate()? I think that was just as > liable to crash too. I reproduced the crash today with commit be9581ea8c05 ("mm: fix crashes from deferred split racing folio migration") reverted: BUG: kernel NULL pointer dereference, address: 0000000000000008 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page PGD 0 P4D 0 Oops: 0002 [#1] PREEMPT SMP NOPTI CPU: 11 PID: 1481 Comm: bash Not tainted 6.9.0-rc4-00265-gf6a8dd98a2ce-dirty #76 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014 RIP: 0010:folio_undo_large_rmappable+0xa0/0xe0 RSP: 0018:ffffa4104950fbd0 EFLAGS: 00000097 RAX: ffff9e06001c8800 RBX: ffffc8f6614f0090 RCX: 0000000000000001 RDX: 0000000000000000 RSI: 0000000000000286 RDI: ffff9e060b23db98 RBP: ffffc8f6614f0000 R08: 0000000000002453 R09: ffffffffaead2a48 R10: 0000000000000000 R11: 0000000000000001 R12: ffff9e060b23db98 R13: 0000000000000000 R14: 0000000000000000 R15: ffffa4104950fcc8 FS: 00007f6821eb0740(0000) GS:ffff9e0ddfcc0000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000008 CR3: 0000000884502000 CR4: 00000000000006f0 Call Trace: mem_cgroup_migrate+0x186/0x1d0 migrate_folio_extra+0x5c/0x90 move_to_new_folio+0xff/0x250 migrate_pages+0x702/0xd20 soft_offline_page+0x29b/0x7a0 soft_offline_page_store+0x52/0x90 kernfs_fop_write_iter+0x12c/0x1d0 vfs_write+0x387/0x550 ksys_write+0x64/0xe0 do_syscall_64+0xc2/0x1d0 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f6821d14887 RSP: 002b:00007ffeecdb19a8 EFLAGS: 00000246 ORIG_RAX: 0000000000000001 RAX: ffffffffffffffda RBX: 000000000000000c RCX: 00007f6821d14887 RDX: 000000000000000c RSI: 0000563cfd799e10 RDI: 0000000000000001 RBP: 0000563cfd799e10 R08: 00007f6821dd1460 R09: 000000007fffffff R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000c R13: 00007f6821e1b780 R14: 00007f6821e17600 R15: 00007f6821e16a00 This requires memory_hugetlb_accounting is enabled on cgroup2. Or folio_memcg of hugetlb folio will be NULL and thus simply return before calling folio_undo_large_rmappable() in mem_cgroup_migrate(). memory_hugetlb_accounting isnot enable in my test env, so I didn't trigger this bug earlier. So Fixes: be9581ea8c05 ("mm: fix crashes from deferred split racing folio migration") tag might also be removed? > > I would like to hear definitively from Matthew, whether a hugetlb page > should or should not be reported as large_rmappable - is your patch here > just fixing a surprise, or in danger of adding another surprise somewhere? IIUC, large_rmappable is only used for thp. See below code: static inline bool is_transparent_hugepage(const struct folio *folio) { if (!folio_test_large(folio)) return false; return is_huge_zero_folio(folio) || folio_test_large_rmappable(folio); } But I might be miss something. Thanks. .