Skip to content

Commit 92be3d6

Browse files
yhuang-intelIngo Molnar
authored andcommitted
kexec/i386: allocate page table pages dynamically
Impact: save .text size when kexec is built in but not loaded This patch adds an architecture specific struct kimage_arch into struct kimage. The pointers to page table pages used by kexec are added to struct kimage_arch. The page tables pages are dynamically allocated in machine_kexec_prepare instead of statically from BSS segment. This will save up to 20k memory when kexec image is not loaded. Signed-off-by: Huang Ying <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent 31498a0 commit 92be3d6

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

arch/x86/include/asm/kexec.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ relocate_kernel(unsigned long indirection_page,
170170
unsigned long start_address) ATTRIB_NORET;
171171
#endif
172172

173+
#ifdef CONFIG_X86_32
174+
#define ARCH_HAS_KIMAGE_ARCH
175+
176+
struct kimage_arch {
177+
pgd_t *pgd;
178+
#ifdef CONFIG_X86_PAE
179+
pmd_t *pmd0;
180+
pmd_t *pmd1;
181+
#endif
182+
pte_t *pte0;
183+
pte_t *pte1;
184+
};
185+
#endif
186+
173187
#endif /* __ASSEMBLY__ */
174188

175189
#endif /* _ASM_X86_KEXEC_H */

arch/x86/kernel/machine_kexec_32.c

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/numa.h>
1414
#include <linux/ftrace.h>
1515
#include <linux/suspend.h>
16+
#include <linux/gfp.h>
1617

1718
#include <asm/pgtable.h>
1819
#include <asm/pgalloc.h>
@@ -25,15 +26,6 @@
2526
#include <asm/system.h>
2627
#include <asm/cacheflush.h>
2728

28-
#define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
29-
static u32 kexec_pgd[1024] PAGE_ALIGNED;
30-
#ifdef CONFIG_X86_PAE
31-
static u32 kexec_pmd0[1024] PAGE_ALIGNED;
32-
static u32 kexec_pmd1[1024] PAGE_ALIGNED;
33-
#endif
34-
static u32 kexec_pte0[1024] PAGE_ALIGNED;
35-
static u32 kexec_pte1[1024] PAGE_ALIGNED;
36-
3729
static void set_idt(void *newidt, __u16 limit)
3830
{
3931
struct desc_ptr curidt;
@@ -76,6 +68,37 @@ static void load_segments(void)
7668
#undef __STR
7769
}
7870

71+
static void machine_kexec_free_page_tables(struct kimage *image)
72+
{
73+
free_page((unsigned long)image->arch.pgd);
74+
#ifdef CONFIG_X86_PAE
75+
free_page((unsigned long)image->arch.pmd0);
76+
free_page((unsigned long)image->arch.pmd1);
77+
#endif
78+
free_page((unsigned long)image->arch.pte0);
79+
free_page((unsigned long)image->arch.pte1);
80+
}
81+
82+
static int machine_kexec_alloc_page_tables(struct kimage *image)
83+
{
84+
image->arch.pgd = (pgd_t *)get_zeroed_page(GFP_KERNEL);
85+
#ifdef CONFIG_X86_PAE
86+
image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
87+
image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
88+
#endif
89+
image->arch.pte0 = (pte_t *)get_zeroed_page(GFP_KERNEL);
90+
image->arch.pte1 = (pte_t *)get_zeroed_page(GFP_KERNEL);
91+
if (!image->arch.pgd ||
92+
#ifdef CONFIG_X86_PAE
93+
!image->arch.pmd0 || !image->arch.pmd1 ||
94+
#endif
95+
!image->arch.pte0 || !image->arch.pte1) {
96+
machine_kexec_free_page_tables(image);
97+
return -ENOMEM;
98+
}
99+
return 0;
100+
}
101+
79102
/*
80103
* A architecture hook called to validate the
81104
* proposed image and prepare the control pages
@@ -87,13 +110,14 @@ static void load_segments(void)
87110
* reboot code buffer to allow us to avoid allocations
88111
* later.
89112
*
90-
* Make control page executable.
113+
* - Make control page executable.
114+
* - Allocate page tables
91115
*/
92116
int machine_kexec_prepare(struct kimage *image)
93117
{
94118
if (nx_enabled)
95119
set_pages_x(image->control_code_page, 1);
96-
return 0;
120+
return machine_kexec_alloc_page_tables(image);
97121
}
98122

99123
/*
@@ -104,6 +128,7 @@ void machine_kexec_cleanup(struct kimage *image)
104128
{
105129
if (nx_enabled)
106130
set_pages_nx(image->control_code_page, 1);
131+
machine_kexec_free_page_tables(image);
107132
}
108133

109134
/*
@@ -150,18 +175,18 @@ void machine_kexec(struct kimage *image)
150175
relocate_kernel_ptr = control_page;
151176
page_list[PA_CONTROL_PAGE] = __pa(control_page);
152177
page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
153-
page_list[PA_PGD] = __pa(kexec_pgd);
154-
page_list[VA_PGD] = (unsigned long)kexec_pgd;
178+
page_list[PA_PGD] = __pa(image->arch.pgd);
179+
page_list[VA_PGD] = (unsigned long)image->arch.pgd;
155180
#ifdef CONFIG_X86_PAE
156-
page_list[PA_PMD_0] = __pa(kexec_pmd0);
157-
page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
158-
page_list[PA_PMD_1] = __pa(kexec_pmd1);
159-
page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
181+
page_list[PA_PMD_0] = __pa(image->arch.pmd0);
182+
page_list[VA_PMD_0] = (unsigned long)image->arch.pmd0;
183+
page_list[PA_PMD_1] = __pa(image->arch.pmd1);
184+
page_list[VA_PMD_1] = (unsigned long)image->arch.pmd1;
160185
#endif
161-
page_list[PA_PTE_0] = __pa(kexec_pte0);
162-
page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
163-
page_list[PA_PTE_1] = __pa(kexec_pte1);
164-
page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
186+
page_list[PA_PTE_0] = __pa(image->arch.pte0);
187+
page_list[VA_PTE_0] = (unsigned long)image->arch.pte0;
188+
page_list[PA_PTE_1] = __pa(image->arch.pte1);
189+
page_list[VA_PTE_1] = (unsigned long)image->arch.pte1;
165190

166191
if (image->type == KEXEC_TYPE_DEFAULT)
167192
page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)

include/linux/kexec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ struct kimage {
100100
#define KEXEC_TYPE_DEFAULT 0
101101
#define KEXEC_TYPE_CRASH 1
102102
unsigned int preserve_context : 1;
103+
104+
#ifdef ARCH_HAS_KIMAGE_ARCH
105+
struct kimage_arch arch;
106+
#endif
103107
};
104108

105109

0 commit comments

Comments
 (0)