On 31.07.2024 10:31, Chen, Jiqian wrote:
> On 2024/7/30 21:09, Andrew Cooper wrote:
>> On 08/07/2024 12:41 pm, Jiqian Chen wrote:
>>> If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for
>>> a passthrough device by using gsi, see qemu code
>>> xen_pt_realize->xc_physdev_map_pirq and libxl code
>>> pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq
>>> will call into Xen, but in hvm_physdev_op, PHYSDEVOP_map_pirq
>>> is not allowed because currd is PVH dom0 and PVH has no
>>> X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.
>>>
>>> So, allow PHYSDEVOP_map_pirq when dom0 is PVH and also allow
>>> PHYSDEVOP_unmap_pirq for the removal device path to unmap pirq.
>>> And add a new check to prevent (un)map when the subject domain
>>> doesn't have a notion of PIRQ.
>>>
>>> So that the interrupt of a passthrough device can be
>>> successfully mapped to pirq for domU with a notion of PIRQ
>>> when dom0 is PVH
>>>
>>> Signed-off-by: Jiqian Chen <jiqian.c...@amd.com>
>>> Signed-off-by: Huang Rui <ray.hu...@amd.com>
>>> Signed-off-by: Jiqian Chen <jiqian.c...@amd.com>
>>> ---
>>>  xen/arch/x86/hvm/hypercall.c |  6 ++++++
>>>  xen/arch/x86/physdev.c       | 12 ++++++++++--
>>>  2 files changed, 16 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/xen/arch/x86/hvm/hypercall.c b/xen/arch/x86/hvm/hypercall.c
>>> index 0fab670a4871..03ada3c880bd 100644
>>> --- a/xen/arch/x86/hvm/hypercall.c
>>> +++ b/xen/arch/x86/hvm/hypercall.c
>>> @@ -71,8 +71,14 @@ long hvm_physdev_op(int cmd, 
>>> XEN_GUEST_HANDLE_PARAM(void) arg)
>>>  
>>>      switch ( cmd )
>>>      {
>>> +        /*
>>> +        * Only being permitted for management of other domains.
>>> +        * Further restrictions are enforced in do_physdev_op.
>>> +        */
>>>      case PHYSDEVOP_map_pirq:
>>>      case PHYSDEVOP_unmap_pirq:
>>> +        break;
>>> +
>>>      case PHYSDEVOP_eoi:
>>>      case PHYSDEVOP_irq_status_query:
>>>      case PHYSDEVOP_get_free_pirq:
>>> diff --git a/xen/arch/x86/physdev.c b/xen/arch/x86/physdev.c
>>> index d6dd622952a9..9f30a8c63a06 100644
>>> --- a/xen/arch/x86/physdev.c
>>> +++ b/xen/arch/x86/physdev.c
>>> @@ -323,7 +323,11 @@ ret_t do_physdev_op(int cmd, 
>>> XEN_GUEST_HANDLE_PARAM(void) arg)
>>>          if ( !d )
>>>              break;
>>>  
>>> -        ret = physdev_map_pirq(d, map.type, &map.index, &map.pirq, &msi);
>>> +        /* Only mapping when the subject domain has a notion of PIRQ */
>>> +        if ( !is_hvm_domain(d) || has_pirq(d) )
>>> +            ret = physdev_map_pirq(d, map.type, &map.index, &map.pirq, 
>>> &msi);
>>> +        else
>>> +            ret = -EOPNOTSUPP;
>>>  
>>>          rcu_unlock_domain(d);
>>>  
>>> @@ -346,7 +350,11 @@ ret_t do_physdev_op(int cmd, 
>>> XEN_GUEST_HANDLE_PARAM(void) arg)
>>>          if ( !d )
>>>              break;
>>>  
>>> -        ret = physdev_unmap_pirq(d, unmap.pirq);
>>> +        /* Only unmapping when the subject domain has a notion of PIRQ */
>>> +        if ( !is_hvm_domain(d) || has_pirq(d) )
>>> +            ret = physdev_unmap_pirq(d, unmap.pirq);
>>> +        else
>>> +            ret = -EOPNOTSUPP;
>>>  
>>>          rcu_unlock_domain(d);
>>>  
>>
>> Gitlab is displeased with your offering.
>>
>> https://gitlab.com/xen-project/xen/-/pipelines/1393459622
>>
>> This breaks both {adl,zen3p}-pci-hvm-x86-64-gcc-debug, and given the:
>>
>> (XEN) [    8.150305] HVM restore d1: CPU 0
>> libxl: error: libxl_pci.c:1491:pci_add_dm_done: Domain
>> 1:xc_physdev_map_pirq irq=18 (error=-1): Not supported
>> libxl: error: libxl_pci.c:1809:device_pci_add_done: Domain
>> 1:libxl__device_pci_add failed for PCI device 0:3:0.0 (rc -3)
>> libxl: error: libxl_create.c:1962:domcreate_attach_devices: Domain
>> 1:unable to add pci devices
>> libxl: error: libxl_xshelp.c:206:libxl__xs_read_mandatory: xenstore read
>> failed: `/libxl/1/type': No such file or directory
>> libxl: warning: libxl_dom.c:49:libxl__domain_type: unable to get domain
>> type for domid=1, assuming HVM
>> libxl: error: libxl_domain.c:1616:domain_destroy_domid_cb: Domain
>> 1:xc_domain_destroy failed: No such process
> 
> Sorry to forget to validate the scenario of "hvm_pirq=0" for HVM guest since 
> V10->V11(remove the self-check "d == currd").
> 
> V10 version:
> +        /* Prevent self-map when currd has no X86_EMU_USE_PIRQ flag */
> +        if ( is_hvm_domain(d) && !has_pirq(d) && d == currd )
> +        {
> +            rcu_unlock_domain(d);
> +            return -EOPNOTSUPP;
> +        }
> 
> V11 version:
> +        /* Prevent mapping when the subject domain has no X86_EMU_USE_PIRQ */
> +        if ( is_hvm_domain(d) && !has_pirq(d) )
> +        {
> +            rcu_unlock_domain(d);
> +            return -EOPNOTSUPP;
> +        }
> 
> V10 is fine for when hvm_pirq is enable or disable. 
> This issue is from V11, the cause is that when pass "hvm_pirq=0" to HVM 
> guest, then has_pirq() is false, but it still uses the pirq to route the 
> interrupt of passthrough devices.
> So, it still does xc_physdev_(un)map_pirq, then fails at the has_pirq() check.
> 
> Hi Jan,
> Should I need to change to V10 to only prevent the self-mapping when the 
> subject domain has no PIRQ?
> So that it can allow PHYSDEVOP_map_pirq for foreign mapping, no matter the 
> dom0 or the domU has PIRQ or not?

No, my position there hasn't changed. I continue to view it as wrong to
have any d == currd checks here.

Jan

Reply via email to