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 5A7CCC5AD49 for ; Wed, 28 May 2025 12:13:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B8366B0089; Wed, 28 May 2025 08:13:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 869676B008A; Wed, 28 May 2025 08:13:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77DA76B008C; Wed, 28 May 2025 08:13:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 59A556B0089 for ; Wed, 28 May 2025 08:13:26 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A482912093C for ; Wed, 28 May 2025 12:13:25 +0000 (UTC) X-FDA: 83492206770.14.14932CA Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf20.hostedemail.com (Postfix) with ESMTP id 485A01C000A for ; Wed, 28 May 2025 12:13:21 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748434404; a=rsa-sha256; cv=none; b=xetrWxxaT4Ojr7woQTtOLOEt3AjHLXGRJGhL0nLWZ79CBby1qaS1A3fDuFu4QIEtfMZut0 2PIEYWyrHeK0C7R9Jm5KciOZ/xOT21Bp9G/t5QyUEF2nRPttiudVwVruJrIDuKEZ87w3wp WC7F8GMjf15k+//mVpcqkjplkCnBPl4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=tujinjiang@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=1748434404; 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=AOPSwY1PT0wglf7dwFfNbC3vtuyZfmxYbvu8VYu1+MY=; b=YS6AIZ5/Y/9epJif8ycitOeU+dz2L1qqgomshiMrxoZLmfLTwlILoLlSVyOLiHYdABzML+ tkDBLgJp5j7orCNUTMhi05sJwAavMnGaexohcXgmgiShzrYtfmrcdk2uwRBerRrISmGJL1 gnweAHPUKUsfDj7x6Y/+NYVMgkZv00w= Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4b6pFK0f9SzvWqZ; Wed, 28 May 2025 20:08:53 +0800 (CST) Received: from kwepemo200002.china.huawei.com (unknown [7.202.195.209]) by mail.maildlp.com (Postfix) with ESMTPS id E0AA5180B61; Wed, 28 May 2025 20:13:17 +0800 (CST) Received: from [10.174.179.13] (10.174.179.13) by kwepemo200002.china.huawei.com (7.202.195.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 28 May 2025 20:13:17 +0800 Message-ID: Date: Wed, 28 May 2025 20:13:16 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH] mm: fix COW mapping handing in generic_access_phys To: David Hildenbrand , , , , , , , CC: , , Peter Xu References: <20250528015617.302681-1-tujinjiang@huawei.com> <0d4f0180-52e6-47c9-b141-54e7e7c86880@redhat.com> From: Jinjiang Tu In-Reply-To: <0d4f0180-52e6-47c9-b141-54e7e7c86880@redhat.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.179.13] X-ClientProxiedBy: kwepems100002.china.huawei.com (7.221.188.206) To kwepemo200002.china.huawei.com (7.202.195.209) X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 485A01C000A X-Stat-Signature: dffm9m58awgkgpeqnfu7wwd7ss3s4jqg X-Rspam-User: X-HE-Tag: 1748434401-4561 X-HE-Meta: U2FsdGVkX1/t5c4PU49Z1gEVhs2GxehJtNHwSTphvEYA1yl6uHNO1YOJpPJqkUy0x6P/4g6hbf3LL1FeBhbT5xh6sjCdyUzBFPfDFQJdh85qhCdYelvcKL2Msz929rGtkRMgsIne19nA/qVvwKPjobQhUqh9bGA0YUoo4X+vP0Ufu9gdU9JECWx22UVgHBmSEe0xdyLzjGgzHwTtSDx+GXOovflEr2UGpPCxX4h0SMdtPuezIx5nxrIIQcyhFbdTkQIm36a/b3NkEvKSzgAiGTvGu4ynvoEB53i1BE6lye/Ixh6S1WjQWsuLaZGLVrGQCh8Tf4nwKLwsrcZOdCLSj7iAGjLQp4s+dMKqevW7GiYJv+FuhpJw1kRPXKsXcRV6yP4KCflI+JvIboob5hcM6Y/SBe9JL366//XXevB1fsZ14IRcDPCOfc8vAl5kYRiXoIGqoy7or02+ESO7VjZsH+ptb4dSFoh/Eeze9NEOpxkhgGq+WkzE4Gnz38RsaU6vRttRnIIhLcma6MYoHOv1rZsZANzDy0aoXCiyBLasTWa2Pd2CDu9WdpuzztJYB6/SY+zVxr3qL4Axm0387GMjMVDjG9i1FOxIqrkDmUVC7mpnm5O5ZjYkDZDG9Clvd5FMC8aMCVrdrjMP2GB8x1eodQmzr6bNJ1VqJEyuilq2eVQDxnNK/oSlJKQ3IzmV2eYjxx/BP+rnOd+udTqf9GiJqszsv4LdKtGg8e/rB8o+CpuRW2kSInJ8+eW/WqieiLyJbcoQJ8R3QTszE95oNTPUmN43N9eYkWtUU9PsLBaZ5sLwYxd8B/l3HvS6Hkv9IRSwyyAlfNNvHGZUX1eYreeCPZ4t3CBJ2LDHbS+lC7rmSYJfTZ2Af3KjTlHCMsp4BHKfRFDbuYOKKj3bSCP9HKRGG1jzuT6V+P1YKCustPnPWucoJDGTcwivm/AgaVuNbzdwCd7bCIob8+CeBvtW7G8 D+k5tKu7 d7aBB24BjvRYvsKMCfpYeAovr2no7Ylb3PsyVHSKOetLZoFtG3i9jRrTRFifwEuMPxzJ58wua8V+bx02UJcDM1U41p15tiwLLGJQlwSsIdUuIF86REw3hXakf7wC/McZYx0NuIk6+l4xz+ERaspxYmqyM1JL3+g6xmrZ7KVukDBzuII5vp+9KYDxtxWqYMSGyln8B7fOUg2lbJFkZbahRlVTOOPajOtLtEGQ3djBJ2cO6+ePPZyCn2zXv4VbSEz7+zFclspLdxj4X5p2/ncJEnQBwlAA+Ko+oxTVEhHIsnzoP2qKV1aYtCdcZqokhOd7/DZ7Q5p4qf3x2ekE= 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: 在 2025/5/28 16:59, David Hildenbrand 写道: > On 28.05.25 03:56, Jinjiang Tu wrote: >> Syzkaller reports a below BUG: >>   ioremap on RAM at 0x0000000022727000 - 0x0000000022727fff >>   WARNING: CPU: 3 PID: 3609 at arch/x86/mm/ioremap.c:216 >> __ioremap_caller+0x644/0x7f0 arch/x86/mm/ioremap.c:216 >>   Modules linked in: >>   CPU: 3 PID: 3609 Comm: syz.2.577 Not tainted 6.6.0+ #63 >>   Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS >> rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014 >>   RIP: 0010:__ioremap_caller+0x644/0x7f0 arch/x86/mm/ioremap.c:216 >>   Call Trace: >>    >>    generic_access_phys+0x241/0x480 mm/memory.c:6458 >>    __access_remote_vm+0x6af/0x970 mm/memory.c:6535 >>    access_process_vm+0x53/0x80 mm/memory.c:6600 >>    get_cmdline+0x192/0x380 mm/util.c:1041 >>    audit_log_proctitle kernel/auditsc.c:1620 [inline] >>    audit_log_exit+0x1424/0x18c0 kernel/auditsc.c:1811 >>    __audit_syscall_exit+0x252/0x2f0 kernel/auditsc.c:2079 >>    audit_syscall_exit include/linux/audit.h:356 [inline] >>    syscall_exit_work+0x10f/0x130 kernel/entry/common.c:166 >>    __syscall_exit_to_user_mode_work kernel/entry/common.c:205 [inline] >>    syscall_exit_to_user_mode+0x10/0x1e0 kernel/entry/common.c:218 >>    do_syscall_64+0x66/0x110 arch/x86/entry/common.c:87 >>    entry_SYSCALL_64_after_hwframe+0x78/0xe2 >> >> The /dev/mem is mapped with COW mapping, and mremap at the >> mm->args_start. >> The special pfn mapping is replaced by anon folios due to COW. >> generic_access_phys() is supposed to handle iomem, instead of RAM pfn, >> thus trigger a WARN_ON. >> >> Similar to commit 04c35ab3bdae ("x86/mm/pat: fix VM_PAT handling in >> COW mappings"). check if the pte is special to reject Cowed anon folios. >> >> Signed-off-by: Jinjiang Tu >> --- >>   mm/memory.c | 7 +++++++ >>   1 file changed, 7 insertions(+) >> >> diff --git a/mm/memory.c b/mm/memory.c >> index 49199410805c..e1dac84536ee 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -6840,6 +6840,13 @@ int generic_access_phys(struct vm_area_struct >> *vma, unsigned long addr, >>   retry: >>       if (follow_pfnmap_start(&args)) >>           return -EINVAL; >> + >> +    /* Never return PFNs of anon folios in COW mappings. */ >> +    if (!args.special) { >> +        follow_pfnmap_end(&args); >> +        return -EINVAL; >> +    } >> + >>       prot = args.pgprot; >>       phys_addr = (resource_size_t)args.pfn << PAGE_SHIFT; >>       writable = args.writable; > > I assume we trigger this through vma->vm_ops->access, when the vm_ops > have generic_access_phys set. > > I still dislike exposing the "special" bit here, as it is absolutely > not what we should care about in the caller. > > In case our arch does not support pte_special, you fix will not catch > that case ... Yes,  I reference to the check in follow_phys(), but forgot follow_phys() is only defined in x86, which supports pte special. > > The following might be better: > > diff --git a/mm/memory.c b/mm/memory.c > index 37d8738f5e12e..810adb8d1a53b 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -6681,6 +6681,14 @@ int generic_access_phys(struct vm_area_struct > *vma, unsigned long addr, >         prot = args.pgprot; >         phys_addr = (resource_size_t)args.pfn << PAGE_SHIFT; >         writable = args.writable; > + > +       /* Refuse (refcounted) anonymous pages in CoW mappings. */ > +       if (is_cow_mapping(vma->vm_flags) && > +           vm_normal_page(vma, addr, ptep_get(args.ptep))) { > +               follow_pfnmap_end(&args); > +               return -EINVAL; > +       } > + >         follow_pfnmap_end(&args); > >         if ((write & FOLL_WRITE) && !writable) > >