hugetlbpage.c 6.8 KB
Newer Older
S
Steve Capper 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * arch/arm64/mm/hugetlbpage.c
 *
 * Copyright (C) 2013 Linaro Ltd.
 *
 * Based on arch/x86/mm/hugetlbpage.c.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/hugetlb.h>
#include <linux/pagemap.h>
#include <linux/err.h>
#include <linux/sysctl.h>
#include <asm/mman.h>
#include <asm/tlb.h>
#include <asm/tlbflush.h>
#include <asm/pgalloc.h>

int pmd_huge(pmd_t pmd)
{
32
	return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
S
Steve Capper 已提交
33 34 35 36
}

int pud_huge(pud_t pud)
{
37
#ifndef __PAGETABLE_PMD_FOLDED
38
	return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
39 40 41
#else
	return 0;
#endif
S
Steve Capper 已提交
42 43
}

44 45 46 47 48 49 50 51 52 53
/*
 * Select all bits except the pfn
 */
static inline pgprot_t pte_pgprot(pte_t pte)
{
	unsigned long pfn = pte_pfn(pte);

	return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
}

54
static int find_num_contig(struct mm_struct *mm, unsigned long addr,
55
			   pte_t *ptep, size_t *pgsize)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
{
	pgd_t *pgd = pgd_offset(mm, addr);
	pud_t *pud;
	pmd_t *pmd;

	*pgsize = PAGE_SIZE;
	pud = pud_offset(pgd, addr);
	pmd = pmd_offset(pud, addr);
	if ((pte_t *)pmd == ptep) {
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
			    pte_t *ptep, pte_t pte)
{
	size_t pgsize;
	int i;
76
	int ncontig;
77
	unsigned long pfn, dpfn;
78 79
	pgprot_t hugeprot;

80 81 82 83 84 85
	/*
	 * Code needs to be expanded to handle huge swap and migration
	 * entries. Needed for HUGETLB and MEMORY_FAILURE.
	 */
	WARN_ON(!pte_present(pte));

86
	if (!pte_cont(pte)) {
87 88 89 90
		set_pte_at(mm, addr, ptep, pte);
		return;
	}

91
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
92
	pfn = pte_pfn(pte);
93
	dpfn = pgsize >> PAGE_SHIFT;
94
	hugeprot = pte_pgprot(pte);
95 96

	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		pr_debug("%s: set pte %p to 0x%llx\n", __func__, ptep,
			 pte_val(pfn_pte(pfn, hugeprot)));
		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
	}
}

pte_t *huge_pte_alloc(struct mm_struct *mm,
		      unsigned long addr, unsigned long sz)
{
	pgd_t *pgd;
	pud_t *pud;
	pte_t *pte = NULL;

	pr_debug("%s: addr:0x%lx sz:0x%lx\n", __func__, addr, sz);
	pgd = pgd_offset(mm, addr);
	pud = pud_alloc(mm, pgd, addr);
	if (!pud)
		return NULL;

	if (sz == PUD_SIZE) {
		pte = (pte_t *)pud;
	} else if (sz == (PAGE_SIZE * CONT_PTES)) {
		pmd_t *pmd = pmd_alloc(mm, pud, addr);

		WARN_ON(addr & (sz - 1));
		/*
		 * Note that if this code were ever ported to the
		 * 32-bit arm platform then it will cause trouble in
		 * the case where CONFIG_HIGHPTE is set, since there
		 * will be no pte_unmap() to correspond with this
		 * pte_alloc_map().
		 */
129
		pte = pte_alloc_map(mm, pmd, addr);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	} else if (sz == PMD_SIZE) {
		if (IS_ENABLED(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) &&
		    pud_none(*pud))
			pte = huge_pmd_share(mm, addr, pud);
		else
			pte = (pte_t *)pmd_alloc(mm, pud, addr);
	} else if (sz == (PMD_SIZE * CONT_PMDS)) {
		pmd_t *pmd;

		pmd = pmd_alloc(mm, pud, addr);
		WARN_ON(addr & (sz - 1));
		return (pte_t *)pmd;
	}

	pr_debug("%s: addr:0x%lx sz:0x%lx ret pte=%p/0x%llx\n", __func__, addr,
	       sz, pte, pte_val(*pte));
	return pte;
}

149 150
pte_t *huge_pte_offset(struct mm_struct *mm,
		       unsigned long addr, unsigned long sz)
151 152 153
{
	pgd_t *pgd;
	pud_t *pud;
154
	pmd_t *pmd;
155 156 157 158 159

	pgd = pgd_offset(mm, addr);
	pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
	if (!pgd_present(*pgd))
		return NULL;
160

161
	pud = pud_offset(pgd, addr);
162
	if (pud_none(*pud))
163
		return NULL;
164 165
	/* swap or huge page */
	if (!pud_present(*pud) || pud_huge(*pud))
166
		return (pte_t *)pud;
167 168
	/* table; check the next level */

169
	pmd = pmd_offset(pud, addr);
170
	if (pmd_none(*pmd))
171
		return NULL;
172
	if (!pmd_present(*pmd) || pmd_huge(*pmd))
173
		return (pte_t *)pmd;
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	return NULL;
}

pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
			 struct page *page, int writable)
{
	size_t pagesize = huge_page_size(hstate_vma(vma));

	if (pagesize == CONT_PTE_SIZE) {
		entry = pte_mkcont(entry);
	} else if (pagesize == CONT_PMD_SIZE) {
		entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
	} else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
		pr_warn("%s: unrecognized huge page size 0x%lx\n",
			__func__, pagesize);
	}
	return entry;
}

pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
			      unsigned long addr, pte_t *ptep)
{
197 198 199 200 201
	int ncontig, i;
	size_t pgsize;
	pte_t orig_pte = huge_ptep_get(ptep);

	if (!pte_cont(orig_pte))
202
		return ptep_get_and_clear(mm, addr, ptep);
203 204 205 206 207 208 209 210 211 212

	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
		/*
		 * If HW_AFDBM is enabled, then the HW could
		 * turn on the dirty bit for any of the page
		 * in the set, so check them all.
		 */
		if (pte_dirty(ptep_get_and_clear(mm, addr, ptep)))
			orig_pte = pte_mkdirty(orig_pte);
213
	}
214 215

	return orig_pte;
216 217 218 219 220 221
}

int huge_ptep_set_access_flags(struct vm_area_struct *vma,
			       unsigned long addr, pte_t *ptep,
			       pte_t pte, int dirty)
{
222 223 224 225 226 227
	int ncontig, i, changed = 0;
	size_t pgsize = 0;
	unsigned long pfn = pte_pfn(pte), dpfn;
	pgprot_t hugeprot;

	if (!pte_cont(pte))
228
		return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
229 230 231 232 233 234 235 236

	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
	dpfn = pgsize >> PAGE_SHIFT;
	hugeprot = pte_pgprot(pte);

	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) {
		changed |= ptep_set_access_flags(vma, addr, ptep,
				pfn_pte(pfn, hugeprot), dirty);
237
	}
238 239

	return changed;
240 241 242 243 244
}

void huge_ptep_set_wrprotect(struct mm_struct *mm,
			     unsigned long addr, pte_t *ptep)
{
245 246
	int ncontig, i;
	size_t pgsize;
247

248
	if (!pte_cont(*ptep)) {
249
		ptep_set_wrprotect(mm, addr, ptep);
250
		return;
251
	}
252 253 254 255

	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize)
		ptep_set_wrprotect(mm, addr, ptep);
256 257 258 259 260
}

void huge_ptep_clear_flush(struct vm_area_struct *vma,
			   unsigned long addr, pte_t *ptep)
{
261 262 263 264
	int ncontig, i;
	size_t pgsize;

	if (!pte_cont(*ptep)) {
265
		ptep_clear_flush(vma, addr, ptep);
266
		return;
267
	}
268 269 270 271

	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize)
		ptep_clear_flush(vma, addr, ptep);
272 273
}

S
Steve Capper 已提交
274 275 276
static __init int setup_hugepagesz(char *opt)
{
	unsigned long ps = memparse(opt, &opt);
277

S
Steve Capper 已提交
278 279 280 281 282
	if (ps == PMD_SIZE) {
		hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
	} else if (ps == PUD_SIZE) {
		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
	} else {
283
		hugetlb_bad_size();
284
		pr_err("hugepagesz: Unsupported page size %lu K\n", ps >> 10);
S
Steve Capper 已提交
285 286 287 288 289
		return 0;
	}
	return 1;
}
__setup("hugepagesz=", setup_hugepagesz);