Currently we have many duplicates in definitions around follow_huge_addr(), follow_huge_pmd(), and follow_huge_pud(), so this patch tries to remove the m. The basic idea is to put the default implementation for these functions in mm/hugetlb.c as weak symbols (regardless of CONFIG_ARCH_WANT_GENERAL_HUGETL B), and to implement arch-specific code only when the arch needs it. For follow_huge_addr(), only powerpc and ia64 have their own implementation, and in all other architectures this function just returns ERR_PTR(-EINVAL). So this patch sets returning ERR_PTR(-EINVAL) as default. As for follow_huge_(pmd|pud)(), if (pmd|pud)_huge() is implemented to always return 0 in your architecture (like in ia64 or sparc,) it's never called (the callsite is optimized away) no matter how implemented it is. So in such architectures, we don't need arch-specific implementation. In some architecture (like mips, s390 and tile,) their current arch-specific follow_huge_(pmd|pud)() are effectively identical with the common code, so this patch lets these architecture use the common code. One exception is metag, where pmd_huge() could return non-zero but it expects follow_huge_pmd() to always return NULL. This means that we need arch-specific implementation which returns NULL. This behavior looks strange to me (because non-zero pmd_huge() implies that the architecture supports PMD-based hugepage, so follow_huge_pmd() can/should return some relevant value,) but that's beyond this cleanup patch, so let's keep it. Justification of non-trivial changes: - in s390, follow_huge_pmd() checks !MACHINE_HAS_HPAGE at first, and this patch removes the check. This is OK because we can assume MACHINE_HAS_HPAGE is true when follow_huge_pmd() can be called (note that pmd_huge() has the same check and always returns 0 for !MACHINE_HAS_HPAGE.) - in s390 and mips, we use HPAGE_MASK instead of PMD_MASK as done in common code. This patch forces these archs use PMD_MASK, but it's OK because they are identical in both archs. In s390, both of HPAGE_SHIFT and PMD_SHIFT are 20. In mips, HPAGE_SHIFT is defined as (PAGE_SHIFT + PAGE_SHIFT - 3) and PMD_SHIFT is define as (PAGE_SHIFT + PAGE_SHIFT + PTE_ORDER - 3), but PTE_ORDER is always 0, so these are identical. Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Acked-by: Hugh Dickins <hughd@google.com> Cc: James Hogan <james.hogan@imgtec.com> Cc: David Rientjes <rientjes@google.com> Cc: Mel Gorman <mel@csn.ul.ie> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@suse.cz> Cc: Rik van Riel <riel@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Luiz Capitulino <lcapitulino@redhat.com> Cc: Nishanth Aravamudan <nacc@linux.vnet.ibm.com> Cc: Lee Schermerhorn <lee.schermerhorn@hp.com> Cc: Steve Capper <steve.capper@linaro.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
		
			
				
	
	
		
			206 lines
		
	
	
	
		
			5.5 KiB
			
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			206 lines
		
	
	
	
		
			5.5 KiB
			
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 *  IBM System z Huge TLB Page Support for Kernel.
 | 
						|
 *
 | 
						|
 *    Copyright IBM Corp. 2007
 | 
						|
 *    Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
 | 
						|
 */
 | 
						|
 | 
						|
#include <linux/mm.h>
 | 
						|
#include <linux/hugetlb.h>
 | 
						|
 | 
						|
static inline pmd_t __pte_to_pmd(pte_t pte)
 | 
						|
{
 | 
						|
	pmd_t pmd;
 | 
						|
 | 
						|
	/*
 | 
						|
	 * Convert encoding		  pte bits	   pmd bits
 | 
						|
	 *				.IR...wrdytp	dy..R...I...wr
 | 
						|
	 * empty			.10...000000 -> 00..0...1...00
 | 
						|
	 * prot-none, clean, old	.11...000001 -> 00..1...1...00
 | 
						|
	 * prot-none, clean, young	.11...000101 -> 01..1...1...00
 | 
						|
	 * prot-none, dirty, old	.10...001001 -> 10..1...1...00
 | 
						|
	 * prot-none, dirty, young	.10...001101 -> 11..1...1...00
 | 
						|
	 * read-only, clean, old	.11...010001 -> 00..1...1...01
 | 
						|
	 * read-only, clean, young	.01...010101 -> 01..1...0...01
 | 
						|
	 * read-only, dirty, old	.11...011001 -> 10..1...1...01
 | 
						|
	 * read-only, dirty, young	.01...011101 -> 11..1...0...01
 | 
						|
	 * read-write, clean, old	.11...110001 -> 00..0...1...11
 | 
						|
	 * read-write, clean, young	.01...110101 -> 01..0...0...11
 | 
						|
	 * read-write, dirty, old	.10...111001 -> 10..0...1...11
 | 
						|
	 * read-write, dirty, young	.00...111101 -> 11..0...0...11
 | 
						|
	 */
 | 
						|
	if (pte_present(pte)) {
 | 
						|
		pmd_val(pmd) = pte_val(pte) & PAGE_MASK;
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_READ) >> 4;
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_WRITE) >> 4;
 | 
						|
		pmd_val(pmd) |=	(pte_val(pte) & _PAGE_INVALID) >> 5;
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_PROTECT);
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_DIRTY) << 10;
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_YOUNG) << 10;
 | 
						|
	} else
 | 
						|
		pmd_val(pmd) = _SEGMENT_ENTRY_INVALID;
 | 
						|
	return pmd;
 | 
						|
}
 | 
						|
 | 
						|
static inline pte_t __pmd_to_pte(pmd_t pmd)
 | 
						|
{
 | 
						|
	pte_t pte;
 | 
						|
 | 
						|
	/*
 | 
						|
	 * Convert encoding		   pmd bits	    pte bits
 | 
						|
	 *				dy..R...I...wr	  .IR...wrdytp
 | 
						|
	 * empty			00..0...1...00 -> .10...001100
 | 
						|
	 * prot-none, clean, old	00..0...1...00 -> .10...000001
 | 
						|
	 * prot-none, clean, young	01..0...1...00 -> .10...000101
 | 
						|
	 * prot-none, dirty, old	10..0...1...00 -> .10...001001
 | 
						|
	 * prot-none, dirty, young	11..0...1...00 -> .10...001101
 | 
						|
	 * read-only, clean, old	00..1...1...01 -> .11...010001
 | 
						|
	 * read-only, clean, young	01..1...1...01 -> .11...010101
 | 
						|
	 * read-only, dirty, old	10..1...1...01 -> .11...011001
 | 
						|
	 * read-only, dirty, young	11..1...1...01 -> .11...011101
 | 
						|
	 * read-write, clean, old	00..0...1...11 -> .10...110001
 | 
						|
	 * read-write, clean, young	01..0...1...11 -> .10...110101
 | 
						|
	 * read-write, dirty, old	10..0...1...11 -> .10...111001
 | 
						|
	 * read-write, dirty, young	11..0...1...11 -> .10...111101
 | 
						|
	 */
 | 
						|
	if (pmd_present(pmd)) {
 | 
						|
		pte_val(pte) = pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN_LARGE;
 | 
						|
		pte_val(pte) |= _PAGE_LARGE | _PAGE_PRESENT;
 | 
						|
		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_READ) << 4;
 | 
						|
		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) << 4;
 | 
						|
		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_INVALID) << 5;
 | 
						|
		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_PROTECT);
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_DIRTY) << 10;
 | 
						|
		pmd_val(pmd) |= (pte_val(pte) & _PAGE_YOUNG) << 10;
 | 
						|
	} else
 | 
						|
		pte_val(pte) = _PAGE_INVALID;
 | 
						|
	return pte;
 | 
						|
}
 | 
						|
 | 
						|
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
 | 
						|
		     pte_t *ptep, pte_t pte)
 | 
						|
{
 | 
						|
	pmd_t pmd;
 | 
						|
 | 
						|
	pmd = __pte_to_pmd(pte);
 | 
						|
	if (!MACHINE_HAS_HPAGE) {
 | 
						|
		/* Emulated huge ptes loose the dirty and young bit */
 | 
						|
		pmd_val(pmd) &= ~_SEGMENT_ENTRY_ORIGIN;
 | 
						|
		pmd_val(pmd) |= pte_page(pte)[1].index;
 | 
						|
	} else
 | 
						|
		pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
 | 
						|
	*(pmd_t *) ptep = pmd;
 | 
						|
}
 | 
						|
 | 
						|
pte_t huge_ptep_get(pte_t *ptep)
 | 
						|
{
 | 
						|
	unsigned long origin;
 | 
						|
	pmd_t pmd;
 | 
						|
 | 
						|
	pmd = *(pmd_t *) ptep;
 | 
						|
	if (!MACHINE_HAS_HPAGE && pmd_present(pmd)) {
 | 
						|
		origin = pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN;
 | 
						|
		pmd_val(pmd) &= ~_SEGMENT_ENTRY_ORIGIN;
 | 
						|
		pmd_val(pmd) |= *(unsigned long *) origin;
 | 
						|
		/* Emulated huge ptes are young and dirty by definition */
 | 
						|
		pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG | _SEGMENT_ENTRY_DIRTY;
 | 
						|
	}
 | 
						|
	return __pmd_to_pte(pmd);
 | 
						|
}
 | 
						|
 | 
						|
pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
 | 
						|
			      unsigned long addr, pte_t *ptep)
 | 
						|
{
 | 
						|
	pmd_t *pmdp = (pmd_t *) ptep;
 | 
						|
	pte_t pte = huge_ptep_get(ptep);
 | 
						|
 | 
						|
	pmdp_flush_direct(mm, addr, pmdp);
 | 
						|
	pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
 | 
						|
	return pte;
 | 
						|
}
 | 
						|
 | 
						|
int arch_prepare_hugepage(struct page *page)
 | 
						|
{
 | 
						|
	unsigned long addr = page_to_phys(page);
 | 
						|
	pte_t pte;
 | 
						|
	pte_t *ptep;
 | 
						|
	int i;
 | 
						|
 | 
						|
	if (MACHINE_HAS_HPAGE)
 | 
						|
		return 0;
 | 
						|
 | 
						|
	ptep = (pte_t *) pte_alloc_one(&init_mm, addr);
 | 
						|
	if (!ptep)
 | 
						|
		return -ENOMEM;
 | 
						|
 | 
						|
	pte_val(pte) = addr;
 | 
						|
	for (i = 0; i < PTRS_PER_PTE; i++) {
 | 
						|
		set_pte_at(&init_mm, addr + i * PAGE_SIZE, ptep + i, pte);
 | 
						|
		pte_val(pte) += PAGE_SIZE;
 | 
						|
	}
 | 
						|
	page[1].index = (unsigned long) ptep;
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
void arch_release_hugepage(struct page *page)
 | 
						|
{
 | 
						|
	pte_t *ptep;
 | 
						|
 | 
						|
	if (MACHINE_HAS_HPAGE)
 | 
						|
		return;
 | 
						|
 | 
						|
	ptep = (pte_t *) page[1].index;
 | 
						|
	if (!ptep)
 | 
						|
		return;
 | 
						|
	clear_table((unsigned long *) ptep, _PAGE_INVALID,
 | 
						|
		    PTRS_PER_PTE * sizeof(pte_t));
 | 
						|
	page_table_free(&init_mm, (unsigned long *) ptep);
 | 
						|
	page[1].index = 0;
 | 
						|
}
 | 
						|
 | 
						|
pte_t *huge_pte_alloc(struct mm_struct *mm,
 | 
						|
			unsigned long addr, unsigned long sz)
 | 
						|
{
 | 
						|
	pgd_t *pgdp;
 | 
						|
	pud_t *pudp;
 | 
						|
	pmd_t *pmdp = NULL;
 | 
						|
 | 
						|
	pgdp = pgd_offset(mm, addr);
 | 
						|
	pudp = pud_alloc(mm, pgdp, addr);
 | 
						|
	if (pudp)
 | 
						|
		pmdp = pmd_alloc(mm, pudp, addr);
 | 
						|
	return (pte_t *) pmdp;
 | 
						|
}
 | 
						|
 | 
						|
pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 | 
						|
{
 | 
						|
	pgd_t *pgdp;
 | 
						|
	pud_t *pudp;
 | 
						|
	pmd_t *pmdp = NULL;
 | 
						|
 | 
						|
	pgdp = pgd_offset(mm, addr);
 | 
						|
	if (pgd_present(*pgdp)) {
 | 
						|
		pudp = pud_offset(pgdp, addr);
 | 
						|
		if (pud_present(*pudp))
 | 
						|
			pmdp = pmd_offset(pudp, addr);
 | 
						|
	}
 | 
						|
	return (pte_t *) pmdp;
 | 
						|
}
 | 
						|
 | 
						|
int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
 | 
						|
{
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
int pmd_huge(pmd_t pmd)
 | 
						|
{
 | 
						|
	if (!MACHINE_HAS_HPAGE)
 | 
						|
		return 0;
 | 
						|
 | 
						|
	return !!(pmd_val(pmd) & _SEGMENT_ENTRY_LARGE);
 | 
						|
}
 | 
						|
 | 
						|
int pud_huge(pud_t pud)
 | 
						|
{
 | 
						|
	return 0;
 | 
						|
}
 |