From patchwork Wed May 8 11:34:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13658636 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 24CEBC19F4F for ; Wed, 8 May 2024 11:42:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gGJn+MI/mLy/LgmkctDM2YXg8m2fh7EIH46oFKCYzWI=; b=H5zv5UHzJqY/Iz sJhkBwtwajjaE7b74unp+lQvI/DMZcJY3nT9/4Hs3tBb0WhSqPfklbxB6r1+5nPA21AEZbY6lK49l ZwXtVhAHTYN5R1b+YrhCywtyz8BF0X12Y995moEnPwdrWJKcq7o07zoSiSBVAWElufmTGGfFxa3Dz Fyg9NT5zjPSYd1nCbjz9ZTZS0U7PzpBTlChdbkSziSBj8YXrClbpyDkEmbaC0cSWEZpvFROxKRrfF C/52Y4RWhGUj75ckRZy1yzZki/cwOmyuouQ9ixFS7AMkkiBWr1Ul2lsn2KeArBsO4LyUIHXcg+QkH LJ3tDi529OGfoMM3ZrbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4fh3-0000000FJAu-2xnc; Wed, 08 May 2024 11:42:41 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1s4fgz-0000000FJ7s-1Wph for linux-arm-kernel@lists.infradead.org; Wed, 08 May 2024 11:42:39 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-41b79450f8cso27142965e9.3 for ; Wed, 08 May 2024 04:42:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1715168556; x=1715773356; 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=p3J74NkW9ioW00Pqnp327XZ4SYUtOBKlfZoGCCQmZ+M=; b=C3QwSEkQb/iv9K0WDBtk/FW6bN33tBJ2IxY4mCDX3La8/jvmCNfswT5Jzvk9kkmJhp 1X8JlR2WjzPiWEivr47RxueDzSGyo5JeplhAdQ9XIqbTmwdSHXT7bw7Qt8AmT5/s2mQZ q/tEbv3sKgt8J9IC/k1bi6TlofeBNQjWgdil+2T96TUmqQxRXJLhaAd/+dFGYPxs4s91 vd3EoFZRb5JpN0CTuj91jknhA8mO5i3fDYwBFVxZ0qYhb0V00siCqrTKGhDFGvZS4Cxo c3nP8/+HQt3IWwl/u98SphcMA/RBxaJgP8oPzGdJLkFyB9BkgeE1lnIMfEikzUnRrxyf rcCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715168556; x=1715773356; 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=p3J74NkW9ioW00Pqnp327XZ4SYUtOBKlfZoGCCQmZ+M=; b=CsT+xK18io5layHIXHizpC2KIF6VqYmklXLmMRV6HPWnZtJoTtCkDEZUoyBzz6dqiD /rpRzWhKbUEyXdMOoaXdXlhS/mBh1XbAK8adD9erXA/fA6r0VYgxj09UNMLwzFJUcpe4 xE4gjnetOYje6vqJyKFjsxZGLHu8pezKHajKUDqKamp4W1MfINm5heu/EWruu3yJIIQD dHoHptRUAicpdolrOPBphIUvF5+e2L+n3xAHT7VJqcsf4RXOn4yZyBKyH6dAz/PQWsW9 J6ikm4Z8I7dCsWndKcg0dwqLdS6Kl+kDCWNtOS7Xb+QDdHS6xregPB0neUM5YtemM26e cqgg== X-Forwarded-Encrypted: i=1; AJvYcCXcJcwDX3jIBZjywGObvoIsrOm4m5Bj/IfptcyTNDPgwUDrXw+l4s3X6DBqpWidn6ZrHo3bp3mr9QV+6ewN3M+zQCa95mRHs0ZgrpqhH/V2E2WuOOI= X-Gm-Message-State: AOJu0Ywvy4rvfHyF3nbeNc59qspUTj4VqE4htiiwUshDk3E51+HZlE2z vH7mxsWWYFCQLlOlwhkzt6vjkROUEEypVUDkUrR+bkggDdTYveqzg+ToE2ghitg= X-Google-Smtp-Source: AGHT+IHE8o6Es8ZSc8TwRFtN83wex84gZARiN4tVTDQiUBBDnfszL6lfcSMLSwFM5H55V+vhK3nrvQ== X-Received: by 2002:adf:b1d1:0:b0:34f:6c9c:19a3 with SMTP id ffacd0b85a97d-34fcade2a1cmr2221817f8f.15.1715168555855; Wed, 08 May 2024 04:42:35 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id h1-20020a056000000100b0034e0b56a62bsm15118070wrx.44.2024.05.08.04.42.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 04:42:35 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH RESEND v2 8/9] mm: Use common huge_ptep_set_wrprotect() function for riscv/arm64 Date: Wed, 8 May 2024 13:34:18 +0200 Message-Id: <20240508113419.18620-9-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240508113419.18620-1-alexghiti@rivosinc.com> References: <20240508113419.18620-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240508_044237_498960_B3F956E9 X-CRM114-Status: GOOD ( 14.12 ) 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 After some adjustments, both architectures have the same implementation so move it to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/mm/hugetlbpage.c | 27 --------------------------- arch/riscv/include/asm/pgtable.h | 7 ++++--- arch/riscv/mm/hugetlbpage.c | 22 ---------------------- mm/contpte.c | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 52 deletions(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 052a5bf2926c..e56f2c8ec7e7 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -277,33 +277,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, pte_t *ptep) -{ - unsigned long pfn, dpfn; - pgprot_t hugeprot; - int ncontig, i; - size_t pgsize; - pte_t pte; - - if (!pte_cont(__ptep_get(ptep))) { - __ptep_set_wrprotect(mm, addr, ptep); - return; - } - - ncontig = find_num_contig(mm, addr, ptep, &pgsize); - dpfn = pgsize >> PAGE_SHIFT; - - pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); - pte = pte_wrprotect(pte); - - hugeprot = pte_pgprot(pte); - pfn = pte_pfn(pte); - - for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); -} - pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 20f62505d0bc..9e397935536e 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -668,9 +668,8 @@ static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, return pte; } -#define __HAVE_ARCH_PTEP_SET_WRPROTECT -static inline void ptep_set_wrprotect(struct mm_struct *mm, - unsigned long address, pte_t *ptep) +static inline void __ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep) { atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); } @@ -704,6 +703,8 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, #define pte_clear __pte_clear #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags +#define __HAVE_ARCH_PTEP_SET_WRPROTECT +#define ptep_set_wrprotect __ptep_set_wrprotect #define pgprot_nx pgprot_nx static inline pgprot_t pgprot_nx(pgprot_t _prot) diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index a59b776e9c8b..440d3bde88f2 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -173,28 +173,6 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags) return entry; } -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep) -{ - pte_t pte = ptep_get(ptep); - size_t pgsize; - pte_t orig_pte; - int pte_num; - - if (!pte_napot(pte)) { - ptep_set_wrprotect(mm, addr, ptep); - return; - } - - pte_num = arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); - - orig_pte = get_clear_contig_flush(mm, addr, ptep, pte_num); - orig_pte = pte_wrprotect(orig_pte); - - set_ptes(mm, addr, ptep, orig_pte, pte_num); -} - pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) diff --git a/mm/contpte.c b/mm/contpte.c index 9c7a9f250bca..8ad2a3099dfd 100644 --- a/mm/contpte.c +++ b/mm/contpte.c @@ -14,6 +14,7 @@ * - __ptep_get_and_clear() * - __pte_clear() * - __ptep_set_access_flags() + * - __ptep_set_wrprotect() * - pte_cont() * - arch_contpte_get_num_contig() */ @@ -25,6 +26,7 @@ * - huge_pte_clear() * - huge_ptep_get_and_clear() * - huge_ptep_set_access_flags() + * - huge_ptep_set_wrprotect() */ pte_t huge_ptep_get(pte_t *ptep) @@ -233,3 +235,23 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, return 1; } + +void huge_ptep_set_wrprotect(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) +{ + int ncontig; + size_t pgsize; + pte_t pte; + + if (!pte_cont(__ptep_get(ptep))) { + __ptep_set_wrprotect(mm, addr, ptep); + return; + } + + ncontig = arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); + + pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); + pte = pte_wrprotect(pte); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); +}