Subject: [PATCH v2 3/7] selftests/powerpc: Allow bind_to_cpu() to automatically pick CPU

From: Benjamin Gray <[email-masked]>
Date: Thu, 6 Apr 2023 14:33:16 +1000

All current users of bind_to_cpu() don't care _which_ CPU they get, just
that they are bound to a single free one. So alter the interface to

	1. Accept a BIND_CPU_ANY value that tells it to automatically
	   pick a CPU
	2. Return the picked CPU

And convert all these users to bind_to_cpu(BIND_CPU_ANY).

Signed-off-by: Benjamin Gray <[email-masked]>

---

v2:	* New in v2
---
 tools/testing/selftests/powerpc/include/utils.h     |  2 ++
 .../powerpc/pmu/ebb/cpu_event_pinned_vs_ebb_test.c  |  3 +--
 .../powerpc/pmu/ebb/cpu_event_vs_ebb_test.c         |  3 +--
 .../powerpc/pmu/ebb/ebb_vs_cpu_event_test.c         |  3 +--
 .../powerpc/pmu/ebb/multi_ebb_procs_test.c          |  6 ++----
 tools/testing/selftests/powerpc/pmu/lib.c           |  6 ++----
 tools/testing/selftests/powerpc/utils.c             | 13 ++++++++++++-
 7 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/powerpc/include/utils.h b/tools/testing/selftests/powerpc/include/utils.h
index d3589e16a20f..44bfd48b93d6 100644
--- a/tools/testing/selftests/powerpc/include/utils.h
+++ b/tools/testing/selftests/powerpc/include/utils.h
@@ -31,6 +31,8 @@ int read_auxv(char *buf, ssize_t buf_size);
 void *find_auxv_entry(int type, char *auxv);
 void *get_auxv_entry(int type);
 
+#define BIND_CPU_ANY	(-1)
+
 int pick_online_cpu(void);
 int bind_to_cpu(int cpu);
 
diff --git a/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_pinned_vs_ebb_test.c b/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_pinned_vs_ebb_test.c
index 3cd33eb51e5e..fab7f34d7ce1 100644
--- a/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_pinned_vs_ebb_test.c
+++ b/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_pinned_vs_ebb_test.c
@@ -45,9 +45,8 @@ int cpu_event_pinned_vs_ebb(void)
 
 	SKIP_IF(!ebb_is_supported());
 
-	cpu = pick_online_cpu();
+	cpu = bind_to_cpu(BIND_CPU_ANY);
 	FAIL_IF(cpu < 0);
-	FAIL_IF(bind_to_cpu(cpu));
 
 	FAIL_IF(pipe(read_pipe.fds) == -1);
 	FAIL_IF(pipe(write_pipe.fds) == -1);
diff --git a/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_vs_ebb_test.c b/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_vs_ebb_test.c
index 8466ef9d7de8..7c54c262036e 100644
--- a/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_vs_ebb_test.c
+++ b/tools/testing/selftests/powerpc/pmu/ebb/cpu_event_vs_ebb_test.c
@@ -43,9 +43,8 @@ int cpu_event_vs_ebb(void)
 
 	SKIP_IF(!ebb_is_supported());
 
-	cpu = pick_online_cpu();
+	cpu = bind_to_cpu(BIND_CPU_ANY);
 	FAIL_IF(cpu < 0);
-	FAIL_IF(bind_to_cpu(cpu));
 
 	FAIL_IF(pipe(read_pipe.fds) == -1);
 	FAIL_IF(pipe(write_pipe.fds) == -1);
diff --git a/tools/testing/selftests/powerpc/pmu/ebb/ebb_vs_cpu_event_test.c b/tools/testing/selftests/powerpc/pmu/ebb/ebb_vs_cpu_event_test.c
index 4d822cb3589c..d7064b54c64f 100644
--- a/tools/testing/selftests/powerpc/pmu/ebb/ebb_vs_cpu_event_test.c
+++ b/tools/testing/selftests/powerpc/pmu/ebb/ebb_vs_cpu_event_test.c
@@ -43,9 +43,8 @@ int ebb_vs_cpu_event(void)
 
 	SKIP_IF(!ebb_is_supported());
 
-	cpu = pick_online_cpu();
+	cpu = bind_to_cpu(BIND_CPU_ANY);
 	FAIL_IF(cpu < 0);
-	FAIL_IF(bind_to_cpu(cpu));
 
 	FAIL_IF(pipe(read_pipe.fds) == -1);
 	FAIL_IF(pipe(write_pipe.fds) == -1);
diff --git a/tools/testing/selftests/powerpc/pmu/ebb/multi_ebb_procs_test.c b/tools/testing/selftests/powerpc/pmu/ebb/multi_ebb_procs_test.c
index 9b0f70d59702..4ac22b2e774f 100644
--- a/tools/testing/selftests/powerpc/pmu/ebb/multi_ebb_procs_test.c
+++ b/tools/testing/selftests/powerpc/pmu/ebb/multi_ebb_procs_test.c
@@ -75,13 +75,11 @@ static int cycles_child(void)
 int multi_ebb_procs(void)
 {
 	pid_t pids[NR_CHILDREN];
-	int cpu, rc, i;
+	int rc, i;
 
 	SKIP_IF(!ebb_is_supported());
 
-	cpu = pick_online_cpu();
-	FAIL_IF(cpu < 0);
-	FAIL_IF(bind_to_cpu(cpu));
+	FAIL_IF(bind_to_cpu(BIND_CPU_ANY) < 0);
 
 	for (i = 0; i < NR_CHILDREN; i++) {
 		pids[i] = fork();
diff --git a/tools/testing/selftests/powerpc/pmu/lib.c b/tools/testing/selftests/powerpc/pmu/lib.c
index 144f90a78d69..321357987408 100644
--- a/tools/testing/selftests/powerpc/pmu/lib.c
+++ b/tools/testing/selftests/powerpc/pmu/lib.c
@@ -103,12 +103,10 @@ static int eat_cpu_child(union pipe read_pipe, union pipe write_pipe)
 pid_t eat_cpu(int (test_function)(void))
 {
 	union pipe read_pipe, write_pipe;
-	int cpu, rc;
+	int rc;
 	pid_t pid;
 
-	cpu = pick_online_cpu();
-	FAIL_IF(cpu < 0);
-	FAIL_IF(bind_to_cpu(cpu));
+	FAIL_IF(bind_to_cpu(BIND_CPU_ANY) < 0);
 
 	if (pipe(read_pipe.fds) == -1)
 		return -1;
diff --git a/tools/testing/selftests/powerpc/utils.c b/tools/testing/selftests/powerpc/utils.c
index cdb996dba703..252fb4a95e90 100644
--- a/tools/testing/selftests/powerpc/utils.c
+++ b/tools/testing/selftests/powerpc/utils.c
@@ -455,13 +455,24 @@ int pick_online_cpu(void)
 int bind_to_cpu(int cpu)
 {
 	cpu_set_t mask;
+	int err;
+
+	if (cpu == BIND_CPU_ANY) {
+		cpu = pick_online_cpu();
+		if (cpu < 0)
+			return cpu;
+	}
 
 	printf("Binding to cpu %d\n", cpu);
 
 	CPU_ZERO(&mask);
 	CPU_SET(cpu, &mask);
 
-	return sched_setaffinity(0, sizeof(mask), &mask);
+	err = sched_setaffinity(0, sizeof(mask), &mask);
+	if (err)
+		return err;
+
+	return cpu;
 }
 
 bool is_ppc64le(void)
-- 
2.39.2