From patchwork Tue Sep 24 06:09:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13810127 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 E1D3FCF9C71 for ; Tue, 24 Sep 2024 06:11:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74E6B6B0093; Tue, 24 Sep 2024 02:11:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D6F06B0095; Tue, 24 Sep 2024 02:11:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 577E36B0096; Tue, 24 Sep 2024 02:11:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 32DC06B0093 for ; Tue, 24 Sep 2024 02:11:08 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AA1F8A17F1 for ; Tue, 24 Sep 2024 06:11:07 +0000 (UTC) X-FDA: 82598608974.09.75B0048 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf26.hostedemail.com (Postfix) with ESMTP id C8C83140010 for ; Tue, 24 Sep 2024 06:11:05 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Eh59EGpj; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727158205; a=rsa-sha256; cv=none; b=TbDXBrZFpWCJZSL95hUbv8A4VZldNpnGfhZSJ0d644zRDV6ZeA+AP//dHNwQFytDmKJ46Y e0me5hZ7azP/pXFz8dpZ2tLJmfWtuqE5O2hRP4dQb+Uzxr7dLChN96R94KBV0ianvRnjxo 6xOQNiYC5rDpKnyFJv3apiuEzXx+iZw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Eh59EGpj; spf=pass (imf26.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.175 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=1727158205; 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=WqTOjUnPHL3XRjrt8I2PlSfGhzwzG7n3dPRatvbgECo=; b=r5ZrmaPlp42KsIyNNkx5sWMAGa9JMgkX1LEu/l5r7cmoxbaJdr3ee2dAERwr6l5bHNGAuh r/jYvsmh0sB30jGqRwIAD/D4R2Z2A9kZHSs4sT3Kx/XVYnfDovoe10vTMpIGaNR26jhOHh 7AaP5kqY624/T48dLkx0YWHy3WS09PE= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2059112f0a7so45870965ad.3 for ; Mon, 23 Sep 2024 23:11:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1727158264; x=1727763064; 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=WqTOjUnPHL3XRjrt8I2PlSfGhzwzG7n3dPRatvbgECo=; b=Eh59EGpjabFg6lCs/McEIspZS6BbKE4VgT5DGEgOmJKH2hGWYkaq/n2EhEOVAsKPFM Mu+mxs+85RN8pzBp7Wfixb8mnjHYod10jlXGP08DIqJPFUW9raiA/GiQbiZbpPU1W3HX 8zw9t/uwAYBH9IP4UrSNIJFA6u6nWiEyyle7IpjkA3BrDihd7NshraX4Y0BVIYQn+IVP jcqZBwXXLXnNEe7ZbU8VraadwEw6IEuPgW2dFpQXq7CLNrSy6Ll6z7+4IYI/3bwU3EoR uGPSbfwnrrxip4fQBQGNp/1SCq0QBDKQru+hkVhJZR7sKSRUsdOot7+lxXM9nqz5Mb3P NNiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727158264; x=1727763064; 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=WqTOjUnPHL3XRjrt8I2PlSfGhzwzG7n3dPRatvbgECo=; b=uNa6BIFM1RJA5o9eJOmvry5XLxBjFsvOJ7KXEFokS11n8Eu/dO0NaJTPtLGFCpBkHR BRsqnLOJLPyLmpJ5y0ugesMp57Afy1hRIblgCKpy4X8t4PCDu38sr9zzV+unOQ8vC86f b359RpdPmjjyn1sutl/73k20GQcefWSNJ5XKVpXMFeN27x5VEhVOHosqM39/HNiqWBjH qftDoDu8BBO+NMxMGuTf9NyAqM3sjj8EykQZ+GzVhzaQ28LemqS+ccnORkUss6rsQYd3 983979poEu6JXiL0cEYL5s6RfqAZMMEP5VzUnCWmLeMZEbzi0k0ojBLC4coGGl8p97HA kydQ== X-Forwarded-Encrypted: i=1; AJvYcCWZgdEYV4W9BDNgOo5Ef3e4u/ko6QEv3h2Q42zgVMWK/6Jdn7xaidQh07bkXuRafQBnU4ETb1IzhQ==@kvack.org X-Gm-Message-State: AOJu0Yz6xr9wg0GZj19YT5mmZIfM9c+xkCyNycoSX4sgI1q6rKYCx+ne CbZEftlzB/DHeDiz8Yc72YxQ7jfw8nuGuGwtcexZiZcm4jNdLqtTTHz4RJob294= X-Google-Smtp-Source: AGHT+IGIKI1reF9Jfo+dWimmN1cyjgiGD8qn8mcbfIJc2tOglwlNFzTXQQXYEG1VSSXByHdLorgo7Q== X-Received: by 2002:a17:903:944:b0:205:4e15:54c8 with SMTP id d9443c01a7336-208d8440f7amr165611365ad.61.1727158259613; Mon, 23 Sep 2024 23:10:59 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20af17229c9sm4344885ad.85.2024.09.23.23.10.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 23:10:59 -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 v4 05/13] arm: adjust_pte() use pte_offset_map_rw_nolock() Date: Tue, 24 Sep 2024 14:09:57 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 43ks3uoh9ocpju6yft77ohqaixjwgawi X-Rspamd-Queue-Id: C8C83140010 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1727158265-657511 X-HE-Meta: U2FsdGVkX18EJbYd5zVGjacDKI3wAoSZvhklZ/z6BKCUm42ihT1mHmImXBS4JCHG5lbUYRnfsL5q6Geh3mN1ROq7rxdCNRFfpdN8WiBohxdwEKfOI/2xwFjiNB8G0H6Ezfwy8zavvIhgGspY0ve4SMdjewMNJRe3oUpsxHxWFw4m70B09EUgLW6vroEJRMTvsC0ikSIh+TYaO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79qDYzf7yvMn4lh33RTqgPdILxSeAjBAXripYQOFrt4Q9IKienvyhHT9VFUyUhFc5sVQYk/g3SaQHX+DAKufsSM5EzxRqvZP5g1WUZJFFlkLXeFDcNTtB7Ae4feEYNNxUwdkQ/Uvt3xCCsll7ebJ+szjzHfRIVkPEhqjBkLx28= 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 --- Hi David and Muchun, I did not remove your Acked-by and Reviewed-by tags since there is no functional change. 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(); }