Skip to content

Commit 6dc2a77

Browse files
sunilmutliuw
authored andcommitted
x86/Hyper-V: Support for free page reporting
Linux has support for free page reporting now (36e66c5) for virtualized environment. On Hyper-V when virtually backed VMs are configured, Hyper-V will advertise cold memory discard capability, when supported. This patch adds the support to hook into the free page reporting infrastructure and leverage the Hyper-V cold memory discard hint hypercall to report/free these pages back to the host. Signed-off-by: Sunil Muthuswamy <sunilmut@microsoft.com> Tested-by: Matheus Castello <matheus@castello.eng.br> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Tested-by: Nathan Chancellor <nathan@kernel.org> Link: https://lore.kernel.org/r/SN4PR2101MB0880121FA4E2FEC67F35C1DCC0649@SN4PR2101MB0880.namprd21.prod.outlook.com Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent 1b60280 commit 6dc2a77

6 files changed

Lines changed: 180 additions & 8 deletions

File tree

‎arch/x86/hyperv/hv_init.c‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ void __init hyperv_init(void)
498498
x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
499499
#endif
500500

501+
/* Query the VMs extended capability once, so that it can be cached. */
502+
hv_query_ext_cap(0);
501503
return;
502504

503505
remove_cpuhp_state:
@@ -601,7 +603,7 @@ EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
601603

602604
enum hv_isolation_type hv_get_isolation_type(void)
603605
{
604-
if (!(ms_hyperv.features_b & HV_ISOLATION))
606+
if (!(ms_hyperv.priv_high & HV_ISOLATION))
605607
return HV_ISOLATION_TYPE_NONE;
606608
return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
607609
}
@@ -612,3 +614,50 @@ bool hv_is_isolation_supported(void)
612614
return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
613615
}
614616
EXPORT_SYMBOL_GPL(hv_is_isolation_supported);
617+
618+
/* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */
619+
bool hv_query_ext_cap(u64 cap_query)
620+
{
621+
/*
622+
* The address of the 'hv_extended_cap' variable will be used as an
623+
* output parameter to the hypercall below and so it should be
624+
* compatible with 'virt_to_phys'. Which means, it's address should be
625+
* directly mapped. Use 'static' to keep it compatible; stack variables
626+
* can be virtually mapped, making them imcompatible with
627+
* 'virt_to_phys'.
628+
* Hypercall input/output addresses should also be 8-byte aligned.
629+
*/
630+
static u64 hv_extended_cap __aligned(8);
631+
static bool hv_extended_cap_queried;
632+
u64 status;
633+
634+
/*
635+
* Querying extended capabilities is an extended hypercall. Check if the
636+
* partition supports extended hypercall, first.
637+
*/
638+
if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS))
639+
return false;
640+
641+
/* Extended capabilities do not change at runtime. */
642+
if (hv_extended_cap_queried)
643+
return hv_extended_cap & cap_query;
644+
645+
status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL,
646+
&hv_extended_cap);
647+
648+
/*
649+
* The query extended capabilities hypercall should not fail under
650+
* any normal circumstances. Avoid repeatedly making the hypercall, on
651+
* error.
652+
*/
653+
hv_extended_cap_queried = true;
654+
status &= HV_HYPERCALL_RESULT_MASK;
655+
if (status != HV_STATUS_SUCCESS) {
656+
pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n",
657+
status);
658+
return false;
659+
}
660+
661+
return hv_extended_cap & cap_query;
662+
}
663+
EXPORT_SYMBOL_GPL(hv_query_ext_cap);

‎arch/x86/kernel/cpu/mshyperv.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ static void __init ms_hyperv_init_platform(void)
265265
* Extract the features and hints
266266
*/
267267
ms_hyperv.features = cpuid_eax(HYPERV_CPUID_FEATURES);
268-
ms_hyperv.features_b = cpuid_ebx(HYPERV_CPUID_FEATURES);
268+
ms_hyperv.priv_high = cpuid_ebx(HYPERV_CPUID_FEATURES);
269269
ms_hyperv.misc_features = cpuid_edx(HYPERV_CPUID_FEATURES);
270270
ms_hyperv.hints = cpuid_eax(HYPERV_CPUID_ENLIGHTMENT_INFO);
271271

272-
pr_info("Hyper-V: features 0x%x, hints 0x%x, misc 0x%x\n",
273-
ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features);
272+
pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
273+
ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
274+
ms_hyperv.misc_features);
274275

275276
ms_hyperv.max_vp_index = cpuid_eax(HYPERV_CPUID_IMPLEMENT_LIMITS);
276277
ms_hyperv.max_lp_index = cpuid_ebx(HYPERV_CPUID_IMPLEMENT_LIMITS);
@@ -316,7 +317,7 @@ static void __init ms_hyperv_init_platform(void)
316317
x86_platform.calibrate_cpu = hv_get_tsc_khz;
317318
}
318319

319-
if (ms_hyperv.features_b & HV_ISOLATION) {
320+
if (ms_hyperv.priv_high & HV_ISOLATION) {
320321
ms_hyperv.isolation_config_a = cpuid_eax(HYPERV_CPUID_ISOLATION_CONFIG);
321322
ms_hyperv.isolation_config_b = cpuid_ebx(HYPERV_CPUID_ISOLATION_CONFIG);
322323

‎drivers/hv/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ config HYPERV_UTILS
2323
config HYPERV_BALLOON
2424
tristate "Microsoft Hyper-V Balloon driver"
2525
depends on HYPERV
26+
select PAGE_REPORTING
2627
help
2728
Select this option to enable Hyper-V Balloon driver.
2829

‎drivers/hv/hv_balloon.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/memory.h>
2222
#include <linux/notifier.h>
2323
#include <linux/percpu_counter.h>
24+
#include <linux/page_reporting.h>
2425

2526
#include <linux/hyperv.h>
2627
#include <asm/hyperv-tlfs.h>
@@ -563,6 +564,8 @@ struct hv_dynmem_device {
563564
* The negotiated version agreed by host.
564565
*/
565566
__u32 version;
567+
568+
struct page_reporting_dev_info pr_dev_info;
566569
};
567570

568571
static struct hv_dynmem_device dm_device;
@@ -1568,6 +1571,89 @@ static void balloon_onchannelcallback(void *context)
15681571

15691572
}
15701573

1574+
/* Hyper-V only supports reporting 2MB pages or higher */
1575+
#define HV_MIN_PAGE_REPORTING_ORDER 9
1576+
#define HV_MIN_PAGE_REPORTING_LEN (HV_HYP_PAGE_SIZE << HV_MIN_PAGE_REPORTING_ORDER)
1577+
static int hv_free_page_report(struct page_reporting_dev_info *pr_dev_info,
1578+
struct scatterlist *sgl, unsigned int nents)
1579+
{
1580+
unsigned long flags;
1581+
struct hv_memory_hint *hint;
1582+
int i;
1583+
u64 status;
1584+
struct scatterlist *sg;
1585+
1586+
WARN_ON_ONCE(nents > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
1587+
WARN_ON_ONCE(sgl->length < HV_MIN_PAGE_REPORTING_LEN);
1588+
local_irq_save(flags);
1589+
hint = *(struct hv_memory_hint **)this_cpu_ptr(hyperv_pcpu_input_arg);
1590+
if (!hint) {
1591+
local_irq_restore(flags);
1592+
return -ENOSPC;
1593+
}
1594+
1595+
hint->type = HV_EXT_MEMORY_HEAT_HINT_TYPE_COLD_DISCARD;
1596+
hint->reserved = 0;
1597+
for_each_sg(sgl, sg, nents, i) {
1598+
union hv_gpa_page_range *range;
1599+
1600+
range = &hint->ranges[i];
1601+
range->address_space = 0;
1602+
/* page reporting only reports 2MB pages or higher */
1603+
range->page.largepage = 1;
1604+
range->page.additional_pages =
1605+
(sg->length / HV_MIN_PAGE_REPORTING_LEN) - 1;
1606+
range->page_size = HV_GPA_PAGE_RANGE_PAGE_SIZE_2MB;
1607+
range->base_large_pfn =
1608+
page_to_hvpfn(sg_page(sg)) >> HV_MIN_PAGE_REPORTING_ORDER;
1609+
}
1610+
1611+
status = hv_do_rep_hypercall(HV_EXT_CALL_MEMORY_HEAT_HINT, nents, 0,
1612+
hint, NULL);
1613+
local_irq_restore(flags);
1614+
if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) {
1615+
pr_err("Cold memory discard hypercall failed with status %llx\n",
1616+
status);
1617+
return -EINVAL;
1618+
}
1619+
1620+
return 0;
1621+
}
1622+
1623+
static void enable_page_reporting(void)
1624+
{
1625+
int ret;
1626+
1627+
/* Essentially, validating 'PAGE_REPORTING_MIN_ORDER' is big enough. */
1628+
if (pageblock_order < HV_MIN_PAGE_REPORTING_ORDER) {
1629+
pr_debug("Cold memory discard is only supported on 2MB pages and above\n");
1630+
return;
1631+
}
1632+
1633+
if (!hv_query_ext_cap(HV_EXT_CAPABILITY_MEMORY_COLD_DISCARD_HINT)) {
1634+
pr_debug("Cold memory discard hint not supported by Hyper-V\n");
1635+
return;
1636+
}
1637+
1638+
BUILD_BUG_ON(PAGE_REPORTING_CAPACITY > HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES);
1639+
dm_device.pr_dev_info.report = hv_free_page_report;
1640+
ret = page_reporting_register(&dm_device.pr_dev_info);
1641+
if (ret < 0) {
1642+
dm_device.pr_dev_info.report = NULL;
1643+
pr_err("Failed to enable cold memory discard: %d\n", ret);
1644+
} else {
1645+
pr_info("Cold memory discard hint enabled\n");
1646+
}
1647+
}
1648+
1649+
static void disable_page_reporting(void)
1650+
{
1651+
if (dm_device.pr_dev_info.report) {
1652+
page_reporting_unregister(&dm_device.pr_dev_info);
1653+
dm_device.pr_dev_info.report = NULL;
1654+
}
1655+
}
1656+
15711657
static int balloon_connect_vsp(struct hv_device *dev)
15721658
{
15731659
struct dm_version_request version_req;
@@ -1713,6 +1799,7 @@ static int balloon_probe(struct hv_device *dev,
17131799
if (ret != 0)
17141800
return ret;
17151801

1802+
enable_page_reporting();
17161803
dm_device.state = DM_INITIALIZED;
17171804

17181805
dm_device.thread =
@@ -1727,6 +1814,7 @@ static int balloon_probe(struct hv_device *dev,
17271814
probe_error:
17281815
dm_device.state = DM_INIT_ERROR;
17291816
dm_device.thread = NULL;
1817+
disable_page_reporting();
17301818
vmbus_close(dev->channel);
17311819
#ifdef CONFIG_MEMORY_HOTPLUG
17321820
unregister_memory_notifier(&hv_memory_nb);
@@ -1749,6 +1837,7 @@ static int balloon_remove(struct hv_device *dev)
17491837
cancel_work_sync(&dm->ha_wrk.wrk);
17501838

17511839
kthread_stop(dm->thread);
1840+
disable_page_reporting();
17521841
vmbus_close(dev->channel);
17531842
#ifdef CONFIG_MEMORY_HOTPLUG
17541843
unregister_memory_notifier(&hv_memory_nb);

‎include/asm-generic/hyperv-tlfs.h‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@
8989
#define HV_ACCESS_STATS BIT(8)
9090
#define HV_DEBUGGING BIT(11)
9191
#define HV_CPU_MANAGEMENT BIT(12)
92+
#define HV_ENABLE_EXTENDED_HYPERCALLS BIT(20)
9293
#define HV_ISOLATION BIT(22)
9394

94-
9595
/*
9696
* TSC page layout.
9797
*/
@@ -159,11 +159,18 @@ struct ms_hyperv_tsc_page {
159159
#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
160160
#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
161161

162+
/* Extended hypercalls */
163+
#define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001
164+
#define HV_EXT_CALL_MEMORY_HEAT_HINT 0x8003
165+
162166
#define HV_FLUSH_ALL_PROCESSORS BIT(0)
163167
#define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES BIT(1)
164168
#define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY BIT(2)
165169
#define HV_FLUSH_USE_EXTENDED_RANGE_FORMAT BIT(3)
166170

171+
/* Extended capability bits */
172+
#define HV_EXT_CAPABILITY_MEMORY_COLD_DISCARD_HINT BIT(8)
173+
167174
enum HV_GENERIC_SET_FORMAT {
168175
HV_GENERIC_SET_SPARSE_4K,
169176
HV_GENERIC_SET_ALL,
@@ -408,15 +415,23 @@ struct hv_guest_mapping_flush {
408415
* by the bitwidth of "additional_pages" in union hv_gpa_page_range.
409416
*/
410417
#define HV_MAX_FLUSH_PAGES (2048)
418+
#define HV_GPA_PAGE_RANGE_PAGE_SIZE_2MB 0
419+
#define HV_GPA_PAGE_RANGE_PAGE_SIZE_1GB 1
411420

412-
/* HvFlushGuestPhysicalAddressList hypercall */
421+
/* HvFlushGuestPhysicalAddressList, HvExtCallMemoryHeatHint hypercall */
413422
union hv_gpa_page_range {
414423
u64 address_space;
415424
struct {
416425
u64 additional_pages:11;
417426
u64 largepage:1;
418427
u64 basepfn:52;
419428
} page;
429+
struct {
430+
u64 reserved:12;
431+
u64 page_size:1;
432+
u64 reserved1:8;
433+
u64 base_large_pfn:43;
434+
};
420435
};
421436

422437
/*
@@ -774,4 +789,20 @@ struct hv_input_unmap_device_interrupt {
774789
#define HV_SOURCE_SHADOW_NONE 0x0
775790
#define HV_SOURCE_SHADOW_BRIDGE_BUS_RANGE 0x1
776791

792+
/*
793+
* The whole argument should fit in a page to be able to pass to the hypervisor
794+
* in one hypercall.
795+
*/
796+
#define HV_MEMORY_HINT_MAX_GPA_PAGE_RANGES \
797+
((HV_HYP_PAGE_SIZE - sizeof(struct hv_memory_hint)) / \
798+
sizeof(union hv_gpa_page_range))
799+
800+
/* HvExtCallMemoryHeatHint hypercall */
801+
#define HV_EXT_MEMORY_HEAT_HINT_TYPE_COLD_DISCARD 2
802+
struct hv_memory_hint {
803+
u64 type:2;
804+
u64 reserved:62;
805+
union hv_gpa_page_range ranges[];
806+
} __packed;
807+
777808
#endif

‎include/asm-generic/mshyperv.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
struct ms_hyperv_info {
2929
u32 features;
30-
u32 features_b;
30+
u32 priv_high;
3131
u32 misc_features;
3232
u32 hints;
3333
u32 nested_features;
@@ -179,6 +179,7 @@ bool hv_is_hibernation_supported(void);
179179
enum hv_isolation_type hv_get_isolation_type(void);
180180
bool hv_is_isolation_supported(void);
181181
void hyperv_cleanup(void);
182+
bool hv_query_ext_cap(u64 cap_query);
182183
#else /* CONFIG_HYPERV */
183184
static inline bool hv_is_hyperv_initialized(void) { return false; }
184185
static inline bool hv_is_hibernation_supported(void) { return false; }

0 commit comments

Comments
 (0)