From patchwork Thu Sep 26 06:46:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13812861 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 9B276CCFA07 for ; Thu, 26 Sep 2024 06:47:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C4CA6B00A4; Thu, 26 Sep 2024 02:47:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 375A36B00A5; Thu, 26 Sep 2024 02:47:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23E616B00A6; Thu, 26 Sep 2024 02:47:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 06F186B00A4 for ; Thu, 26 Sep 2024 02:47:29 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A8ACE1A1530 for ; Thu, 26 Sep 2024 06:47:28 +0000 (UTC) X-FDA: 82605958176.06.1C595B7 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf29.hostedemail.com (Postfix) with ESMTP id DE040120003 for ; Thu, 26 Sep 2024 06:47:26 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=izWquh72; spf=pass (imf29.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.160.178 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727333126; 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=VXy51M36oXFNN3kWVEF6T1v76BlvE2cEswNH4MTPbjU=; b=yZCSM1RKIdAEAirkb4/RI1U7miXr14nOHzmXppF3fPW8EB031dhnKAQegZeELhmrFNqIfg 63Foq7ORa2aanZzDFf4AiYaZpez/uXi/IjAym/oaOc9eKZgGyQekD8s650oIc+XK6Xuzhu Pd3kdrBvQTJ1vaPSOPDZRSYzFSmATEk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727333126; a=rsa-sha256; cv=none; b=kBPvQ0jxyjrtcqLKdyWLarFO/QX679hlQOiQoByfV3LkCXiOZ56B/ArPK4+KYeeCpLWOaO zJ1vFPBG9IWbkDXMJHWDax6qT90uJA3uzHHylVWtJ3CRdfKUMFTR+lyEB1sFvCneWnaztu fK2VyMZinQjJ+coY2nRHjYPwlapSae4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=izWquh72; spf=pass (imf29.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.160.178 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-45832b2784bso4621241cf.3 for ; Wed, 25 Sep 2024 23:47:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1727333246; x=1727938046; 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=VXy51M36oXFNN3kWVEF6T1v76BlvE2cEswNH4MTPbjU=; b=izWquh72CM87Sz8nYSgXWo8OqJvCz7axyLXE7TC+SZIDfmHffEfLeXGkWobWPXWyXK HIhebODT+ivdmVkEMGUQAPXIsl1hEGH28ZaaooONvadCa1VF2j8wNxSrRvv1mo1MkkyZ tUAbBfvxdDmNw7qumWTAz5rQXm88dZpqG9YGShbn5P6DUtUzzFA5vLOsGupeE5bil7Mm SrMW1MUYgPZ/lqbRDhijGjW32Rn+Rr9w8a4MOeVI6hj81W/0YpWn9cFGmQf1FieUJSP3 bKFCWFPMrSqcO/UIhy5fQvpYt0aNKagmd3h5zuF5yvVyQ73gxO/xZEcR2P9b3NiDgWnR ei3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727333246; x=1727938046; 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=VXy51M36oXFNN3kWVEF6T1v76BlvE2cEswNH4MTPbjU=; b=vzdAprJP6BtriJHkoQHHFBQFf/9D4aGLXjcH+wAdU3UcYlIA9ZeH8vOP4f8P3KnmJT vSFR9hILds9imeQRJZVMlK89EyxxFhhlvqbnSkubVVeKnMzi7t2OuNeYd2bfiGuiWVE7 H6Fo4L7akH5G6d69yzB0AwcLKmA+0MDkGhT0azPk9GdWV9E2qRRVUeL4wxBUK+2sJn93 Ct/CLO342xDxbeqsaQ87OMiecrFCxFKGHAWM+fyhCKWOTLGWdB4m3CVc01hI6Im0WWy/ OSfPd6JZCbe/j1EVJLTp2GWMRaNoMGdjSqFOLXCU5U2lzCoIapVNdnRjINwxy1v4mx5n jSgg== X-Forwarded-Encrypted: i=1; AJvYcCVl84Jp4VdjchZ9bYfdFRSwC9SrMmUc6sK0vVrFjixY2/jCm+N1gICg1vr7kwB/nuV5TpzAFjmALw==@kvack.org X-Gm-Message-State: AOJu0YyNrO3UopKE5QGLMgVSqk2o8kzR1p7EGhhAdMhlebQx9yDxY1Ge 4s668Sc0m6mf4JfNEfKONsQ9UlCUHI7HFxfe7G83X3Yy0gM9R9iifysRQczxATg= X-Google-Smtp-Source: AGHT+IGInGARMP6O0lOtN8I02J26ctBRW9WAU/Ifxw0RvFOYg2EOnEie2juwEDAOYLUuf+d4v6O7qQ== X-Received: by 2002:ac8:5885:0:b0:456:94fe:dcdf with SMTP id d75a77b69052e-45b5def4674mr66807991cf.35.1727333246109; Wed, 25 Sep 2024 23:47:26 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([203.208.167.150]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45b5257ff1esm23024611cf.38.2024.09.25.23.47.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2024 23:47:25 -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 v5 05/13] arm: adjust_pte() use pte_offset_map_rw_nolock() Date: Thu, 26 Sep 2024 14:46:18 +0800 Message-Id: <0eaf6b69aeb2fe35092a633fed12537efe645303.1727332572.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DE040120003 X-Stat-Signature: o6j49spntptu1dwktp6rh59rrufsrgwk X-HE-Tag: 1727333246-979060 X-HE-Meta: U2FsdGVkX19QauVTH3ciQollVlXVT6cJyeML/Oxp4BEI8EfTKCgG9XEljI07eQl13NJskvVffWrtilYzbUWuROJCH1vLFhr5aXrmJyXdB/9VwF/X4PXqgJEUl+5eofK7LVSDKNSYsw4gGrJLicnrsf8G3DVgJg2gEwOVdRj6Xiv5B5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ECPTP2b+Dd+ezeEwzxtu+GG1MhHmiDDNEa12IIHKOZHg+Z5rMkDl7oG8beXjyZHpzkG9XXPcpZMCbZ1UvaA7OABAVt2EULQ+W4/UPf84D1dZ+dtmsW0fcEREkvc/c+wCAmWzoEtdL+91wQuxidEnN7GEvfJWigLtwaJ2F2gFIL/lN2E1WzuPseFlKEcYjkCK3pZzlYjf4= 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 do_adjust_pte(), we may modify the pte entry. The corresponding pmd entry may have been modified concurrently. Therefore, in order to ensure the stability if pmd entry, use pte_offset_map_rw_nolock() to replace pte_offset_map_nolock(), and do pmd_same() check after holding the PTL. All callers of update_mmu_cache_range() hold the vmf->ptl, so we can determined whether split PTE locks is being used by doing the following, just as we do elsewhere in the kernel. ptl != vmf->ptl And then we can delete the do_pte_lock() and do_pte_unlock(). Signed-off-by: Qi Zheng Acked-by: David Hildenbrand Reviewed-by: Muchun Song --- arch/arm/mm/fault-armv.c | 53 +++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/arch/arm/mm/fault-armv.c b/arch/arm/mm/fault-armv.c index 831793cd6ff94..2bec87c3327d2 100644 --- a/arch/arm/mm/fault-armv.c +++ b/arch/arm/mm/fault-armv.c @@ -61,32 +61,8 @@ static int do_adjust_pte(struct vm_area_struct *vma, unsigned long address, return ret; } -#if defined(CONFIG_SPLIT_PTE_PTLOCKS) -/* - * If we are using split PTE locks, then we need to take the page - * lock here. Otherwise we are using shared mm->page_table_lock - * which is already locked, thus cannot take it. - */ -static inline void do_pte_lock(spinlock_t *ptl) -{ - /* - * Use nested version here to indicate that we are already - * holding one similar spinlock. - */ - spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); -} - -static inline void do_pte_unlock(spinlock_t *ptl) -{ - spin_unlock(ptl); -} -#else /* !defined(CONFIG_SPLIT_PTE_PTLOCKS) */ -static inline void do_pte_lock(spinlock_t *ptl) {} -static inline void do_pte_unlock(spinlock_t *ptl) {} -#endif /* defined(CONFIG_SPLIT_PTE_PTLOCKS) */ - static int adjust_pte(struct vm_area_struct *vma, unsigned long address, - unsigned long pfn) + unsigned long pfn, struct vm_fault *vmf) { spinlock_t *ptl; pgd_t *pgd; @@ -94,6 +70,7 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address, pud_t *pud; pmd_t *pmd; pte_t *pte; + pmd_t pmdval; int ret; pgd = pgd_offset(vma->vm_mm, address); @@ -112,20 +89,33 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address, if (pmd_none_or_clear_bad(pmd)) return 0; +again: /* * This is called while another page table is mapped, so we * must use the nested version. This also means we need to * open-code the spin-locking. */ - pte = pte_offset_map_nolock(vma->vm_mm, pmd, address, &ptl); + pte = pte_offset_map_rw_nolock(vma->vm_mm, pmd, address, &pmdval, &ptl); if (!pte) return 0; - do_pte_lock(ptl); + /* + * If we are using split PTE locks, then we need to take the page + * lock here. Otherwise we are using shared mm->page_table_lock + * which is already locked, thus cannot take it. + */ + if (ptl != vmf->ptl) { + spin_lock_nested(ptl, SINGLE_DEPTH_NESTING); + if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) { + pte_unmap_unlock(pte, ptl); + goto again; + } + } ret = do_adjust_pte(vma, address, pfn, pte); - do_pte_unlock(ptl); + if (ptl != vmf->ptl) + spin_unlock(ptl); pte_unmap(pte); return ret; @@ -133,7 +123,8 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address, static void make_coherent(struct address_space *mapping, struct vm_area_struct *vma, - unsigned long addr, pte_t *ptep, unsigned long pfn) + unsigned long addr, pte_t *ptep, unsigned long pfn, + struct vm_fault *vmf) { struct mm_struct *mm = vma->vm_mm; struct vm_area_struct *mpnt; @@ -160,7 +151,7 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma, if (!(mpnt->vm_flags & VM_MAYSHARE)) continue; offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT; - aliases += adjust_pte(mpnt, mpnt->vm_start + offset, pfn); + aliases += adjust_pte(mpnt, mpnt->vm_start + offset, pfn, vmf); } flush_dcache_mmap_unlock(mapping); if (aliases) @@ -203,7 +194,7 @@ void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma, __flush_dcache_folio(mapping, folio); if (mapping) { if (cache_is_vivt()) - make_coherent(mapping, vma, addr, ptep, pfn); + make_coherent(mapping, vma, addr, ptep, pfn, vmf); else if (vma->vm_flags & VM_EXEC) __flush_icache_all(); }