os: don't corrupt pre-existing memory-backend data with prealloc
PATCH 1/1 os: don't corrupt pre-existing memory-backend data with prealloc
When using a memory-backend object with prealloc turned on, QEMU will memset() the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.
Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
util/oslib-posix.c +2 -1
@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val); } }
Signature · 1 line
2.9.3
Quoted 28 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 359
Quoted 6 lines
util/oslib-posix.c +2
-1@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val);
I think you wanted:
memset(area + (hpagesize * i), val, 1);
because what you are suggesting will overwrite even more than the first byte with zeroes.
Michal
Quoted 38 lines · Michal Privoznik
Quoted 28 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 359
Quoted 6 lines
util/oslib-posix.c +2
-1@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val);I think you wanted:
Quoted 4 lines
memset(area + (hpagesize * i), val, 1);because what you are suggesting will overwrite even more than the first byte with zeroes.
Lol, yes, I'm stupid.
Anyway, rather than repost this yet, I'm interested if this is actually the right way to fix the problem or if we should do something totally different....
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 41 lines · Daniel P. Berrange
Quoted 40 lines · Michal Privoznik
Quoted 28 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 359
Quoted 6 lines
util/oslib-posix.c +2
-1@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val);I think you wanted:
memset(area + (hpagesize * i), val, 1);
Quoted 8 lines
Quoted 2 lines
because what you are suggesting will overwrite even more than the first byte with zeroes.
Lol, yes, I'm stupid.
Anyway, rather than repost this yet, I'm interested if this is actually the right way to fix the problem or if we should do something totally different....
So, I've done some analysis and if the optimizations are enabled, this whole body is optimized away. Not the loop though. Here's what I've tested:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int ret = EXIT_FAILURE;
unsigned char *ptr;
size_t i, j;
if (!(ptr = malloc(1024 * 4))) {
perror("malloc");
goto cleanup;
}
for (i = 0; i < 4; i++) {
unsigned char val = ptr[i*1024];
memset(ptr + i * 1024, val, 1);
}
ret = EXIT_SUCCESS;
cleanup:
free(ptr);
return ret;
}But if I make @val volatile, I can enforce the compiler to include the body of the loop and actually read and write some bytes. BTW: if I replace memset with *(ptr + i * 1024) = val; and don't make @val volatile even the loop is optimized away.
I was compiling with:
gcc -S -O3 -o test.S test.c
Michal
Quoted 81 lines · Michal Privoznik
Quoted 41 lines · Daniel P. Berrange
Quoted 40 lines · Michal Privoznik
Quoted 28 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 359
Quoted 6 lines
util/oslib-posix.c +2
-1@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val);I think you wanted:
memset(area + (hpagesize * i), val, 1);Quoted 8 lines
Quoted 2 lines
because what you are suggesting will overwrite even more than the first byte with zeroes.
Lol, yes, I'm stupid.
Anyway, rather than repost this yet, I'm interested if this is actually the right way to fix the problem or if we should do something totally different....
So, I've done some analysis and if the optimizations are enabled, this whole body is optimized away. Not the loop though. Here's what I've tested:
#include <
stdio.h>
#include <stdlib.h>
#include <string.h>int
main(int argc, char *argv[]){ int ret = EXIT_FAILURE; unsigned char *ptr; size_t i, j; if (!(ptr = malloc(1024 * 4))) { perror("malloc"); goto cleanup; } for (i = 0; i < 4; i++) { unsigned char val = ptr[i*1024]; memset(ptr + i * 1024, val, 1); } ret = EXIT_SUCCESS; cleanup: free(ptr); return ret; }
Quoted 4 lines
But if I make @val volatile, I can enforce the compiler to include the body of the loop and actually read and write some bytes. BTW: if I replace memset with *(ptr + i * 1024) = val; and don't make @val volatile even the loop is optimized away.
Ok, yeah, it makes sense that the compiler can optimize that away without volatile. I wonder if adding volatile has much of a performance impact on this loop...
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 82 lines · * Daniel P. Berrange ([email protected]) wrote:
Quoted 81 lines · Michal Privoznik
Quoted 41 lines · Daniel P. Berrange
Quoted 40 lines · Michal Privoznik
Quoted 28 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 359
Quoted 6 lines
util/oslib-posix.c +2
-1@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val);I think you wanted:
memset(area + (hpagesize * i), val, 1);Quoted 8 lines
Quoted 2 lines
because what you are suggesting will overwrite even more than the first byte with zeroes.
Lol, yes, I'm stupid.
Anyway, rather than repost this yet, I'm interested if this is actually the right way to fix the problem or if we should do something totally different....
So, I've done some analysis and if the optimizations are enabled, this whole body is optimized away. Not the loop though. Here's what I've tested:
#include <
stdio.h>
#include <stdlib.h>
#include <string.h>int
main(int argc, char *argv[]){ int ret = EXIT_FAILURE; unsigned char *ptr; size_t i, j; if (!(ptr = malloc(1024 * 4))) { perror("malloc"); goto cleanup; } for (i = 0; i < 4; i++) { unsigned char val = ptr[i*1024]; memset(ptr + i * 1024, val, 1); } ret = EXIT_SUCCESS; cleanup: free(ptr); return ret; }
Quoted 8 lines
Quoted 4 lines
But if I make @val volatile, I can enforce the compiler to include the body of the loop and actually read and write some bytes. BTW: if I replace memset with *(ptr + i * 1024) = val; and don't make @val volatile even the loop is optimized away.
Ok, yeah, it makes sense that the compiler can optimize that away without volatile. I wonder if adding volatile has much of a performance impact on this loop...
I don't think we have anything else in QEMU to do it (other than atomic's
but we don't need this to be atomic). I don't think the use of memset()
helps, because the compiler is free to optimise that out as well; so I think 'volatile' is a reasonable use (although I seem to have a soft-spot for volatile and I know everyone else tells me I'm mad).
However, note the performance of this loop is important as shown by Jitendra's recent patchset to parallelise it.
I wonder if MADV_WILLNEED makes any difference.
Dave
Quoted 7 lines
Regards,
DanielSignature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 3 lines
Ok, yeah, it makes sense that the compiler can optimize that away without volatile. I wonder if adding volatile has much of a performance impact on this loop...
I don't think we have anything else in QEMU to do it (other than atomic's
but we don't need this to be atomic). I don't think the use ofmemset()
helps, because the compiler is free to optimise that out as well; so I think 'volatile' is a reasonable use (although I seem to have a soft-spot for volatile and I know everyone else tells me I'm mad).
Yes, I think it is fine to use volatile here.
Paolo
Quoted 28 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0
util/oslib-posix.c void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp) · line 359
Quoted 6 lines
util/oslib-posix.c +2
-1@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)/* MAP_POPULATE silently ignores failures */ for (i =0 ; i < numpages; i++) { -memset (area + (hpagesize * i),0 ,1 ); +char val = *(area + (hpagesize * i)); +memset (area + (hpagesize * i),0 , val);
Please include a comment in the final patch explaining why we want to preserve memory contents.
In the case of NVDIMM I'm not sure if the memset is needed at all. The memory already exists - no new pages need to be allocated by the kernel. We just want the page table entries to be populated for the NVDIMM when -mem-prealloc is used.
Perhaps Andrea or Rik have ideas on improving the kernel interface and whether mmap(MAP_POPULATE) should be used with NVDIMM instead of this userspace "touch every page" workaround?
Stefan
Quoted 48 lines · Stefan Hajnoczi
Quoted 29 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)Quoted 6 lines
/*MAP_POPULATEsilently ignores failures */ for (i = 0; i < numpages; i++) {
- memset(area + (hpagesize * i), 0, 1); + char val = *(area + (hpagesize * i)); + memset(area + (hpagesize * i), 0, val);Please include a comment in the final patch explaining why we want to preserve memory contents.
In the case of NVDIMM I'm not sure if the memset is needed at all. The
memory already exists - no new pages need to be allocated by the kernel.
We just want the page table entries to be populated for the NVDIMM when
-mem-prealloc is used.
Quoted 4 lines
Perhaps Andrea or Rik have ideas on improving the kernel interface and
whethermmap(MAP_POPULATE)should be used with NVDIMM instead of this userspace "touch every page" workaround?
Why do we need the page table entries to be populated in advance at all?
The high cost of the page fault for regular memory is zeroing out the memory pages before we give them to userspace.
Simply faulting in the NVDIMM memory as it is touched may make more sense than treating it like DRAM, especially given that with DAX, NVDIMM areas may be orders of magnitude larger than RAM, and we really do not want to set up all the page tables for every part of the guest DAX "disk".
Quoted 49 lines · Rik van Riel
Quoted 48 lines · Stefan Hajnoczi
Quoted 29 lines · Daniel P. Berrange
When using a memory-backend object with prealloc turned on, QEMU will
memset()the first byte in every memory page to zero. While this might have been acceptable for memory backends associated with RAM, this corrupts application data for NVDIMMs.Instead of setting every page to zero, read the current byte value and then just write that same value back, so we are not corrupting the original data.
Signed-off-by Daniel P. Berrange <[email protected]>---
I'm unclear if this is actually still safe in practice ? Is the compiler permitted to optimize away the read+write since it doesn't change the memory value. I'd hope not, but I've been surprised before...
IMHO this is another factor in favour of requesting an API from the kernel to provide the prealloc behaviour we want.
util/oslib-posix.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)util/oslib-posix.c +0
-0@@ -355,7 +355,8 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)Quoted 6 lines
/*MAP_POPULATEsilently ignores failures */ for (i = 0; i < numpages; i++) {
- memset(area + (hpagesize * i), 0, 1); + char val = *(area + (hpagesize * i)); + memset(area + (hpagesize * i), 0, val);Please include a comment in the final patch explaining why we want to preserve memory contents.
In the case of NVDIMM I'm not sure if the memset is needed at all. The
memory already exists - no new pages need to be allocated by the kernel.
We just want the page table entries to be populated for the NVDIMM when
-mem-prealloc is used.
Quoted 7 lines
Quoted 4 lines
Perhaps Andrea or Rik have ideas on improving the kernel interface and
whethermmap(MAP_POPULATE)should be used with NVDIMM instead of this userspace "touch every page" workaround?Why do we need the page table entries to be populated in advance at all?
It is a choice apps using QEMU have - they can tell QEMU whether they want prealloc or not - if they decide they do want it, then we should not be corrupting the data.
Quoted 3 lines
The high cost of the page fault for regular memory is zeroing out the memory pages before we give them to userspace.
NVDIMM in the guest might be backed by regular memory in the host - QEMU doesn't require use of NVDIMM in the host.
Quoted 6 lines
Simply faulting in the NVDIMM memory as it is touched may make more sense than treating it like DRAM, especially given that with DAX, NVDIMM areas may be orders of magnitude larger than RAM, and we really do not want to set up all the page tables for every part of the guest DAX "disk".
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|