From patchwork Mon Oct 21 16:48:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roman Gushchin X-Patchwork-Id: 13844457 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 3E225D15DBD for ; Mon, 21 Oct 2024 16:48:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8E496B0085; Mon, 21 Oct 2024 12:48:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B3D966B0088; Mon, 21 Oct 2024 12:48:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A05156B0089; Mon, 21 Oct 2024 12:48:58 -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 81BC96B0085 for ; Mon, 21 Oct 2024 12:48:58 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C46728068C for ; Mon, 21 Oct 2024 16:48:44 +0000 (UTC) X-FDA: 82698193788.29.35E646B Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) by imf05.hostedemail.com (Postfix) with ESMTP id B440910001F for ; Mon, 21 Oct 2024 16:48:27 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uL3ruZF8; spf=pass (imf05.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.181 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729529286; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=VM62Ppuaj9ExI8TZbSZPXIH7o5k8upPR7JSKZv9bTk8=; b=cJx6AttJyCX46jJxP3UMvU0ksjRAD+cfn2zYFVsdHJCHJvFRP2XfSSnWSVa2ZAf1sUIuPi 4sjVLp+sk0c4VXEqKF7vdeTQ9UCjbQ1x2vC6gZY+fYYiJETOsApv8yahH/It63yYiI+mzE h4GYF4NLdKb/dZQB1zXZPy6lpU4j91o= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uL3ruZF8; spf=pass (imf05.hostedemail.com: domain of roman.gushchin@linux.dev designates 95.215.58.181 as permitted sender) smtp.mailfrom=roman.gushchin@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729529286; a=rsa-sha256; cv=none; b=ugUu8X1w64ruO/WOQOQ6kE73t2VNcvy3FJ3ff2jAySfhi9gqBPmlr+Dlw77mWhAf2OeAyO bcdXyXlqugFAH9PfWPLNylTI+wicUZgEECsBk9wMRx942SxXUEsFYBFiMzt2K0LxIL5g54 fFlZFU9rt0eF/qV0uR+WXoRqMN+pdC4= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1729529334; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=VM62Ppuaj9ExI8TZbSZPXIH7o5k8upPR7JSKZv9bTk8=; b=uL3ruZF8Uc9ag0vqVwIbqMohY2DljJYsG9RM8UBAGRKBWdOr+NyfYsKUAmSvoDI1+dLWub Mp+UEO76kTcRiUCWEjmJ/4nLAJd9xpFC1t/3+Oe8icCNL7CNJUNoPSb6w6e2PtUNR3lpyR 86m09p40dBnsXw8qLGNxE8SLdcM8f2I= From: Roman Gushchin To: Andrew Morton Cc: linux-mm@kvack.org, Vlastimil Babka , linux-kernel@vger.kernel.org, Roman Gushchin , stable@vger.kernel.org, Hugh Dickins , Matthew Wilcox Subject: [PATCH] mm: page_alloc: move mlocked flag clearance into free_pages_prepare() Date: Mon, 21 Oct 2024 16:48:36 +0000 Message-ID: <20241021164837.2681358-1-roman.gushchin@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: 64dcc33frftcfkzsuyzobommengajums X-Rspamd-Queue-Id: B440910001F X-Rspamd-Server: rspam11 X-HE-Tag: 1729529307-377283 X-HE-Meta: U2FsdGVkX1+j0P58JYT+lUlFIW/64/6IjCkyNyUSiueQOCJHehvmnhIiv6CNJaklVWTRjbuO/BfS08jOFXKPHnWefR6rvD5rE+N4AWHxw7+20xRfRGPvo9y8HahZtDy7z8HWavYOchvuF76zK+TvyeJmT7VqzkILTeO4rwRf+bh813REM1vzceRPkAnks9bgqSSnmvrdoIbmcr4wXUuOa2bD1zW6Abwa9LVevc+cNjsMSqfVrPpCjiiIUWgI/0wByuI4gVEzsWmuQhDem/c2yv+WHFrVRo/JEwWyx0cu4BIEqNwjnKidF+0k/d8NyfJK15CKTJpfqb79i1YzzTkswY8SLfl+KXfuV78k1XrpiHj+ubUL6brOMDcDByK3nwVTfm+sHZexs1+a0INWprcuM3hnElM6l+YEPZKCBUPnPuYEkhLBVwJCq0pAUHTHTEaTop9wkEDF4OB/7R5GGOk+5n7J3cUDfVkDEV+DviwNrnDLX2Cw9WxUZcnN632qx0v1ISY7iyDSE7mGnfRUW64ye3KzSYfR646lEiA5MAbh53grZ9oRHb+NdDbir2v4worRtKAdf4odaRZLZEPjfopJ4yffYofoXSgrkuZOoXG+FgZ20r4DKHtc6f68wrpzXcyZLvnES3KhkGIa/mZ3HW9IqaOmSr4JGpVLtFf3F2j6A1qWX4N7kUN045EHEAcMPOM4iqTRdSBLBf7DeHSfKaCpUCUruB+nWdlOIc+f2JcoAXyfxY6YlhdcgMM8o0uZL1hM3gfesHBRYErNZjrRgzcnPdHNu2IFZN4q2Kbx4T/jKx1G5aMZmSj3lpIU4c7h6oMTY3ym/1Dz+6cM059SWEiEZjNQaIoWL0Px7F4Cm9/vdTl3Y2XnLoyWsrMvaN1OOEiKkmZ3QFlmfzsIcELJdsdZV6bWlQEV0YB0xEzqVTGIo4vB0JYzpo4Sttug2Q6X2AsKiviZaxYSAXaCzo+w21p zAYGwcME nSPnS3AX35VQleLobwHPrsHUQbFWiLJgwLqF9vkY7NIKIgo+1AuKBezbs4YVFAcGV1lHLWfwY+Wb3Mkr+Mzs/5V1urB+CcFJpxmM58Hh74MGHoFv8VZvLPqGBVFN+bS0fdY+TeCYJh88ltW1ew74e1iJoMUAkfOYs+x6NTX4DhsnoRm/63ZPNK/IJPQ3PoQB7r2SP7VEq36xR5so5b7wRENyC9vj0LuXvce94b67S/5GdXOxG9Z6k767sWBVz3EjMkOzaxOzIOuQmh4V9i6rV1sy4jb1dkpFyYacz60dkStKmKJ/fGaN6pBkxkQ9iUGs6Ve7K0161QXw+VNWp1TyaND5h1lbGCCe5PBQs 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: Syzbot reported [1] a bad page state problem caused by a page being freed using free_page() still having a mlocked flag at free_pages_prepare() stage: BUG: Bad page state in process syz.0.15 pfn:1137bb page: refcount:0 mapcount:0 mapping:0000000000000000 index:0xffff8881137bb870 pfn:0x1137bb flags: 0x400000000080000(mlocked|node=0|zone=1) raw: 0400000000080000 0000000000000000 dead000000000122 0000000000000000 raw: ffff8881137bb870 0000000000000000 00000000ffffffff 0000000000000000 page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) set page_owner tracks the page as allocated page last allocated via order 0, migratetype Unmovable, gfp_mask 0x400dc0(GFP_KERNEL_ACCOUNT|__GFP_ZERO), pid 3005, tgid 3004 (syz.0.15), ts 61546 608067, free_ts 61390082085 set_page_owner include/linux/page_owner.h:32 [inline] post_alloc_hook+0x1f3/0x230 mm/page_alloc.c:1537 prep_new_page mm/page_alloc.c:1545 [inline] get_page_from_freelist+0x3008/0x31f0 mm/page_alloc.c:3457 __alloc_pages_noprof+0x292/0x7b0 mm/page_alloc.c:4733 alloc_pages_mpol_noprof+0x3e8/0x630 mm/mempolicy.c:2265 kvm_coalesced_mmio_init+0x1f/0xf0 virt/kvm/coalesced_mmio.c:99 kvm_create_vm virt/kvm/kvm_main.c:1235 [inline] kvm_dev_ioctl_create_vm virt/kvm/kvm_main.c:5500 [inline] kvm_dev_ioctl+0x13bb/0x2320 virt/kvm/kvm_main.c:5542 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:907 [inline] __se_sys_ioctl+0xf9/0x170 fs/ioctl.c:893 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0x69/0x110 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x76/0x7e page last free pid 951 tgid 951 stack trace: reset_page_owner include/linux/page_owner.h:25 [inline] free_pages_prepare mm/page_alloc.c:1108 [inline] free_unref_page+0xcb1/0xf00 mm/page_alloc.c:2638 vfree+0x181/0x2e0 mm/vmalloc.c:3361 delayed_vfree_work+0x56/0x80 mm/vmalloc.c:3282 process_one_work kernel/workqueue.c:3229 [inline] process_scheduled_works+0xa5c/0x17a0 kernel/workqueue.c:3310 worker_thread+0xa2b/0xf70 kernel/workqueue.c:3391 kthread+0x2df/0x370 kernel/kthread.c:389 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 The problem was originally introduced by commit b109b87050df ("mm/munlock: replace clear_page_mlock() by final clearance"): it was handling focused on handling pagecache and anonymous memory and wasn't suitable for lower level get_page()/free_page() API's used for example by KVM, as with this reproducer. Fix it by moving the mlocked flag clearance down to free_page_prepare(). The bug itself if fairly old and harmless (aside from generating these warnings), so the stable backport is likely not justified. Closes: https://syzkaller.appspot.com/x/report.txt?x=169a47d0580000 Fixes: b109b87050df ("mm/munlock: replace clear_page_mlock() by final clearance") Signed-off-by: Roman Gushchin Cc: Cc: Hugh Dickins Cc: Matthew Wilcox --- mm/page_alloc.c | 9 +++++++++ mm/swap.c | 14 -------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index bc55d39eb372..24200651ad92 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1044,6 +1044,7 @@ __always_inline bool free_pages_prepare(struct page *page, bool skip_kasan_poison = should_skip_kasan_poison(page); bool init = want_init_on_free(); bool compound = PageCompound(page); + struct folio *folio = page_folio(page); VM_BUG_ON_PAGE(PageTail(page), page); @@ -1053,6 +1054,14 @@ __always_inline bool free_pages_prepare(struct page *page, if (memcg_kmem_online() && PageMemcgKmem(page)) __memcg_kmem_uncharge_page(page, order); + if (unlikely(folio_test_mlocked(folio))) { + long nr_pages = folio_nr_pages(folio); + + __folio_clear_mlocked(folio); + zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages); + count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages); + } + if (unlikely(PageHWPoison(page)) && !order) { /* Do not let hwpoison pages hit pcplists/buddy */ reset_page_owner(page, order); diff --git a/mm/swap.c b/mm/swap.c index 835bdf324b76..7cd0f4719423 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -78,20 +78,6 @@ static void __page_cache_release(struct folio *folio, struct lruvec **lruvecp, lruvec_del_folio(*lruvecp, folio); __folio_clear_lru_flags(folio); } - - /* - * In rare cases, when truncation or holepunching raced with - * munlock after VM_LOCKED was cleared, Mlocked may still be - * found set here. This does not indicate a problem, unless - * "unevictable_pgs_cleared" appears worryingly large. - */ - if (unlikely(folio_test_mlocked(folio))) { - long nr_pages = folio_nr_pages(folio); - - __folio_clear_mlocked(folio); - zone_stat_mod_folio(folio, NR_MLOCK, -nr_pages); - count_vm_events(UNEVICTABLE_PGCLEARED, nr_pages); - } } /*