Skip to contentSkip to conversation

vga optmization

Qemu-devel Glauber Costa 8 participants 26 messages Nov 3, 2008 – Nov 7, 2008

lore
Share & tools
lore
#0 lore raw

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Let me know what you think

cpu-all.h +5 -10
index cdd79bc..9118f4d 100644
@@ -46,6 +46,8 @@
 
 #ifdef BSWAP_NEEDED
 
+#include "kvm.h"
+
 static inline uint16_t tswap16(uint16_t s)
 {
     return bswap16(s);
@@ -909,17 +911,10 @@ int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
 #define KQEMU_DIRTY_FLAG     0x04
 #define MIGRATION_DIRTY_FLAG 0x08
 
-/* read dirty bit (return 0 or 1) */
-static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
-}
+int cpu_physical_memory_get_dirty(ram_addr_t addr, int dirty_flags);
+int cpu_physical_memory_is_dirty(ram_addr_t addr);
 
-static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
-                                                int dirty_flags)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
-}
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr);
 
 static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
 {
exec.c +29 -0
index ef1072b..4ea145e 100644
@@ -1818,11 +1818,40 @@ int cpu_physical_memory_set_dirty_tracking(int enable)
     return 0;
 }
 
+int cpu_physical_memory_get_dirty(ram_addr_t addr,
+                                                int dirty_flags)
+{
+    int is_dirty = 0;
+    is_dirty = phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
+    if (is_dirty)
+        goto out;
+#ifdef CONFIG_KVM
+    if (kvm_enabled())
+        is_dirty = kvm_physical_memory_get_dirty(addr);
+        /* to make it usable below */
+        is_dirty = !!is_dirty * 0xff;
+#endif
+out:
+    return is_dirty;
+}
+
+/* read dirty bit (return 0 or 1) */
+int cpu_physical_memory_is_dirty(ram_addr_t addr)
+{
+    return cpu_physical_memory_get_dirty(addr, 0xff) == 0xff;
+}
+
 int cpu_physical_memory_get_dirty_tracking(void)
 {
     return in_migration;
 }
 
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr)
+{
+    if (kvm_enabled())
+        kvm_physical_sync_dirty_bitmap(start_addr);
+}
+
 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
 {
     ram_addr_t ram_addr;
hw/cirrus_vga.c +77 -4

132 lines not on this page · raw message

hw/vga.c +6 -0
index 9540db0..275ef0a 100644
@@ -28,6 +28,7 @@
 #include "vga_int.h"
 #include "pixel_ops.h"
 #include "qemu-timer.h"
+#include "kvm.h"
 
 //#define DEBUG_VGA
 //#define DEBUG_VGA_MEM
@@ -1568,6 +1569,8 @@ static void vga_draw_graphic(VGAState *s, int full_update)
     uint32_t v, addr1, addr;
     vga_draw_line_func *vga_draw_line;
 
+    qemu_physical_sync_dirty_bitmap(s->vram_offset);
+
     full_update |= update_basic_params(s);
 
     s->get_resolution(s, &width, &height);
@@ -2102,6 +2105,9 @@ static void vga_map(PCIDevice *pci_dev, int region_num,
     } else {
         cpu_register_physical_memory(addr, s->vram_size, s->vram_offset);
     }
+
+    if (kvm_enabled())
+        kvm_log_start(addr, VGA_RAM_SIZE);
 }
 
 void vga_common_init(VGAState *s, DisplayState *ds, uint8_t *vga_ram_base,
hw/vga_int.h +2 -1
index 82a755e..7d3c011 100644
@@ -100,7 +100,7 @@ typedef void (* vga_update_retrace_info_fn)(struct VGAState *s);
 
 #define VGA_STATE_COMMON                                                \
     uint8_t *vram_ptr;                                                  \
-    unsigned long vram_offset;                                          \
+    ram_addr_t vram_offset;                                             \
     unsigned int vram_size;                                             \
     unsigned long bios_offset;                                          \
     unsigned int bios_size;                                             \
@@ -129,6 +129,7 @@ typedef void (* vga_update_retrace_info_fn)(struct VGAState *s);
     int dac_8bit;                                                       \
     uint8_t palette[768];                                               \
     int32_t bank_offset;                                                \
+    uint32_t vga_io_memory;                                             \
     int (*get_bpp)(struct VGAState *s);                                 \
     void (*get_offsets)(struct VGAState *s,                             \
                         uint32_t *pline_offset,                         \
kvm-all.c +178 -19

240 lines not on this page · raw message

kvm.h +3 -0
index 37102b4..6e2d9d2 100644
@@ -38,6 +38,9 @@ void kvm_set_phys_mem(target_phys_addr_t start_addr,
                       ram_addr_t size,
                       ram_addr_t phys_offset);
 
+int kvm_physical_memory_get_dirty(ram_addr_t addr);
+void kvm_physical_sync_dirty_bitmap(ram_addr_t start_addr);
+
 /* internal API */
 
 struct KVMState;
#1 lore raw

Can we try to avoid any if (kvm_enabled()) in the vga code and use some kind of more generic hook instead?
Xen has some very similar changes to the vga code, it would be nice to merge them as well.

Quoted 16 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Let me know what you think

#2 lore raw
Quoted 4 lines · Stefano Stabellini

Can we try to avoid any if (kvm_enabled()) in the vga code and use some kind of more generic hook instead?
Xen has some very similar changes to the vga code, it would be nice to merge them as well.

Yes, we could. But then we'd need to have qemu to view memory as slots. It's worthy, but long term.

By the way, posting those xen changes would be awesome.
If we both can avoid using hv-specific hooks (which I did: there's only one left), and
so do you, I'm sure we can come up with something very nice in the end of the day.

Quoted 24 lines
Quoted 16 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Let me know what you think

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#3 lore raw
Quoted 7 lines · Glauber Costa
Quoted 4 lines · Stefano Stabellini

Can we try to avoid any if (kvm_enabled()) in the vga code and use some kind of more generic hook instead?
Xen has some very similar changes to the vga code, it would be nice to merge them as well.

Quoted 8 lines

Yes, we could. But then we'd need to have qemu to view memory as slots. It's worthy, but long term.

By the way, posting those xen changes would be awesome.
If we both can avoid using hv-specific hooks (which I did: there's only one left), and
so do you, I'm sure we can come up with something very nice in the end of the day.

I totally agree.
Posting those patches has been on my TODO list for quite some time, I think that now is the time to increase the priority :) I'll keep you posted.

#4 lore raw
In reply to Glauber Costa
Quoted 7 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

Quoted 7 lines

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Let me know what you think

The patch does not apply, because of the kvm files. What patches do I need to apply first? I'd like to see how does the optimization apply to TCX.

This also may mean that some of my comments below can be invalid.

Quoted 1 line

+void set_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end, ram_addr_t target)

No "static"?

Quoted 5 lines

+{

 +    /* align begin and end address */
 +    begin = begin & TARGET_PAGE_MASK;
 +    end = begin + VGA_RAM_SIZE;
 +    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;

This will fail if "end" is at the last page of the memory.

Quoted 1 line

+void unset_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end)

No "static"?

Quoted 5 lines

+{

 +    /* align begin and end address */
 +    end = begin + VGA_RAM_SIZE;
 +    begin = begin & TARGET_PAGE_MASK;
 +    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;

This will fail if "end" is at the last page of the memory.

Quoted 1 line

+void unmap_linear_vram(CirrusVGAState *s)

No "static"?

Quoted 1 line

+ uint32_t vga_io_memory; \

cpu_register_io_memory() returns an "int".

#8 lore raw
In reply to Glauber Costa
Quoted 7 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

Quoted 7 lines

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Let me know what you think

Why don't you modify the lower level QEMU dirty bits handling functions to be consistent with the KVM dirty bits ? By doing that you can avoid patching the device drivers and have smaller code. The fact that KVM use physical memory addresses is not a problem if you can convert the ram addresses to physical memory addresses (in most cases there is only one physical address corresponding to one RAM address).

And if KVM does not use the dynamic translator during large amount of time it might be worth bypassing most of the QEMU dirty bits handling system to use only the KVM system in order to get better performance.

Regards,

Fabrice.

#5 lore raw
In reply to Blue Swirl
Quoted 14 lines · Blue Swirl
Quoted 13 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Quoted 5 lines
Quoted 1 line

Let me know what you think

The patch does not apply, because of the kvm files. What patches do I need to apply first? I'd like to see how does the optimization apply to TCX.

Yes, you are missing kvm patches some time ago, which he plans to merge.

Quoted 5 lines

This also may mean that some of my comments below can be invalid.

Quoted 1 line

+void set_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end, ram_addr_t target)

No "static"?

thanks, will update.

Quoted 8 lines
Quoted 6 lines

+{

 +    /* align begin and end address */
 +    begin = begin & TARGET_PAGE_MASK;
 +    end = begin + VGA_RAM_SIZE;
 +    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;

This will fail if "end" is at the last page of the memory.

How so? I don't think it's possible for the addr 0xsomething0001 to be valid, but 0x(something+1)000 to be not.

Quoted 4 lines
Quoted 2 lines

+ uint32_t vga_io_memory; \

cpu_register_io_memory() returns an "int".

can it possibly be negative?
that said, of course I can change it for consistency, but I'd like to understand it more

thanks

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#9 lore raw
In reply to Fabrice Bellard
Quoted 14 lines · Fabrice Bellard
Quoted 13 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Quoted 8 lines
Quoted 1 line

Let me know what you think

Why don't you modify the lower level QEMU dirty bits handling functions to be consistent with the KVM dirty bits ? By doing that you can avoid patching the device drivers and have smaller code. The fact that KVM use physical memory addresses is not a problem if you can convert the ram addresses to physical memory addresses (in most cases there is only one physical address corresponding to one RAM address).

Not here. We have to map vga linear frame buffer as RAM. So the option is to always do that. That's pretty much the only device specific thing we're doing here
About physical memory, exactly: code in kvm userspace does it a little bit differently,
and it is _this_ code that does it the qemu way. So it seems we're in agreement.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#6 lore raw
In reply to Glauber Costa
Quoted 41 lines · Glauber Costa
Quoted 14 lines · Blue Swirl
Quoted 13 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Quoted 5 lines
Quoted 1 line

Let me know what you think

The patch does not apply, because of the kvm files. What patches do I need to apply first? I'd like to see how does the optimization apply to TCX.

Yes, you are missing kvm patches some time ago, which he plans to merge.

Quoted 5 lines

This also may mean that some of my comments below can be invalid.

Quoted 1 line

+void set_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end, ram_addr_t target)

No "static"?

thanks, will update.

Quoted 7 lines
Quoted 6 lines

+{

 +    /* align begin and end address */
 +    begin = begin & TARGET_PAGE_MASK;
 +    end = begin + VGA_RAM_SIZE;
 +    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;
Quoted 5 lines
Quoted 1 line

This will fail if "end" is at the last page of the memory.

How so? I don't think it's possible for the addr 0xsomething0001 to be
valid, but 0x(something+1)000 to be not.

0xfffff001 + 0xfff = 0 (wrapped), then masking still gives 0.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

+ uint32_t vga_io_memory; \

cpu_register_io_memory() returns an "int".

can it possibly be negative?
that said, of course I can change it for consistency, but I'd like to understand it more

In case of error it could be negative but I'm only thinking about consistency.

#7 lore raw
Quoted 42 lines · Blue Swirl
Quoted 41 lines · Glauber Costa
Quoted 14 lines · Blue Swirl
Quoted 13 lines · Glauber Costa

Hi guys,

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Quoted 5 lines
Quoted 1 line

Let me know what you think

The patch does not apply, because of the kvm files. What patches do I need to apply first? I'd like to see how does the optimization apply to TCX.

Yes, you are missing kvm patches some time ago, which he plans to merge.

Quoted 5 lines

This also may mean that some of my comments below can be invalid.

Quoted 1 line

+void set_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end, ram_addr_t target)

No "static"?

thanks, will update.

Quoted 7 lines
Quoted 6 lines

+{

 +    /* align begin and end address */
 +    begin = begin & TARGET_PAGE_MASK;
 +    end = begin + VGA_RAM_SIZE;
 +    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

This will fail if "end" is at the last page of the memory.

How so? I don't think it's possible for the addr 0xsomething0001 to be
valid, but 0x(something+1)000 to be not.

0xfffff001 + 0xfff = 0 (wrapped), then masking still gives 0.

oh, you're talking about the last page in the possible address space. In that case I agree, but I believe we're not expecting vga to ever be there.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

+ uint32_t vga_io_memory; \

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

cpu_register_io_memory() returns an "int".

can it possibly be negative?
that said, of course I can change it for consistency, but I'd like to understand it more

In case of error it could be negative but I'm only thinking about consistency.

fair. I'll update it.

Quoted 2 lines
Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#10 lore raw
In reply to Glauber Costa
Quoted 22 lines · Glauber Costa

this is a port of current kvm vga memory optimization to our new infrastructure proposed by anthony. It's goal is to use as few kvm specific hooks as possible. In fact, the only one I'm relying on is enabling/disabling of logging. The rest, is pretty much general.

We map the linear frame buffer area as RAM, and then use dirty tracking to decide whether or not to update it. To be consistent with qemu, this version, differently from upstream kvm, tracks memory based on its physical address, represented by vram_offset, instead of vram_ptr, or any other construct.

Let me know what you think

exec.c +7 -0
+int cpu_physical_memory_get_dirty(ram_addr_t addr,
+                                                int dirty_flags)
+{
+    int is_dirty = 0;
+    is_dirty = phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
+    if (is_dirty)
+        goto out;

exec.c int cpu_physical_memory_set_dirty_tracking(int enable) · line 1837

Quoted 10 lines
exec.c +10 -0
+#ifdef CONFIG_KVM
+    if (kvm_enabled())
+        is_dirty = kvm_physical_memory_get_dirty(addr);
+        /* to make it usable below */
+        is_dirty = !!is_dirty * 0xff;
+#endif
+out:
+    return is_dirty;
+}
+

The kvm dirty bitmap and qemu dirty bitmap are different. 'qemu dirty' means 'written to since hte last time the dirty bit was cleared', while 'kvm dirty' means 'written to since the last time the bitmap was synchronized'. So the qemu bitmap is stickier than the kvm bitmap.

The current code accounts for that by merging the kvm bitmap into the qemu bitmap, but you're losing some information here. It doesn't matter for vga, since you're clearing the dirty bit immediately anyway, but it will matter for other uses (example, live migration with the vga optimization enabled).

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#11 lore raw
In reply to Glauber Costa

Hi,

2008/11/3 Glauber Costa <[email protected]>:
[...]

Quoted 21 lines
cpu-all.h +2 -4
index cdd79bc..9118f4d 100644
@@ -46,6 +46,8 @@
 
 #ifdef BSWAP_NEEDED
 
+#include "kvm.h"
+
 static inline uint16_t tswap16(uint16_t s)
 {
    return bswap16(s);
@@ -909,17 +911,10 @@ int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
 #define KQEMU_DIRTY_FLAG     0x04
 #define MIGRATION_DIRTY_FLAG 0x08
 
-/* read dirty bit (return 0 or 1) */
-static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;

cpu-all.h int cpu_memory_rw_debug(CPUState *env, target_ulong addr, · line 917

Quoted 10 lines
cpu-all.h +3 -6
-}
+int cpu_physical_memory_get_dirty(ram_addr_t addr, int dirty_flags);
+int cpu_physical_memory_is_dirty(ram_addr_t addr);
 
-static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
-                                                int dirty_flags)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
-}
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr);

This will prevent the functions from being inlined even if KVM is disabled (e.g. on other archs) and I think it could be easily retained.

Cheers

#12 lore raw
Quoted 29 lines · andrzej zaborowski

Hi,

2008/11/3 Glauber Costa <[email protected]>:
[...]

Quoted 25 lines
cpu-all.h +4 -5
index cdd79bc..9118f4d 100644
@@ -46,6 +46,8 @@
 
 #ifdef BSWAP_NEEDED
 
+#include "kvm.h"
+
 static inline uint16_t tswap16(uint16_t s)
 {
    return bswap16(s);
@@ -909,17 +911,10 @@ int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
 #define KQEMU_DIRTY_FLAG     0x04
 #define MIGRATION_DIRTY_FLAG 0x08
 
-/* read dirty bit (return 0 or 1) */
-static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
-}
+int cpu_physical_memory_get_dirty(ram_addr_t addr, int dirty_flags);
+int cpu_physical_memory_is_dirty(ram_addr_t addr);
 
Quoted 11 lines

cpu-all.h int cpu_memory_rw_debug(CPUState *env, target_ulong addr, · line 917

Quoted 6 lines
cpu-all.h +1 -5
-static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
-                                                int dirty_flags)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
-}
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr);

This will prevent the functions from being inlined even if KVM is disabled (e.g. on other archs) and I think it could be easily retained.

I agree on this.

Quoted 6 lines

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr)

{
    if (kvm_enabled())
        kvm_physical_sync_dirty_bitmap(start_addr);
}

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
	/* sync and merge the two bitmaps between start_addr and end_addr */
}

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

#13 lore raw
Quoted 61 lines · Stefano Stabellini
Quoted 29 lines · andrzej zaborowski

Hi,

2008/11/3 Glauber Costa <[email protected]>:
[...]

Quoted 25 lines
cpu-all.h +4 -5
index cdd79bc..9118f4d 100644
@@ -46,6 +46,8 @@
 
 #ifdef BSWAP_NEEDED
 
+#include "kvm.h"
+
 static inline uint16_t tswap16(uint16_t s)
 {
    return bswap16(s);
@@ -909,17 +911,10 @@ int cpu_memory_rw_debug(CPUState *env, target_ulong addr,
 #define KQEMU_DIRTY_FLAG     0x04
 #define MIGRATION_DIRTY_FLAG 0x08
 
-/* read dirty bit (return 0 or 1) */
-static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
-}
+int cpu_physical_memory_get_dirty(ram_addr_t addr, int dirty_flags);
+int cpu_physical_memory_is_dirty(ram_addr_t addr);
 
Quoted 11 lines

cpu-all.h int cpu_memory_rw_debug(CPUState *env, target_ulong addr, · line 917

Quoted 6 lines
cpu-all.h +1 -5
-static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
-                                                int dirty_flags)
-{
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
-}
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr);

This will prevent the functions from being inlined even if KVM is disabled (e.g. on other archs) and I think it could be easily retained.

I agree on this.

Quoted 6 lines

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr)

{
    if (kvm_enabled())
        kvm_physical_sync_dirty_bitmap(start_addr);
}

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

Quoted 8 lines

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
       /* sync and merge the two bitmaps between start_addr and end_addr */
}

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

Either way works for me. I'm fine with your suggestion.

Quoted 3 lines
Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#14 lore raw
In reply to Stefano Stabellini
Quoted 7 lines · Stefano Stabellini

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

Quoted 9 lines

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
	/* sync and merge the two bitmaps between start_addr and end_addr */
}

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

That's how current kvm userspace works. It's also more correct, since the kvm bitmap feeds both the vga dirty bits and the live migration dirty bits.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Stefano Stabellini

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
    /* sync and merge the two bitmaps between start_addr and end_addr */
}
Quoted 6 lines
Quoted 4 lines

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

That's how current kvm userspace works. It's also more correct, since the kvm bitmap feeds both the vga dirty bits and the live migration dirty bits.

Yes, I was thinking the same thing myself.

Regards,

Anthony Liguori

#16 lore raw
In reply to Avi Kivity
Quoted 16 lines · Avi Kivity
Quoted 15 lines · Stefano Stabellini

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
       /* sync and merge the two bitmaps between start_addr and end_addr

*/
}

Quoted 7 lines
Quoted 4 lines

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

That's how current kvm userspace works. It's also more correct, since the kvm bitmap feeds both the vga dirty bits and the live migration dirty bits.

My reason to do this way, was to be lazy about updating the dirty bitmap. But if the common case is to check for all pages in the region, then it won't matter much.

Quoted 7 lines

--
error compiling committee.c: too many arguments to function

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#20 lore raw
In reply to Avi Kivity
Quoted 14 lines · Avi Kivity
Quoted 13 lines · Stefano Stabellini

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
    /* sync and merge the two bitmaps between start_addr and end_addr */
}
Quoted 9 lines
Quoted 4 lines

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

That's how current kvm userspace works. It's also more correct, since the kvm bitmap feeds both the vga dirty bits and the live migration dirty bits.

That's also how currently qemu-xen works.
I am glad that we agree :)

#17 lore raw
In reply to Glauber Costa
Quoted 20 lines · Glauber Costa
Quoted 19 lines · Avi Kivity
Quoted 18 lines · Stefano Stabellini

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
       /* sync and merge the two bitmaps between start_addr and end_addr

*/
}

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

Quoted 6 lines
Quoted 2 lines

That's how current kvm userspace works. It's also more correct, since the kvm bitmap feeds both the vga dirty bits and the live migration dirty bits.

My reason to do this way, was to be lazy about updating the dirty bitmap. But if the common case is to check for all pages in the region, then it won't matter much.

We have an optimization not to always check for all pages in the region,
it is not really good looking, but it is working fine.
I could send a patch with this optimization after yours, it only affects vga.c.

#21 lore raw
In reply to Stefano Stabellini
Quoted 21 lines · Stefano Stabellini
Quoted 19 lines · Avi Kivity
Quoted 17 lines · Stefano Stabellini

Why don't you make qemu_physical_sync_dirty_bitmap take also and end address, and you merge the two bitmaps in this address range in this function, so you don't have to change cpu_physical_memory_get_dirty at all?

I am saying to do something like:

void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)

{
    /* sync and merge the two bitmaps between start_addr and end_addr */
}

then leave cpu_physical_memory_get_dirty untouched.
I would prefer this approch, it also leaves more space to other optimizations.

Quoted 8 lines
Quoted 4 lines

That's how current kvm userspace works. It's also more correct, since the kvm bitmap feeds both the vga dirty bits and the live migration dirty bits.

That's also how currently qemu-xen works.
I am glad that we agree :)

I'm attaching a new version. Let me know if it's better this way.

exec.c +11 -0
index ef1072b..2ed5e2b 100644
@@ -1823,6 +1823,17 @@ int cpu_physical_memory_get_dirty_tracking(void)
     return in_migration;
 }
 
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)
+{
+    if (kvm_enabled()) {
+        ram_addr_t addr;
+        kvm_physical_sync_dirty_bitmap(start_addr);
+        for (addr = start_addr; addr < end_addr; addr += TARGET_PAGE_SIZE)
+            phys_ram_dirty[addr >> TARGET_PAGE_BITS] |= kvm_physical_memory_get_dirty(addr);
+        
+    }
+}
+
 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
 {
     ram_addr_t ram_addr;
hw/cirrus_vga.c +77 -4

132 lines not on this page · raw message

hw/vga.c +6 -0
index 9540db0..1568be4 100644
@@ -28,6 +28,7 @@
 #include "vga_int.h"
 #include "pixel_ops.h"
 #include "qemu-timer.h"
+#include "kvm.h"
 
 //#define DEBUG_VGA
 //#define DEBUG_VGA_MEM
@@ -1568,6 +1569,8 @@ static void vga_draw_graphic(VGAState *s, int full_update)
     uint32_t v, addr1, addr;
     vga_draw_line_func *vga_draw_line;
 
+    qemu_physical_sync_dirty_bitmap(s->vram_offset, s->vram_offset + VGA_RAM_SIZE);
+
     full_update |= update_basic_params(s);
 
     s->get_resolution(s, &width, &height);
@@ -2102,6 +2105,9 @@ static void vga_map(PCIDevice *pci_dev, int region_num,
     } else {
         cpu_register_physical_memory(addr, s->vram_size, s->vram_offset);
     }
+
+    if (kvm_enabled())
+        kvm_log_start(addr, VGA_RAM_SIZE);
 }
 
 void vga_common_init(VGAState *s, DisplayState *ds, uint8_t *vga_ram_base,
hw/vga_int.h +2 -1
index 82a755e..91a8d77 100644
@@ -100,7 +100,7 @@ typedef void (* vga_update_retrace_info_fn)(struct VGAState *s);
 
 #define VGA_STATE_COMMON                                                \
     uint8_t *vram_ptr;                                                  \
-    unsigned long vram_offset;                                          \
+    ram_addr_t vram_offset;                                             \
     unsigned int vram_size;                                             \
     unsigned long bios_offset;                                          \
     unsigned int bios_size;                                             \
@@ -129,6 +129,7 @@ typedef void (* vga_update_retrace_info_fn)(struct VGAState *s);
     int dac_8bit;                                                       \
     uint8_t palette[768];                                               \
     int32_t bank_offset;                                                \
+    int vga_io_memory;                                             \
     int (*get_bpp)(struct VGAState *s);                                 \
     void (*get_offsets)(struct VGAState *s,                             \
                         uint32_t *pline_offset,                         \
kvm-all.c +178 -19

240 lines not on this page · raw message

kvm.h +3 -0
index 37102b4..6e2d9d2 100644
@@ -38,6 +38,9 @@ void kvm_set_phys_mem(target_phys_addr_t start_addr,
                       ram_addr_t size,
                       ram_addr_t phys_offset);
 
+int kvm_physical_memory_get_dirty(ram_addr_t addr);
+void kvm_physical_sync_dirty_bitmap(ram_addr_t start_addr);
+
 /* internal API */
 
 struct KVMState;
#22 lore raw
Quoted 5 lines · Glauber Costa
exec.c +0 -0
index ef1072b..2ed5e2b 100644

exec.c int cpu_physical_memory_get_dirty_tracking(void) · line 1832

Quoted 10 lines
exec.c +7 -0
@@ -1823,6 +1823,17 @@ int cpu_physical_memory_get_dirty_tracking(void)
     return in_migration;
 }
 
+void qemu_physical_sync_dirty_bitmap(ram_addr_t start_addr, ram_addr_t end_addr)
+{
+    if (kvm_enabled()) {
+        ram_addr_t addr;
+        kvm_physical_sync_dirty_bitmap(start_addr);
+        for (addr = start_addr; addr < end_addr; addr += TARGET_PAGE_SIZE)
+            phys_ram_dirty[addr >> TARGET_PAGE_BITS] |= kvm_physical_memory_get_dirty(addr);

Might as well push this into kvm-all.c so that this becomes:

if (kvm_enabled())
kvm_physical_sync_dirty_bitmap(start_addr, end_addr);

And this very likely will become a hook one day.

And I think the function should be renamed to cpu_physical_memory_update_dirty() to be more in line with the rest of the functions.

Quoted 36 lines
exec.c +3 -0
+    }
+}
+
 static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
 {
     ram_addr_t ram_addr;
hw/cirrus_vga.c +7 -0
index af9c9e6..bc1a3af 100644
@@ -31,6 +31,7 @@
 #include "pci.h"
 #include "console.h"
 #include "vga_int.h"
+#include "kvm.h"
 
 /*
  * TODO:
@@ -248,6 +249,9 @@ typedef struct CirrusVGAState {
     int cirrus_linear_io_addr;
     int cirrus_linear_bitblt_io_addr;
     int cirrus_mmio_io_addr;
+    uint32_t cirrus_lfb_addr;
+    uint32_t cirrus_lfb_end;
+    uint32_t cirrus_lfb_mapped;
     uint32_t cirrus_addr_mask;
     uint32_t linear_mmio_mask;
     uint8_t cirrus_shadow_gr0;
@@ -2618,6 +2622,69 @@ static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = {
     cirrus_linear_bitblt_writel,
 };
 
+static void set_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end, ram_addr_t target)
+{
+    /* align begin and end address */

hw/cirrus_vga.c static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = { · line 2637

Quoted 10 lines
hw/cirrus_vga.c +9 -0
+    begin = begin & TARGET_PAGE_MASK;
+    end = begin + VGA_RAM_SIZE;
+    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;
+
+    cpu_register_physical_memory(begin, end - begin, target);
+
+    if (kvm_enabled()) {
+        /* XXX: per-slot dirty tracking in qemu may get rid of it */
+        kvm_log_start(begin, end - begin);
 

I feel confident that we can get rid of this. We just need a good suggestion.

Quoted 6 lines
hw/cirrus_vga.c +6 -0
+    }
+}
+
+static void unset_vram_mapping(target_phys_addr_t begin, target_phys_addr_t end)
+{
+    /* align begin and end address */

hw/cirrus_vga.c static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = {

Quoted 10 lines
hw/cirrus_vga.c +9 -0
+    end = begin + VGA_RAM_SIZE;
+    begin = begin & TARGET_PAGE_MASK;
+    end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;
+
+    if (kvm_enabled()) {
+        /* XXX: per-slot dirty tracking in qemu may get rid of it */
+        kvm_log_stop(begin, end - begin);
+    }
+    cpu_register_physical_memory(begin, end - begin, IO_MEM_UNASSIGNED);
 

This is a little odd. We only ever toggle the vram mapping from normal IO_RAM to MMIO. We should never switch it to IO_MEM_UNASSIGNED.

Quoted 12 lines
hw/cirrus_vga.c +12 -0
+int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len)
+{
+
+        KVMState *s = kvm_state;
+        KVMSlot *mem = kvm_lookup_slot(s, phys_addr);
+
+        if (mem == NULL) {
+                fprintf(stderr, "BUG: %s: invalid parameters\n", __func__);
+                return -EINVAL;
+        }
+        dprintf("slot %d: disabling logging\n", mem->region.slot);
+

hw/cirrus_vga.c static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = { · line 2675

Quoted 10 lines
hw/cirrus_vga.c +9 -0
+        if (mem->logging_count--)
+                return 0;
+
+        return kvm_dirty_pages_log_change(mem,
+                                          0,
+                                          KVM_MEM_LOG_DIRTY_PAGES);
+}
+
+#define BITMAP_WORD(mem) (sizeof(*(mem)->dirty_bitmap) * 8)
 

this doesn't need to be a macro.

Can you split this patch up a bit? At least one patch containing the KVM slot changes. Also split out the cirrus verse standard VGA changes. I think if you switch things around to get rid of kvm_physical_get_dirty(), you'll find you can malloc the memory for the dirty bitmap at the time of sync() which should significantly reduce your overall complexity.

Regards,

Anthony Liguori

#18 lore raw
In reply to Glauber Costa
Quoted 4 lines · Glauber Costa

My reason to do this way, was to be lazy about updating the dirty bitmap. But if the common case is to check for all pages in the region, then it won't matter much.

In the common case we only check for the displayable area. We should split the region into two, one for the displayable area and one for the offscreen framebuffer (the boundary changes dynamically with the resolution). If qemu can tell us nobody is looking at the screen, we can dirty tracking altogether.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#19 lore raw
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Glauber Costa

My reason to do this way, was to be lazy about updating the dirty bitmap. But if the common case is to check for all pages in the region, then it won't matter much.

In the common case we only check for the displayable area. We should split the region into two, one for the displayable area and one for the offscreen framebuffer (the boundary changes dynamically with the resolution). If qemu can tell us nobody is looking at the screen, we can dirty tracking altogether.

Yeah, there already is an ds->idle in the DisplayState code. This is set by VNC and by SDL.

Of course, dirty updating is driven by vga_hw_update() which is driven by a timer who's interval is determined by ds->idle so we already do this in practice.

Regards,

Anthony Liguori

#23 lore raw
In reply to Glauber Costa
Quoted 5 lines · Glauber Costa
Quoted 2 lines

That's also how currently qemu-xen works.
I am glad that we agree :)

I'm attaching a new version. Let me know if it's better this way.

Yes, this is much better thanks.

I still have comments about possible improvements, so I wrote patch (against your patch).
This patch is not meant to be applied, is only meant to be read (I believe that C code is more meaningful than English :).

Some of the changes include:

- instead of adding cirrus_lfb_addr, add a more generic lfb_addr to VGAState, so that can be reused in the future for possible stdvga only mappings;

- instead of using cirrus_lfb_mapped as a boolean, use it as the mapping address, it is more useful that way;

- instead of keeping the kvm dirty map always enabled, enable it only when the framebuffer is linear and in graphical mode;

- look at the changes to vga.c, there is a simple check to reduce the dirty area to sync.

It would be nice to check if the last two changes are actually a performance improvement.

hw/cirrus_vga.c +11 -13
diff -r a5bcebe9e2bc hw/cirrus_vga.c
@@ -249,9 +249,6 @@
     int cirrus_linear_io_addr;
     int cirrus_linear_bitblt_io_addr;
     int cirrus_mmio_io_addr;
-    uint32_t cirrus_lfb_addr;
-    uint32_t cirrus_lfb_end;
-    uint32_t cirrus_lfb_mapped;
     uint32_t cirrus_addr_mask;
     uint32_t linear_mmio_mask;
     uint8_t cirrus_shadow_gr0;
@@ -2655,9 +2652,10 @@
 static void map_linear_vram(CirrusVGAState *s)
 {
 
-    if (!s->cirrus_lfb_mapped && s->cirrus_lfb_addr && s->cirrus_lfb_end) {
-        set_vram_mapping(s->cirrus_lfb_addr, s->cirrus_lfb_end, s->vram_offset);
-        s->cirrus_lfb_mapped = 1;
+    if (!s->map_addr && s->lfb_addr && s->lfb_end) {
+        set_vram_mapping(s->lfb_addr, s->lfb_end, s->vram_offset);
+        s->map_addr = s->lfb_addr;
+        s->map_end = s->lfb_end;
     }
     
     if(!(s->cirrus_srcptr != s->cirrus_srcptr_end)
@@ -2674,10 +2672,10 @@
 static void unmap_linear_vram(CirrusVGAState *s)
 {
 
-    if (s->cirrus_lfb_mapped && s->cirrus_lfb_addr && s->cirrus_lfb_end) {
-        unset_vram_mapping(s->cirrus_lfb_addr,
-                           s->cirrus_lfb_end);
-        s->cirrus_lfb_mapped = 0;
+    if (s->map_addr && s->lfb_addr && s->lfb_end) {
+        unset_vram_mapping(s->lfb_addr,
+                           s->lfb_end);
+        s->map_addr = s->map_end = 0;
     }
 
     cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
@@ -3331,9 +3329,9 @@
     cpu_register_physical_memory(addr + 0x1000000, 0x400000,
 				 s->cirrus_linear_bitblt_io_addr);
 
-    s->cirrus_lfb_mapped = 0;
-    s->cirrus_lfb_addr = addr;
-    s->cirrus_lfb_end = addr + VGA_RAM_SIZE;
+    s->map_addr = 0;
+    s->lfb_addr = addr;
+    s->lfb_end = addr + VGA_RAM_SIZE;
 }
 
 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,
hw/vga.c +34 -1
diff -r a5bcebe9e2bc hw/vga.c
@@ -1244,6 +1244,9 @@
     vga_draw_glyph8_func *vga_draw_glyph8;
     vga_draw_glyph9_func *vga_draw_glyph9;
 
+    /* disable dirty bit tracking */
+    // kvm_log_stop();
+
     full_update |= update_palette16(s);
     palette = s->last_palette;
 
@@ -1569,7 +1572,8 @@
     uint32_t v, addr1, addr;
     vga_draw_line_func *vga_draw_line;
 
-    qemu_physical_sync_dirty_bitmap(s->vram_offset, s->vram_offset + VGA_RAM_SIZE);
+    /* Enable dirty bit tracking */
+    // kvm_log_start
 
     full_update |= update_basic_params(s);
 
@@ -1657,6 +1661,31 @@
         s->cursor_invalidate(s);
 
     line_offset = s->line_offset;
+
+    if (s->lfb_addr) {
+        if (height - 1 > s->line_compare || multi_run || (s->cr[0x17] & 3) != 3) {
+            /* Tricky things happen, just track all video memory */
+            start = 0;
+            end = s->vram_size;
+        } else {
+            /* Tricky things won't have any effect, i.e. we are in the very simple
+             * (and very usual) case of a linear buffer. */
+            /* use page table dirty bit tracking for the LFB plus border */
+            start = (s->start_addr * 4) & TARGET_PAGE_MASK;
+            end = ((s->start_addr * 4 + height * line_offset) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
+        }
+
+        for (y = 0 ; y < start; y += TARGET_PAGE_SIZE)
+            /* We will not read that anyway. */
+            cpu_physical_memory_set_dirty(s->vram_offset + y);
+
+        qemu_physical_sync_dirty_bitmap(s->lfb_addr + y, end);
+
+        for ( ; y < s->vram_size; y += TARGET_PAGE_SIZE)
+            /* We will not read that anyway. */
+            cpu_physical_memory_set_dirty(s->vram_offset + y);
+    }
+
 #if 0
     printf("w=%d h=%d v=%d line_offset=%d cr[0x09]=0x%02x cr[0x17]=0x%02x linecmp=%d sr[0x01]=0x%02x\n",
            width, height, v, line_offset, s->cr[9], s->cr[0x17], s->line_compare, s->sr[0x01]);
@@ -1746,6 +1775,10 @@
         return;
     if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
         return;
+
+    /* disable dirty bit tracking */
+    // kvm_log_stop();
+
     if (s->ds->depth == 8)
         val = s->rgb_to_pixel(0, 0, 0);
     else
hw/vga_int.h +4 -0
diff -r a5bcebe9e2bc hw/vga_int.h
@@ -106,6 +106,10 @@
     unsigned int bios_size;                                             \
     target_phys_addr_t base_ctrl;                                       \
     int it_shift;                                                       \
+    unsigned long lfb_addr;                                             \
+    unsigned long lfb_end;                                              \
+    uint32_t map_addr;                                                  \
+    uint32_t map_end;                                                   \
     PCIDevice *pci_dev;                                                 \
     uint32_t latch;                                                     \
     uint8_t sr_index;                                                   \
#24 lore raw
Quoted 26 lines · Stefano Stabellini
Quoted 5 lines · Glauber Costa
Quoted 2 lines

That's also how currently qemu-xen works.
I am glad that we agree :)

I'm attaching a new version. Let me know if it's better this way.

Yes, this is much better thanks.

I still have comments about possible improvements, so I wrote patch (against your patch).
This patch is not meant to be applied, is only meant to be read (I believe that C code is more meaningful than English :).

Some of the changes include:

- instead of adding cirrus_lfb_addr, add a more generic lfb_addr to VGAState, so that can be reused in the future for possible stdvga only mappings;

- instead of using cirrus_lfb_mapped as a boolean, use it as the mapping address, it is more useful that way;

Quoted 8 lines

- instead of keeping the kvm dirty map always enabled, enable it only when the framebuffer is linear and in graphical mode;

- look at the changes to vga.c, there is a simple check to reduce the dirty area to sync.

It would be nice to check if the last two changes are actually a performance improvement.

I'm not opposed to the changes. I'll redo a patch and resend it today. However, I'm not planning to include the last optimization you sent. It is better that we do it in a separate commit. If you can get numbers, even better. I'll try it myself too.

Quoted 137 lines

diff -r a5bcebe9e2bc hw/cirrus_vga.c

hw/cirrus_vga.c +11 -13
@@ -249,9 +249,6 @@
     int cirrus_linear_io_addr;
     int cirrus_linear_bitblt_io_addr;
     int cirrus_mmio_io_addr;
-    uint32_t cirrus_lfb_addr;
-    uint32_t cirrus_lfb_end;
-    uint32_t cirrus_lfb_mapped;
     uint32_t cirrus_addr_mask;
     uint32_t linear_mmio_mask;
     uint8_t cirrus_shadow_gr0;
@@ -2655,9 +2652,10 @@
 static void map_linear_vram(CirrusVGAState *s)
 {
 
-    if (!s->cirrus_lfb_mapped && s->cirrus_lfb_addr && s->cirrus_lfb_end) {
-        set_vram_mapping(s->cirrus_lfb_addr, s->cirrus_lfb_end, s->vram_offset);
-        s->cirrus_lfb_mapped = 1;
+    if (!s->map_addr && s->lfb_addr && s->lfb_end) {
+        set_vram_mapping(s->lfb_addr, s->lfb_end, s->vram_offset);
+        s->map_addr = s->lfb_addr;
+        s->map_end = s->lfb_end;
     }
 
     if(!(s->cirrus_srcptr != s->cirrus_srcptr_end)
@@ -2674,10 +2672,10 @@
 static void unmap_linear_vram(CirrusVGAState *s)
 {
 
-    if (s->cirrus_lfb_mapped && s->cirrus_lfb_addr && s->cirrus_lfb_end) {
-        unset_vram_mapping(s->cirrus_lfb_addr,
-                           s->cirrus_lfb_end);
-        s->cirrus_lfb_mapped = 0;
+    if (s->map_addr && s->lfb_addr && s->lfb_end) {
+        unset_vram_mapping(s->lfb_addr,
+                           s->lfb_end);
+        s->map_addr = s->map_end = 0;
     }
 
     cpu_register_physical_memory(isa_mem_base + 0x000a0000, 0x20000,
@@ -3331,9 +3329,9 @@
     cpu_register_physical_memory(addr + 0x1000000, 0x400000,
 				 s->cirrus_linear_bitblt_io_addr);
 
-    s->cirrus_lfb_mapped = 0;
-    s->cirrus_lfb_addr = addr;
-    s->cirrus_lfb_end = addr + VGA_RAM_SIZE;
+    s->map_addr = 0;
+    s->lfb_addr = addr;
+    s->lfb_end = addr + VGA_RAM_SIZE;
 }
 
 static void cirrus_pci_mmio_map(PCIDevice *d, int region_num,

diff -r a5bcebe9e2bc hw/vga.c

hw/vga.c +34 -1
@@ -1244,6 +1244,9 @@
     vga_draw_glyph8_func *vga_draw_glyph8;
     vga_draw_glyph9_func *vga_draw_glyph9;
 
+    /* disable dirty bit tracking */
+    // kvm_log_stop();
+
     full_update |= update_palette16(s);
     palette = s->last_palette;
 
@@ -1569,7 +1572,8 @@
     uint32_t v, addr1, addr;
     vga_draw_line_func *vga_draw_line;
 
-    qemu_physical_sync_dirty_bitmap(s->vram_offset, s->vram_offset + VGA_RAM_SIZE);
+    /* Enable dirty bit tracking */
+    // kvm_log_start
 
     full_update |= update_basic_params(s);
 
@@ -1657,6 +1661,31 @@
         s->cursor_invalidate(s);
 
     line_offset = s->line_offset;
+
+    if (s->lfb_addr) {
+        if (height - 1 > s->line_compare || multi_run || (s->cr[0x17] & 3) != 3) {
+            /* Tricky things happen, just track all video memory */
+            start = 0;
+            end = s->vram_size;
+        } else {
+            /* Tricky things won't have any effect, i.e. we are in the very simple
+             * (and very usual) case of a linear buffer. */
+            /* use page table dirty bit tracking for the LFB plus border */
+            start = (s->start_addr * 4) & TARGET_PAGE_MASK;
+            end = ((s->start_addr * 4 + height * line_offset) + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
+        }
+
+        for (y = 0 ; y < start; y += TARGET_PAGE_SIZE)
+            /* We will not read that anyway. */
+            cpu_physical_memory_set_dirty(s->vram_offset + y);
+
+        qemu_physical_sync_dirty_bitmap(s->lfb_addr + y, end);
+
+        for ( ; y < s->vram_size; y += TARGET_PAGE_SIZE)
+            /* We will not read that anyway. */
+            cpu_physical_memory_set_dirty(s->vram_offset + y);
+    }
+
 #if 0
     printf("w=%d h=%d v=%d line_offset=%d cr[0x09]=0x%02x cr[0x17]=0x%02x linecmp=%d sr[0x01]=0x%02x\n",
            width, height, v, line_offset, s->cr[9], s->cr[0x17], s->line_compare, s->sr[0x01]);
@@ -1746,6 +1775,10 @@
         return;
     if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
         return;
+
+    /* disable dirty bit tracking */
+    // kvm_log_stop();
+
     if (s->ds->depth == 8)
         val = s->rgb_to_pixel(0, 0, 0);
     else

diff -r a5bcebe9e2bc hw/vga_int.h

hw/vga_int.h +4 -0
@@ -106,6 +106,10 @@
     unsigned int bios_size;                                             \
     target_phys_addr_t base_ctrl;                                       \
     int it_shift;                                                       \
+    unsigned long lfb_addr;                                             \
+    unsigned long lfb_end;                                              \
+    uint32_t map_addr;                                                  \
+    uint32_t map_end;                                                   \
     PCIDevice *pci_dev;                                                 \
     uint32_t latch;                                                     \
     uint8_t sr_index;                                                   \
#25 lore raw
Quoted 31 lines · Glauber Costa
Quoted 30 lines · Stefano Stabellini
Quoted 4 lines · Glauber Costa
Quoted 2 lines

That's also how currently qemu-xen works.
I am glad that we agree :)

I'm attaching a new version. Let me know if it's better this way.

Yes, this is much better thanks.

I still have comments about possible improvements, so I wrote patch (against your patch).
This patch is not meant to be applied, is only meant to be read (I believe that C code is more meaningful than English :).

Some of the changes include:

- instead of adding cirrus_lfb_addr, add a more generic lfb_addr to VGAState, so that can be reused in the future for possible stdvga only mappings;

- instead of using cirrus_lfb_mapped as a boolean, use it as the mapping address, it is more useful that way;

- instead of keeping the kvm dirty map always enabled, enable it only when the framebuffer is linear and in graphical mode;

- look at the changes to vga.c, there is a simple check to reduce the dirty area to sync.

Quoted 6 lines
Quoted 2 lines

It would be nice to check if the last two changes are actually a performance improvement.

I'm not opposed to the changes. I'll redo a patch and resend it today. However, I'm not planning to include the last optimization you sent. It is better that we do it in a separate commit. If you can get numbers, even better. I'll try it myself too.

Fine with me.