Xen PV support for hugepages
Subsystems (3)
PATCH 1/2 Xen PV support for hugepages
This patch adds support to Xen for hugepages in a PV environment. The patch is against the latest xen-unstable tree on xenbits.xensource.com.
It must be enabled via the command-line option "allowhugepage".
It is assumed that the guest has guaranteed that the hugepage is physically aligned and contiguous.
There is no support yet for save/restore/migrate.
----
./xen/include/asm-x86/x86_32/page.h renamed from /./xen/include/asm-x86/x86_32/page.h +1 -1
@@ -112,7 +112,7 @@ extern unsigned int PAGE_HYPERVISOR_NOCA* Disallow unused flag bits plus PAT/PSE, PCD, PWT and GLOBAL. * Permit the NX bit if the hardware supports it. */ -#define BASE_DISALLOW_MASK (0xFFFFF198U & ~_PAGE_NX) +#define BASE_DISALLOW_MASK (0xFFFFF118U & ~_PAGE_NX)#define L1_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_GNTTAB)#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK)
./xen/include/asm-x86/x86_64/page.h renamed from /./xen/include/asm-x86/x86_64/page.h +1 -1
@@ -112,7 +112,7 @@ typedef l4_pgentry_t root_pgentry_t;* Permit the NX bit if the hardware supports it. * Note that range [62:52] is available for software use on x86/64. */ -#define BASE_DISALLOW_MASK (0xFF800198U & ~_PAGE_NX) +#define BASE_DISALLOW_MASK (0xFF800118U & ~_PAGE_NX)#define L1_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_GNTTAB)#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK)
./xen/arch/x86/mm.c renamed from /./xen/arch/x86/mm.c +75 -9
121 lines not on this page · raw message
PATCH 2/2 Linux support for hugepages as a Xen PV guest
This patch adds support to Linux for hugepages as a Xen guest. It is against the linux-2.6.18-xen on xenbits.xensource.com.
It currently assumes that the guest memory is physically aligned and contiguous. Detection and avoidance of non-aligned memory will be available in a future patch.
----
./fs/Kconfig renamed from /./fs/Kconfig +1 -1
@@ -870,7 +870,7 @@ config TMPFS config HUGETLBFS bool "HugeTLB file system support" depends X86 || IA64 || PPC64 || SPARC64 || SUPERH || BROKEN - depends on !XEN +# depends on !XEN help hugetlbfs is a filesystem backing for HugeTLB pages, based on ramfs. For architectures that support it, say Y here and read
./include/asm-x86_64/mach-xen/asm/page.h renamed from /./include/asm-x86_64/mach-xen/asm/page.h +2 -0
@@ -62,6 +62,8 @@#define HPAGE_MASK (~(HPAGE_SIZE - 1))#define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT) +#define ARCH_HAS_SETCLEAR_HUGE_PTE +#ifdef __KERNEL__#ifndef __ASSEMBLY__
./include/asm-x86_64/mach-xen/asm/pgtable.h renamed from /./include/asm-x86_64/mach-xen/asm/pgtable.h +19 -0
@@ -261,6 +261,12 @@ static inline unsigned long pud_bad(pud_set_pte ((ptep), (pteval)); \ }while (0 ) +static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval) { +if ((mm != current->mm && mm != &init_mm) || +HYPERVISOR_update_va_mapping (addr, pteval,0 )) +set_pmd ((pmd_t *)ptep, (pmd_t ){__pte_val (pteval)}); +} +#define pte_none (x) (!(x).pte)#define pte_present (x) ((x).pte & (_PAGE_PRESENT | _PAGE_PROTNONE))#define pte_clear (mm,addr,xp) do { set_pte_at(mm, addr, xp, __pte(0)); } while (0)
@@ -296,6 +302,19 @@ static inline pte_t ptep_get_and_clear(sreturn pte; } +static inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) +{ +pte_t pte = *ptep; +if (!pte_none (pte)) { +if ((mm != &init_mm) || +HYPERVISOR_update_va_mapping (addr,__pte (0 ),0 )) { + pte = *ptep; +set_pmd ((pmd_t *)ptep,__pmd (0 )); + } + } +return pte; +} +static inline pte_t ptep_get_and_clear_full (struct mm_struct *mm,unsigned long addr,pte_t *ptep,int full) {if (full) {
./arch/x86_64/mm/pageattr-xen.c renamed from /./arch/x86_64/mm/pageattr-xen.c +2 -0
@@ -62,6 +62,8 @@ static void _pin_lock(struct mm_struct *if (pmd_none (*pmd))continue ; +if (pte_huge (*(pte_t *)pmd)) +continue ; ptl =pte_lockptr (0 , pmd);if (lock)spin_lock (ptl);
./mm/hugetlb.c renamed from /./mm/hugetlb.c +4 -2
@@ -294,12 +294,14 @@ static pte_t make_huge_pte(struct vm_are int writable) {pte_t entry; +pgprot_t pgprot; + pgprot =__pgprot (pgprot_val (vma->vm_page_prot) | _PAGE_PRESENT);if (writable) { entry = -pte_mkwrite (pte_mkdirty (mk_pte (page, vma->vm_page_prot))); +pte_mkwrite (pte_mkdirty (mk_pte (page, pgprot))); }else { - entry =pte_wrprotect (mk_pte (page, vma->vm_page_prot)); + entry =pte_wrprotect (mk_pte (page, pgprot)); } entry =pte_mkyoung (entry); entry =pte_mkhuge (entry);
Re: [PATCH 2/2] Linux support for hugepages as a Xen PV guest
Quoted 8 lines · On 10/10/08 14:30, "[email protected]" <[email protected]> wrote:
This patch adds support to Linux for hugepages as a Xen guest. It is against the linux-2.6.18-xen on xenbits.xensource.com.
It currently assumes that the guest memory is physically aligned and contiguous. Detection and avoidance of non-aligned memory will be available in a future patch.
Signed-off-by Dave McCracken <[email protected]>
hugetlbfs should only be enabled at runtime if supported by the hypervisor. Perhaps we can enable the PSE CPUID flag if the hypervisor supports superpages?
Quoted 16 lines
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +2
-0@@ -294,12 +294,14 @@ static pte_t make_huge_pte(struct vm_are int writable) { pte_t entry; + pgprot_t pgprot; + pgprot = __pgprot(pgprot_val(vma->vm_page_prot) | _PAGE_PRESENT); if (writable) {entry =
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-1- pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); + pte_mkwrite(pte_mkdirty(mk_pte(page, pgprot)));} else {
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-1- entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); + entry = pte_wrprotect(mk_pte(page, pgprot));
Quoted 3 lines
}
entry =pte_mkyoung(entry);
entry =pte_mkhuge(entry);
Why do we need to do something different for Xen here?
-- Keir
Re: [PATCH 2/2] Linux support for hugepages as a Xen PV guest
Quoted 3 lines · Keir Fraser
hugetlbfs should only be enabled at runtime if supported by the hypervisor. Perhaps we can enable the PSE CPUID flag if the hypervisor supports superpages?
I'll look into it.
Quoted 8 lines
Quoted 8 lines
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-0@@ -294,12 +294,14 @@ static pte_t make_huge_pte(struct vm_are int writable) { pte_t entry; + pgprot_t pgprot;
Quoted 13 lines
Quoted 11 lines
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-0+ pgprot = __pgprot(pgprot_val(vma->vm_page_prot) | _PAGE_PRESENT); if (writable) {entry =
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-1- pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); + pte_mkwrite(pte_mkdirty(mk_pte(page, pgprot)));} else {
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-1- entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); + entry = pte_wrprotect(mk_pte(page, pgprot));}
entry =pte_mkyoung(entry);
entry =pte_mkhuge(entry);Why do we need to do something different for Xen here?
In the original implementation _PAGE_PRESENT is set in later macros. Xen needs it early to trigger the proper phys_to_machine() translation.
Dave McCracken
Re: [PATCH 2/2] Linux support for hugepages as a Xen PVguest
Quoted 23 lines
Quoted 9 lines
Quoted 9 lines
Dave McCracken <[email protected]> 10.10.08 17:06 >>>
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-0@@ -294,12 +294,14 @@ static pte_t make_huge_pte(struct vm_are int writable) { pte_t entry; + pgprot_t pgprot;Quoted 13 lines
Quoted 11 lines
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-0+ pgprot = __pgprot(pgprot_val(vma->vm_page_prot) | _PAGE_PRESENT); if (writable) {entry =
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-1- pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); + pte_mkwrite(pte_mkdirty(mk_pte(page, pgprot)));} else {
./mm/hugetlb.c 2008-10-02 15:07:54.000000000 -0500 renamed from /./mm/hugetlb.c 2008-07-17 09:54:19.000000000 -0500 +1
-1- entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); + entry = pte_wrprotect(mk_pte(page, pgprot));}
entry =pte_mkyoung(entry);
entry =pte_mkhuge(entry);Why do we need to do something different for Xen here?
Quoted 2 lines
In the original implementation
_PAGE_PRESENTis set in later macros. Xen needs it early to trigger the properphys_to_machine()translation.
That should then better be taken care of in the place where _PAGE_PRESENT gets actually set. Also, doesn't vma->vm_page_prot already properly reflect the intended setting of _PAGE_PRESENT?
Jan
Re: Re: [PATCH 2/2] Linux support for hugepages as a Xen PV guest
In reply to Dave McCrackenQuoted 6 lines · Dave McCracken
Quoted 3 lines · Keir Fraser
hugetlbfs should only be enabled at runtime if supported by the hypervisor. Perhaps we can enable the PSE CPUID flag if the hypervisor supports superpages?
I'll look into it.
Opening the cover to this exposed some very interesting things on the Linux side.
For starters, Xen currently does remove PSE from the CPUID it returns to the guest. I'll add some code to allow PSE when hugepages are enabled.
Second, in Linux for the x86_64 architecture the macro to check for PSE is hard-wired to always be true.
Third, hugepages in Linux makes no check whatsoever for the presence or absence of PSE. It assumes it's there.
I'll look into the simplest way to make the various Linux parts obey the PSE flag properly.
Dave McCracken
Oracle Corp.
This is all for the Linux-xen-2.6.18 tree on xensource.
Re: Re: [PATCH 2/2] Linux support for hugepages as a Xen PV guest
Quoted 2 lines · Dave McCracken
For starters, Xen currently does remove PSE from the CPUID it returns to the guest. I'll add some code to allow PSE when hugepages are enabled.
Yep.
Quoted 2 lines
Second, in Linux for the
x86_64architecture the macro to check for PSE is hard-wired to always be true.
Not in current kernels.
Quoted 2 lines
Third, hugepages in Linux makes no check whatsoever for the presence or absence of PSE. It assumes it's there.
Not in current kernels. But it does assume that if PSE is present, then physical memory can always be mapped with large pages.
Quoted 2 lines
I'll look into the simplest way to make the various Linux parts obey the PSE flag properly.
No, use a current mainline git linux kernel.
J
Re: Re: [PATCH 2/2] Linux support for hugepages as a Xen PV guest
Quoted 1 line · Jeremy Fitzhardinge
No, use a current mainline git linux kernel.
I'm currently using xen-unstable and linux-2.6.18-xen as the base for my development. Are you saying I should move to the current Linux mainline instead? Is linux-2.6.18-xen now obsolete?
Dave McCracken
Re: Re: [PATCH 2/2] Linux support for hugepages as a Xen PV guest
Quoted 1 line · Dave McCracken
I'm currently using xen-unstable and linux-2.6.18-xen as the base for my development. Are you saying I should move to the current Linux mainline instead?
Yes.
Quoted 2 lines
Is linux-2.6.18-xen now obsolete?
More or less. Mainline is missing a few features, but it is stable and useful. 2.6.18-xen shouldn't be used for any new work.
J
Re: [PATCH 2/2] Linux support for hugepages as a Xen PVguest
In reply to [email protected]./include/asm-x86_64/mach-xen/asm/pgtable.h static inline unsigned long pud_bad(pud_ · line 268
Quoted 5 lines
./include/asm-x86_64/mach-xen/asm/pgtable.h renamed from /./include/asm-x86_64/mach-xen/asm/pgtable.h +5
-0+static inline void set_huge_pte_at (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pteval) { +if ((mm != current->mm && mm != &init_mm) || +HYPERVISOR_update_va_mapping (addr, pteval,0 )) +set_pmd ((pmd_t *)ptep, (pmd_t ){__pte_val (pteval)}); +}
Your Xen patch made no attempt at implementing L2 modifications through update_va_mapping, so why have the guest even try?
./include/asm-x86_64/mach-xen/asm/pgtable.h static inline pte_t ptep_get_and_clear(s · line 312
Quoted 8 lines
./include/asm-x86_64/mach-xen/asm/pgtable.h renamed from /./include/asm-x86_64/mach-xen/asm/pgtable.h +8
-0+static inline pte_t huge_ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) +{ +pte_t pte = *ptep; +if (!pte_none (pte)) { +if ((mm != &init_mm) || +HYPERVISOR_update_va_mapping (addr,__pte (0 ),0 )) { + pte = *ptep; +set_pmd ((pmd_t *)ptep,__pmd (0 ));
Same here.
Jan
./xen/include/asm-x86/x86_32/page.h extern unsigned int PAGE_HYPERVISOR_NOCA · line 118
Quoted 11 lines
./xen/include/asm-x86/x86_32/page.h renamed from /./xen/include/asm-x86/x86_32/page.h +1
-1@@ -112,7 +112,7 @@ extern unsigned int PAGE_HYPERVISOR_NOCA* Disallow unused flag bits plus PAT/PSE, PCD, PWT and GLOBAL. * Permit the NX bit if the hardware supports it. */ -#define BASE_DISALLOW_MASK (0xFFFFF198U & ~_PAGE_NX) +#define BASE_DISALLOW_MASK (0xFFFFF118U & ~_PAGE_NX)#define L1_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_GNTTAB)#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK)
You ought to or in the bit you removed from BASE_DISALLOW_MASK into L1_DISALLOW_MASK.
./xen/include/asm-x86/x86_64/page.h typedef l4_pgentry_t root_pgentry_t; · line 118
Quoted 11 lines
./xen/include/asm-x86/x86_64/page.h renamed from /./xen/include/asm-x86/x86_64/page.h +1
-1@@ -112,7 +112,7 @@ typedef l4_pgentry_t root_pgentry_t;* Permit the NX bit if the hardware supports it. * Note that range [62:52] is available for software use on x86/64. */ -#define BASE_DISALLOW_MASK (0xFF800198U & ~_PAGE_NX) +#define BASE_DISALLOW_MASK (0xFF800118U & ~_PAGE_NX)#define L1_DISALLOW_MASK (BASE_DISALLOW_MASK | _PAGE_GNTTAB)#define L2_DISALLOW_MASK (BASE_DISALLOW_MASK)
Same here, but also for L3 (unless you also support Gb pages) and L4.
./xen/arch/x86/mm.c unsigned long total_pages; · line 168
Quoted 12 lines
./xen/arch/x86/mm.c renamed from /./xen/arch/x86/mm.c +3
-0@@ -160,6 +160,9 @@ unsigned long total_pages;#define PAGE_CACHE_ATTRS (_PAGE_PAT|_PAGE_PCD|_PAGE_PWT) +static int opt_allow_hugepage =0 ; +boolean_param ("allowhugepage" , opt_allow_hugepage); +#define l1_disallow_mask (d) \ ((d != dom_io) && \ (rangeset_is_empty((d)->iomem_caps) && \
And you also need to adjust l1_disallow_mask() to honor opt_allow_hugepage.
./xen/arch/x86/mm.c get_page_from_l2e( · line 801
Quoted 5 lines
./xen/arch/x86/mm.c renamed from /./xen/arch/x86/mm.c +5
-0+ rc =get_data_page (page, d, writeable); +if (unlikely (!rc) ) +return rc; + +for ( m = mfn+1 , me = m + (L1_PAGETABLE_ENTRIES -1 ); m <= me; m++ )
Isn't this off by one (i.e. shouldn't the condition be m < me)?
./xen/arch/x86/mm.c get_page_from_l2e( · line 814
Quoted 2 lines
./xen/arch/x86/mm.c renamed from /./xen/arch/x86/mm.c +2
-0+map_pages_to_xen ((unsigned long )mfn_to_virt (mfn), mfn,L1_PAGETABLE_ENTRIES , +PAGE_HYPERVISOR |l2e_get_flags (l2e));
You need to translate flags to l1 notion here - map_pages_to_xen() expects it this way. Even more, it's not valid to simply or in PAGE_HYPERVISOR - you need to follow the same logic as used in get_page_from_l1e().
Quoted 1 line
+ for ( m = mfn+1, me = m + (L1_PAGETABLE_ENTRIES-1); m <= me; m++ )
Same as above.
Jan