Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-31 Thread Paolo Bonzini
On 31/07/19 10:19, Thomas Huth wrote:
>>> @@ -293,6 +341,10 @@ static void run_test(enum vm_guest_mode mode, unsigned 
>>> long iterations,
>>>  * case where the size is not aligned to 64 pages.
>>>  */
>>> guest_num_pages = (1ul << (30 - guest_page_shift)) + 16;
>>> +#ifdef __s390x__
>>> +   /* Round up to multiple of 1M (segment size) */
>>> +   guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
>> We could maybe do this for all architectures as well.
> It's really only needed on s390x, so I think we should keep the #ifdef here.

Yes, on non-s390 we should keep covering the case where the size is not
a multiple of BITS_PER_LONG.

Paolo


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-31 Thread David Hildenbrand
On 30.07.19 20:04, Christian Borntraeger wrote:
> 
> 
> On 30.07.19 19:11, Thomas Huth wrote:
>> On 30/07/2019 16.57, Christian Borntraeger wrote:
>>>
>>>
>>> On 30.07.19 12:01, Thomas Huth wrote:
 To run the dirty_log_test on s390x, we have to make sure that we
 access the dirty log bitmap with little endian byte ordering and
 we have to properly align the memslot of the guest.
 Also all dirty bits of a segment are set once on s390x when one
 of the pages of a segment are written to for the first time, so
 we have to make sure that we touch all pages during the first
 iteration to keep the test in sync here.
>>>
>>> While this fixes the test (and the migration does work fine), it still
>>> means that s390x overindicates the dirty bit for sparsely populated
>>> 1M segments. It is just a performance issue, but maybe we should try 
>>> to get this fixed.
>>
>> I hope you don't expect me to fix this - the gmap code is really not my
>> turf...
> 
> No, this is clearly on our turf. 

FWIW, we share the pagetables with the userspace process. We mark a page
as dirty (PGSTE_UC_BIT) when
- We modify the storage key
- We map a PTE as RW (pgste_set_pte())

I assume all PTEs of the segment are mapped RW (for example, if user
space wrote to such a PTE), that is why we have the PGSTE_UC_BIT bit set.

As PGSTE_UC_BIT also tracks what userspace did, not only KVM via the
GMAP, this might indeed be correct.

-- 

Thanks,

David / dhildenb


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-31 Thread Andrew Jones
On Wed, Jul 31, 2019 at 10:19:57AM +0200, Thomas Huth wrote:
> On 30/07/2019 12.57, Andrew Jones wrote:
> > On Tue, Jul 30, 2019 at 12:01:12PM +0200, Thomas Huth wrote:
> >> To run the dirty_log_test on s390x, we have to make sure that we
> >> access the dirty log bitmap with little endian byte ordering and
> >> we have to properly align the memslot of the guest.
> >> Also all dirty bits of a segment are set once on s390x when one
> >> of the pages of a segment are written to for the first time, so
> >> we have to make sure that we touch all pages during the first
> >> iteration to keep the test in sync here.
> >>
> >> Signed-off-by: Thomas Huth 
> >> ---
> [...]
> >> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c 
> >> b/tools/testing/selftests/kvm/dirty_log_test.c
> >> index ceb52b952637..7a1223ad0ff3 100644
> >> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> >> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> >> @@ -26,9 +26,22 @@
> >>  /* The memory slot index to track dirty pages */
> >>  #define TEST_MEM_SLOT_INDEX   1
> >>  
> >> +#ifdef __s390x__
> >> +
> >> +/*
> >> + * On s390x, the ELF program is sometimes linked at 0x8000, so we can
> >> + * not use 0x4000 here without overlapping into that region. Thus 
> >> let's
> >> + * use 0xc000 as base address there instead.
> >> + */
> >> +#define DEFAULT_GUEST_TEST_MEM0xc000
> > 
> > I think both x86 and aarch64 should be ok with this offset. If testing
> > proves it does, then we can just change it for all architecture.
> 
> Ok. It seems to work on x86 - could you please check aarch64, since I
> don't have such a system available right now?

Tested it. It works on aarch64 too.

> 
> >> +/* Dirty bitmaps are always little endian, so we need to swap on big 
> >> endian */
> >> +#if defined(__s390x__)
> >> +# define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
> >> +# define test_bit_le(nr, addr) \
> >> +  test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> >> +# define set_bit_le(nr, addr) \
> >> +  set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> >> +# define clear_bit_le(nr, addr) \
> >> +  clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> >> +# define test_and_set_bit_le(nr, addr) \
> >> +  test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> >> +# define test_and_clear_bit_le(nr, addr) \
> >> +  test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> >> +#else
> >> +# define test_bit_le  test_bit
> >> +# define set_bit_le   set_bit
> >> +# define clear_bit_le clear_bit
> >> +# define test_and_set_bit_le  test_and_set_bit
> >> +# define test_and_clear_bit_letest_and_clear_bit
> >> +#endif
> > 
> > nit: does the formatting above look right after applying the patch?
> 
> It looked ok to me, but I can add some more tabs to even make it nicer :)
> 
> >> @@ -293,6 +341,10 @@ static void run_test(enum vm_guest_mode mode, 
> >> unsigned long iterations,
> >> * case where the size is not aligned to 64 pages.
> >> */
> >>guest_num_pages = (1ul << (30 - guest_page_shift)) + 16;
> >> +#ifdef __s390x__
> >> +  /* Round up to multiple of 1M (segment size) */
> >> +  guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
> > 
> > We could maybe do this for all architectures as well.
> 
> It's really only needed on s390x, so I think we should keep the #ifdef here.
>

OK

Thanks,
drew 


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-31 Thread Thomas Huth
On 30/07/2019 12.57, Andrew Jones wrote:
> On Tue, Jul 30, 2019 at 12:01:12PM +0200, Thomas Huth wrote:
>> To run the dirty_log_test on s390x, we have to make sure that we
>> access the dirty log bitmap with little endian byte ordering and
>> we have to properly align the memslot of the guest.
>> Also all dirty bits of a segment are set once on s390x when one
>> of the pages of a segment are written to for the first time, so
>> we have to make sure that we touch all pages during the first
>> iteration to keep the test in sync here.
>>
>> Signed-off-by: Thomas Huth 
>> ---
[...]
>> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c 
>> b/tools/testing/selftests/kvm/dirty_log_test.c
>> index ceb52b952637..7a1223ad0ff3 100644
>> --- a/tools/testing/selftests/kvm/dirty_log_test.c
>> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
>> @@ -26,9 +26,22 @@
>>  /* The memory slot index to track dirty pages */
>>  #define TEST_MEM_SLOT_INDEX 1
>>  
>> +#ifdef __s390x__
>> +
>> +/*
>> + * On s390x, the ELF program is sometimes linked at 0x8000, so we can
>> + * not use 0x4000 here without overlapping into that region. Thus let's
>> + * use 0xc000 as base address there instead.
>> + */
>> +#define DEFAULT_GUEST_TEST_MEM  0xc000
> 
> I think both x86 and aarch64 should be ok with this offset. If testing
> proves it does, then we can just change it for all architecture.

Ok. It seems to work on x86 - could you please check aarch64, since I
don't have such a system available right now?

>> +/* Dirty bitmaps are always little endian, so we need to swap on big endian 
>> */
>> +#if defined(__s390x__)
>> +# define BITOP_LE_SWIZZLE   ((BITS_PER_LONG-1) & ~0x7)
>> +# define test_bit_le(nr, addr) \
>> +test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
>> +# define set_bit_le(nr, addr) \
>> +set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
>> +# define clear_bit_le(nr, addr) \
>> +clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
>> +# define test_and_set_bit_le(nr, addr) \
>> +test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
>> +# define test_and_clear_bit_le(nr, addr) \
>> +test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
>> +#else
>> +# define test_bit_letest_bit
>> +# define set_bit_le set_bit
>> +# define clear_bit_le   clear_bit
>> +# define test_and_set_bit_letest_and_set_bit
>> +# define test_and_clear_bit_le  test_and_clear_bit
>> +#endif
> 
> nit: does the formatting above look right after applying the patch?

It looked ok to me, but I can add some more tabs to even make it nicer :)

>> @@ -293,6 +341,10 @@ static void run_test(enum vm_guest_mode mode, unsigned 
>> long iterations,
>>   * case where the size is not aligned to 64 pages.
>>   */
>>  guest_num_pages = (1ul << (30 - guest_page_shift)) + 16;
>> +#ifdef __s390x__
>> +/* Round up to multiple of 1M (segment size) */
>> +guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
> 
> We could maybe do this for all architectures as well.

It's really only needed on s390x, so I think we should keep the #ifdef here.

 Thomas


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-31 Thread Christian Borntraeger



On 30.07.19 21:06, Paolo Bonzini wrote:
> On 30/07/19 16:57, Christian Borntraeger wrote:
>> While this fixes the test (and the migration does work fine), it still
>> means that s390x overindicates the dirty bit for sparsely populated
>> 1M segments. It is just a performance issue, but maybe we should try 
>> to get this fixed. Not sure what to do here to remember us about this, 
>> adding this as expected fail?
> 
> if it's only on the first access after enabling dirty logging, that
> shouldn't be that bad?

No its not bad, but certainly something to improve if time allows. 



Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Paolo Bonzini
On 30/07/19 16:57, Christian Borntraeger wrote:
> While this fixes the test (and the migration does work fine), it still
> means that s390x overindicates the dirty bit for sparsely populated
> 1M segments. It is just a performance issue, but maybe we should try 
> to get this fixed. Not sure what to do here to remember us about this, 
> adding this as expected fail?

if it's only on the first access after enabling dirty logging, that
shouldn't be that bad?

Paolo


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Christian Borntraeger



On 30.07.19 19:11, Thomas Huth wrote:
> On 30/07/2019 16.57, Christian Borntraeger wrote:
>>
>>
>> On 30.07.19 12:01, Thomas Huth wrote:
>>> To run the dirty_log_test on s390x, we have to make sure that we
>>> access the dirty log bitmap with little endian byte ordering and
>>> we have to properly align the memslot of the guest.
>>> Also all dirty bits of a segment are set once on s390x when one
>>> of the pages of a segment are written to for the first time, so
>>> we have to make sure that we touch all pages during the first
>>> iteration to keep the test in sync here.
>>
>> While this fixes the test (and the migration does work fine), it still
>> means that s390x overindicates the dirty bit for sparsely populated
>> 1M segments. It is just a performance issue, but maybe we should try 
>> to get this fixed.
> 
> I hope you don't expect me to fix this - the gmap code is really not my
> turf...

No, this is clearly on our turf. 
> 
>> Not sure what to do here to remember us about this, 
>> adding this as expected fail?
> 
> There is no such thing like an expected failure in KVM selftests -
> that's only available in kvm-unit-tests.
> 
> So the only option that I currently see is to add a printf("TODO: ...")
> on s390x here... would that work for you?

Maybe just keep this as is - we should just not forget about it.



Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Thomas Huth
On 30/07/2019 16.57, Christian Borntraeger wrote:
> 
> 
> On 30.07.19 12:01, Thomas Huth wrote:
>> To run the dirty_log_test on s390x, we have to make sure that we
>> access the dirty log bitmap with little endian byte ordering and
>> we have to properly align the memslot of the guest.
>> Also all dirty bits of a segment are set once on s390x when one
>> of the pages of a segment are written to for the first time, so
>> we have to make sure that we touch all pages during the first
>> iteration to keep the test in sync here.
> 
> While this fixes the test (and the migration does work fine), it still
> means that s390x overindicates the dirty bit for sparsely populated
> 1M segments. It is just a performance issue, but maybe we should try 
> to get this fixed.

I hope you don't expect me to fix this - the gmap code is really not my
turf...

> Not sure what to do here to remember us about this, 
> adding this as expected fail?

There is no such thing like an expected failure in KVM selftests -
that's only available in kvm-unit-tests.

So the only option that I currently see is to add a printf("TODO: ...")
on s390x here... would that work for you?

 Thomas


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Christian Borntraeger



On 30.07.19 12:01, Thomas Huth wrote:
> To run the dirty_log_test on s390x, we have to make sure that we
> access the dirty log bitmap with little endian byte ordering and
> we have to properly align the memslot of the guest.
> Also all dirty bits of a segment are set once on s390x when one
> of the pages of a segment are written to for the first time, so
> we have to make sure that we touch all pages during the first
> iteration to keep the test in sync here.

While this fixes the test (and the migration does work fine), it still
means that s390x overindicates the dirty bit for sparsely populated
1M segments. It is just a performance issue, but maybe we should try 
to get this fixed. Not sure what to do here to remember us about this, 
adding this as expected fail?
> 
> Signed-off-by: Thomas Huth 
> ---
>  tools/testing/selftests/kvm/Makefile |  1 +
>  tools/testing/selftests/kvm/dirty_log_test.c | 70 ++--
>  2 files changed, 66 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/Makefile 
> b/tools/testing/selftests/kvm/Makefile
> index ba7849751989..ac7e63e00fee 100644
> --- a/tools/testing/selftests/kvm/Makefile
> +++ b/tools/testing/selftests/kvm/Makefile
> @@ -33,6 +33,7 @@ TEST_GEN_PROGS_aarch64 += dirty_log_test
>  TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
>  
>  TEST_GEN_PROGS_s390x += s390x/sync_regs_test
> +TEST_GEN_PROGS_s390x += dirty_log_test
>  TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
>  
>  TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c 
> b/tools/testing/selftests/kvm/dirty_log_test.c
> index ceb52b952637..7a1223ad0ff3 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -26,9 +26,22 @@
>  /* The memory slot index to track dirty pages */
>  #define TEST_MEM_SLOT_INDEX  1
>  
> +#ifdef __s390x__
> +
> +/*
> + * On s390x, the ELF program is sometimes linked at 0x8000, so we can
> + * not use 0x4000 here without overlapping into that region. Thus let's
> + * use 0xc000 as base address there instead.
> + */
> +#define DEFAULT_GUEST_TEST_MEM   0xc000
> +
> +#else
> +
>  /* Default guest test memory offset, 1G */
>  #define DEFAULT_GUEST_TEST_MEM   0x4000
>  
> +#endif
> +
>  /* How many pages to dirty for each guest loop */
>  #define TEST_PAGES_PER_LOOP  1024
>  
> @@ -38,6 +51,27 @@
>  /* Interval for each host loop (ms) */
>  #define TEST_HOST_LOOP_INTERVAL  10UL
>  
> +/* Dirty bitmaps are always little endian, so we need to swap on big endian 
> */
> +#if defined(__s390x__)
> +# define BITOP_LE_SWIZZLE((BITS_PER_LONG-1) & ~0x7)
> +# define test_bit_le(nr, addr) \
> + test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define set_bit_le(nr, addr) \
> + set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define clear_bit_le(nr, addr) \
> + clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define test_and_set_bit_le(nr, addr) \
> + test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define test_and_clear_bit_le(nr, addr) \
> + test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +#else
> +# define test_bit_le test_bit
> +# define set_bit_le  set_bit
> +# define clear_bit_leclear_bit
> +# define test_and_set_bit_le test_and_set_bit
> +# define test_and_clear_bit_le   test_and_clear_bit
> +#endif
> +
>  /*
>   * Guest/Host shared variables. Ensure addr_gva2hva() and/or
>   * sync_global_to/from_guest() are used when accessing from
> @@ -69,11 +103,25 @@ static uint64_t guest_test_virt_mem = 
> DEFAULT_GUEST_TEST_MEM;
>   */
>  static void guest_code(void)
>  {
> + uint64_t addr;
>   int i;
>  
> +#ifdef __s390x__
> + /*
> +  * On s390x, all pages of a 1M segment are initially marked as dirty
> +  * when a page of the segment is written to for the very first time.
> +  * To compensate this specialty in this test, we need to touch all
> +  * pages during the first iteration.
> +  */
> + for (i = 0; i < guest_num_pages; i++) {
> + addr = guest_test_virt_mem + i * guest_page_size;
> + *(uint64_t *)addr = READ_ONCE(iteration);
> + }
> +#endif
> +
>   while (true) {
>   for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
> - uint64_t addr = guest_test_virt_mem;
> + addr = guest_test_virt_mem;
>   addr += (READ_ONCE(random_array[i]) % guest_num_pages)
>   * guest_page_size;
>   addr &= ~(host_page_size - 1);
> @@ -158,15 +206,15 @@ static void vm_dirty_log_verify(unsigned long *bmap)
>   value_ptr = host_test_mem + page * host_page_size;
>  
>   /* If this is a special page that we were tracking... */
> - if (test_and_clear_bit(page, host_bmap_track)) {
> + if (test_and_clear_bit_le(page, host_bmap_track)) {

Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Paolo Bonzini
On 30/07/19 12:01, Thomas Huth wrote:
> +#ifdef __s390x__
> + /*
> +  * On s390x, all pages of a 1M segment are initially marked as dirty
> +  * when a page of the segment is written to for the very first time.
> +  * To compensate this specialty in this test, we need to touch all
> +  * pages during the first iteration.
> +  */
> + for (i = 0; i < guest_num_pages; i++) {
> + addr = guest_test_virt_mem + i * guest_page_size;
> + *(uint64_t *)addr = READ_ONCE(iteration);
> + }
> +#endif

Go ahead and make this unconditional.

Paolo


Re: [PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Andrew Jones
On Tue, Jul 30, 2019 at 12:01:12PM +0200, Thomas Huth wrote:
> To run the dirty_log_test on s390x, we have to make sure that we
> access the dirty log bitmap with little endian byte ordering and
> we have to properly align the memslot of the guest.
> Also all dirty bits of a segment are set once on s390x when one
> of the pages of a segment are written to for the first time, so
> we have to make sure that we touch all pages during the first
> iteration to keep the test in sync here.
> 
> Signed-off-by: Thomas Huth 
> ---
>  tools/testing/selftests/kvm/Makefile |  1 +
>  tools/testing/selftests/kvm/dirty_log_test.c | 70 ++--
>  2 files changed, 66 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/Makefile 
> b/tools/testing/selftests/kvm/Makefile
> index ba7849751989..ac7e63e00fee 100644
> --- a/tools/testing/selftests/kvm/Makefile
> +++ b/tools/testing/selftests/kvm/Makefile
> @@ -33,6 +33,7 @@ TEST_GEN_PROGS_aarch64 += dirty_log_test
>  TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
>  
>  TEST_GEN_PROGS_s390x += s390x/sync_regs_test
> +TEST_GEN_PROGS_s390x += dirty_log_test
>  TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
>  
>  TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
> diff --git a/tools/testing/selftests/kvm/dirty_log_test.c 
> b/tools/testing/selftests/kvm/dirty_log_test.c
> index ceb52b952637..7a1223ad0ff3 100644
> --- a/tools/testing/selftests/kvm/dirty_log_test.c
> +++ b/tools/testing/selftests/kvm/dirty_log_test.c
> @@ -26,9 +26,22 @@
>  /* The memory slot index to track dirty pages */
>  #define TEST_MEM_SLOT_INDEX  1
>  
> +#ifdef __s390x__
> +
> +/*
> + * On s390x, the ELF program is sometimes linked at 0x8000, so we can
> + * not use 0x4000 here without overlapping into that region. Thus let's
> + * use 0xc000 as base address there instead.
> + */
> +#define DEFAULT_GUEST_TEST_MEM   0xc000

I think both x86 and aarch64 should be ok with this offset. If testing
proves it does, then we can just change it for all architecture.

> +
> +#else
> +
>  /* Default guest test memory offset, 1G */
>  #define DEFAULT_GUEST_TEST_MEM   0x4000
>  
> +#endif
> +
>  /* How many pages to dirty for each guest loop */
>  #define TEST_PAGES_PER_LOOP  1024
>  
> @@ -38,6 +51,27 @@
>  /* Interval for each host loop (ms) */
>  #define TEST_HOST_LOOP_INTERVAL  10UL
>  
> +/* Dirty bitmaps are always little endian, so we need to swap on big endian 
> */
> +#if defined(__s390x__)
> +# define BITOP_LE_SWIZZLE((BITS_PER_LONG-1) & ~0x7)
> +# define test_bit_le(nr, addr) \
> + test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define set_bit_le(nr, addr) \
> + set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define clear_bit_le(nr, addr) \
> + clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define test_and_set_bit_le(nr, addr) \
> + test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +# define test_and_clear_bit_le(nr, addr) \
> + test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
> +#else
> +# define test_bit_le test_bit
> +# define set_bit_le  set_bit
> +# define clear_bit_leclear_bit
> +# define test_and_set_bit_le test_and_set_bit
> +# define test_and_clear_bit_le   test_and_clear_bit
> +#endif

nit: does the formatting above look right after applying the patch?

> +
>  /*
>   * Guest/Host shared variables. Ensure addr_gva2hva() and/or
>   * sync_global_to/from_guest() are used when accessing from
> @@ -69,11 +103,25 @@ static uint64_t guest_test_virt_mem = 
> DEFAULT_GUEST_TEST_MEM;
>   */
>  static void guest_code(void)
>  {
> + uint64_t addr;
>   int i;
>  
> +#ifdef __s390x__
> + /*
> +  * On s390x, all pages of a 1M segment are initially marked as dirty
> +  * when a page of the segment is written to for the very first time.
> +  * To compensate this specialty in this test, we need to touch all
> +  * pages during the first iteration.
> +  */
> + for (i = 0; i < guest_num_pages; i++) {
> + addr = guest_test_virt_mem + i * guest_page_size;
> + *(uint64_t *)addr = READ_ONCE(iteration);
> + }
> +#endif
> +
>   while (true) {
>   for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
> - uint64_t addr = guest_test_virt_mem;
> + addr = guest_test_virt_mem;
>   addr += (READ_ONCE(random_array[i]) % guest_num_pages)
>   * guest_page_size;
>   addr &= ~(host_page_size - 1);
> @@ -158,15 +206,15 @@ static void vm_dirty_log_verify(unsigned long *bmap)
>   value_ptr = host_test_mem + page * host_page_size;
>  
>   /* If this is a special page that we were tracking... */
> - if (test_and_clear_bit(page, host_bmap_track)) {
> + if (test_and_clear_bit_le(page, host_bmap_track)) {
>   host_track_next_count++;
> - 

[PATCH 2/2] KVM: selftests: Enable dirty_log_test on s390x

2019-07-30 Thread Thomas Huth
To run the dirty_log_test on s390x, we have to make sure that we
access the dirty log bitmap with little endian byte ordering and
we have to properly align the memslot of the guest.
Also all dirty bits of a segment are set once on s390x when one
of the pages of a segment are written to for the first time, so
we have to make sure that we touch all pages during the first
iteration to keep the test in sync here.

Signed-off-by: Thomas Huth 
---
 tools/testing/selftests/kvm/Makefile |  1 +
 tools/testing/selftests/kvm/dirty_log_test.c | 70 ++--
 2 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/Makefile 
b/tools/testing/selftests/kvm/Makefile
index ba7849751989..ac7e63e00fee 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -33,6 +33,7 @@ TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
 
 TEST_GEN_PROGS_s390x += s390x/sync_regs_test
+TEST_GEN_PROGS_s390x += dirty_log_test
 TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
 
 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
diff --git a/tools/testing/selftests/kvm/dirty_log_test.c 
b/tools/testing/selftests/kvm/dirty_log_test.c
index ceb52b952637..7a1223ad0ff3 100644
--- a/tools/testing/selftests/kvm/dirty_log_test.c
+++ b/tools/testing/selftests/kvm/dirty_log_test.c
@@ -26,9 +26,22 @@
 /* The memory slot index to track dirty pages */
 #define TEST_MEM_SLOT_INDEX1
 
+#ifdef __s390x__
+
+/*
+ * On s390x, the ELF program is sometimes linked at 0x8000, so we can
+ * not use 0x4000 here without overlapping into that region. Thus let's
+ * use 0xc000 as base address there instead.
+ */
+#define DEFAULT_GUEST_TEST_MEM 0xc000
+
+#else
+
 /* Default guest test memory offset, 1G */
 #define DEFAULT_GUEST_TEST_MEM 0x4000
 
+#endif
+
 /* How many pages to dirty for each guest loop */
 #define TEST_PAGES_PER_LOOP1024
 
@@ -38,6 +51,27 @@
 /* Interval for each host loop (ms) */
 #define TEST_HOST_LOOP_INTERVAL10UL
 
+/* Dirty bitmaps are always little endian, so we need to swap on big endian */
+#if defined(__s390x__)
+# define BITOP_LE_SWIZZLE  ((BITS_PER_LONG-1) & ~0x7)
+# define test_bit_le(nr, addr) \
+   test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define set_bit_le(nr, addr) \
+   set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define clear_bit_le(nr, addr) \
+   clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define test_and_set_bit_le(nr, addr) \
+   test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+# define test_and_clear_bit_le(nr, addr) \
+   test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
+#else
+# define test_bit_le   test_bit
+# define set_bit_leset_bit
+# define clear_bit_le  clear_bit
+# define test_and_set_bit_le   test_and_set_bit
+# define test_and_clear_bit_le test_and_clear_bit
+#endif
+
 /*
  * Guest/Host shared variables. Ensure addr_gva2hva() and/or
  * sync_global_to/from_guest() are used when accessing from
@@ -69,11 +103,25 @@ static uint64_t guest_test_virt_mem = 
DEFAULT_GUEST_TEST_MEM;
  */
 static void guest_code(void)
 {
+   uint64_t addr;
int i;
 
+#ifdef __s390x__
+   /*
+* On s390x, all pages of a 1M segment are initially marked as dirty
+* when a page of the segment is written to for the very first time.
+* To compensate this specialty in this test, we need to touch all
+* pages during the first iteration.
+*/
+   for (i = 0; i < guest_num_pages; i++) {
+   addr = guest_test_virt_mem + i * guest_page_size;
+   *(uint64_t *)addr = READ_ONCE(iteration);
+   }
+#endif
+
while (true) {
for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
-   uint64_t addr = guest_test_virt_mem;
+   addr = guest_test_virt_mem;
addr += (READ_ONCE(random_array[i]) % guest_num_pages)
* guest_page_size;
addr &= ~(host_page_size - 1);
@@ -158,15 +206,15 @@ static void vm_dirty_log_verify(unsigned long *bmap)
value_ptr = host_test_mem + page * host_page_size;
 
/* If this is a special page that we were tracking... */
-   if (test_and_clear_bit(page, host_bmap_track)) {
+   if (test_and_clear_bit_le(page, host_bmap_track)) {
host_track_next_count++;
-   TEST_ASSERT(test_bit(page, bmap),
+   TEST_ASSERT(test_bit_le(page, bmap),
"Page %"PRIu64" should have its dirty bit "
"set in this iteration but it is missing",
page);
}
 
-   if (test_bit(page, bmap)) {
+   if (test_bit_le(page, bmap)) {
host_dirty_count++;