[PATCH v5 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API

Ashish Kalra posted 3 patches 2 weeks, 2 days ago
There is a newer version of this series
[PATCH v5 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API
Posted by Ashish Kalra 2 weeks, 2 days ago
From: Ashish Kalra <ashish.kalra@amd.com>

When leaking certain page types, such as Hypervisor Fixed (HV_FIXED)
pages, it does not make sense to dump RMP contents for the 2MB range of
the page(s) being leaked. In the case of HV_FIXED pages, this is not an
error situation where the surrounding 2MB page RMP entries can provide
debug information.

Add new __snp_leak_pages() API with dump_rmp bool parameter to support
continue adding pages to the snp_leaked_pages_list but not issue
dump_rmpentry().

Make snp_leak_pages() a wrapper for the common case which also allows
existing users to continue to dump RMP entries.

Suggested-by: Thomas Lendacky <Thomas.Lendacky@amd.com>
Suggested-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Acked-by: Borislav Petkov (AMD) <bp@alien8.de>
Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
---
 arch/x86/include/asm/sev.h | 8 +++++++-
 arch/x86/virt/svm/sev.c    | 7 ++++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index 00475b814ac4..7a1ae990b15f 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -635,10 +635,15 @@ void snp_dump_hva_rmpentry(unsigned long address);
 int psmash(u64 pfn);
 int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable);
 int rmp_make_shared(u64 pfn, enum pg_level level);
-void snp_leak_pages(u64 pfn, unsigned int npages);
+void __snp_leak_pages(u64 pfn, unsigned int npages, bool dump_rmp);
 void kdump_sev_callback(void);
 void snp_fixup_e820_tables(void);
 
+static inline void snp_leak_pages(u64 pfn, unsigned int pages)
+{
+	__snp_leak_pages(pfn, pages, true);
+}
+
 static inline void sev_evict_cache(void *va, int npages)
 {
 	volatile u8 val __always_unused;
@@ -668,6 +673,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 as
 	return -ENODEV;
 }
 static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
+static inline void __snp_leak_pages(u64 pfn, unsigned int npages, bool dump_rmp) {}
 static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
 static inline void kdump_sev_callback(void) { }
 static inline void snp_fixup_e820_tables(void) {}
diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
index 942372e69b4d..ee643a6cd691 100644
--- a/arch/x86/virt/svm/sev.c
+++ b/arch/x86/virt/svm/sev.c
@@ -1029,7 +1029,7 @@ int rmp_make_shared(u64 pfn, enum pg_level level)
 }
 EXPORT_SYMBOL_GPL(rmp_make_shared);
 
-void snp_leak_pages(u64 pfn, unsigned int npages)
+void __snp_leak_pages(u64 pfn, unsigned int npages, bool dump_rmp)
 {
 	struct page *page = pfn_to_page(pfn);
 
@@ -1052,14 +1052,15 @@ void snp_leak_pages(u64 pfn, unsigned int npages)
 		    (PageHead(page) && compound_nr(page) <= npages))
 			list_add_tail(&page->buddy_list, &snp_leaked_pages_list);
 
-		dump_rmpentry(pfn);
+		if (dump_rmp)
+			dump_rmpentry(pfn);
 		snp_nr_leaked_pages++;
 		pfn++;
 		page++;
 	}
 	spin_unlock(&snp_leaked_pages_list_lock);
 }
-EXPORT_SYMBOL_GPL(snp_leak_pages);
+EXPORT_SYMBOL_GPL(__snp_leak_pages);
 
 void kdump_sev_callback(void)
 {
-- 
2.34.1
Re: [PATCH v5 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API
Posted by Borislav Petkov 2 weeks, 2 days ago
On Mon, Sep 15, 2025 at 09:21:58PM +0000, Ashish Kalra wrote:
> @@ -668,6 +673,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 as
>  	return -ENODEV;
>  }
>  static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
> +static inline void __snp_leak_pages(u64 pfn, unsigned int npages, bool dump_rmp) {}
>  static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}

I basically don't even have to build your patch to see that this can't build.
See below.

When your patch touches code behind different CONFIG_ items, you must make
sure it builds with both settings of each CONFIG_ item.

In file included from arch/x86/boot/startup/gdt_idt.c:9:
./arch/x86/include/asm/sev.h:679:20: error: redefinition of ‘snp_leak_pages’
  679 | static inline void snp_leak_pages(u64 pfn, unsigned int pages)
      |                    ^~~~~~~~~~~~~~
./arch/x86/include/asm/sev.h:673:20: note: previous definition of ‘snp_leak_pages’ with type ‘void(u64,  unsigned int)’ {aka ‘void(long long unsigned int,  unsigned int)’}
  673 | static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
      |                    ^~~~~~~~~~~~~~
make[4]: *** [scripts/Makefile.build:287: arch/x86/boot/startup/gdt_idt.o] Error 1
make[3]: *** [scripts/Makefile.build:556: arch/x86/boot/startup] Error 2
make[2]: *** [scripts/Makefile.build:556: arch/x86] Error 2
make[2]: *** Waiting for unfinished jobs....
In file included from drivers/iommu/amd/init.c:32:
./arch/x86/include/asm/sev.h:679:20: error: redefinition of ‘snp_leak_pages’
  679 | static inline void snp_leak_pages(u64 pfn, unsigned int pages)
      |                    ^~~~~~~~~~~~~~
./arch/x86/include/asm/sev.h:673:20: note: previous definition of ‘snp_leak_pages’ with type ‘void(u64,  unsigned int)’ {aka ‘void(long long unsigned int,  unsigned int)’}
  673 | static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
      |                    ^~~~~~~~~~~~~~
make[5]: *** [scripts/Makefile.build:287: drivers/iommu/amd/init.o] Error 1
make[4]: *** [scripts/Makefile.build:556: drivers/iommu/amd] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:556: drivers/iommu] Error 2
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [scripts/Makefile.build:556: drivers] Error 2
make[1]: *** [/mnt/kernel/kernel/linux/Makefile:2011: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v5 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API
Posted by Kalra, Ashish 2 weeks, 1 day ago
On 9/16/2025 8:12 AM, Borislav Petkov wrote:
> On Mon, Sep 15, 2025 at 09:21:58PM +0000, Ashish Kalra wrote:
>> @@ -668,6 +673,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 as
>>  	return -ENODEV;
>>  }
>>  static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
>> +static inline void __snp_leak_pages(u64 pfn, unsigned int npages, bool dump_rmp) {}
>>  static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
> 
> I basically don't even have to build your patch to see that this can't build.
> See below.
> 
> When your patch touches code behind different CONFIG_ items, you must make
> sure it builds with both settings of each CONFIG_ item.
>

I have done the builds with both CONFIG_KVM_AMD_SEV and !CONFIG_KVM_AMD_SEV and the builds are done
without any errors.

And i don't understand how can snp_leak_pages() have this redefinition, as one of them is under
#ifdef CONFIG_KVM_AMD_SEV and the other one is the #else part of it.

Thanks,
Ashish
 
> In file included from arch/x86/boot/startup/gdt_idt.c:9:
> ./arch/x86/include/asm/sev.h:679:20: error: redefinition of ‘snp_leak_pages’
>   679 | static inline void snp_leak_pages(u64 pfn, unsigned int pages)
>       |                    ^~~~~~~~~~~~~~
> ./arch/x86/include/asm/sev.h:673:20: note: previous definition of ‘snp_leak_pages’ with type ‘void(u64,  unsigned int)’ {aka ‘void(long long unsigned int,  unsigned int)’}
>   673 | static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
>       |                    ^~~~~~~~~~~~~~
> make[4]: *** [scripts/Makefile.build:287: arch/x86/boot/startup/gdt_idt.o] Error 1
> make[3]: *** [scripts/Makefile.build:556: arch/x86/boot/startup] Error 2
> make[2]: *** [scripts/Makefile.build:556: arch/x86] Error 2
> make[2]: *** Waiting for unfinished jobs....
> In file included from drivers/iommu/amd/init.c:32:
> ./arch/x86/include/asm/sev.h:679:20: error: redefinition of ‘snp_leak_pages’
>   679 | static inline void snp_leak_pages(u64 pfn, unsigned int pages)
>       |                    ^~~~~~~~~~~~~~
> ./arch/x86/include/asm/sev.h:673:20: note: previous definition of ‘snp_leak_pages’ with type ‘void(u64,  unsigned int)’ {aka ‘void(long long unsigned int,  unsigned int)’}
>   673 | static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
>       |                    ^~~~~~~~~~~~~~
> make[5]: *** [scripts/Makefile.build:287: drivers/iommu/amd/init.o] Error 1
> make[4]: *** [scripts/Makefile.build:556: drivers/iommu/amd] Error 2
> make[4]: *** Waiting for unfinished jobs....
> make[3]: *** [scripts/Makefile.build:556: drivers/iommu] Error 2
> make[3]: *** Waiting for unfinished jobs....
> make[2]: *** [scripts/Makefile.build:556: drivers] Error 2
> make[1]: *** [/mnt/kernel/kernel/linux/Makefile:2011: .] Error 2
> make: *** [Makefile:248: __sub-make] Error 2
> 
Re: [PATCH v5 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API
Posted by Tom Lendacky 2 weeks, 2 days ago
On 9/16/25 08:12, Borislav Petkov wrote:
> On Mon, Sep 15, 2025 at 09:21:58PM +0000, Ashish Kalra wrote:
>> @@ -668,6 +673,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 as
>>  	return -ENODEV;
>>  }
>>  static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
>> +static inline void __snp_leak_pages(u64 pfn, unsigned int npages, bool dump_rmp) {}
>>  static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
> 
> I basically don't even have to build your patch to see that this can't build.
> See below.

Did the patch merge correctly? I can't see how it would fail since both
the original and new definitions are in separate parts of the #ifdef... It
should have failed even before given the way it was changed.

Maybe I'm missing something.

Thanks,
Tom

> 
> When your patch touches code behind different CONFIG_ items, you must make
> sure it builds with both settings of each CONFIG_ item.
> 
> In file included from arch/x86/boot/startup/gdt_idt.c:9:
> ./arch/x86/include/asm/sev.h:679:20: error: redefinition of ‘snp_leak_pages’
>   679 | static inline void snp_leak_pages(u64 pfn, unsigned int pages)
>       |                    ^~~~~~~~~~~~~~
> ./arch/x86/include/asm/sev.h:673:20: note: previous definition of ‘snp_leak_pages’ with type ‘void(u64,  unsigned int)’ {aka ‘void(long long unsigned int,  unsigned int)’}
>   673 | static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
>       |                    ^~~~~~~~~~~~~~
> make[4]: *** [scripts/Makefile.build:287: arch/x86/boot/startup/gdt_idt.o] Error 1
> make[3]: *** [scripts/Makefile.build:556: arch/x86/boot/startup] Error 2
> make[2]: *** [scripts/Makefile.build:556: arch/x86] Error 2
> make[2]: *** Waiting for unfinished jobs....
> In file included from drivers/iommu/amd/init.c:32:
> ./arch/x86/include/asm/sev.h:679:20: error: redefinition of ‘snp_leak_pages’
>   679 | static inline void snp_leak_pages(u64 pfn, unsigned int pages)
>       |                    ^~~~~~~~~~~~~~
> ./arch/x86/include/asm/sev.h:673:20: note: previous definition of ‘snp_leak_pages’ with type ‘void(u64,  unsigned int)’ {aka ‘void(long long unsigned int,  unsigned int)’}
>   673 | static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
>       |                    ^~~~~~~~~~~~~~
> make[5]: *** [scripts/Makefile.build:287: drivers/iommu/amd/init.o] Error 1
> make[4]: *** [scripts/Makefile.build:556: drivers/iommu/amd] Error 2
> make[4]: *** Waiting for unfinished jobs....
> make[3]: *** [scripts/Makefile.build:556: drivers/iommu] Error 2
> make[3]: *** Waiting for unfinished jobs....
> make[2]: *** [scripts/Makefile.build:556: drivers] Error 2
> make[1]: *** [/mnt/kernel/kernel/linux/Makefile:2011: .] Error 2
> make: *** [Makefile:248: __sub-make] Error 2
> 

Re: [PATCH v5 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API
Posted by Borislav Petkov 2 weeks, 1 day ago
On Tue, Sep 16, 2025 at 08:58:42AM -0500, Tom Lendacky wrote:
> Did the patch merge correctly? I can't see how it would fail since both
> the original and new definitions are in separate parts of the #ifdef... It
> should have failed even before given the way it was changed.
> 
> Maybe I'm missing something.

Nah, you're right. Because your patch is moving sev_evict_cache(), I ended up
resolving it wrong. Sorry for the noise.

Ashish, all patches against tip go ontop of the tip/master branch. Please redo
your set ontop.

Thx.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette