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

Haifeng Xu via ltp
2024-09-21T04:39:24Z

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 using sizeof on the mask instead of sizeof(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>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
---
Changes since v1:
- Don't change the type of the mask and use sizeof on it.
---
.../kernel/sched/hyperthreading/ht_affinity/ht_affinity.c | 4 ++--
1 file changed, 2 insertions(+), 2 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..3c2fe1bf1 100644
--- a/testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c
+++ b/testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c
@@ -67,7 +67,7 @@ int HT_SetAffinity(void)
tst_resm(TINFO, "Set test process affinity.");
printf("mask: %x\n", mask);

- sched_setaffinity(pid, sizeof(unsigned long), &mask);
+ sched_setaffinity(pid, sizeof(mask), &mask);

for (j = 0; j < 10; j++) {
for (k = 0; k < 10; k++) {
@@ -95,7 +95,7 @@ int HT_SetAffinity(void)
tst_resm(TINFO, "Set test process affinity.");
printf("mask: %x\n", mask);

- sched_setaffinity(pid, sizeof(unsigned long), &mask);
+ sched_setaffinity(pid, sizeof(mask), &mask);

for (j = 0; j < 10; j++) {
for (k = 0; k < 10; k++) {
--
2.25.1