[PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()

Roman Kisel posted 5 patches 1 year ago
arch/x86/hyperv/hv_init.c   |  6 +++---
arch/x86/hyperv/hv_vtl.c    |  2 +-
drivers/hv/hv_common.c      | 11 +++++++---
include/hyperv/hvgdk_mini.h | 41 +++++++++++++++++++++++++++++++++++++
4 files changed, 53 insertions(+), 7 deletions(-)
[PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Roman Kisel 1 year ago
The get_vtl(void) function

* has got one bug when the code started using a wrong pointer type after
  refactoring, and also
* the both function in question don't adhere to the requirements of
  the Hypervisor Top-Level Funactional Specification[1, 2] as the code overlaps
  the input and output areas for a hypercall.

The first issue leads to a wrong 100% reproducible computation due to reading
a byte worth of data at a wrong offset. That in turn leads to using a nonsensical
value ("fortunately", could catch it easily!) for the current VTL when initiating
VMBus communications. As a repercussion from that, the system wouldn't boot. The
fix is straightforward: use the correct pointer type.

The second issue doesn't seem to lead to any reproducible breakage just yet. It is
fixed with using the output hypercall pages allocated per-CPU, and that isn't the
only or the most obvious choice so let me elaborate why that fix appears to be the
best one in my opinion out of the options I could conceive of.

The approach chosen for fixing the second issue makes two things shine through:

* these functions just get a vCPU register, no special treatment needs to be
  involved,
* VTLs and dom0 can and should share code as both exist to provide services to
  a guest(s), be that from within the partition or from outside of it.

The projected benefits include replacing the functions in question with a future
`hv_get_vp_registers` one shared between dom0 and VTLs to allow for a better test
coverage.

I have validated the fixes by booting the fixed kernel in VTL2 up using OpenVMM and
OpenHCL[3, 4].

[1] https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/tlfs/hypercall-interface
[2] https://github.com/MicrosoftDocs/Virtualization-Documentation/tree/main/tlfs
[3] https://openvmm.dev/guide/user_guide/openhcl.html
[4] https://github.com/microsoft/OpenVMM

[v6]
  - Sending v6 a week after v5 to adhere to the requirements of the kernel
    documentation,
  - Added all tags the patches received prior to v6,
  - Fixed the bitfield layout for the ARM64 structures (patch 1),
  - Hoisted the common condition used in several if statements into
    a separate function to improve code maintainability (patch 3).

[v5]: https://lore.kernel.org/lkml/20241230180941.244418-1-romank@linux.microsoft.com/
  - In the first patch, removed some arch-specific #ifdef guards to fix the
    arm64 build and stick to the direction chosen for the Hyper-V header files.
    I could not remove all of them as some interrupt state structures
    are defined differently for x64 and arm64 and are found in the same
    enclosing `union hv_register_value`.

    No changes to other patches (approved in v4).

[v4]: https://lore.kernel.org/lkml/20241227183155.122827-1-romank@linux.microsoft.com/
  - Wrapped DECLARE_FLEX_ARRAY into a struct and added one more
    member as the documentation requires,
  - Removed superfluous type coercion,
  - Fixed tags,
  - Rebased onto the latest hyperv-next branch.

[v3]: https://lore.kernel.org/lkml/20241226213110.899497-1-romank@linux.microsoft.com/
  - Added a fix for hv_vtl_apicid_to_vp_id(),
  - Split out the patch for enabling the hypercall output page,
  - Updated the title of the patch series,

[v2]: https://lore.kernel.org/lkml/20241226203050.800524-1-romank@linux.microsoft.com/
  - Used the suggestions to define an additional structure to improve code readability,
  - Split out the patch with that definition.

[v1]: https://lore.kernel.org/lkml/20241218205421.319969-1-romank@linux.microsoft.com/

Roman Kisel (5):
  hyperv: Define struct hv_output_get_vp_registers
  hyperv: Fix pointer type in get_vtl(void)
  hyperv: Enable the hypercall output page for the VTL mode
  hyperv: Do not overlap the hvcall IO areas in get_vtl()
  hyperv: Do not overlap the hvcall IO areas in hv_vtl_apicid_to_vp_id()

 arch/x86/hyperv/hv_init.c   |  6 +++---
 arch/x86/hyperv/hv_vtl.c    |  2 +-
 drivers/hv/hv_common.c      | 11 +++++++---
 include/hyperv/hvgdk_mini.h | 41 +++++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 7 deletions(-)


base-commit: 26e1b813fcd02984b1cac5f3decdf4b0bb56fe02
-- 
2.34.1
Re: [PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Wei Liu 1 year ago
On Wed, Jan 08, 2025 at 02:21:33PM -0800, Roman Kisel wrote:
[...]
> Roman Kisel (5):
>   hyperv: Define struct hv_output_get_vp_registers
>   hyperv: Fix pointer type in get_vtl(void)
>   hyperv: Enable the hypercall output page for the VTL mode
>   hyperv: Do not overlap the hvcall IO areas in get_vtl()
>   hyperv: Do not overlap the hvcall IO areas in hv_vtl_apicid_to_vp_id()

The patches have been pushed to hyperv-next. Roman and Nuno, please
check the tree for correctness.

Thanks,
Wei.
Re: [PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Roman Kisel 1 year ago

On 1/9/2025 12:18 PM, Wei Liu wrote:
> On Wed, Jan 08, 2025 at 02:21:33PM -0800, Roman Kisel wrote:
> [...]
>> Roman Kisel (5):
>>    hyperv: Define struct hv_output_get_vp_registers
>>    hyperv: Fix pointer type in get_vtl(void)
>>    hyperv: Enable the hypercall output page for the VTL mode
>>    hyperv: Do not overlap the hvcall IO areas in get_vtl()
>>    hyperv: Do not overlap the hvcall IO areas in hv_vtl_apicid_to_vp_id()
> 
> The patches have been pushed to hyperv-next. Roman and Nuno, please
> check the tree for correctness.

This

```c
union hv_arm64_pending_synthetic_exception_event {
	u64 as_uint64[2];
	struct {
		u8 event_pending : 1;
		u8 event_type : 3;
		u8 reserved : 4;
		u8 rsvd[3];
		u64 context;
	} __packed;
};
```

needs to have the `u32 exception_type;` field:

```c
union hv_arm64_pending_synthetic_exception_event {
	u64 as_uint64[2];
	struct {
		u8 event_pending : 1;
		u8 event_type : 3;
		u8 reserved : 4;
		u8 rsvd[3];
		u32 exception_type;
		u64 context;
	} __packed;
};
```
as otherwise the struct won't cover the array.
Testing the VMs currently with the latest hyperv-next.

> 
> Thanks,
> Wei.

-- 
Thank you,
Roman
Re: [PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Wei Liu 1 year ago
On Thu, Jan 09, 2025 at 01:40:34PM -0800, Roman Kisel wrote:
> 
> 
> On 1/9/2025 12:18 PM, Wei Liu wrote:
> > On Wed, Jan 08, 2025 at 02:21:33PM -0800, Roman Kisel wrote:
> > [...]
> > > Roman Kisel (5):
> > >    hyperv: Define struct hv_output_get_vp_registers
> > >    hyperv: Fix pointer type in get_vtl(void)
> > >    hyperv: Enable the hypercall output page for the VTL mode
> > >    hyperv: Do not overlap the hvcall IO areas in get_vtl()
> > >    hyperv: Do not overlap the hvcall IO areas in hv_vtl_apicid_to_vp_id()
> > 
> > The patches have been pushed to hyperv-next. Roman and Nuno, please
> > check the tree for correctness.
> 
> This
> 
> ```c
> union hv_arm64_pending_synthetic_exception_event {
> 	u64 as_uint64[2];
> 	struct {
> 		u8 event_pending : 1;
> 		u8 event_type : 3;
> 		u8 reserved : 4;
> 		u8 rsvd[3];
> 		u64 context;
> 	} __packed;
> };
> ```
> 
> needs to have the `u32 exception_type;` field:
> 
> ```c
> union hv_arm64_pending_synthetic_exception_event {
> 	u64 as_uint64[2];
> 	struct {
> 		u8 event_pending : 1;
> 		u8 event_type : 3;
> 		u8 reserved : 4;
> 		u8 rsvd[3];
> 		u32 exception_type;
> 		u64 context;
> 	} __packed;
> };
> ```
> as otherwise the struct won't cover the array.
> Testing the VMs currently with the latest hyperv-next.

Fixed. I c&p'ed the code then deleted the right version of the struct.
Thanks for checking.

Wei.
Re: [PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Roman Kisel 1 year ago

On 1/9/2025 1:56 PM, Wei Liu wrote:
> On Thu, Jan 09, 2025 at 01:40:34PM -0800, Roman Kisel wrote:

[...]

>>
>> needs to have the `u32 exception_type;` field:
>>
>> ```c
>> union hv_arm64_pending_synthetic_exception_event {
>> 	u64 as_uint64[2];
>> 	struct {
>> 		u8 event_pending : 1;
>> 		u8 event_type : 3;
>> 		u8 reserved : 4;
>> 		u8 rsvd[3];
>> 		u32 exception_type;
>> 		u64 context;
>> 	} __packed;
>> };
>> ```
>> as otherwise the struct won't cover the array.
>> Testing the VMs currently with the latest hyperv-next.
> 
> Fixed. I c&p'ed the code then deleted the right version of the struct.
> Thanks for checking.

Happy to help :D

Validated with the VMs, and with the latest hyperv-next, the issue is
fixed!! Appreciate your help and guidance; thank you, Easwar, Michael,
Nuno, Stanislav, Tianyu and Wei for the suggestions that have let make 
this patchset so much better :)

Borislav, I apologize for sending the patchset versions too often. I'm
sorry for causing you trouble due to that. I have read up the kernel
documentation, and will be a better citizen of the LKML.

> 
> Wei.

-- 
Thank you,
Roman
Re: [PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Nuno Das Neves 1 year ago
On 1/9/2025 12:18 PM, Wei Liu wrote:
> On Wed, Jan 08, 2025 at 02:21:33PM -0800, Roman Kisel wrote:
> [...]
>> Roman Kisel (5):
>>   hyperv: Define struct hv_output_get_vp_registers
>>   hyperv: Fix pointer type in get_vtl(void)
>>   hyperv: Enable the hypercall output page for the VTL mode
>>   hyperv: Do not overlap the hvcall IO areas in get_vtl()
>>   hyperv: Do not overlap the hvcall IO areas in hv_vtl_apicid_to_vp_id()
> 
> The patches have been pushed to hyperv-next. Roman and Nuno, please
> check the tree for correctness.
> 
> Thanks,
> Wei.

I checked, looks like the first two patches of the series are missing?

Nuno
Re: [PATCH v6 0/5] hyperv: Fixes for get_vtl(), hv_vtl_apicid_to_vp_id()
Posted by Roman Kisel 1 year ago

On 1/9/2025 12:28 PM, Nuno Das Neves wrote:
> On 1/9/2025 12:18 PM, Wei Liu wrote:
>> On Wed, Jan 08, 2025 at 02:21:33PM -0800, Roman Kisel wrote:
>> [...]
>>> Roman Kisel (5):
>>>    hyperv: Define struct hv_output_get_vp_registers
>>>    hyperv: Fix pointer type in get_vtl(void)
>>>    hyperv: Enable the hypercall output page for the VTL mode
>>>    hyperv: Do not overlap the hvcall IO areas in get_vtl()
>>>    hyperv: Do not overlap the hvcall IO areas in hv_vtl_apicid_to_vp_id()
>>
>> The patches have been pushed to hyperv-next. Roman and Nuno, please
>> check the tree for correctness.
>>
>> Thanks,
>> Wei.
> 
> I checked, looks like the first two patches of the series are missing?
IIUC, they were to be rolled up into your earlier patches

> 
> Nuno

-- 
Thank you,
Roman