x86: don't use a memory page for mapping the shared info page
cover x86: don't use a memory page for mapping the shared info page
This is a small add-on series after the live-update series sent a week ago.
I realized that having the shared info page in the normal RAM area is a bad idea when considering kexec, as the new kernel might want it at a different location.
So this series is moving the shared info page away from the RAM areas, resulting in a net win of one memory page.
Juergen Gross (2):
mm: provide a way to do very early page table allocations
x86: don't use a memory page for mapping the shared info page
arch/x86/mm.c | 23 +++++++++++++++-------- arch/x86/setup.c | 15 ++++++++------- arch/x86/x86_32.S | 7 +------ arch/x86/x86_64.S | 7 +------ hypervisor.c | 15 +++++++++++---- 5 files changed, 36 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 mm: provide a way to do very early page table allocations
Add a small pool of statically allocated memory pages to be handed out for very early page table allocations.
This will make it possible to do virtual allocations e.g. for mapping the shared info page.
--- arch/x86/mm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/mm.c +15 -1
@@ -640,12 +640,17 @@ void change_readonly(bool readonly)* return a valid PTE for a given virtual address. If PTE does not exist, * allocate page-table pages. */ +#define N_PTS 4 +static char early_pt[PAGE_SIZE * N_PTS] __attribute__((aligned(PAGE_SIZE))); +static unsigned long n_early_pt =N_PTS ; +static int need_pgt_func (unsigned long va,unsigned int lvl,bool is_leaf,pgentry_t *pte,void *par) {pgentry_t **result = par;unsigned long pt_mfn;unsigned long pt_pfn; +unsigned long pt_addr;unsigned int idx;if ( !is_leaf )
@@ -664,7 +669,16 @@ static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, } pt_mfn =virt_to_mfn (pte); - pt_pfn =virt_to_pfn (alloc_page ()); +if ( n_early_pt ) + { + n_early_pt--; + pt_addr = (unsigned long )&early_pt[n_early_pt *PAGE_SIZE ]; + } +else + { + pt_addr =alloc_page (); + } + pt_pfn =virt_to_pfn (pt_addr);if ( !pt_pfn )return -1 ; idx =idx_from_va_lvl (va, lvl);
Signature · 1 line
2.43.0
Re: [MINI-OS PATCH 1/2] mm: provide a way to do very early page table allocations
Quoted 16 lines · Juergen Gross
Add a small pool of statically allocated memory pages to be handed out for very early page table allocations.
This will make it possible to do virtual allocations e.g. for mapping the shared info page.
Signed-off-by Juergen Gross <[email protected]>--- arch/x86/mm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/mm.c +0
-0
arch/x86/mm.c void change_readonly(bool readonly) · line 643
Quoted 4 lines
arch/x86/mm.c +1
-0@@ -640,12 +640,17 @@ void change_readonly(bool readonly)* return a valid PTE for a given virtual address. If PTE does not exist, * allocate page-table pages. */ +#define N_PTS 4
Wouldn't it be prudent to have a comment here mentioning how this number was derived, i.e. what's known to be covered? (To map the shared info page I expect you really only need 3? Hence without a comment things may remain unclear.)
arch/x86/mm.c void change_readonly(bool readonly) · line 644
Quoted 1 line
arch/x86/mm.c +1
-0+static char early_pt[PAGE_SIZE * N_PTS] __attribute__((aligned(PAGE_SIZE)));
Maybe better early_pt[N_PTS][PAGE_SIZE], simplifying the allocation code below a little?
arch/x86/mm.c void change_readonly(bool readonly) · line 645
Quoted 1 line
arch/x86/mm.c +1
-0+static unsigned long n_early_pt =N_PTS ;
unsigned int would do, I expect? With the suggestion above this could then also use ARRAY_SIZE(), at which point there would be no real need for N_PTS anymore.
Quoted 14 lines
arch/x86/mm.c +1
-0static int need_pgt_func (unsigned long va,unsigned int lvl,bool is_leaf,pgentry_t *pte,void *par) {pgentry_t **result = par;unsigned long pt_mfn;unsigned long pt_pfn; +unsigned long pt_addr;unsigned int idx;if ( !is_leaf )@@ -664,7 +669,16 @@ static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, } pt_mfn =virt_to_mfn (pte);
arch/x86/mm.c static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, · line 680
Quoted 10 lines
arch/x86/mm.c +9
-1- pt_pfn =virt_to_pfn (alloc_page ()); +if ( n_early_pt ) + { + n_early_pt--; + pt_addr = (unsigned long )&early_pt[n_early_pt *PAGE_SIZE ]; + } +else + { + pt_addr =alloc_page (); + }
The failure pattern when one fails to increase early_pt[] is likely going to be problematic. Wouldn't it be better to check for failure here?
Jan
Re: [MINI-OS PATCH 1/2] mm: provide a way to do very early page table allocations
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Juergen Gross
Add a small pool of statically allocated memory pages to be handed out for very early page table allocations.
This will make it possible to do virtual allocations e.g. for mapping the shared info page.
Signed-off-by Juergen Gross <[email protected]>--- arch/x86/mm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
Quoted 14 lines
arch/x86/mm.c void change_readonly(bool readonly) · line 643
Quoted 9 lines
arch/x86/mm.c +1
-0@@ -640,12 +640,17 @@ void change_readonly(bool readonly)* return a valid PTE for a given virtual address. If PTE does not exist, * allocate page-table pages. */ +#define N_PTS 4Wouldn't it be prudent to have a comment here mentioning how this number was derived, i.e. what's known to be covered? (To map the shared info page I expect you really only need 3? Hence without a comment things may remain unclear.)
Yes, 3 would have been enough. OTOH having another spare doesn't hurt, as the memory will be allocated anyway.
I'll add a comment in this regard.
Quoted 5 lines
arch/x86/mm.c void change_readonly(bool readonly) · line 644
Quoted 2 lines
arch/x86/mm.c +1
-0+static char early_pt[PAGE_SIZE * N_PTS] __attribute__((aligned(PAGE_SIZE)));Maybe better early_pt[N_PTS][PAGE_SIZE], simplifying the allocation code below a little?
Yes, good idea.
Quoted 6 lines
arch/x86/mm.c void change_readonly(bool readonly) · line 645
Quoted 2 lines
arch/x86/mm.c +1
-0+static unsigned long n_early_pt =N_PTS ;unsigned int would do, I expect? With the suggestion above this could then also use
ARRAY_SIZE(), at which point there would be no real need forN_PTSanymore.
True.
Quoted 14 lines
Quoted 14 lines
arch/x86/mm.c +1
-0static int need_pgt_func (unsigned long va,unsigned int lvl,bool is_leaf,pgentry_t *pte,void *par) {pgentry_t **result = par;unsigned long pt_mfn;unsigned long pt_pfn; +unsigned long pt_addr;unsigned int idx;if ( !is_leaf )@@ -664,7 +669,16 @@ static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, }
Quoted 14 lines
arch/x86/mm.c static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, · line 680
Quoted 11 lines
arch/x86/mm.c +9
-1pt_mfn =virt_to_mfn (pte); - pt_pfn =virt_to_pfn (alloc_page ()); +if ( n_early_pt ) + { + n_early_pt--; + pt_addr = (unsigned long )&early_pt[n_early_pt *PAGE_SIZE ]; + } +else + { + pt_addr =alloc_page (); + }The failure pattern when one fails to increase early_pt[] is likely going to be problematic. Wouldn't it be better to check for failure here?
Hmm, not sure this is true. I tried the shared info mapping without adding the special early alloc code first and finding the bug was quite easy.
Juergen
Re: [MINI-OS PATCH 1/2] mm: provide a way to do very early page table allocations
Quoted 20 lines · Jürgen Groß
Quoted 4 lines · Jan Beulich
Quoted 3 lines · Juergen Gross
(quoted diff) +0
-0@@ -664,7 +669,16 @@ static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, }Quoted 14 lines
arch/x86/mm.c static int need_pgt_func(unsigned long va, unsigned int lvl, bool is_leaf, · line 680
Quoted 11 lines
arch/x86/mm.c +9
-1pt_mfn =virt_to_mfn (pte); - pt_pfn =virt_to_pfn (alloc_page ()); +if ( n_early_pt ) + { + n_early_pt--; + pt_addr = (unsigned long )&early_pt[n_early_pt *PAGE_SIZE ]; + } +else + { + pt_addr =alloc_page (); + }The failure pattern when one fails to increase early_pt[] is likely going to be problematic. Wouldn't it be better to check for failure here?
Quoted 2 lines
Hmm, not sure this is true. I tried the shared info mapping without adding the special early alloc code first and finding the bug was quite easy.
Feel free to ignore my comment then.
Jan
PATCH 2/2 x86: don't use a memory page for mapping the shared info page
In reply to Juergen GrossThere is no need to use a populated memory page for mapping the shared info page at that location. Just use an allocated virtual address for the shared info page. For PVH allocate an unused pfn.
--- arch/x86/mm.c | 7 ------- arch/x86/setup.c | 15 ++++++++------- arch/x86/x86_32.S | 7 +------ arch/x86/x86_64.S | 7 +------ hypervisor.c | 15 +++++++++++---- 5 files changed, 21 insertions(+), 30 deletions(-)
arch/x86/mm.c +0 -7
@@ -497,7 +497,6 @@ static void build_pagetable(unsigned long *start_pfn, unsigned long *max_pfn)/* * Mark portion of the address space read only. */ -extern struct shared_info shared_info;struct change_readonly_par {unsigned long etext;
@@ -519,12 +518,6 @@ static int change_readonly_func(unsigned long va, unsigned int lvl,if ( va + (1UL << ptdata[lvl].shift) > ro->etext )return 1 ; -if ( va == (unsigned long )&shared_info ) - { -printk ("skipped %lx\n" , va); -return 0 ; - } - newval = ro->readonly ? (*pte & ~_PAGE_RW) : (*pte | _PAGE_RW);#ifdef CONFIG_PARAVIRT
arch/x86/setup.c +8 -7
@@ -47,8 +47,6 @@ shared_info_t *HYPERVISOR_shared_info;*/ char stack[2 *STACK_SIZE ]; -extern char shared_info[PAGE_SIZE ]; -static inline void fpu_init (void ) { asmvolatile ("fninit" ); }
@@ -76,18 +74,21 @@ static void set_info_ptr(start_info_t *ptr)#define hpc_init () +static unsigned long shared_info_va; +shared_info_t *map_shared_info (void ) {int rc; -unsigned long pa = start_info_ptr->shared_info; -if ( (rc =HYPERVISOR_update_va_mapping ((unsigned long )shared_info, -__pte (pa |7 ),UVMF_INVLPG )) ) +if ( !shared_info_va ) + shared_info_va =alloc_virt_kernel (1 ); + rc =map_frame_rw (shared_info_va,PHYS_PFN (start_info_ptr->shared_info)); +if ( rc ) {printk ("Failed to map shared_info!! rc=%d\n" , rc);do_exit (); } -return (shared_info_t *)shared_info; +return (shared_info_t *)shared_info_va; }void unmap_shared_info (void )
@@ -95,7 +96,7 @@ void unmap_shared_info(void)int rc;pte_t nullpte = { }; -if ( (rc =HYPERVISOR_update_va_mapping ((unsigned long )shared_info, +if ( (rc =HYPERVISOR_update_va_mapping (shared_info_va, nullpte,UVMF_INVLPG )) ) {printk ("Failed to unmap shared_info page!! rc=%d\n" , rc);
arch/x86/x86_32.S +1 -6
@@ -36,13 +36,8 @@ _start: stack_start: .long stack+(2*__STACK_SIZE), __KERNEL_SS -.globl shared_info, hypercall_page - /* Unpleasant -- the PTE that maps this page is actually overwritten */ - /* to map the real shared-info page! :-) */ .align __PAGE_SIZE -shared_info: - .fill __PAGE_SIZE,1,0 - +.globl hypercall_page hypercall_page: .fill __PAGE_SIZE,1,0
arch/x86/x86_64.S +1 -6
@@ -33,13 +33,8 @@ _start: stack_start: .quad stack+(2*__STACK_SIZE) -.globl shared_info, hypercall_page - /* Unpleasant -- the PTE that maps this page is actually overwritten */ - /* to map the real shared-info page! :-) */ .align __PAGE_SIZE -shared_info: - .fill __PAGE_SIZE,1,0 - +.globl hypercall_page hypercall_page: .fill __PAGE_SIZE,1,0
hypervisor.c +11 -4
@@ -27,8 +27,10 @@#include <mini-os/os.h> #include <mini-os/lib.h> +#include <mini-os/e820.h> #include <mini-os/hypervisor.h> #include <mini-os/events.h> +#include <mini-os/mm.h> #include <xen/memory.h> EXPORT_SYMBOL (hypercall_page);
@@ -37,7 +39,8 @@ EXPORT_SYMBOL(hypercall_page); ((sh)->evtchn_pending[idx] & ~(sh)->evtchn_mask[idx])#ifndef CONFIG_PARAVIRT -extern shared_info_t shared_info; +static unsigned long shinfo_pfn; +static unsigned long shinfo_va;int hvm_get_parameter (int idx,uint64_t *value) {
@@ -69,14 +72,16 @@ shared_info_t *map_shared_info(void) {struct xen_add_to_physmap xatp; + shinfo_pfn =e820_get_reserved_pfns (1 ); xatp.domid =DOMID_SELF ; xatp.idx =0 ; xatp.space = XENMAPSPACE_shared_info; - xatp.gpfn =virt_to_pfn (&shared_info); + xatp.gpfn = shinfo_pfn;if (HYPERVISOR_memory_op (XENMEM_add_to_physmap, &xatp) !=0 )BUG (); + shinfo_va =map_frame_virt (shinfo_pfn); -return &shared_info; +return (shared_info_t *)shinfo_va; }void unmap_shared_info (void )
@@ -84,9 +89,11 @@ void unmap_shared_info(void)struct xen_remove_from_physmap xrtp; xrtp.domid =DOMID_SELF ; - xrtp.gpfn =virt_to_pfn (&shared_info); + xrtp.gpfn = shinfo_pfn;if (HYPERVISOR_memory_op (XENMEM_remove_from_physmap, &xrtp) !=0 )BUG (); +unmap_frames (shinfo_va,1 ); + e820_put_reserved_pfns(shinfo_pfn, 1); }#endif
Signature · 1 line
2.43.0
Quoted 26 lines · Juergen Gross
hypervisor.c +4
-1@@ -27,8 +27,10 @@#include <mini-os/os.h> #include <mini-os/lib.h> +#include <mini-os/e820.h> #include <mini-os/hypervisor.h> #include <mini-os/events.h> +#include <mini-os/mm.h> #include <xen/memory.h> EXPORT_SYMBOL (hypercall_page);@@ -37,7 +39,8 @@ EXPORT_SYMBOL(hypercall_page); ((sh)->evtchn_pending[idx] & ~(sh)->evtchn_mask[idx])#ifndef CONFIG_PARAVIRT -extern shared_info_t shared_info; +static unsigned long shinfo_pfn; +static unsigned long shinfo_va;int hvm_get_parameter (int idx,uint64_t *value) {@@ -69,14 +72,16 @@ shared_info_t *map_shared_info(void) {struct xen_add_to_physmap xatp;
hypervisor.c shared_info_t *map_shared_info(void) · line 82
Quoted 10 lines
hypervisor.c +3
-1+ shinfo_pfn =e820_get_reserved_pfns (1 ); xatp.domid =DOMID_SELF ; xatp.idx =0 ; xatp.space = XENMAPSPACE_shared_info; - xatp.gpfn =virt_to_pfn (&shared_info); + xatp.gpfn = shinfo_pfn;if (HYPERVISOR_memory_op (XENMEM_add_to_physmap, &xatp) !=0 )BUG (); + shinfo_va =map_frame_virt (shinfo_pfn);
In the PV variant you first check whether you already have a VA. Why is that needed there, but not here? (Originally I meant to ask why you don't use map_frame_virt() there as well.)
Talking of map_frame_virt() - I take it its 2nd parameter being named "mfn" is kind of stale (pre-dating the addition of PVH support)?
hypervisor.c void unmap_shared_info(void) · line 95
Quoted 9 lines
hypervisor.c +2
-1@@ -84,9 +89,11 @@ void unmap_shared_info(void)struct xen_remove_from_physmap xrtp; xrtp.domid =DOMID_SELF ; - xrtp.gpfn =virt_to_pfn (&shared_info); + xrtp.gpfn = shinfo_pfn;if (HYPERVISOR_memory_op (XENMEM_remove_from_physmap, &xrtp) !=0 )BUG (); +unmap_frames (shinfo_va,1 );
Better do this ahead of the hypercall?
Jan
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Juergen Gross
hypervisor.c +4
-1@@ -27,8 +27,10 @@#include <mini-os/os.h> #include <mini-os/lib.h> +#include <mini-os/e820.h> #include <mini-os/hypervisor.h> #include <mini-os/events.h> +#include <mini-os/mm.h> #include <xen/memory.h> EXPORT_SYMBOL (hypercall_page);@@ -37,7 +39,8 @@ EXPORT_SYMBOL(hypercall_page); ((sh)->evtchn_pending[idx] & ~(sh)->evtchn_mask[idx])#ifndef CONFIG_PARAVIRT -extern shared_info_t shared_info; +static unsigned long shinfo_pfn; +static unsigned long shinfo_va;int hvm_get_parameter (int idx,uint64_t *value) {@@ -69,14 +72,16 @@ shared_info_t *map_shared_info(void) {struct xen_add_to_physmap xatp;
Quoted 13 lines
hypervisor.c shared_info_t *map_shared_info(void) · line 82
Quoted 9 lines
hypervisor.c +3
-1+ shinfo_pfn =e820_get_reserved_pfns (1 ); xatp.domid =DOMID_SELF ; xatp.idx =0 ; xatp.space = XENMAPSPACE_shared_info; - xatp.gpfn =virt_to_pfn (&shared_info); + xatp.gpfn = shinfo_pfn;if (HYPERVISOR_memory_op (XENMEM_add_to_physmap, &xatp) !=0 )BUG (); + shinfo_va =map_frame_virt (shinfo_pfn);In the PV variant you first check whether you already have a VA. Why is that needed there, but not here? (Originally I meant to ask why you don't use
map_frame_virt()there as well.)
Good catch. It is needed here, otherwise we'll leak the virtual page during a suspend/resume cycle.
Quoted 3 lines
Talking of
map_frame_virt()- I take it its 2nd parameter being named "mfn" is kind of stale (pre-dating the addition of PVH support)?
I think it was misnamed back then.
Quoted 4 lines
Quoted 4 lines
hypervisor.c +0
-0@@ -84,9 +89,11 @@ void unmap_shared_info(void)struct xen_remove_from_physmap xrtp;
Quoted 8 lines
hypervisor.c void unmap_shared_info(void) · line 95
Quoted 6 lines
hypervisor.c +2
-1xrtp.domid =DOMID_SELF ; - xrtp.gpfn =virt_to_pfn (&shared_info); + xrtp.gpfn = shinfo_pfn;if (HYPERVISOR_memory_op (XENMEM_remove_from_physmap, &xrtp) !=0 )BUG (); +unmap_frames (shinfo_va,1 );Better do this ahead of the hypercall?
Agreed.
Juergen