hugetlbpage.c 12.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
S
Steve Capper 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * arch/arm64/mm/hugetlbpage.c
 *
 * Copyright (C) 2013 Linaro Ltd.
 *
 * Based on arch/x86/mm/hugetlbpage.c.
 */

#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>

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * HugeTLB Support Matrix
 *
 * ---------------------------------------------------
 * | Page Size | CONT PTE |  PMD  | CONT PMD |  PUD  |
 * ---------------------------------------------------
 * |     4K    |   64K    |   2M  |    32M   |   1G  |
 * |    16K    |    2M    |  32M  |     1G   |       |
 * |    64K    |    2M    | 512M  |    16G   |       |
 * ---------------------------------------------------
 */

/*
 * Reserve CMA areas for the largest supported gigantic
 * huge page when requested. Any other smaller gigantic
 * huge pages could still be served from those areas.
 */
#ifdef CONFIG_CMA
void __init arm64_hugetlb_cma_reserve(void)
{
	int order;

43 44 45
	if (pud_sect_supported())
		order = PUD_SHIFT - PAGE_SHIFT;
	else
46 47
		order = CONT_PMD_SHIFT - PAGE_SHIFT;

48 49 50 51 52 53 54 55 56 57 58
	/*
	 * HugeTLB CMA reservation is required for gigantic
	 * huge pages which could not be allocated via the
	 * page allocator. Just warn if there is any change
	 * breaking this assumption.
	 */
	WARN_ON(order <= MAX_ORDER);
	hugetlb_cma_reserve(order);
}
#endif /* CONFIG_CMA */

59
static bool __hugetlb_valid_size(unsigned long size)
60
{
61
	switch (size) {
62
#ifndef __PAGETABLE_PMD_FOLDED
63
	case PUD_SIZE:
64
		return pud_sect_supported();
65 66
#endif
	case CONT_PMD_SIZE:
67
	case PMD_SIZE:
68 69 70
	case CONT_PTE_SIZE:
		return true;
	}
71

72 73
	return false;
}
74 75 76 77 78 79 80 81 82 83 84 85 86

#ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
bool arch_hugetlb_migration_supported(struct hstate *h)
{
	size_t pagesize = huge_page_size(h);

	if (!__hugetlb_valid_size(pagesize)) {
		pr_warn("%s: unrecognized huge page size 0x%lx\n",
			__func__, pagesize);
		return false;
	}
	return true;
}
87 88
#endif

S
Steve Capper 已提交
89 90
int pmd_huge(pmd_t pmd)
{
91
	return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
S
Steve Capper 已提交
92 93 94 95
}

int pud_huge(pud_t pud)
{
96
#ifndef __PAGETABLE_PMD_FOLDED
97
	return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
98 99 100
#else
	return 0;
#endif
S
Steve Capper 已提交
101 102
}

103 104 105 106 107 108 109 110 111 112
/*
 * 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));
}

113
static int find_num_contig(struct mm_struct *mm, unsigned long addr,
114
			   pte_t *ptep, size_t *pgsize)
115
{
116
	pgd_t *pgdp = pgd_offset(mm, addr);
117
	p4d_t *p4dp;
118 119
	pud_t *pudp;
	pmd_t *pmdp;
120 121

	*pgsize = PAGE_SIZE;
122 123
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_offset(p4dp, addr);
124 125
	pmdp = pmd_offset(pudp, addr);
	if ((pte_t *)pmdp == ptep) {
126 127 128 129 130 131
		*pgsize = PMD_SIZE;
		return CONT_PMDS;
	}
	return CONT_PTES;
}

132 133 134 135 136 137 138
static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
{
	int contig_ptes = 0;

	*pgsize = size;

	switch (size) {
139
#ifndef __PAGETABLE_PMD_FOLDED
140
	case PUD_SIZE:
141 142 143
		if (pud_sect_supported())
			contig_ptes = 1;
		break;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#endif
	case PMD_SIZE:
		contig_ptes = 1;
		break;
	case CONT_PMD_SIZE:
		*pgsize = PMD_SIZE;
		contig_ptes = CONT_PMDS;
		break;
	case CONT_PTE_SIZE:
		*pgsize = PAGE_SIZE;
		contig_ptes = CONT_PTES;
		break;
	}

	return contig_ptes;
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
pte_t huge_ptep_get(pte_t *ptep)
{
	int ncontig, i;
	size_t pgsize;
	pte_t orig_pte = ptep_get(ptep);

	if (!pte_present(orig_pte) || !pte_cont(orig_pte))
		return orig_pte;

	ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize);
	for (i = 0; i < ncontig; i++, ptep++) {
		pte_t pte = ptep_get(ptep);

		if (pte_dirty(pte))
			orig_pte = pte_mkdirty(orig_pte);

		if (pte_young(pte))
			orig_pte = pte_mkyoung(orig_pte);
	}
	return orig_pte;
}

183 184 185 186 187 188 189 190
/*
 * Changing some bits of contiguous entries requires us to follow a
 * Break-Before-Make approach, breaking the whole contiguous set
 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
 * "Misprogramming of the Contiguous bit", page D4-1762.
 *
 * This helper performs the break step.
 */
191
static pte_t get_clear_contig(struct mm_struct *mm,
192 193 194 195 196
			     unsigned long addr,
			     pte_t *ptep,
			     unsigned long pgsize,
			     unsigned long ncontig)
{
197
	pte_t orig_pte = ptep_get(ptep);
198
	unsigned long i;
199 200 201 202 203 204

	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
		pte_t pte = ptep_get_and_clear(mm, addr, ptep);

		/*
		 * If HW_AFDBM is enabled, then the HW could turn on
205 206
		 * the dirty or accessed bit for any page in the set,
		 * so check them all.
207 208 209
		 */
		if (pte_dirty(pte))
			orig_pte = pte_mkdirty(orig_pte);
210 211 212

		if (pte_young(pte))
			orig_pte = pte_mkyoung(orig_pte);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	}
	return orig_pte;
}

/*
 * Changing some bits of contiguous entries requires us to follow a
 * Break-Before-Make approach, breaking the whole contiguous set
 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
 * "Misprogramming of the Contiguous bit", page D4-1762.
 *
 * This helper performs the break step for use cases where the
 * original pte is not needed.
 */
static void clear_flush(struct mm_struct *mm,
			     unsigned long addr,
			     pte_t *ptep,
			     unsigned long pgsize,
			     unsigned long ncontig)
{
232
	struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
233 234 235 236 237 238 239 240
	unsigned long i, saddr = addr;

	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
		pte_clear(mm, addr, ptep);

	flush_tlb_range(&vma, saddr, addr);
}

241 242 243 244 245
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
			    pte_t *ptep, pte_t pte)
{
	size_t pgsize;
	int i;
246
	int ncontig;
247
	unsigned long pfn, dpfn;
248 249
	pgprot_t hugeprot;

250 251 252 253 254 255
	/*
	 * Code needs to be expanded to handle huge swap and migration
	 * entries. Needed for HUGETLB and MEMORY_FAILURE.
	 */
	WARN_ON(!pte_present(pte));

256
	if (!pte_cont(pte)) {
257 258 259 260
		set_pte_at(mm, addr, ptep, pte);
		return;
	}

261
	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
262
	pfn = pte_pfn(pte);
263
	dpfn = pgsize >> PAGE_SHIFT;
264
	hugeprot = pte_pgprot(pte);
265

266 267
	clear_flush(mm, addr, ptep, pgsize, ncontig);

268
	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
269 270 271
		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
}

272 273 274 275 276 277 278 279 280 281 282 283
void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
			  pte_t *ptep, pte_t pte, unsigned long sz)
{
	int i, ncontig;
	size_t pgsize;

	ncontig = num_contig_ptes(sz, &pgsize);

	for (i = 0; i < ncontig; i++, ptep++)
		set_pte(ptep, pte);
}

284
pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
285 286
		      unsigned long addr, unsigned long sz)
{
287
	pgd_t *pgdp;
288
	p4d_t *p4dp;
289 290 291 292 293
	pud_t *pudp;
	pmd_t *pmdp;
	pte_t *ptep = NULL;

	pgdp = pgd_offset(mm, addr);
294 295
	p4dp = p4d_offset(pgdp, addr);
	pudp = pud_alloc(mm, p4dp, addr);
296
	if (!pudp)
297 298 299
		return NULL;

	if (sz == PUD_SIZE) {
300
		ptep = (pte_t *)pudp;
301
	} else if (sz == (CONT_PTE_SIZE)) {
302
		pmdp = pmd_alloc(mm, pudp, addr);
303 304
		if (!pmdp)
			return NULL;
305 306 307 308 309 310 311 312 313

		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().
		 */
314
		ptep = pte_alloc_map(mm, pmdp, addr);
315
	} else if (sz == PMD_SIZE) {
316
		if (want_pmd_share(vma, addr) && pud_none(READ_ONCE(*pudp)))
317
			ptep = huge_pmd_share(mm, vma, addr, pudp);
318
		else
319
			ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
320
	} else if (sz == (CONT_PMD_SIZE)) {
321
		pmdp = pmd_alloc(mm, pudp, addr);
322
		WARN_ON(addr & (sz - 1));
323
		return (pte_t *)pmdp;
324 325
	}

326
	return ptep;
327 328
}

329 330
pte_t *huge_pte_offset(struct mm_struct *mm,
		       unsigned long addr, unsigned long sz)
331
{
332
	pgd_t *pgdp;
333
	p4d_t *p4dp;
334 335
	pud_t *pudp, pud;
	pmd_t *pmdp, pmd;
336

337 338
	pgdp = pgd_offset(mm, addr);
	if (!pgd_present(READ_ONCE(*pgdp)))
339
		return NULL;
340

341 342 343 344 345
	p4dp = p4d_offset(pgdp, addr);
	if (!p4d_present(READ_ONCE(*p4dp)))
		return NULL;

	pudp = pud_offset(p4dp, addr);
346 347
	pud = READ_ONCE(*pudp);
	if (sz != PUD_SIZE && pud_none(pud))
348
		return NULL;
349
	/* hugepage or swap? */
350 351
	if (pud_huge(pud) || !pud_present(pud))
		return (pte_t *)pudp;
352 353
	/* table; check the next level */

354 355 356
	if (sz == CONT_PMD_SIZE)
		addr &= CONT_PMD_MASK;

357 358
	pmdp = pmd_offset(pudp, addr);
	pmd = READ_ONCE(*pmdp);
359
	if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
360
	    pmd_none(pmd))
361
		return NULL;
362 363
	if (pmd_huge(pmd) || !pmd_present(pmd))
		return (pte_t *)pmdp;
364

365 366
	if (sz == CONT_PTE_SIZE)
		return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK));
367

368 369 370
	return NULL;
}

371
pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
372
{
373
	size_t pagesize = 1UL << shift;
374

375
	entry = pte_mkhuge(entry);
376 377 378 379 380 381 382 383 384 385 386
	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;
}

387 388 389 390 391 392 393 394 395 396 397 398
void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
		    pte_t *ptep, unsigned long sz)
{
	int i, ncontig;
	size_t pgsize;

	ncontig = num_contig_ptes(sz, &pgsize);

	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
		pte_clear(mm, addr, ptep);
}

399 400 401
pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
			      unsigned long addr, pte_t *ptep)
{
402
	int ncontig;
403
	size_t pgsize;
404
	pte_t orig_pte = ptep_get(ptep);
405 406

	if (!pte_cont(orig_pte))
407
		return ptep_get_and_clear(mm, addr, ptep);
408 409 410

	ncontig = find_num_contig(mm, addr, ptep, &pgsize);

411
	return get_clear_contig(mm, addr, ptep, pgsize, ncontig);
412 413
}

414 415 416 417 418 419 420 421 422 423 424 425 426
/*
 * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
 * and write permission.
 *
 * For a contiguous huge pte range we need to check whether or not write
 * permission has to change only on the first pte in the set. Then for
 * all the contiguous ptes we need to check whether or not there is a
 * discrepancy between dirty or young.
 */
static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
{
	int i;

427
	if (pte_write(pte) != pte_write(ptep_get(ptep)))
428 429 430
		return 1;

	for (i = 0; i < ncontig; i++) {
431
		pte_t orig_pte = ptep_get(ptep + i);
432 433 434 435 436 437 438 439 440 441 442

		if (pte_dirty(pte) != pte_dirty(orig_pte))
			return 1;

		if (pte_young(pte) != pte_young(orig_pte))
			return 1;
	}

	return 0;
}

443 444 445 446
int huge_ptep_set_access_flags(struct vm_area_struct *vma,
			       unsigned long addr, pte_t *ptep,
			       pte_t pte, int dirty)
{
447
	int ncontig, i;
448 449 450
	size_t pgsize = 0;
	unsigned long pfn = pte_pfn(pte), dpfn;
	pgprot_t hugeprot;
451
	pte_t orig_pte;
452 453

	if (!pte_cont(pte))
454
		return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
455 456 457 458

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

459 460 461
	if (!__cont_access_flags_changed(ptep, pte, ncontig))
		return 0;

462
	orig_pte = get_clear_contig(vma->vm_mm, addr, ptep, pgsize, ncontig);
463

464
	/* Make sure we don't lose the dirty or young state */
465 466 467
	if (pte_dirty(orig_pte))
		pte = pte_mkdirty(pte);

468 469 470
	if (pte_young(orig_pte))
		pte = pte_mkyoung(pte);

471 472 473
	hugeprot = pte_pgprot(pte);
	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
		set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
474

475
	return 1;
476 477 478 479 480
}

void huge_ptep_set_wrprotect(struct mm_struct *mm,
			     unsigned long addr, pte_t *ptep)
{
481 482
	unsigned long pfn, dpfn;
	pgprot_t hugeprot;
483 484
	int ncontig, i;
	size_t pgsize;
485
	pte_t pte;
486

487
	if (!pte_cont(READ_ONCE(*ptep))) {
488
		ptep_set_wrprotect(mm, addr, ptep);
489
		return;
490
	}
491 492

	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
493 494
	dpfn = pgsize >> PAGE_SHIFT;

495
	pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
496 497 498 499 500 501 502
	pte = pte_wrprotect(pte);

	hugeprot = pte_pgprot(pte);
	pfn = pte_pfn(pte);

	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
503 504 505 506 507
}

void huge_ptep_clear_flush(struct vm_area_struct *vma,
			   unsigned long addr, pte_t *ptep)
{
508
	size_t pgsize;
509
	int ncontig;
510

511
	if (!pte_cont(READ_ONCE(*ptep))) {
512
		ptep_clear_flush(vma, addr, ptep);
513
		return;
514
	}
515 516

	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
517
	clear_flush(vma->vm_mm, addr, ptep, pgsize, ncontig);
518 519
}

520 521
static int __init hugetlbpage_init(void)
{
522 523 524
	if (pud_sect_supported())
		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);

525
	hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT);
526
	hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
527
	hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT);
528 529 530 531 532

	return 0;
}
arch_initcall(hugetlbpage_init);

533
bool __init arch_hugetlb_valid_size(unsigned long size)
S
Steve Capper 已提交
534
{
535
	return __hugetlb_valid_size(size);
536
}