Skip to contentSkip to conversation

ht_affinity.c: fix ht_affinity test failure

PATCH Superseded Haifeng Xu via ltp 2 participants 7 messages Sep 20, 2024 – Sep 23, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ht_affinity.c: fix ht_affinity test failure

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.

---
 .../kernel/sched/hyperthreading/ht_affinity/ht_affinity.c   | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c +3 -3
index f6e9f2745..e6829c2d2 100644
@@ -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);
 
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 4 lines

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.

Good catch, however it would be better if we used sizeof on the mask instead of sizeof(unsigned long) in the sched_setaffinity() as well:

testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c +2 -2
index f6e9f2745..3c2fe1bf1 100644
@@ -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++) {

Most of the code does that already which makes it impossible to pass different size than the actual size.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 34 lines · Cyril Hrubis

Hi!

Quoted 4 lines

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.

Good catch, however it would be better if we used sizeof on the mask instead of sizeof(unsigned long) in the sched_setaffinity() as well:

testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c +2 -2
index f6e9f2745..3c2fe1bf1 100644
@@ -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++) {
Quoted 3 lines

Most of the code does that already which makes it impossible to pass different size than the actual size.

From the kernel source code, the user_mask_ptr is unsigned long.

SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
unsigned long __user *, user_mask_ptr)

so maybe we can keep the type of user_mask_ptr consistent with it.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi!

Quoted 6 lines

From the kernel source code, the user_mask_ptr is unsigned long.

SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
unsigned long __user *, user_mask_ptr)

so maybe we can keep the type of user_mask_ptr consistent with it.

It's actually accessed as an array. See get_user_cpu_mask() function just above this syscall definition and userspace is suppose to pass an array big enough for all the flags of CPUs it cares about.

There are also macros in the libc sched.h header that allows you to allocate an array big enough for the current machine and manipulate the bits, see man CPU_ALLOC.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 8 lines · Cyril Hrubis

Hi!

Quoted 6 lines

From the kernel source code, the user_mask_ptr is unsigned long.

SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
unsigned long __user *, user_mask_ptr)

so maybe we can keep the type of user_mask_ptr consistent with it.

Quoted 8 lines

It's actually accessed as an array. See get_user_cpu_mask() function just above this syscall definition and userspace is suppose to pass an array big enough for all the flags of CPUs it cares about.

There are also macros in the libc sched.h header that allows you to allocate an array big enough for the current machine and manipulate the bits, see man CPU_ALLOC.

OK. I'll post next version with your suggestions.
Thanks!

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v2 1/1 ht_affinity.c: fix ht_affinity test failure

In reply to Cyril Hrubis

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.

---
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(-)
testcases/kernel/sched/hyperthreading/ht_affinity/ht_affinity.c +2 -2
index f6e9f2745..3c2fe1bf1 100644
@@ -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++) {
Signature · 5 lines
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp