From patchwork Mon Jan 27 09:35:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13951131 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 D0204C0218C for ; Mon, 27 Jan 2025 09:43:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AA25280141; Mon, 27 Jan 2025 04:43:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6338728013F; Mon, 27 Jan 2025 04:43:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4AD58280141; Mon, 27 Jan 2025 04:43:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2A22828013F for ; Mon, 27 Jan 2025 04:43:56 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DA56E1A213D for ; Mon, 27 Jan 2025 09:43:55 +0000 (UTC) X-FDA: 83052745230.14.89E7B47 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by imf07.hostedemail.com (Postfix) with ESMTP id DFDA140002 for ; Mon, 27 Jan 2025 09:43:53 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xT4n3iaK; spf=pass (imf07.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.43 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737971034; 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=C5hVtVN5ts8qRfTru3U6IxaXR2nRBQvNb9/UQuUojb8=; b=vRzhYT04RC/5DfGufKZrgPRjc1CSH0dJj66B12Yd4/B2JVCg3TY7rz2bTdbGs3KksWLKsS YcYFMJK74dUP6P/s28omS4ewqq6LOqOCmY+SuQ+O4BiSShPwKNhYi9LbG9dGRvChHHIilS xTGZ8H/AZtRQ0hLEonEifZ7yNvf+kmc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xT4n3iaK; spf=pass (imf07.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.43 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737971034; a=rsa-sha256; cv=none; b=XaheYWCyp4HXU28A6DKbsZJLJb/QOuutjEL+Pdymj7C6vHzysyZW+By49tFhyPTt2CCRt2 odo8VgIgYoG7POexOcsPIAWGKVwGt6R2HloWDbJ67FjTkQgGk77pr1DQsBjEGtbAoC5ZLW KWQ8NJuC9AVULZLPmlWbyUJzdXIqgtU= Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-388cae9eb9fso2395011f8f.3 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=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=C5hVtVN5ts8qRfTru3U6IxaXR2nRBQvNb9/UQuUojb8=; b=xT4n3iaK7VJwhxRtIng+lflfZIhZR02Dsd6endAFAv3abuUHRMo6CNiqsUeCgSej58 FNw6+jw3vdhz1zQo7Azo2B3r1vI/sJC47Iuc3hCbepmRheahLyKIcc8ZOfC01vzc8+4M wT7veFhc5XINfgXwcGIVohF16YSkQbPAtmqeSpJIRpJ1YYOlttrqL8QUUoOulysh2xZw bfT7g1lD+ja1oBC1wnnWDj6BYzeMDmydbozr4AipNFdIytxWo6vjr+OA7WrzAEZty5LE 3f6N/RVHuQ/i8k3UqCSTgomU116ZNMDj3sjmv/hnredo/Hrp+bTW9j4hl5QNWVN86XQR Jg+Q== 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=t8h7QyjaCNNI/qhFbaBWjD9kycQVa2D9lq5a4f4cJlu9HrNGZ5Gfa2VM8wShxyIEC0 dcEz0qRrM3nw1JukDZMWykYnfJs9S25HyU9XIbUgMLSodxYS01mstKqoEKu1nEZaIj1B FbrI4uB4jEy9SCDJems6I5n74Q7YFPT4fh+Jcmpojk4K10k+n/QjD7HgUKGHDAgVUuE/ 44K3XEdsOAvud0L6PzlXOTta9IqakdZ0sZZdF35w90dkNLONKyqWYvkcL3hBCCl5wWiu v11stsxBQ/eq56eykAHdBhjY4YQK3RcJiX3xF8/Z/UYk3+n/ENXCv6MlzsZIR5Qt+/z1 9exg== X-Forwarded-Encrypted: i=1; AJvYcCXifsIVPKgXaLcvFGlzJ/9bEOxUOJEOyyZ7ygEq1zHE7fHOnD4zcds22lV0bzTwiff/g9+dKbfpYg==@kvack.org X-Gm-Message-State: AOJu0YzVwUWdgQZtzkB2eg17OJ809ztdKBSgDVNqp0aBYYViGwfmrctJ SKwuJNL/rGZ8PRh+XEnyERe72yKdtlc5WexWzaBP9fhteRtz+w4jfoAJB7mGva8= X-Gm-Gg: ASbGnctPQaKqbeKjcNujDQrMiFh2TGZCsI8Ha70K3xbCWsfHq1Ac5iBrSwJo5Au+TKM VdPh0patuzP5QqzgJbuVy9fcwOnSvON+GFg2i9+1DthEMHsIG/iLfZL7ricjymEDJXR8k1NOZ7T wvV4gd2l/VgWr2lVXIri4XRqLiXJbWdlKs0dfd3FcN/WhUEeaV5eDcrd9z7L2bjQUDc7X0zP0Tr 38fgf3njnx9MgQm5cPl9MIrNvDZUm4rPSBeN58GHypuoh4ViN/g9H4fvAD4IT+LcVSJKxpziEwi DpF3ApSRRDJ5Qs3FSMC/yr30SzY2kXQ= 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> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DFDA140002 X-Stat-Signature: eqbnr45bynpzai47wqpwoms7jsqtxe9a X-Rspam-User: X-HE-Tag: 1737971033-15454 X-HE-Meta: U2FsdGVkX1/3E68qgZlLp0QzhsW7A2JDBU9GznJxf5U0MjggHOWi3fLlPQMU70LOy1i0QFxOgc9WOFJUGvfGrE7Ped72R/tHRxCHmzIIGFn4ogeNZROrRUVTNDe5VrYfjMuE4KwlHqjmBebq/90qEa2wA7hAAL4UCZMXddb7rBBUynFdX6BhxcQcMB9/fm1fJawUTb5Bw+VHHCZmef6GRM9919+r1tDPBXAzYj85qz6QQ/ZxnfMQ4szaGmr0APMfXeE/77ATttBNelBnduJEBZG7coEif1001EvZ9MueZN5RxuRgob6CnmGd5pYz7Jl/kITF7N0CedhGTvOR1fAw5UjFNV4UP0v1TXVP09+uIlttiBhrHjM4rt68F8FoKAJyEnnsyU4XdjWQMvrZBGAQALs8YzcTOANOjXwdNBsOL8261xDfxo4bBuj3dnLs4I72rYtSLzrTCCsAuDjFA9tFkJRHWxDFkDwC0dnLay0Yru24xGRmek6T+XOne2o0MYi/99NI/e/F3ves+dU4YbQ0TRyQPiP8rb/xmRQ7VGnLKl8sTsBOgQuEHG6v8BJ5X/pmFLl9MRoor/oaZRkc37FuXvYV/Qsd65ZY8kL2yON0BgmO4we4LYbgDFi2dnsUq2QLg3eqzObagmpCT376lROJ0E86fiXkTwLTUOB4qH5KgcUX5kasM7JBcxuwRdhWpncElsQGhD3d0XGZCqUAg73TIr+LKaceOH/JscU39AvJhhJMBRKbTisLKK0Anq6vgFqrZEhTQBQ3LSuGalqYmml5KyBpZM4xs+GYclLilSzFr80dwxV+nr83nZLdJcBMxkb6fKyzOx15gPyFYvY+ebxf5xc44gthGDbeDx/ETAXqUEFyu+cmISMPmw301vAC147filOVWO7yencCIzADaHNXSnwPH3L4SWSH1ZBuK9Eg7aFK0jeOIMq2J/YeOhzldwbrY57YA461eidLyfsRPhY 1eeFuMu1 Z9t5ce/1wpJYF7DG8TGD58x7BCE/VcqA3nHt+Jk8DRsu0Nq0LnKdHP9aXa4muX4NqYf1iLJKH/r2qJYC3WcMdCmDmDwPeVvusw1t3Fq+/y9MmSsATtK0xRNKEmalCtWswr2UYZxychzrvyt2dqseRcSYgbeH4i6h2y2Q3nCkgvKXKZiOt1nnNmHetPwI5ng143/BF7OLUZ/w1t5zienRBFCsoAaSQX88QMDHAx0KnVz6yEg87JDq6Wv2UHHi7k4F8DKb2wSG2cnG77UU80+8/0yfsInpjcRpHklrV7+n1M0rrPZcMQsKdSyc2GAXTS20ntu2jJg1oniyAcDzFtAEzNdpQoDGFXXVM3RhsIOTR1h/9QcVA1MkISOyawQ5k5+vNEmnThEAl927ggWeOtk7anF294AeGwzTS356bclZV14c3nOjztjyX30P3flvaUO6S8k5KpobivRmcuI8A1aBr1gvz8g== 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: 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/hugetlb.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) 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 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/hugetlb.h b/arch/riscv/include/asm/hugetlb.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); -#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 diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.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_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); } @@ -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 #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 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); - 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, NULL); - - 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/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpte.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_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty); +#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() */ 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, 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); +}