[PATCH v12 06/10] vfio/region: Add a helper to get VFIO region from memory region

Vivek Kasireddy posted 10 patches 2 weeks, 4 days ago
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, "Michael S. Tsirkin" <mst@redhat.com>, Alex Williamson <alex@shazbot.org>, "Cédric Le Goater" <clg@redhat.com>
[PATCH v12 06/10] vfio/region: Add a helper to get VFIO region from memory region
Posted by Vivek Kasireddy 2 weeks, 4 days ago
Having a way to get the VFIO region associated with a memory region
is helpful in various scenarios. For example, this capability can
be useful in retrieving the region info such as index and offset
needed for mapping a part of a VFIO region or creating a dmabuf.

Cc: Alex Williamson <alex@shazbot.org>
Cc: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
 hw/vfio/vfio-region.h | 10 ++++++++++
 hw/vfio/region.c      | 11 +++++++++++
 2 files changed, 21 insertions(+)

diff --git a/hw/vfio/vfio-region.h b/hw/vfio/vfio-region.h
index 9b21d4ee5b..3dd47f77f6 100644
--- a/hw/vfio/vfio-region.h
+++ b/hw/vfio/vfio-region.h
@@ -45,4 +45,14 @@ void vfio_region_unmap(VFIORegion *region);
 void vfio_region_exit(VFIORegion *region);
 void vfio_region_finalize(VFIORegion *region);
 
+/**
+ * Return the VFIO region associated with a given MemoryRegion. This can
+ * be useful in retrieving region info such as index and offset.
+ *
+ * @mr: MemoryRegion to use
+ *
+ * Returns the region or NULL on error.
+ */
+void *vfio_get_region_from_mr(MemoryRegion *mr);
+
 #endif /* HW_VFIO_REGION_H */
diff --git a/hw/vfio/region.c b/hw/vfio/region.c
index 47fdc2df34..9d7ac339c9 100644
--- a/hw/vfio/region.c
+++ b/hw/vfio/region.c
@@ -539,3 +539,14 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
     trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
                                         enabled);
 }
+
+void *vfio_get_region_from_mr(MemoryRegion *mr)
+{
+    while (mr->container) {
+        if (mr->ops == &vfio_region_ops) {
+            return mr->opaque;
+        }
+        mr = mr->container;
+    }
+    return NULL;
+}
-- 
2.53.0


Re: [PATCH v12 06/10] vfio/region: Add a helper to get VFIO region from memory region
Posted by Cédric Le Goater 1 week, 6 days ago
On 3/19/26 06:15, Vivek Kasireddy wrote:
> Having a way to get the VFIO region associated with a memory region
> is helpful in various scenarios. For example, this capability can
> be useful in retrieving the region info such as index and offset
> needed for mapping a part of a VFIO region or creating a dmabuf.
> 
> Cc: Alex Williamson <alex@shazbot.org>
> Cc: Cédric Le Goater <clg@redhat.com>
> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
>   hw/vfio/vfio-region.h | 10 ++++++++++
>   hw/vfio/region.c      | 11 +++++++++++
>   2 files changed, 21 insertions(+)
> 
> diff --git a/hw/vfio/vfio-region.h b/hw/vfio/vfio-region.h
> index 9b21d4ee5b..3dd47f77f6 100644
> --- a/hw/vfio/vfio-region.h
> +++ b/hw/vfio/vfio-region.h
> @@ -45,4 +45,14 @@ void vfio_region_unmap(VFIORegion *region);
>   void vfio_region_exit(VFIORegion *region);
>   void vfio_region_finalize(VFIORegion *region);
>   
> +/**
> + * Return the VFIO region associated with a given MemoryRegion. This can
> + * be useful in retrieving region info such as index and offset.
> + *
> + * @mr: MemoryRegion to use
> + *
> + * Returns the region or NULL on error.
> + */
> +void *vfio_get_region_from_mr(MemoryRegion *mr);
> +
>   #endif /* HW_VFIO_REGION_H */
> diff --git a/hw/vfio/region.c b/hw/vfio/region.c
> index 47fdc2df34..9d7ac339c9 100644
> --- a/hw/vfio/region.c
> +++ b/hw/vfio/region.c
> @@ -539,3 +539,14 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
>       trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
>                                           enabled);
>   }
> +
> +void *vfio_get_region_from_mr(MemoryRegion *mr)

can we return a "VFIORegion *" ?

Thanks,

C.

> +{
> +    while (mr->container) {
> +        if (mr->ops == &vfio_region_ops) {
> +            return mr->opaque;
> +        }
> +        mr = mr->container;
> +    }
> +    return NULL;
> +}


RE: [PATCH v12 06/10] vfio/region: Add a helper to get VFIO region from memory region
Posted by Kasireddy, Vivek 1 week, 6 days ago
> Subject: Re: [PATCH v12 06/10] vfio/region: Add a helper to get VFIO
> region from memory region
> 
> On 3/19/26 06:15, Vivek Kasireddy wrote:
> > Having a way to get the VFIO region associated with a memory region
> > is helpful in various scenarios. For example, this capability can
> > be useful in retrieving the region info such as index and offset
> > needed for mapping a part of a VFIO region or creating a dmabuf.
> >
> > Cc: Alex Williamson <alex@shazbot.org>
> > Cc: Cédric Le Goater <clg@redhat.com>
> > Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> > ---
> >   hw/vfio/vfio-region.h | 10 ++++++++++
> >   hw/vfio/region.c      | 11 +++++++++++
> >   2 files changed, 21 insertions(+)
> >
> > diff --git a/hw/vfio/vfio-region.h b/hw/vfio/vfio-region.h
> > index 9b21d4ee5b..3dd47f77f6 100644
> > --- a/hw/vfio/vfio-region.h
> > +++ b/hw/vfio/vfio-region.h
> > @@ -45,4 +45,14 @@ void vfio_region_unmap(VFIORegion *region);
> >   void vfio_region_exit(VFIORegion *region);
> >   void vfio_region_finalize(VFIORegion *region);
> >
> > +/**
> > + * Return the VFIO region associated with a given MemoryRegion.
> This can
> > + * be useful in retrieving region info such as index and offset.
> > + *
> > + * @mr: MemoryRegion to use
> > + *
> > + * Returns the region or NULL on error.
> > + */
> > +void *vfio_get_region_from_mr(MemoryRegion *mr);
> > +
> >   #endif /* HW_VFIO_REGION_H */
> > diff --git a/hw/vfio/region.c b/hw/vfio/region.c
> > index 47fdc2df34..9d7ac339c9 100644
> > --- a/hw/vfio/region.c
> > +++ b/hw/vfio/region.c
> > @@ -539,3 +539,14 @@ void
> vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
> >
> trace_vfio_region_mmaps_set_enabled(memory_region_name(region-
> >mem),
> >                                           enabled);
> >   }
> > +
> > +void *vfio_get_region_from_mr(MemoryRegion *mr)
> 
> can we return a "VFIORegion *" ?
Sure, I'll make the change.

Thanks,
Vivek

> 
> Thanks,
> 
> C.
> 
> > +{
> > +    while (mr->container) {
> > +        if (mr->ops == &vfio_region_ops) {
> > +            return mr->opaque;
> > +        }
> > +        mr = mr->container;
> > +    }
> > +    return NULL;
> > +}