Skip to content

Commit 3817854

Browse files
NunoDasNevesliuw
authored andcommitted
hyperv: Log hypercall status codes as strings
Introduce hv_status_printk() macros as a convenience to log hypercall errors, formatting them with the status code (HV_STATUS_*) as a raw hex value and also as a string, which saves some time while debugging. Create a table of HV_STATUS_ codes with strings and mapped errnos, and use it for hv_result_to_string() and hv_result_to_errno(). Use the new hv_status_printk()s in hv_proc.c, hyperv-iommu.c, and irqdomain.c hypercalls to aid debugging in the root partition. Signed-off-by: Nuno Das Neves <nunodasneves@linux.microsoft.com> Reviewed-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com> Link: https://lore.kernel.org/r/1741980536-3865-2-git-send-email-nunodasneves@linux.microsoft.com Signed-off-by: Wei Liu <wei.liu@kernel.org> Message-ID: <1741980536-3865-2-git-send-email-nunodasneves@linux.microsoft.com>
1 parent e792d84 commit 3817854

File tree

5 files changed

+118
-44
lines changed

5 files changed

+118
-44
lines changed

arch/x86/hyperv/irqdomain.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static int hv_map_interrupt(union hv_device_id device_id, bool level,
6464
local_irq_restore(flags);
6565

6666
if (!hv_result_success(status))
67-
pr_err("%s: hypercall failed, status %lld\n", __func__, status);
67+
hv_status_err(status, "\n");
6868

6969
return hv_result(status);
7070
}
@@ -224,7 +224,7 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
224224
kfree(stored_entry);
225225

226226
if (status != HV_STATUS_SUCCESS) {
227-
pr_debug("%s: failed to unmap, status %lld", __func__, status);
227+
hv_status_debug(status, "failed to unmap\n");
228228
return;
229229
}
230230
}
@@ -273,7 +273,7 @@ static void hv_teardown_msi_irq(struct pci_dev *dev, struct irq_data *irqd)
273273
status = hv_unmap_msi_interrupt(dev, &old_entry);
274274

275275
if (status != HV_STATUS_SUCCESS)
276-
pr_err("%s: hypercall failed, status %lld\n", __func__, status);
276+
hv_status_err(status, "\n");
277277
}
278278

279279
static void hv_msi_free_irq(struct irq_domain *domain,

drivers/hv/hv_common.c

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -684,40 +684,6 @@ u64 __weak hv_tdx_hypercall(u64 control, u64 param1, u64 param2)
684684
}
685685
EXPORT_SYMBOL_GPL(hv_tdx_hypercall);
686686

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-
}
720-
721687
void hv_identify_partition_type(void)
722688
{
723689
/* Assume guest role */
@@ -740,3 +706,98 @@ void hv_identify_partition_type(void)
740706
pr_crit("Hyper-V: CONFIG_MSHV_ROOT not enabled!\n");
741707
}
742708
}
709+
710+
struct hv_status_info {
711+
char *string;
712+
int errno;
713+
u16 code;
714+
};
715+
716+
/*
717+
* Note on the errno mappings:
718+
* A failed hypercall is usually only recoverable (or loggable) near
719+
* the call site where the HV_STATUS_* code is known. So the errno
720+
* it gets converted to is not too useful further up the stack.
721+
* Provide a few mappings that could be useful, and revert to -EIO
722+
* as a fallback.
723+
*/
724+
static const struct hv_status_info hv_status_infos[] = {
725+
#define _STATUS_INFO(status, errno) { #status, (errno), (status) }
726+
_STATUS_INFO(HV_STATUS_SUCCESS, 0),
727+
_STATUS_INFO(HV_STATUS_INVALID_HYPERCALL_CODE, -EINVAL),
728+
_STATUS_INFO(HV_STATUS_INVALID_HYPERCALL_INPUT, -EINVAL),
729+
_STATUS_INFO(HV_STATUS_INVALID_ALIGNMENT, -EIO),
730+
_STATUS_INFO(HV_STATUS_INVALID_PARAMETER, -EINVAL),
731+
_STATUS_INFO(HV_STATUS_ACCESS_DENIED, -EIO),
732+
_STATUS_INFO(HV_STATUS_INVALID_PARTITION_STATE, -EIO),
733+
_STATUS_INFO(HV_STATUS_OPERATION_DENIED, -EIO),
734+
_STATUS_INFO(HV_STATUS_UNKNOWN_PROPERTY, -EIO),
735+
_STATUS_INFO(HV_STATUS_PROPERTY_VALUE_OUT_OF_RANGE, -EIO),
736+
_STATUS_INFO(HV_STATUS_INSUFFICIENT_MEMORY, -ENOMEM),
737+
_STATUS_INFO(HV_STATUS_INVALID_PARTITION_ID, -EINVAL),
738+
_STATUS_INFO(HV_STATUS_INVALID_VP_INDEX, -EINVAL),
739+
_STATUS_INFO(HV_STATUS_NOT_FOUND, -EIO),
740+
_STATUS_INFO(HV_STATUS_INVALID_PORT_ID, -EINVAL),
741+
_STATUS_INFO(HV_STATUS_INVALID_CONNECTION_ID, -EINVAL),
742+
_STATUS_INFO(HV_STATUS_INSUFFICIENT_BUFFERS, -EIO),
743+
_STATUS_INFO(HV_STATUS_NOT_ACKNOWLEDGED, -EIO),
744+
_STATUS_INFO(HV_STATUS_INVALID_VP_STATE, -EIO),
745+
_STATUS_INFO(HV_STATUS_NO_RESOURCES, -EIO),
746+
_STATUS_INFO(HV_STATUS_PROCESSOR_FEATURE_NOT_SUPPORTED, -EIO),
747+
_STATUS_INFO(HV_STATUS_INVALID_LP_INDEX, -EINVAL),
748+
_STATUS_INFO(HV_STATUS_INVALID_REGISTER_VALUE, -EINVAL),
749+
_STATUS_INFO(HV_STATUS_INVALID_LP_INDEX, -EIO),
750+
_STATUS_INFO(HV_STATUS_INVALID_REGISTER_VALUE, -EIO),
751+
_STATUS_INFO(HV_STATUS_OPERATION_FAILED, -EIO),
752+
_STATUS_INFO(HV_STATUS_TIME_OUT, -EIO),
753+
_STATUS_INFO(HV_STATUS_CALL_PENDING, -EIO),
754+
_STATUS_INFO(HV_STATUS_VTL_ALREADY_ENABLED, -EIO),
755+
#undef _STATUS_INFO
756+
};
757+
758+
static inline const struct hv_status_info *find_hv_status_info(u64 hv_status)
759+
{
760+
int i;
761+
u16 code = hv_result(hv_status);
762+
763+
for (i = 0; i < ARRAY_SIZE(hv_status_infos); ++i) {
764+
const struct hv_status_info *info = &hv_status_infos[i];
765+
766+
if (info->code == code)
767+
return info;
768+
}
769+
770+
return NULL;
771+
}
772+
773+
/* Convert a hypercall result into a linux-friendly error code. */
774+
int hv_result_to_errno(u64 status)
775+
{
776+
const struct hv_status_info *info;
777+
778+
/* hv_do_hypercall() may return U64_MAX, hypercalls aren't possible */
779+
if (unlikely(status == U64_MAX))
780+
return -EOPNOTSUPP;
781+
782+
info = find_hv_status_info(status);
783+
if (info)
784+
return info->errno;
785+
786+
return -EIO;
787+
}
788+
EXPORT_SYMBOL_GPL(hv_result_to_errno);
789+
790+
const char *hv_result_to_string(u64 status)
791+
{
792+
const struct hv_status_info *info;
793+
794+
if (unlikely(status == U64_MAX))
795+
return "Hypercall page missing!";
796+
797+
info = find_hv_status_info(status);
798+
if (info)
799+
return info->string;
800+
801+
return "Unknown";
802+
}
803+
EXPORT_SYMBOL_GPL(hv_result_to_string);

drivers/hv/hv_proc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
8787
page_count, 0, input_page, NULL);
8888
local_irq_restore(flags);
8989
if (!hv_result_success(status)) {
90-
pr_err("Failed to deposit pages: %lld\n", status);
90+
hv_status_err(status, "\n");
9191
ret = hv_result_to_errno(status);
9292
goto err_free_allocations;
9393
}
@@ -137,8 +137,8 @@ int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
137137

138138
if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
139139
if (!hv_result_success(status)) {
140-
pr_err("%s: cpu %u apic ID %u, %lld\n", __func__,
141-
lp_index, apic_id, status);
140+
hv_status_err(status, "cpu %u apic ID: %u\n",
141+
lp_index, apic_id);
142142
ret = hv_result_to_errno(status);
143143
}
144144
break;
@@ -179,8 +179,8 @@ int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
179179

180180
if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
181181
if (!hv_result_success(status)) {
182-
pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
183-
vp_index, flags, status);
182+
hv_status_err(status, "vcpu: %u, lp: %u\n",
183+
vp_index, flags);
184184
ret = hv_result_to_errno(status);
185185
}
186186
break;

drivers/iommu/hyperv-iommu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ hyperv_root_ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
217217
status = hv_unmap_ioapic_interrupt(ioapic_id, &entry);
218218

219219
if (status != HV_STATUS_SUCCESS)
220-
pr_debug("%s: unexpected unmap status %lld\n", __func__, status);
220+
hv_status_debug(status, "failed to unmap\n");
221221

222222
data->entry.ioapic_rte.as_uint64 = 0;
223223
data->entry.source = 0; /* Invalid source */
@@ -228,7 +228,7 @@ hyperv_root_ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg)
228228
vector, &entry);
229229

230230
if (status != HV_STATUS_SUCCESS) {
231-
pr_err("%s: map hypercall failed, status %lld\n", __func__, status);
231+
hv_status_err(status, "map failed\n");
232232
return;
233233
}
234234

include/asm-generic/mshyperv.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,19 @@ static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
298298
return __cpumask_to_vpset(vpset, cpus, func);
299299
}
300300

301+
#define _hv_status_fmt(fmt) "%s: Hyper-V status: %#x = %s: " fmt
302+
#define hv_status_printk(level, status, fmt, ...) \
303+
do { \
304+
u64 __status = (status); \
305+
pr_##level(_hv_status_fmt(fmt), __func__, hv_result(__status), \
306+
hv_result_to_string(__status), ##__VA_ARGS__); \
307+
} while (0)
308+
#define hv_status_err(status, fmt, ...) \
309+
hv_status_printk(err, status, fmt, ##__VA_ARGS__)
310+
#define hv_status_debug(status, fmt, ...) \
311+
hv_status_printk(debug, status, fmt, ##__VA_ARGS__)
312+
313+
const char *hv_result_to_string(u64 hv_status);
301314
int hv_result_to_errno(u64 status);
302315
void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
303316
bool hv_is_hyperv_initialized(void);

0 commit comments

Comments
 (0)