[LTP] [PATCH] ht_affinity.c: fix ht_affinity test failure

Haifeng Xu haifeng.xu@shopee.com
Fri Sep 20 10:30:36 CEST 2024


Running ht_affinity test fails:

	smt_smp_affinity    0  TINFO  :  Set affinity through system call
	smt_smp_affinity    0  TINFO  :  Set test process affinity.
	mask: 1
	smt_smp_affinity    0  TINFO  :  ...Error

	...

	smt_smp_affinity    0  TINFO  :  Set test process affinity.
	mask: c0000000
	smt_smp_affinity    0  TINFO  :  ...Error
	smt_smp_affinity    3  TFAIL  :  ht_affinity.c:240: System call setaffinity() is error.

The type of cpumask pointer used in set_affinity() is unsigned long, but
ht_affinity used a unsigned int pointer. When kernel copy cpumask from
user-space pointer, the high 32bit of cpumask is a random value. So the
process can't be bind to the cpu specified by users.

After converting the cpumask from unsigned int to unsigned long, ht_affinity
test succeeds:

	smt_smp_affinity    0  TINFO  :  Set affinity through system call
	smt_smp_affinity    0  TINFO  :  Set test process affinity.
	mask: 1
	smt_smp_affinity    0  TINFO  :  ...OK

	...

	smt_smp_affinity    0  TINFO  :  Set test process affinity.
	mask: c0000000
	smt_smp_affinity    0  TINFO  :  ...OK
	smt_smp_affinity    3  TPASS  :  System call setaffinity() is OK.

Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
---
 .../kernel/sched/hyperthreading/ht_affinity/ht_affinity.c   | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c b/testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c
index f6e9f2745..e6829c2d2 100644
--- a/testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c
+++ b/testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c
@@ -48,7 +48,7 @@ len - length in bytes of the bitmask pointed to by user_mask_ptr.
 
 int HT_SetAffinity(void)
 {
-	unsigned int mask;
+	unsigned long mask;
 	pid_t pid;
 	int result = 1;
 	int cpu_count, i, j, k, cpuid;
@@ -65,7 +65,7 @@ int HT_SetAffinity(void)
 
 	for (i = 0, mask = 0x1; i < cpu_count; i++, mask = mask << 1) {
 		tst_resm(TINFO, "Set test process affinity.");
-		printf("mask: %x\n", mask);
+		printf("mask: %lx\n", mask);
 
 		sched_setaffinity(pid, sizeof(unsigned long), &mask);
 
@@ -93,7 +93,7 @@ int HT_SetAffinity(void)
 
 	for (i = 0, mask = 0x3; i < cpu_count - 1; i++, mask = mask << 1) {
 		tst_resm(TINFO, "Set test process affinity.");
-		printf("mask: %x\n", mask);
+		printf("mask: %lx\n", mask);
 
 		sched_setaffinity(pid, sizeof(unsigned long), &mask);
 
-- 
2.25.1



More information about the ltp mailing list