From patchwork Wed Sep 4 08:40:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13790198 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 5940BCD37B6 for ; Wed, 4 Sep 2024 09:13:54 +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: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ffoF6/h1+uprLqHi9escfobEZTHc+3j5UpPJif6C52k=; b=XujGvwIIIYTNs7iw4Qtf9Hyov/ 08F/BxjmVLG5DAZl+sFXxLcZJYykwh8RE3j9sYZPfkdjKfyh5XjKUGmGtG+Rfi1A8QXoXqnT27h/H P+RzNHeDmHzqMGHO/2wP81dGsvM4tOCtTy6oARZ4jA58RsITXpsMlw5FUcp9cKqoEnJxev+utEAjE FrujwZajCSxeMSIdDu6xM29GybWNxevOjEcqUXGHr2N2MSBgH5qUzZTyHSqkLXhNx7PCrVgR8MtX1 HU/U0QakYK6FU9JG7PNftXdRe1Fxrsx62C12Pxbhf1DiLlwqetIKsv7QIC4eaO297TtOxGXC1Yp3U yzNCG29Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1slm5A-00000003chs-3Pfk; Wed, 04 Sep 2024 09:13:44 +0000 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1slla4-00000003TNP-30rO for linux-arm-kernel@lists.infradead.org; Wed, 04 Sep 2024 08:41:38 +0000 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-7d4ed6158bcso1440728a12.1 for ; Wed, 04 Sep 2024 01:41:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1725439296; x=1726044096; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ffoF6/h1+uprLqHi9escfobEZTHc+3j5UpPJif6C52k=; b=HV8x1aZ6/h3zsoAhWqlbBs+RH+lq61zFbEaVi8AfNk0JtLaG9QsZ5TcesjOzgq8SEn 0xwAfNiiRhvXAJUiFKCv1IdT0bGtXOQSshjrxgI1Xd8QeB3/yss/+EEUHwZyUMxouUxU 8FUUCN9UvY+xiBZllsWqbIEJRoEY2BfS33Bs7Ea/Cqqs/cHA0o5hmLlDV1oj1ww4BHqr 67dRaTqz/jnk9/06GPjt6ZS0opiYhkIyy0v0iqT1J29UDUcG4Lzq+H8m4MBi4P+gU3SV MmRfvg37gzKwxUVivt1sv5PNNEV3zDM1vx/n78PJg/8ZJpY6poHVOT6zCKp1C6/qEpHD 0cTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725439296; x=1726044096; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ffoF6/h1+uprLqHi9escfobEZTHc+3j5UpPJif6C52k=; b=G2IAVM1FXHgjMDxZOze5J80kQRazJDyMlLUcM4C8y5kxUZM4Xi85FagIpJqJfYrmiS AvMKrnpKSGPXEQFcxGp0tHgWF/cnbbMZQco+H7KpM4f50ycjcNvbEFTYdRvZMKiImTdI zN0JK8oAScyNQctXAKoLIW/vC8ZaDgYX8lIuNaSgmc/t9nC2czfso/4AVu6bYhqOHyH6 u9BzB4JmMawhO0x9NvgKRDFUFIWcZ/rKWVWz8UpDJx0d28csqNBF7QqKNimncZFpM1ts zpMF4rmtpwau3UMl3YR2knJX1pGDpwUC0U2es/PRDec+eF8vukYURK0eUhnLtZ6fBPiI 9s3A== X-Forwarded-Encrypted: i=1; AJvYcCWP5krHprIN/uB31wrxGM2JoxiLVkM1xUxZ3FYv7h4cMQCRUQ38FGyDLEAK8jXCUFf9w9CBR9wq9ryA1o9boziK@lists.infradead.org X-Gm-Message-State: AOJu0Yw+W/7z5u/vm4R1pf9xgOy/Qg/s5QHyXtAhMXhcjHkHl3SceUHS jHGfTq4xOy8+FAbEKNVwm7L1UoUvsjsaIwY+zxj5gu8v54ol+Jh4LgKGEmCJZSY= X-Google-Smtp-Source: AGHT+IHnX4UPdOrG4UbMFan2fo99NWoUyUAqXexLL4zg80mEgnx1zTpF+DDeMcB5gD3OIXAOIo10hg== X-Received: by 2002:a05:6a20:b40a:b0:1ce:f77a:67d4 with SMTP id adf61e73a8af0-1cef77a69e6mr6594806637.6.1725439295802; Wed, 04 Sep 2024 01:41:35 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.242]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206ae95a51csm9414045ad.117.2024.09.04.01.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 01:41:35 -0700 (PDT) From: Qi Zheng To: david@redhat.com, hughd@google.com, willy@infradead.org, muchun.song@linux.dev, 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 Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, Qi Zheng Subject: [PATCH v3 08/14] mm: copy_pte_range() use pte_offset_map_rw_nolock() Date: Wed, 4 Sep 2024 16:40:16 +0800 Message-Id: <20240904084022.32728-9-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20240904084022.32728-1-zhengqi.arch@bytedance.com> References: <20240904084022.32728-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240904_014136_777777_5E45DC19 X-CRM114-Status: GOOD ( 13.83 ) 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 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. 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();