Skip to contentSkip to conversation

CPUPhysMemoryClient: Fixes and batching

PATCH Applied Alex Williamson 3 participants 16 messages May 3, 2011 – May 25, 2011

lore
Share & tools
lore
#0 lore raw

v2 cover CPUPhysMemoryClient: Fixes and batching

This series pulls together several related patches for bugs and performance that I found last week. Only the 2nd patch is actually modified from inital posting, adding the comments suggested by Markus. The 1st two patches fix pretty serious brokeness in the CPUPhysMemoryClient interface. Of the two current users, kvm and vhost, only vhost is actually affected by these bugs. Please apply. Thanks,

Alex

---

Alex Williamson (3):
CPUPhysMemoryClient: Batch contiguous addresses when playing catchup
CPUPhysMemoryClient: Pass guest physical address not region offset
CPUPhysMemoryClient: Fix typo in phys memory client registration

 exec.c |   46 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 40 insertions(+), 6 deletions(-)
#1 lore raw

PATCH v2 1/3 CPUPhysMemoryClient: Fix typo in phys memory client registration

When we register a physical memory client, we try to walk the page tables, calling the set_memory hook for every entry. Effectively playing catchup for the client for everything already registered. With this type, we only walk the 2nd entry of the l1 table, typically missing all of the registered memory.

---

 exec.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
exec.c +1 -1
index c3dc68a..8790ad8 100644
@@ -1770,7 +1770,7 @@ static void phys_page_for_each(CPUPhysMemoryClient *client)
     int i;
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + 1);
+                             l1_phys_map + i);
     }
 }
 
#2 lore raw

PATCH v2 2/3 CPUPhysMemoryClient: Pass guest physical address not region offset

In reply to Alex Williamson

When we're trying to get a newly registered phys memory client updated with the current page mappings, we end up passing the region offset (a ram_addr_t) as the start address rather than the actual guest physical memory address (target_phys_addr_t). If your guest has less than 3.5G of memory, these are coincidentally the same thing. If there's more, the region offset for the memory above 4G starts over at 0, so the set_memory client will overwrite it's lower memory entries.

Instead, keep track of the guest phsyical address as we're walking the tables and pass that to the set_memory client.

---

 exec.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)
exec.c +12 -4
index 8790ad8..bbd5c86 100644
@@ -1741,8 +1741,14 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
+/* The l1_phys_map provides the upper P_L1_BITs of the guest physical
+ * address.  Each intermediate table provides the next L2_BITs of guest
+ * physical address space.  The number of levels vary based on host and
+ * guest configuration, making it efficient to build the final guest
+ * physical address by seeding the L1 offset and shifting and adding in
+ * each L2 offset as we recurse through them. */
 static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp)
+                                 int level, void **lp, target_phys_addr_t addr)
 {
     int i;
 
@@ -1751,16 +1757,18 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
     }
     if (level == 0) {
         PhysPageDesc *pd = *lp;
+        addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, pd[i].region_offset,
+                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
                                    TARGET_PAGE_SIZE, pd[i].phys_offset);
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
-            phys_page_for_each_1(client, level - 1, pp + i);
+            phys_page_for_each_1(client, level - 1, pp + i,
+                                 (addr << L2_BITS) | i);
         }
     }
 }
@@ -1770,7 +1778,7 @@ static void phys_page_for_each(CPUPhysMemoryClient *client)
     int i;
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i);
+                             l1_phys_map + i, i);
     }
 }
 
#3 lore raw

PATCH v2 3/3 CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

In reply to Alex Williamson

When a phys memory client registers and we play catchup by walking the page tables, we can make a huge improvement in the number of times the set_memory callback is called by batching contiguous pages together. With a 4G guest, this reduces the number of callbacks at registration from 1048866 to 296.

---

 exec.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
exec.c +32 -6
index bbd5c86..a0678a4 100644
@@ -1741,14 +1741,21 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
+struct last_map {
+    target_phys_addr_t start_addr;
+    ram_addr_t size;
+    ram_addr_t phys_offset;
+};
+
 /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  * address.  Each intermediate table provides the next L2_BITs of guest
  * physical address space.  The number of levels vary based on host and
  * guest configuration, making it efficient to build the final guest
  * physical address by seeding the L1 offset and shifting and adding in
  * each L2 offset as we recurse through them. */
-static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp, target_phys_addr_t addr)
+static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
+                                 void **lp, target_phys_addr_t addr,
+                                 struct last_map *map)
 {
     int i;
 
@@ -1760,15 +1767,29 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
         addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
-                                   TARGET_PAGE_SIZE, pd[i].phys_offset);
+                target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
+
+                if (map->size &&
+                    start_addr == map->start_addr + map->size &&
+                    pd[i].phys_offset == map->phys_offset + map->size) {
+
+                    map->size += TARGET_PAGE_SIZE;
+                    continue;
+                } else if (map->size) {
+                    client->set_memory(client, map->start_addr,
+                                       map->size, map->phys_offset);
+                }
+
+                map->start_addr = start_addr;
+                map->size = TARGET_PAGE_SIZE;
+                map->phys_offset = pd[i].phys_offset;
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
             phys_page_for_each_1(client, level - 1, pp + i,
-                                 (addr << L2_BITS) | i);
+                                 (addr << L2_BITS) | i, map);
         }
     }
 }
@@ -1776,9 +1797,14 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
 static void phys_page_for_each(CPUPhysMemoryClient *client)
 {
     int i;
+    struct last_map map = { 0 };
+
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i, i);
+                             l1_phys_map + i, i, &map);
+    }
+    if (map.size) {
+        client->set_memory(client, map.start_addr, map.size, map.phys_offset);
     }
 }
 
#4 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 17 lines · Alex Williamson

When a phys memory client registers and we play catchup by walking the page tables, we can make a huge improvement in the number of times the set_memory callback is called by batching contiguous pages together. With a 4G guest, this reduces the number of callbacks at registration from 1048866 to 296.

---

 exec.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
exec.c +0 -0
index bbd5c86..a0678a4 100644

exec.c static int cpu_notify_migration_log(int enable) · line 1746

Quoted 6 lines
exec.c +3 -0
@@ -1741,14 +1741,21 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
+struct last_map {
+    target_phys_addr_t start_addr;
+    ram_addr_t size;

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

Quoted 51 lines
exec.c +23 -5
+    ram_addr_t phys_offset;
+};
+
 /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  * address.  Each intermediate table provides the next L2_BITs of guest
  * physical address space.  The number of levels vary based on host and
  * guest configuration, making it efficient to build the final guest
  * physical address by seeding the L1 offset and shifting and adding in
  * each L2 offset as we recurse through them. */
-static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp, target_phys_addr_t addr)
+static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
+                                 void **lp, target_phys_addr_t addr,
+                                 struct last_map *map)
 {
     int i;
 
@@ -1760,15 +1767,29 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
         addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
-                                   TARGET_PAGE_SIZE, pd[i].phys_offset);
+                target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
+
+                if (map->size &&
+                    start_addr == map->start_addr + map->size &&
+                    pd[i].phys_offset == map->phys_offset + map->size) {
+
+                    map->size += TARGET_PAGE_SIZE;
+                    continue;
+                } else if (map->size) {
+                    client->set_memory(client, map->start_addr,
+                                       map->size, map->phys_offset);
+                }
+
+                map->start_addr = start_addr;
+                map->size = TARGET_PAGE_SIZE;
+                map->phys_offset = pd[i].phys_offset;
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
             phys_page_for_each_1(client, level - 1, pp + i,
-                                 (addr << L2_BITS) | i);
+                                 (addr << L2_BITS) | i, map);
         }
     }
 }

exec.c static void phys_page_for_each_1(CPUPhysMemoryClient *client, · line 1801

Quoted 5 lines
exec.c +2 -0
@@ -1776,9 +1797,14 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
 static void phys_page_for_each(CPUPhysMemoryClient *client)
 {
     int i;
+    struct last_map map = { 0 };
+

Nit: just {} is enough.

Quoted 10 lines
exec.c +4 -1
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i, i);
+                             l1_phys_map + i, i, &map);
+    }
+    if (map.size) {
+        client->set_memory(client, map.start_addr, map.size, map.phys_offset);
     }
 }
 
#15 lore raw
In reply to Alex Williamson
Quoted 9 lines · Alex Williamson

This series pulls together several related patches for bugs and performance that I found last week. Only the 2nd patch is actually modified from inital posting, adding the comments suggested by Markus. The 1st two patches fix pretty serious brokeness in the CPUPhysMemoryClient interface. Of the two current users, kvm and vhost, only vhost is actually affected by these bugs. Please apply. Thanks,

Alex

Applied first two, thanks!

Quoted 10 lines

---

Alex Williamson (3):
CPUPhysMemoryClient: Batch contiguous addresses when playing catchup
CPUPhysMemoryClient: Pass guest physical address not region offset
CPUPhysMemoryClient: Fix typo in phys memory client registration

 exec.c |   46 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 40 insertions(+), 6 deletions(-)
#5 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

In reply to Michael S. Tsirkin
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Alex Williamson

When a phys memory client registers and we play catchup by walking the page tables, we can make a huge improvement in the number of times the set_memory callback is called by batching contiguous pages together. With a 4G guest, this reduces the number of callbacks at registration from 1048866 to 296.

---

 exec.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
exec.c +0 -0
index bbd5c86..a0678a4 100644
@@ -1741,14 +1741,21 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
Quoted 6 lines

exec.c static int cpu_notify_migration_log(int enable) · line 1746

Quoted 3 lines
exec.c +3 -0
+struct last_map {
+    target_phys_addr_t start_addr;
+    ram_addr_t size;

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

struct CPUPhysMemoryClient {
void (*set_memory)(struct CPUPhysMemoryClient *client,
target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset);

ram_addr_t seems to be the standard for describing these types of things. It's an unsigned long, so 4G is only concern for 32b builds, which don't support that much memory anyway. Please apply. Thanks,

Alex

Quoted 69 lines
Quoted 51 lines
exec.c +23 -5
+    ram_addr_t phys_offset;
+};
+
 /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  * address.  Each intermediate table provides the next L2_BITs of guest
  * physical address space.  The number of levels vary based on host and
  * guest configuration, making it efficient to build the final guest
  * physical address by seeding the L1 offset and shifting and adding in
  * each L2 offset as we recurse through them. */
-static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp, target_phys_addr_t addr)
+static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
+                                 void **lp, target_phys_addr_t addr,
+                                 struct last_map *map)
 {
     int i;
 
@@ -1760,15 +1767,29 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
         addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
-                                   TARGET_PAGE_SIZE, pd[i].phys_offset);
+                target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
+
+                if (map->size &&
+                    start_addr == map->start_addr + map->size &&
+                    pd[i].phys_offset == map->phys_offset + map->size) {
+
+                    map->size += TARGET_PAGE_SIZE;
+                    continue;
+                } else if (map->size) {
+                    client->set_memory(client, map->start_addr,
+                                       map->size, map->phys_offset);
+                }
+
+                map->start_addr = start_addr;
+                map->size = TARGET_PAGE_SIZE;
+                map->phys_offset = pd[i].phys_offset;
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
             phys_page_for_each_1(client, level - 1, pp + i,
-                                 (addr << L2_BITS) | i);
+                                 (addr << L2_BITS) | i, map);
         }
     }
 }

exec.c static void phys_page_for_each_1(CPUPhysMemoryClient *client, · line 1801

Quoted 5 lines
exec.c +2 -0
@@ -1776,9 +1797,14 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
 static void phys_page_for_each(CPUPhysMemoryClient *client)
 {
     int i;
+    struct last_map map = { 0 };
+

Nit: just {} is enough.

Quoted 10 lines
exec.c +4 -1
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i, i);
+                             l1_phys_map + i, i, &map);
+    }
+    if (map.size) {
+        client->set_memory(client, map.start_addr, map.size, map.phys_offset);
     }
 }
 
Acked-by
#6 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 11 lines · Alex Williamson
Quoted 2 lines
Quoted 2 lines

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

struct CPUPhysMemoryClient {
void (*set_memory)(struct CPUPhysMemoryClient *client,
target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset);

ram_addr_t seems to be the standard for describing these types of things. It's an unsigned long, so 4G is only concern for 32b builds, which don't support that much memory anyway. Please apply. Thanks,

A memory size can obviously not be bigger than the maximum physical address, so I find it really hard to see how this could overflow.

It seems fair to use it for the size here.

#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 9 lines · Jes Sorensen
Quoted 8 lines · Alex Williamson
Quoted 2 lines
Quoted 2 lines

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

struct CPUPhysMemoryClient {
void (*set_memory)(struct CPUPhysMemoryClient *client,
target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset);

Quoted 6 lines
Quoted 3 lines

ram_addr_t seems to be the standard for describing these types of things. It's an unsigned long, so 4G is only concern for 32b builds, which don't support that much memory anyway. Please apply. Thanks,

A memory size can obviously not be bigger than the maximum physical address, so I find it really hard to see how this could overflow.

For example, a 4G size does not fit in 32 bits.

Quoted 4 lines

It seems fair to use it for the size here.

#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

In reply to Alex Williamson
Quoted 35 lines · Alex Williamson
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Alex Williamson

When a phys memory client registers and we play catchup by walking the page tables, we can make a huge improvement in the number of times the set_memory callback is called by batching contiguous pages together. With a 4G guest, this reduces the number of callbacks at registration from 1048866 to 296.

---

 exec.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
exec.c +0 -0
index bbd5c86..a0678a4 100644
@@ -1741,14 +1741,21 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
Quoted 6 lines

exec.c static int cpu_notify_migration_log(int enable) · line 1746

Quoted 3 lines
exec.c +3 -0
+struct last_map {
+    target_phys_addr_t start_addr;
+    ram_addr_t size;

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

struct CPUPhysMemoryClient {
void (*set_memory)(struct CPUPhysMemoryClient *client,
target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset);

Quoted 5 lines

ram_addr_t seems to be the standard for describing these types of things. It's an unsigned long, so 4G is only concern for 32b builds, which don't support that much memory anyway. Please apply. Thanks,

Alex

OK, I don't think it's a problem in practice.
I dislike the use of _addr for sizes, we should
have _size_t, but that's a separate problem,
this patch is consistent.

I'll give people a bit of time to review and reply though, there seems to be no rush.

Quoted 71 lines
Quoted 69 lines
Quoted 51 lines
exec.c +23 -5
+    ram_addr_t phys_offset;
+};
+
 /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  * address.  Each intermediate table provides the next L2_BITs of guest
  * physical address space.  The number of levels vary based on host and
  * guest configuration, making it efficient to build the final guest
  * physical address by seeding the L1 offset and shifting and adding in
  * each L2 offset as we recurse through them. */
-static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp, target_phys_addr_t addr)
+static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
+                                 void **lp, target_phys_addr_t addr,
+                                 struct last_map *map)
 {
     int i;
 
@@ -1760,15 +1767,29 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
         addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
-                                   TARGET_PAGE_SIZE, pd[i].phys_offset);
+                target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
+
+                if (map->size &&
+                    start_addr == map->start_addr + map->size &&
+                    pd[i].phys_offset == map->phys_offset + map->size) {
+
+                    map->size += TARGET_PAGE_SIZE;
+                    continue;
+                } else if (map->size) {
+                    client->set_memory(client, map->start_addr,
+                                       map->size, map->phys_offset);
+                }
+
+                map->start_addr = start_addr;
+                map->size = TARGET_PAGE_SIZE;
+                map->phys_offset = pd[i].phys_offset;
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
             phys_page_for_each_1(client, level - 1, pp + i,
-                                 (addr << L2_BITS) | i);
+                                 (addr << L2_BITS) | i, map);
         }
     }
 }

exec.c static void phys_page_for_each_1(CPUPhysMemoryClient *client, · line 1801

Quoted 5 lines
exec.c +2 -0
@@ -1776,9 +1797,14 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
 static void phys_page_for_each(CPUPhysMemoryClient *client)
 {
     int i;
+    struct last_map map = { 0 };
+

Nit: just {} is enough.

Quoted 10 lines
exec.c +4 -1
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i, i);
+                             l1_phys_map + i, i, &map);
+    }
+    if (map.size) {
+        client->set_memory(client, map.start_addr, map.size, map.phys_offset);
     }
 }
 
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

In reply to Michael S. Tsirkin
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines
Quoted 2 lines

A memory size can obviously not be bigger than the maximum physical address, so I find it really hard to see how this could overflow.

For example, a 4G size does not fit in 32 bits.

That is the only corner case - you can handle that by -1 if you like.

Jes

#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 6 lines · Jes Sorensen
Quoted 3 lines · Michael S. Tsirkin
Quoted 2 lines
Quoted 2 lines

A memory size can obviously not be bigger than the maximum physical address, so I find it really hard to see how this could overflow.

For example, a 4G size does not fit in 32 bits.

That is the only corner case

True.

Quoted 1 line

you can handle that by -1 if you like.

But then all users need to be updated.
Seems easier to break out of the loop easier. It's likely not a real problem, certainly not on a pc, don't know about other systems.

Quoted 2 lines

Jes

#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Jes Sorensen
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

A memory size can obviously not be bigger than the maximum physical address, so I find it really hard to see how this could overflow.

For example, a 4G size does not fit in 32 bits.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

That is the only corner case

True.

Quoted 1 line
Quoted 1 line

you can handle that by -1 if you like.

But then all users need to be updated.
Seems easier to break out of the loop easier. It's likely not a real problem, certainly not on a pc, don't know about other systems.

I think it is quite fair to limit the amount of memory we support when running 32 bit qemu binaries. I would expect more things to break than just this if we tried to support 4GB of RAM on a 32 bit host.

Cheers,
Jes

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 16 lines · Jes Sorensen
Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Jes Sorensen
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

A memory size can obviously not be bigger than the maximum physical address, so I find it really hard to see how this could overflow.

For example, a 4G size does not fit in 32 bits.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

That is the only corner case

True.

Quoted 1 line
Quoted 1 line

you can handle that by -1 if you like.

But then all users need to be updated.
Seems easier to break out of the loop easier. It's likely not a real problem, certainly not on a pc, don't know about other systems.

Quoted 6 lines

I think it is quite fair to limit the amount of memory we support when running 32 bit qemu binaries. I would expect more things to break than just this if we tried to support 4GB of RAM on a 32 bit host.

Cheers,
Jes

Fair enough.

#13 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

In reply to Michael S. Tsirkin
Quoted 42 lines · Michael S. Tsirkin
Quoted 35 lines · Alex Williamson
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Alex Williamson

When a phys memory client registers and we play catchup by walking the page tables, we can make a huge improvement in the number of times the set_memory callback is called by batching contiguous pages together. With a 4G guest, this reduces the number of callbacks at registration from 1048866 to 296.

---

 exec.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
exec.c +0 -0
index bbd5c86..a0678a4 100644
@@ -1741,14 +1741,21 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
Quoted 6 lines

exec.c static int cpu_notify_migration_log(int enable) · line 1746

Quoted 3 lines
exec.c +3 -0
+struct last_map {
+    target_phys_addr_t start_addr;
+    ram_addr_t size;

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

struct CPUPhysMemoryClient {
void (*set_memory)(struct CPUPhysMemoryClient *client,
target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset);

Quoted 5 lines

ram_addr_t seems to be the standard for describing these types of things. It's an unsigned long, so 4G is only concern for 32b builds, which don't support that much memory anyway. Please apply. Thanks,

Alex

Quoted 7 lines

OK, I don't think it's a problem in practice.
I dislike the use of _addr for sizes, we should
have _size_t, but that's a separate problem,
this patch is consistent.

I'll give people a bit of time to review and reply though, there seems to be no rush.

Bump. I didn't see anything come out of the discussion that would suggest a respin. Please apply. Thanks,

Alex

Quoted 71 lines
Quoted 71 lines
Quoted 69 lines
Quoted 51 lines
exec.c +23 -5
+    ram_addr_t phys_offset;
+};
+
 /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  * address.  Each intermediate table provides the next L2_BITs of guest
  * physical address space.  The number of levels vary based on host and
  * guest configuration, making it efficient to build the final guest
  * physical address by seeding the L1 offset and shifting and adding in
  * each L2 offset as we recurse through them. */
-static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp, target_phys_addr_t addr)
+static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
+                                 void **lp, target_phys_addr_t addr,
+                                 struct last_map *map)
 {
     int i;
 
@@ -1760,15 +1767,29 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
         addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
-                                   TARGET_PAGE_SIZE, pd[i].phys_offset);
+                target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
+
+                if (map->size &&
+                    start_addr == map->start_addr + map->size &&
+                    pd[i].phys_offset == map->phys_offset + map->size) {
+
+                    map->size += TARGET_PAGE_SIZE;
+                    continue;
+                } else if (map->size) {
+                    client->set_memory(client, map->start_addr,
+                                       map->size, map->phys_offset);
+                }
+
+                map->start_addr = start_addr;
+                map->size = TARGET_PAGE_SIZE;
+                map->phys_offset = pd[i].phys_offset;
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
             phys_page_for_each_1(client, level - 1, pp + i,
-                                 (addr << L2_BITS) | i);
+                                 (addr << L2_BITS) | i, map);
         }
     }
 }

exec.c static void phys_page_for_each_1(CPUPhysMemoryClient *client, · line 1801

Quoted 5 lines
exec.c +2 -0
@@ -1776,9 +1797,14 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
 static void phys_page_for_each(CPUPhysMemoryClient *client)
 {
     int i;
+    struct last_map map = { 0 };
+

Nit: just {} is enough.

Quoted 10 lines
exec.c +4 -1
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i, i);
+                             l1_phys_map + i, i, &map);
+    }
+    if (map.size) {
+        client->set_memory(client, map.start_addr, map.size, map.phys_offset);
     }
 }
 
#14 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] CPUPhysMemoryClient: Batch contiguous addresses when playing catchup

Quoted 48 lines · Alex Williamson
Quoted 47 lines · Michael S. Tsirkin
Quoted 35 lines · Alex Williamson
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Alex Williamson

When a phys memory client registers and we play catchup by walking the page tables, we can make a huge improvement in the number of times the set_memory callback is called by batching contiguous pages together. With a 4G guest, this reduces the number of callbacks at registration from 1048866 to 296.

---

 exec.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
exec.c +0 -0
index bbd5c86..a0678a4 100644
@@ -1741,14 +1741,21 @@ static int cpu_notify_migration_log(int enable)
     return 0;
 }
 
Quoted 6 lines

exec.c static int cpu_notify_migration_log(int enable) · line 1746

Quoted 3 lines
exec.c +3 -0
+struct last_map {
+    target_phys_addr_t start_addr;
+    ram_addr_t size;

A bit worried that ram_addr_t size might thinkably overflow (it's just a long, could be a 4G ram). Break it out when it fills up?

struct CPUPhysMemoryClient {
void (*set_memory)(struct CPUPhysMemoryClient *client,
target_phys_addr_t start_addr, ram_addr_t size, ram_addr_t phys_offset);

Quoted 5 lines

ram_addr_t seems to be the standard for describing these types of things. It's an unsigned long, so 4G is only concern for 32b builds, which don't support that much memory anyway. Please apply. Thanks,

Alex

OK, I don't think it's a problem in practice.
I dislike the use of _addr for sizes, we should
have _size_t, but that's a separate problem,
this patch is consistent.

Quoted 7 lines
Quoted 2 lines

I'll give people a bit of time to review and reply though, there seems to be no rush.

Bump. I didn't see anything come out of the discussion that would suggest a respin. Please apply. Thanks,

Alex

Applied.
Thanks,

Quoted 73 lines
Quoted 71 lines
Quoted 71 lines
Quoted 69 lines
Quoted 51 lines
exec.c +23 -5
+    ram_addr_t phys_offset;
+};
+
 /* The l1_phys_map provides the upper P_L1_BITs of the guest physical
  * address.  Each intermediate table provides the next L2_BITs of guest
  * physical address space.  The number of levels vary based on host and
  * guest configuration, making it efficient to build the final guest
  * physical address by seeding the L1 offset and shifting and adding in
  * each L2 offset as we recurse through them. */
-static void phys_page_for_each_1(CPUPhysMemoryClient *client,
-                                 int level, void **lp, target_phys_addr_t addr)
+static void phys_page_for_each_1(CPUPhysMemoryClient *client, int level,
+                                 void **lp, target_phys_addr_t addr,
+                                 struct last_map *map)
 {
     int i;
 
@@ -1760,15 +1767,29 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
         addr <<= L2_BITS + TARGET_PAGE_BITS;
         for (i = 0; i < L2_SIZE; ++i) {
             if (pd[i].phys_offset != IO_MEM_UNASSIGNED) {
-                client->set_memory(client, addr | i << TARGET_PAGE_BITS,
-                                   TARGET_PAGE_SIZE, pd[i].phys_offset);
+                target_phys_addr_t start_addr = addr | i << TARGET_PAGE_BITS;
+
+                if (map->size &&
+                    start_addr == map->start_addr + map->size &&
+                    pd[i].phys_offset == map->phys_offset + map->size) {
+
+                    map->size += TARGET_PAGE_SIZE;
+                    continue;
+                } else if (map->size) {
+                    client->set_memory(client, map->start_addr,
+                                       map->size, map->phys_offset);
+                }
+
+                map->start_addr = start_addr;
+                map->size = TARGET_PAGE_SIZE;
+                map->phys_offset = pd[i].phys_offset;
             }
         }
     } else {
         void **pp = *lp;
         for (i = 0; i < L2_SIZE; ++i) {
             phys_page_for_each_1(client, level - 1, pp + i,
-                                 (addr << L2_BITS) | i);
+                                 (addr << L2_BITS) | i, map);
         }
     }
 }

exec.c static void phys_page_for_each_1(CPUPhysMemoryClient *client, · line 1801

Quoted 5 lines
exec.c +2 -0
@@ -1776,9 +1797,14 @@ static void phys_page_for_each_1(CPUPhysMemoryClient *client,
 static void phys_page_for_each(CPUPhysMemoryClient *client)
 {
     int i;
+    struct last_map map = { 0 };
+

Nit: just {} is enough.

Quoted 10 lines
exec.c +4 -1
     for (i = 0; i < P_L1_SIZE; ++i) {
         phys_page_for_each_1(client, P_L1_SHIFT / L2_BITS - 1,
-                             l1_phys_map + i, i);
+                             l1_phys_map + i, i, &map);
+    }
+    if (map.size) {
+        client->set_memory(client, map.start_addr, map.size, map.phys_offset);
     }
 }