Skip to content

Commit 6dce7bc

Browse files
kelleymhSasha Levin
authored andcommitted
arm64: hyperv: Add memory alloc/free functions for Hyper-V size pages
Add ARM64-specific code to allocate memory with HV_HYP_PAGE_SIZE size and alignment. These are for use when pages need to be shared with Hyper-V. Separate functions are needed as the page size used by Hyper-V may not be the same as the guest page size. This code is built only when CONFIG_HYPERV is enabled. Signed-off-by: Michael Kelley <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent a7e6159 commit 6dce7bc

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

arch/arm64/hyperv/hv_core.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,49 @@
2323
#include <asm/mshyperv.h>
2424

2525

26+
/*
27+
* Functions for allocating and freeing memory with size and
28+
* alignment HV_HYP_PAGE_SIZE. These functions are needed because
29+
* the guest page size may not be the same as the Hyper-V page
30+
* size. We depend upon kmalloc() aligning power-of-two size
31+
* allocations to the allocation size boundary, so that the
32+
* allocated memory appears to Hyper-V as a page of the size
33+
* it expects.
34+
*
35+
* These functions are used by arm64 specific code as well as
36+
* arch independent Hyper-V drivers.
37+
*/
38+
39+
void *hv_alloc_hyperv_page(void)
40+
{
41+
BUILD_BUG_ON(PAGE_SIZE < HV_HYP_PAGE_SIZE);
42+
43+
if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
44+
return (void *)__get_free_page(GFP_KERNEL);
45+
else
46+
return kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
47+
}
48+
EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
49+
50+
void *hv_alloc_hyperv_zeroed_page(void)
51+
{
52+
if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
53+
return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
54+
else
55+
return kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
56+
}
57+
EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page);
58+
59+
void hv_free_hyperv_page(unsigned long addr)
60+
{
61+
if (PAGE_SIZE == HV_HYP_PAGE_SIZE)
62+
free_page(addr);
63+
else
64+
kfree((void *)addr);
65+
}
66+
EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
67+
68+
2669
/*
2770
* hv_do_hypercall- Invoke the specified hypercall
2871
*/

include/asm-generic/mshyperv.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ void hv_remove_crash_handler(void);
101101

102102
extern int vmbus_interrupt;
103103

104+
void *hv_alloc_hyperv_page(void);
105+
void *hv_alloc_hyperv_zeroed_page(void);
106+
void hv_free_hyperv_page(unsigned long addr);
107+
108+
104109
#if IS_ENABLED(CONFIG_HYPERV)
105110
/*
106111
* Hypervisor's notion of virtual processor ID is different from

0 commit comments

Comments
 (0)