From nobody Fri Jan 31 00:06:21 2025 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4955B1D540 for ; Mon, 27 Jan 2025 09:43:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737971036; cv=none; b=aq5YPoQpk5lQ6t+z7clPzseF4Z/ywYSU2Gv2GAYsekCC4wF2dxyxc0T/0GbtUjDwmODKupi66yk2HzbFzf7XPmG4r9VsKisIQ4MA+JWUQ288RlRQnA1FG/+aQ/p1Kn1gpRQ9DTfxzGyF4P3ST6IEwscucqo6i3ZZm+kt4TYnP3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737971036; c=relaxed/simple; bh=W4BU+7ZxHze8a03+hdx9R9TLCBIXkq4dSl9lYttp77g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cEFmimTlJv/YNnDvqTNCg3fxT8tuhgOvd0rksJ5LJGfh3uiSiy9r8PDTp6WwAcHT6nnF6JoxYxAlMC6NceLOl6pPJ77Qei+dWOHzj/uKJOV572tcEqU2dECPYSGamM33FFPwk1TSvUqWdGcFNbY2sqnzuiBMUlMOloBlWtPkyyM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=1OtYMwuZ; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="1OtYMwuZ" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-38a25d4b9d4so2206319f8f.0 for ; Mon, 27 Jan 2025 01:43:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737971032; x=1738575832; darn=vger.kernel.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=C5hVtVN5ts8qRfTru3U6IxaXR2nRBQvNb9/UQuUojb8=; b=1OtYMwuZ+HIUd08opbq0z+OVzPutCVV7BMI/rRs1nx2p04iQo/Y95hm5WyZgLPy1xi jmSV+Wp14WBAJ5Rmwp34wWPmaUdq908zec14MQ6wKZ+ZkimtF7+8dVH1PXwwCcm3CnTz Bx1kXymXZzWXvj1jxo/jm7ZtRic19RsBgJF6f6t5G9tnFQ5upH0JN0hm//NZfg+fnac3 PDDT22phDrVT/PGypRejbLVe5vX/ySAhv10NDSv9MQc9yjwXjQGTnSU92Drs0R4aLTN4 F1n8PMBw1/wsoubWV6lbHXEG/FEYk4UJufEJfpNMMTKE9BePqOsJiKPsTimZuJeS6RVV s8jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737971032; x=1738575832; 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=C5hVtVN5ts8qRfTru3U6IxaXR2nRBQvNb9/UQuUojb8=; b=I7lh7Pku0t+ouqeJBl6GfoFjz/KTLtWp+g6IcuD+Psqbbgz84DMBjljJqNQQac1zrW MFfM3BNwzGD6TWu5vm8jwR64Tc22G3DW/qQwxrM2aqtwlJ85ot3Dhp1/uWBjDcso2+/w AdMqziins676UXB2ERznWkEfn800wrb+/KO3A8pz3b4AMLjN2YCEBWQMHSa2VeiYltJP Iw5WguQovn8tYtTcgKZz2KeKG7HmejTTFzAV0t15jG/yVdLfkk/AzaRh49AAulsCcsK/ +rMT5jhOBLBKio19lsYM2T0SOPDgHhx3WnQJPPTXh3UJ9ObAT23PJVvCcjSq1vBPjpZt s2Kw== X-Forwarded-Encrypted: i=1; AJvYcCWSKOaOokYyjf/5qf7BJ7HprBC5My4Eg0jw07GKvuqKrABGZYHtOzrTFspp95ftPJKVpCFeHqA/9QZhFDs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+UlafrvObgiMFvo+ZXVda0ErR6r+nGqKKjUnq3RPZJV0Qr4pQ wudB5SAr5T1avWByxOkaGHyAv7RzOZ2spBs0j5ZX3gpF3h/Qeh7ew53yVwG8lk0= X-Gm-Gg: ASbGnctM886Ip5O+8xx7yU/SEmJZiUGfLe3BVQwXO2lV59Sos2AQHJ8TJkjwTVvaIEq ugvu9HQVRicsROvTaasqsZw3K9qnMFSgBS+Zj7rGxp1lcQVqUnaVGDXgcougG128dPUPIInp4jF 6IF/PeCv3eQnaayKMf1QsP+6iWk5mHF6arbY6f8DdUjqvAK3MpNHIa4fIBG9ghXHCkcQ+0i/+YX VPdR8/7EY41tA6aQUbid67ZRsgdI27nhJlhOpcbJ9PlSZb1avoWY6fP6rsYDtDsqdIWFMhMmC4d offwxtNFoSTDOrZSmcwc2/97Dv/kaB4= X-Google-Smtp-Source: AGHT+IFXkE/IMZA1k4v5FDtRJn2j5Onh6MT51lwkeaLHVsBqz96ACklS2Q489STBeMVgKrxepukPbg== X-Received: by 2002:a05:6000:18a8:b0:385:f69a:7e5f with SMTP id ffacd0b85a97d-38bf57a9a86mr46329622f8f.38.1737971032476; Mon, 27 Jan 2025 01:43:52 -0800 (PST) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3e22:3e78:ce5a:32c3]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a17d6a6sm10405111f8f.26.2025.01.27.01.43.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 01:43:52 -0800 (PST) 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 v4 8/9] mm: Use common huge_ptep_set_wrprotect() function for riscv/arm64 Date: Mon, 27 Jan 2025 10:35:29 +0100 Message-Id: <20250127093530.19548-9-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250127093530.19548-1-alexghiti@rivosinc.com> References: <20250127093530.19548-1-alexghiti@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After some adjustments, both architectures have the same implementation so move it to the generic code. Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/hugetlb.h | 3 --- arch/arm64/mm/hugetlbpage.c | 27 --------------------------- arch/riscv/include/asm/hugetlb.h | 4 ---- arch/riscv/include/asm/pgtable.h | 7 ++++--- arch/riscv/mm/hugetlbpage.c | 22 ---------------------- include/linux/hugetlb_contpte.h | 4 ++++ mm/hugetlb_contpte.c | 22 ++++++++++++++++++++++ 7 files changed, 30 insertions(+), 59 deletions(-) diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index 654f5f2f03a3..fd1de0caad3f 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -35,9 +35,6 @@ static inline void arch_clear_hugetlb_flags(struct folio = *folio) =20 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte -#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT -extern void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, pte_t *ptep); #define __HAVE_ARCH_HUGE_PTEP_CLEAR_FLUSH extern pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 643ba2043f0f..0430cb41f381 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 shi= ft, vm_flags_t flags) return entry; } =20 -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 =3D find_num_contig(mm, addr, ptep, &pgsize); - dpfn =3D pgsize >> PAGE_SHIFT; - - pte =3D get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); - pte =3D pte_wrprotect(pte); - - hugeprot =3D pte_pgprot(pte); - pfn =3D pte_pfn(pte); - - for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize, pfn +=3D 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/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index bf533c2cef84..4c692dd82779 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -24,10 +24,6 @@ bool arch_hugetlb_migration_supported(struct hstate *h); pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); =20 -#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, pte_t *ptep); - pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags= ); #define arch_make_huge_pte arch_make_huge_pte =20 diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgta= ble.h index 290d5fbfe031..5a29153a4013 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -645,9 +645,8 @@ static inline pte_t __ptep_get_and_clear(struct mm_stru= ct *mm, return pte; } =20 -#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); } @@ -741,6 +740,8 @@ static inline pte_t ptep_get(pte_t *ptep) #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 =20 #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 0e2ca7327479..8963a4e77742 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 shi= ft, vm_flags_t flags) return entry; } =20 -void huge_ptep_set_wrprotect(struct mm_struct *mm, - unsigned long addr, - pte_t *ptep) -{ - pte_t pte =3D ptep_get(ptep); - pte_t orig_pte; - int pte_num; - - if (!pte_napot(pte)) { - ptep_set_wrprotect(mm, addr, ptep); - return; - } - - pte_num =3D arch_contpte_get_num_contig(mm, addr, ptep, 0, NULL); - - orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); - - orig_pte =3D 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/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h index fea47035ac38..02bce0ed93d8 100644 --- a/include/linux/hugetlb_contpte.h +++ b/include/linux/hugetlb_contpte.h @@ -27,4 +27,8 @@ extern int huge_ptep_set_access_flags(struct vm_area_stru= ct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty); =20 +#define __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT +extern void huge_ptep_set_wrprotect(struct mm_struct *mm, + unsigned long addr, pte_t *ptep); + #endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c index 49950c1ce615..de505350ef48 100644 --- a/mm/hugetlb_contpte.c +++ b/mm/hugetlb_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() */ =20 pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -232,3 +234,23 @@ int huge_ptep_set_access_flags(struct vm_area_struct *= vma, =20 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 =3D arch_contpte_get_num_contig(mm, addr, ptep, 0, &pgsize); + + pte =3D get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); + pte =3D pte_wrprotect(pte); + + set_contptes(mm, addr, ptep, pte, ncontig, pgsize); +} --=20 2.39.2