Re: [PATCH] sched/core: expand sched_getaffinity(2) to return number of CPUs

From: Florian Weimer
Date: Mon Apr 08 2019 - 03:49:41 EST


* Alexey Dobriyan:

>> >> Patch overloads sched_getaffinity(len=0) to simply return "nr_cpu_ids".
>> >> This will make gettting CPU mask require at most 2 system calls
>> >> and will eliminate unnecessary code.
>> >>
>> >> len=0 is chosen so that
>> >> * passing zeroes is the simplest thing
>> >>
>> >> syscall(__NR_sched_getaffinity, 0, 0, NULL)
>> >>
>> >> will simply do the right thing,
>> >>
>> >> * old kernels returned -EINVAL unconditionally.
>> >>
>> >> Note: glibc segfaults upon exiting from system call because it tries to
>> >> clear the rest of the buffer if return value is positive, so
>> >> applications will have to use syscall(3).
>> >> Good news is that it proves noone uses sched_getaffinity(pid, 0, NULL).
>>
>> Given that old kernels fail with EINVAL, that evidence is fairly
>> restricted.
>>
>> I'm not sure if it's a good idea to overload this interface. I expect
>> that users will want to call sched_getaffinity (the system call wrapper)
>> with cpusetsize == 0 to query the value, so there will be pressure on
>> glibc to remove the memset. At that point we have an API that obscurely
>> fails with old glibc versions, but suceeds with newer ones, which isn't
>> great.
>
> I can do "if (len == 536870912)" so that bit count overflows on old
> kernels into EINVAL and is unlikely to be used ever.

I don't see how this solves this particular issue. It will still result
in a mysterious crash if programs use an updated system call wrapper.

Thanks,
Florian