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

Haifeng Xu haifeng.xu@shopee.com
Fri Sep 20 12:52:27 CEST 2024



On 2024/9/20 16:58, Cyril Hrubis wrote:
> Hi!
>> 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:
> 
> 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++) {
> 
> 
> 
> 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.


More information about the ltp mailing list