aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Chagin <dchagin@FreeBSD.org>2023-02-15 09:23:15 +0000
committerDmitry Chagin <dchagin@FreeBSD.org>2023-02-15 09:23:15 +0000
commitcbc32e4c5e8427e0f1032d9d3aa5863dd1538c11 (patch)
tree461fd17f2699a40ea4bf6499cd305f2282059a5b
parent3bafe73affe37de3e94d91d4c1310f1272375970 (diff)
downloadsrc-cbc32e4c5e8427e0f1032d9d3aa5863dd1538c11.tar.gz
src-cbc32e4c5e8427e0f1032d9d3aa5863dd1538c11.zip
cpuset: Add compat shim to the sched_affinity functions
To allow to run a newer world on a pre-1400079 kernel a compat shims to the sched_affinity functions has beed added. Reported by: antoine Tested by: antoine Reviewed by: kib Differential revision: https://reviews.freebsd.org/D38555 MFC after: 3 days
-rw-r--r--lib/libc/gen/sched_getaffinity.c15
-rw-r--r--lib/libc/gen/sched_setaffinity.c14
-rw-r--r--lib/libc/include/libc_private.h6
-rw-r--r--sys/sys/param.h1
4 files changed, 34 insertions, 2 deletions
diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c
index 95145a1eb019..fce47fbfc0d2 100644
--- a/lib/libc/gen/sched_getaffinity.c
+++ b/lib/libc/gen/sched_getaffinity.c
@@ -26,16 +26,29 @@
* SUCH DAMAGE.
*/
+#define _WANT_P_OSREL
+#include <sys/param.h>
#include <errno.h>
#include <sched.h>
#include <string.h>
+#include "libc_private.h"
+
int
sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset)
{
+ cpuwhich_t which;
int error;
- error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID,
+ if (__getosreldate() < P_OSREL_TIDPID) {
+ if (pid == 0 || pid > _PID_MAX)
+ which = CPU_WHICH_TID;
+ else
+ which = CPU_WHICH_PID;
+ } else
+ which = CPU_WHICH_TIDPID;
+
+ error = cpuset_getaffinity(CPU_LEVEL_WHICH, which,
pid == 0 ? -1 : pid, cpusetsz, cpuset);
if (error == -1 && errno == ERANGE)
errno = EINVAL;
diff --git a/lib/libc/gen/sched_setaffinity.c b/lib/libc/gen/sched_setaffinity.c
index 36ed0f45d417..b878e1affc41 100644
--- a/lib/libc/gen/sched_setaffinity.c
+++ b/lib/libc/gen/sched_setaffinity.c
@@ -26,20 +26,32 @@
* SUCH DAMAGE.
*/
+#define _WANT_P_OSREL
#include <sys/param.h>
#include <sys/sysctl.h>
#include <errno.h>
#include <sched.h>
#include <string.h>
+#include "libc_private.h"
+
int
sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset)
{
static int mp_maxid;
+ cpuwhich_t which;
cpuset_t c;
int error, lbs, cpu;
size_t len, sz;
+ if (__getosreldate() < P_OSREL_TIDPID) {
+ if (pid == 0 || pid > _PID_MAX)
+ which = CPU_WHICH_TID;
+ else
+ which = CPU_WHICH_PID;
+ } else
+ which = CPU_WHICH_TIDPID;
+
sz = cpusetsz > sizeof(cpuset_t) ? sizeof(cpuset_t) : cpusetsz;
memset(&c, 0, sizeof(c));
memcpy(&c, cpuset, sz);
@@ -58,7 +70,7 @@ sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset)
if (cpu > mp_maxid)
CPU_CLR(cpu, &c);
}
- error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID,
+ error = cpuset_setaffinity(CPU_LEVEL_WHICH, which,
pid == 0 ? -1 : pid, sizeof(cpuset_t), &c);
if (error == -1 && errno == EDEADLK)
errno = EINVAL;
diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h
index 641b9272c7fc..0a8e531e0c3c 100644
--- a/lib/libc/include/libc_private.h
+++ b/lib/libc/include/libc_private.h
@@ -40,6 +40,12 @@
#include <sys/_pthreadtypes.h>
/*
+ * The kernel doesn't expose PID_MAX to the user space. Save it here
+ * to allow to run a newer world on a pre-1400079 kernel.
+ */
+#define _PID_MAX 99999
+
+/*
* This global flag is non-zero when a process has created one
* or more threads. It is used to avoid calling locking functions
* when they are not required.
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 2d5edf9a0b90..e0808609447d 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -107,6 +107,7 @@
#define P_OSREL_CK_SUPERBLOCK 1300000
#define P_OSREL_CK_INODE 1300005
#define P_OSREL_POWERPC_NEW_AUX_ARGS 1300070
+#define P_OSREL_TIDPID 1400079
#define P_OSREL_MAJOR(x) ((x) / 100000)
#endif