From nobody Sun Jan 02 23:47:27 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 5CE9C19197F5; Sun, 2 Jan 2022 23:47:35 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4JRwXk6Mnzz4Tmq; Sun, 2 Jan 2022 23:47:34 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 202NlREC001841 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Mon, 3 Jan 2022 01:47:30 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 202NlREC001841 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 202NlRnY001840; Mon, 3 Jan 2022 01:47:27 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Mon, 3 Jan 2022 01:47:27 +0200 From: Konstantin Belousov To: Stefan Esser Cc: Antoine Brodin , Konstantin Belousov , src-committers , dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org, FreeBSD Ports Management Team Subject: Re: git: e2650af157bc - main - Make CPU_SET macros compliant with other implementations Message-ID: References: <202112301154.1BUBsR1q017491@gitrepo.freebsd.org> <9dffb50a-9374-be91-8007-ce8933571398@freebsd.org> <64aa958f-7a17-b504-b414-bdcbe7da5f51@freebsd.org> List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <64aa958f-7a17-b504-b414-bdcbe7da5f51@freebsd.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.5 X-Spam-Checker-Version: SpamAssassin 3.4.5 (2021-03-20) on tom.home X-Rspamd-Queue-Id: 4JRwXk6Mnzz4Tmq X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-ThisMailContainsUnwantedMimeParts: N On Mon, Jan 03, 2022 at 12:35:39AM +0100, Stefan Esser wrote: > Am 02.01.22 um 23:16 schrieb Konstantin Belousov: > > On Sun, Jan 02, 2022 at 10:45:14PM +0100, Stefan Esser wrote: > >> Am 02.01.22 um 20:51 schrieb Antoine Brodin: > [...] > >> Python 3.8.12 (default, Dec 31 2021, 10:50:47) > >>>>> import os > >>>>> os.sched_getaffinity(0) > >> Traceback (most recent call last): > >> File "", line 1, in > >> OSError: [Errno 34] Result too large > >> > >> This is a Python interpreter problem: it seems that the wrapper > >> for the sched_getaffinity() function that has been introduced by > >> kib in is buggy. > >> > >> As a work-around I have added a patch to comment out the > >> os.sched_getaffinity(0) call (which used to cause an Attribute > >> error that was caught by try/except, before). > >> > >> See ports commit 507c189b2876. > > > > Buggy in which way? > > My assumption was that the wrapper in the Python interpreter in > Modules/posixmodules.c function os_sched_getaffinity_impl() does > not work with the FreeBSD implementation of sched_getaffinity(). > > The relevant code in the Python wrapper is: > > ncpus = NCPUS_START; > while (1) { > setsize = CPU_ALLOC_SIZE(ncpus); > mask = CPU_ALLOC(ncpus); > if (mask == NULL) > return PyErr_NoMemory(); > if (sched_getaffinity(pid, setsize, mask) == 0) > break; > CPU_FREE(mask); > if (errno != EINVAL) > return posix_error(); > if (ncpus > INT_MAX / 2) { > PyErr_SetString(PyExc_OverflowError, "could not allocate " > "a large enough CPU set"); > return NULL; > } > ncpus = ncpus * 2; > } > > NCPUS_START is 8 * sizeof(unsigned long) = 64 on a 64 bit CPU. > > > Our cpuset_getaffinity(2) syscall returns ERANGE for cpuset size not > > equal to CPU_SETSIZE. It seems that python source expects EINVAL in > > this case. > > Yes, anything except EINVAL will cause the loop to exit prematurely. > > > I can change the wrapper to translate ERANGE to EINVAL. sched_setaffinity() > > probably would require a symmetrical patch, but lets postpone it. > > Yes. > > > diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c > > index 2ae8c5b763a3..8748d7a60278 100644 > > --- a/lib/libc/gen/sched_getaffinity.c > > +++ b/lib/libc/gen/sched_getaffinity.c > > @@ -26,11 +26,29 @@ > > * SUCH DAMAGE. > > */ > > > > +#include > > #include > > +#include > > > > int > > sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset) > > { > > + /* > > + * Be more Linux-compatible: > > + * - return EINVAL in passed size is less than size of cpuset_t > > + * in advance, instead of ERANGE from the syscall > > + * - if passed size is larger than the size of cpuset_t, be > > + * permissive by claming it back to sizeof(cpuset_t) and > > + * zeroing the rest. > > + */ > > + if (cpusetsz < sizeof(cpuset_t)) > > + return (EINVAL); > > + if (cpusetsz > sizeof(cpuset_t)) { > > + memset((char *)cpuset + sizeof(cpuset_t), 0, > > + cpusetsz - sizeof(cpuset_t)); > > + cpusetsz = sizeof(cpuset_t); > > + } > > + > > return (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, > > pid == 0 ? -1 : pid, cpusetsz, cpuset)); > > } > > I have rebuilt the C library with this patch, but it did not fix > the problem, since the value checked in the loop is errno, not > the return code of sched_getaffinity(). I see, thank you for noting this. > > The following code is tested to work: > > #include > #include > > int > sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset) > { > int result; > > result = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, > pid == 0 ? -1 : pid, cpusetsz, cpuset); > > if (result && errno == ERANGE) > errno = EINVAL; > > return (result); > } I want to be more permissive outright, in particular, allow larger cpusets than kernel handles. Updated patch is below. diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c index 2ae8c5b763a3..a26d098deb83 100644 --- a/lib/libc/gen/sched_getaffinity.c +++ b/lib/libc/gen/sched_getaffinity.c @@ -26,11 +26,30 @@ * SUCH DAMAGE. */ +#include #include +#include int sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset) { + /* + * Be more Linux-compatible: + * - return EINVAL in passed size is less than size of cpuset_t + * in advance, instead of ERANGE from the syscall + * - if passed size is larger than the size of cpuset_t, be + * permissive by claming it back to sizeof(cpuset_t) and + * zeroing the rest. + */ + if (cpusetsz < sizeof(cpuset_t)) { + errno = EINVAL; + return (-1); + if (cpusetsz > sizeof(cpuset_t)) { + memset((char *)cpuset + sizeof(cpuset_t), 0, + cpusetsz - sizeof(cpuset_t)); + cpusetsz = sizeof(cpuset_t); + } + return (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid == 0 ? -1 : pid, cpusetsz, cpuset)); }