Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1502472 > unrolled thread
| Started by | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| First post | 2016-10-17 23:30 +0200 |
| Last post | 2016-10-24 09:20 +0200 |
| Articles | 20 on this page of 52 — 4 participants |
Back to article view | Back to linux.kernel
[PATCH v9 00/12] Add Mediated device support Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
[PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 21:40 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-20 22:40 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 23:10 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-20 23:20 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 23:30 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-21 05:10 +0200
Re: [PATCH v9 10/12] vfio: Add function to get device_api string from vfio_device_info.flags Alex Williamson <alex.williamson@redhat.com> - 2016-10-21 05:30 +0200
[PATCH v9 02/12] vfio: VFIO based driver for Mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
RE: [PATCH v9 02/12] vfio: VFIO based driver for Mediated devices "Tian, Kevin" <kevin.tian@intel.com> - 2016-10-26 09:00 +0200
Re: [PATCH v9 02/12] vfio: VFIO based driver for Mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-26 17:10 +0200
[PATCH v9 08/12] vfio_pci: Updated to use vfio_set_irqs_validate_and_prepare() Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
[PATCH v9 11/12] docs: Add Documentation for Mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 11/12] docs: Add Documentation for Mediated devices Alex Williamson <alex.williamson@redhat.com> - 2016-10-25 18:20 +0200
[PATCH v9 09/12] vfio_platform: Updated to use vfio_set_irqs_validate_and_prepare() Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
[PATCH v9 04/12] vfio iommu: Add support for mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Alex Williamson <alex.williamson@redhat.com> - 2016-10-19 23:10 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-20 22:20 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Alex Williamson <alex.williamson@redhat.com> - 2016-10-24 04:40 +0200
RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices "Tian, Kevin" <kevin.tian@intel.com> - 2016-10-26 09:30 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-26 17:10 +0200
RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices "Tian, Kevin" <kevin.tian@intel.com> - 2016-10-26 10:00 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Alex Williamson <alex.williamson@redhat.com> - 2016-10-26 17:30 +0200
RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices "Tian, Kevin" <kevin.tian@intel.com> - 2016-10-26 10:00 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Alex Williamson <alex.williamson@redhat.com> - 2016-10-26 17:20 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Jike Song <jike.song@intel.com> - 2016-10-21 10:00 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Alex Williamson <alex.williamson@redhat.com> - 2016-10-21 16:40 +0200
Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-24 12:40 +0200
[PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 21:30 +0200
Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-24 23:30 +0200
Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() Alex Williamson <alex.williamson@redhat.com> - 2016-10-24 23:40 +0200
[PATCH v9 03/12] vfio: Rearrange functions to get vfio_group from dev Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 03/12] vfio: Rearrange functions to get vfio_group from dev Alex Williamson <alex.williamson@redhat.com> - 2016-10-19 19:30 +0200
[PATCH v9 05/12] vfio: Introduce common function to add capabilities Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 21:30 +0200
Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-24 23:30 +0200
Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities Alex Williamson <alex.williamson@redhat.com> - 2016-10-24 23:40 +0200
[PATCH v9 01/12] vfio: Mediated device Core driver Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-17 23:30 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Alex Williamson <alex.williamson@redhat.com> - 2016-10-19 01:20 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-19 21:20 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 00:30 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Jike Song <jike.song@intel.com> - 2016-10-20 09:30 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Alex Williamson <alex.williamson@redhat.com> - 2016-10-20 19:20 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Jike Song <jike.song@intel.com> - 2016-10-21 04:50 +0200
RE: [PATCH v9 01/12] vfio: Mediated device Core driver "Tian, Kevin" <kevin.tian@intel.com> - 2016-10-26 09:00 +0200
Re: [PATCH v9 01/12] vfio: Mediated device Core driver Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-26 17:00 +0200
Re: [PATCH v9 00/12] Add Mediated device support Alex Williamson <alex.williamson@redhat.com> - 2016-10-17 23:50 +0200
Re: [PATCH v9 12/12] docs: Sample driver to demonstrate how to use Mediated device framework. Alex Williamson <alex.williamson@redhat.com> - 2016-10-18 19:20 +0200
Re: [PATCH v9 12/12] docs: Sample driver to demonstrate how to use Mediated device framework. Kirti Wankhede <kwankhede@nvidia.com> - 2016-10-19 21:20 +0200
Re: [PATCH v9 00/12] Add Mediated device support Jike Song <jike.song@intel.com> - 2016-10-24 09:20 +0200
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | "Tian, Kevin" <kevin.tian@intel.com> |
|---|---|
| Date | 2016-10-26 09:30 +0200 |
| Subject | RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <swqOS-pQ-3@gated-at.bofh.it> |
| In reply to | #1506832 |
> From: Alex Williamson [mailto:alex.williamson@redhat.com]
> Sent: Monday, October 24, 2016 10:32 AM
>
> > >> -static long vfio_unpin_pages(unsigned long pfn, long npage,
> > >> - int prot, bool do_accounting)
> > >> +static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
> > >> + unsigned long pfn, long npage, int prot,
> > >> + bool do_accounting)
> > >
> > > Have you noticed that it's kind of confusing that
> > > __vfio_{un}pin_pages_remote() uses current, which does a
> > > get_user_pages_fast() while "local" uses a provided task_struct and
> > > uses get_user_pages_*remote*()? And also what was effectively local
> > > (ie. we're pinning for our own use here) is now "remote" and pinning
> > > for a remote, vendor driver consumer, is now "local". It's not very
> > > intuitive.
> > >
I questioned this confusing naming in v8 too...
> >
> > 'local' in local_domain was suggested to describe the domain for local
> > page tracking. Earlier suggestions to have 'mdev' or 'noimmu' in this
> > name were discarded. May be we should revisit what the name should be.
> > Any suggestion?
> >
> > For local_domain, to pin pages, flow is:
> >
> > for local_domain
> > |- vfio_pin_pages()
> > |- vfio_iommu_type1_pin_pages()
> > |- __vfio_pin_page_local()
> > |- vaddr_get_pfn(task->mm)
> > |- get_user_pages_remote()
> >
> > __vfio_pin_page_local() --> get_user_pages_remote()
>
>
> In vfio.c we have the concept of an external user, perhaps that could
> be continued here. An mdev driver would be an external, or remote
> pinning.
>
I prefer to use remote here. It's aligned with underlying mm operations
Thanks
Kevin
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-26 17:10 +0200 |
| Subject | Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <swy02-5vO-25@gated-at.bofh.it> |
| In reply to | #1508869 |
On 10/26/2016 12:49 PM, Tian, Kevin wrote:
>> From: Alex Williamson [mailto:alex.williamson@redhat.com]
>> Sent: Monday, October 24, 2016 10:32 AM
>>
>>>>> -static long vfio_unpin_pages(unsigned long pfn, long npage,
>>>>> - int prot, bool do_accounting)
>>>>> +static long __vfio_unpin_pages_remote(struct vfio_iommu *iommu,
>>>>> + unsigned long pfn, long npage, int prot,
>>>>> + bool do_accounting)
>>>>
>>>> Have you noticed that it's kind of confusing that
>>>> __vfio_{un}pin_pages_remote() uses current, which does a
>>>> get_user_pages_fast() while "local" uses a provided task_struct and
>>>> uses get_user_pages_*remote*()? And also what was effectively local
>>>> (ie. we're pinning for our own use here) is now "remote" and pinning
>>>> for a remote, vendor driver consumer, is now "local". It's not very
>>>> intuitive.
>>>>
>
> I questioned this confusing naming in v8 too...
>
I do tried to address your concerns on v8.
>>>
>>> 'local' in local_domain was suggested to describe the domain for local
>>> page tracking. Earlier suggestions to have 'mdev' or 'noimmu' in this
>>> name were discarded. May be we should revisit what the name should be.
>>> Any suggestion?
>>>
>>> For local_domain, to pin pages, flow is:
>>>
>>> for local_domain
>>> |- vfio_pin_pages()
>>> |- vfio_iommu_type1_pin_pages()
>>> |- __vfio_pin_page_local()
>>> |- vaddr_get_pfn(task->mm)
>>> |- get_user_pages_remote()
>>>
>>> __vfio_pin_page_local() --> get_user_pages_remote()
>>
>>
>> In vfio.c we have the concept of an external user, perhaps that could
>> be continued here. An mdev driver would be an external, or remote
>> pinning.
>>
>
> I prefer to use remote here. It's aligned with underlying mm operations
>
Using 'remote' in this case is also confusing since it is already used
in this file. I liked Alex's suggestion to use external and I'll have
those changed in next version of patch set.
Kirti
[toc] | [prev] | [next] | [standalone]
| From | "Tian, Kevin" <kevin.tian@intel.com> |
|---|---|
| Date | 2016-10-26 10:00 +0200 |
| Subject | RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <swrhT-A6-5@gated-at.bofh.it> |
| In reply to | #1504345 |
> From: Tian, Kevin
> Sent: Wednesday, October 26, 2016 3:54 PM
>
> > From: Alex Williamson [mailto:alex.williamson@redhat.com]
> > Sent: Thursday, October 20, 2016 5:03 AM
> > > @@ -83,6 +92,21 @@ struct vfio_group {
> > > };
> > >
> > > /*
> > > + * Guest RAM pinning working set or DMA target
> > > + */
> > > +struct vfio_pfn {
> > > + struct rb_node node;
> > > + unsigned long vaddr; /* virtual addr */
> > > + dma_addr_t iova; /* IOVA */
> > > + unsigned long pfn; /* Host pfn */
> > > + int prot;
> > > + atomic_t ref_count;
> > > +};
> >
> > Somehow we're going to need to fit an invalidation callback here too.
> > How would we handle a case where there are multiple mdev devices, from
> > different vendor drivers, that all have the same pfn pinned? I'm
> > already concerned about the per pfn overhead we're introducing here so
> > clearly we cannot store an invalidation callback per pinned page, per
> > vendor driver. Perhaps invalidations should be done using a notifier
> > chain per vfio_iommu, the vendor drivers are required to register on
> > that chain (fail pinning with empty notifier list) user unmapping
> > will be broadcast to the notifier chain, the vendor driver will be
> > responsible for deciding if each unmap is relevant to them (potentially
> > it's for a pinning from another driver).
> >
> > I expect we also need to enforce that vendors perform a synchronous
> > unmap such that after returning from the notifier list call, the
> > vfio_pfn should no longer exist. If it does we might need to BUG_ON.
> > Also be careful to pay attention to the locking of the notifier vs
> > unpin callbacks to avoid deadlocks.
> >
>
> What about just requesting vendor driver to provide a callback in parent
> device ops?
>
> Curious in which scenario the user application (say Qemu here) may
> unmap memory pages which are still pinned by vendor driver... Is it
> purely about a corner case which we want to handle elegantly?
>
> If yes, possibly a simpler way is to force destroying mdev instead of
> asking vendor driver to take care of each invalidation request under
> such situation. Since anyway the mdev device won't be in an usable
> state anymore... (sorry if I missed the key problem here.)
>
or calling reset callback of parent device driver, if we don't want to
break libvirt's expectation by blindly removing mdev device...
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-26 17:30 +0200 |
| Subject | Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <swyjo-5Fl-5@gated-at.bofh.it> |
| In reply to | #1508891 |
On Wed, 26 Oct 2016 07:54:56 +0000
"Tian, Kevin" <kevin.tian@intel.com> wrote:
> > From: Tian, Kevin
> > Sent: Wednesday, October 26, 2016 3:54 PM
> >
> > > From: Alex Williamson [mailto:alex.williamson@redhat.com]
> > > Sent: Thursday, October 20, 2016 5:03 AM
> > > > @@ -83,6 +92,21 @@ struct vfio_group {
> > > > };
> > > >
> > > > /*
> > > > + * Guest RAM pinning working set or DMA target
> > > > + */
> > > > +struct vfio_pfn {
> > > > + struct rb_node node;
> > > > + unsigned long vaddr; /* virtual addr */
> > > > + dma_addr_t iova; /* IOVA */
> > > > + unsigned long pfn; /* Host pfn */
> > > > + int prot;
> > > > + atomic_t ref_count;
> > > > +};
> > >
> > > Somehow we're going to need to fit an invalidation callback here too.
> > > How would we handle a case where there are multiple mdev devices, from
> > > different vendor drivers, that all have the same pfn pinned? I'm
> > > already concerned about the per pfn overhead we're introducing here so
> > > clearly we cannot store an invalidation callback per pinned page, per
> > > vendor driver. Perhaps invalidations should be done using a notifier
> > > chain per vfio_iommu, the vendor drivers are required to register on
> > > that chain (fail pinning with empty notifier list) user unmapping
> > > will be broadcast to the notifier chain, the vendor driver will be
> > > responsible for deciding if each unmap is relevant to them (potentially
> > > it's for a pinning from another driver).
> > >
> > > I expect we also need to enforce that vendors perform a synchronous
> > > unmap such that after returning from the notifier list call, the
> > > vfio_pfn should no longer exist. If it does we might need to BUG_ON.
> > > Also be careful to pay attention to the locking of the notifier vs
> > > unpin callbacks to avoid deadlocks.
> > >
> >
> > What about just requesting vendor driver to provide a callback in parent
> > device ops?
> >
> > Curious in which scenario the user application (say Qemu here) may
> > unmap memory pages which are still pinned by vendor driver... Is it
> > purely about a corner case which we want to handle elegantly?
> >
> > If yes, possibly a simpler way is to force destroying mdev instead of
> > asking vendor driver to take care of each invalidation request under
> > such situation. Since anyway the mdev device won't be in an usable
> > state anymore... (sorry if I missed the key problem here.)
> >
>
> or calling reset callback of parent device driver, if we don't want to
> break libvirt's expectation by blindly removing mdev device...
I think we're going off into the weeds here. mdev devices need to
honor the existing API, therefore an unmap should result in preventing
the device from further access to the unmapped pages, nothing more,
nothing less. Thanks,
Alex
[toc] | [prev] | [next] | [standalone]
| From | "Tian, Kevin" <kevin.tian@intel.com> |
|---|---|
| Date | 2016-10-26 10:00 +0200 |
| Subject | RE: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <swrhT-A6-13@gated-at.bofh.it> |
| In reply to | #1504345 |
> From: Alex Williamson [mailto:alex.williamson@redhat.com]
> Sent: Thursday, October 20, 2016 5:03 AM
> > @@ -83,6 +92,21 @@ struct vfio_group {
> > };
> >
> > /*
> > + * Guest RAM pinning working set or DMA target
> > + */
> > +struct vfio_pfn {
> > + struct rb_node node;
> > + unsigned long vaddr; /* virtual addr */
> > + dma_addr_t iova; /* IOVA */
> > + unsigned long pfn; /* Host pfn */
> > + int prot;
> > + atomic_t ref_count;
> > +};
>
> Somehow we're going to need to fit an invalidation callback here too.
> How would we handle a case where there are multiple mdev devices, from
> different vendor drivers, that all have the same pfn pinned? I'm
> already concerned about the per pfn overhead we're introducing here so
> clearly we cannot store an invalidation callback per pinned page, per
> vendor driver. Perhaps invalidations should be done using a notifier
> chain per vfio_iommu, the vendor drivers are required to register on
> that chain (fail pinning with empty notifier list) user unmapping
> will be broadcast to the notifier chain, the vendor driver will be
> responsible for deciding if each unmap is relevant to them (potentially
> it's for a pinning from another driver).
>
> I expect we also need to enforce that vendors perform a synchronous
> unmap such that after returning from the notifier list call, the
> vfio_pfn should no longer exist. If it does we might need to BUG_ON.
> Also be careful to pay attention to the locking of the notifier vs
> unpin callbacks to avoid deadlocks.
>
What about just requesting vendor driver to provide a callback in parent
device ops?
Curious in which scenario the user application (say Qemu here) may
unmap memory pages which are still pinned by vendor driver... Is it
purely about a corner case which we want to handle elegantly?
If yes, possibly a simpler way is to force destroying mdev instead of
asking vendor driver to take care of each invalidation request under
such situation. Since anyway the mdev device won't be in an usable
state anymore... (sorry if I missed the key problem here.)
Thanks
Kevin
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-26 17:20 +0200 |
| Subject | Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <swy9I-5BD-25@gated-at.bofh.it> |
| In reply to | #1508893 |
On Wed, 26 Oct 2016 07:53:43 +0000
"Tian, Kevin" <kevin.tian@intel.com> wrote:
> > From: Alex Williamson [mailto:alex.williamson@redhat.com]
> > Sent: Thursday, October 20, 2016 5:03 AM
> > > @@ -83,6 +92,21 @@ struct vfio_group {
> > > };
> > >
> > > /*
> > > + * Guest RAM pinning working set or DMA target
> > > + */
> > > +struct vfio_pfn {
> > > + struct rb_node node;
> > > + unsigned long vaddr; /* virtual addr */
> > > + dma_addr_t iova; /* IOVA */
> > > + unsigned long pfn; /* Host pfn */
> > > + int prot;
> > > + atomic_t ref_count;
> > > +};
> >
> > Somehow we're going to need to fit an invalidation callback here too.
> > How would we handle a case where there are multiple mdev devices, from
> > different vendor drivers, that all have the same pfn pinned? I'm
> > already concerned about the per pfn overhead we're introducing here so
> > clearly we cannot store an invalidation callback per pinned page, per
> > vendor driver. Perhaps invalidations should be done using a notifier
> > chain per vfio_iommu, the vendor drivers are required to register on
> > that chain (fail pinning with empty notifier list) user unmapping
> > will be broadcast to the notifier chain, the vendor driver will be
> > responsible for deciding if each unmap is relevant to them (potentially
> > it's for a pinning from another driver).
> >
> > I expect we also need to enforce that vendors perform a synchronous
> > unmap such that after returning from the notifier list call, the
> > vfio_pfn should no longer exist. If it does we might need to BUG_ON.
> > Also be careful to pay attention to the locking of the notifier vs
> > unpin callbacks to avoid deadlocks.
> >
>
> What about just requesting vendor driver to provide a callback in parent
> device ops?
How does the iommu driver get to the mdev vendor driver callback? We
can also have pages pinned by multiple vendor drivers, I don't think
we want the additional overhead of a per page list of invalidation
callbacks.
> Curious in which scenario the user application (say Qemu here) may
> unmap memory pages which are still pinned by vendor driver... Is it
> purely about a corner case which we want to handle elegantly?
The vfio type1 iommu API provides a MAP and UNMAP interface. The unmap
call is expected to work regardless of how it might inhibit the device
from working. This is currently true of iommu protected devices today,
a user can unmap pages which might be DMA targets for the device and
the iommu prevents further access to those pages, possibly at the
expense of device operation. We cannot support an interface where a
user can unmap a set of pages and map in new pages to replace them when
the vendor driver might be caching stale mappings.
In normal VM operation perhaps this is a corner case, but the API is
not defined only for the normal and expected behavior of a VM.
> If yes, possibly a simpler way is to force destroying mdev instead of
> asking vendor driver to take care of each invalidation request under
> such situation. Since anyway the mdev device won't be in an usable
> state anymore... (sorry if I missed the key problem here.)
That's a pretty harsh response for an operation which is completely
valid from an API perspective. What if the VM does an unmap of all
memory around reset? We cannot guarantee that the guest driver will
have a chance to do cleanup, the guest may have crashed or a
system_reset invoked. Would you have the mdev destroyed in this case?
How could QEMU, which has no device specific driver to know that vendor
pinnings are present, recover from this? Thanks,
Alex
[toc] | [prev] | [next] | [standalone]
| From | Jike Song <jike.song@intel.com> |
|---|---|
| Date | 2016-10-21 10:00 +0200 |
| Subject | Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <suCU9-2xD-5@gated-at.bofh.it> |
| In reply to | #1502479 |
On 10/18/2016 05:22 AM, Kirti Wankhede wrote:
> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> index 2ba19424e4a1..5d67058a611d 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
[snip]
> static int vfio_iommu_type1_attach_group(void *iommu_data,
> struct iommu_group *iommu_group)
> {
> struct vfio_iommu *iommu = iommu_data;
> - struct vfio_group *group, *g;
> + struct vfio_group *group;
> struct vfio_domain *domain, *d;
> struct bus_type *bus = NULL;
> int ret;
> @@ -746,10 +1136,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
> mutex_lock(&iommu->lock);
>
> list_for_each_entry(d, &iommu->domain_list, next) {
> - list_for_each_entry(g, &d->group_list, next) {
> - if (g->iommu_group != iommu_group)
> - continue;
> + if (find_iommu_group(d, iommu_group)) {
> + mutex_unlock(&iommu->lock);
> + return -EINVAL;
> + }
> + }
>
> + if (iommu->local_domain) {
> + if (find_iommu_group(iommu->local_domain, iommu_group)) {
> mutex_unlock(&iommu->lock);
> return -EINVAL;
> }
> @@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
> if (ret)
> goto out_free;
>
> + if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
> + (bus == &mdev_bus_type)) {
Hi Kirti,
By refering mdev_bus_type directly you are making vfio_iommu_type1.ko depends
on mdev.ko, but in Kconfig doesn't guarantee the dependency. For example,
if CONFIG_VFIO_IOMMU_TYPE1=y and CONFIG_VFIO_MDEV=m, the building will fail.
--
Thanks,
Jike
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-21 16:40 +0200 |
| Subject | Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <suJ9g-6OP-5@gated-at.bofh.it> |
| In reply to | #1505541 |
On Fri, 21 Oct 2016 15:49:07 +0800
Jike Song <jike.song@intel.com> wrote:
> On 10/18/2016 05:22 AM, Kirti Wankhede wrote:
> > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
> > index 2ba19424e4a1..5d67058a611d 100644
> > --- a/drivers/vfio/vfio_iommu_type1.c
> > +++ b/drivers/vfio/vfio_iommu_type1.c
> [snip]
> > static int vfio_iommu_type1_attach_group(void *iommu_data,
> > struct iommu_group *iommu_group)
> > {
> > struct vfio_iommu *iommu = iommu_data;
> > - struct vfio_group *group, *g;
> > + struct vfio_group *group;
> > struct vfio_domain *domain, *d;
> > struct bus_type *bus = NULL;
> > int ret;
> > @@ -746,10 +1136,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
> > mutex_lock(&iommu->lock);
> >
> > list_for_each_entry(d, &iommu->domain_list, next) {
> > - list_for_each_entry(g, &d->group_list, next) {
> > - if (g->iommu_group != iommu_group)
> > - continue;
> > + if (find_iommu_group(d, iommu_group)) {
> > + mutex_unlock(&iommu->lock);
> > + return -EINVAL;
> > + }
> > + }
> >
> > + if (iommu->local_domain) {
> > + if (find_iommu_group(iommu->local_domain, iommu_group)) {
> > mutex_unlock(&iommu->lock);
> > return -EINVAL;
> > }
> > @@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
> > if (ret)
> > goto out_free;
> >
> > + if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
> > + (bus == &mdev_bus_type)) {
>
> Hi Kirti,
>
> By refering mdev_bus_type directly you are making vfio_iommu_type1.ko depends
> on mdev.ko, but in Kconfig doesn't guarantee the dependency. For example,
> if CONFIG_VFIO_IOMMU_TYPE1=y and CONFIG_VFIO_MDEV=m, the building will fail.
Good point, Jike. I don't think we want to make existing vfio modules
dependent on mdev modules. I wonder if we can lookup the mdev_bus_type
symbol w/o triggering the module load. Thanks,
Alex
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-24 12:40 +0200 |
| Subject | Re: [PATCH v9 04/12] vfio iommu: Add support for mediated devices |
| Message-ID | <svKPD-6q5-1@gated-at.bofh.it> |
| In reply to | #1506004 |
On 10/21/2016 8:06 PM, Alex Williamson wrote:
> On Fri, 21 Oct 2016 15:49:07 +0800
> Jike Song <jike.song@intel.com> wrote:
>
>> On 10/18/2016 05:22 AM, Kirti Wankhede wrote:
>>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
>>> index 2ba19424e4a1..5d67058a611d 100644
>>> --- a/drivers/vfio/vfio_iommu_type1.c
>>> +++ b/drivers/vfio/vfio_iommu_type1.c
>> [snip]
>>> static int vfio_iommu_type1_attach_group(void *iommu_data,
>>> struct iommu_group *iommu_group)
>>> {
>>> struct vfio_iommu *iommu = iommu_data;
>>> - struct vfio_group *group, *g;
>>> + struct vfio_group *group;
>>> struct vfio_domain *domain, *d;
>>> struct bus_type *bus = NULL;
>>> int ret;
>>> @@ -746,10 +1136,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
>>> mutex_lock(&iommu->lock);
>>>
>>> list_for_each_entry(d, &iommu->domain_list, next) {
>>> - list_for_each_entry(g, &d->group_list, next) {
>>> - if (g->iommu_group != iommu_group)
>>> - continue;
>>> + if (find_iommu_group(d, iommu_group)) {
>>> + mutex_unlock(&iommu->lock);
>>> + return -EINVAL;
>>> + }
>>> + }
>>>
>>> + if (iommu->local_domain) {
>>> + if (find_iommu_group(iommu->local_domain, iommu_group)) {
>>> mutex_unlock(&iommu->lock);
>>> return -EINVAL;
>>> }
>>> @@ -769,6 +1163,30 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
>>> if (ret)
>>> goto out_free;
>>>
>>> + if (IS_ENABLED(CONFIG_VFIO_MDEV) && !iommu_present(bus) &&
>>> + (bus == &mdev_bus_type)) {
>>
>> Hi Kirti,
>>
>> By refering mdev_bus_type directly you are making vfio_iommu_type1.ko depends
>> on mdev.ko, but in Kconfig doesn't guarantee the dependency. For example,
>> if CONFIG_VFIO_IOMMU_TYPE1=y and CONFIG_VFIO_MDEV=m, the building will fail.
>
> Good point, Jike. I don't think we want to make existing vfio modules
> dependent on mdev modules. I wonder if we can lookup the mdev_bus_type
> symbol w/o triggering the module load. Thanks,
>
Ok. Modifying the check as below works in above case:
mdev_bus = symbol_get(mdev_bus_type);
if (mdev_bus && (bus == mdev_bus) && !iommu_present(bus) ) {
symbol_put(mdev_bus_type);
...
}
Kirti
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-17 23:30 +0200 |
| Subject | [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() |
| Message-ID | <stnDQ-JF-31@gated-at.bofh.it> |
| In reply to | #1502472 |
Update msix_sparse_mmap_cap() to use vfio_info_add_capability()
Update region type capability to use vfio_info_add_capability()
Can't split this commit for MSIx and region_type cap since there is a
common code which need to be updated for both the cases.
Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Change-Id: I52bb28c7875a6da5a79ddad1843e6088aff58a45
---
drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
1 file changed, 27 insertions(+), 45 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index d624a527777f..1ec0565b48ea 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -556,12 +556,12 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
}
static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
+ struct vfio_region_info *info,
struct vfio_info_cap *caps)
{
- struct vfio_info_cap_header *header;
struct vfio_region_info_cap_sparse_mmap *sparse;
size_t end, size;
- int nr_areas = 2, i = 0;
+ int nr_areas = 2, i = 0, ret;
end = pci_resource_len(vdev->pdev, vdev->msix_bar);
@@ -572,13 +572,10 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
- header = vfio_info_cap_add(caps, size,
- VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
- if (IS_ERR(header))
- return PTR_ERR(header);
+ sparse = kzalloc(size, GFP_KERNEL);
+ if (!sparse)
+ return -ENOMEM;
- sparse = container_of(header,
- struct vfio_region_info_cap_sparse_mmap, header);
sparse->nr_areas = nr_areas;
if (vdev->msix_offset & PAGE_MASK) {
@@ -594,26 +591,11 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
i++;
}
- return 0;
-}
-
-static int region_type_cap(struct vfio_pci_device *vdev,
- struct vfio_info_cap *caps,
- unsigned int type, unsigned int subtype)
-{
- struct vfio_info_cap_header *header;
- struct vfio_region_info_cap_type *cap;
-
- header = vfio_info_cap_add(caps, sizeof(*cap),
- VFIO_REGION_INFO_CAP_TYPE, 1);
- if (IS_ERR(header))
- return PTR_ERR(header);
+ ret = vfio_info_add_capability(info, caps,
+ VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
+ kfree(sparse);
- cap = container_of(header, struct vfio_region_info_cap_type, header);
- cap->type = type;
- cap->subtype = subtype;
-
- return 0;
+ return ret;
}
int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
@@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
if (vdev->bar_mmap_supported[info.index]) {
info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
if (info.index == vdev->msix_bar) {
- ret = msix_sparse_mmap_cap(vdev, &caps);
+ ret = msix_sparse_mmap_cap(vdev, &info,
+ &caps);
if (ret)
return ret;
}
@@ -752,6 +735,9 @@ static long vfio_pci_ioctl(void *device_data,
break;
default:
+ {
+ struct vfio_region_info_cap_type cap_type;
+
if (info.index >=
VFIO_PCI_NUM_REGIONS + vdev->num_regions)
return -EINVAL;
@@ -762,27 +748,23 @@ static long vfio_pci_ioctl(void *device_data,
info.size = vdev->region[i].size;
info.flags = vdev->region[i].flags;
- ret = region_type_cap(vdev, &caps,
- vdev->region[i].type,
- vdev->region[i].subtype);
+ cap_type.type = vdev->region[i].type;
+ cap_type.subtype = vdev->region[i].subtype;
+
+ ret = vfio_info_add_capability(&info, &caps,
+ VFIO_REGION_INFO_CAP_TYPE,
+ &cap_type);
if (ret)
return ret;
+
+ }
}
- if (caps.size) {
- info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
- if (info.argsz < sizeof(info) + caps.size) {
- info.argsz = sizeof(info) + caps.size;
- info.cap_offset = 0;
- } else {
- vfio_info_cap_shift(&caps, sizeof(info));
- if (copy_to_user((void __user *)arg +
- sizeof(info), caps.buf,
- caps.size)) {
- kfree(caps.buf);
- return -EFAULT;
- }
- info.cap_offset = sizeof(info);
+ if (info.cap_offset) {
+ if (copy_to_user((void __user *)arg + info.cap_offset,
+ caps.buf, caps.size)) {
+ kfree(caps.buf);
+ return -EFAULT;
}
kfree(caps.buf);
--
2.7.0
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-20 21:30 +0200 |
| Subject | Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() |
| Message-ID | <surcm-3mp-17@gated-at.bofh.it> |
| In reply to | #1502480 |
On Tue, 18 Oct 2016 02:52:06 +0530
Kirti Wankhede <kwankhede@nvidia.com> wrote:
> Update msix_sparse_mmap_cap() to use vfio_info_add_capability()
> Update region type capability to use vfio_info_add_capability()
> Can't split this commit for MSIx and region_type cap since there is a
> common code which need to be updated for both the cases.
>
> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
> Signed-off-by: Neo Jia <cjia@nvidia.com>
> Change-Id: I52bb28c7875a6da5a79ddad1843e6088aff58a45
> ---
> drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
> 1 file changed, 27 insertions(+), 45 deletions(-)
>
> diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
> index d624a527777f..1ec0565b48ea 100644
> --- a/drivers/vfio/pci/vfio_pci.c
> +++ b/drivers/vfio/pci/vfio_pci.c
> @@ -556,12 +556,12 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
> }
>
> static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
> + struct vfio_region_info *info,
> struct vfio_info_cap *caps)
> {
> - struct vfio_info_cap_header *header;
> struct vfio_region_info_cap_sparse_mmap *sparse;
> size_t end, size;
> - int nr_areas = 2, i = 0;
> + int nr_areas = 2, i = 0, ret;
>
> end = pci_resource_len(vdev->pdev, vdev->msix_bar);
>
> @@ -572,13 +572,10 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
>
> size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
>
> - header = vfio_info_cap_add(caps, size,
> - VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
> - if (IS_ERR(header))
> - return PTR_ERR(header);
> + sparse = kzalloc(size, GFP_KERNEL);
> + if (!sparse)
> + return -ENOMEM;
>
> - sparse = container_of(header,
> - struct vfio_region_info_cap_sparse_mmap, header);
> sparse->nr_areas = nr_areas;
>
> if (vdev->msix_offset & PAGE_MASK) {
> @@ -594,26 +591,11 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
> i++;
> }
>
> - return 0;
> -}
> -
> -static int region_type_cap(struct vfio_pci_device *vdev,
> - struct vfio_info_cap *caps,
> - unsigned int type, unsigned int subtype)
> -{
> - struct vfio_info_cap_header *header;
> - struct vfio_region_info_cap_type *cap;
> -
> - header = vfio_info_cap_add(caps, sizeof(*cap),
> - VFIO_REGION_INFO_CAP_TYPE, 1);
> - if (IS_ERR(header))
> - return PTR_ERR(header);
> + ret = vfio_info_add_capability(info, caps,
> + VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
> + kfree(sparse);
>
> - cap = container_of(header, struct vfio_region_info_cap_type, header);
> - cap->type = type;
> - cap->subtype = subtype;
> -
> - return 0;
> + return ret;
> }
>
> int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
> @@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
> if (vdev->bar_mmap_supported[info.index]) {
> info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
> if (info.index == vdev->msix_bar) {
> - ret = msix_sparse_mmap_cap(vdev, &caps);
> + ret = msix_sparse_mmap_cap(vdev, &info,
> + &caps);
> if (ret)
> return ret;
> }
> @@ -752,6 +735,9 @@ static long vfio_pci_ioctl(void *device_data,
>
> break;
> default:
> + {
> + struct vfio_region_info_cap_type cap_type;
> +
> if (info.index >=
> VFIO_PCI_NUM_REGIONS + vdev->num_regions)
> return -EINVAL;
> @@ -762,27 +748,23 @@ static long vfio_pci_ioctl(void *device_data,
> info.size = vdev->region[i].size;
> info.flags = vdev->region[i].flags;
>
> - ret = region_type_cap(vdev, &caps,
> - vdev->region[i].type,
> - vdev->region[i].subtype);
> + cap_type.type = vdev->region[i].type;
> + cap_type.subtype = vdev->region[i].subtype;
> +
> + ret = vfio_info_add_capability(&info, &caps,
> + VFIO_REGION_INFO_CAP_TYPE,
> + &cap_type);
> if (ret)
> return ret;
> +
> + }
> }
>
> - if (caps.size) {
> - info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
> - if (info.argsz < sizeof(info) + caps.size) {
> - info.argsz = sizeof(info) + caps.size;
> - info.cap_offset = 0;
> - } else {
> - vfio_info_cap_shift(&caps, sizeof(info));
> - if (copy_to_user((void __user *)arg +
> - sizeof(info), caps.buf,
> - caps.size)) {
> - kfree(caps.buf);
> - return -EFAULT;
> - }
> - info.cap_offset = sizeof(info);
I prefer the case above, I'm fine with breaking out helpers to build a
buffer containing the capability chain, but I would rather have the
caller manage placing that back into the return structure. That also
allows the helper to be independent of the structure we're operating
on, it could be a region_info, irq_info, device_info, etc. It only
needs to know the layout of the capability type we're trying to add,
not the info structure itself. Thanks,
Alex
> + if (info.cap_offset) {
> + if (copy_to_user((void __user *)arg + info.cap_offset,
> + caps.buf, caps.size)) {
> + kfree(caps.buf);
> + return -EFAULT;
> }
>
> kfree(caps.buf);
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-24 23:30 +0200 |
| Subject | Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() |
| Message-ID | <svUYF-4HF-15@gated-at.bofh.it> |
| In reply to | #1505201 |
On 10/21/2016 12:54 AM, Alex Williamson wrote:
> On Tue, 18 Oct 2016 02:52:06 +0530
> Kirti Wankhede <kwankhede@nvidia.com> wrote:
>
>> Update msix_sparse_mmap_cap() to use vfio_info_add_capability()
>> Update region type capability to use vfio_info_add_capability()
>> Can't split this commit for MSIx and region_type cap since there is a
>> common code which need to be updated for both the cases.
>>
>> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
>> Signed-off-by: Neo Jia <cjia@nvidia.com>
>> Change-Id: I52bb28c7875a6da5a79ddad1843e6088aff58a45
>> ---
>> drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
>> 1 file changed, 27 insertions(+), 45 deletions(-)
>>
>> diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
>> index d624a527777f..1ec0565b48ea 100644
>> --- a/drivers/vfio/pci/vfio_pci.c
>> +++ b/drivers/vfio/pci/vfio_pci.c
>> @@ -556,12 +556,12 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
>> }
>>
>> static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
>> + struct vfio_region_info *info,
>> struct vfio_info_cap *caps)
>> {
>> - struct vfio_info_cap_header *header;
>> struct vfio_region_info_cap_sparse_mmap *sparse;
>> size_t end, size;
>> - int nr_areas = 2, i = 0;
>> + int nr_areas = 2, i = 0, ret;
>>
>> end = pci_resource_len(vdev->pdev, vdev->msix_bar);
>>
>> @@ -572,13 +572,10 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
>>
>> size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
>>
>> - header = vfio_info_cap_add(caps, size,
>> - VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
>> - if (IS_ERR(header))
>> - return PTR_ERR(header);
>> + sparse = kzalloc(size, GFP_KERNEL);
>> + if (!sparse)
>> + return -ENOMEM;
>>
>> - sparse = container_of(header,
>> - struct vfio_region_info_cap_sparse_mmap, header);
>> sparse->nr_areas = nr_areas;
>>
>> if (vdev->msix_offset & PAGE_MASK) {
>> @@ -594,26 +591,11 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
>> i++;
>> }
>>
>> - return 0;
>> -}
>> -
>> -static int region_type_cap(struct vfio_pci_device *vdev,
>> - struct vfio_info_cap *caps,
>> - unsigned int type, unsigned int subtype)
>> -{
>> - struct vfio_info_cap_header *header;
>> - struct vfio_region_info_cap_type *cap;
>> -
>> - header = vfio_info_cap_add(caps, sizeof(*cap),
>> - VFIO_REGION_INFO_CAP_TYPE, 1);
>> - if (IS_ERR(header))
>> - return PTR_ERR(header);
>> + ret = vfio_info_add_capability(info, caps,
>> + VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
>> + kfree(sparse);
>>
>> - cap = container_of(header, struct vfio_region_info_cap_type, header);
>> - cap->type = type;
>> - cap->subtype = subtype;
>> -
>> - return 0;
>> + return ret;
>> }
>>
>> int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
>> @@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
>> if (vdev->bar_mmap_supported[info.index]) {
>> info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
>> if (info.index == vdev->msix_bar) {
>> - ret = msix_sparse_mmap_cap(vdev, &caps);
>> + ret = msix_sparse_mmap_cap(vdev, &info,
>> + &caps);
>> if (ret)
>> return ret;
>> }
>> @@ -752,6 +735,9 @@ static long vfio_pci_ioctl(void *device_data,
>>
>> break;
>> default:
>> + {
>> + struct vfio_region_info_cap_type cap_type;
>> +
>> if (info.index >=
>> VFIO_PCI_NUM_REGIONS + vdev->num_regions)
>> return -EINVAL;
>> @@ -762,27 +748,23 @@ static long vfio_pci_ioctl(void *device_data,
>> info.size = vdev->region[i].size;
>> info.flags = vdev->region[i].flags;
>>
>> - ret = region_type_cap(vdev, &caps,
>> - vdev->region[i].type,
>> - vdev->region[i].subtype);
>> + cap_type.type = vdev->region[i].type;
>> + cap_type.subtype = vdev->region[i].subtype;
>> +
>> + ret = vfio_info_add_capability(&info, &caps,
>> + VFIO_REGION_INFO_CAP_TYPE,
>> + &cap_type);
>> if (ret)
>> return ret;
>> +
>> + }
>> }
>>
>> - if (caps.size) {
>> - info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
>> - if (info.argsz < sizeof(info) + caps.size) {
>> - info.argsz = sizeof(info) + caps.size;
>> - info.cap_offset = 0;
>> - } else {
>> - vfio_info_cap_shift(&caps, sizeof(info));
>> - if (copy_to_user((void __user *)arg +
>> - sizeof(info), caps.buf,
>> - caps.size)) {
>> - kfree(caps.buf);
>> - return -EFAULT;
>> - }
>> - info.cap_offset = sizeof(info);
>
> I prefer the case above, I'm fine with breaking out helpers to build a
> buffer containing the capability chain, but I would rather have the
> caller manage placing that back into the return structure. That also
> allows the helper to be independent of the structure we're operating
> on, it could be a region_info, irq_info, device_info, etc. It only
> needs to know the layout of the capability type we're trying to add,
> not the info structure itself. Thanks,
>
The capability feature is for region_info (VFIO_REGION_INFO_FLAG_CAPS),
then structure we are operating couldn't be irq_info or device_info, right?
Kirti.
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-24 23:40 +0200 |
| Subject | Re: [PATCH v9 06/12] vfio_pci: Update vfio_pci to use vfio_info_add_capability() |
| Message-ID | <svV8l-4KG-15@gated-at.bofh.it> |
| In reply to | #1507744 |
On Tue, 25 Oct 2016 02:52:39 +0530
Kirti Wankhede <kwankhede@nvidia.com> wrote:
> On 10/21/2016 12:54 AM, Alex Williamson wrote:
> > On Tue, 18 Oct 2016 02:52:06 +0530
> > Kirti Wankhede <kwankhede@nvidia.com> wrote:
> >
> >> Update msix_sparse_mmap_cap() to use vfio_info_add_capability()
> >> Update region type capability to use vfio_info_add_capability()
> >> Can't split this commit for MSIx and region_type cap since there is a
> >> common code which need to be updated for both the cases.
> >>
> >> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
> >> Signed-off-by: Neo Jia <cjia@nvidia.com>
> >> Change-Id: I52bb28c7875a6da5a79ddad1843e6088aff58a45
> >> ---
> >> drivers/vfio/pci/vfio_pci.c | 72 +++++++++++++++++----------------------------
> >> 1 file changed, 27 insertions(+), 45 deletions(-)
> >>
> >> diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
> >> index d624a527777f..1ec0565b48ea 100644
> >> --- a/drivers/vfio/pci/vfio_pci.c
> >> +++ b/drivers/vfio/pci/vfio_pci.c
> >> @@ -556,12 +556,12 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
> >> }
> >>
> >> static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
> >> + struct vfio_region_info *info,
> >> struct vfio_info_cap *caps)
> >> {
> >> - struct vfio_info_cap_header *header;
> >> struct vfio_region_info_cap_sparse_mmap *sparse;
> >> size_t end, size;
> >> - int nr_areas = 2, i = 0;
> >> + int nr_areas = 2, i = 0, ret;
> >>
> >> end = pci_resource_len(vdev->pdev, vdev->msix_bar);
> >>
> >> @@ -572,13 +572,10 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
> >>
> >> size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
> >>
> >> - header = vfio_info_cap_add(caps, size,
> >> - VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
> >> - if (IS_ERR(header))
> >> - return PTR_ERR(header);
> >> + sparse = kzalloc(size, GFP_KERNEL);
> >> + if (!sparse)
> >> + return -ENOMEM;
> >>
> >> - sparse = container_of(header,
> >> - struct vfio_region_info_cap_sparse_mmap, header);
> >> sparse->nr_areas = nr_areas;
> >>
> >> if (vdev->msix_offset & PAGE_MASK) {
> >> @@ -594,26 +591,11 @@ static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
> >> i++;
> >> }
> >>
> >> - return 0;
> >> -}
> >> -
> >> -static int region_type_cap(struct vfio_pci_device *vdev,
> >> - struct vfio_info_cap *caps,
> >> - unsigned int type, unsigned int subtype)
> >> -{
> >> - struct vfio_info_cap_header *header;
> >> - struct vfio_region_info_cap_type *cap;
> >> -
> >> - header = vfio_info_cap_add(caps, sizeof(*cap),
> >> - VFIO_REGION_INFO_CAP_TYPE, 1);
> >> - if (IS_ERR(header))
> >> - return PTR_ERR(header);
> >> + ret = vfio_info_add_capability(info, caps,
> >> + VFIO_REGION_INFO_CAP_SPARSE_MMAP, sparse);
> >> + kfree(sparse);
> >>
> >> - cap = container_of(header, struct vfio_region_info_cap_type, header);
> >> - cap->type = type;
> >> - cap->subtype = subtype;
> >> -
> >> - return 0;
> >> + return ret;
> >> }
> >>
> >> int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
> >> @@ -704,7 +686,8 @@ static long vfio_pci_ioctl(void *device_data,
> >> if (vdev->bar_mmap_supported[info.index]) {
> >> info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
> >> if (info.index == vdev->msix_bar) {
> >> - ret = msix_sparse_mmap_cap(vdev, &caps);
> >> + ret = msix_sparse_mmap_cap(vdev, &info,
> >> + &caps);
> >> if (ret)
> >> return ret;
> >> }
> >> @@ -752,6 +735,9 @@ static long vfio_pci_ioctl(void *device_data,
> >>
> >> break;
> >> default:
> >> + {
> >> + struct vfio_region_info_cap_type cap_type;
> >> +
> >> if (info.index >=
> >> VFIO_PCI_NUM_REGIONS + vdev->num_regions)
> >> return -EINVAL;
> >> @@ -762,27 +748,23 @@ static long vfio_pci_ioctl(void *device_data,
> >> info.size = vdev->region[i].size;
> >> info.flags = vdev->region[i].flags;
> >>
> >> - ret = region_type_cap(vdev, &caps,
> >> - vdev->region[i].type,
> >> - vdev->region[i].subtype);
> >> + cap_type.type = vdev->region[i].type;
> >> + cap_type.subtype = vdev->region[i].subtype;
> >> +
> >> + ret = vfio_info_add_capability(&info, &caps,
> >> + VFIO_REGION_INFO_CAP_TYPE,
> >> + &cap_type);
> >> if (ret)
> >> return ret;
> >> +
> >> + }
> >> }
> >>
> >> - if (caps.size) {
> >> - info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
> >> - if (info.argsz < sizeof(info) + caps.size) {
> >> - info.argsz = sizeof(info) + caps.size;
> >> - info.cap_offset = 0;
> >> - } else {
> >> - vfio_info_cap_shift(&caps, sizeof(info));
> >> - if (copy_to_user((void __user *)arg +
> >> - sizeof(info), caps.buf,
> >> - caps.size)) {
> >> - kfree(caps.buf);
> >> - return -EFAULT;
> >> - }
> >> - info.cap_offset = sizeof(info);
> >
> > I prefer the case above, I'm fine with breaking out helpers to build a
> > buffer containing the capability chain, but I would rather have the
> > caller manage placing that back into the return structure. That also
> > allows the helper to be independent of the structure we're operating
> > on, it could be a region_info, irq_info, device_info, etc. It only
> > needs to know the layout of the capability type we're trying to add,
> > not the info structure itself. Thanks,
> >
>
> The capability feature is for region_info (VFIO_REGION_INFO_FLAG_CAPS),
> then structure we are operating couldn't be irq_info or device_info, right?
My point was that I prefer the caller managing region_info.flags and
region_info.cap_offset, the helper only managing creating the
capability chain within the buffer. There is a namespace conflict for
the various capabilities though, each foo_info has their own space,
which can conflict, which make make a common helper impractical.
Thanks,
Alex
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-17 23:30 +0200 |
| Subject | [PATCH v9 03/12] vfio: Rearrange functions to get vfio_group from dev |
| Message-ID | <stnDQ-JF-33@gated-at.bofh.it> |
| In reply to | #1502472 |
Rearrange functions to have common function to increment container_users.
Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Change-Id: I1f93262bdbab75094bc24b087b29da35ba70c4c6
---
drivers/vfio/vfio.c | 57 ++++++++++++++++++++++++++++++++++-------------------
1 file changed, 37 insertions(+), 20 deletions(-)
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index d1d70e0b011b..2e83bdf007fe 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -480,6 +480,21 @@ static struct vfio_group *vfio_group_get_from_minor(int minor)
return group;
}
+static struct vfio_group *vfio_group_get_from_dev(struct device *dev)
+{
+ struct iommu_group *iommu_group;
+ struct vfio_group *group;
+
+ iommu_group = iommu_group_get(dev);
+ if (!iommu_group)
+ return NULL;
+
+ group = vfio_group_get_from_iommu(iommu_group);
+ iommu_group_put(iommu_group);
+
+ return group;
+}
+
/**
* Device objects - create, release, get, put, search
*/
@@ -811,16 +826,10 @@ EXPORT_SYMBOL_GPL(vfio_add_group_dev);
*/
struct vfio_device *vfio_device_get_from_dev(struct device *dev)
{
- struct iommu_group *iommu_group;
struct vfio_group *group;
struct vfio_device *device;
- iommu_group = iommu_group_get(dev);
- if (!iommu_group)
- return NULL;
-
- group = vfio_group_get_from_iommu(iommu_group);
- iommu_group_put(iommu_group);
+ group = vfio_group_get_from_dev(dev);
if (!group)
return NULL;
@@ -1376,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group)
group, vfio_dev_viable) == 0);
}
+static int vfio_group_add_container_user(struct vfio_group *group)
+{
+ if (!atomic_inc_not_zero(&group->container_users))
+ return -EINVAL;
+
+ if (group->noiommu) {
+ atomic_dec(&group->container_users);
+ return -EPERM;
+ }
+ if (!group->container->iommu_driver || !vfio_group_viable(group)) {
+ atomic_dec(&group->container_users);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
static const struct file_operations vfio_device_fops;
static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
@@ -1685,23 +1711,14 @@ static const struct file_operations vfio_device_fops = {
struct vfio_group *vfio_group_get_external_user(struct file *filep)
{
struct vfio_group *group = filep->private_data;
+ int ret;
if (filep->f_op != &vfio_group_fops)
return ERR_PTR(-EINVAL);
- if (!atomic_inc_not_zero(&group->container_users))
- return ERR_PTR(-EINVAL);
-
- if (group->noiommu) {
- atomic_dec(&group->container_users);
- return ERR_PTR(-EPERM);
- }
-
- if (!group->container->iommu_driver ||
- !vfio_group_viable(group)) {
- atomic_dec(&group->container_users);
- return ERR_PTR(-EINVAL);
- }
+ ret = vfio_group_add_container_user(group);
+ if (ret)
+ return ERR_PTR(ret);
vfio_group_get(group);
--
2.7.0
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-19 19:30 +0200 |
| Subject | Re: [PATCH v9 03/12] vfio: Rearrange functions to get vfio_group from dev |
| Message-ID | <su2QF-4lW-21@gated-at.bofh.it> |
| In reply to | #1502481 |
On Tue, 18 Oct 2016 02:52:03 +0530
Kirti Wankhede <kwankhede@nvidia.com> wrote:
> Rearrange functions to have common function to increment container_users.
>
> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
> Signed-off-by: Neo Jia <cjia@nvidia.com>
> Change-Id: I1f93262bdbab75094bc24b087b29da35ba70c4c6
> ---
> drivers/vfio/vfio.c | 57 ++++++++++++++++++++++++++++++++++-------------------
> 1 file changed, 37 insertions(+), 20 deletions(-)
Ideally these would be two separate patches, one pulling
vfio_group_get_from_dev() out of vfio_device_get_from_dev() and
replacing the existing use, the other doing the same for
vfio_group_add_container_user(). Otherwise it looks good. Thanks,
Alex
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index d1d70e0b011b..2e83bdf007fe 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -480,6 +480,21 @@ static struct vfio_group *vfio_group_get_from_minor(int minor)
> return group;
> }
>
> +static struct vfio_group *vfio_group_get_from_dev(struct device *dev)
> +{
> + struct iommu_group *iommu_group;
> + struct vfio_group *group;
> +
> + iommu_group = iommu_group_get(dev);
> + if (!iommu_group)
> + return NULL;
> +
> + group = vfio_group_get_from_iommu(iommu_group);
> + iommu_group_put(iommu_group);
> +
> + return group;
> +}
> +
> /**
> * Device objects - create, release, get, put, search
> */
> @@ -811,16 +826,10 @@ EXPORT_SYMBOL_GPL(vfio_add_group_dev);
> */
> struct vfio_device *vfio_device_get_from_dev(struct device *dev)
> {
> - struct iommu_group *iommu_group;
> struct vfio_group *group;
> struct vfio_device *device;
>
> - iommu_group = iommu_group_get(dev);
> - if (!iommu_group)
> - return NULL;
> -
> - group = vfio_group_get_from_iommu(iommu_group);
> - iommu_group_put(iommu_group);
> + group = vfio_group_get_from_dev(dev);
> if (!group)
> return NULL;
>
> @@ -1376,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group)
> group, vfio_dev_viable) == 0);
> }
>
> +static int vfio_group_add_container_user(struct vfio_group *group)
> +{
> + if (!atomic_inc_not_zero(&group->container_users))
> + return -EINVAL;
> +
> + if (group->noiommu) {
> + atomic_dec(&group->container_users);
> + return -EPERM;
> + }
> + if (!group->container->iommu_driver || !vfio_group_viable(group)) {
> + atomic_dec(&group->container_users);
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> static const struct file_operations vfio_device_fops;
>
> static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
> @@ -1685,23 +1711,14 @@ static const struct file_operations vfio_device_fops = {
> struct vfio_group *vfio_group_get_external_user(struct file *filep)
> {
> struct vfio_group *group = filep->private_data;
> + int ret;
>
> if (filep->f_op != &vfio_group_fops)
> return ERR_PTR(-EINVAL);
>
> - if (!atomic_inc_not_zero(&group->container_users))
> - return ERR_PTR(-EINVAL);
> -
> - if (group->noiommu) {
> - atomic_dec(&group->container_users);
> - return ERR_PTR(-EPERM);
> - }
> -
> - if (!group->container->iommu_driver ||
> - !vfio_group_viable(group)) {
> - atomic_dec(&group->container_users);
> - return ERR_PTR(-EINVAL);
> - }
> + ret = vfio_group_add_container_user(group);
> + if (ret)
> + return ERR_PTR(ret);
>
> vfio_group_get(group);
>
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-17 23:30 +0200 |
| Subject | [PATCH v9 05/12] vfio: Introduce common function to add capabilities |
| Message-ID | <stnDQ-JF-35@gated-at.bofh.it> |
| In reply to | #1502472 |
Vendor driver using mediated device framework should use
vfio_info_add_capability() to add capabilities.
Introduced this function to reduce code duplication in vendor drivers.
Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Change-Id: I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
---
drivers/vfio/vfio.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/vfio.h | 4 +++
2 files changed, 82 insertions(+)
diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index a5a210005b65..e96cb3f7a23c 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
}
EXPORT_SYMBOL_GPL(vfio_info_cap_shift);
+static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
+{
+ struct vfio_info_cap_header *header;
+ struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
+ size_t size;
+
+ size = sizeof(*sparse) + sparse->nr_areas * sizeof(*sparse->areas);
+ header = vfio_info_cap_add(caps, size,
+ VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
+ if (IS_ERR(header))
+ return PTR_ERR(header);
+
+ sparse_cap = container_of(header,
+ struct vfio_region_info_cap_sparse_mmap, header);
+ sparse_cap->nr_areas = sparse->nr_areas;
+ memcpy(sparse_cap->areas, sparse->areas,
+ sparse->nr_areas * sizeof(*sparse->areas));
+ return 0;
+}
+
+static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
+{
+ struct vfio_info_cap_header *header;
+ struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
+
+ header = vfio_info_cap_add(caps, sizeof(*cap),
+ VFIO_REGION_INFO_CAP_TYPE, 1);
+ if (IS_ERR(header))
+ return PTR_ERR(header);
+
+ type_cap = container_of(header, struct vfio_region_info_cap_type,
+ header);
+ type_cap->type = cap->type;
+ type_cap->subtype = cap->subtype;
+ return 0;
+}
+
+int vfio_info_add_capability(struct vfio_region_info *info,
+ struct vfio_info_cap *caps,
+ int cap_type_id,
+ void *cap_type)
+{
+ int ret;
+
+ if (!cap_type)
+ return 0;
+
+ switch (cap_type_id) {
+ case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
+ ret = sparse_mmap_cap(caps, cap_type);
+ if (ret)
+ return ret;
+ break;
+
+ case VFIO_REGION_INFO_CAP_TYPE:
+ ret = region_type_cap(caps, cap_type);
+ if (ret)
+ return ret;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
+
+ if (caps->size) {
+ if (info->argsz < sizeof(*info) + caps->size) {
+ info->argsz = sizeof(*info) + caps->size;
+ info->cap_offset = 0;
+ } else {
+ vfio_info_cap_shift(caps, sizeof(*info));
+ info->cap_offset = sizeof(*info);
+ }
+ }
+ return 0;
+}
+EXPORT_SYMBOL(vfio_info_add_capability);
+
/*
* Pin a set of guest PFNs and return their associated host PFNs for local
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 0bd25ba6223d..854a4b40be02 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -108,6 +108,10 @@ extern struct vfio_info_cap_header *vfio_info_cap_add(
struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
+extern int vfio_info_add_capability(struct vfio_region_info *info,
+ struct vfio_info_cap *caps,
+ int cap_type_id, void *cap_type);
+
struct pci_dev;
#ifdef CONFIG_EEH
extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
--
2.7.0
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-20 21:30 +0200 |
| Subject | Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities |
| Message-ID | <surcl-3mp-1@gated-at.bofh.it> |
| In reply to | #1502482 |
On Tue, 18 Oct 2016 02:52:05 +0530
Kirti Wankhede <kwankhede@nvidia.com> wrote:
> Vendor driver using mediated device framework should use
> vfio_info_add_capability() to add capabilities.
> Introduced this function to reduce code duplication in vendor drivers.
>
> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
> Signed-off-by: Neo Jia <cjia@nvidia.com>
> Change-Id: I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
> ---
> drivers/vfio/vfio.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> include/linux/vfio.h | 4 +++
> 2 files changed, 82 insertions(+)
>
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index a5a210005b65..e96cb3f7a23c 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
> }
> EXPORT_SYMBOL_GPL(vfio_info_cap_shift);
>
> +static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
> +{
> + struct vfio_info_cap_header *header;
> + struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
> + size_t size;
> +
> + size = sizeof(*sparse) + sparse->nr_areas * sizeof(*sparse->areas);
> + header = vfio_info_cap_add(caps, size,
> + VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
> + if (IS_ERR(header))
> + return PTR_ERR(header);
> +
> + sparse_cap = container_of(header,
> + struct vfio_region_info_cap_sparse_mmap, header);
> + sparse_cap->nr_areas = sparse->nr_areas;
> + memcpy(sparse_cap->areas, sparse->areas,
> + sparse->nr_areas * sizeof(*sparse->areas));
> + return 0;
> +}
> +
> +static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
> +{
> + struct vfio_info_cap_header *header;
> + struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
> +
> + header = vfio_info_cap_add(caps, sizeof(*cap),
> + VFIO_REGION_INFO_CAP_TYPE, 1);
> + if (IS_ERR(header))
> + return PTR_ERR(header);
> +
> + type_cap = container_of(header, struct vfio_region_info_cap_type,
> + header);
> + type_cap->type = cap->type;
> + type_cap->subtype = cap->subtype;
> + return 0;
> +}
> +
> +int vfio_info_add_capability(struct vfio_region_info *info,
> + struct vfio_info_cap *caps,
> + int cap_type_id,
> + void *cap_type)
> +{
> + int ret;
> +
> + if (!cap_type)
> + return 0;
> +
> + switch (cap_type_id) {
> + case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
> + ret = sparse_mmap_cap(caps, cap_type);
> + if (ret)
> + return ret;
> + break;
> +
> + case VFIO_REGION_INFO_CAP_TYPE:
> + ret = region_type_cap(caps, cap_type);
> + if (ret)
> + return ret;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
> +
> + if (caps->size) {
> + if (info->argsz < sizeof(*info) + caps->size) {
> + info->argsz = sizeof(*info) + caps->size;
> + info->cap_offset = 0;
> + } else {
> + vfio_info_cap_shift(caps, sizeof(*info));
> + info->cap_offset = sizeof(*info);
This doesn't work. We build the capability chain in a buffer and
vfio_info_cap_add() expects the chain to be zero-based as each
capability is added. vfio_info_cap_shift() is meant to be called once
on that buffer immediately before copying it back to the user buffer to
adjust the chain offsets to account for the offset within the buffer.
vfio_info_cap_shift() cannot be called repeatedly on the buffer as we
do support multiple capabilities in a chain.
> + }
> + }
> + return 0;
> +}
> +EXPORT_SYMBOL(vfio_info_add_capability);
> +
>
> /*
> * Pin a set of guest PFNs and return their associated host PFNs for
> local diff --git a/include/linux/vfio.h b/include/linux/vfio.h
> index 0bd25ba6223d..854a4b40be02 100644
> --- a/include/linux/vfio.h
> +++ b/include/linux/vfio.h
> @@ -108,6 +108,10 @@ extern struct vfio_info_cap_header
> *vfio_info_cap_add( struct vfio_info_cap *caps, size_t size, u16 id,
> u16 version); extern void vfio_info_cap_shift(struct vfio_info_cap
> *caps, size_t offset);
> +extern int vfio_info_add_capability(struct vfio_region_info *info,
> + struct vfio_info_cap *caps,
> + int cap_type_id, void *cap_type);
> +
> struct pci_dev;
> #ifdef CONFIG_EEH
> extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-24 23:30 +0200 |
| Subject | Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities |
| Message-ID | <svUYG-4HF-29@gated-at.bofh.it> |
| In reply to | #1505200 |
On 10/21/2016 12:54 AM, Alex Williamson wrote:
> On Tue, 18 Oct 2016 02:52:05 +0530
> Kirti Wankhede <kwankhede@nvidia.com> wrote:
>
>> Vendor driver using mediated device framework should use
>> vfio_info_add_capability() to add capabilities.
>> Introduced this function to reduce code duplication in vendor drivers.
>>
>> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
>> Signed-off-by: Neo Jia <cjia@nvidia.com>
>> Change-Id: I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
>> ---
>> drivers/vfio/vfio.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>> include/linux/vfio.h | 4 +++
>> 2 files changed, 82 insertions(+)
>>
>> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
>> index a5a210005b65..e96cb3f7a23c 100644
>> --- a/drivers/vfio/vfio.c
>> +++ b/drivers/vfio/vfio.c
>> @@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
>> }
>> EXPORT_SYMBOL_GPL(vfio_info_cap_shift);
>>
>> +static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
>> +{
>> + struct vfio_info_cap_header *header;
>> + struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
>> + size_t size;
>> +
>> + size = sizeof(*sparse) + sparse->nr_areas * sizeof(*sparse->areas);
>> + header = vfio_info_cap_add(caps, size,
>> + VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
>> + if (IS_ERR(header))
>> + return PTR_ERR(header);
>> +
>> + sparse_cap = container_of(header,
>> + struct vfio_region_info_cap_sparse_mmap, header);
>> + sparse_cap->nr_areas = sparse->nr_areas;
>> + memcpy(sparse_cap->areas, sparse->areas,
>> + sparse->nr_areas * sizeof(*sparse->areas));
>> + return 0;
>> +}
>> +
>> +static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
>> +{
>> + struct vfio_info_cap_header *header;
>> + struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
>> +
>> + header = vfio_info_cap_add(caps, sizeof(*cap),
>> + VFIO_REGION_INFO_CAP_TYPE, 1);
>> + if (IS_ERR(header))
>> + return PTR_ERR(header);
>> +
>> + type_cap = container_of(header, struct vfio_region_info_cap_type,
>> + header);
>> + type_cap->type = cap->type;
>> + type_cap->subtype = cap->subtype;
>> + return 0;
>> +}
>> +
>> +int vfio_info_add_capability(struct vfio_region_info *info,
>> + struct vfio_info_cap *caps,
>> + int cap_type_id,
>> + void *cap_type)
>> +{
>> + int ret;
>> +
>> + if (!cap_type)
>> + return 0;
>> +
>> + switch (cap_type_id) {
>> + case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
>> + ret = sparse_mmap_cap(caps, cap_type);
>> + if (ret)
>> + return ret;
>> + break;
>> +
>> + case VFIO_REGION_INFO_CAP_TYPE:
>> + ret = region_type_cap(caps, cap_type);
>> + if (ret)
>> + return ret;
>> + break;
>> + default:
>> + return -EINVAL;
>> + }
>> +
>> + info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
>> +
>> + if (caps->size) {
>> + if (info->argsz < sizeof(*info) + caps->size) {
>> + info->argsz = sizeof(*info) + caps->size;
>> + info->cap_offset = 0;
>> + } else {
>> + vfio_info_cap_shift(caps, sizeof(*info));
>> + info->cap_offset = sizeof(*info);
>
> This doesn't work. We build the capability chain in a buffer and
> vfio_info_cap_add() expects the chain to be zero-based as each
> capability is added. vfio_info_cap_shift() is meant to be called once
> on that buffer immediately before copying it back to the user buffer to
> adjust the chain offsets to account for the offset within the buffer.
> vfio_info_cap_shift() cannot be called repeatedly on the buffer as we
> do support multiple capabilities in a chain.
>
From the code I see, we add one type of capability at a time, either
VFIO_REGION_INFO_CAP_SPARSE_MMAP or VFIO_REGION_INFO_CAP_TYPE. Both are
not the part of same case in the switch, right?
I do tested VFIO_REGION_INFO_CAP_SPARSE_MMAP by mapping some part of
BAR0 and that works.
Kirti.
[toc] | [prev] | [next] | [standalone]
| From | Alex Williamson <alex.williamson@redhat.com> |
|---|---|
| Date | 2016-10-24 23:40 +0200 |
| Subject | Re: [PATCH v9 05/12] vfio: Introduce common function to add capabilities |
| Message-ID | <svV8l-4KG-19@gated-at.bofh.it> |
| In reply to | #1507752 |
On Tue, 25 Oct 2016 02:57:58 +0530
Kirti Wankhede <kwankhede@nvidia.com> wrote:
> On 10/21/2016 12:54 AM, Alex Williamson wrote:
> > On Tue, 18 Oct 2016 02:52:05 +0530
> > Kirti Wankhede <kwankhede@nvidia.com> wrote:
> >
> >> Vendor driver using mediated device framework should use
> >> vfio_info_add_capability() to add capabilities.
> >> Introduced this function to reduce code duplication in vendor drivers.
> >>
> >> Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
> >> Signed-off-by: Neo Jia <cjia@nvidia.com>
> >> Change-Id: I6fca329fa2291f37a2c859d0bc97574d9e2ce1a6
> >> ---
> >> drivers/vfio/vfio.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> >> include/linux/vfio.h | 4 +++
> >> 2 files changed, 82 insertions(+)
> >>
> >> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> >> index a5a210005b65..e96cb3f7a23c 100644
> >> --- a/drivers/vfio/vfio.c
> >> +++ b/drivers/vfio/vfio.c
> >> @@ -1799,6 +1799,84 @@ void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
> >> }
> >> EXPORT_SYMBOL_GPL(vfio_info_cap_shift);
> >>
> >> +static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
> >> +{
> >> + struct vfio_info_cap_header *header;
> >> + struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
> >> + size_t size;
> >> +
> >> + size = sizeof(*sparse) + sparse->nr_areas * sizeof(*sparse->areas);
> >> + header = vfio_info_cap_add(caps, size,
> >> + VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
> >> + if (IS_ERR(header))
> >> + return PTR_ERR(header);
> >> +
> >> + sparse_cap = container_of(header,
> >> + struct vfio_region_info_cap_sparse_mmap, header);
> >> + sparse_cap->nr_areas = sparse->nr_areas;
> >> + memcpy(sparse_cap->areas, sparse->areas,
> >> + sparse->nr_areas * sizeof(*sparse->areas));
> >> + return 0;
> >> +}
> >> +
> >> +static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
> >> +{
> >> + struct vfio_info_cap_header *header;
> >> + struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
> >> +
> >> + header = vfio_info_cap_add(caps, sizeof(*cap),
> >> + VFIO_REGION_INFO_CAP_TYPE, 1);
> >> + if (IS_ERR(header))
> >> + return PTR_ERR(header);
> >> +
> >> + type_cap = container_of(header, struct vfio_region_info_cap_type,
> >> + header);
> >> + type_cap->type = cap->type;
> >> + type_cap->subtype = cap->subtype;
> >> + return 0;
> >> +}
> >> +
> >> +int vfio_info_add_capability(struct vfio_region_info *info,
> >> + struct vfio_info_cap *caps,
> >> + int cap_type_id,
> >> + void *cap_type)
> >> +{
> >> + int ret;
> >> +
> >> + if (!cap_type)
> >> + return 0;
> >> +
> >> + switch (cap_type_id) {
> >> + case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
> >> + ret = sparse_mmap_cap(caps, cap_type);
> >> + if (ret)
> >> + return ret;
> >> + break;
> >> +
> >> + case VFIO_REGION_INFO_CAP_TYPE:
> >> + ret = region_type_cap(caps, cap_type);
> >> + if (ret)
> >> + return ret;
> >> + break;
> >> + default:
> >> + return -EINVAL;
> >> + }
> >> +
> >> + info->flags |= VFIO_REGION_INFO_FLAG_CAPS;
> >> +
> >> + if (caps->size) {
> >> + if (info->argsz < sizeof(*info) + caps->size) {
> >> + info->argsz = sizeof(*info) + caps->size;
> >> + info->cap_offset = 0;
> >> + } else {
> >> + vfio_info_cap_shift(caps, sizeof(*info));
> >> + info->cap_offset = sizeof(*info);
> >
> > This doesn't work. We build the capability chain in a buffer and
> > vfio_info_cap_add() expects the chain to be zero-based as each
> > capability is added. vfio_info_cap_shift() is meant to be called once
> > on that buffer immediately before copying it back to the user buffer to
> > adjust the chain offsets to account for the offset within the buffer.
> > vfio_info_cap_shift() cannot be called repeatedly on the buffer as we
> > do support multiple capabilities in a chain.
> >
>
> From the code I see, we add one type of capability at a time, either
> VFIO_REGION_INFO_CAP_SPARSE_MMAP or VFIO_REGION_INFO_CAP_TYPE. Both are
> not the part of same case in the switch, right?
> I do tested VFIO_REGION_INFO_CAP_SPARSE_MMAP by mapping some part of
> BAR0 and that works.
That simply means that we don't _currently_ have a user that implements
multiple chain entries. The interface is however designed to support
multiple entries and this breaks that goal. Thanks,
Alex
[toc] | [prev] | [next] | [standalone]
| From | Kirti Wankhede <kwankhede@nvidia.com> |
|---|---|
| Date | 2016-10-17 23:30 +0200 |
| Subject | [PATCH v9 01/12] vfio: Mediated device Core driver |
| Message-ID | <stnDQ-JF-23@gated-at.bofh.it> |
| In reply to | #1502472 |
Design for Mediated Device Driver:
Main purpose of this driver is to provide a common interface for mediated
device management that can be used by different drivers of different
devices.
This module provides a generic interface to create the device, add it to
mediated bus, add device to IOMMU group and then add it to vfio group.
Below is the high Level block diagram, with Nvidia, Intel and IBM devices
as example, since these are the devices which are going to actively use
this module as of now.
+---------------+
| |
| +-----------+ | mdev_register_driver() +--------------+
| | | +<------------------------+ __init() |
| | mdev | | | |
| | bus | +------------------------>+ |<-> VFIO user
| | driver | | probe()/remove() | vfio_mdev.ko | APIs
| | | | | |
| +-----------+ | +--------------+
| |
| MDEV CORE |
| MODULE |
| mdev.ko |
| +-----------+ | mdev_register_device() +--------------+
| | | +<------------------------+ |
| | | | | nvidia.ko |<-> physical
| | | +------------------------>+ | device
| | | | callback +--------------+
| | Physical | |
| | device | | mdev_register_device() +--------------+
| | interface | |<------------------------+ |
| | | | | i915.ko |<-> physical
| | | +------------------------>+ | device
| | | | callback +--------------+
| | | |
| | | | mdev_register_device() +--------------+
| | | +<------------------------+ |
| | | | | ccw_device.ko|<-> physical
| | | +------------------------>+ | device
| | | | callback +--------------+
| +-----------+ |
+---------------+
Core driver provides two types of registration interfaces:
1. Registration interface for mediated bus driver:
/**
* struct mdev_driver - Mediated device's driver
* @name: driver name
* @probe: called when new device created
* @remove:called when device removed
* @driver:device driver structure
*
**/
struct mdev_driver {
const char *name;
int (*probe) (struct device *dev);
void (*remove) (struct device *dev);
struct device_driver driver;
};
Mediated bus driver for mdev device should use this interface to register
and unregister with core driver respectively:
int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
void mdev_unregister_driver(struct mdev_driver *drv);
Medisted bus driver is responsible to add/delete mediated devices to/from
VFIO group when devices are bound and unbound to the driver.
2. Physical device driver interface
This interface provides vendor driver the set APIs to manage physical
device related work in its driver. APIs are :
* dev_attr_groups: attributes of the parent device.
* mdev_attr_groups: attributes of the mediated device.
* supported_type_groups: attributes to define supported type. This is
mandatory field.
* create: to allocate basic resources in driver for a mediated device.
* remove: to free resources in driver when mediated device is destroyed.
* open: open callback of mediated device
* release: release callback of mediated device
* read : read emulation callback.
* write: write emulation callback.
* mmap: mmap emulation callback.
* ioctl: ioctl callback.
Drivers should use these interfaces to register and unregister device to
mdev core driver respectively:
extern int mdev_register_device(struct device *dev,
const struct parent_ops *ops);
extern void mdev_unregister_device(struct device *dev);
There are no locks to serialize above callbacks in mdev driver and
vfio_mdev driver. If required, vendor driver can have locks to serialize
above APIs in their driver.
Signed-off-by: Kirti Wankhede <kwankhede@nvidia.com>
Signed-off-by: Neo Jia <cjia@nvidia.com>
Change-Id: I73a5084574270b14541c529461ea2f03c292d510
---
drivers/vfio/Kconfig | 1 +
drivers/vfio/Makefile | 1 +
drivers/vfio/mdev/Kconfig | 11 ++
drivers/vfio/mdev/Makefile | 4 +
drivers/vfio/mdev/mdev_core.c | 372 +++++++++++++++++++++++++++++++++++++++
drivers/vfio/mdev/mdev_driver.c | 128 ++++++++++++++
drivers/vfio/mdev/mdev_private.h | 41 +++++
drivers/vfio/mdev/mdev_sysfs.c | 296 +++++++++++++++++++++++++++++++
include/linux/mdev.h | 177 +++++++++++++++++++
9 files changed, 1031 insertions(+)
create mode 100644 drivers/vfio/mdev/Kconfig
create mode 100644 drivers/vfio/mdev/Makefile
create mode 100644 drivers/vfio/mdev/mdev_core.c
create mode 100644 drivers/vfio/mdev/mdev_driver.c
create mode 100644 drivers/vfio/mdev/mdev_private.h
create mode 100644 drivers/vfio/mdev/mdev_sysfs.c
create mode 100644 include/linux/mdev.h
diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig
index da6e2ce77495..23eced02aaf6 100644
--- a/drivers/vfio/Kconfig
+++ b/drivers/vfio/Kconfig
@@ -48,4 +48,5 @@ menuconfig VFIO_NOIOMMU
source "drivers/vfio/pci/Kconfig"
source "drivers/vfio/platform/Kconfig"
+source "drivers/vfio/mdev/Kconfig"
source "virt/lib/Kconfig"
diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile
index 7b8a31f63fea..4a23c13b6be4 100644
--- a/drivers/vfio/Makefile
+++ b/drivers/vfio/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o
obj-$(CONFIG_VFIO_PCI) += pci/
obj-$(CONFIG_VFIO_PLATFORM) += platform/
+obj-$(CONFIG_VFIO_MDEV) += mdev/
diff --git a/drivers/vfio/mdev/Kconfig b/drivers/vfio/mdev/Kconfig
new file mode 100644
index 000000000000..93addace9a67
--- /dev/null
+++ b/drivers/vfio/mdev/Kconfig
@@ -0,0 +1,11 @@
+
+config VFIO_MDEV
+ tristate "Mediated device driver framework"
+ depends on VFIO
+ default n
+ help
+ Provides a framework to virtualize devices which don't have SR_IOV
+ capability built-in.
+ See Documentation/vfio-mdev/vfio-mediated-device.txt for more details.
+
+ If you don't know what do here, say N.
diff --git a/drivers/vfio/mdev/Makefile b/drivers/vfio/mdev/Makefile
new file mode 100644
index 000000000000..31bc04801d94
--- /dev/null
+++ b/drivers/vfio/mdev/Makefile
@@ -0,0 +1,4 @@
+
+mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o
+
+obj-$(CONFIG_VFIO_MDEV) += mdev.o
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
new file mode 100644
index 000000000000..7db5ec164aeb
--- /dev/null
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -0,0 +1,372 @@
+/*
+ * Mediated device Core Driver
+ *
+ * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
+ * Author: Neo Jia <cjia@nvidia.com>
+ * Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/sysfs.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+#define DRIVER_VERSION "0.1"
+#define DRIVER_AUTHOR "NVIDIA Corporation"
+#define DRIVER_DESC "Mediated device Core Driver"
+
+static LIST_HEAD(parent_list);
+static DEFINE_MUTEX(parent_list_lock);
+static struct class_compat *mdev_bus_compat_class;
+
+static int _find_mdev_device(struct device *dev, void *data)
+{
+ struct mdev_device *mdev;
+
+ if (!dev_is_mdev(dev))
+ return 0;
+
+ mdev = to_mdev_device(dev);
+
+ if (uuid_le_cmp(mdev->uuid, *(uuid_le *)data) == 0)
+ return 1;
+
+ return 0;
+}
+
+static struct mdev_device *__find_mdev_device(struct parent_device *parent,
+ uuid_le uuid)
+{
+ struct device *dev;
+
+ dev = device_find_child(parent->dev, &uuid, _find_mdev_device);
+ if (!dev)
+ return NULL;
+
+ put_device(dev);
+
+ return to_mdev_device(dev);
+}
+
+/* Should be called holding parent_list_lock */
+static struct parent_device *__find_parent_device(struct device *dev)
+{
+ struct parent_device *parent;
+
+ list_for_each_entry(parent, &parent_list, next) {
+ if (parent->dev == dev)
+ return parent;
+ }
+ return NULL;
+}
+
+static void mdev_release_parent(struct kref *kref)
+{
+ struct parent_device *parent = container_of(kref, struct parent_device,
+ ref);
+ struct device *dev = parent->dev;
+
+ kfree(parent);
+ put_device(dev);
+}
+
+static
+inline struct parent_device *mdev_get_parent(struct parent_device *parent)
+{
+ if (parent)
+ kref_get(&parent->ref);
+
+ return parent;
+}
+
+static inline void mdev_put_parent(struct parent_device *parent)
+{
+ if (parent)
+ kref_put(&parent->ref, mdev_release_parent);
+}
+
+static int mdev_device_create_ops(struct kobject *kobj,
+ struct mdev_device *mdev)
+{
+ struct parent_device *parent = mdev->parent;
+ int ret;
+
+ ret = parent->ops->create(kobj, mdev);
+ if (ret)
+ return ret;
+
+ ret = sysfs_create_groups(&mdev->dev.kobj,
+ parent->ops->mdev_attr_groups);
+ if (ret)
+ parent->ops->remove(mdev);
+
+ return ret;
+}
+
+static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
+{
+ struct parent_device *parent = mdev->parent;
+ int ret;
+
+ /*
+ * Vendor driver can return error if VMM or userspace application is
+ * using this mdev device.
+ */
+ ret = parent->ops->remove(mdev);
+ if (ret && !force_remove)
+ return -EBUSY;
+
+ sysfs_remove_groups(&mdev->dev.kobj, parent->ops->mdev_attr_groups);
+ return 0;
+}
+
+static int mdev_device_remove_cb(struct device *dev, void *data)
+{
+ return mdev_device_remove(dev, data ? *(bool *)data : true);
+}
+
+/*
+ * mdev_register_device : Register a device
+ * @dev: device structure representing parent device.
+ * @ops: Parent device operation structure to be registered.
+ *
+ * Add device to list of registered parent devices.
+ * Returns a negative value on error, otherwise 0.
+ */
+int mdev_register_device(struct device *dev, const struct parent_ops *ops)
+{
+ int ret = 0;
+ struct parent_device *parent;
+
+ /* check for mandatory ops */
+ if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
+ return -EINVAL;
+
+ dev = get_device(dev);
+ if (!dev)
+ return -EINVAL;
+
+ mutex_lock(&parent_list_lock);
+
+ /* Check for duplicate */
+ parent = __find_parent_device(dev);
+ if (parent) {
+ ret = -EEXIST;
+ goto add_dev_err;
+ }
+
+ parent = kzalloc(sizeof(*parent), GFP_KERNEL);
+ if (!parent) {
+ ret = -ENOMEM;
+ goto add_dev_err;
+ }
+
+ kref_init(&parent->ref);
+
+ parent->dev = dev;
+ parent->ops = ops;
+
+ ret = parent_create_sysfs_files(parent);
+ if (ret) {
+ mutex_unlock(&parent_list_lock);
+ mdev_put_parent(parent);
+ return ret;
+ }
+
+ ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
+ if (ret)
+ dev_warn(dev, "Failed to create compatibility class link\n");
+
+ list_add(&parent->next, &parent_list);
+ mutex_unlock(&parent_list_lock);
+
+ dev_info(dev, "MDEV: Registered\n");
+ return 0;
+
+add_dev_err:
+ mutex_unlock(&parent_list_lock);
+ put_device(dev);
+ return ret;
+}
+EXPORT_SYMBOL(mdev_register_device);
+
+/*
+ * mdev_unregister_device : Unregister a parent device
+ * @dev: device structure representing parent device.
+ *
+ * Remove device from list of registered parent devices. Give a chance to free
+ * existing mediated devices for given device.
+ */
+
+void mdev_unregister_device(struct device *dev)
+{
+ struct parent_device *parent;
+ bool force_remove = true;
+
+ mutex_lock(&parent_list_lock);
+ parent = __find_parent_device(dev);
+
+ if (!parent) {
+ mutex_unlock(&parent_list_lock);
+ return;
+ }
+ dev_info(dev, "MDEV: Unregistering\n");
+
+ /*
+ * Remove parent from the list and remove "mdev_supported_types"
+ * sysfs files so that no new mediated device could be
+ * created for this parent
+ */
+ list_del(&parent->next);
+ parent_remove_sysfs_files(parent);
+
+ mutex_unlock(&parent_list_lock);
+
+ class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
+
+ device_for_each_child(dev, (void *)&force_remove,
+ mdev_device_remove_cb);
+ mdev_put_parent(parent);
+}
+EXPORT_SYMBOL(mdev_unregister_device);
+
+static void mdev_device_release(struct device *dev)
+{
+ struct mdev_device *mdev = to_mdev_device(dev);
+
+ dev_dbg(&mdev->dev, "MDEV: destroying\n");
+ kfree(mdev);
+}
+
+int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
+{
+ int ret;
+ struct mdev_device *mdev;
+ struct parent_device *parent;
+ struct mdev_type *type = to_mdev_type(kobj);
+
+ parent = mdev_get_parent(type->parent);
+ if (!parent)
+ return -EINVAL;
+
+ /* Check for duplicate */
+ mdev = __find_mdev_device(parent, uuid);
+ if (mdev) {
+ ret = -EEXIST;
+ goto create_err;
+ }
+
+ mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+ if (!mdev) {
+ ret = -ENOMEM;
+ goto create_err;
+ }
+
+ memcpy(&mdev->uuid, &uuid, sizeof(uuid_le));
+ mdev->parent = parent;
+ kref_init(&mdev->ref);
+
+ mdev->dev.parent = dev;
+ mdev->dev.bus = &mdev_bus_type;
+ mdev->dev.release = mdev_device_release;
+ dev_set_name(&mdev->dev, "%pUl", uuid.b);
+
+ ret = device_register(&mdev->dev);
+ if (ret) {
+ put_device(&mdev->dev);
+ goto create_err;
+ }
+
+ ret = mdev_device_create_ops(kobj, mdev);
+ if (ret)
+ goto create_failed;
+
+ ret = mdev_create_sysfs_files(&mdev->dev, type);
+ if (ret) {
+ mdev_device_remove_ops(mdev, true);
+ goto create_failed;
+ }
+
+ mdev->type_kobj = kobj;
+ dev_dbg(&mdev->dev, "MDEV: created\n");
+
+ return ret;
+
+create_failed:
+ device_unregister(&mdev->dev);
+
+create_err:
+ mdev_put_parent(parent);
+ return ret;
+}
+
+int mdev_device_remove(struct device *dev, bool force_remove)
+{
+ struct mdev_device *mdev;
+ struct parent_device *parent;
+ struct mdev_type *type;
+ int ret = 0;
+
+ if (!dev_is_mdev(dev))
+ return 0;
+
+ mdev = to_mdev_device(dev);
+ parent = mdev->parent;
+ type = to_mdev_type(mdev->type_kobj);
+
+ ret = mdev_device_remove_ops(mdev, force_remove);
+ if (ret)
+ return ret;
+
+ mdev_remove_sysfs_files(dev, type);
+ device_unregister(dev);
+ mdev_put_parent(parent);
+ return ret;
+}
+
+static int __init mdev_init(void)
+{
+ int ret;
+
+ ret = mdev_bus_register();
+ if (ret) {
+ pr_err("Failed to register mdev bus\n");
+ return ret;
+ }
+
+ mdev_bus_compat_class = class_compat_register("mdev_bus");
+ if (!mdev_bus_compat_class) {
+ mdev_bus_unregister();
+ return -ENOMEM;
+ }
+
+ /*
+ * Attempt to load known vfio_mdev. This gives us a working environment
+ * without the user needing to explicitly load vfio_mdev driver.
+ */
+ request_module_nowait("vfio_mdev");
+
+ return ret;
+}
+
+static void __exit mdev_exit(void)
+{
+ class_compat_unregister(mdev_bus_compat_class);
+ mdev_bus_unregister();
+}
+
+module_init(mdev_init)
+module_exit(mdev_exit)
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/vfio/mdev/mdev_driver.c b/drivers/vfio/mdev/mdev_driver.c
new file mode 100644
index 000000000000..7768ef87f528
--- /dev/null
+++ b/drivers/vfio/mdev/mdev_driver.c
@@ -0,0 +1,128 @@
+/*
+ * MDEV driver
+ *
+ * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
+ * Author: Neo Jia <cjia@nvidia.com>
+ * Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/device.h>
+#include <linux/iommu.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+static int mdev_attach_iommu(struct mdev_device *mdev)
+{
+ int ret;
+ struct iommu_group *group;
+
+ group = iommu_group_alloc();
+ if (IS_ERR(group)) {
+ dev_err(&mdev->dev, "MDEV: failed to allocate group!\n");
+ return PTR_ERR(group);
+ }
+
+ ret = iommu_group_add_device(group, &mdev->dev);
+ if (ret) {
+ dev_err(&mdev->dev, "MDEV: failed to add dev to group!\n");
+ goto attach_fail;
+ }
+
+ dev_info(&mdev->dev, "MDEV: group_id = %d\n",
+ iommu_group_id(group));
+attach_fail:
+ iommu_group_put(group);
+ return ret;
+}
+
+static void mdev_detach_iommu(struct mdev_device *mdev)
+{
+ iommu_group_remove_device(&mdev->dev);
+ dev_info(&mdev->dev, "MDEV: detaching iommu\n");
+}
+
+static int mdev_probe(struct device *dev)
+{
+ struct mdev_driver *drv = to_mdev_driver(dev->driver);
+ struct mdev_device *mdev = to_mdev_device(dev);
+ int ret;
+
+ ret = mdev_attach_iommu(mdev);
+ if (ret) {
+ dev_err(dev, "Failed to attach IOMMU\n");
+ return ret;
+ }
+
+ if (drv && drv->probe)
+ ret = drv->probe(dev);
+
+ if (ret)
+ mdev_detach_iommu(mdev);
+
+ return ret;
+}
+
+static int mdev_remove(struct device *dev)
+{
+ struct mdev_driver *drv = to_mdev_driver(dev->driver);
+ struct mdev_device *mdev = to_mdev_device(dev);
+
+ if (drv && drv->remove)
+ drv->remove(dev);
+
+ mdev_detach_iommu(mdev);
+
+ return 0;
+}
+
+struct bus_type mdev_bus_type = {
+ .name = "mdev",
+ .probe = mdev_probe,
+ .remove = mdev_remove,
+};
+EXPORT_SYMBOL_GPL(mdev_bus_type);
+
+/*
+ * mdev_register_driver - register a new MDEV driver
+ * @drv: the driver to register
+ * @owner: module owner of driver to be registered
+ *
+ * Returns a negative value on error, otherwise 0.
+ */
+int mdev_register_driver(struct mdev_driver *drv, struct module *owner)
+{
+ /* initialize common driver fields */
+ drv->driver.name = drv->name;
+ drv->driver.bus = &mdev_bus_type;
+ drv->driver.owner = owner;
+
+ /* register with core */
+ return driver_register(&drv->driver);
+}
+EXPORT_SYMBOL(mdev_register_driver);
+
+/*
+ * mdev_unregister_driver - unregister MDEV driver
+ * @drv: the driver to unregister
+ *
+ */
+void mdev_unregister_driver(struct mdev_driver *drv)
+{
+ driver_unregister(&drv->driver);
+}
+EXPORT_SYMBOL(mdev_unregister_driver);
+
+int mdev_bus_register(void)
+{
+ return bus_register(&mdev_bus_type);
+}
+
+void mdev_bus_unregister(void)
+{
+ bus_unregister(&mdev_bus_type);
+}
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
new file mode 100644
index 000000000000..000c93fcfdbd
--- /dev/null
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -0,0 +1,41 @@
+/*
+ * Mediated device interal definitions
+ *
+ * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
+ * Author: Neo Jia <cjia@nvidia.com>
+ * Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef MDEV_PRIVATE_H
+#define MDEV_PRIVATE_H
+
+int mdev_bus_register(void);
+void mdev_bus_unregister(void);
+
+struct mdev_type {
+ struct kobject kobj;
+ struct kobject *devices_kobj;
+ struct parent_device *parent;
+ struct list_head next;
+ struct attribute_group *group;
+};
+
+#define to_mdev_type_attr(_attr) \
+ container_of(_attr, struct mdev_type_attribute, attr)
+#define to_mdev_type(_kobj) \
+ container_of(_kobj, struct mdev_type, kobj)
+
+int parent_create_sysfs_files(struct parent_device *parent);
+void parent_remove_sysfs_files(struct parent_device *parent);
+
+int mdev_create_sysfs_files(struct device *dev, struct mdev_type *type);
+void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type);
+
+int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid);
+int mdev_device_remove(struct device *dev, bool force_remove);
+
+#endif /* MDEV_PRIVATE_H */
diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
new file mode 100644
index 000000000000..426e35cf79d0
--- /dev/null
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -0,0 +1,296 @@
+/*
+ * File attributes for Mediated devices
+ *
+ * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
+ * Author: Neo Jia <cjia@nvidia.com>
+ * Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/sysfs.h>
+#include <linux/ctype.h>
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/uuid.h>
+#include <linux/mdev.h>
+
+#include "mdev_private.h"
+
+/* Static functions */
+
+static ssize_t mdev_type_attr_show(struct kobject *kobj,
+ struct attribute *__attr, char *buf)
+{
+ struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
+ struct mdev_type *type = to_mdev_type(kobj);
+ ssize_t ret = -EIO;
+
+ if (attr->show)
+ ret = attr->show(kobj, type->parent->dev, buf);
+ return ret;
+}
+
+static ssize_t mdev_type_attr_store(struct kobject *kobj,
+ struct attribute *__attr,
+ const char *buf, size_t count)
+{
+ struct mdev_type_attribute *attr = to_mdev_type_attr(__attr);
+ struct mdev_type *type = to_mdev_type(kobj);
+ ssize_t ret = -EIO;
+
+ if (attr->store)
+ ret = attr->store(&type->kobj, type->parent->dev, buf, count);
+ return ret;
+}
+
+static const struct sysfs_ops mdev_type_sysfs_ops = {
+ .show = mdev_type_attr_show,
+ .store = mdev_type_attr_store,
+};
+
+static ssize_t create_store(struct kobject *kobj, struct device *dev,
+ const char *buf, size_t count)
+{
+ char *str;
+ uuid_le uuid;
+ int ret;
+
+ if (count < UUID_STRING_LEN)
+ return -EINVAL;
+
+ str = kstrndup(buf, count, GFP_KERNEL);
+ if (!str)
+ return -ENOMEM;
+
+ ret = uuid_le_to_bin(str, &uuid);
+ if (!ret) {
+
+ ret = mdev_device_create(kobj, dev, uuid);
+ if (ret)
+ pr_err("mdev_create: Failed to create mdev device\n");
+ else
+ ret = count;
+ }
+
+ kfree(str);
+ return ret;
+}
+
+MDEV_TYPE_ATTR_WO(create);
+
+static void mdev_type_release(struct kobject *kobj)
+{
+ struct mdev_type *type = to_mdev_type(kobj);
+
+ pr_debug("Releasing group %s\n", kobj->name);
+ kfree(type);
+}
+
+static struct kobj_type mdev_type_ktype = {
+ .sysfs_ops = &mdev_type_sysfs_ops,
+ .release = mdev_type_release,
+};
+
+struct mdev_type *add_mdev_supported_type(struct parent_device *parent,
+ struct attribute_group *group)
+{
+ struct mdev_type *type;
+ int ret;
+
+ if (!group->name) {
+ pr_err("%s: Type name empty!\n", __func__);
+ return ERR_PTR(-EINVAL);
+ }
+
+ type = kzalloc(sizeof(*type), GFP_KERNEL);
+ if (!type)
+ return ERR_PTR(-ENOMEM);
+
+ type->kobj.kset = parent->mdev_types_kset;
+
+ ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
+ "%s-%s", dev_driver_string(parent->dev),
+ group->name);
+ if (ret) {
+ kfree(type);
+ return ERR_PTR(ret);
+ }
+
+ ret = sysfs_create_file(&type->kobj, &mdev_type_attr_create.attr);
+ if (ret)
+ goto attr_create_failed;
+
+ type->devices_kobj = kobject_create_and_add("devices", &type->kobj);
+ if (!type->devices_kobj) {
+ ret = -ENOMEM;
+ goto attr_devices_failed;
+ }
+
+ ret = sysfs_create_files(&type->kobj,
+ (const struct attribute **)group->attrs);
+ if (ret) {
+ ret = -ENOMEM;
+ goto attrs_failed;
+ }
+
+ type->group = group;
+ type->parent = parent;
+ return type;
+
+attrs_failed:
+ kobject_put(type->devices_kobj);
+attr_devices_failed:
+ sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
+attr_create_failed:
+ kobject_del(&type->kobj);
+ kobject_put(&type->kobj);
+ return ERR_PTR(ret);
+}
+
+static void remove_mdev_supported_type(struct mdev_type *type)
+{
+ sysfs_remove_files(&type->kobj,
+ (const struct attribute **)type->group->attrs);
+ kobject_put(type->devices_kobj);
+ sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
+ kobject_del(&type->kobj);
+ kobject_put(&type->kobj);
+}
+
+static int add_mdev_supported_type_groups(struct parent_device *parent)
+{
+ int i;
+
+ for (i = 0; parent->ops->supported_type_groups[i]; i++) {
+ struct mdev_type *type;
+
+ type = add_mdev_supported_type(parent,
+ parent->ops->supported_type_groups[i]);
+ if (IS_ERR(type)) {
+ struct mdev_type *ltype, *tmp;
+
+ list_for_each_entry_safe(ltype, tmp, &parent->type_list,
+ next) {
+ list_del(<ype->next);
+ remove_mdev_supported_type(ltype);
+ }
+ return PTR_ERR(type);
+ }
+ list_add(&type->next, &parent->type_list);
+ }
+ return 0;
+}
+
+/* mdev sysfs Functions */
+
+void parent_remove_sysfs_files(struct parent_device *parent)
+{
+ struct mdev_type *type, *tmp;
+
+ list_for_each_entry_safe(type, tmp, &parent->type_list, next) {
+ list_del(&type->next);
+ remove_mdev_supported_type(type);
+ }
+
+ sysfs_remove_groups(&parent->dev->kobj, parent->ops->dev_attr_groups);
+ kset_unregister(parent->mdev_types_kset);
+}
+
+int parent_create_sysfs_files(struct parent_device *parent)
+{
+ int ret;
+
+ parent->mdev_types_kset = kset_create_and_add("mdev_supported_types",
+ NULL, &parent->dev->kobj);
+
+ if (!parent->mdev_types_kset)
+ return -ENOMEM;
+
+ INIT_LIST_HEAD(&parent->type_list);
+
+ ret = sysfs_create_groups(&parent->dev->kobj,
+ parent->ops->dev_attr_groups);
+ if (ret)
+ goto create_err;
+
+ ret = add_mdev_supported_type_groups(parent);
+ if (ret)
+ sysfs_remove_groups(&parent->dev->kobj,
+ parent->ops->dev_attr_groups);
+ else
+ return ret;
+
+create_err:
+ kset_unregister(parent->mdev_types_kset);
+ return ret;
+}
+
+static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ unsigned long val;
+
+ if (kstrtoul(buf, 0, &val) < 0)
+ return -EINVAL;
+
+ if (val && device_remove_file_self(dev, attr)) {
+ int ret;
+
+ ret = mdev_device_remove(dev, false);
+ if (ret) {
+ device_create_file(dev, attr);
+ return ret;
+ }
+ }
+
+ return count;
+}
+
+static DEVICE_ATTR_WO(remove);
+
+static const struct attribute *mdev_device_attrs[] = {
+ &dev_attr_remove.attr,
+ NULL,
+};
+
+int mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+ int ret;
+
+ ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
+ if (ret) {
+ pr_err("Failed to create remove sysfs entry\n");
+ return ret;
+ }
+
+ ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
+ if (ret) {
+ pr_err("Failed to create symlink in types\n");
+ goto device_link_failed;
+ }
+
+ ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
+ if (ret) {
+ pr_err("Failed to create symlink in device directory\n");
+ goto type_link_failed;
+ }
+
+ return ret;
+
+type_link_failed:
+ sysfs_remove_link(type->devices_kobj, dev_name(dev));
+device_link_failed:
+ sysfs_remove_files(&dev->kobj, mdev_device_attrs);
+ return ret;
+}
+
+void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
+{
+ sysfs_remove_link(&dev->kobj, "mdev_type");
+ sysfs_remove_link(type->devices_kobj, dev_name(dev));
+ sysfs_remove_files(&dev->kobj, mdev_device_attrs);
+
+}
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
new file mode 100644
index 000000000000..727209b2a67f
--- /dev/null
+++ b/include/linux/mdev.h
@@ -0,0 +1,177 @@
+/*
+ * Mediated device definition
+ *
+ * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
+ * Author: Neo Jia <cjia@nvidia.com>
+ * Kirti Wankhede <kwankhede@nvidia.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef MDEV_H
+#define MDEV_H
+
+#include <uapi/linux/vfio.h>
+
+struct parent_device;
+
+/* Mediated device */
+struct mdev_device {
+ struct device dev;
+ struct parent_device *parent;
+ uuid_le uuid;
+ void *driver_data;
+
+ /* internal only */
+ struct kref ref;
+ struct list_head next;
+ struct kobject *type_kobj;
+};
+
+
+/**
+ * struct parent_ops - Structure to be registered for each parent device to
+ * register the device to mdev module.
+ *
+ * @owner: The module owner.
+ * @dev_attr_groups: Attributes of the parent device.
+ * @mdev_attr_groups: Attributes of the mediated device.
+ * @supported_type_groups: Attributes to define supported types. It is mandatory
+ * to provide supported types.
+ * @create: Called to allocate basic resources in parent device's
+ * driver for a particular mediated device. It is
+ * mandatory to provide create ops.
+ * @kobj: kobject of type for which 'create' is called.
+ * @mdev: mdev_device structure on of mediated device
+ * that is being created
+ * Returns integer: success (0) or error (< 0)
+ * @remove: Called to free resources in parent device's driver for a
+ * a mediated device. It is mandatory to provide 'remove'
+ * ops.
+ * @mdev: mdev_device device structure which is being
+ * destroyed
+ * Returns integer: success (0) or error (< 0)
+ * @open: Open mediated device.
+ * @mdev: mediated device.
+ * Returns integer: success (0) or error (< 0)
+ * @release: release mediated device
+ * @mdev: mediated device.
+ * @read: Read emulation callback
+ * @mdev: mediated device structure
+ * @buf: read buffer
+ * @count: number of bytes to read
+ * @ppos: address.
+ * Retuns number on bytes read on success or error.
+ * @write: Write emulation callback
+ * @mdev: mediated device structure
+ * @buf: write buffer
+ * @count: number of bytes to be written
+ * @ppos: address.
+ * Retuns number on bytes written on success or error.
+ * @ioctl: IOCTL callback
+ * @mdev: mediated device structure
+ * @cmd: mediated device structure
+ * @arg: mediated device structure
+ * @mmap: mmap callback
+ * Parent device that support mediated device should be registered with mdev
+ * module with parent_ops structure.
+ */
+
+struct parent_ops {
+ struct module *owner;
+ const struct attribute_group **dev_attr_groups;
+ const struct attribute_group **mdev_attr_groups;
+ struct attribute_group **supported_type_groups;
+
+ int (*create)(struct kobject *kobj, struct mdev_device *mdev);
+ int (*remove)(struct mdev_device *mdev);
+ int (*open)(struct mdev_device *mdev);
+ void (*release)(struct mdev_device *mdev);
+ ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
+ size_t count, loff_t *ppos);
+ ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
+ size_t count, loff_t *ppos);
+ ssize_t (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
+ unsigned long arg);
+ int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
+};
+
+/* Parent Device */
+struct parent_device {
+ struct device *dev;
+ const struct parent_ops *ops;
+
+ /* internal */
+ struct kref ref;
+ struct list_head next;
+ struct kset *mdev_types_kset;
+ struct list_head type_list;
+};
+
+/* interface for exporting mdev supported type attributes */
+struct mdev_type_attribute {
+ struct attribute attr;
+ ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
+ ssize_t (*store)(struct kobject *kobj, struct device *dev,
+ const char *buf, size_t count);
+};
+
+#define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \
+struct mdev_type_attribute mdev_type_attr_##_name = \
+ __ATTR(_name, _mode, _show, _store)
+#define MDEV_TYPE_ATTR_RW(_name) \
+ struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
+#define MDEV_TYPE_ATTR_RO(_name) \
+ struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
+#define MDEV_TYPE_ATTR_WO(_name) \
+ struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
+
+/**
+ * struct mdev_driver - Mediated device driver
+ * @name: driver name
+ * @probe: called when new device created
+ * @remove: called when device removed
+ * @driver: device driver structure
+ *
+ **/
+struct mdev_driver {
+ const char *name;
+ int (*probe)(struct device *dev);
+ void (*remove)(struct device *dev);
+ struct device_driver driver;
+};
+
+static inline struct mdev_driver *to_mdev_driver(struct device_driver *drv)
+{
+ return drv ? container_of(drv, struct mdev_driver, driver) : NULL;
+}
+
+static inline struct mdev_device *to_mdev_device(struct device *dev)
+{
+ return dev ? container_of(dev, struct mdev_device, dev) : NULL;
+}
+
+static inline void *mdev_get_drvdata(struct mdev_device *mdev)
+{
+ return mdev->driver_data;
+}
+
+static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
+{
+ mdev->driver_data = data;
+}
+
+extern struct bus_type mdev_bus_type;
+
+#define dev_is_mdev(d) ((d)->bus == &mdev_bus_type)
+
+extern int mdev_register_device(struct device *dev,
+ const struct parent_ops *ops);
+extern void mdev_unregister_device(struct device *dev);
+
+extern int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
+extern void mdev_unregister_driver(struct mdev_driver *drv);
+
+#endif /* MDEV_H */
--
2.7.0
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | linux.kernel
csiph-web