diff --git a/coreutils-nproc-affinity-1.patch b/coreutils-nproc-affinity-1.patch new file mode 100644 index 0000000..8748d4e --- /dev/null +++ b/coreutils-nproc-affinity-1.patch @@ -0,0 +1,55 @@ +commit 45c2456a56337ebcafe0dd9faa2bd995ccbc3357 +Author: Florian Weimer +Date: Mon Nov 11 14:05:53 2024 +0100 + + nproc: Use affinity mask even on systems with more than 1024 CPUs. + + * lib/nproc.c (num_processors_via_affinity_mask): Retry + with larger affinity masks if CPU_ALLOC_SIZE is available. + +diff --git a/lib/nproc.c b/lib/nproc.c +index 92a07e8289..48bc3d06fa 100644 +--- a/lib/nproc.c ++++ b/lib/nproc.c +@@ -20,6 +20,7 @@ + #include + #include "nproc.h" + ++#include + #include + #include + #include +@@ -124,6 +125,33 @@ num_processors_via_affinity_mask (void) + return count; + } + } ++#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC \ ++ && defined CPU_ALLOC_SIZE /* glibc >= 2.6 */ ++ { ++ unsigned int alloc_count = 1024; ++ while (1) ++ { ++ cpu_set_t *set = CPU_ALLOC (alloc_count); ++ if (set == NULL) ++ return 0; ++ unsigned int size = CPU_ALLOC_SIZE (alloc_count); ++ if (sched_getaffinity (0, size, set) == 0) ++ { ++ unsigned int count = CPU_COUNT_S (size, set); ++ CPU_FREE (set); ++ return count; ++ } ++ if (errno != EINVAL) ++ { ++ CPU_FREE (set); ++ return 0; ++ } ++ CPU_FREE (set); ++ alloc_count *= 2; ++ if (alloc_count == 0) ++ return 0; ++ } ++ } + #elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */ + { + cpu_set_t set; diff --git a/coreutils-nproc-affinity-2.patch b/coreutils-nproc-affinity-2.patch new file mode 100644 index 0000000..aeca09c --- /dev/null +++ b/coreutils-nproc-affinity-2.patch @@ -0,0 +1,64 @@ +commit ee0bc695303775da5026091a65e8ec2b764f4a26 +Author: Bruno Haible +Date: Mon Nov 11 15:40:52 2024 +0100 + + nproc: Use affinity mask even in out-of-memory situations. + + * lib/nproc.c (num_processors_via_affinity_mask): Use a stack-allocated + cpu_set_t as fallback. Add comments. + +diff --git a/lib/nproc.c b/lib/nproc.c +index 48bc3d06fa..0b5898d88f 100644 +--- a/lib/nproc.c ++++ b/lib/nproc.c +@@ -125,15 +125,25 @@ num_processors_via_affinity_mask (void) + return count; + } + } +-#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC \ +- && defined CPU_ALLOC_SIZE /* glibc >= 2.6 */ ++#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */ ++ /* There are two ways to use the sched_getaffinity() function: ++ - With a statically-sized cpu_set_t. ++ - With a dynamically-sized cpu_set_t. ++ Documentation: ++ ++ ++ The second way has the advantage that it works on systems with more than ++ 1024 CPUs. The first way has the advantage that it works also when memory ++ is tight. */ ++# if defined CPU_ALLOC_SIZE /* glibc >= 2.6 */ + { + unsigned int alloc_count = 1024; +- while (1) ++ for (;;) + { + cpu_set_t *set = CPU_ALLOC (alloc_count); + if (set == NULL) +- return 0; ++ /* Out of memory. */ ++ break; + unsigned int size = CPU_ALLOC_SIZE (alloc_count); + if (sched_getaffinity (0, size, set) == 0) + { +@@ -143,16 +153,19 @@ num_processors_via_affinity_mask (void) + } + if (errno != EINVAL) + { ++ /* Some other error. */ + CPU_FREE (set); + return 0; + } + CPU_FREE (set); ++ /* Retry with some larger cpu_set_t. */ + alloc_count *= 2; + if (alloc_count == 0) ++ /* Integer overflow. Avoid an endless loop. */ + return 0; + } + } +-#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */ ++# endif + { + cpu_set_t set; + diff --git a/coreutils-nproc-affinity.patch b/coreutils-nproc-affinity.patch deleted file mode 100644 index 8c682d4..0000000 --- a/coreutils-nproc-affinity.patch +++ /dev/null @@ -1,44 +0,0 @@ -diff -ur coreutils-9.5.orig/lib/nproc.c coreutils-9.5/lib/nproc.c ---- coreutils-9.5.orig/lib/nproc.c 2024-01-01 14:21:47.000000000 +0100 -+++ coreutils-9.5/lib/nproc.c 2024-11-11 10:48:05.435716502 +0100 -@@ -20,6 +20,7 @@ - #include - #include "nproc.h" - -+#include - #include - #include - #include -@@ -124,6 +125,31 @@ - return count; - } - } -+#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC \ -+ && defined CPU_COUNT /* glibc >= 2.3.4 */ -+ { -+ unsigned int alloc_count = 1024; -+ while (1) -+ { -+ cpu_set_t *set = CPU_ALLOC (alloc_count); -+ unsigned int size = CPU_ALLOC_SIZE (alloc_count); -+ if (sched_getaffinity (0, size, set) == 0) -+ { -+ unsigned int count = CPU_COUNT_S (size, set); -+ CPU_FREE (set); -+ return count; -+ } -+ if (errno != EINVAL) -+ { -+ CPU_FREE (set); -+ return 0; -+ } -+ CPU_FREE (set); -+ alloc_count *= 2; -+ if (alloc_count == 0) -+ return 0; -+ } -+ } - #elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */ - { - cpu_set_t set; -Only in coreutils-9.5/lib: nproc.c~ diff --git a/coreutils.spec b/coreutils.spec index 71334b9..759434b 100644 --- a/coreutils.spec +++ b/coreutils.spec @@ -1,7 +1,7 @@ Summary: A set of basic GNU tools commonly used in shell scripts Name: coreutils Version: 9.5 -Release: 10%{?dist}.nproc.1 +Release: 11%{?dist} # some used parts of gnulib are under various variants of LGPL License: GPL-3.0-or-later AND GFDL-1.3-no-invariants-or-later AND LGPL-2.1-or-later AND LGPL-3.0-or-later Url: https://www.gnu.org/software/coreutils/ @@ -36,7 +36,9 @@ Patch104: coreutils-df-direct.patch # https://git.savannah.gnu.org/cgit/gnulib.git/patch/?id=43f7f428a1665950233557bd97611bd5e996b5cb Patch105: coreutils-9.5-readutmp-web-session.patch -Patch106: coreutils-nproc-affinity.patch +# https://bugzilla.redhat.com/show_bug.cgi?id=2325167 +Patch106: coreutils-nproc-affinity-1.patch +Patch107: coreutils-nproc-affinity-2.patch # (sb) lin18nux/lsb compliance - multibyte functionality patch Patch800: coreutils-i18n.patch @@ -280,6 +282,9 @@ rm -f $RPM_BUILD_ROOT%{_infodir}/dir %license COPYING %changelog +* Wed Nov 13 2024 Florian Weimer - 9.5-11 +- Affinity mask handling in nproc for large CPU counts (rhbz#2325167) + * Fri Sep 27 2024 Lukáš Zaoral - 9.5-10 - fix fold -b with UTF8 locale (RHEL-60295)