Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * arch/arm64/mm/hugetlbpage.c
  3 *
  4 * Copyright (C) 2013 Linaro Ltd.
  5 *
  6 * Based on arch/x86/mm/hugetlbpage.c.
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License version 2 as
 10 * published by the Free Software Foundation.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 */
 17
 18#include <linux/init.h>
 19#include <linux/fs.h>
 20#include <linux/mm.h>
 21#include <linux/hugetlb.h>
 22#include <linux/pagemap.h>
 23#include <linux/err.h>
 24#include <linux/sysctl.h>
 25#include <asm/mman.h>
 26#include <asm/tlb.h>
 27#include <asm/tlbflush.h>
 28#include <asm/pgalloc.h>
 29
 30int pmd_huge(pmd_t pmd)
 31{
 32	return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
 33}
 34
 35int pud_huge(pud_t pud)
 36{
 37#ifndef __PAGETABLE_PMD_FOLDED
 38	return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
 39#else
 40	return 0;
 41#endif
 42}
 43
 
 
 
 
 
 
 
 
 
 
 44static int find_num_contig(struct mm_struct *mm, unsigned long addr,
 45			   pte_t *ptep, pte_t pte, size_t *pgsize)
 46{
 47	pgd_t *pgd = pgd_offset(mm, addr);
 48	pud_t *pud;
 49	pmd_t *pmd;
 50
 51	*pgsize = PAGE_SIZE;
 52	if (!pte_cont(pte))
 53		return 1;
 54	if (!pgd_present(*pgd)) {
 55		VM_BUG_ON(!pgd_present(*pgd));
 56		return 1;
 57	}
 58	pud = pud_offset(pgd, addr);
 59	if (!pud_present(*pud)) {
 60		VM_BUG_ON(!pud_present(*pud));
 61		return 1;
 62	}
 63	pmd = pmd_offset(pud, addr);
 64	if (!pmd_present(*pmd)) {
 65		VM_BUG_ON(!pmd_present(*pmd));
 66		return 1;
 67	}
 68	if ((pte_t *)pmd == ptep) {
 69		*pgsize = PMD_SIZE;
 70		return CONT_PMDS;
 71	}
 72	return CONT_PTES;
 73}
 74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 75void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
 76			    pte_t *ptep, pte_t pte)
 77{
 78	size_t pgsize;
 79	int i;
 80	int ncontig = find_num_contig(mm, addr, ptep, pte, &pgsize);
 81	unsigned long pfn;
 82	pgprot_t hugeprot;
 83
 84	if (ncontig == 1) {
 
 
 
 
 
 
 85		set_pte_at(mm, addr, ptep, pte);
 86		return;
 87	}
 88
 
 89	pfn = pte_pfn(pte);
 90	hugeprot = __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
 91	for (i = 0; i < ncontig; i++) {
 92		pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
 93			 pte_val(pfn_pte(pfn, hugeprot)));
 
 
 94		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
 95		ptep++;
 96		pfn += pgsize >> PAGE_SHIFT;
 97		addr += pgsize;
 98	}
 
 
 
 
 
 
 
 
 99}
100
101pte_t *huge_pte_alloc(struct mm_struct *mm,
102		      unsigned long addr, unsigned long sz)
103{
104	pgd_t *pgd;
105	pud_t *pud;
106	pte_t *pte = NULL;
107
108	pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
109	pgd = pgd_offset(mm, addr);
110	pud = pud_alloc(mm, pgd, addr);
111	if (!pud)
112		return NULL;
113
114	if (sz == PUD_SIZE) {
115		pte = (pte_t *)pud;
116	} else if (sz == (PAGE_SIZE * CONT_PTES)) {
117		pmd_t *pmd = pmd_alloc(mm, pud, addr);
118
119		WARN_ON(addr & (sz - 1));
120		/*
121		 * Note that if this code were ever ported to the
122		 * 32-bit arm platform then it will cause trouble in
123		 * the case where CONFIG_HIGHPTE is set, since there
124		 * will be no pte_unmap() to correspond with this
125		 * pte_alloc_map().
126		 */
127		pte = pte_alloc_map(mm, pmd, addr);
128	} else if (sz == PMD_SIZE) {
129		if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
130		    pud_none(*pud))
131			pte = huge_pmd_share(mm, addr, pud);
132		else
133			pte = (pte_t *)pmd_alloc(mm, pud, addr);
134	} else if (sz == (PMD_SIZE * CONT_PMDS)) {
135		pmd_t *pmd;
136
137		pmd = pmd_alloc(mm, pud, addr);
138		WARN_ON(addr & (sz - 1));
139		return (pte_t *)pmd;
140	}
141
142	pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
143	       sz, pte, pte_val(*pte));
144	return pte;
145}
146
147pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 
148{
149	pgd_t *pgd;
150	pud_t *pud;
151	pmd_t *pmd = NULL;
152	pte_t *pte = NULL;
153
154	pgd = pgd_offset(mm, addr);
155	pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
156	if (!pgd_present(*pgd))
157		return NULL;
158	pud = pud_offset(pgd, addr);
159	if (!pud_present(*pud))
160		return NULL;
161
162	if (pud_huge(*pud))
163		return (pte_t *)pud;
164	pmd = pmd_offset(pud, addr);
165	if (!pmd_present(*pmd))
 
 
 
 
 
 
 
 
 
 
 
 
166		return NULL;
 
 
 
 
 
167
168	if (pte_cont(pmd_pte(*pmd))) {
169		pmd = pmd_offset(
170			pud, (addr & CONT_PMD_MASK));
171		return (pte_t *)pmd;
172	}
173	if (pmd_huge(*pmd))
174		return (pte_t *)pmd;
175	pte = pte_offset_kernel(pmd, addr);
176	if (pte_present(*pte) && pte_cont(*pte)) {
177		pte = pte_offset_kernel(
178			pmd, (addr & CONT_PTE_MASK));
179		return pte;
180	}
181	return NULL;
182}
183
184pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
185			 struct page *page, int writable)
186{
187	size_t pagesize = huge_page_size(hstate_vma(vma));
188
189	if (pagesize == CONT_PTE_SIZE) {
190		entry = pte_mkcont(entry);
191	} else if (pagesize == CONT_PMD_SIZE) {
192		entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
193	} else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
194		pr_warn("%s: unrecognized huge page size 0x%lx\n",
195			__func__, pagesize);
196	}
197	return entry;
198}
199
 
 
 
 
 
 
 
 
 
 
 
 
200pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
201			      unsigned long addr, pte_t *ptep)
202{
203	pte_t pte;
 
 
204
205	if (pte_cont(*ptep)) {
206		int ncontig, i;
207		size_t pgsize;
208		pte_t *cpte;
209		bool is_dirty = false;
210
211		cpte = huge_pte_offset(mm, addr);
212		ncontig = find_num_contig(mm, addr, cpte, *cpte, &pgsize);
213		/* save the 1st pte to return */
214		pte = ptep_get_and_clear(mm, addr, cpte);
215		for (i = 1; i < ncontig; ++i) {
216			/*
217			 * If HW_AFDBM is enabled, then the HW could
218			 * turn on the dirty bit for any of the page
219			 * in the set, so check them all.
220			 */
221			++cpte;
222			if (pte_dirty(ptep_get_and_clear(mm, addr, cpte)))
223				is_dirty = true;
224		}
225		if (is_dirty)
226			return pte_mkdirty(pte);
227		else
228			return pte;
229	} else {
230		return ptep_get_and_clear(mm, addr, ptep);
231	}
 
 
 
232}
233
234int huge_ptep_set_access_flags(struct vm_area_struct *vma,
235			       unsigned long addr, pte_t *ptep,
236			       pte_t pte, int dirty)
237{
238	pte_t *cpte;
 
 
 
 
239
240	if (pte_cont(pte)) {
241		int ncontig, i, changed = 0;
242		size_t pgsize = 0;
243		unsigned long pfn = pte_pfn(pte);
244		/* Select all bits except the pfn */
245		pgprot_t hugeprot =
246			__pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^
247				 pte_val(pte));
248
249		cpte = huge_pte_offset(vma->vm_mm, addr);
250		pfn = pte_pfn(*cpte);
251		ncontig = find_num_contig(vma->vm_mm, addr, cpte,
252					  *cpte, &pgsize);
253		for (i = 0; i < ncontig; ++i, ++cpte) {
254			changed = ptep_set_access_flags(vma, addr, cpte,
255							pfn_pte(pfn,
256								hugeprot),
257							dirty);
258			pfn += pgsize >> PAGE_SHIFT;
259		}
260		return changed;
261	} else {
262		return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
263	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264}
265
266void huge_ptep_set_wrprotect(struct mm_struct *mm,
267			     unsigned long addr, pte_t *ptep)
268{
269	if (pte_cont(*ptep)) {
270		int ncontig, i;
271		pte_t *cpte;
272		size_t pgsize = 0;
273
274		cpte = huge_pte_offset(mm, addr);
275		ncontig = find_num_contig(mm, addr, cpte, *cpte, &pgsize);
276		for (i = 0; i < ncontig; ++i, ++cpte)
277			ptep_set_wrprotect(mm, addr, cpte);
278	} else {
279		ptep_set_wrprotect(mm, addr, ptep);
 
280	}
 
 
 
 
 
 
 
 
 
 
 
 
281}
282
283void huge_ptep_clear_flush(struct vm_area_struct *vma,
284			   unsigned long addr, pte_t *ptep)
285{
286	if (pte_cont(*ptep)) {
287		int ncontig, i;
288		pte_t *cpte;
289		size_t pgsize = 0;
290
291		cpte = huge_pte_offset(vma->vm_mm, addr);
292		ncontig = find_num_contig(vma->vm_mm, addr, cpte,
293					  *cpte, &pgsize);
294		for (i = 0; i < ncontig; ++i, ++cpte)
295			ptep_clear_flush(vma, addr, cpte);
296	} else {
297		ptep_clear_flush(vma, addr, ptep);
 
298	}
 
 
 
299}
300
301static __init int setup_hugepagesz(char *opt)
302{
303	unsigned long ps = memparse(opt, &opt);
304
305	if (ps == PMD_SIZE) {
306		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
307	} else if (ps == PUD_SIZE) {
308		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
309	} else {
310		pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
311		return 0;
 
 
312	}
313	return 1;
 
 
 
314}
315__setup("hugepagesz=", setup_hugepagesz);
v4.17
  1/*
  2 * arch/arm64/mm/hugetlbpage.c
  3 *
  4 * Copyright (C) 2013 Linaro Ltd.
  5 *
  6 * Based on arch/x86/mm/hugetlbpage.c.
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License version 2 as
 10 * published by the Free Software Foundation.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 */
 17
 18#include <linux/init.h>
 19#include <linux/fs.h>
 20#include <linux/mm.h>
 21#include <linux/hugetlb.h>
 22#include <linux/pagemap.h>
 23#include <linux/err.h>
 24#include <linux/sysctl.h>
 25#include <asm/mman.h>
 26#include <asm/tlb.h>
 27#include <asm/tlbflush.h>
 28#include <asm/pgalloc.h>
 29
 30int pmd_huge(pmd_t pmd)
 31{
 32	return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
 33}
 34
 35int pud_huge(pud_t pud)
 36{
 37#ifndef __PAGETABLE_PMD_FOLDED
 38	return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
 39#else
 40	return 0;
 41#endif
 42}
 43
 44/*
 45 * Select all bits except the pfn
 46 */
 47static inline pgprot_t pte_pgprot(pte_t pte)
 48{
 49	unsigned long pfn = pte_pfn(pte);
 50
 51	return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
 52}
 53
 54static int find_num_contig(struct mm_struct *mm, unsigned long addr,
 55			   pte_t *ptep, size_t *pgsize)
 56{
 57	pgd_t *pgdp = pgd_offset(mm, addr);
 58	pud_t *pudp;
 59	pmd_t *pmdp;
 60
 61	*pgsize = PAGE_SIZE;
 62	pudp = pud_offset(pgdp, addr);
 63	pmdp = pmd_offset(pudp, addr);
 64	if ((pte_t *)pmdp == ptep) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 65		*pgsize = PMD_SIZE;
 66		return CONT_PMDS;
 67	}
 68	return CONT_PTES;
 69}
 70
 71static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
 72{
 73	int contig_ptes = 0;
 74
 75	*pgsize = size;
 76
 77	switch (size) {
 78#ifdef CONFIG_ARM64_4K_PAGES
 79	case PUD_SIZE:
 80#endif
 81	case PMD_SIZE:
 82		contig_ptes = 1;
 83		break;
 84	case CONT_PMD_SIZE:
 85		*pgsize = PMD_SIZE;
 86		contig_ptes = CONT_PMDS;
 87		break;
 88	case CONT_PTE_SIZE:
 89		*pgsize = PAGE_SIZE;
 90		contig_ptes = CONT_PTES;
 91		break;
 92	}
 93
 94	return contig_ptes;
 95}
 96
 97/*
 98 * Changing some bits of contiguous entries requires us to follow a
 99 * Break-Before-Make approach, breaking the whole contiguous set
100 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
101 * "Misprogramming of the Contiguous bit", page D4-1762.
102 *
103 * This helper performs the break step.
104 */
105static pte_t get_clear_flush(struct mm_struct *mm,
106			     unsigned long addr,
107			     pte_t *ptep,
108			     unsigned long pgsize,
109			     unsigned long ncontig)
110{
111	struct vm_area_struct vma = { .vm_mm = mm };
112	pte_t orig_pte = huge_ptep_get(ptep);
113	bool valid = pte_valid(orig_pte);
114	unsigned long i, saddr = addr;
115
116	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
117		pte_t pte = ptep_get_and_clear(mm, addr, ptep);
118
119		/*
120		 * If HW_AFDBM is enabled, then the HW could turn on
121		 * the dirty bit for any page in the set, so check
122		 * them all.  All hugetlb entries are already young.
123		 */
124		if (pte_dirty(pte))
125			orig_pte = pte_mkdirty(orig_pte);
126	}
127
128	if (valid)
129		flush_tlb_range(&vma, saddr, addr);
130	return orig_pte;
131}
132
133/*
134 * Changing some bits of contiguous entries requires us to follow a
135 * Break-Before-Make approach, breaking the whole contiguous set
136 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
137 * "Misprogramming of the Contiguous bit", page D4-1762.
138 *
139 * This helper performs the break step for use cases where the
140 * original pte is not needed.
141 */
142static void clear_flush(struct mm_struct *mm,
143			     unsigned long addr,
144			     pte_t *ptep,
145			     unsigned long pgsize,
146			     unsigned long ncontig)
147{
148	struct vm_area_struct vma = { .vm_mm = mm };
149	unsigned long i, saddr = addr;
150
151	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
152		pte_clear(mm, addr, ptep);
153
154	flush_tlb_range(&vma, saddr, addr);
155}
156
157void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
158			    pte_t *ptep, pte_t pte)
159{
160	size_t pgsize;
161	int i;
162	int ncontig;
163	unsigned long pfn, dpfn;
164	pgprot_t hugeprot;
165
166	/*
167	 * Code needs to be expanded to handle huge swap and migration
168	 * entries. Needed for HUGETLB and MEMORY_FAILURE.
169	 */
170	WARN_ON(!pte_present(pte));
171
172	if (!pte_cont(pte)) {
173		set_pte_at(mm, addr, ptep, pte);
174		return;
175	}
176
177	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
178	pfn = pte_pfn(pte);
179	dpfn = pgsize >> PAGE_SHIFT;
180	hugeprot = pte_pgprot(pte);
181
182	clear_flush(mm, addr, ptep, pgsize, ncontig);
183
184	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
185		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
186}
187
188void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
189			  pte_t *ptep, pte_t pte, unsigned long sz)
190{
191	int i, ncontig;
192	size_t pgsize;
193
194	ncontig = num_contig_ptes(sz, &pgsize);
195
196	for (i = 0; i < ncontig; i++, ptep++)
197		set_pte(ptep, pte);
198}
199
200pte_t *huge_pte_alloc(struct mm_struct *mm,
201		      unsigned long addr, unsigned long sz)
202{
203	pgd_t *pgdp;
204	pud_t *pudp;
205	pmd_t *pmdp;
206	pte_t *ptep = NULL;
207
208	pgdp = pgd_offset(mm, addr);
209	pudp = pud_alloc(mm, pgdp, addr);
210	if (!pudp)
211		return NULL;
212
213	if (sz == PUD_SIZE) {
214		ptep = (pte_t *)pudp;
215	} else if (sz == (PAGE_SIZE * CONT_PTES)) {
216		pmdp = pmd_alloc(mm, pudp, addr);
217
218		WARN_ON(addr & (sz - 1));
219		/*
220		 * Note that if this code were ever ported to the
221		 * 32-bit arm platform then it will cause trouble in
222		 * the case where CONFIG_HIGHPTE is set, since there
223		 * will be no pte_unmap() to correspond with this
224		 * pte_alloc_map().
225		 */
226		ptep = pte_alloc_map(mm, pmdp, addr);
227	} else if (sz == PMD_SIZE) {
228		if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
229		    pud_none(READ_ONCE(*pudp)))
230			ptep = huge_pmd_share(mm, addr, pudp);
231		else
232			ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
233	} else if (sz == (PMD_SIZE * CONT_PMDS)) {
234		pmdp = pmd_alloc(mm, pudp, addr);
 
 
235		WARN_ON(addr & (sz - 1));
236		return (pte_t *)pmdp;
237	}
238
239	return ptep;
 
 
240}
241
242pte_t *huge_pte_offset(struct mm_struct *mm,
243		       unsigned long addr, unsigned long sz)
244{
245	pgd_t *pgdp;
246	pud_t *pudp, pud;
247	pmd_t *pmdp, pmd;
 
248
249	pgdp = pgd_offset(mm, addr);
250	if (!pgd_present(READ_ONCE(*pgdp)))
 
 
 
 
251		return NULL;
252
253	pudp = pud_offset(pgdp, addr);
254	pud = READ_ONCE(*pudp);
255	if (sz != PUD_SIZE && pud_none(pud))
256		return NULL;
257	/* hugepage or swap? */
258	if (pud_huge(pud) || !pud_present(pud))
259		return (pte_t *)pudp;
260	/* table; check the next level */
261
262	if (sz == CONT_PMD_SIZE)
263		addr &= CONT_PMD_MASK;
264
265	pmdp = pmd_offset(pudp, addr);
266	pmd = READ_ONCE(*pmdp);
267	if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
268	    pmd_none(pmd))
269		return NULL;
270	if (pmd_huge(pmd) || !pmd_present(pmd))
271		return (pte_t *)pmdp;
272
273	if (sz == CONT_PTE_SIZE)
274		return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK));
275
 
 
 
 
 
 
 
 
 
 
 
 
 
276	return NULL;
277}
278
279pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
280			 struct page *page, int writable)
281{
282	size_t pagesize = huge_page_size(hstate_vma(vma));
283
284	if (pagesize == CONT_PTE_SIZE) {
285		entry = pte_mkcont(entry);
286	} else if (pagesize == CONT_PMD_SIZE) {
287		entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
288	} else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
289		pr_warn("%s: unrecognized huge page size 0x%lx\n",
290			__func__, pagesize);
291	}
292	return entry;
293}
294
295void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
296		    pte_t *ptep, unsigned long sz)
297{
298	int i, ncontig;
299	size_t pgsize;
300
301	ncontig = num_contig_ptes(sz, &pgsize);
302
303	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
304		pte_clear(mm, addr, ptep);
305}
306
307pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
308			      unsigned long addr, pte_t *ptep)
309{
310	int ncontig;
311	size_t pgsize;
312	pte_t orig_pte = huge_ptep_get(ptep);
313
314	if (!pte_cont(orig_pte))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315		return ptep_get_and_clear(mm, addr, ptep);
316
317	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
318
319	return get_clear_flush(mm, addr, ptep, pgsize, ncontig);
320}
321
322int huge_ptep_set_access_flags(struct vm_area_struct *vma,
323			       unsigned long addr, pte_t *ptep,
324			       pte_t pte, int dirty)
325{
326	int ncontig, i, changed = 0;
327	size_t pgsize = 0;
328	unsigned long pfn = pte_pfn(pte), dpfn;
329	pgprot_t hugeprot;
330	pte_t orig_pte;
331
332	if (!pte_cont(pte))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333		return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
334
335	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
336	dpfn = pgsize >> PAGE_SHIFT;
337
338	orig_pte = get_clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
339	if (!pte_same(orig_pte, pte))
340		changed = 1;
341
342	/* Make sure we don't lose the dirty state */
343	if (pte_dirty(orig_pte))
344		pte = pte_mkdirty(pte);
345
346	hugeprot = pte_pgprot(pte);
347	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
348		set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
349
350	return changed;
351}
352
353void huge_ptep_set_wrprotect(struct mm_struct *mm,
354			     unsigned long addr, pte_t *ptep)
355{
356	unsigned long pfn, dpfn;
357	pgprot_t hugeprot;
358	int ncontig, i;
359	size_t pgsize;
360	pte_t pte;
361
362	if (!pte_cont(READ_ONCE(*ptep))) {
 
 
 
363		ptep_set_wrprotect(mm, addr, ptep);
364		return;
365	}
366
367	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
368	dpfn = pgsize >> PAGE_SHIFT;
369
370	pte = get_clear_flush(mm, addr, ptep, pgsize, ncontig);
371	pte = pte_wrprotect(pte);
372
373	hugeprot = pte_pgprot(pte);
374	pfn = pte_pfn(pte);
375
376	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
377		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
378}
379
380void huge_ptep_clear_flush(struct vm_area_struct *vma,
381			   unsigned long addr, pte_t *ptep)
382{
383	size_t pgsize;
384	int ncontig;
385
386	if (!pte_cont(READ_ONCE(*ptep))) {
 
 
 
 
 
 
 
387		ptep_clear_flush(vma, addr, ptep);
388		return;
389	}
390
391	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
392	clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
393}
394
395static __init int setup_hugepagesz(char *opt)
396{
397	unsigned long ps = memparse(opt, &opt);
398
399	switch (ps) {
400#ifdef CONFIG_ARM64_4K_PAGES
401	case PUD_SIZE:
402#endif
403	case PMD_SIZE * CONT_PMDS:
404	case PMD_SIZE:
405	case PAGE_SIZE * CONT_PTES:
406		hugetlb_add_hstate(ilog2(ps) - PAGE_SHIFT);
407		return 1;
408	}
409
410	hugetlb_bad_size();
411	pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
412	return 0;
413}
414__setup("hugepagesz=", setup_hugepagesz);
415
416#ifdef CONFIG_ARM64_64K_PAGES
417static __init int add_default_hugepagesz(void)
418{
419	if (size_to_hstate(CONT_PTES * PAGE_SIZE) == NULL)
420		hugetlb_add_hstate(CONT_PTE_SHIFT);
421	return 0;
422}
423arch_initcall(add_default_hugepagesz);
424#endif