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: 13790096 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 F118ECD3431 for ; Wed, 4 Sep 2024 08:41:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7FC368D023B; Wed, 4 Sep 2024 04:41:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 787048D0239; Wed, 4 Sep 2024 04:41:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FF408D023B; Wed, 4 Sep 2024 04:41:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3573C8D0239 for ; Wed, 4 Sep 2024 04:41:39 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E2FA3120E5D for ; Wed, 4 Sep 2024 08:41:38 +0000 (UTC) X-FDA: 82526412276.15.A95DDF2 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf07.hostedemail.com (Postfix) with ESMTP id 14FF540011 for ; Wed, 4 Sep 2024 08:41:36 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=bukH+c+T; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf07.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725439273; a=rsa-sha256; cv=none; b=BihjQLgmy5Oz+SnL8R2CM+3MJHz1Tz4aW1qf46egRLXFbjQ1J6lPu8nfzpdTBvNPvgGSvi 4AwYaU9wle4lbJX0MUo+Cy+6N8CFwSlvj7l6Ze9k7EqHM64cNXB2PUY5Nzvzua4mHrj+9h KqFCYluTFzUZ1n2d2BpRgT5CApwwj5Y= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=bukH+c+T; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf07.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.182 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725439273; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ffoF6/h1+uprLqHi9escfobEZTHc+3j5UpPJif6C52k=; b=grfZvLB3FfHJivQSNr3abiLbHePUWS8wSvnpXwy/CbaBJSmG/Y8sLdj0Oy0u5cyl4CSu6D TOEktCmdai0CBxh0jZKCgo+OWZBpGCV83LITwGv3i/e7CTfbtorveABr6dCVZNqB13mGrW //xDmEBiTvaEemtGC5m0GkAO1x6ibc0= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7d4ed6158bcso1440729a12.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=kvack.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=bukH+c+T/nx2mTuA4ed64O1uXJDLjUBtrkDPu0/SvWEhyR+rgdPdrSbKV7Khh0GAte COHpoJjzIssydrF7SpeK+SBZznHyrhoQfeJGzkJCwU14hj5Xw5hGuUL3/F9EJxE8d3w6 A8zyBUchgZ74kLtX3Q2BsK6kIyO1YEJdA95sRisFz/NT6JI16/18Sz7u7rCou+qRm2Dn 0+7fitYroKNcJknDftuIBtlx8uysIK3sZOogZdcsc7PavBVfO0rWb75/QJkOK6AV70EA 9XmxAWIwX6MZftXbK6b/fsxACkqmzZE6ZyLB69+pogdYm8AGONurJN7rqKrmDhrsElnQ sApg== 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=ao3j3hN0GGUqz2DYcnj7NzKxIZB0EHpPbKi0EX/gg2zyoE2xueYvJGNC2XOiVg0UMU eA5OmhiCMS3B5EH7OfiIZ10Bh1tOQJcUIocI8/cxtJu5bXY+bTaLMNHGrJVfgs5k4xKh mFvC6OGJ6Gyq6HNuRP3Lu4hI4kHP/mIjLaAwgfm1fHZ3pDYBCvdkN9gdNIB0embI5+3G QXE3jHFL6aQADv1bKIkruO0AsSrSa87XB6kVQ+Lhf6YHbjH3lyQCw6c/FCHErwcIw6AW tcJfXIq+qg6LvDt35X2HD2NF0puR/v+0bLgXotC7M5q9c6jq+oaMbB1151uShgEoa0+4 +nZg== X-Forwarded-Encrypted: i=1; AJvYcCUUnfUh87cDDbFnM6KDTIY6B+GMW7dTNRwyrMXy7LWGJcTOHLYBmpHA9mMx5Tbv7x3QFh+c08lzAg==@kvack.org X-Gm-Message-State: AOJu0Yzp0TyPOG8csQEctlmN1bXHCAW8ZjyyE6OQA6wh1HtPT/WixgVK lCxYKiQFnZD1Ic7sNuFBt0WTmM6PhMiiBsVh8IK3xX/1hlhMd2HqikFFJkW1Izs= 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-Rspam-User: X-Rspamd-Queue-Id: 14FF540011 X-Rspamd-Server: rspam01 X-Stat-Signature: d145a4iqgz99d4wauuh5gaduebg37rgo X-HE-Tag: 1725439296-589862 X-HE-Meta: U2FsdGVkX1/yCMR36eycI7Gvf4FvivCAcsLJcsaVHv5/urpddwyUZ+U/J+ZtTQV61O9+sBkE2PvjxU5VAHR6o2CgjVm0VTyAnPio88yCPTz6it5BoD7fcf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iTCUwIcJ7SdA8qnYPCR8QLJC/LIOotIVuCzeZrUK+Jvn2nyOSw6rvdWZT1AbgtzbAa1rwZu8wurk3j71ZFcMk8TKmZLRWlZ6poQiSsr+bQS82TmmyzegNoDIBWZDm4bqzBrnukGpwbZESVdUuWTLATAqtL9ikVSUDcxdKOMA+bGBvsuHIRH1+aTeqxqn2hZC9W5Odv3I3udlfmgqXCfldiV9QMkkUMxb0iueg2vrLUnE65Zf2IF1AUqlFt 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: 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();