Skip to content

Commit b0b592c

Browse files
matttbesuryasaimadhu
authored andcommitted
x86/pm: Fix false positive kmemleak report in msr_build_context()
Since e2a1256 ("x86/speculation: Restore speculation related MSRs during S3 resume") kmemleak reports this issue: unreferenced object 0xffff888009cedc00 (size 256): comm "swapper/0", pid 1, jiffies 4294693823 (age 73.764s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 48 00 00 00 00 00 00 00 ........H....... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace: msr_build_context (include/linux/slab.h:621) pm_check_save_msr (arch/x86/power/cpu.c:520) do_one_initcall (init/main.c:1298) kernel_init_freeable (init/main.c:1370) kernel_init (init/main.c:1504) ret_from_fork (arch/x86/entry/entry_64.S:304) Reproducer: - boot the VM with a debug kernel config (see multipath-tcp/mptcp_net-next#268) - wait ~1 minute - start a kmemleak scan The root cause here is alignment within the packed struct saved_context (from suspend_64.h). Kmemleak only searches for pointers that are aligned (see how pointers are scanned in kmemleak.c), but pahole shows that the saved_msrs struct member and all members after it in the structure are unaligned: struct saved_context { struct pt_regs regs; /* 0 168 */ /* --- cacheline 2 boundary (128 bytes) was 40 bytes ago --- */ u16 ds; /* 168 2 */ ... u64 misc_enable; /* 232 8 */ bool misc_enable_saved; /* 240 1 */ /* Note below odd offset values for the remainder of this struct */ struct saved_msrs saved_msrs; /* 241 16 */ /* --- cacheline 4 boundary (256 bytes) was 1 bytes ago --- */ long unsigned int efer; /* 257 8 */ u16 gdt_pad; /* 265 2 */ struct desc_ptr gdt_desc; /* 267 10 */ u16 idt_pad; /* 277 2 */ struct desc_ptr idt; /* 279 10 */ u16 ldt; /* 289 2 */ u16 tss; /* 291 2 */ long unsigned int tr; /* 293 8 */ long unsigned int safety; /* 301 8 */ long unsigned int return_address; /* 309 8 */ /* size: 317, cachelines: 5, members: 25 */ /* last cacheline: 61 bytes */ } __attribute__((__packed__)); Move misc_enable_saved to the end of the struct declaration so that saved_msrs fits in before the cacheline 4 boundary. The comment above the saved_context declaration says to fix wakeup_64.S file and __save/__restore_processor_state() if the struct is modified: it looks like all the accesses in wakeup_64.S are done through offsets which are computed at build-time. Update that comment accordingly. At the end, the false positive kmemleak report is due to a limitation from kmemleak but it is always good to avoid unaligned members for optimisation purposes. Please note that it looks like this issue is not new, e.g. https://lore.kernel.org/all/[email protected]/ https://lore.kernel.org/all/[email protected]/ [ bp: Massage + cleanup commit message. ] Fixes: 7a9c2dd ("x86/pm: Introduce quirk framework to save/restore extra MSR registers around suspend/resume") Suggested-by: Mat Martineau <[email protected]> Signed-off-by: Matthieu Baerts <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Rafael J. Wysocki <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0205f8a commit b0b592c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

arch/x86/include/asm/suspend_32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ struct saved_context {
1919
u16 gs;
2020
unsigned long cr0, cr2, cr3, cr4;
2121
u64 misc_enable;
22-
bool misc_enable_saved;
2322
struct saved_msrs saved_msrs;
2423
struct desc_ptr gdt_desc;
2524
struct desc_ptr idt;
@@ -28,6 +27,7 @@ struct saved_context {
2827
unsigned long tr;
2928
unsigned long safety;
3029
unsigned long return_address;
30+
bool misc_enable_saved;
3131
} __attribute__((packed));
3232

3333
/* routines for saving/restoring kernel state */

arch/x86/include/asm/suspend_64.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* Image of the saved processor state, used by the low level ACPI suspend to
1515
* RAM code and by the low level hibernation code.
1616
*
17-
* If you modify it, fix arch/x86/kernel/acpi/wakeup_64.S and make sure that
18-
* __save/__restore_processor_state(), defined in arch/x86/kernel/suspend_64.c,
19-
* still work as required.
17+
* If you modify it, check how it is used in arch/x86/kernel/acpi/wakeup_64.S
18+
* and make sure that __save/__restore_processor_state(), defined in
19+
* arch/x86/power/cpu.c, still work as required.
20+
*
21+
* Because the structure is packed, make sure to avoid unaligned members. For
22+
* optimisation purposes but also because tools like kmemleak only search for
23+
* pointers that are aligned.
2024
*/
2125
struct saved_context {
2226
struct pt_regs regs;
@@ -36,7 +40,6 @@ struct saved_context {
3640

3741
unsigned long cr0, cr2, cr3, cr4;
3842
u64 misc_enable;
39-
bool misc_enable_saved;
4043
struct saved_msrs saved_msrs;
4144
unsigned long efer;
4245
u16 gdt_pad; /* Unused */
@@ -48,6 +51,7 @@ struct saved_context {
4851
unsigned long tr;
4952
unsigned long safety;
5053
unsigned long return_address;
54+
bool misc_enable_saved;
5155
} __attribute__((packed));
5256

5357
#define loaddebug(thread,register) \

0 commit comments

Comments
 (0)