Having a way to figure out the region index (or bar) associated
with a memory region is helpful in various scenarios. For example,
this capability can be useful in retrieving the region info 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/region.c | 14 ++++++++++++++
include/hw/vfio/vfio-device.h | 10 ++++++++++
2 files changed, 24 insertions(+)
diff --git a/hw/vfio/region.c b/hw/vfio/region.c
index 47fdc2df34..4cb92216ef 100644
--- a/hw/vfio/region.c
+++ b/hw/vfio/region.c
@@ -539,3 +539,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
enabled);
}
+
+int vfio_get_region_index_from_mr(MemoryRegion *mr)
+{
+ VFIORegion *region;
+
+ while (mr->container) {
+ if (mr->ops == &vfio_region_ops) {
+ region = mr->opaque;
+ return region->nr;
+ }
+ mr = mr->container;
+ }
+ return -1;
+}
diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
index 828a31c006..596c7f5a10 100644
--- a/include/hw/vfio/vfio-device.h
+++ b/include/hw/vfio/vfio-device.h
@@ -308,6 +308,16 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t
int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
struct vfio_irq_info *info);
+
+/**
+ * Return the region index associated with a given MemoryRegion. The index
+ * can be useful in retrieving region info.
+ *
+ * @mr: MemoryRegion to use
+ *
+ * Returns the region index or -1 on error.
+ */
+int vfio_get_region_index_from_mr(MemoryRegion *mr);
#endif
/* Returns 0 on success, or a negative errno. */
--
2.53.0
On 3/11/26 03:50, Vivek Kasireddy wrote:
> Having a way to figure out the region index (or bar) associated
> with a memory region is helpful in various scenarios. For example,
> this capability can be useful in retrieving the region info 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/region.c | 14 ++++++++++++++
> include/hw/vfio/vfio-device.h | 10 ++++++++++
> 2 files changed, 24 insertions(+)
>
> diff --git a/hw/vfio/region.c b/hw/vfio/region.c
> index 47fdc2df34..4cb92216ef 100644
> --- a/hw/vfio/region.c
> +++ b/hw/vfio/region.c
> @@ -539,3 +539,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
> trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
> enabled);
> }
> +
> +int vfio_get_region_index_from_mr(MemoryRegion *mr)
I think you can move this routine as a static in device.c now. No need
to export a service.
Thanks,
C.
> +{
> + VFIORegion *region;
> +
> + while (mr->container) {
> + if (mr->ops == &vfio_region_ops) {
> + region = mr->opaque;
> + return region->nr;
> + }
> + mr = mr->container;
> + }
> + return -1;
> +}
> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
> index 828a31c006..596c7f5a10 100644
> --- a/include/hw/vfio/vfio-device.h
> +++ b/include/hw/vfio/vfio-device.h
> @@ -308,6 +308,16 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t
>
> int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
> struct vfio_irq_info *info);
> +
> +/**
> + * Return the region index associated with a given MemoryRegion. The index
> + * can be useful in retrieving region info.
> + *
> + * @mr: MemoryRegion to use
> + *
> + * Returns the region index or -1 on error.
> + */
> +int vfio_get_region_index_from_mr(MemoryRegion *mr);
> #endif
>
> /* Returns 0 on success, or a negative errno. */
On 3/17/26 15:41, Cédric Le Goater wrote:
> On 3/11/26 03:50, Vivek Kasireddy wrote:
>> Having a way to figure out the region index (or bar) associated
>> with a memory region is helpful in various scenarios. For example,
>> this capability can be useful in retrieving the region info 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/region.c | 14 ++++++++++++++
>> include/hw/vfio/vfio-device.h | 10 ++++++++++
>> 2 files changed, 24 insertions(+)
>>
>> diff --git a/hw/vfio/region.c b/hw/vfio/region.c
>> index 47fdc2df34..4cb92216ef 100644
>> --- a/hw/vfio/region.c
>> +++ b/hw/vfio/region.c
>> @@ -539,3 +539,17 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
>> trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem),
>> enabled);
>> }
>> +
>> +int vfio_get_region_index_from_mr(MemoryRegion *mr)
>
>
> I think you can move this routine as a static in device.c now. No need
> to export a service.
you can't because of vfio_region_ops.
Returning a VFIORegion seems cleaner though.
Thanks,
C.
>
> Thanks,
>
> C.
>
>> +{
>> + VFIORegion *region;
>> +
>> + while (mr->container) {
>> + if (mr->ops == &vfio_region_ops) {
>> + region = mr->opaque;
>> + return region->nr;
>> + }
>> + mr = mr->container;
>> + }
>> + return -1;
>> +}
>> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
>> index 828a31c006..596c7f5a10 100644
>> --- a/include/hw/vfio/vfio-device.h
>> +++ b/include/hw/vfio/vfio-device.h
>> @@ -308,6 +308,16 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t
>> int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
>> struct vfio_irq_info *info);
>> +
>> +/**
>> + * Return the region index associated with a given MemoryRegion. The index
>> + * can be useful in retrieving region info.
>> + *
>> + * @mr: MemoryRegion to use
>> + *
>> + * Returns the region index or -1 on error.
>> + */
>> +int vfio_get_region_index_from_mr(MemoryRegion *mr);
>> #endif
>> /* Returns 0 on success, or a negative errno. */
>
> Subject: Re: [PATCH v11 06/10] vfio/region: Add a helper to get region
> index from memory region
>
> On 3/17/26 15:41, Cédric Le Goater wrote:
> > On 3/11/26 03:50, Vivek Kasireddy wrote:
> >> Having a way to figure out the region index (or bar) associated
> >> with a memory region is helpful in various scenarios. For example,
> >> this capability can be useful in retrieving the region info 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/region.c | 14 ++++++++++++++
> >> include/hw/vfio/vfio-device.h | 10 ++++++++++
> >> 2 files changed, 24 insertions(+)
> >>
> >> diff --git a/hw/vfio/region.c b/hw/vfio/region.c
> >> index 47fdc2df34..4cb92216ef 100644
> >> --- a/hw/vfio/region.c
> >> +++ b/hw/vfio/region.c
> >> @@ -539,3 +539,17 @@ void
> vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled)
> >> trace_vfio_region_mmaps_set_enabled(memory_region_name(re
> gion->mem),
> >> enabled);
> >> }
> >> +
> >> +int vfio_get_region_index_from_mr(MemoryRegion *mr)
> >
> >
> > I think you can move this routine as a static in device.c now. No need
> > to export a service.
>
> you can't because of vfio_region_ops.
>
> Returning a VFIORegion seems cleaner though.
I agree. Will make the change in the next version.
Thanks,
Vivek
>
> Thanks,
>
> C.
>
>
> >
> > Thanks,
> >
> > C.
> >
> >> +{
> >> + VFIORegion *region;
> >> +
> >> + while (mr->container) {
> >> + if (mr->ops == &vfio_region_ops) {
> >> + region = mr->opaque;
> >> + return region->nr;
> >> + }
> >> + mr = mr->container;
> >> + }
> >> + return -1;
> >> +}
> >> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-
> device.h
> >> index 828a31c006..596c7f5a10 100644
> >> --- a/include/hw/vfio/vfio-device.h
> >> +++ b/include/hw/vfio/vfio-device.h
> >> @@ -308,6 +308,16 @@ bool
> vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t
> cap_t
> >> int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
> >> struct vfio_irq_info *info);
> >> +
> >> +/**
> >> + * Return the region index associated with a given MemoryRegion.
> The index
> >> + * can be useful in retrieving region info.
> >> + *
> >> + * @mr: MemoryRegion to use
> >> + *
> >> + * Returns the region index or -1 on error.
> >> + */
> >> +int vfio_get_region_index_from_mr(MemoryRegion *mr);
> >> #endif
> >> /* Returns 0 on success, or a negative errno. */
> >
© 2016 - 2026 Red Hat, Inc.