Re: [PATCH v2 14/30] KVM: s390: pci: add basic kvm_zdev structure

Matthew Rosato
2022-01-18T18:39:59Z

On 1/18/22 12:32 PM, Pierre Morel wrote:
>
>
> On 1/17/22 17:25, Pierre Morel wrote:
>>
>>
>> On 1/14/22 21:31, Matthew Rosato wrote:
>>> This structure will be used to carry kvm passthrough information
>>> related to
>>> zPCI devices.
>>>
>>> Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
>>> ---
>>>   arch/s390/include/asm/kvm_pci.h | 29 +++++++++++++++++++++
>>>   arch/s390/include/asm/pci.h     |  3 +++
>>>   arch/s390/kvm/Makefile          |  2 +-
>>>   arch/s390/kvm/pci.c             | 46 +++++++++++++++++++++++++++++++++
>>>   4 files changed, 79 insertions(+), 1 deletion(-)
>>>   create mode 100644 arch/s390/include/asm/kvm_pci.h
>>>   create mode 100644 arch/s390/kvm/pci.c
>>>
>>> diff --git a/arch/s390/include/asm/kvm_pci.h
>>> b/arch/s390/include/asm/kvm_pci.h
>>> new file mode 100644
>>> index 000000000000..aafee2976929
>>> --- /dev/null
>>> +++ b/arch/s390/include/asm/kvm_pci.h
>>> @@ -0,0 +1,29 @@
>>> +/* SPDX-License-Identifier: GPL-2.0 */
>>> +/*
>>> + * KVM PCI Passthrough for virtual machines on s390
>>> + *
>>> + * Copyright IBM Corp. 2021
>>> + *
>>> + *    Author(s): Matthew Rosato <mjrosato@linux.ibm.com>
>>> + */
>>> +
>>> +
>>
>> One blank line too much.
>>
>> Otherwise, look good to me.
>>
>> Reviewed-by: Pierre Morel <pmorel@linux.ibm.com>
>>
>>> +#ifndef ASM_KVM_PCI_H
>>> +#define ASM_KVM_PCI_H
>>> +
>>> +#include <linux/types.h>
>>> +#include <linux/kvm_types.h>
>>> +#include <linux/kvm_host.h>
>>> +#include <linux/kvm.h>
>>> +#include <linux/pci.h>
>>> +
>>> +struct kvm_zdev {
>>> +    struct zpci_dev *zdev;
>>> +    struct kvm *kvm;
>>> +};
>>> +
>>> +int kvm_s390_pci_dev_open(struct zpci_dev *zdev);
>>> +void kvm_s390_pci_dev_release(struct zpci_dev *zdev);
>>> +void kvm_s390_pci_attach_kvm(struct zpci_dev *zdev, struct kvm *kvm);
>>> +
>>> +#endif /* ASM_KVM_PCI_H */
>>> diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h
>>> index f3cd2da8128c..9b6c657d8d31 100644
>>> --- a/arch/s390/include/asm/pci.h
>>> +++ b/arch/s390/include/asm/pci.h
>>> @@ -97,6 +97,7 @@ struct zpci_bar_struct {
>>>   };
>>>   struct s390_domain;
>>> +struct kvm_zdev;
>>>   #define ZPCI_FUNCTIONS_PER_BUS 256
>>>   struct zpci_bus {
>>> @@ -190,6 +191,8 @@ struct zpci_dev {
>>>       struct dentry    *debugfs_dev;
>>>       struct s390_domain *s390_domain; /* s390 IOMMU domain data */
>>> +
>>> +    struct kvm_zdev *kzdev; /* passthrough data */
>>>   };
>>>   static inline bool zdev_enabled(struct zpci_dev *zdev)
>>> diff --git a/arch/s390/kvm/Makefile b/arch/s390/kvm/Makefile
>>> index b3aaadc60ead..a26f4fe7b680 100644
>>> --- a/arch/s390/kvm/Makefile
>>> +++ b/arch/s390/kvm/Makefile
>>> @@ -11,5 +11,5 @@ ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
>>>   kvm-objs := $(common-objs) kvm-s390.o intercept.o interrupt.o
>>> priv.o sigp.o
>>>   kvm-objs += diag.o gaccess.o guestdbg.o vsie.o pv.o
>>> -
>>> +kvm-$(CONFIG_PCI) += pci.o

As discussed in other threads, I will look at changing this to
kvm-$(CONFIG_VFIO_PCI_ZDEV) += pci.o
Along with other IS_ENABLE(CONFIG_PCI) ->
IS_ENABLED(CONFIG_VFIO_PCI_ZDDEV) changes

>>>   obj-$(CONFIG_KVM) += kvm.o
>>> diff --git a/arch/s390/kvm/pci.c b/arch/s390/kvm/pci.c
>>> new file mode 100644
>>> index 000000000000..1c33bc7bf2bd
>>> --- /dev/null
>>> +++ b/arch/s390/kvm/pci.c
>>> @@ -0,0 +1,46 @@
>>> +// SPDX-License-Identifier: GPL-2.0
>>> +/*
>>> + * s390 kvm PCI passthrough support
>>> + *
>>> + * Copyright IBM Corp. 2021
>>> + *
>>> + *    Author(s): Matthew Rosato <mjrosato@linux.ibm.com>
>>> + */
>>> +
>>> +#include <linux/kvm_host.h>
>>> +#include <linux/pci.h>
>>> +#include <asm/kvm_pci.h>
>>> +
>>> +int kvm_s390_pci_dev_open(struct zpci_dev *zdev)
>>> +{
>>> +    struct kvm_zdev *kzdev;
>>> +
>>> +    kzdev = kzalloc(sizeof(struct kvm_zdev), GFP_KERNEL);
>>> +    if (!kzdev)
>>> +        return -ENOMEM;
>>> +
>>> +    kzdev->zdev = zdev;
>>> +    zdev->kzdev = kzdev;
>>> +
>>> +    return 0;
>>> +}
>>> +EXPORT_SYMBOL_GPL(kvm_s390_pci_dev_open);
>>> +
>>> +void kvm_s390_pci_dev_release(struct zpci_dev *zdev)
>>> +{
>>> +    struct kvm_zdev *kzdev;
>>> +
>>> +    kzdev = zdev->kzdev;
>>> +    WARN_ON(kzdev->zdev != zdev);
>>> +    zdev->kzdev = 0;
>>> +    kfree(kzdev);
>>> +}
>>> +EXPORT_SYMBOL_GPL(kvm_s390_pci_dev_release);
>>> +
>>> +void kvm_s390_pci_attach_kvm(struct zpci_dev *zdev, struct kvm *kvm)
>>> +{
>>> +    struct kvm_zdev *kzdev = zdev->kzdev;
>>> +
>>> +    kzdev->kvm = kvm;
>>> +}
>>> +EXPORT_SYMBOL_GPL(kvm_s390_pci_attach_kvm);
>>>
>>
>
> Working now on patch 24, I am not sure that this function is necessary.
> the only purpose seems to set kzdev->kvm = kvm while we already know
> kzdev in the caller.
>

Yep, as mentioned in the patch 24 thread I will drop this function and
set kzdev->kvm = kvm directly in patch 24.