From nobody Thu Dec 18 14:51:40 2025 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 D04C21F8F09 for ; Fri, 21 Mar 2025 13:09:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562587; cv=none; b=FPMHCrl+n6uz3ptmkWTvSPbv+6YjYdSCSGvIBBMzh17mzT3MORWZKv5xsg6As6fVe0hMH56LCeFfDtFm/XOObWZqzGvKRWY1WHMgy+Ba7UUHHdhvp6HvtRmhC98sTxLHXsCsLPjkXoLUr7A4WPF0RBEwnJtd/81dBjpH+n236cw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742562587; c=relaxed/simple; bh=p3RauSFB+74ijXsM1H7UMKUd6CXeWNsI0FXkrs+Bl2Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mIh0zxw7jINbzomv2Hri22RmZzLSsK/FCJyPpg4npcna9U8LuihoETYi0wkmLT2UoKBNgQYSdsLu1wgDsXtxi9mCQwGkwArEAxahzKSfJgeQ5i8Uv6O/nJx9nU13FSDxKEs0WRwn2QKLfYKYQ6YlLBDHq4iw3g7W8aPj9hDc3e8= 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=HKRYh3W4; arc=none smtp.client-ip=209.85.128.45 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="HKRYh3W4" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-43cfa7e7f54so12055355e9.1 for ; Fri, 21 Mar 2025 06:09:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1742562583; x=1743167383; 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=e8EtFYpkeeZSqWY8m05eSUUviRVNOc3GBKbkMNHHDeA=; b=HKRYh3W4aNQ4oXGfpb7f5alz1+EUUrSbSUEq4EU7CiEGo47lcZ0pF3QSDM8QETX/AF WEYjlm7r+1/h+2oXq2jF4KJYoqtIM3+/vnhk/LLltHhUXFBnRLEjsCSrzZ03lpbqlNaz NuFYfigFdK+aTNyCBTq+CqhaLFpN5m/VbnXRWt0TOVRVZ/MiPK33rrX2qUxxHKXMl4s0 s+ZiW6lwFLayr9jVdTB8UuNwqlwo+efuxFgpFLmPQUr3jDL38OOzWj5LLL2k8PONH8u3 aVJrkYeZIaIofvSOvATU/jpu73xBzNKDlnZGcqR3LWpnPiqkJj5NYSGrClLk1LLAlaWq r1ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742562583; x=1743167383; 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=e8EtFYpkeeZSqWY8m05eSUUviRVNOc3GBKbkMNHHDeA=; b=ZgpH/t2KCGPRA2Jc8qfZ8Lj4+DrTkhE9Njn/oDlj58qr8jeU9/sxlYI2LeqGUt3u7g ghBMiDDxVVsSkaS8RyBjFx4/NGUk7iwok/SU9sxEVxzRgSedZAFLWBYRRFBjfCDRWoYU 8bPnn5cFxIahLUG/kn9thvJShHR3LSRHxDiMGJE4UC0RA3InTUimLmZFxXayZIxr/K+U HcUy6L0OPxcRHdZpbH+EhvZwFRyIw+iz3fos0yQ7SRlL6JTXyGvK8BBwD4TnRepMNcgQ ERyPZukg9gEVwZbsbKbQev9+QrpT0X9qcnhDq8E0cy1xxUor1uM0rLNlcJHj0AJYM8tE 3/kQ== X-Forwarded-Encrypted: i=1; AJvYcCWpoVlgYDhOzonh1qLNmCkr9OsC6ko+HNfSQxn4YzaF1d+7tMeRsDlTWBhDhBQHU7/zqH7d7xAit5gjVuk=@vger.kernel.org X-Gm-Message-State: AOJu0YwXGY0BTRMkyDVO53DwLEVoZ4EEoRo8s1Tu830ZmiUI3lPNJsTc TrRlx63KIIhzx5Fvojd23L1bxILREVA8X6KPxGtyRdeF78HAV1D/hPnQivczbuk= X-Gm-Gg: ASbGncs7uqG4HtHDg9zI5PcSEMv6Lqa0m/ZIYMC6fazVRv4nD9k58rnofqCgtaHEyDC LGMMFc/jZftfWyWXdmYAmuNhIH6ELudgfaYf9KWEfYPEe4dPQTP/nZMYhAv7JTiDKLh40fYkhaM AD+0LlHpP8b4LF8D/lBArOBRtT4/TXYYPzUFN7QqUBYObIj6Om3ZqqwzoOvU+pRkEBaOL8XKs5T M4FqRthJegieJJKDn4e+hD5uu/P9tc77w4JnQZfC0UrrNQ6HaKgfy/aoq6jMjWcL5dmWOmYy7u0 U5a42rtj/5Tre70sW2t3l6Ld286KVaNgKy2gZT/h6Tc+MySG/vkQN1ANI5jy7EoQIFzAdw== X-Google-Smtp-Source: AGHT+IFrlDybl2QKCVYqfxTyu64dXX62tEzPiiOD5kkpTcHrUIRBIEyVsyFQKXhsbfpJSLh/l0FWog== X-Received: by 2002:a05:600c:4e4b:b0:439:9737:675b with SMTP id 5b1f17b1804b1-43d502f8536mr33623225e9.7.1742562582938; Fri, 21 Mar 2025 06:09:42 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com ([2001:861:3382:ef90:3d12:52fe:c1cc:c94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3997f9a3c7fsm2377861f8f.34.2025.03.21.06.09.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Mar 2025 06:09:42 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Ryan Roberts , Mark Rutland , Matthew Wilcox , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , 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 v5 3/9] mm: Use common huge_ptep_get() function for riscv/arm64 Date: Fri, 21 Mar 2025 14:06:29 +0100 Message-Id: <20250321130635.227011-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250321130635.227011-1-alexghiti@rivosinc.com> References: <20250321130635.227011-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/Kconfig | 1 + arch/arm64/include/asm/hugetlb.h | 3 +- arch/arm64/include/asm/pgtable.h | 57 +++++++++++++++++++++++++-- arch/arm64/mm/hugetlbpage.c | 66 ++------------------------------ arch/riscv/Kconfig | 1 + arch/riscv/include/asm/hugetlb.h | 6 +-- arch/riscv/include/asm/pgtable.h | 45 ++++++++++++++++++++++ arch/riscv/mm/hugetlbpage.c | 62 +++--------------------------- include/linux/hugetlb_contpte.h | 12 ++++++ mm/Kconfig | 3 ++ mm/Makefile | 1 + mm/hugetlb_contpte.c | 32 ++++++++++++++++ 12 files changed, 161 insertions(+), 128 deletions(-) create mode 100644 include/linux/hugetlb_contpte.h create mode 100644 mm/hugetlb_contpte.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 940343beb3d4..5a1e1bc73c15 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -113,6 +113,7 @@ config ARM64 select ARCH_WANT_DEFAULT_BPF_JIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS + select ARCH_WANT_GENERAL_HUGETLB_CONTPTE select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && = !ARM64_VA_BITS_36) select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANTS_EXECMEM_LATE diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/huge= tlb.h index 07fbf5bf85a7..0604e01dca97 100644 --- a/arch/arm64/include/asm/hugetlb.h +++ b/arch/arm64/include/asm/hugetlb.h @@ -13,6 +13,7 @@ #include #include #include +#include =20 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION #define arch_hugetlb_migration_supported arch_hugetlb_migration_supported @@ -53,8 +54,6 @@ extern pte_t huge_ptep_clear_flush(struct vm_area_struct = *vma, #define __HAVE_ARCH_HUGE_PTE_CLEAR extern void huge_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz); -#define __HAVE_ARCH_HUGE_PTEP_GET -extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t= *ptep); =20 void __init arm64_hugetlb_cma_reserve(void); =20 diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgta= ble.h index 0b2a2ad1b9e8..af8156929c1d 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -420,9 +420,10 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigne= d long nr) return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte)); } =20 -static inline void __set_ptes(struct mm_struct *mm, - unsigned long __always_unused addr, - pte_t *ptep, pte_t pte, unsigned int nr) +static inline void ___set_ptes(struct mm_struct *mm, + unsigned long __always_unused addr, + pte_t *ptep, pte_t pte, unsigned int nr, + size_t pgsize) { page_table_check_ptes_set(mm, ptep, pte, nr); __sync_cache_and_tags(pte, nr); @@ -433,10 +434,15 @@ static inline void __set_ptes(struct mm_struct *mm, if (--nr =3D=3D 0) break; ptep++; - pte =3D pte_advance_pfn(pte, 1); + pte =3D pte_advance_pfn(pte, pgsize >> PAGE_SHIFT); } } =20 +#define __set_ptes(mm, addr, ptep, pte, nr) \ + ___set_ptes(mm, addr, ptep, pte, nr, PAGE_SIZE) + +#define set_contptes ___set_ptes + /* * Hugetlb definitions. */ @@ -1825,6 +1831,49 @@ static inline void clear_young_dirty_ptes(struct vm_= area_struct *vma, =20 #endif /* CONFIG_ARM64_CONTPTE */ =20 +static inline bool __hugetlb_valid_size(unsigned long size) +{ + switch (size) { +#ifndef __PAGETABLE_PMD_FOLDED + case PUD_SIZE: + return pud_sect_supported(); +#endif + case CONT_PMD_SIZE: + case PMD_SIZE: + case CONT_PTE_SIZE: + return true; + } + + return false; +} + +static inline int arch_contpte_get_num_contig(pte_t *ptep, + unsigned long size, + size_t *pgsize) +{ + int contig_ptes =3D 1; + + if (pgsize) + *pgsize =3D size; + + switch (size) { + case CONT_PMD_SIZE: + if (pgsize) + *pgsize =3D PMD_SIZE; + contig_ptes =3D CONT_PMDS; + break; + case CONT_PTE_SIZE: + if (pgsize) + *pgsize =3D PAGE_SIZE; + contig_ptes =3D CONT_PTES; + break; + default: + WARN_ON(!__hugetlb_valid_size(size)); + } + + return contig_ptes; +} + #endif /* !__ASSEMBLY__ */ =20 #endif /* __ASM_PGTABLE_H */ diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index b3a7fafe8892..60a2bb7575c1 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -49,22 +49,6 @@ void __init arm64_hugetlb_cma_reserve(void) } #endif /* CONFIG_CMA */ =20 -static bool __hugetlb_valid_size(unsigned long size) -{ - switch (size) { -#ifndef __PAGETABLE_PMD_FOLDED - case PUD_SIZE: - return pud_sect_supported(); -#endif - case CONT_PMD_SIZE: - case PMD_SIZE: - case CONT_PTE_SIZE: - return true; - } - - return false; -} - #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION bool arch_hugetlb_migration_supported(struct hstate *h) { @@ -98,50 +82,6 @@ static int find_num_contig(struct mm_struct *mm, unsigne= d long addr, return CONT_PTES; } =20 -static inline int num_contig_ptes(unsigned long size, size_t *pgsize) -{ - int contig_ptes =3D 1; - - *pgsize =3D size; - - switch (size) { - case CONT_PMD_SIZE: - *pgsize =3D PMD_SIZE; - contig_ptes =3D CONT_PMDS; - break; - case CONT_PTE_SIZE: - *pgsize =3D PAGE_SIZE; - contig_ptes =3D CONT_PTES; - break; - default: - WARN_ON(!__hugetlb_valid_size(size)); - } - - return contig_ptes; -} - -pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) -{ - int ncontig, i; - size_t pgsize; - pte_t orig_pte =3D __ptep_get(ptep); - - if (!pte_present(orig_pte) || !pte_cont(orig_pte)) - return orig_pte; - - ncontig =3D num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize); - for (i =3D 0; i < ncontig; i++, ptep++) { - pte_t pte =3D __ptep_get(ptep); - - if (pte_dirty(pte)) - orig_pte =3D pte_mkdirty(orig_pte); - - if (pte_young(pte)) - orig_pte =3D pte_mkyoung(orig_pte); - } - return orig_pte; -} - /* * Changing some bits of contiguous entries requires us to follow a * Break-Before-Make approach, breaking the whole contiguous set @@ -221,7 +161,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned lon= g addr, unsigned long pfn, dpfn; pgprot_t hugeprot; =20 - ncontig =3D num_contig_ptes(sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); =20 if (!pte_present(pte)) { for (i =3D 0; i < ncontig; i++, ptep++, addr +=3D pgsize) @@ -382,7 +322,7 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long= addr, int i, ncontig; size_t pgsize; =20 - ncontig =3D num_contig_ptes(sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); =20 for (i =3D 0; i < ncontig; i++, addr +=3D pgsize, ptep++) __pte_clear(mm, addr, ptep); @@ -394,7 +334,7 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, uns= igned long addr, int ncontig; size_t pgsize; =20 - ncontig =3D num_contig_ptes(sz, &pgsize); + ncontig =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); return get_clear_contig(mm, addr, ptep, pgsize, ncontig); } =20 diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 7612c52e9b1e..2a5b2a9f2816 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -79,6 +79,7 @@ config RISCV select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_GENERAL_HUGETLB if !RISCV_ISA_SVNAPOT + select ARCH_WANT_GENERAL_HUGETLB_CONTPTE if RISCV_ISA_SVNAPOT select ARCH_WANT_HUGE_PMD_SHARE if 64BIT select ARCH_WANT_LD_ORPHAN_WARN if !XIP_KERNEL select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP diff --git a/arch/riscv/include/asm/hugetlb.h b/arch/riscv/include/asm/huge= tlb.h index 446126497768..69393346ade0 100644 --- a/arch/riscv/include/asm/hugetlb.h +++ b/arch/riscv/include/asm/hugetlb.h @@ -4,6 +4,9 @@ =20 #include #include +#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB_CONTPTE +#include +#endif =20 static inline void arch_clear_hugetlb_flags(struct folio *folio) { @@ -44,9 +47,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte, int dirty); =20 -#define __HAVE_ARCH_HUGE_PTEP_GET -pte_t huge_ptep_get(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 2e62d7e607db..286fe1a32ded 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -296,6 +296,8 @@ static inline unsigned long pte_napot(pte_t pte) return pte_val(pte) & _PAGE_NAPOT; } =20 +#define pte_cont pte_napot + #define pte_valid_napot(pte) (pte_present(pte) && pte_napot(pte)) =20 /* @@ -606,6 +608,49 @@ static inline void ___set_ptes(struct mm_struct *mm, u= nsigned long addr, } } =20 +#ifdef CONFIG_RISCV_ISA_SVNAPOT +/* + * Some hugetlb functions can be called on !present ptes, so we must use t= he + * size parameter when it is passed. + */ +static inline int arch_contpte_get_num_contig(pte_t *ptep, unsigned long s= ize, + size_t *pgsize) +{ + unsigned long hugepage_shift; + pte_t __pte; + + if (size) { + if (size >=3D PGDIR_SIZE) + hugepage_shift =3D PGDIR_SHIFT; + else if (size >=3D P4D_SIZE) + hugepage_shift =3D P4D_SHIFT; + else if (size >=3D PUD_SIZE) + hugepage_shift =3D PUD_SHIFT; + else if (size >=3D PMD_SIZE) + hugepage_shift =3D PMD_SHIFT; + else + hugepage_shift =3D PAGE_SHIFT; + } else { + /* + * We must read the raw value of the pte to get the size of + * the mapping + */ + __pte =3D ___ptep_get(ptep); + + /* Make sure __pte is not a swap entry */ + BUG_ON(!pte_valid_napot(__pte)); + + hugepage_shift =3D PAGE_SHIFT; + size =3D napot_cont_size(napot_cont_order(__pte)); + } + + if (pgsize) + *pgsize =3D BIT(hugepage_shift); + + return size >> hugepage_shift; +} +#endif + static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { diff --git a/arch/riscv/mm/hugetlbpage.c b/arch/riscv/mm/hugetlbpage.c index 60b7e738b31a..b9eb6b7b214d 100644 --- a/arch/riscv/mm/hugetlbpage.c +++ b/arch/riscv/mm/hugetlbpage.c @@ -3,30 +3,6 @@ #include =20 #ifdef CONFIG_RISCV_ISA_SVNAPOT -pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) -{ - unsigned long pte_num; - int i; - pte_t orig_pte =3D ptep_get(ptep); - - if (!pte_present(orig_pte) || !pte_napot(orig_pte)) - return orig_pte; - - pte_num =3D napot_pte_num(napot_cont_order(orig_pte)); - - for (i =3D 0; i < pte_num; i++, ptep++) { - pte_t pte =3D ptep_get(ptep); - - if (pte_dirty(pte)) - orig_pte =3D pte_mkdirty(orig_pte); - - if (pte_young(pte)) - orig_pte =3D pte_mkyoung(orig_pte); - } - - return orig_pte; -} - pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, @@ -215,26 +191,6 @@ static void clear_flush(struct mm_struct *mm, flush_tlb_range(&vma, saddr, addr); } =20 -static int num_contig_ptes_from_size(unsigned long sz, size_t *pgsize) -{ - unsigned long hugepage_shift; - - if (sz >=3D PGDIR_SIZE) - hugepage_shift =3D PGDIR_SHIFT; - else if (sz >=3D P4D_SIZE) - hugepage_shift =3D P4D_SHIFT; - else if (sz >=3D PUD_SIZE) - hugepage_shift =3D PUD_SHIFT; - else if (sz >=3D PMD_SIZE) - hugepage_shift =3D PMD_SHIFT; - else - hugepage_shift =3D PAGE_SHIFT; - - *pgsize =3D 1 << hugepage_shift; - - return sz >> hugepage_shift; -} - /* * When dealing with NAPOT mappings, the privileged specification indicate= s that * "if an update needs to be made, the OS generally should first mark all = of the @@ -252,7 +208,7 @@ void set_huge_pte_at(struct mm_struct *mm, size_t pgsize; int i, pte_num; =20 - pte_num =3D num_contig_ptes_from_size(sz, &pgsize); + pte_num =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); =20 if (!pte_present(pte)) { for (i =3D 0; i < pte_num; i++, ptep++, addr +=3D pgsize) @@ -277,15 +233,13 @@ int huge_ptep_set_access_flags(struct vm_area_struct = *vma, int dirty) { struct mm_struct *mm =3D vma->vm_mm; - unsigned long order; pte_t orig_pte; int pte_num; =20 if (!pte_napot(pte)) return ptep_set_access_flags(vma, addr, ptep, pte, dirty); =20 - order =3D napot_cont_order(pte); - pte_num =3D napot_pte_num(order); + pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 if (pte_dirty(orig_pte)) @@ -303,14 +257,13 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz) { - size_t pgsize; pte_t orig_pte =3D ptep_get(ptep); int pte_num; =20 if (!pte_napot(orig_pte)) return ptep_get_and_clear(mm, addr, ptep); =20 - pte_num =3D num_contig_ptes_from_size(sz, &pgsize); + pte_num =3D arch_contpte_get_num_contig(ptep, sz, NULL); =20 return get_clear_contig(mm, addr, ptep, pte_num); } @@ -320,7 +273,6 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pte_t *ptep) { pte_t pte =3D ptep_get(ptep); - unsigned long order; pte_t orig_pte; int pte_num; =20 @@ -329,8 +281,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, return; } =20 - order =3D napot_cont_order(pte); - pte_num =3D napot_pte_num(order); + pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); orig_pte =3D get_clear_contig_flush(mm, addr, ptep, pte_num); =20 orig_pte =3D pte_wrprotect(orig_pte); @@ -348,7 +299,7 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, if (!pte_napot(pte)) return ptep_clear_flush(vma, addr, ptep); =20 - pte_num =3D napot_pte_num(napot_cont_order(pte)); + pte_num =3D arch_contpte_get_num_contig(ptep, 0, NULL); =20 return get_clear_contig_flush(vma->vm_mm, addr, ptep, pte_num); } @@ -367,8 +318,7 @@ void huge_pte_clear(struct mm_struct *mm, return; } =20 - pte_num =3D num_contig_ptes_from_size(sz, &pgsize); - + pte_num =3D arch_contpte_get_num_contig(ptep, sz, &pgsize); for (i =3D 0; i < pte_num; i++, addr +=3D pgsize, ptep++) pte_clear(mm, addr, ptep); } diff --git a/include/linux/hugetlb_contpte.h b/include/linux/hugetlb_contpt= e.h new file mode 100644 index 000000000000..2ea17e4fe36b --- /dev/null +++ b/include/linux/hugetlb_contpte.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2025 Rivos Inc. + */ + +#ifndef _LINUX_HUGETLB_CONTPTE_H +#define _LINUX_HUGETLB_CONTPTE_H + +#define __HAVE_ARCH_HUGE_PTEP_GET +extern pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t= *ptep); + +#endif /* _LINUX_HUGETLB_CONTPTE_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 1b501db06417..f9d3f3d49f3e 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -853,6 +853,9 @@ config NOMMU_INITIAL_TRIM_EXCESS config ARCH_WANT_GENERAL_HUGETLB bool =20 +config ARCH_WANT_GENERAL_HUGETLB_CONTPTE + bool + config ARCH_WANTS_THP_SWAP def_bool n =20 diff --git a/mm/Makefile b/mm/Makefile index 850386a67b3e..76e8b995f551 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -96,6 +96,7 @@ obj-$(CONFIG_MIGRATION) +=3D migrate.o obj-$(CONFIG_NUMA) +=3D memory-tiers.o obj-$(CONFIG_DEVICE_MIGRATION) +=3D migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) +=3D huge_memory.o khugepaged.o +obj-$(CONFIG_ARCH_WANT_GENERAL_HUGETLB_CONTPTE) +=3D hugetlb_contpte.o obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.o obj-$(CONFIG_MEMCG_V1) +=3D memcontrol-v1.o obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o diff --git a/mm/hugetlb_contpte.c b/mm/hugetlb_contpte.c new file mode 100644 index 000000000000..500d0b96a680 --- /dev/null +++ b/mm/hugetlb_contpte.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2025 Rivos Inc. + */ + +#include +#include +#include + +pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) +{ + int ncontig, i; + pte_t orig_pte =3D __ptep_get(ptep); + + if (!pte_present(orig_pte) || !pte_cont(orig_pte)) + return orig_pte; + + ncontig =3D arch_contpte_get_num_contig(ptep, + page_size(pte_page(orig_pte)), + NULL); + + for (i =3D 0; i < ncontig; i++, ptep++) { + pte_t pte =3D __ptep_get(ptep); + + if (pte_dirty(pte)) + orig_pte =3D pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte =3D pte_mkyoung(orig_pte); + } + return orig_pte; +} --=20 2.39.2