From nobody Mon Jan 03 02:31:47 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 07E08191A9A3; Mon, 3 Jan 2022 02:31:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JS0BC5S6xz4sFs; Mon, 3 Jan 2022 02:31:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 90A711D4DE; Mon, 3 Jan 2022 02:31:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2032Vl5b056585; Mon, 3 Jan 2022 02:31:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2032VlMU056584; Mon, 3 Jan 2022 02:31:47 GMT (envelope-from git) Date: Mon, 3 Jan 2022 02:31:47 GMT Message-Id: <202201030231.2032VlMU056584@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: d9cacbf4b010 - main - sched_get/setaffinity(): try to be more compatible with Linux 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=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d9cacbf4b010d96dbfe96a09259972a95c675f26 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641177107; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=V920LuMyxoHUCZS/gbKxkOATvZ0Lb0NwsQByG9pEERI=; b=hkpERPKtV+P0rp+/MDixAsM0UcY4UY9aOkjMWrr29HbHmsSw3k1VRDWy/GYe7UIPSY7VDg axlS5OyjnbK8zBK4O54SKKCypRFkNUgMi98ji6ntBj1ToTZ/3KFjLXqiGTA9iZlHlPuM1s 5iw/myJRNv11ZHEGDJfV7+pmgFli5yRw1K5x5FyU1xgiLaLcSq1qwlgK31ANP9ZsHJevyw hbayJFhpDeaaXBhedRzWWNV12L8ZYy+8otpw6hvukfgaWm1aB75y2C0WOcsz401WmyiUhu 5PwdKnesXOBlgNbi31L0rqsnI7xJeSGreE8e3lGvbBqScQjtXCWI5OKAAdiHug== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641177107; a=rsa-sha256; cv=none; b=kgKzXsWlfR8pBSGEr+OsMphADgTTbyPOxESmO5m4SohI34z/+O+KgFXg44NiDxcOXIOjSD gwBMpCx6iyucottbkkZpisAecXvf3UJ0A2zG4yJJU8J48/9kZciv/0qXPd1zWjPPQI7a3h 5FMnbZgBQLDoXWZzWBxpJTQt8ti+/H+gUZqGpy/08gSr6sYs+j2XTWq4sj6vooE8O+M4O4 Ocdi/fdpv26/xo5EnEE+Lbt+UokghfTgCUdZXx6KubQI0cvGVPgRHUXQUMtITeMG8T5GdT SRApwWYYb+B/36Y6aOcU+l3iwee6nCoBEA8b+3ewSt/qNUpv9hkZBQSasmO4rA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=d9cacbf4b010d96dbfe96a09259972a95c675f26 commit d9cacbf4b010d96dbfe96a09259972a95c675f26 Author: Konstantin Belousov AuthorDate: 2022-01-02 22:11:49 +0000 Commit: Konstantin Belousov CommitDate: 2022-01-03 02:31:40 +0000 sched_get/setaffinity(): try to be more compatible with Linux in handling the cpuset sizes different from sizeof(cpuset_t). For both cases, cpuset size shorter than sizeof(cpuset_t) results in EINVAL on Linux. For sched_getaffinity(), be more permissive and accept cpuset size larger than our cpuset_t, by clipping the syscall argument and zeroing the rest of the output buffer. For sched_setaffinity(), we should allow shorter cpusets than current ABI size, again zeroing the rest of the bits. With this change, python os.sched_get/setaffinity functions work. Reported by: se Sponsored by: The FreeBSD Foundation MFC after: 1 week --- lib/libc/gen/sched_getaffinity.c | 20 ++++++++++++++++++++ lib/libc/gen/sched_setaffinity.c | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c index 2ae8c5b763a3..7d345eb82a3b 100644 --- a/lib/libc/gen/sched_getaffinity.c +++ b/lib/libc/gen/sched_getaffinity.c @@ -26,11 +26,31 @@ * 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)); } diff --git a/lib/libc/gen/sched_setaffinity.c b/lib/libc/gen/sched_setaffinity.c index 52fc6d4bc811..91ab2a401bda 100644 --- a/lib/libc/gen/sched_setaffinity.c +++ b/lib/libc/gen/sched_setaffinity.c @@ -26,11 +26,22 @@ * SUCH DAMAGE. */ +#include #include +#include int sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset) { + cpuset_t c; + + if (cpusetsz > sizeof(cpuset_t)) { + errno = EINVAL; + return (-1); + } else { + memset(&c, 0, sizeof(c)); + memcpy(&c, cpuset, cpusetsz); + } return (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, - pid == 0 ? -1 : pid, cpusetsz, cpuset)); + pid == 0 ? -1 : pid, sizeof(cpuset_t), &c)); }