From patchwork Wed May 20 16:24:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 6831 Received: (qmail 68277 invoked by alias); 20 May 2015 16:24:15 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 68262 invoked by uid 89); 20 May 2015 16:24:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=AWL, BAYES_50, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Message-ID: <555CB525.6050505@redhat.com> Date: Wed, 20 May 2015 18:24:05 +0200 From: Florian Weimer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: GNU C Library CC: KOSAKI Motohiro Subject: [PATCH] Remove CPU mask size detection from setaffinity The code looks quite broken to me and fails to achieve what it tries to do, as explained in the commit message. From a1a557e02d8187dc7bb344da5dbd7bf1b0630fdc Mon Sep 17 00:00:00 2001 Message-Id: From: Florian Weimer Date: Wed, 20 May 2015 18:21:12 +0200 Subject: [PATCH] Remove CPU set size checking from sched_setaffinity, pthread_setaffinity_np To: libc-alpha@sourceware.org With current kernel versions, the check does not reliably detect that unavailable CPUs are requested, for these reasons: (1) The kernel will silently ignore non-allowed CPUs. (2) Similarly, CPU bits which lack an online CPU (possible CPUs) are ignored. (3) The existing probing code assumes that the CPU mask size is a power of two and at least 1024. Neither it has to be a power of two, nor is the minimum possible value 1024, so the value determined is often too large, resulting in incorrect false negatives. The kernel will still return EINVAL if no CPU in the requested set remains which can run the current thread after the affinity change. Applications which care about the exact affinity mask will have to query it using sched_getaffinity after setting it. --- ChangeLog | 21 ++ manual/threads.texi | 2 - nptl/Makefile | 3 +- nptl/check-cpuset.h | 32 ---- nptl/pthread_attr_setaffinity.c | 6 - nptl/pthread_setattr_default_np.c | 5 - nptl/tst-thread-affinity.c | 25 +++ posix/Makefile | 3 +- posix/tst-affinity.c | 265 ++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/check-cpuset.h | 48 ----- sysdeps/unix/sysv/linux/pthread_setaffinity.c | 48 ----- sysdeps/unix/sysv/linux/sched_setaffinity.c | 37 ---- 12 files changed, 315 insertions(+), 180 deletions(-) delete mode 100644 nptl/check-cpuset.h create mode 100644 nptl/tst-thread-affinity.c create mode 100644 posix/tst-affinity.c delete mode 100644 sysdeps/unix/sysv/linux/check-cpuset.h diff --git a/ChangeLog b/ChangeLog index 4de8a25..efb21aa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,24 @@ +2015-05-18 Florian Weimer + + * nptl/check-cpuset.h: Remove. + * nptl/pthread_attr_setaffinity.c (__pthread_attr_setaffinity_new): + Remove CPU set size check. + * nptl/pthread_setattr_default_np.c (pthread_setattr_default_np): + Likewise. + * sysdeps/unix/sysv/linux/check-cpuset.h: Remove. + * sysdeps/unix/sysv/linux/pthread_setaffinity.c + (__kernel_cpumask_size, __determine_cpumask_size): Remove. + (__pthread_setaffinity_new): Remove CPU set size check. + * sysdeps/unix/sysv/linux/sched_setaffinity.c + (__kernel_cpumask_size): Remove. + (__sched_setaffinity_new): Remove CPU set size check. + * manual/threads.texi (Default Thread Attributes): Remove stale + reference to check_cpuset_attr, determine_cpumask_size in comment. + * posix/Makefile (tests): Add tst-affinity. + * posix/tst-affinity.c: New file. + * nptl/Makefile (tests): Add tst-thread-affinity. + * nptl/tst-thread-affinity.c: New file. + 2015-05-18 Arjun Shankar * include/stdio.h: Define __need_wint_t. diff --git a/manual/threads.texi b/manual/threads.texi index 4d080d4..00cc725 100644 --- a/manual/threads.texi +++ b/manual/threads.texi @@ -111,8 +111,6 @@ failure. @c check_sched_priority_attr ok @c sched_get_priority_min dup ok @c sched_get_priority_max dup ok -@c check_cpuset_attr ok -@c determine_cpumask_size ok @c check_stacksize_attr ok @c lll_lock @asulock @aculock @c free dup @ascuheap @acsmem diff --git a/nptl/Makefile b/nptl/Makefile index d784c8d..2dc5467 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -272,7 +272,8 @@ tests = tst-typesizes \ tst-getpid3 \ tst-setuid3 \ tst-initializers1 $(addprefix tst-initializers1-,c89 gnu89 c99 gnu99) \ - tst-bad-schedattr + tst-bad-schedattr \ + tst-thread-affinity xtests = tst-setuid1 tst-setuid1-static tst-setuid2 \ tst-mutexpp1 tst-mutexpp6 tst-mutexpp10 test-srcs = tst-oddstacklimit diff --git a/nptl/check-cpuset.h b/nptl/check-cpuset.h deleted file mode 100644 index 315bdf2..0000000 --- a/nptl/check-cpuset.h +++ /dev/null @@ -1,32 +0,0 @@ -/* Validate cpu_set_t values for NPTL. Stub version. - Copyright (C) 2015 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include - -/* Returns 0 if CS and SZ are valid values for the cpuset and cpuset size - respectively. Otherwise it returns an error number. */ -static inline int -check_cpuset_attr (const cpu_set_t *cs, const size_t sz) -{ - if (sz == 0) - return 0; - - /* This means pthread_attr_setaffinity will return ENOSYS, which - is the right thing when the cpu_set_t features are not available. */ - return ENOSYS; -} diff --git a/nptl/pthread_attr_setaffinity.c b/nptl/pthread_attr_setaffinity.c index 7a127b8..571835d 100644 --- a/nptl/pthread_attr_setaffinity.c +++ b/nptl/pthread_attr_setaffinity.c @@ -23,7 +23,6 @@ #include #include #include -#include int @@ -43,11 +42,6 @@ __pthread_attr_setaffinity_new (pthread_attr_t *attr, size_t cpusetsize, } else { - int ret = check_cpuset_attr (cpuset, cpusetsize); - - if (ret) - return ret; - if (iattr->cpusetsize != cpusetsize) { void *newp = (cpu_set_t *) realloc (iattr->cpuset, cpusetsize); diff --git a/nptl/pthread_setattr_default_np.c b/nptl/pthread_setattr_default_np.c index 457a467..1a661f1 100644 --- a/nptl/pthread_setattr_default_np.c +++ b/nptl/pthread_setattr_default_np.c @@ -21,7 +21,6 @@ #include #include #include -#include int @@ -48,10 +47,6 @@ pthread_setattr_default_np (const pthread_attr_t *in) return ret; } - ret = check_cpuset_attr (real_in->cpuset, real_in->cpusetsize); - if (ret) - return ret; - /* stacksize == 0 is fine. It means that we don't change the current value. */ if (real_in->stacksize != 0) diff --git a/nptl/tst-thread-affinity.c b/nptl/tst-thread-affinity.c new file mode 100644 index 0000000..ff2e88a --- /dev/null +++ b/nptl/tst-thread-affinity.c @@ -0,0 +1,25 @@ +/* Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#define SETAFFINITY(size, set) \ + pthread_setaffinity_np (pthread_self (), (size), (set)) +#define GETAFFINITY(size, set) \ + pthread_getaffinity_np (pthread_self (), (size), (set)) + +#include "../posix/tst-affinity.c" diff --git a/posix/Makefile b/posix/Makefile index 15e8818..5e70a10 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -87,7 +87,8 @@ tests := tstgetopt testfnm runtests runptests \ bug-getopt1 bug-getopt2 bug-getopt3 bug-getopt4 \ bug-getopt5 tst-getopt_long1 bug-regex34 bug-regex35 \ tst-pathconf tst-getaddrinfo4 tst-rxspencer-no-utf8 \ - tst-fnmatch3 bug-regex36 tst-getaddrinfo5 + tst-fnmatch3 bug-regex36 tst-getaddrinfo5 \ + tst-affinity xtests := bug-ga2 ifeq (yes,$(build-shared)) test-srcs := globtest diff --git a/posix/tst-affinity.c b/posix/tst-affinity.c new file mode 100644 index 0000000..ceb703c --- /dev/null +++ b/posix/tst-affinity.c @@ -0,0 +1,265 @@ +/* Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* This file is included by nptl/tst-thread-affinity.c to test the + pthread variants of the functions. */ + +#include +#include +#include +#include +#include + +/* Overide this to test other functions. */ +#ifndef GETAFFINITY +#define GETAFFINITY(size, set) sched_getaffinity (0, (size), (set)) +#endif +#ifndef SETAFFINITY +#define SETAFFINITY(size, set) sched_setaffinity (0, (size), (set)) +#endif + +struct conf +{ + int set_size; /* in bits */ + int first_cpu; + int second_cpu; +}; + +static int +find_set_size (void) +{ + /* We need to use multiples of 64 because otherwise, CPU_ALLOC + over-allocates, and and we do not see all bits returned by the + kernel. */ + for (int num_cpus = 64; num_cpus <= INT_MAX / 2; num_cpus += 64) + { + cpu_set_t *set = CPU_ALLOC (num_cpus); + size_t size = CPU_ALLOC_SIZE (num_cpus); + + if (set == NULL) + { + printf ("CPU_ALLOC(%d) failed\n", num_cpus); + return -1; + } + if (GETAFFINITY (size, set) == 0) + { + CPU_FREE (set); + return num_cpus; + } + if (errno != EINVAL) + { + printf ("getaffinity for %d CPUs: %m\n", num_cpus); + CPU_FREE (set); + return -1; + } + CPU_FREE (set); + } + puts ("Cannot find maximum CPU number"); + return -1; +} + +static int +find_flippable_cpu (const cpu_set_t *set, size_t size, int start_cpu) +{ + size_t cpus_found = 0; + size_t total_cpus = CPU_COUNT_S (size, set); + + /* We have to look at all CPU bits because there is no interface + which gives us the number of bits in *set. */ + for (int cpu = 0; cpus_found < total_cpus; ++cpu) + { + if (CPU_ISSET_S (cpu, size, set)) + { + if (cpu >= start_cpu) + return cpu; + ++cpus_found; + } + } + return -1; +} + +static void +setup_conf (struct conf *conf) +{ + *conf = (struct conf) {-1, -1, -1}; + conf->set_size = find_set_size (); + if (conf->set_size > 0) + { + cpu_set_t *set = CPU_ALLOC (conf->set_size); + + if (set == NULL) + { + printf ("CPU_ALLOC (%d) failed\n", conf->set_size); + CPU_FREE (set); + return; + } + if (GETAFFINITY (CPU_ALLOC_SIZE (conf->set_size), set) < 0) + { + printf ("getaffinity failed: %m\n"); + CPU_FREE (set); + return; + } + conf->first_cpu = find_flippable_cpu + (set, CPU_ALLOC_SIZE (conf->set_size), 0); + if (conf->first_cpu >= 0) + conf->second_cpu = find_flippable_cpu + (set, CPU_ALLOC_SIZE (conf->set_size), conf->first_cpu + 1); + else + puts ("No test CPU found"); + if (conf->second_cpu < 0) + puts ("No second test CPU found (reducing test scope)"); + CPU_FREE (set); + } +} + +static bool +test_size (const struct conf *conf, size_t size) +{ + cpu_set_t *initial_set = CPU_ALLOC (size); + cpu_set_t *set2 = CPU_ALLOC (size); + cpu_set_t *active_cpu_set = CPU_ALLOC (size); + int active_cpu; + + if (initial_set == NULL || set2 == NULL || active_cpu_set == NULL) + { + printf ("size %zu: CPU_ALLOC failed\n", size); + return false; + } + size = CPU_ALLOC_SIZE (size); + + if (GETAFFINITY (size, initial_set) < 0) + { + printf ("size %zu: getaffinity: %m\n", size); + return false; + } + if (SETAFFINITY (size, initial_set) < 0) + { + printf ("size %zu: setaffinity: %m\n", size); + return true; + } + + /* Use one-CPU set to test switching between CPUs. */ + CPU_ZERO_S (size, active_cpu_set); + CPU_SET_S (conf->first_cpu, size, active_cpu_set); + if (SETAFFINITY (size, active_cpu_set) < 0) + { + printf ("size %zu: setaffinity (2): %m\n", size); + return false; + } + active_cpu = sched_getcpu (); + if (active_cpu != conf->first_cpu) + { + printf ("Unexpected first CPU: %d\n", active_cpu); + return false; + } + if (GETAFFINITY (size, set2) < 0) + { + printf ("size %zu: getaffinity (2): %m\n", size); + return false; + } + if (!CPU_EQUAL_S (size, active_cpu_set, set2)) + { + printf ("size %zu: CPU sets do not match\n", size); + return false; + } + + if (conf->second_cpu >= 0) + { + CPU_ZERO_S (size, active_cpu_set); + CPU_SET_S (conf->second_cpu, size, active_cpu_set); + sched_yield (); + active_cpu = sched_getcpu (); + if (active_cpu != conf->first_cpu) + { + printf ("Unexpected first CPU (2): %d\n", active_cpu); + return false; + } + if (SETAFFINITY (size, active_cpu_set) < 0) + { + printf ("size %zu: setaffinity (2): %m\n", size); + return false; + } + active_cpu = sched_getcpu (); + if (active_cpu != conf->second_cpu) + { + printf ("Unexpected second CPU: %d\n", active_cpu); + return false; + } + if (GETAFFINITY (size, set2) < 0) + { + printf ("size %zu: getaffinity (2): %m\n", size); + return false; + } + if (!CPU_EQUAL_S (size, active_cpu_set, set2)) + { + printf ("size %zu: CPU sets do not match\n", size); + return false; + } + } + + if (SETAFFINITY (size, initial_set) < 0) + { + printf ("size %zu: setaffinity (3): %m\n", size); + return false; + } + if (GETAFFINITY (size, set2) < 0) + { + printf ("size %zu: getaffinity (3): %m\n", size); + return false; + } + if (!CPU_EQUAL_S (size, initial_set, set2)) + { + printf ("size %zu: CPU sets do not match (2)\n", size); + return false; + } + + CPU_FREE (initial_set); + CPU_FREE (set2); + CPU_FREE (active_cpu_set); + + return true; +} + +static int +do_test (void) +{ + { + cpu_set_t set; + if (GETAFFINITY (sizeof (set), &set) < 0 && errno == ENOSYS) + { + puts ("getaffinity not supported"); + return 0; + } + } + + struct conf conf; + setup_conf (&conf); + printf ("Detected CPU set size (in bits): %d\n", conf.set_size); + printf ("First test CPU: %d\n", conf.first_cpu); + printf ("Second test CPU: %d\n", conf.second_cpu); + if (conf.set_size < 0 || conf.first_cpu < 0) + return 1; + + if (test_size (&conf, 1024) + && test_size (&conf, 2) + && test_size (&conf, 1024 * 1024)) + return 0; + return 1; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/check-cpuset.h b/sysdeps/unix/sysv/linux/check-cpuset.h deleted file mode 100644 index 1d55e0b..0000000 --- a/sysdeps/unix/sysv/linux/check-cpuset.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Validate cpu_set_t values for NPTL. Linux version. - Copyright (C) 2002-2015 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include - - -/* Defined in pthread_setaffinity.c. */ -extern size_t __kernel_cpumask_size attribute_hidden; -extern int __determine_cpumask_size (pid_t tid); - -/* Returns 0 if CS and SZ are valid values for the cpuset and cpuset size - respectively. Otherwise it returns an error number. */ -static inline int -check_cpuset_attr (const cpu_set_t *cs, const size_t sz) -{ - if (__kernel_cpumask_size == 0) - { - int res = __determine_cpumask_size (THREAD_SELF->tid); - if (res) - return res; - } - - /* Check whether the new bitmask has any bit set beyond the - last one the kernel accepts. */ - for (size_t cnt = __kernel_cpumask_size; cnt < sz; ++cnt) - if (((char *) cs)[cnt] != '\0') - /* Found a nonzero byte. This means the user request cannot be - fulfilled. */ - return EINVAL; - - return 0; -} diff --git a/sysdeps/unix/sysv/linux/pthread_setaffinity.c b/sysdeps/unix/sysv/linux/pthread_setaffinity.c index e891818..2ebf09d 100644 --- a/sysdeps/unix/sysv/linux/pthread_setaffinity.c +++ b/sysdeps/unix/sysv/linux/pthread_setaffinity.c @@ -23,62 +23,14 @@ #include -size_t __kernel_cpumask_size attribute_hidden; - - -/* Determine the size of cpumask_t in the kernel. */ -int -__determine_cpumask_size (pid_t tid) -{ - size_t psize; - int res; - - for (psize = 128; ; psize *= 2) - { - char buf[psize]; - INTERNAL_SYSCALL_DECL (err); - - res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, tid, psize, buf); - if (INTERNAL_SYSCALL_ERROR_P (res, err)) - { - if (INTERNAL_SYSCALL_ERRNO (res, err) != EINVAL) - return INTERNAL_SYSCALL_ERRNO (res, err); - } - else - break; - } - - if (res != 0) - __kernel_cpumask_size = res; - - return 0; -} - - int __pthread_setaffinity_new (pthread_t th, size_t cpusetsize, const cpu_set_t *cpuset) { const struct pthread *pd = (const struct pthread *) th; - INTERNAL_SYSCALL_DECL (err); int res; - if (__glibc_unlikely (__kernel_cpumask_size == 0)) - { - res = __determine_cpumask_size (pd->tid); - if (res != 0) - return res; - } - - /* We now know the size of the kernel cpumask_t. Make sure the user - does not request to set a bit beyond that. */ - for (size_t cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt) - if (((char *) cpuset)[cnt] != '\0') - /* Found a nonzero byte. This means the user request cannot be - fulfilled. */ - return EINVAL; - res = INTERNAL_SYSCALL (sched_setaffinity, err, 3, pd->tid, cpusetsize, cpuset); diff --git a/sysdeps/unix/sysv/linux/sched_setaffinity.c b/sysdeps/unix/sysv/linux/sched_setaffinity.c index b528617..dfddce7 100644 --- a/sysdeps/unix/sysv/linux/sched_setaffinity.c +++ b/sysdeps/unix/sysv/linux/sched_setaffinity.c @@ -22,50 +22,13 @@ #include #include #include -#include #ifdef __NR_sched_setaffinity -static size_t __kernel_cpumask_size; - int __sched_setaffinity_new (pid_t pid, size_t cpusetsize, const cpu_set_t *cpuset) { - if (__glibc_unlikely (__kernel_cpumask_size == 0)) - { - INTERNAL_SYSCALL_DECL (err); - int res; - - size_t psize = 128; - void *p = alloca (psize); - - while (res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, getpid (), - psize, p), - INTERNAL_SYSCALL_ERROR_P (res, err) - && INTERNAL_SYSCALL_ERRNO (res, err) == EINVAL) - p = extend_alloca (p, psize, 2 * psize); - - if (res == 0 || INTERNAL_SYSCALL_ERROR_P (res, err)) - { - __set_errno (INTERNAL_SYSCALL_ERRNO (res, err)); - return -1; - } - - __kernel_cpumask_size = res; - } - - /* We now know the size of the kernel cpumask_t. Make sure the user - does not request to set a bit beyond that. */ - for (size_t cnt = __kernel_cpumask_size; cnt < cpusetsize; ++cnt) - if (((char *) cpuset)[cnt] != '\0') - { - /* Found a nonzero byte. This means the user request cannot be - fulfilled. */ - __set_errno (EINVAL); - return -1; - } - int result = INLINE_SYSCALL (sched_setaffinity, 3, pid, cpusetsize, cpuset); #ifdef RESET_VGETCPU_CACHE -- 2.1.0