[PATCH V7 08/29] memory: flat section iterator

Steve Sistare posted 29 patches 4 years, 1 month ago
There is a newer version of this series
[PATCH V7 08/29] memory: flat section iterator
Posted by Steve Sistare 4 years, 1 month ago
Add an iterator over the sections of a flattened address space.

Signed-off-by: Steve Sistare <steven.sistare@oracle.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 137f5f3..9660475 100644
--- a/include/exec/memory.h
+++ b/include/exec/memory.h
@@ -2338,6 +2338,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
                                            RamDiscardManager *rdm);
 
 /**
+ * memory_region_section_cb: callback for address_space_flat_for_each_section()
+ *
+ * @s: 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 *s,
+                                        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 30b2f68..40f3522 100644
--- a/softmmu/memory.c
+++ b/softmmu/memory.c
@@ -2663,6 +2663,26 @@ bool memory_region_is_mapped(MemoryRegion *mr)
     return mr->container ? true : false;
 }
 
+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 section = section_from_flat_range(fr, view);
+        ret = func(&section, 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 V7 08/29] memory: flat section iterator
Posted by Marc-André Lureau 3 years, 11 months ago
Hi

On Thu, Dec 23, 2021 at 12:17 AM Steve Sistare <steven.sistare@oracle.com>
wrote:

> Add an iterator over the sections of a flattened address space.
>
> Signed-off-by: Steve Sistare <steven.sistare@oracle.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 137f5f3..9660475 100644
> --- a/include/exec/memory.h
> +++ b/include/exec/memory.h
> @@ -2338,6 +2338,37 @@ void
> memory_region_set_ram_discard_manager(MemoryRegion *mr,
>                                             RamDiscardManager *rdm);
>
>  /**
> + * memory_region_section_cb: callback for
> address_space_flat_for_each_section()
> + *
> + * @s: 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 *s,
> +                                        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 30b2f68..40f3522 100644
> --- a/softmmu/memory.c
> +++ b/softmmu/memory.c
> @@ -2663,6 +2663,26 @@ bool memory_region_is_mapped(MemoryRegion *mr)
>      return mr->container ? true : false;
>  }
>
> +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 section = section_from_flat_range(fr, view);
> +        ret = func(&section, 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
>
>
>
lgtm,

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


-- 
Marc-André Lureau
Re: [PATCH V7 08/29] memory: flat section iterator
Posted by Philippe Mathieu-Daudé 3 years, 11 months ago
On 22/12/21 20:05, Steve Sistare wrote:
> Add an iterator over the sections of a flattened address space.
> 
> Signed-off-by: Steve Sistare <steven.sistare@oracle.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 137f5f3..9660475 100644
> --- a/include/exec/memory.h
> +++ b/include/exec/memory.h
> @@ -2338,6 +2338,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
>                                              RamDiscardManager *rdm);
>   
>   /**
> + * memory_region_section_cb: callback for address_space_flat_for_each_section()
> + *
> + * @s: MemoryRegionSection of the range

Nitpicking, can we name this @mrs?

> + * @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 *s,
> +                                        void *opaque,
> +                                        Error **errp);
Re: [PATCH V7 08/29] memory: flat section iterator
Posted by Steven Sistare 3 years, 11 months ago
On 3/4/2022 7:48 AM, Philippe Mathieu-Daudé wrote:
> On 22/12/21 20:05, Steve Sistare wrote:
>> Add an iterator over the sections of a flattened address space.
>>
>> Signed-off-by: Steve Sistare <steven.sistare@oracle.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 137f5f3..9660475 100644
>> --- a/include/exec/memory.h
>> +++ b/include/exec/memory.h
>> @@ -2338,6 +2338,37 @@ void memory_region_set_ram_discard_manager(MemoryRegion *mr,
>>                                              RamDiscardManager *rdm);
>>     /**
>> + * memory_region_section_cb: callback for address_space_flat_for_each_section()
>> + *
>> + * @s: MemoryRegionSection of the range
> 
> Nitpicking, can we name this @mrs?

Sure thing - Steve

>> + * @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 *s,
>> +                                        void *opaque,
>> +                                        Error **errp);