94084c
commit cda99af14e82b4bb6abaecd717ebe3b57c0aa534
94084c
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
94084c
Date:   Mon Sep 6 12:28:24 2021 -0300
94084c
94084c
    linux: Simplify get_nprocs
94084c
    
94084c
    This patch simplifies the memory allocation code and uses the sched
94084c
    routines instead of reimplement it.  This still uses a stack
94084c
    allocation buffer, so it can be used on malloc initialization code.
94084c
    
94084c
    Linux currently supports at maximum of 4096 cpus for most architectures:
94084c
    
94084c
    $ find -iname Kconfig | xargs git grep -A10 -w NR_CPUS | grep -w range
94084c
    arch/alpha/Kconfig-     range 2 32
94084c
    arch/arc/Kconfig-       range 2 4096
94084c
    arch/arm/Kconfig-       range 2 16 if DEBUG_KMAP_LOCAL
94084c
    arch/arm/Kconfig-       range 2 32 if !DEBUG_KMAP_LOCAL
94084c
    arch/arm64/Kconfig-     range 2 4096
94084c
    arch/csky/Kconfig-      range 2 32
94084c
    arch/hexagon/Kconfig-   range 2 6 if SMP
94084c
    arch/ia64/Kconfig-      range 2 4096
94084c
    arch/mips/Kconfig-      range 2 256
94084c
    arch/openrisc/Kconfig-  range 2 32
94084c
    arch/parisc/Kconfig-    range 2 32
94084c
    arch/riscv/Kconfig-     range 2 32
94084c
    arch/s390/Kconfig-      range 2 512
94084c
    arch/sh/Kconfig-        range 2 32
94084c
    arch/sparc/Kconfig-     range 2 32 if SPARC32
94084c
    arch/sparc/Kconfig-     range 2 4096 if SPARC64
94084c
    arch/um/Kconfig-        range 1 1
94084c
    arch/x86/Kconfig-# [NR_CPUS_RANGE_BEGIN ... NR_CPUS_RANGE_END] range.
94084c
    arch/x86/Kconfig-       range NR_CPUS_RANGE_BEGIN NR_CPUS_RANGE_END
94084c
    arch/xtensa/Kconfig-    range 2 32
94084c
    
94084c
    With x86 supporting 8192:
94084c
    
94084c
    arch/x86/Kconfig
94084c
     976 config NR_CPUS_RANGE_END
94084c
     977         int
94084c
     978         depends on X86_64
94084c
     979         default 8192 if  SMP && CPUMASK_OFFSTACK
94084c
     980         default  512 if  SMP && !CPUMASK_OFFSTACK
94084c
     981         default    1 if !SMP
94084c
    
94084c
    So using a maximum of 32k cpu should cover all cases (and I would
94084c
    expect once we start to have many more CPUs that Linux would provide
94084c
    a more straightforward way to query for such information).
94084c
    
94084c
    A test is added to check if sched_getaffinity can successfully return
94084c
    with large buffers.
94084c
    
94084c
    Checked on x86_64-linux-gnu and i686-linux-gnu.
94084c
    
94084c
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
94084c
    (cherry picked from commit 33099d72e41cf8a129b362e9709eb2be9372d844)
94084c
94084c
diff --git a/posix/Makefile b/posix/Makefile
94084c
index a5229777eeb0e067..61fcdf015b4ec83b 100644
94084c
--- a/posix/Makefile
94084c
+++ b/posix/Makefile
94084c
@@ -107,7 +107,8 @@ tests		:= test-errno tstgetopt testfnm runtests runptests \
94084c
 		   tst-sysconf-empty-chroot tst-glob_symlinks tst-fexecve \
94084c
 		   tst-glob-tilde test-ssize-max tst-spawn4 bug-regex37 \
94084c
 		   bug-regex38 tst-regcomp-truncated tst-spawn-chdir \
94084c
-		   tst-wordexp-nocmd tst-execveat tst-spawn5
94084c
+		   tst-wordexp-nocmd tst-execveat tst-spawn5 \
94084c
+		   tst-sched_getaffinity
94084c
 
94084c
 # Test for the glob symbol version that was replaced in glibc 2.27.
94084c
 ifeq ($(have-GLIBC_2.26)$(build-shared),yesyes)
94084c
diff --git a/posix/tst-sched_getaffinity.c b/posix/tst-sched_getaffinity.c
94084c
new file mode 100644
94084c
index 0000000000000000..db9d517a96fdd99e
94084c
--- /dev/null
94084c
+++ b/posix/tst-sched_getaffinity.c
94084c
@@ -0,0 +1,48 @@
94084c
+/* Tests for sched_getaffinity with large buffers.
94084c
+   Copyright (C) 2021 Free Software Foundation, Inc.
94084c
+   This file is part of the GNU C Library.
94084c
+
94084c
+   The GNU C Library is free software; you can redistribute it and/or
94084c
+   modify it under the terms of the GNU Lesser General Public
94084c
+   License as published by the Free Software Foundation; either
94084c
+   version 2.1 of the License, or (at your option) any later version.
94084c
+
94084c
+   The GNU C Library is distributed in the hope that it will be useful,
94084c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
94084c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
94084c
+   Lesser General Public License for more details.
94084c
+
94084c
+   You should have received a copy of the GNU Lesser General Public
94084c
+   License along with the GNU C Library; if not, see
94084c
+   <https://www.gnu.org/licenses/>.  */
94084c
+
94084c
+#include <array_length.h>
94084c
+#include <sched.h>
94084c
+#include <support/check.h>
94084c
+
94084c
+/* NB: this test may fail on system with more than 32k cpus.  */
94084c
+
94084c
+static int
94084c
+do_test (void)
94084c
+{
94084c
+  /* The values are larger than the default cpu_set_t.  */
94084c
+  const int bufsize[] = { 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, 1<<16, 1<<17 };
94084c
+  int cpucount[array_length (bufsize)];
94084c
+
94084c
+  for (int i = 0; i < array_length (bufsize); i++)
94084c
+    {
94084c
+      cpu_set_t *cpuset = CPU_ALLOC (bufsize[i]);
94084c
+      TEST_VERIFY (cpuset != NULL);
94084c
+      size_t size = CPU_ALLOC_SIZE (bufsize[i]);
94084c
+      TEST_COMPARE (sched_getaffinity (0, size, cpuset), 0);
94084c
+      cpucount[i] = CPU_COUNT_S (size, cpuset);
94084c
+      CPU_FREE (cpuset);
94084c
+    }
94084c
+
94084c
+  for (int i = 0; i < array_length (cpucount) - 1; i++)
94084c
+    TEST_COMPARE (cpucount[i], cpucount[i + 1]);
94084c
+
94084c
+  return 0;
94084c
+}
94084c
+
94084c
+#include <support/test-driver.c>
94084c
diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c
94084c
index 120ce1bb756b09cc..61d20e7bab8640f2 100644
94084c
--- a/sysdeps/unix/sysv/linux/getsysstats.c
94084c
+++ b/sysdeps/unix/sysv/linux/getsysstats.c
94084c
@@ -29,61 +29,29 @@
94084c
 #include <sys/sysinfo.h>
94084c
 #include <sysdep.h>
94084c
 
94084c
-/* Compute the population count of the entire array.  */
94084c
-static int
94084c
-__get_nprocs_count (const unsigned long int *array, size_t length)
94084c
-{
94084c
-  int count = 0;
94084c
-  for (size_t i = 0; i < length; ++i)
94084c
-    if (__builtin_add_overflow (count,  __builtin_popcountl (array[i]),
94084c
-				&count))
94084c
-      return INT_MAX;
94084c
-  return count;
94084c
-}
94084c
-
94084c
-/* __get_nprocs with a large buffer.  */
94084c
-static int
94084c
-__get_nprocs_large (void)
94084c
-{
94084c
-  /* This code cannot use scratch_buffer because it is used during
94084c
-     malloc initialization.  */
94084c
-  size_t pagesize = GLRO (dl_pagesize);
94084c
-  unsigned long int *page = __mmap (0, pagesize, PROT_READ | PROT_WRITE,
94084c
-				    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
94084c
-  if (page == MAP_FAILED)
94084c
-    return 2;
94084c
-  int r = INTERNAL_SYSCALL_CALL (sched_getaffinity, 0, pagesize, page);
94084c
-  int count;
94084c
-  if (r > 0)
94084c
-    count = __get_nprocs_count (page, pagesize / sizeof (unsigned long int));
94084c
-  else if (r == -EINVAL)
94084c
-    /* One page is still not enough to store the bits.  A more-or-less
94084c
-       arbitrary value.  This assumes t hat such large systems never
94084c
-       happen in practice.  */
94084c
-    count = GLRO (dl_pagesize) * CHAR_BIT;
94084c
-  else
94084c
-    count = 2;
94084c
-  __munmap (page, GLRO (dl_pagesize));
94084c
-  return count;
94084c
-}
94084c
-
94084c
 int
94084c
 __get_nprocs (void)
94084c
 {
94084c
-  /* Fast path for most systems.  The kernel expects a buffer size
94084c
-     that is a multiple of 8.  */
94084c
-  unsigned long int small_buffer[1024 / CHAR_BIT / sizeof (unsigned long int)];
94084c
-  int r = INTERNAL_SYSCALL_CALL (sched_getaffinity, 0,
94084c
-				 sizeof (small_buffer), small_buffer);
94084c
+  enum
94084c
+    {
94084c
+      max_num_cpus = 32768,
94084c
+      cpu_bits_size = CPU_ALLOC_SIZE (32768)
94084c
+    };
94084c
+
94084c
+  /* This cannot use malloc because it is used on malloc initialization.  */
94084c
+  __cpu_mask cpu_bits[cpu_bits_size / sizeof (__cpu_mask)];
94084c
+  int r = INTERNAL_SYSCALL_CALL (sched_getaffinity, 0, cpu_bits_size,
94084c
+				 cpu_bits);
94084c
   if (r > 0)
94084c
-    return __get_nprocs_count (small_buffer, r / sizeof (unsigned long int));
94084c
+    return CPU_COUNT_S (cpu_bits_size, (cpu_set_t*) cpu_bits);
94084c
   else if (r == -EINVAL)
94084c
-    /* The kernel requests a larger buffer to store the data.  */
94084c
-    return __get_nprocs_large ();
94084c
-  else
94084c
-    /* Some other error.  2 is conservative (not a uniprocessor
94084c
-       system, so atomics are needed). */
94084c
-    return 2;
94084c
+    /* The input buffer is still not enough to store the number of cpus.  This
94084c
+       is an arbitrary values assuming such systems should be rare and there
94084c
+       is no offline cpus.  */
94084c
+    return max_num_cpus;
94084c
+  /* Some other error.  2 is conservative (not a uniprocessor system, so
94084c
+     atomics are needed). */
94084c
+  return 2;
94084c
 }
94084c
 libc_hidden_def (__get_nprocs)
94084c
 weak_alias (__get_nprocs, get_nprocs)