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