[PATCH V8 12/39] memory: flat section iterator

Steve Sistare posted 39 patches 3 years, 7 months ago
Maintainers: Stefano Stabellini <sstabellini@kernel.org>, Anthony Perard <anthony.perard@citrix.com>, Paul Durrant <paul@xen.org>, David Hildenbrand <david@redhat.com>, Igor Mammedov <imammedo@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, "Philippe Mathieu-Daudé" <f4bug@amsat.org>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Alex Williamson <alex.williamson@redhat.com>, Steve Sistare <steven.sistare@oracle.com>, Mark Kanda <mark.kanda@oracle.com>, Peter Xu <peterx@redhat.com>, Juan Quintela <quintela@redhat.com>, Markus Armbruster <armbru@redhat.com>, Michael Roth <michael.roth@amd.com>, John Snow <jsnow@redhat.com>, Cleber Rosa <crosa@redhat.com>, Beraldo Leal <bleal@redhat.com>, Eric Blake <eblake@redhat.com>, Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>, Wainer dos Santos Moschetta <wainersm@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Stefan Weil <sw@weilnetz.de>
There is a newer version of this series
[PATCH V8 12/39] memory: flat section iterator
Posted by Steve Sistare 3 years, 7 months ago
Add an iterator over the sections of a flattened address space.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/exec/memory.h | 31 +++++++++++++++++++++++++++++++
 softmmu/memory.c      | 20 ++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/include/exec/memory.h b/include/exec/memory.h
index a03301d..6a257a4 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -2343,6 +2343,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
                                            RamDiscardManager *rdm);
 
 /**
+ * memory_region_section_cb: callback for address_space_flat_for_each_section()
+ *
+ * @mrs: MemoryRegionSection of the range
+ * @opaque: data pointer passed to address_space_flat_for_each_section()
+ * @errp: error message, returned to the address_space_flat_for_each_section
+ *        caller.
+ *
+ * Returns: non-zero to stop the iteration, and 0 to continue.  The same
+ * non-zero value is returned to the address_space_flat_for_each_section caller.
+ */
+
+typedef int (*memory_region_section_cb)(MemoryRegionSection *mrs,
+                                        void *opaque,
+                                        Error **errp);
+
+/**
+ * address_space_flat_for_each_section: walk the ranges in the address space
+ * flat view and call @func for each.  Return 0 on success, else return non-zero
+ * with a message in @errp.
+ *
+ * @as: target address space
+ * @func: callback function
+ * @opaque: passed to @func
+ * @errp: passed to @func
+ */
+int address_space_flat_for_each_section(AddressSpace *as,
+                                        memory_region_section_cb func,
+                                        void *opaque,
+                                        Error **errp);
+
+/**
  * memory_region_find: translate an address/size relative to a
  * MemoryRegion into a #MemoryRegionSection.
  *
diff --git a/softmmu/memory.c b/softmmu/memory.c
index 0fe6fac..e5aefdd 100644
--- a/softmmu/memory.c
+++ b/softmmu/memory.c
@@ -2683,6 +2683,26 @@ bool memory_region_is_mapped(MemoryRegion *mr)
     return !!mr->container || mr->mapped_via_alias;
 }
 
+int address_space_flat_for_each_section(AddressSpace *as,
+                                        memory_region_section_cb func,
+                                        void *opaque,
+                                        Error **errp)
+{
+    FlatView *view = address_space_get_flatview(as);
+    FlatRange *fr;
+    int ret;
+
+    FOR_EACH_FLAT_RANGE(fr, view) {
+        MemoryRegionSection mrs = section_from_flat_range(fr, view);
+        ret = func(&mrs, opaque, errp);
+        if (ret) {
+            return ret;
+        }
+    }
+
+    return 0;
+}
+
 /* Same as memory_region_find, but it does not add a reference to the
  * returned region.  It must be called from an RCU critical section.
  */
-- 
1.8.3.1


Re: [PATCH V8 12/39] memory: flat section iterator
Posted by Peng Liang 3 years, 7 months ago

On 6/15/2022 10:51 PM, Steve Sistare wrote:
> Add an iterator over the sections of a flattened address space.
> 
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  include/exec/memory.h | 31 +++++++++++++++++++++++++++++++
>  softmmu/memory.c      | 20 ++++++++++++++++++++
>  2 files changed, 51 insertions(+)
> 
> diff --git a/include/exec/memory.h b/include/exec/memory.h
> index a03301d..6a257a4 100644
> --- a/include/exec/memory.h
> +++ b/include/exec/memory.h
> @@ -2343,6 +2343,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
>                                             RamDiscardManager *rdm);
>  
>  /**
> + * memory_region_section_cb: callback for address_space_flat_for_each_section()
> + *
> + * @mrs: MemoryRegionSection of the range
> + * @opaque: data pointer passed to address_space_flat_for_each_section()
> + * @errp: error message, returned to the address_space_flat_for_each_section
> + *        caller.
> + *
> + * Returns: non-zero to stop the iteration, and 0 to continue.  The same
> + * non-zero value is returned to the address_space_flat_for_each_section caller.
> + */
> +
> +typedef int (*memory_region_section_cb)(MemoryRegionSection *mrs,
> +                                        void *opaque,
> +                                        Error **errp);
> +
> +/**
> + * address_space_flat_for_each_section: walk the ranges in the address space
> + * flat view and call @func for each.  Return 0 on success, else return non-zero
> + * with a message in @errp.
> + *
> + * @as: target address space
> + * @func: callback function
> + * @opaque: passed to @func
> + * @errp: passed to @func
> + */
> +int address_space_flat_for_each_section(AddressSpace *as,
> +                                        memory_region_section_cb func,
> +                                        void *opaque,
> +                                        Error **errp);
> +
> +/**
>   * memory_region_find: translate an address/size relative to a
>   * MemoryRegion into a #MemoryRegionSection.
>   *
> diff --git a/softmmu/memory.c b/softmmu/memory.c
> index 0fe6fac..e5aefdd 100644
> --- a/softmmu/memory.c
> +++ b/softmmu/memory.c
> @@ -2683,6 +2683,26 @@ bool memory_region_is_mapped(MemoryRegion *mr)
>      return !!mr->container || mr->mapped_via_alias;
>  }
>  
> +int address_space_flat_for_each_section(AddressSpace *as,
> +                                        memory_region_section_cb func,
> +                                        void *opaque,
> +                                        Error **errp)
> +{
> +    FlatView *view = address_space_get_flatview(as);
> +    FlatRange *fr;
> +    int ret;
> +
> +    FOR_EACH_FLAT_RANGE(fr, view) {
> +        MemoryRegionSection mrs = section_from_flat_range(fr, view);
> +        ret = func(&mrs, opaque, errp);
> +        if (ret) {
> +            return ret;
> +        }
> +    }
> +

Hi Steve,
I guess a flatview_unref(view) is missing here? Because the return value
of address_space_get_flatview has been flatview_ref.

> +    return 0;
> +}
> +
>  /* Same as memory_region_find, but it does not add a reference to the
>   * returned region.  It must be called from an RCU critical section.
>   */

Re: [PATCH V8 12/39] memory: flat section iterator
Posted by Steven Sistare 3 years, 7 months ago
On 7/3/2022 3:52 AM, Peng Liang wrote:
> On 6/15/2022 10:51 PM, Steve Sistare wrote:
>> Add an iterator over the sections of a flattened address space.
>>
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
>> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>> ---
>>  include/exec/memory.h | 31 +++++++++++++++++++++++++++++++
>>  softmmu/memory.c      | 20 ++++++++++++++++++++
>>  2 files changed, 51 insertions(+)
>>
>> diff --git a/include/exec/memory.h b/include/exec/memory.h
>> index a03301d..6a257a4 100644
>> --- a/include/exec/memory.h
>> +++ b/include/exec/memory.h
>> @@ -2343,6 +2343,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
>>                                             RamDiscardManager *rdm);
>>  
>>  /**
>> + * memory_region_section_cb: callback for address_space_flat_for_each_section()
>> + *
>> + * @mrs: MemoryRegionSection of the range
>> + * @opaque: data pointer passed to address_space_flat_for_each_section()
>> + * @errp: error message, returned to the address_space_flat_for_each_section
>> + *        caller.
>> + *
>> + * Returns: non-zero to stop the iteration, and 0 to continue.  The same
>> + * non-zero value is returned to the address_space_flat_for_each_section caller.
>> + */
>> +
>> +typedef int (*memory_region_section_cb)(MemoryRegionSection *mrs,
>> +                                        void *opaque,
>> +                                        Error **errp);
>> +
>> +/**
>> + * address_space_flat_for_each_section: walk the ranges in the address space
>> + * flat view and call @func for each.  Return 0 on success, else return non-zero
>> + * with a message in @errp.
>> + *
>> + * @as: target address space
>> + * @func: callback function
>> + * @opaque: passed to @func
>> + * @errp: passed to @func
>> + */
>> +int address_space_flat_for_each_section(AddressSpace *as,
>> +                                        memory_region_section_cb func,
>> +                                        void *opaque,
>> +                                        Error **errp);
>> +
>> +/**
>>   * memory_region_find: translate an address/size relative to a
>>   * MemoryRegion into a #MemoryRegionSection.
>>   *
>> diff --git a/softmmu/memory.c b/softmmu/memory.c
>> index 0fe6fac..e5aefdd 100644
>> --- a/softmmu/memory.c
>> +++ b/softmmu/memory.c
>> @@ -2683,6 +2683,26 @@ bool memory_region_is_mapped(MemoryRegion *mr)
>>      return !!mr->container || mr->mapped_via_alias;
>>  }
>>  
>> +int address_space_flat_for_each_section(AddressSpace *as,
>> +                                        memory_region_section_cb func,
>> +                                        void *opaque,
>> +                                        Error **errp)
>> +{
>> +    FlatView *view = address_space_get_flatview(as);
>> +    FlatRange *fr;
>> +    int ret;
>> +
>> +    FOR_EACH_FLAT_RANGE(fr, view) {
>> +        MemoryRegionSection mrs = section_from_flat_range(fr, view);
>> +        ret = func(&mrs, opaque, errp);
>> +        if (ret) {
>> +            return ret;
>> +        }
>> +    }
>> +
> 
> Hi Steve,
> I guess a flatview_unref(view) is missing here? Because the return value
> of address_space_get_flatview has been flatview_ref.

Yes!  Good catch, will fix, thanks - Steve

>> +    return 0;
>> +}
>> +
>>  /* Same as memory_region_find, but it does not add a reference to the
>>   * returned region.  It must be called from an RCU critical section.
>>   */