[PATCH] physmem: Simplify dirty memory type checks with loop

Bin Guo posted 1 patch 10 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260401100005.20651-1-guobin@linux.alibaba.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
system/physmem.c | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
[PATCH] physmem: Simplify dirty memory type checks with loop
Posted by Bin Guo 10 hours ago
In physical_memory_range_includes_clean(), we have three nearly identical
if-statements checking different DIRTY_MEMORY types (VGA, CODE, MIGRATION).
This code duplication makes maintenance harder and increases the risk of
inconsistencies when adding new dirty memory types.

Replace the repetitive checks with a simple loop that iterates through
all DIRTY_MEMORY_NUM types, checking only those specified in the mask.
This reduces code size and makes it easier to add new dirty memory types
in the future.

Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
---
 system/physmem.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/system/physmem.c b/system/physmem.c
index 4e26f1a1d4..c58d940e80 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -981,17 +981,11 @@ uint8_t physical_memory_range_includes_clean(ram_addr_t start,
 {
     uint8_t ret = 0;
 
-    if (mask & (1 << DIRTY_MEMORY_VGA) &&
-        !physical_memory_all_dirty(start, length, DIRTY_MEMORY_VGA)) {
-        ret |= (1 << DIRTY_MEMORY_VGA);
-    }
-    if (mask & (1 << DIRTY_MEMORY_CODE) &&
-        !physical_memory_all_dirty(start, length, DIRTY_MEMORY_CODE)) {
-        ret |= (1 << DIRTY_MEMORY_CODE);
-    }
-    if (mask & (1 << DIRTY_MEMORY_MIGRATION) &&
-        !physical_memory_all_dirty(start, length, DIRTY_MEMORY_MIGRATION)) {
-        ret |= (1 << DIRTY_MEMORY_MIGRATION);
+    for (int i = 0; i < DIRTY_MEMORY_NUM; i++) {
+        if ((mask & (1 << i)) &&
+            !physical_memory_all_dirty(start, length, i)) {
+            ret |= (1 << i);
+        }
     }
     return ret;
 }
-- 
2.50.1 (Apple Git-155)
Re: [PATCH] physmem: Simplify dirty memory type checks with loop
Posted by Peter Xu 5 hours ago
On Wed, Apr 01, 2026 at 06:00:05PM +0800, Bin Guo wrote:
> In physical_memory_range_includes_clean(), we have three nearly identical
> if-statements checking different DIRTY_MEMORY types (VGA, CODE, MIGRATION).
> This code duplication makes maintenance harder and increases the risk of
> inconsistencies when adding new dirty memory types.
> 
> Replace the repetitive checks with a simple loop that iterates through
> all DIRTY_MEMORY_NUM types, checking only those specified in the mask.
> This reduces code size and makes it easier to add new dirty memory types
> in the future.
> 
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>

Reviewed-by: Peter Xu <peterx@redhat.com>

-- 
Peter Xu
Re: [PATCH] physmem: Simplify dirty memory type checks with loop
Posted by Philippe Mathieu-Daudé 10 hours ago
On 1/4/26 12:00, Bin Guo wrote:
> In physical_memory_range_includes_clean(), we have three nearly identical
> if-statements checking different DIRTY_MEMORY types (VGA, CODE, MIGRATION).
> This code duplication makes maintenance harder and increases the risk of
> inconsistencies when adding new dirty memory types.
> 
> Replace the repetitive checks with a simple loop that iterates through
> all DIRTY_MEMORY_NUM types, checking only those specified in the mask.
> This reduces code size and makes it easier to add new dirty memory types
> in the future.
> 
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
> ---
>   system/physmem.c | 16 +++++-----------
>   1 file changed, 5 insertions(+), 11 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>