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 <nunodasne...@linux.microsoft.com>
Reviewed-by: Easwar Hariharan <eahar...@linux.microsoft.com>
Reviewed-by: Michael Kelley <mhkli...@outlook.com>
---
 drivers/hv/hv_common.c         | 34 ++++++++++++++++++++++++++++++++++
 drivers/hv/hv_proc.c           | 10 +++++-----
 include/asm-generic/mshyperv.h |  1 +
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
index ee3083937b4f..5cf9894b9e79 100644
--- a/drivers/hv/hv_common.c
+++ b/drivers/hv/hv_common.c
@@ -683,3 +683,37 @@ u64 __weak hv_tdx_hypercall(u64 control, u64 param1, u64 
param2)
        return HV_STATUS_INVALID_PARAMETER;
 }
 EXPORT_SYMBOL_GPL(hv_tdx_hypercall);
+
+/* Convert a hypercall result into a linux-friendly error code. */
+int hv_result_to_errno(u64 status)
+{
+       /* hv_do_hypercall() may return U64_MAX, hypercalls aren't possible */
+       if (unlikely(status == U64_MAX))
+               return -EOPNOTSUPP;
+       /*
+        * A failed hypercall is usually only recoverable (or loggable) near
+        * the call site where the HV_STATUS_* code is known. So the errno
+        * it gets converted to is not too useful further up the stack.
+        * Provide a few mappings that could be useful, and revert to -EIO
+        * as a fallback.
+        */
+       switch (hv_result(status)) {
+       case HV_STATUS_SUCCESS:
+               return 0;
+       case HV_STATUS_INVALID_HYPERCALL_CODE:
+       case HV_STATUS_INVALID_HYPERCALL_INPUT:
+       case HV_STATUS_INVALID_PARAMETER:
+       case HV_STATUS_INVALID_PARTITION_ID:
+       case HV_STATUS_INVALID_VP_INDEX:
+       case HV_STATUS_INVALID_PORT_ID:
+       case HV_STATUS_INVALID_CONNECTION_ID:
+       case HV_STATUS_INVALID_LP_INDEX:
+       case HV_STATUS_INVALID_REGISTER_VALUE:
+               return -EINVAL;
+       case HV_STATUS_INSUFFICIENT_MEMORY:
+               return -ENOMEM;
+       default:
+               break;
+       }
+       return -EIO;
+}
diff --git a/drivers/hv/hv_proc.c b/drivers/hv/hv_proc.c
index 3e410489f480..2fae18e4f7d2 100644
--- a/drivers/hv/hv_proc.c
+++ b/drivers/hv/hv_proc.c
@@ -88,7 +88,7 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 
num_pages)
        local_irq_restore(flags);
        if (!hv_result_success(status)) {
                pr_err("Failed to deposit pages: %lld\n", status);
-               ret = hv_result(status);
+               ret = hv_result_to_errno(status);
                goto err_free_allocations;
        }
 
@@ -114,7 +114,7 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 
apic_id)
        struct hv_output_add_logical_processor *output;
        u64 status;
        unsigned long flags;
-       int ret = HV_STATUS_SUCCESS;
+       int ret = 0;
 
        /*
         * 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)
                        if (!hv_result_success(status)) {
                                pr_err("%s: cpu %u apic ID %u, %lld\n", 
__func__,
                                       lp_index, apic_id, status);
-                               ret = hv_result(status);
+                               ret = hv_result_to_errno(status);
                        }
                        break;
                }
@@ -154,7 +154,7 @@ int hv_call_create_vp(int node, u64 partition_id, u32 
vp_index, u32 flags)
        struct hv_create_vp *input;
        u64 status;
        unsigned long irq_flags;
-       int ret = HV_STATUS_SUCCESS;
+       int ret = 0;
 
        /* Root VPs don't seem to need pages deposited */
        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)
                        if (!hv_result_success(status)) {
                                pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
                                       vp_index, flags, status);
-                               ret = hv_result(status);
+                               ret = hv_result_to_errno(status);
                        }
                        break;
                }
diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h
index 7adc10a4fa3e..3f115e2bcdaa 100644
--- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h
@@ -297,6 +297,7 @@ static inline int cpumask_to_vpset_skip(struct hv_vpset 
*vpset,
        return __cpumask_to_vpset(vpset, cpus, func);
 }
 
+int hv_result_to_errno(u64 status);
 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
 bool hv_is_hyperv_initialized(void);
 bool hv_is_hibernation_supported(void);
-- 
2.34.1


Reply via email to