Skip to content

Commit 9d8731a

Browse files
NunoDasNevesliuw
authored andcommitted
hyperv: Convert hypercall statuses to linux error codes
Return linux-friendly error codes from hypercall helper functions, which allows them to be used more flexibly. Introduce hv_result_to_errno() for this purpose, which also handles the special value U64_MAX returned from hv_do_hypercall(). Signed-off-by: Nuno Das Neves <[email protected]> Reviewed-by: Easwar Hariharan <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/1740167795-13296-2-git-send-email-nunodasneves@linux.microsoft.com Signed-off-by: Wei Liu <[email protected]> Message-ID: <1740167795-13296-2-git-send-email-nunodasneves@linux.microsoft.com>
1 parent 3a7f778 commit 9d8731a

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

drivers/hv/hv_common.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,37 @@ u64 __weak hv_tdx_hypercall(u64 control, u64 param1, u64 param2)
683683
return HV_STATUS_INVALID_PARAMETER;
684684
}
685685
EXPORT_SYMBOL_GPL(hv_tdx_hypercall);
686+
687+
/* Convert a hypercall result into a linux-friendly error code. */
688+
int hv_result_to_errno(u64 status)
689+
{
690+
/* hv_do_hypercall() may return U64_MAX, hypercalls aren't possible */
691+
if (unlikely(status == U64_MAX))
692+
return -EOPNOTSUPP;
693+
/*
694+
* A failed hypercall is usually only recoverable (or loggable) near
695+
* the call site where the HV_STATUS_* code is known. So the errno
696+
* it gets converted to is not too useful further up the stack.
697+
* Provide a few mappings that could be useful, and revert to -EIO
698+
* as a fallback.
699+
*/
700+
switch (hv_result(status)) {
701+
case HV_STATUS_SUCCESS:
702+
return 0;
703+
case HV_STATUS_INVALID_HYPERCALL_CODE:
704+
case HV_STATUS_INVALID_HYPERCALL_INPUT:
705+
case HV_STATUS_INVALID_PARAMETER:
706+
case HV_STATUS_INVALID_PARTITION_ID:
707+
case HV_STATUS_INVALID_VP_INDEX:
708+
case HV_STATUS_INVALID_PORT_ID:
709+
case HV_STATUS_INVALID_CONNECTION_ID:
710+
case HV_STATUS_INVALID_LP_INDEX:
711+
case HV_STATUS_INVALID_REGISTER_VALUE:
712+
return -EINVAL;
713+
case HV_STATUS_INSUFFICIENT_MEMORY:
714+
return -ENOMEM;
715+
default:
716+
break;
717+
}
718+
return -EIO;
719+
}

drivers/hv/hv_proc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
8888
local_irq_restore(flags);
8989
if (!hv_result_success(status)) {
9090
pr_err("Failed to deposit pages: %lld\n", status);
91-
ret = hv_result(status);
91+
ret = hv_result_to_errno(status);
9292
goto err_free_allocations;
9393
}
9494

@@ -114,7 +114,7 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
114114
struct hv_output_add_logical_processor *output;
115115
u64 status;
116116
unsigned long flags;
117-
int ret = HV_STATUS_SUCCESS;
117+
int ret = 0;
118118

119119
/*
120120
* When adding a logical processor, the hypervisor may return
@@ -139,7 +139,7 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
139139
if (!hv_result_success(status)) {
140140
pr_err("%s: cpu %u apic ID %u, %lld\n", __func__,
141141
lp_index, apic_id, status);
142-
ret = hv_result(status);
142+
ret = hv_result_to_errno(status);
143143
}
144144
break;
145145
}
@@ -154,7 +154,7 @@ int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
154154
struct hv_create_vp *input;
155155
u64 status;
156156
unsigned long irq_flags;
157-
int ret = HV_STATUS_SUCCESS;
157+
int ret = 0;
158158

159159
/* Root VPs don't seem to need pages deposited */
160160
if (partition_id != hv_current_partition_id) {
@@ -181,7 +181,7 @@ int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
181181
if (!hv_result_success(status)) {
182182
pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
183183
vp_index, flags, status);
184-
ret = hv_result(status);
184+
ret = hv_result_to_errno(status);
185185
}
186186
break;
187187
}

include/asm-generic/mshyperv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
297297
return __cpumask_to_vpset(vpset, cpus, func);
298298
}
299299

300+
int hv_result_to_errno(u64 status);
300301
void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
301302
bool hv_is_hyperv_initialized(void);
302303
bool hv_is_hibernation_supported(void);

0 commit comments

Comments
 (0)