Hibernation: Arbitrary boot kernel support on x86_64
Subsystems (2)
cover Hibernation: Arbitrary boot kernel support on x86_64
Hi,
The following patches make it possible to restore a hibernation image with the help of a kernel different from the image one.
The first patch adds the generic, platform independent code needed for that. The second patch implements the idea on x86_64 and the two remaining patches are generalizations.
Details in the changelogs, comments welcome.
Greetings,
Rafael
Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
RFC PATCH 1/4 Hibernation: Arbitrary boot kernel support - generic code
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.
--- kernel/power/power.h | 6 +++- kernel/power/snapshot.c | 60 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 17 deletions(-)
kernel/power/power.h +4 -2
@@ -11,14 +11,16 @@ struct swsusp_info {unsigned long size; } __attribute__((aligned(PAGE_SIZE))); - -#ifdef CONFIG_HIBERNATION +/* Maximum size of architecture specific data in a hibernation header */ +#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) +/* * Keep some memory free so that I/O operations can succeed without paging * [Might this be more than 4 MB?] */ #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) +/* * Keep 1 MB of memory free so that device drivers can allocate some pages in * their .suspend() routines without breaking the suspend to disk.
kernel/power/snapshot.c +45 -15
91 lines not on this page · raw message
Re: [RFC][PATCH -mm 1/4] Hibernation: Arbitrary boot kernel support - generic code
Quoted 11 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct
new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 1/4] Hibernation: Arbitrary boot kernel support - generic code
In reply to Rafael J. WysockiQuoted 11 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct
new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
RFC PATCH 1/4 Hibernation: Arbitrary boot kernel support - generic code
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Add the bits needed for supporting arbitrary boot kernels to the common hibernation code.
To support arbitrary boot kernels, make it possible to replace the 'struct new_utsname' and the kernel version in the hibernation image header by some architecture specific data that will be used to verify if the image is valid and to restore the image.
--- kernel/power/power.h | 6 +++- kernel/power/snapshot.c | 60 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 17 deletions(-)
kernel/power/power.h +4 -2
@@ -11,14 +11,16 @@ struct swsusp_info {unsigned long size; } __attribute__((aligned(PAGE_SIZE))); - -#ifdef CONFIG_HIBERNATION +/* Maximum size of architecture specific data in a hibernation header */ +#define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) +/* * Keep some memory free so that I/O operations can succeed without paging * [Might this be more than 4 MB?] */ #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) +/* * Keep 1 MB of memory free so that device drivers can allocate some pages in * their .suspend() routines without breaking the suspend to disk.
kernel/power/snapshot.c +45 -15
91 lines not on this page · raw message
RFC PATCH 2/4 Hibernation: Arbitrary boot kernel support on x86_64
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on x86_64 with the help of a kernel different from the one in the image.
The idea is to split the core restoration code into two separate parts and to place each of them in a different page. The first part belongs to the boot kernel and is executed as the last step of the image kernel's memory restoration procedure. It restores all of the image kernel's memory that has not been restored yet except for the one page containing the very code that is being executed at that time. The final operation performed by it is a jump to the second part of the core restoration code that belongs to the image kernel and has just been restored. This code restores the last remaining page of the image kernel's memory containing the first, already executed, part of the core restoration code (temporary page tables created by the boot kernel are used at this stage). It also makes the CPU switch to the image kernel's page tables and restores the state of general purpose registers (including the stack pointer) from before the hibernation.
The main issue with this idea is that in order to jump to the second part of the restoration code the boot kernel needs to know its address. However, this address may be passed to it in the image header. Namely, the part of the image header previously used for checking if the version of the image kernel is correct can be replaced with some architecture specific data that will allow the boot kernel to jump to the right address within the image kernel. These data should also be used for checking if the image kernel is compatible with the boot kernel (as far as the memory restroration procedure is concerned). It can be done, for example, with the help of a "magic" value that has to be equal in both kernels, so that they can be regarded as compatible.
--- arch/x86_64/kernel/suspend.c | 43 ++++++++++++++++++++++++++ arch/x86_64/kernel/suspend_asm.S | 63 +++++++++++++++++++++++++++++++++------ include/asm-x86_64/suspend.h | 6 +++ 3 files changed, 103 insertions(+), 9 deletions(-)
arch/x86_64/kernel/suspend_asm.S +54 -9
@@ -2,8 +2,8 @@ * * Distribute under GPLv2. * - * swsusp_arch_resume may not use any stack, nor any variable that is - * not "NoSave" during copying pages: + * swsusp_arch_resume must not use any stack or any nonlocal variables while + * copying pages: * * Its rewriting one kernel image with another. What is stack in "old" * image could very well be data page in "new" image, and overwriting
@@ -36,10 +36,20 @@ ENTRY(swsusp_arch_suspend) pushfq popq pt_regs_eflags(%rax) + /* save the address of restore_registers */ + movq $restore_registers, %rax + movq %rax, restore_jump_address(%rip) + call swsusp_save ret ENTRY(restore_image) + /* compute the address of the page we are at and store it in R9 */ + movq $(restore_image - __START_KERNEL_map), %rax + movq $__PAGE_OFFSET, %r9 + addq %rax, %r9 + andq $PAGE_MASK, %r9 + /* switch to temporary page tables */ movq $__PAGE_OFFSET, %rdx movq temp_level4_pgt(%rip), %rax
@@ -54,6 +64,11 @@ ENTRY(restore_image) movq %rcx, %cr3; movq %rax, %cr4; # turn PGE back on + /* prepare to jump to the image kernel */ + movq restore_jump_address(%rip), %rax + + /* copy image data to their original locations */ + xorq %r10, %r10 movq restore_pblist(%rip), %rdx loop: testq %rdx, %rdx
@@ -62,16 +77,46 @@ loop: /* get addresses from the pbe and copy the page */ movq pbe_address(%rdx), %rsi movq pbe_orig_address(%rdx), %rdi - movq $512, %rcx + /* skip the page we are at (address stored in R9) */ + cmpq %rdi, %r9 + jne 1f + /* save the address of the data to be copied to the skipped page */ + movq %rsi, %r10 + jmp 2f +1: movq $(PAGE_SIZE >> 3), %rcx rep movsq /* progress to the next pbe */ - movq pbe_next(%rdx), %rdx +2: movq pbe_next(%rdx), %rdx jmp loop done: + /* jump to the restore_registers address from the image header */ + jmpq *%rax + /* + * NOTE: This assumes that the boot kernel's text mapping covers the + * image kernel's page containing restore_registers and the address of + * this page is the same as in the image kernel's text mapping (it + * should always be true, because the text mapping is linear, starting + * from 0, and is supposed to cover the entire kernel text for every + * kernel). + */ + +.balign PAGE_SIZE + /* code below belongs to the image kernel */ +ENTRY(restore_registers) + /* check if we have one more image page to copy */ + testq %r10, %r10 + jz 1f + /* copy the skipped page */ + movq %r10, %rsi + movq %r9, %rdi + movq $(PAGE_SIZE >> 3), %rcx + rep + movsq + /* go back to the original page tables */ - movq $(init_level4_pgt - __START_KERNEL_map), %rax +1: movq $(init_level4_pgt - __START_KERNEL_map), %rax addq phys_base(%rip), %rax movq %rax, %cr3
@@ -84,10 +129,7 @@ done: movq %rcx, %cr3 movq %rax, %cr4; # turn PGE back on - movl $24, %eax - movl %eax, %ds - - /* We don't restore %rax, it must be 0 anyway */ + /* restore GPRs (we don't restore %rax, it must be 0 anyway) */ movq $saved_context, %rax movq pt_regs_rsp(%rax), %rsp movq pt_regs_rbp(%rax), %rbp
@@ -109,4 +151,7 @@ done: xorq %rax, %rax + /* tell the hibernation core that we've just restored the memory */ + movq %rax, in_suspend(%rip) + ret
arch/x86_64/kernel/suspend.c +43 -0
53 lines not on this page · raw message
include/asm-x86_64/suspend.h +6 -0
@@ -43,4 +43,10 @@ extern void fix_processor_context(void);/* routines for saving/restoring kernel state */ extern int acpi_save_state_mem (void ); +#define ARCH_HAS_HIBERNATION_HEADER + +/* arch/x86_64/kernel/suspend.c */ +extern int arch_hibernation_header_save (void *addr,unsigned int max_size); +extern int arch_hibernation_header_restore (void *addr); +#endif /* __ASM_X86_64_SUSPEND_H */
Quoted 30 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with the help of a kernel different from the one in the image.The idea is to split the core restoration code into two separate parts and to place each of them in a different page. The first part belongs to the boot kernel and is executed as the last step of the image kernel's memory restoration procedure. It restores all of the image kernel's memory that has not been restored yet except for the one page containing the very code that is being executed at that time. The final operation performed by it is a jump to the second part of the core restoration code that belongs to the image kernel and has just been restored. This code restores the last remaining page of the image kernel's memory containing the first, already executed, part of the core restoration code (temporary page tables created by the boot kernel are used at this stage). It also makes the CPU switch to the image kernel's page tables and restores the state of general purpose registers (including the stack pointer) from before the hibernation.
The main issue with this idea is that in order to jump to the second part of the restoration code the boot kernel needs to know its address. However, this address may be passed to it in the image header. Namely, the part of the image header previously used for checking if the version of the image kernel is correct can be replaced with some architecture specific data that will allow the boot kernel to jump to the right address within the image kernel. These data should also be used for checking if the image kernel is compatible with the boot kernel (as far as the memory restroration procedure is concerned). It can be done, for example, with the help of a "magic" value that has to be equal in both kernels, so that they can be regarded as compatible.
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 30 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Make it possible to restore a hibernation image on
x86_64with the help of a kernel different from the one in the image.The idea is to split the core restoration code into two separate parts and to place each of them in a different page. The first part belongs to the boot kernel and is executed as the last step of the image kernel's memory restoration procedure. It restores all of the image kernel's memory that has not been restored yet except for the one page containing the very code that is being executed at that time. The final operation performed by it is a jump to the second part of the core restoration code that belongs to the image kernel and has just been restored. This code restores the last remaining page of the image kernel's memory containing the first, already executed, part of the core restoration code (temporary page tables created by the boot kernel are used at this stage). It also makes the CPU switch to the image kernel's page tables and restores the state of general purpose registers (including the stack pointer) from before the hibernation.
The main issue with this idea is that in order to jump to the second part of the restoration code the boot kernel needs to know its address. However, this address may be passed to it in the image header. Namely, the part of the image header previously used for checking if the version of the image kernel is correct can be replaced with some architecture specific data that will allow the boot kernel to jump to the right address within the image kernel. These data should also be used for checking if the image kernel is compatible with the boot kernel (as far as the memory restroration procedure is concerned). It can be done, for example, with the help of a "magic" value that has to be equal in both kernels, so that they can be regarded as compatible.
Quoted 1 line
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
RFC PATCH 3/4 Hibernation: Pass CR3 in the image header on x86_64
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Since we already pass the address of restore_registers() in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid using init_level4_pgt page tables during the restore.
--- arch/x86_64/kernel/suspend.c | 26 +++++++++++++++++++++----- arch/x86_64/kernel/suspend_asm.S | 8 +++++--- 2 files changed, 26 insertions(+), 8 deletions(-)
arch/x86_64/kernel/suspend.c +21 -5
@@ -9,6 +9,7 @@#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h>
@@ -150,6 +151,12 @@ extern int restore_image(void);*/ unsigned long restore_jump_address; +/* +* Value of the cr3 register from before the hibernation (this value is passed +* in the image header). +*/ +unsigned long restore_cr3; +pgd_t *temp_level4_pgt;static int res_phys_pud_init (pud_t *pud,unsigned long address,unsigned long end)
@@ -239,7 +246,9 @@ int pfn_is_nosave(unsigned long pfn)struct restore_data_record {unsigned long jump_address; -unsigned long control; +unsigned long cr3; +unsigned long magic; +u32 crc; };#define RESTORE_MAGIC 0x0123456789ABCDEFUL
@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a {struct restore_data_record *rdr = addr; -if (max_size <sizeof (struct restore_data_record )) +if (max_size <sizeof (*rdr))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ; + rdr->crc =0 ; + rdr->crc =crc32_le (0 , addr,sizeof (*rdr));return 0 ; }
@@ -268,9 +280,13 @@ int arch_hibernation_header_save(void *aint arch_hibernation_header_restore (void *addr) {struct restore_data_record *rdr = addr; +u32 crc; restore_jump_address = rdr->jump_address; -return (rdr->control == (restore_jump_address ^RESTORE_MAGIC )) ? -0 : -EINVAL ; + restore_cr3 = rdr->cr3; + crc = rdr->crc; + rdr->crc =0 ; +return (crc ==crc32_le (0 , addr,sizeof (*rdr)) + && rdr->magic ==RESTORE_MAGIC ) ?0 : -EINVAL ; }#endif /* CONFIG_HIBERNATION */
arch/x86_64/kernel/suspend_asm.S +5 -3
@@ -39,6 +39,9 @@ ENTRY(swsusp_arch_suspend) /* save the address of restore_registers */ movq $restore_registers, %rax movq %rax, restore_jump_address(%rip) + /* save cr3 */ + movq %cr3, %rax + movq %rax, restore_cr3(%rip) call swsusp_save ret
@@ -66,6 +69,7 @@ ENTRY(restore_image) /* prepare to jump to the image kernel */ movq restore_jump_address(%rip), %rax + movq restore_cr3(%rip), %rbx /* copy image data to their original locations */ xorq %r10, %r10
@@ -116,9 +120,7 @@ ENTRY(restore_registers) movsq /* go back to the original page tables */ -1: movq $(init_level4_pgt - __START_KERNEL_map), %rax - addq phys_base(%rip), %rax - movq %rax, %cr3 +1: movq %rbx, %cr3 /* Flush TLB, including "global" things (vmalloc) */ movq mmu_cr4_features(%rip), %rax
Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
Hi!
Quoted 14 lines
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.Signed-off-by Rafael J. Wysocki <[email protected]>arch/x86_64/kernel/suspend.c +0
-1@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a {struct restore_data_record *rdr = addr; -if (max_size <sizeof (struct restore_data_record ))
arch/x86_64/kernel/suspend.c int arch_hibernation_header_save(void *a · line 273
Quoted 10 lines
arch/x86_64/kernel/suspend.c +5
-1+if (max_size <sizeof (*rdr))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ; + rdr->crc =0 ; + rdr->crc =crc32_le (0 , addr,sizeof (*rdr));return 0 ; }
No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
In reply to Rafael J. WysockiHi!
Quoted 14 lines
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.Signed-off-by Rafael J. Wysocki <[email protected]>arch/x86_64/kernel/suspend.c +0
-1@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a {struct restore_data_record *rdr = addr; -if (max_size <sizeof (struct restore_data_record ))
arch/x86_64/kernel/suspend.c int arch_hibernation_header_save(void *a · line 273
Quoted 10 lines
arch/x86_64/kernel/suspend.c +5
-1+if (max_size <sizeof (*rdr))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ; + rdr->crc =0 ; + rdr->crc =crc32_le (0 , addr,sizeof (*rdr));return 0 ; }
No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
Quoted 32 lines · Pavel Machek
Hi!
Quoted 14 lines
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.Signed-off-by Rafael J. Wysocki <[email protected]>arch/x86_64/kernel/suspend.c +0
-1@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a {struct restore_data_record *rdr = addr; -if (max_size <sizeof (struct restore_data_record ))arch/x86_64/kernel/suspend.c int arch_hibernation_header_save(void *a · line 273
Quoted 10 lines
arch/x86_64/kernel/suspend.c +5
-1+if (max_size <sizeof (*rdr))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ; + rdr->crc =0 ; + rdr->crc =crc32_le (0 , addr,sizeof (*rdr));return 0 ; }No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field. What do you think?
Rafael
Re: [linux-pm] Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
Quoted 33 lines · Rafael J. Wysocki
Quoted 32 lines · Pavel Machek
Hi!
Quoted 14 lines
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.Signed-off-by Rafael J. Wysocki <[email protected]>(quoted diff) +0
-1@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a { struct restore_data_record *rdr = addr; - if (max_size < sizeof(struct restore_data_record))Review int arch_hibernation_header_save(void *a · line 273
Quoted 10 lines
(quoted diff) +5
-1+ if (max_size < sizeof(*rdr)) return -EOVERFLOW; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^ RESTORE_MAGIC); + rdr->cr3 = restore_cr3; + rdr->magic = RESTORE_MAGIC; + rdr->crc = 0; + rdr->crc = crc32_le(0, addr, sizeof(*rdr)); return 0; }No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 6 lines
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field.
Like in the patch below.
---
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of restore_registers() in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid using init_level4_pgt page tables during the restore.
--- arch/x86_64/kernel/suspend.c | 17 +++++++++++++---- arch/x86_64/kernel/suspend_asm.S | 8 +++++--- 2 files changed, 18 insertions(+), 7 deletions(-)
arch/x86_64/kernel/suspend.c +13 -4
@@ -9,6 +9,7 @@#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h>
@@ -150,6 +151,12 @@ extern int restore_image(void);*/ unsigned long restore_jump_address; +/* +* Value of the cr3 register from before the hibernation (this value is passed +* in the image header). +*/ +unsigned long restore_cr3; +pgd_t *temp_level4_pgt;static int res_phys_pud_init (pud_t *pud,unsigned long address,unsigned long end)
@@ -239,7 +246,8 @@ int pfn_is_nosave(unsigned long pfn)struct restore_data_record {unsigned long jump_address; -unsigned long control; +unsigned long cr3; +unsigned long magic; };#define RESTORE_MAGIC 0x0123456789ABCDEFUL
@@ -256,7 +264,8 @@ int arch_hibernation_header_save(void *aif (max_size <sizeof (struct restore_data_record ))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ;return 0 ; }
@@ -270,7 +279,7 @@ int arch_hibernation_header_restore(voidstruct restore_data_record *rdr = addr; restore_jump_address = rdr->jump_address; -return (rdr->control == (restore_jump_address ^RESTORE_MAGIC )) ? - 0 : -EINVAL; + restore_cr3 = rdr->cr3; + return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL; }#endif /* CONFIG_HIBERNATION */
arch/x86_64/kernel/suspend_asm.S +5 -3
@@ -39,6 +39,9 @@ ENTRY(swsusp_arch_suspend) /* save the address of restore_registers */ movq $restore_registers, %rax movq %rax, restore_jump_address(%rip) + /* save cr3 */ + movq %cr3, %rax + movq %rax, restore_cr3(%rip) call swsusp_save ret
@@ -66,6 +69,7 @@ ENTRY(restore_image) /* prepare to jump to the image kernel */ movq restore_jump_address(%rip), %rax + movq restore_cr3(%rip), %rbx /* copy image data to their original locations */ xorq %r10, %r10
@@ -116,9 +120,7 @@ ENTRY(restore_registers) movsq /* go back to the original page tables */ -1: movq $(init_level4_pgt - __START_KERNEL_map), %rax - addq phys_base(%rip), %rax - movq %rax, %cr3 +1: movq %rbx, %cr3 /* Flush TLB, including "global" things (vmalloc) */ movq mmu_cr4_features(%rip), %rax
Re: Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
Hi!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field.
Like in the patch below.
Yep, thanks.
Quoted 7 lines
---
From: Rafael J. Wysocki <[email protected]>Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.
ACK.
arch/x86_64/kernel/suspend.c line 15
Quoted 8 lines
arch/x86_64/kernel/suspend.c +1
-0@@ -9,6 +9,7 @@#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h>
This one is no longer neccessary.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [linux-pm] Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
In reply to Rafael J. WysockiHi!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field.
Like in the patch below.
Yep, thanks.
Quoted 7 lines
---
From: Rafael J. Wysocki <[email protected]>Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.
ACK.
arch/x86_64/kernel/suspend.c line 15
Quoted 8 lines
arch/x86_64/kernel/suspend.c +1
-0@@ -9,6 +9,7 @@#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h>
This one is no longer neccessary.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [linux-pm] Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
Quoted 30 lines · Pavel Machek
Hi!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field.
Like in the patch below.
Yep, thanks.
Quoted 7 lines
---
From: Rafael J. Wysocki <[email protected]>Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.ACK.
Quoted 2 lines
(quoted diff) +0
-0@@ -9,6 +9,7 @@
Quoted 8 lines
arch/x86_64/kernel/suspend.c line 15
Quoted 6 lines
arch/x86_64/kernel/suspend.c +1
-0#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h> This one is no longer neccessary.
I forgot about it.
I've already sent the 1/4 and 2/4 patches to Andrew, so I'll keep the remaining two ones in the queue for a couple of days.
Thanks for the ACKs!
Greetings,
Rafael
Re: Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
In reply to Pavel MachekQuoted 30 lines · Pavel Machek
Hi!
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field.
Like in the patch below.
Yep, thanks.
Quoted 7 lines
---
From: Rafael J. Wysocki <[email protected]>Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.ACK.
Quoted 2 lines
(quoted diff) +0
-0@@ -9,6 +9,7 @@
Quoted 8 lines
arch/x86_64/kernel/suspend.c line 15
Quoted 6 lines
arch/x86_64/kernel/suspend.c +1
-0#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h> This one is no longer neccessary.
I forgot about it.
I've already sent the 1/4 and 2/4 patches to Andrew, so I'll keep the remaining two ones in the queue for a couple of days.
Thanks for the ACKs!
Greetings,
Rafael
Re: Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
In reply to Rafael J. WysockiQuoted 33 lines · Rafael J. Wysocki
Quoted 32 lines · Pavel Machek
Hi!
Quoted 14 lines
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.Signed-off-by Rafael J. Wysocki <[email protected]>(quoted diff) +0
-1@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a { struct restore_data_record *rdr = addr; - if (max_size < sizeof(struct restore_data_record))Review int arch_hibernation_header_save(void *a · line 273
Quoted 10 lines
(quoted diff) +5
-1+ if (max_size < sizeof(*rdr)) return -EOVERFLOW; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^ RESTORE_MAGIC); + rdr->cr3 = restore_cr3; + rdr->magic = RESTORE_MAGIC; + rdr->crc = 0; + rdr->crc = crc32_le(0, addr, sizeof(*rdr)); return 0; }No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 6 lines
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field.
Like in the patch below.
---
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of restore_registers() in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid using init_level4_pgt page tables during the restore.
--- arch/x86_64/kernel/suspend.c | 17 +++++++++++++---- arch/x86_64/kernel/suspend_asm.S | 8 +++++--- 2 files changed, 18 insertions(+), 7 deletions(-)
arch/x86_64/kernel/suspend.c +13 -4
@@ -9,6 +9,7 @@#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h>
@@ -150,6 +151,12 @@ extern int restore_image(void);*/ unsigned long restore_jump_address; +/* +* Value of the cr3 register from before the hibernation (this value is passed +* in the image header). +*/ +unsigned long restore_cr3; +pgd_t *temp_level4_pgt;static int res_phys_pud_init (pud_t *pud,unsigned long address,unsigned long end)
@@ -239,7 +246,8 @@ int pfn_is_nosave(unsigned long pfn)struct restore_data_record {unsigned long jump_address; -unsigned long control; +unsigned long cr3; +unsigned long magic; };#define RESTORE_MAGIC 0x0123456789ABCDEFUL
@@ -256,7 +264,8 @@ int arch_hibernation_header_save(void *aif (max_size <sizeof (struct restore_data_record ))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ;return 0 ; }
@@ -270,7 +279,7 @@ int arch_hibernation_header_restore(voidstruct restore_data_record *rdr = addr; restore_jump_address = rdr->jump_address; -return (rdr->control == (restore_jump_address ^RESTORE_MAGIC )) ? - 0 : -EINVAL; + restore_cr3 = rdr->cr3; + return (rdr->magic == RESTORE_MAGIC) ? 0 : -EINVAL; }#endif /* CONFIG_HIBERNATION */
arch/x86_64/kernel/suspend_asm.S +5 -3
@@ -39,6 +39,9 @@ ENTRY(swsusp_arch_suspend) /* save the address of restore_registers */ movq $restore_registers, %rax movq %rax, restore_jump_address(%rip) + /* save cr3 */ + movq %cr3, %rax + movq %rax, restore_cr3(%rip) call swsusp_save ret
@@ -66,6 +69,7 @@ ENTRY(restore_image) /* prepare to jump to the image kernel */ movq restore_jump_address(%rip), %rax + movq restore_cr3(%rip), %rbx /* copy image data to their original locations */ xorq %r10, %r10
@@ -116,9 +120,7 @@ ENTRY(restore_registers) movsq /* go back to the original page tables */ -1: movq $(init_level4_pgt - __START_KERNEL_map), %rax - addq phys_base(%rip), %rax - movq %rax, %cr3 +1: movq %rbx, %cr3 /* Flush TLB, including "global" things (vmalloc) */ movq mmu_cr4_features(%rip), %rax
Re: [RFC][PATCH -mm 3/4] Hibernation: Pass CR3 in the image header on x86_64
In reply to Pavel MachekQuoted 32 lines · Pavel Machek
Hi!
Quoted 14 lines
From: Rafael J. Wysocki <[email protected]>
Since we already pass the address of
restore_registers()in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid usinginit_level4_pgtpage tables during the restore.Signed-off-by Rafael J. Wysocki <[email protected]>arch/x86_64/kernel/suspend.c +0
-1@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a {struct restore_data_record *rdr = addr; -if (max_size <sizeof (struct restore_data_record ))arch/x86_64/kernel/suspend.c int arch_hibernation_header_save(void *a · line 273
Quoted 10 lines
arch/x86_64/kernel/suspend.c +5
-1+if (max_size <sizeof (*rdr))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ; + rdr->crc =0 ; + rdr->crc =crc32_le (0 , addr,sizeof (*rdr));return 0 ; }No, I do not think I like that. I believe both -> control and -> crc is just useless paranoia. Bitflip in this area is not going to be any worse than bitflip anywhere else, we should not pretend this is somehow "more important".
Quoted 2 lines
-> control should really be "protocol version"... probably should contain some field that is easy to increment.
OK
Perhaps I'll just remove the crc field. What do you think?
Rafael
RFC PATCH 3/4 Hibernation: Pass CR3 in the image header on x86_64
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Since we already pass the address of restore_registers() in the image header, we can also pass the value of the CR3 register from before the hibernation in the same way. This will allow us to avoid using init_level4_pgt page tables during the restore.
--- arch/x86_64/kernel/suspend.c | 26 +++++++++++++++++++++----- arch/x86_64/kernel/suspend_asm.S | 8 +++++--- 2 files changed, 26 insertions(+), 8 deletions(-)
arch/x86_64/kernel/suspend.c +21 -5
@@ -9,6 +9,7 @@#include <linux/smp.h> #include <linux/suspend.h> +#include <linux/crc32.h> #include <asm/proto.h> #include <asm/page.h> #include <asm/pgtable.h>
@@ -150,6 +151,12 @@ extern int restore_image(void);*/ unsigned long restore_jump_address; +/* +* Value of the cr3 register from before the hibernation (this value is passed +* in the image header). +*/ +unsigned long restore_cr3; +pgd_t *temp_level4_pgt;static int res_phys_pud_init (pud_t *pud,unsigned long address,unsigned long end)
@@ -239,7 +246,9 @@ int pfn_is_nosave(unsigned long pfn)struct restore_data_record {unsigned long jump_address; -unsigned long control; +unsigned long cr3; +unsigned long magic; +u32 crc; };#define RESTORE_MAGIC 0x0123456789ABCDEFUL
@@ -253,10 +262,13 @@ int arch_hibernation_header_save(void *a {struct restore_data_record *rdr = addr; -if (max_size <sizeof (struct restore_data_record )) +if (max_size <sizeof (*rdr))return -EOVERFLOW ; rdr->jump_address = restore_jump_address; - rdr->control = (restore_jump_address ^RESTORE_MAGIC ); + rdr->cr3 = restore_cr3; + rdr->magic =RESTORE_MAGIC ; + rdr->crc =0 ; + rdr->crc =crc32_le (0 , addr,sizeof (*rdr));return 0 ; }
@@ -268,9 +280,13 @@ int arch_hibernation_header_save(void *aint arch_hibernation_header_restore (void *addr) {struct restore_data_record *rdr = addr; +u32 crc; restore_jump_address = rdr->jump_address; -return (rdr->control == (restore_jump_address ^RESTORE_MAGIC )) ? -0 : -EINVAL ; + restore_cr3 = rdr->cr3; + crc = rdr->crc; + rdr->crc =0 ; +return (crc ==crc32_le (0 , addr,sizeof (*rdr)) + && rdr->magic ==RESTORE_MAGIC ) ?0 : -EINVAL ; }#endif /* CONFIG_HIBERNATION */
arch/x86_64/kernel/suspend_asm.S +5 -3
@@ -39,6 +39,9 @@ ENTRY(swsusp_arch_suspend) /* save the address of restore_registers */ movq $restore_registers, %rax movq %rax, restore_jump_address(%rip) + /* save cr3 */ + movq %cr3, %rax + movq %rax, restore_cr3(%rip) call swsusp_save ret
@@ -66,6 +69,7 @@ ENTRY(restore_image) /* prepare to jump to the image kernel */ movq restore_jump_address(%rip), %rax + movq restore_cr3(%rip), %rbx /* copy image data to their original locations */ xorq %r10, %r10
@@ -116,9 +120,7 @@ ENTRY(restore_registers) movsq /* go back to the original page tables */ -1: movq $(init_level4_pgt - __START_KERNEL_map), %rax - addq phys_base(%rip), %rax - movq %rax, %cr3 +1: movq %rbx, %cr3 /* Flush TLB, including "global" things (vmalloc) */ movq mmu_cr4_features(%rip), %rax
RFC PATCH 4/4 Hibernation: Use temporary page tables for kernel text mapping on x86_64
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Use temporary page tables for the kernel text mapping during hibernation restore on x86_64.
Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
--- arch/x86_64/kernel/suspend.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-)
arch/x86_64/kernel/suspend.c +34 -7
@@ -182,7 +182,7 @@ static int res_phys_pud_init(pud_t *pud,if (paddr >= end)break ; - pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr; + pe = __PAGE_KERNEL_LARGE | paddr; pe &= __supported_pte_mask;set_pmd (pmd,__pmd (pe)); }
@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]); -/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ; next = start +PGDIR_SIZE ;
@@ -219,7 +236,17 @@ static int set_up_temporary_mappings(voiset_pgd (temp_level4_pgt +pgd_index (start),mk_kernel_pgd (__pa (pud))); } -return 0 ; + +/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ; + error =res_kernel_text_pud_init (pud, __START_KERNEL_map); +if (!error) +set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), +__pgd (__pa (pud) | _PAGE_TABLE)); + +return error; }int swsusp_arch_resume (void )
Re: [RFC][PATCH -mm 4/4] Hibernation: Use temporary page tables for kernel text mapping on x86_64
Hi!
Quoted 48 lines
Use temporary page tables for the kernel text mapping during hibernation restore on
x86_64.Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
arch/x86_64/kernel/suspend.c +21
-3@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]);
arch/x86_64/kernel/suspend.c static int res_phys_pud_init(pud_t *pud, · line 230
Quoted 10 lines
arch/x86_64/kernel/suspend.c +1
-2-/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ;
I believe we leak temp_level4_pgt here.
arch/x86_64/kernel/suspend.c static int set_up_temporary_mappings(voi · line 243
Quoted 4 lines
arch/x86_64/kernel/suspend.c +4
-0+/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ;
...and some more memory here :-(.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 4/4] Hibernation: Use temporary page tables for kernel text mapping on x86_64
In reply to Rafael J. WysockiHi!
Quoted 48 lines
Use temporary page tables for the kernel text mapping during hibernation restore on
x86_64.Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
arch/x86_64/kernel/suspend.c +21
-3@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]);
arch/x86_64/kernel/suspend.c static int res_phys_pud_init(pud_t *pud, · line 230
Quoted 10 lines
arch/x86_64/kernel/suspend.c +1
-2-/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ;
I believe we leak temp_level4_pgt here.
arch/x86_64/kernel/suspend.c static int set_up_temporary_mappings(voi · line 243
Quoted 4 lines
arch/x86_64/kernel/suspend.c +4
-0+/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ;
...and some more memory here :-(.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 4/4] Hibernation: Use temporary page tables for kernel text mapping on x86_64
Quoted 63 lines · Pavel Machek
Hi!
Quoted 48 lines
Use temporary page tables for the kernel text mapping during hibernation restore on
x86_64.Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
arch/x86_64/kernel/suspend.c +21
-3@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]);arch/x86_64/kernel/suspend.c static int res_phys_pud_init(pud_t *pud, · line 230
Quoted 10 lines
arch/x86_64/kernel/suspend.c +1
-2-/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ;I believe we leak
temp_level4_pgthere.
Quoted 6 lines
arch/x86_64/kernel/suspend.c static int set_up_temporary_mappings(voi · line 243
Quoted 4 lines
arch/x86_64/kernel/suspend.c +4
-0+/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ;...and some more memory here :-(.
No, these pages will be freed by swsusp_free(), thanks to the get_safe_page().
I'm not _that_ careless. ;-)
Greetings,
Rafael
Re: [RFC][PATCH -mm 4/4] Hibernation: Use temporary page tables for kernel text mapping on x86_64
Quoted 71 lines · Rafael J. Wysocki
Quoted 63 lines · Pavel Machek
Hi!
Quoted 48 lines
Use temporary page tables for the kernel text mapping during hibernation restore on
x86_64.Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
arch/x86_64/kernel/suspend.c +21
-3@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]);arch/x86_64/kernel/suspend.c static int res_phys_pud_init(pud_t *pud, · line 230
Quoted 10 lines
arch/x86_64/kernel/suspend.c +1
-2-/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ;I believe we leak
temp_level4_pgthere.Quoted 6 lines
arch/x86_64/kernel/suspend.c static int set_up_temporary_mappings(voi · line 243
Quoted 4 lines
arch/x86_64/kernel/suspend.c +4
-0+/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ;...and some more memory here :-(.
Quoted 3 lines
No, these pages will be freed by
swsusp_free(), thanks to theget_safe_page().I'm not _that_ careless. ;-)
Ok, ACK.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 4/4] Hibernation: Use temporary page tables for kernel text mapping on x86_64
In reply to Rafael J. WysockiQuoted 71 lines · Rafael J. Wysocki
Quoted 63 lines · Pavel Machek
Hi!
Quoted 48 lines
Use temporary page tables for the kernel text mapping during hibernation restore on
x86_64.Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
arch/x86_64/kernel/suspend.c +21
-3@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]);arch/x86_64/kernel/suspend.c static int res_phys_pud_init(pud_t *pud, · line 230
Quoted 10 lines
arch/x86_64/kernel/suspend.c +1
-2-/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ;I believe we leak
temp_level4_pgthere.Quoted 6 lines
arch/x86_64/kernel/suspend.c static int set_up_temporary_mappings(voi · line 243
Quoted 4 lines
arch/x86_64/kernel/suspend.c +4
-0+/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ;...and some more memory here :-(.
Quoted 3 lines
No, these pages will be freed by
swsusp_free(), thanks to theget_safe_page().I'm not _that_ careless. ;-)
Ok, ACK.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 4/4] Hibernation: Use temporary page tables for kernel text mapping on x86_64
In reply to Pavel MachekQuoted 63 lines · Pavel Machek
Hi!
Quoted 48 lines
Use temporary page tables for the kernel text mapping during hibernation restore on
x86_64.Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
arch/x86_64/kernel/suspend.c +21
-3@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]);arch/x86_64/kernel/suspend.c static int res_phys_pud_init(pud_t *pud, · line 230
Quoted 10 lines
arch/x86_64/kernel/suspend.c +1
-2-/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ;I believe we leak
temp_level4_pgthere.
Quoted 6 lines
arch/x86_64/kernel/suspend.c static int set_up_temporary_mappings(voi · line 243
Quoted 4 lines
arch/x86_64/kernel/suspend.c +4
-0+/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ;...and some more memory here :-(.
No, these pages will be freed by swsusp_free(), thanks to the get_safe_page().
I'm not _that_ careless. ;-)
Greetings,
Rafael
RFC PATCH 4/4 Hibernation: Use temporary page tables for kernel text mapping on x86_64
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Use temporary page tables for the kernel text mapping during hibernation restore on x86_64.
Without the patch, the original boot kernel's page tables that represent the kernel text mapping are used while the core of the image kernel is being restored. However, in principle, if the boot kernel is not identical to the image kernel, the location of these page tables in the image kernel need not be the same, so we should create a safe copy of the kernel text mapping prior to restoring the core of the image kernel.
--- arch/x86_64/kernel/suspend.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-)
arch/x86_64/kernel/suspend.c +34 -7
@@ -182,7 +182,7 @@ static int res_phys_pud_init(pud_t *pud,if (paddr >= end)break ; - pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr; + pe = __PAGE_KERNEL_LARGE | paddr; pe &= __supported_pte_mask;set_pmd (pmd,__pmd (pe)); }
@@ -190,25 +190,42 @@ static int res_phys_pud_init(pud_t *pud,return 0 ; } +static int res_kernel_text_pud_init (pud_t *pud,unsigned long start) +{ +pmd_t *pmd; +unsigned long paddr; + + pmd = (pmd_t *)get_safe_page (GFP_ATOMIC ); +if (!pmd) +return -ENOMEM ; +set_pud (pud +pud_index (start),__pud (__pa (pmd) | _KERNPG_TABLE)); +for (paddr =0 ; paddr <KERNEL_TEXT_SIZE ; pmd++, paddr +=PMD_SIZE ) { +unsigned long pe; + + pe = __PAGE_KERNEL_LARGE_EXEC | _PAGE_GLOBAL | paddr; + pe &= __supported_pte_mask; +set_pmd (pmd,__pmd (pe)); + } + +return 0 ; +} +static int set_up_temporary_mappings (void ) {unsigned long start, end, next; +pud_t *pud;int error; temp_level4_pgt = (pgd_t *)get_safe_page (GFP_ATOMIC );if (!temp_level4_pgt)return -ENOMEM ; -/* It is safe to reuse the original kernel mapping */ -set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), - init_level4_pgt[pgd_index (__START_KERNEL_map)]); -/* Set up the direct mapping from scratch */ start = (unsigned long )pfn_to_kaddr (0 ); end = (unsigned long )pfn_to_kaddr (end_pfn);for (; start < end; start = next) { -pud_t *pud = (pud_t *)get_safe_page (GFP_ATOMIC ); + pud = (pud_t *)get_safe_page (GFP_ATOMIC );if (!pud)return -ENOMEM ; next = start +PGDIR_SIZE ;
@@ -219,7 +236,17 @@ static int set_up_temporary_mappings(voiset_pgd (temp_level4_pgt +pgd_index (start),mk_kernel_pgd (__pa (pud))); } -return 0 ; + +/* Set up the kernel text mapping from scratch */ + pud = (pud_t *)get_safe_page (GFP_ATOMIC ); +if (!pud) +return -ENOMEM ; + error =res_kernel_text_pud_init (pud, __START_KERNEL_map); +if (!error) +set_pgd (temp_level4_pgt +pgd_index (__START_KERNEL_map), +__pgd (__pa (pud) | _PAGE_TABLE)); + +return error; }int swsusp_arch_resume (void )