linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sched/core: expand sched_getaffinity(2) to return number of CPUs
@ 2019-04-03 20:08 Alexey Dobriyan
  2019-04-04  8:42 ` Peter Zijlstra
  0 siblings, 1 reply; 10+ messages in thread
From: Alexey Dobriyan @ 2019-04-03 20:08 UTC (permalink / raw)
  To: mingo, peterz; +Cc: linux-kernel

Currently there is no easy way to get the number of CPUs on the system.
Applications are divided into 2 groups:
One group allocates buffer and call sched_getaffinity(2) once. It works
but either underallocate or overallocates and in the future such application
will become buggy as Linux will start working on even more SMP-ier systems.

Glibc in particular shipped with 1024 CPUs support maximum at some point
which is quite surprising as glibc maitainers should know better.

Another group dynamically grow buffer until cpumask fits. This is
inefficient as multiple system calls are done.

Nobody seems to parse "/sys/devices/system/cpu/possible".
Even if someone does, parsing sysfs is much slower than necessary.

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).

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 kernel/sched/core.c |    3 +++
 1 file changed, 3 insertions(+)

--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4942,6 +4942,9 @@ SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
 	int ret;
 	cpumask_var_t mask;
 
+	if (len == 0)
+		return nr_cpu_ids;
+
 	if ((len * BITS_PER_BYTE) < nr_cpu_ids)
 		return -EINVAL;
 	if (len & (sizeof(unsigned long)-1))

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2019-04-08  7:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-03 20:08 [PATCH] sched/core: expand sched_getaffinity(2) to return number of CPUs Alexey Dobriyan
2019-04-04  8:42 ` Peter Zijlstra
2019-04-04 18:02   ` Alexey Dobriyan
2019-04-05  9:26     ` Peter Zijlstra
2019-04-05 10:16   ` Florian Weimer
2019-04-05 11:04     ` Peter Zijlstra
2019-04-05 11:08       ` Florian Weimer
2019-04-05 11:49         ` Peter Zijlstra
2019-04-06 19:48     ` Alexey Dobriyan
2019-04-08  7:49       ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).