From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794280959; b=z6E9gwDHuOgBsfTzFyaLzle27X64BDYI3dIRgwG6OyOfEMYS8j99Usj9vwmYmInlU4hJ W57OF7KuoDJF0DEmf5SNiC8+txSjqFMjt0DLPFvjgdkBcFQJzjKtqnffqU9gsZSIYnbJt hkipLcmEKW8LjnOKPp/ORWAWZvFYRuCE55dOPQrzQNH//i4zu2wfCO+PxOggsT0pIKSt0 zIZtKpZiJul8xYAqgLolvX6hZS9wJcrk9ud/2B0jsI8Vi3jgPOHoAOFDCubCEDx4lBCFt bLzcEMORtJVVz7+TbW+9vwCny1dFVeh4nTfY6w3rFfiNW7Me+9oKVTOf9pwFLdjqBBA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794280959; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:In-Reply-To:Date:Message-ID:References: MIME-version:Content-type:Content-Transfer-Encoding; bh=W/wp6KJO63iMMRJlz6HcbO/i7czGvSk/CyVD5tv8Mv8=; b=vLU5We7XehURNJ5V7ThetFwQBXrQhresEtMmF7YyoQc/0YZ0N7ezibk0/BxgE/XM2N6+ HcLsLbl61pxue4OXgbXIGTp4S3YkwToIAMzVDJaTdjj5QGPG/jSqfiZQhaWcWQB+E/QSN P7VbBlFgPDNNpNV7zTRpGJ6tq32cNVz1uI78YSny0Yp9FQsyQ86M5Q5U8lVsbKTnc9Vsz OOtNWg3ReSqTNNT3Wl9n5JqZRLPtvTd+VWV3DeLW5VfMlzDVeKorrAmLKn0Dz52fErI2k WwYKL0YU4JHcWvVCQBqv66wgOOTrLq73zFptH8u6RIYDjn2VqGTdCTsH6XilUBhTCMQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kAzOutwB; arc=none smtp.remote-ip=74.125.82.174 Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C2F260A260E for ; Tue, 10 Nov 2026 03:22:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2C2F260A260E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kAzOutwB Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-3700ed8dbe3so2105404eec.0 for ; Mon, 09 Nov 2026 19:22:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794280957; x=1794885757; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :message-id:date:in-reply-to:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=W/wp6KJO63iMMRJlz6HcbO/i7czGvSk/CyVD5tv8Mv8=; b=kAzOutwBbsGWqT4Ps+ofgIDVt+iyD96+XCm0s0L4jv9uqf18bCIviyx6+M9zbsc6SI 8X1ZjCWVGQD+HlZX/izasG92PKp2gGRL+1m9xZ4/G2DA1KaEny9dB3RgvRPM/yBrNVnn Nbm9ydQ8fVSb0oH050aZ7CrqvCGABRiLVmwRuQw7PkzLUVj5O7FMJuM7IfuK7Kc6WOYV iICSs1lxRXims8EHylU+gVJzg7KJi7MiLC+u/UEOLc6/bRaWi3EAajKLcL2don8+2QZZ LLNTTlKFy3l7+4tWgUsGSpbFNfjC/rBLsJC8m3Cb/J6wSQWUa12BPqVwIu/9oE1JUIqX C3Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794280957; x=1794885757; h=content-transfer-encoding:content-type:mime-version:references :message-id:date:in-reply-to:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=W/wp6KJO63iMMRJlz6HcbO/i7czGvSk/CyVD5tv8Mv8=; b=Kj+CjGLp+iRaKr1xnq3zAm1T0Cp/W/j4Rnf8F2r93rxYbh90zYsxPb/sFr+WA0Tg6Q e7Xdp+ys2WeMRAaOT2IWmkUQamZG8Vq44d45+RNXklutDdPp0qOAql/Fmwq2bO58oWya 2KcSCIEwoIRar2LPOCOyGztiRYzdkkkhikMSwYuBEx4nNHdY/6WBX0Qxn163+nI4YcCE YsakKy42LNLIfD4yETKSHhXMgW+csvk5jFEXk/wxdUiegjrxaVaoltTJ2tJXfAakjLzb 2jcUjOrV/Hi9uCwvcGHUg5IjQ4V94PUx66MGNNdfAcj41UW4E/18XMaHC1qv5QMIm13z lK5w== X-Forwarded-Encrypted: i=1; AL+hErFmoQDDiyW3nQCrKjgLqW6EzT3l9boRRjSj3lMFs+N/BTdDfj5d2VYvi522F6hTQ7glz0ab76KASy49MCQH8zQ=@vger.kernel.org X-Gm-Message-State: APuU4Z+nhH2FdIGHRk6Kn449Zk/1cr7V0CpD+W015ecMghL3i2zhFKEd /uIjmcQKNqnx7IO0PdHhXREBqSKdoa5HNyV+OJjWb3Knbzuw6guAMxazd6u0FA== X-Gm-Gg: AYBFou3h2OFKK5Prk4CgeWbAVBL3H9MRhSIkGb5vWWVqlhWsIG3qtF4sakdg/Ya3H0F Ka2XxJgeW54BNRvOtHoAhVOyfOsrAr8YCFUctAQVMI2VL3AxT7Mxgi0JS9Fm96lI4oAq/MwVGD5 xTk8pktnBo/pVzy/KiChAJJtMvMqE1RkAN5lF63JyaDZVlTRFtL97N6xT07Wsb2KoK5w/zwA2dK 7fe7BrE5dp5SxYSzcIytegp6/NjboeYzl2JSBwAJRlm5oixxuC3dtHuCE7OKrSpixo/RXznCIfe rpZRH3NoEfT/ug0sw5/j0iq8mQGiPUwRlBQ+KaT2aLZqYg5mU1mychZ1p43H/+1RsV9zd3eADnr 393Ga/K8s1k3wZdF9IvvjZ9JLrQUURtDc3HJnjmDc87RJ+ehNcOBFZQ+xyR7lRsa72ekL8zsdD2 ZDx9cFb63CF02MKQzo9IqDIV1ihiuJprYKwYIn+EmA+0wcGWIHLlZ7Xo0tr+v8u7rMaaHAe5gWr CSah3owBme99t1yc2EUXcCQw4DCchx2vDkJdDPfkh8owRaql0KQeSORM6bva2GQOUbZXjE0XRRO 6W6bO8JvmVb92df1PnG2+0POG9QO X-Received: by 2002:a05:7300:3b03:b0:366:2893:7421 with SMTP id 5a478bee46e88-3751e6acd4cmr2378114eec.10.1794280957102; Mon, 09 Nov 2026 19:22:37 -0800 (PST) Received: from pve-server ([49.205.216.49]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-37520bf3e18sm4159008eec.16.2026.11.09.19.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Nov 2026 19:22:36 -0800 (PST) From: Ritesh Harjani (IBM) To: Konstantin Khorenko , Andrew Morton , "David Hildenbrand (Arm)" , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Peter Xu Cc: Alex Williamson , kvm@vger.kernel.org, =?utf-8?Q?Lo?= =?utf-8?Q?=C3=AFc?= Molinari , Lorenzo Stoakes , Shuah Khan , linux-kselftest@vger.kernel.org, dri-devel@lists.freedesktop.org, Madhavan Srinivasan , linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Konstantin Khorenko , Jason Gunthorpe , stable@vger.kernel.org, Sashiko Subject: Re: [PATCH v5 3/4] mm/huge_memory: read a special PMD under the lock in copy_huge_pmd() In-Reply-To: <20261104-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v5-3-c56d0d26f970@virtuozzo.com> Date: Tue, 10 Nov 2026 08:30:44 +0530 Message-ID: References: <20261104-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v5-0-c56d0d26f970@virtuozzo.com> <20261104-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v5-3-c56d0d26f970@virtuozzo.com> X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-version: 1.0 Content-type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Konstantin Khorenko writes: > copy_huge_pmd() reads a special PMD without the page table lock and > copies that value to the child. The mmap write lock held by fork does > not stop unmap_mapping_range(): it zaps under i_mmap_rwsem and the PMD > lock. > > The zap visits the parent VMA before the child's, as dup_mmap() links > the child right after the parent in i_mmap. If it clears the parent PMD > after fork has read it, and passes the child before fork copies it, the > child keeps a mapping the driver has revoked, for example: > > - vfio-pci: the child reaches the BAR after the device memory was > disabled or the device was reset, instead of getting SIGBUS; > - panthor: the child maps the pages of an evicted buffer object after > the driver has released them. > > The same branch deposits no page table under the copy. ppc64 in hash > mode keeps the hash slot array of every huge PMD in that table, so the > child dereferences a NULL table on the first hash fault, in > __hash_page_thp(), and on the zap, in hash__pmdp_huge_get_and_clear(). > > Take the locks first and let vm_normal_page_pmd() tell a PFN mapping > from a folio under them, as copy_present_ptes() does with > vm_normal_page(). The zap clears the parent PMD under the same lock, so > either fork finds it none and skips it, or the copy is in place before > the zap reaches the child. > > The copy of a PFN mapping then goes through the same tail as a folio: > > - it gets a page table where the architecture needs one; > - in a shared mapping it is marked clean in the child, as a PTE is; > - in a CoW mapping, which only VM_MIXEDMAP allows, both sides are write > protected instead of a warning being printed. > > Fixes: bc02afbd4d73 ("mm/fork: accept huge pfnmap entries") > Fixes: d1503aa9ab80 ("powerpc/64s: Add support for huge pfnmaps") Thanks Konstantin, for fixing this. Without this patch I can reproduce the problem on Hash on Power (on my test dev setup), after fork(), when child touches those pages. [ 62.543247][ T242] Kernel attempted to read user page (0) - exploit attempt? (uid: 0) [ 62.544992][ T242] BUG: Kernel NULL pointer dereference on read at 0x00000000 [ 62.545335][ T242] Faulting instruction address: 0xc0000000000a0ee8 cpu 0x0: Vector: 300 (Data Access) at [c00000002035f9d0] pc: c0000000000a0ee8: __hash_page_thp+0x218/0x560 lr: c0000000000a0e14: __hash_page_thp+0x144/0x560 sp: c00000002035fc70 msr: 8000000000009033 dar: 0 dsisr: 40000000 current = 0xc00000001fb08480 paca = 0xc000000005010000 irqmask: 0x03 irq_happened: 0x09 pid = 242, comm = vfio-fork enter ? for help [c00000002035fc70] 0000000000000000 (unreliable) [c00000002035fd30] c000000000098a40 hash_page_mm+0x688/0x8d0 [c00000002035fe10] c000000000098dc8 do_hash_fault+0x140/0x368 [c00000002035fe50] c000000000008be0 data_access_common_virt+0x210/0x220 ---- Exception: 300 (Data Access) at 0000000010001b60 SP (7fffde6dd7b0) is in userspace With this series, I see that the issue is fixed. Thanks for helping in fixing it. Please feel free to add: Tested-by: Ritesh Harjani (IBM) #PowerPC > Cc: stable@vger.kernel.org > Reported-by: Sashiko > Closes: https://sashiko.dev/#/patchset/20261014-b4-prep-vstor-149847-copy_huge_pmd-recheck-special-pmd-v1-1-d225c60bc316@virtuozzo.com?part=1 > Suggested-by: David Hildenbrand (Arm) > Acked-by: Kiryl Shutsemau (Meta) > Acked-by: David Hildenbrand (Arm) > Signed-off-by: Konstantin Khorenko > --- > mm/huge_memory.c | 24 +++--------------------- > 1 file changed, 3 insertions(+), 21 deletions(-) > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index e00632edb3856..9cb5f99aba87f 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -1961,25 +1961,6 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, > int ret = -ENOMEM; > bool writable; > > - pmd = pmdp_get_lockless(src_pmd); > - if (unlikely(pmd_present(pmd) && pmd_special(pmd) && > - !is_huge_zero_pmd(pmd))) { > - dst_ptl = pmd_lock(dst_mm, dst_pmd); > - src_ptl = pmd_lockptr(src_mm, src_pmd); > - spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); > - /* > - * No need to recheck the pmd, it can't change with write > - * mmap lock held here. > - * > - * Meanwhile, making sure it's not a CoW VMA with writable > - * mapping, otherwise it means either the anon page wrongly > - * applied special bit, or we made the PRIVATE mapping be > - * able to wrongly write to the backend MMIO. > - */ > - VM_WARN_ON_ONCE(vma_is_cow_mapping(src_vma) && pmd_write(pmd)); > - goto set_pmd; > - } > - > if (vma_is_anonymous(dst_vma) || arch_needs_pgtable_deposit()) { > pgtable = pte_alloc_one(dst_mm); > if (unlikely(!pgtable)) > @@ -2021,7 +2002,9 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, > goto copy_pmd; > } > > - src_page = pmd_page(pmd); > + src_page = vm_normal_page_pmd(src_vma, addr, pmd); > + if (!src_page) > + goto copy_pmd; > VM_BUG_ON_PAGE(!PageHead(src_page), src_page); > src_folio = page_folio(src_page); > > @@ -2061,7 +2044,6 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, > } > if (src_vma->vm_flags & VM_SHARED) > pmd = pmd_mkclean(pmd); > -set_pmd: > pmd = pmd_mkold(pmd); > set_pmd_at(dst_mm, addr, dst_pmd, pmd); > > > -- > 2.43.0