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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 070BAC3DA4A for ; Fri, 9 Aug 2024 17:16:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:In-Reply-To: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=k1RM34U80cBuoBEaB3XScsq2LvIzrQPbOvXLfubi1Yw=; b=tef59HaLnR2QIV74E3ipPLsEjg Z4HDRG48bEFH4psoP9Xh6R0A6vHiAVz/ZBlRyi03a88lZmwhC9v2Q0AsB+nKo5gRl+iXdYnPM7q89 yR4Pxk6GAKFK2kX2oNpv7+Q7e0JyZoHWAtBpNRrIbjGhG6HDVF2gAAGTTh+TbyedS1yCO95cvn28t K7319IrCFAtmbqrUG9TNAZEECXZyPBB7AtN5wG8cwgnYXOApEAD8HleN8Ed9ZN0r+Jp31PcNdTG0W SkyVnwVK1Dti5FOpxOdcSOQyonHjwirEAs9Q0hU5foajwrzuFAGZQHaQKOaSsDeX2UCQ367D9+yWU DMDPi91g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1scTDg-0000000C2Oc-2tV4; Fri, 09 Aug 2024 17:16:04 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1scTD5-0000000C2IY-416q for linux-arm-kernel@lists.infradead.org; Fri, 09 Aug 2024 17:15:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723223727; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=k1RM34U80cBuoBEaB3XScsq2LvIzrQPbOvXLfubi1Yw=; b=Uum4qQ7g4woKGEJ/mzhRq3R8CRHLomDq16UHTaY5U3O7Y4N7izmhvbDdwh1l9AccXuVs8n 10MnzJuwG6DCJWeXSf2NcUxeZUUAG4JdzN22NmqphdsR7IMOcbkSzBAjHudmsZGacO8N0F WOgu8Ab72ddWG9ZfA+V2L8d0+v7mM1A= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-371-EROTOWb5PxWDxMFeg71oQw-1; Fri, 09 Aug 2024 13:15:25 -0400 X-MC-Unique: EROTOWb5PxWDxMFeg71oQw-1 Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-3db16c5eabeso588397b6e.1 for ; Fri, 09 Aug 2024 10:15:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723223725; x=1723828525; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=k1RM34U80cBuoBEaB3XScsq2LvIzrQPbOvXLfubi1Yw=; b=GD/WF5TvdepaJACw3enrHfekU7DgPQYpgFhS6QtNxPWQ715JhLe/9RCQ2SqPWCWm5L cEaL4EJFvtgS33hMWvOwuNYQ8vQXkGPxNQl5J6yGfqPMl+oHUx9XdPBftfoxiq3KxO+w clLenUzRG0ZGq44taGizTqZFFXLgUTJI3ePmGY17Vmf/Cra0ys5VPvnhcqLBdqRNqBzY aM54Koef21Tv/MGcD16Myeje/Aoxt8TT1a+Ne1r55gP0mYRmDyCQEcKj7MTW7+4/TPb/ Xp4epSkDs02rBpjxo1YItsYmLS84SWAY5QIEeX26+FYmyIpucCNNchWHeOvmj2DUAex8 wqpA== X-Forwarded-Encrypted: i=1; AJvYcCVtlLlhmcV32n3wa/vudBC613zTvR8cS4Lgqk7u+rO67oRel3qc/Tf2UKPLKdU77tq2LHrSwb9pbA+VClVsbPC44pmzsHq8oPQB4isNmcu6OxW9+Ew= X-Gm-Message-State: AOJu0YzGbBPYv7A2kaWigBgjvJoAbSZ49EjryIZtu7Pc6+3av9cQ+/It flxLSZZyR21TBD2ETp78L76iM9xSo2e7uQWyxQE2G9XIGLIkjzZeAtKTHioKdMY0VbwCqec+INy pvhf3PeLHh/r7jWn3/tcHgGrLdisAnd5g3p0oqQ5FwEioreu83oKmNUhjl1Rf7JDEzbf8jQSm X-Received: by 2002:a05:6830:4113:b0:70b:2a0f:d2d8 with SMTP id 46e09a7af769-70b7c47ca90mr1694651a34.4.1723223724969; Fri, 09 Aug 2024 10:15:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDZ8AG8hkigQJRUf5X9BYk1irDNCGK0SoMgTlS1KNGbXDLrarAMaXAoHbkQ/XXZWQbMY2eQg== X-Received: by 2002:a05:6830:4113:b0:70b:2a0f:d2d8 with SMTP id 46e09a7af769-70b7c47ca90mr1694631a34.4.1723223724644; Fri, 09 Aug 2024 10:15:24 -0700 (PDT) Received: from x1n (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a3785e67easm276234285a.52.2024.08.09.10.15.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 10:15:24 -0700 (PDT) Date: Fri, 9 Aug 2024 13:15:21 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sean Christopherson , Oscar Salvador , Jason Gunthorpe , Axel Rasmussen , linux-arm-kernel@lists.infradead.org, x86@kernel.org, Will Deacon , Gavin Shan , Paolo Bonzini , Zi Yan , Andrew Morton , Catalin Marinas , Ingo Molnar , Alistair Popple , Borislav Petkov , Thomas Gleixner , kvm@vger.kernel.org, Dave Hansen , Alex Williamson , Yan Zhao Subject: Re: [PATCH 07/19] mm/fork: Accept huge pfnmap entries Message-ID: References: <20240809160909.1023470-1-peterx@redhat.com> <20240809160909.1023470-8-peterx@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240809_101528_102139_0980A617 X-CRM114-Status: GOOD ( 32.91 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Aug 09, 2024 at 06:32:44PM +0200, David Hildenbrand wrote: > On 09.08.24 18:08, Peter Xu wrote: > > Teach the fork code to properly copy pfnmaps for pmd/pud levels. Pud is > > much easier, the write bit needs to be persisted though for writable and > > shared pud mappings like PFNMAP ones, otherwise a follow up write in either > > parent or child process will trigger a write fault. > > > > Do the same for pmd level. > > > > Signed-off-by: Peter Xu > > --- > > mm/huge_memory.c | 27 ++++++++++++++++++++++++--- > > 1 file changed, 24 insertions(+), 3 deletions(-) > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > index 6568586b21ab..015c9468eed5 100644 > > --- a/mm/huge_memory.c > > +++ b/mm/huge_memory.c > > @@ -1375,6 +1375,22 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, > > pgtable_t pgtable = NULL; > > int ret = -ENOMEM; > > + pmd = pmdp_get_lockless(src_pmd); > > + if (unlikely(pmd_special(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. > > + */ > > + if (is_cow_mapping(src_vma->vm_flags) && pmd_write(pmd)) { > > + pmdp_set_wrprotect(src_mm, addr, src_pmd); > > + pmd = pmd_wrprotect(pmd); > > + } > > + goto set_pmd; > > + } > > + > > I strongly assume we should be using using vm_normal_page_pmd() instead of > pmd_page() further below. pmd_special() should be mostly limited to GUP-fast > and vm_normal_page_pmd(). One thing to mention that it has this: if (!vma_is_anonymous(dst_vma)) return 0; So it's only about anonymous below that. In that case I feel like the pmd_page() is benign, and actually good. Though what you're saying here made me notice my above check doesn't seem to be necessary, I mean, "(is_cow_mapping(src_vma->vm_flags) && pmd_write(pmd))" can't be true when special bit is set, aka, pfnmaps.. and if it's writable for CoW it means it's already an anon. I think I can probably drop that line there, perhaps with a VM_WARN_ON_ONCE() making sure it won't happen. > > Again, we should be doing this similar to how we handle PTEs. > > I'm a bit confused about the "unlikely(!pmd_trans_huge(pmd)" check, below: > what else should we have here if it's not a migration entry but a present > entry? I had a feeling that it was just a safety belt since the 1st day of thp when Andrea worked that out, so that it'll work with e.g. file truncation races. But with current code it looks like it's only anonymous indeed, so looks not possible at least from that pov. Thanks, > > Likely this function needs a bit of rework. > > -- > Cheers, > > David / dhildenb > -- Peter Xu