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 E919FCD5BA2 for ; Thu, 5 Sep 2024 10:01:10 +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-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=CZtqExsDOIYp5HqPgrPdq+CzPwDzSkiYY8yvT9Jxwhw=; b=CQLXln+ZkDQnJxGuCCUGiI0bsR xpm3TGB2/Z8ui3vp7eftevtk6st6bNoRNnrAPFvOrx+/uimbaPy9sPUxfiDi560d60kt1+o/Sds6R HVQDVeAS9fCPwK0PcG0HNFzU8LLLF5ILCduJuFD1cM8IZG9D96N4PXLB6jENFOkpG27WhAhWhGAK1 McpmahOXX8ybsZL8imhhW/PDnB0mFL/crHMVCITmLcIFQ9b8ZRkU6UWF+MB4YG17Ik7pJjlxdt6q9 mqLQFYUI7VrPOP+nqkO+dh9fuRgx3f7GT7OEz8CdJG69uqmJL0WoJhn/74bFzTi/j+YgliUQx4mT7 9zzNaU8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sm9IR-00000007sKZ-1NN5; Thu, 05 Sep 2024 10:00:59 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sm8Jf-00000007fHA-1DG2 for linux-arm-kernel@bombadil.infradead.org; Thu, 05 Sep 2024 08:58:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date:Message-ID: Sender:Reply-To:Content-ID:Content-Description; bh=CZtqExsDOIYp5HqPgrPdq+CzPwDzSkiYY8yvT9Jxwhw=; b=JLbKM0vOiZEPig6EoSXhjvSEZF Ub1zUEdzqw+xobhaluFmrqbPJN0EchOjXJriiO/77IHgUaHcCoBfciQSud9CGTEm9heoAArIMfNzJ ocnYSZJHxyXliQiH5LIEgBeyX4BI+VMgvDylV9mmt3eItYvbxc5q27ndkNDyYzVKsJMI05T2Ip9Lh cU46/XUHdDY2H43DPowmWQjSPf0/Iqpw+/RXUaYxc3CPTJx9beQ2jZ4SXy0PCm5Z5b+SgpyegF095 Vi7FkC5qt0zhdO5Ffy5VyvIoHVOff1mpXamslznViXaBGQEh6gc6G8jb1+UlxWGtXxPyXIP/sXjn5 ufaYLdSw==; Received: from out-179.mta0.migadu.com ([2001:41d0:1004:224b::b3]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sm8Jb-00000000RXc-1DIK for linux-arm-kernel@lists.infradead.org; Thu, 05 Sep 2024 08:58:10 +0000 Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1725526676; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CZtqExsDOIYp5HqPgrPdq+CzPwDzSkiYY8yvT9Jxwhw=; b=hHL2mCMbuNK+Hs/O408hs/N7aassZHQuWT3l/9qpf3OwEqwDoq18fUNQy7T4vQkPAhr7Ip KhS7GNRdIL7YXvr4Kwk1nBVf05wbZgGwqSvv3B9YBaijU8YfoK9B1DhEgjDylQD3XI58dx j3oiLIpOO5R1TF/4CpCnFRK2Hn4WWvc= Date: Thu, 5 Sep 2024 16:57:44 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v3 08/14] mm: copy_pte_range() use pte_offset_map_rw_nolock() To: Qi Zheng Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, david@redhat.com, hughd@google.com, willy@infradead.org, vbabka@kernel.org, akpm@linux-foundation.org, rppt@kernel.org, vishal.moola@gmail.com, peterx@redhat.com, ryan.roberts@arm.com, christophe.leroy2@cs-soprasteria.com References: <20240904084022.32728-1-zhengqi.arch@bytedance.com> <20240904084022.32728-9-zhengqi.arch@bytedance.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Muchun Song In-Reply-To: <20240904084022.32728-9-zhengqi.arch@bytedance.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240905_095808_278639_E0C83DD9 X-CRM114-Status: GOOD ( 19.43 ) 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 2024/9/4 16:40, Qi Zheng wrote: > In copy_pte_range(), we may modify the src_pte entry after holding the > src_ptl, so convert it to using pte_offset_map_rw_nolock(). Since we may > free the PTE page in retract_page_tables() without holding the read lock > of mmap_lock, so we still need to get pmdval and do pmd_same() check after > the ptl is held. See commit 3db82b9374ca92, copy_pte_range and retract_page_tables are using vma->anon_vma to be exclusive. retract_page_tables()                    copy_page_range()     vma_interval_tree_foreach()              if (!vma_needs_copy())         if (READ_ONCE(vma->anon_vma))            return 0;             continue;                        copy_pte_range() So I think mmap write lock here is also used for keeping ->anon_vma stable. And we do not need pmd_same(). Muchun, Thanks. > > Signed-off-by: Qi Zheng > --- > Hi Muchun, since the code has changed, I dropped your Reviewed-by tag here. > > mm/memory.c | 18 +++++++++++++++++- > 1 file changed, 17 insertions(+), 1 deletion(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 06674f94b7a4e..47974cc4bd7f2 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -1082,6 +1082,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, > struct mm_struct *src_mm = src_vma->vm_mm; > pte_t *orig_src_pte, *orig_dst_pte; > pte_t *src_pte, *dst_pte; > + pmd_t pmdval; > pte_t ptent; > spinlock_t *src_ptl, *dst_ptl; > int progress, max_nr, ret = 0; > @@ -1107,13 +1108,28 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, > ret = -ENOMEM; > goto out; > } > - src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); > + > + /* > + * Since we may free the PTE page in retract_page_tables() without > + * holding the read lock of mmap_lock, so we still need to do a > + * pmd_same() check after holding the PTL. > + */ > + src_pte = pte_offset_map_rw_nolock(src_mm, src_pmd, addr, &pmdval, > + &src_ptl); > if (!src_pte) { > pte_unmap_unlock(dst_pte, dst_ptl); > /* ret == 0 */ > goto out; > } > spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); > + > + if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(src_pmd)))) { > + pte_unmap_unlock(src_pte, src_ptl); > + pte_unmap_unlock(dst_pte, dst_ptl); > + /* ret == 0 */ > + goto out; > + } > + > orig_src_pte = src_pte; > orig_dst_pte = dst_pte; > arch_enter_lazy_mmu_mode();