linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] KVM: selftests: Fix target thread to be migrated in rseq_test
@ 2022-07-19  2:08 Gavin Shan
  2022-07-19  7:24 ` Andrew Jones
  2022-07-19 13:04 ` Paolo Bonzini
  0 siblings, 2 replies; 3+ messages in thread
From: Gavin Shan @ 2022-07-19  2:08 UTC (permalink / raw)
  To: kvmarm
  Cc: linux-kernel, linux-kselftest, oliver.upton, seanjc, shuah, maz,
	pbonzini, shan.gavin

In rseq_test, there are two threads, which are vCPU thread and migration
worker separately. Unfortunately, the test has the wrong PID passed to
sched_setaffinity() in the migration worker. It forces migration on the
migration worker because zeroed PID represents the calling thread, which
is the migration worker itself. It means the vCPU thread is never enforced
to migration and it can migrate at any time, which eventually leads to
failure as the following logs show.

  host# uname -r
  5.19.0-rc6-gavin+
  host# # cat /proc/cpuinfo | grep processor | tail -n 1
  processor    : 223
  host# pwd
  /home/gavin/sandbox/linux.main/tools/testing/selftests/kvm
  host# for i in `seq 1 100`; do \
        echo "--------> $i"; ./rseq_test; done
  --------> 1
  --------> 2
  --------> 3
  --------> 4
  --------> 5
  --------> 6
  ==== Test Assertion Failure ====
    rseq_test.c:265: rseq_cpu == cpu
    pid=3925 tid=3925 errno=4 - Interrupted system call
       1  0x0000000000401963: main at rseq_test.c:265 (discriminator 2)
       2  0x0000ffffb044affb: ?? ??:0
       3  0x0000ffffb044b0c7: ?? ??:0
       4  0x0000000000401a6f: _start at ??:?
    rseq CPU = 4, sched CPU = 27

Fix the issue by passing correct parameter, TID of the vCPU thread, to
sched_setaffinity() in the migration worker.

Fixes: 61e52f1630f5 ("KVM: selftests: Add a test for KVM_RUN+rseq to detect task migration bugs")
Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Gavin Shan <gshan@redhat.com>
Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
---
v4: Pick the code change as Sean suggested.
---
 tools/testing/selftests/kvm/rseq_test.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selftests/kvm/rseq_test.c
index 4158da0da2bb..2237d1aac801 100644
--- a/tools/testing/selftests/kvm/rseq_test.c
+++ b/tools/testing/selftests/kvm/rseq_test.c
@@ -82,8 +82,9 @@ static int next_cpu(int cpu)
 	return cpu;
 }
 
-static void *migration_worker(void *ign)
+static void *migration_worker(void *__rseq_tid)
 {
+	pid_t rseq_tid = (pid_t)(unsigned long)__rseq_tid;
 	cpu_set_t allowed_mask;
 	int r, i, cpu;
 
@@ -106,7 +107,7 @@ static void *migration_worker(void *ign)
 		 * stable, i.e. while changing affinity is in-progress.
 		 */
 		smp_wmb();
-		r = sched_setaffinity(0, sizeof(allowed_mask), &allowed_mask);
+		r = sched_setaffinity(rseq_tid, sizeof(allowed_mask), &allowed_mask);
 		TEST_ASSERT(!r, "sched_setaffinity failed, errno = %d (%s)",
 			    errno, strerror(errno));
 		smp_wmb();
@@ -231,7 +232,8 @@ int main(int argc, char *argv[])
 	vm = vm_create_default(VCPU_ID, 0, guest_code);
 	ucall_init(vm, NULL);
 
-	pthread_create(&migration_thread, NULL, migration_worker, 0);
+	pthread_create(&migration_thread, NULL, migration_worker,
+		       (void *)(unsigned long)gettid());
 
 	for (i = 0; !done; i++) {
 		vcpu_run(vm, VCPU_ID);
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] KVM: selftests: Fix target thread to be migrated in rseq_test
  2022-07-19  2:08 [PATCH v4] KVM: selftests: Fix target thread to be migrated in rseq_test Gavin Shan
@ 2022-07-19  7:24 ` Andrew Jones
  2022-07-19 13:04 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Jones @ 2022-07-19  7:24 UTC (permalink / raw)
  To: Gavin Shan
  Cc: kvmarm, shan.gavin, maz, linux-kernel, oliver.upton,
	linux-kselftest, pbonzini, shuah

On Tue, Jul 19, 2022 at 10:08:30AM +0800, Gavin Shan wrote:
> In rseq_test, there are two threads, which are vCPU thread and migration
> worker separately. Unfortunately, the test has the wrong PID passed to
> sched_setaffinity() in the migration worker. It forces migration on the
> migration worker because zeroed PID represents the calling thread, which
> is the migration worker itself. It means the vCPU thread is never enforced
> to migration and it can migrate at any time, which eventually leads to
> failure as the following logs show.
> 
>   host# uname -r
>   5.19.0-rc6-gavin+
>   host# # cat /proc/cpuinfo | grep processor | tail -n 1
>   processor    : 223
>   host# pwd
>   /home/gavin/sandbox/linux.main/tools/testing/selftests/kvm
>   host# for i in `seq 1 100`; do \
>         echo "--------> $i"; ./rseq_test; done
>   --------> 1
>   --------> 2
>   --------> 3
>   --------> 4
>   --------> 5
>   --------> 6
>   ==== Test Assertion Failure ====
>     rseq_test.c:265: rseq_cpu == cpu
>     pid=3925 tid=3925 errno=4 - Interrupted system call
>        1  0x0000000000401963: main at rseq_test.c:265 (discriminator 2)
>        2  0x0000ffffb044affb: ?? ??:0
>        3  0x0000ffffb044b0c7: ?? ??:0
>        4  0x0000000000401a6f: _start at ??:?
>     rseq CPU = 4, sched CPU = 27
> 
> Fix the issue by passing correct parameter, TID of the vCPU thread, to
> sched_setaffinity() in the migration worker.
> 
> Fixes: 61e52f1630f5 ("KVM: selftests: Add a test for KVM_RUN+rseq to detect task migration bugs")
> Suggested-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
> ---
> v4: Pick the code change as Sean suggested.
> ---
>  tools/testing/selftests/kvm/rseq_test.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selftests/kvm/rseq_test.c
> index 4158da0da2bb..2237d1aac801 100644
> --- a/tools/testing/selftests/kvm/rseq_test.c
> +++ b/tools/testing/selftests/kvm/rseq_test.c
> @@ -82,8 +82,9 @@ static int next_cpu(int cpu)
>  	return cpu;
>  }
>  
> -static void *migration_worker(void *ign)
> +static void *migration_worker(void *__rseq_tid)
>  {
> +	pid_t rseq_tid = (pid_t)(unsigned long)__rseq_tid;
>  	cpu_set_t allowed_mask;
>  	int r, i, cpu;
>  
> @@ -106,7 +107,7 @@ static void *migration_worker(void *ign)
>  		 * stable, i.e. while changing affinity is in-progress.
>  		 */
>  		smp_wmb();
> -		r = sched_setaffinity(0, sizeof(allowed_mask), &allowed_mask);
> +		r = sched_setaffinity(rseq_tid, sizeof(allowed_mask), &allowed_mask);
>  		TEST_ASSERT(!r, "sched_setaffinity failed, errno = %d (%s)",
>  			    errno, strerror(errno));
>  		smp_wmb();
> @@ -231,7 +232,8 @@ int main(int argc, char *argv[])
>  	vm = vm_create_default(VCPU_ID, 0, guest_code);
>  	ucall_init(vm, NULL);
>  
> -	pthread_create(&migration_thread, NULL, migration_worker, 0);
> +	pthread_create(&migration_thread, NULL, migration_worker,
> +		       (void *)(unsigned long)gettid());
>  
>  	for (i = 0; !done; i++) {
>  		vcpu_run(vm, VCPU_ID);
> -- 
> 2.23.0

Thanks for figuring this out Gavin and Sean!

Reviewed-by: Andrew Jones <andrew.jones@linux.dev>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] KVM: selftests: Fix target thread to be migrated in rseq_test
  2022-07-19  2:08 [PATCH v4] KVM: selftests: Fix target thread to be migrated in rseq_test Gavin Shan
  2022-07-19  7:24 ` Andrew Jones
@ 2022-07-19 13:04 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2022-07-19 13:04 UTC (permalink / raw)
  To: Gavin Shan, kvmarm
  Cc: linux-kernel, linux-kselftest, oliver.upton, seanjc, shuah, maz,
	shan.gavin

On 7/19/22 04:08, Gavin Shan wrote:
> In rseq_test, there are two threads, which are vCPU thread and migration
> worker separately. Unfortunately, the test has the wrong PID passed to
> sched_setaffinity() in the migration worker. It forces migration on the
> migration worker because zeroed PID represents the calling thread, which
> is the migration worker itself. It means the vCPU thread is never enforced
> to migration and it can migrate at any time, which eventually leads to
> failure as the following logs show.
> 
>    host# uname -r
>    5.19.0-rc6-gavin+
>    host# # cat /proc/cpuinfo | grep processor | tail -n 1
>    processor    : 223
>    host# pwd
>    /home/gavin/sandbox/linux.main/tools/testing/selftests/kvm
>    host# for i in `seq 1 100`; do \
>          echo "--------> $i"; ./rseq_test; done
>    --------> 1
>    --------> 2
>    --------> 3
>    --------> 4
>    --------> 5
>    --------> 6
>    ==== Test Assertion Failure ====
>      rseq_test.c:265: rseq_cpu == cpu
>      pid=3925 tid=3925 errno=4 - Interrupted system call
>         1  0x0000000000401963: main at rseq_test.c:265 (discriminator 2)
>         2  0x0000ffffb044affb: ?? ??:0
>         3  0x0000ffffb044b0c7: ?? ??:0
>         4  0x0000000000401a6f: _start at ??:?
>      rseq CPU = 4, sched CPU = 27
> 
> Fix the issue by passing correct parameter, TID of the vCPU thread, to
> sched_setaffinity() in the migration worker.
> 
> Fixes: 61e52f1630f5 ("KVM: selftests: Add a test for KVM_RUN+rseq to detect task migration bugs")
> Suggested-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
> ---
> v4: Pick the code change as Sean suggested.
> ---
>   tools/testing/selftests/kvm/rseq_test.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selftests/kvm/rseq_test.c
> index 4158da0da2bb..2237d1aac801 100644
> --- a/tools/testing/selftests/kvm/rseq_test.c
> +++ b/tools/testing/selftests/kvm/rseq_test.c
> @@ -82,8 +82,9 @@ static int next_cpu(int cpu)
>   	return cpu;
>   }
>   
> -static void *migration_worker(void *ign)
> +static void *migration_worker(void *__rseq_tid)
>   {
> +	pid_t rseq_tid = (pid_t)(unsigned long)__rseq_tid;
>   	cpu_set_t allowed_mask;
>   	int r, i, cpu;
>   
> @@ -106,7 +107,7 @@ static void *migration_worker(void *ign)
>   		 * stable, i.e. while changing affinity is in-progress.
>   		 */
>   		smp_wmb();
> -		r = sched_setaffinity(0, sizeof(allowed_mask), &allowed_mask);
> +		r = sched_setaffinity(rseq_tid, sizeof(allowed_mask), &allowed_mask);
>   		TEST_ASSERT(!r, "sched_setaffinity failed, errno = %d (%s)",
>   			    errno, strerror(errno));
>   		smp_wmb();
> @@ -231,7 +232,8 @@ int main(int argc, char *argv[])
>   	vm = vm_create_default(VCPU_ID, 0, guest_code);
>   	ucall_init(vm, NULL);
>   
> -	pthread_create(&migration_thread, NULL, migration_worker, 0);
> +	pthread_create(&migration_thread, NULL, migration_worker,
> +		       (void *)(unsigned long)gettid());
>   
>   	for (i = 0; !done; i++) {
>   		vcpu_run(vm, VCPU_ID);

Queued, thanks.

Paolo


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-07-19 13:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-19  2:08 [PATCH v4] KVM: selftests: Fix target thread to be migrated in rseq_test Gavin Shan
2022-07-19  7:24 ` Andrew Jones
2022-07-19 13:04 ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).