Skip to content

Commit 2d806a6

Browse files
committed
Merge tag 'hyperv-fixes-signed-20220617' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux
Pull hyperv fixes from Wei Liu: - Fix hv_init_clocksource annotation (Masahiro Yamada) - Two bug fixes for vmbus driver (Saurabh Sengar) - Fix SEV negotiation (Tianyu Lan) - Fix comments in code (Xiang Wang) - One minor fix to HID driver (Michael Kelley) * tag 'hyperv-fixes-signed-20220617' of git://git.kernel.org/pub/scm/linux/kernel/git/hyperv/linux: x86/Hyper-V: Add SEV negotiate protocol support in Isolation VM Drivers: hv: vmbus: Release cpu lock in error case HID: hyperv: Correctly access fields declared as __le16 clocksource: hyper-v: unexport __init-annotated hv_init_clocksource() Drivers: hv: Fix syntax errors in comments Drivers: hv: vmbus: Don't assign VMbus channel interrupts to isolated CPUs
2 parents 462abc9 + 49d6a3c commit 2d806a6

File tree

8 files changed

+109
-15
lines changed

8 files changed

+109
-15
lines changed

arch/x86/hyperv/hv_init.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/io.h>
1414
#include <asm/apic.h>
1515
#include <asm/desc.h>
16+
#include <asm/sev.h>
1617
#include <asm/hypervisor.h>
1718
#include <asm/hyperv-tlfs.h>
1819
#include <asm/mshyperv.h>
@@ -405,6 +406,11 @@ void __init hyperv_init(void)
405406
}
406407

407408
if (hv_isolation_type_snp()) {
409+
/* Negotiate GHCB Version. */
410+
if (!hv_ghcb_negotiate_protocol())
411+
hv_ghcb_terminate(SEV_TERM_SET_GEN,
412+
GHCB_SEV_ES_PROT_UNSUPPORTED);
413+
408414
hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
409415
if (!hv_ghcb_pg)
410416
goto free_vp_assist_page;

arch/x86/hyperv/ivm.c

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ union hv_ghcb {
5353
} hypercall;
5454
} __packed __aligned(HV_HYP_PAGE_SIZE);
5555

56+
static u16 hv_ghcb_version __ro_after_init;
57+
5658
u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
5759
{
5860
union hv_ghcb *hv_ghcb;
@@ -96,12 +98,85 @@ u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
9698
return status;
9799
}
98100

101+
static inline u64 rd_ghcb_msr(void)
102+
{
103+
return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
104+
}
105+
106+
static inline void wr_ghcb_msr(u64 val)
107+
{
108+
native_wrmsrl(MSR_AMD64_SEV_ES_GHCB, val);
109+
}
110+
111+
static enum es_result hv_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code,
112+
u64 exit_info_1, u64 exit_info_2)
113+
{
114+
/* Fill in protocol and format specifiers */
115+
ghcb->protocol_version = hv_ghcb_version;
116+
ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
117+
118+
ghcb_set_sw_exit_code(ghcb, exit_code);
119+
ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
120+
ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
121+
122+
VMGEXIT();
123+
124+
if (ghcb->save.sw_exit_info_1 & GENMASK_ULL(31, 0))
125+
return ES_VMM_ERROR;
126+
else
127+
return ES_OK;
128+
}
129+
130+
void hv_ghcb_terminate(unsigned int set, unsigned int reason)
131+
{
132+
u64 val = GHCB_MSR_TERM_REQ;
133+
134+
/* Tell the hypervisor what went wrong. */
135+
val |= GHCB_SEV_TERM_REASON(set, reason);
136+
137+
/* Request Guest Termination from Hypvervisor */
138+
wr_ghcb_msr(val);
139+
VMGEXIT();
140+
141+
while (true)
142+
asm volatile("hlt\n" : : : "memory");
143+
}
144+
145+
bool hv_ghcb_negotiate_protocol(void)
146+
{
147+
u64 ghcb_gpa;
148+
u64 val;
149+
150+
/* Save ghcb page gpa. */
151+
ghcb_gpa = rd_ghcb_msr();
152+
153+
/* Do the GHCB protocol version negotiation */
154+
wr_ghcb_msr(GHCB_MSR_SEV_INFO_REQ);
155+
VMGEXIT();
156+
val = rd_ghcb_msr();
157+
158+
if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
159+
return false;
160+
161+
if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
162+
GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
163+
return false;
164+
165+
hv_ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val),
166+
GHCB_PROTOCOL_MAX);
167+
168+
/* Write ghcb page back after negotiating protocol. */
169+
wr_ghcb_msr(ghcb_gpa);
170+
VMGEXIT();
171+
172+
return true;
173+
}
174+
99175
void hv_ghcb_msr_write(u64 msr, u64 value)
100176
{
101177
union hv_ghcb *hv_ghcb;
102178
void **ghcb_base;
103179
unsigned long flags;
104-
struct es_em_ctxt ctxt;
105180

106181
if (!hv_ghcb_pg)
107182
return;
@@ -120,8 +195,7 @@ void hv_ghcb_msr_write(u64 msr, u64 value)
120195
ghcb_set_rax(&hv_ghcb->ghcb, lower_32_bits(value));
121196
ghcb_set_rdx(&hv_ghcb->ghcb, upper_32_bits(value));
122197

123-
if (sev_es_ghcb_hv_call(&hv_ghcb->ghcb, false, &ctxt,
124-
SVM_EXIT_MSR, 1, 0))
198+
if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 1, 0))
125199
pr_warn("Fail to write msr via ghcb %llx.\n", msr);
126200

127201
local_irq_restore(flags);
@@ -133,7 +207,6 @@ void hv_ghcb_msr_read(u64 msr, u64 *value)
133207
union hv_ghcb *hv_ghcb;
134208
void **ghcb_base;
135209
unsigned long flags;
136-
struct es_em_ctxt ctxt;
137210

138211
/* Check size of union hv_ghcb here. */
139212
BUILD_BUG_ON(sizeof(union hv_ghcb) != HV_HYP_PAGE_SIZE);
@@ -152,8 +225,7 @@ void hv_ghcb_msr_read(u64 msr, u64 *value)
152225
}
153226

154227
ghcb_set_rcx(&hv_ghcb->ghcb, msr);
155-
if (sev_es_ghcb_hv_call(&hv_ghcb->ghcb, false, &ctxt,
156-
SVM_EXIT_MSR, 0, 0))
228+
if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 0, 0))
157229
pr_warn("Fail to read msr via ghcb %llx.\n", msr);
158230
else
159231
*value = (u64)lower_32_bits(hv_ghcb->ghcb.save.rax)

arch/x86/include/asm/mshyperv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,13 @@ int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible);
179179
#ifdef CONFIG_AMD_MEM_ENCRYPT
180180
void hv_ghcb_msr_write(u64 msr, u64 value);
181181
void hv_ghcb_msr_read(u64 msr, u64 *value);
182+
bool hv_ghcb_negotiate_protocol(void);
183+
void hv_ghcb_terminate(unsigned int set, unsigned int reason);
182184
#else
183185
static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
184186
static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
187+
static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
188+
static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
185189
#endif
186190

187191
extern bool hv_isolation_type_snp(void);

drivers/clocksource/hyperv_timer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,3 @@ void __init hv_init_clocksource(void)
565565
hv_sched_clock_offset = hv_read_reference_counter();
566566
hv_setup_sched_clock(read_hv_sched_clock_msr);
567567
}
568-
EXPORT_SYMBOL_GPL(hv_init_clocksource);

drivers/hid/hid-hyperv.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
199199
if (!input_device->hid_desc)
200200
goto cleanup;
201201

202-
input_device->report_desc_size = desc->desc[0].wDescriptorLength;
202+
input_device->report_desc_size = le16_to_cpu(
203+
desc->desc[0].wDescriptorLength);
203204
if (input_device->report_desc_size == 0) {
204205
input_device->dev_info_status = -EINVAL;
205206
goto cleanup;
@@ -217,7 +218,7 @@ static void mousevsc_on_receive_device_info(struct mousevsc_dev *input_device,
217218

218219
memcpy(input_device->report_desc,
219220
((unsigned char *)desc) + desc->bLength,
220-
desc->desc[0].wDescriptorLength);
221+
le16_to_cpu(desc->desc[0].wDescriptorLength));
221222

222223
/* Send the ack */
223224
memset(&ack, 0, sizeof(struct mousevsc_prt_msg));

drivers/hv/channel_mgmt.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/cpu.h>
2222
#include <linux/hyperv.h>
2323
#include <asm/mshyperv.h>
24+
#include <linux/sched/isolation.h>
2425

2526
#include "hyperv_vmbus.h"
2627

@@ -638,6 +639,7 @@ static void vmbus_process_offer(struct vmbus_channel *newchannel)
638639
*/
639640
if (newchannel->offermsg.offer.sub_channel_index == 0) {
640641
mutex_unlock(&vmbus_connection.channel_mutex);
642+
cpus_read_unlock();
641643
/*
642644
* Don't call free_channel(), because newchannel->kobj
643645
* is not initialized yet.
@@ -728,16 +730,20 @@ static void init_vp_index(struct vmbus_channel *channel)
728730
u32 i, ncpu = num_online_cpus();
729731
cpumask_var_t available_mask;
730732
struct cpumask *allocated_mask;
733+
const struct cpumask *hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
731734
u32 target_cpu;
732735
int numa_node;
733736

734737
if (!perf_chn ||
735-
!alloc_cpumask_var(&available_mask, GFP_KERNEL)) {
738+
!alloc_cpumask_var(&available_mask, GFP_KERNEL) ||
739+
cpumask_empty(hk_mask)) {
736740
/*
737741
* If the channel is not a performance critical
738742
* channel, bind it to VMBUS_CONNECT_CPU.
739743
* In case alloc_cpumask_var() fails, bind it to
740744
* VMBUS_CONNECT_CPU.
745+
* If all the cpus are isolated, bind it to
746+
* VMBUS_CONNECT_CPU.
741747
*/
742748
channel->target_cpu = VMBUS_CONNECT_CPU;
743749
if (perf_chn)
@@ -758,17 +764,19 @@ static void init_vp_index(struct vmbus_channel *channel)
758764
}
759765
allocated_mask = &hv_context.hv_numa_map[numa_node];
760766

761-
if (cpumask_equal(allocated_mask, cpumask_of_node(numa_node))) {
767+
retry:
768+
cpumask_xor(available_mask, allocated_mask, cpumask_of_node(numa_node));
769+
cpumask_and(available_mask, available_mask, hk_mask);
770+
771+
if (cpumask_empty(available_mask)) {
762772
/*
763773
* We have cycled through all the CPUs in the node;
764774
* reset the allocated map.
765775
*/
766776
cpumask_clear(allocated_mask);
777+
goto retry;
767778
}
768779

769-
cpumask_xor(available_mask, allocated_mask,
770-
cpumask_of_node(numa_node));
771-
772780
target_cpu = cpumask_first(available_mask);
773781
cpumask_set_cpu(target_cpu, allocated_mask);
774782

drivers/hv/hv_kvp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ kvp_send_key(struct work_struct *dummy)
394394
in_msg = kvp_transaction.kvp_msg;
395395

396396
/*
397-
* The key/value strings sent from the host are encoded in
397+
* The key/value strings sent from the host are encoded
398398
* in utf16; convert it to utf8 strings.
399399
* The host assures us that the utf16 strings will not exceed
400400
* the max lengths specified. We will however, reserve room

drivers/hv/vmbus_drv.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/kernel_stat.h>
2222
#include <linux/clockchips.h>
2323
#include <linux/cpu.h>
24+
#include <linux/sched/isolation.h>
2425
#include <linux/sched/task_stack.h>
2526

2627
#include <linux/delay.h>
@@ -1770,6 +1771,9 @@ static ssize_t target_cpu_store(struct vmbus_channel *channel,
17701771
if (target_cpu >= nr_cpumask_bits)
17711772
return -EINVAL;
17721773

1774+
if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ)))
1775+
return -EINVAL;
1776+
17731777
/* No CPUs should come up or down during this. */
17741778
cpus_read_lock();
17751779

0 commit comments

Comments
 (0)