Re: [PATCH v2 07/17] x86/hyperv: extract partition ID from Microsoft Hypervisor if necessary

From: Wei Liu
Date: Fri Nov 13 2020 - 10:22:02 EST


On Thu, Nov 12, 2020 at 04:44:39PM +0100, Vitaly Kuznetsov wrote:
[...]
> > +void __init hv_get_partition_id(void)
> > +{
> > + struct hv_get_partition_id *output_page;
> > + u16 status;
> > + unsigned long flags;
> > +
> > + local_irq_save(flags);
> > + output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
> > + status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page) &
> > + HV_HYPERCALL_RESULT_MASK;
> > + if (status != HV_STATUS_SUCCESS)
> > + pr_err("Failed to get partition ID: %d\n", status);
> > + else
> > + hv_current_partition_id = output_page->partition_id;
>
> Nit: I'd suggest we simplify this to:
>
> if (status != HV_STATUS_SUCCESS) {
> pr_err("Failed to get partition ID: %d\n", status);
> BUG();
> }
> hv_current_partition_id = output_page->partition_id;
>
> and drop BUG_ON() below;
>
> > + local_irq_restore(flags);
> > +
> > + /* No point in proceeding if this failed */
> > + BUG_ON(status != HV_STATUS_SUCCESS);
> > +}
> > +
> > /*
> > * This function is to be invoked early in the boot sequence after the
> > * hypervisor has been detected.
> > @@ -430,6 +453,9 @@ void __init hyperv_init(void)
> >
> > register_syscore_ops(&hv_syscore_ops);
> >
> > + if (hv_root_partition)
> > + hv_get_partition_id();
> > +
>
>
> We don't seem to check that the partition has AccessPartitionId
> privilege. While I guess that root partitions always have it, I'd
> suggest we write this as:
>

Yes. Root should always have that permission. That's my understanding.

> if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
> hv_get_partition_id();
>
> BUG_ON(hv_root_partition && !hv_current_partition_id);
>
> for correctness. Also, we need to make sure '0' is not a valid partition
> id and use e.g. -1 otherwise.
>

I've changed both places.

Wei.