From nobody Mon Feb 06 19:07:49 2023 X-Original-To: dev-commits-src-branches@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 4P9bPK3q43z3n4QM; Mon, 6 Feb 2023 19:07:49 +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 4P9bPK2rRBz3QH3; Mon, 6 Feb 2023 19:07:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675710469; 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=VyjxEDGXYqpJASeX3/pR6jUrRTL/EMMK0v5r9UBFqcs=; b=SUQjMUkv9sxbD1H6WQZzpRDMbj/AtIOEVKQ02KtyFLwMGYz+1Z0DCoexHBWz85RhXP7E5g kZ9QMLLK8exWDkFkRo6VW+9U6x7xzN0kbQEZoQqMbVKo5aEuzO5OWM2ow0sXo8YeV2MOXd HTAwnhFxLcHhee5ExrlDDJOFe9ZZEx4hZxRkJqqS/yz3MF9+mAKhKWZZBckrb1N3gWS4O5 N11A7nl3y1CoADOWB8wJ2YM75mgWuQCCISeHnkM6qbBThGtQQwtD+fY18iIk8/J4UJWsQK HaxBtZDcB2ti00XdHMiRiTRagmYXuxuD2jBuRv/Ac6j+yYRiIxhKg76SYa1HVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675710469; 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=VyjxEDGXYqpJASeX3/pR6jUrRTL/EMMK0v5r9UBFqcs=; b=b/Li69qrobrxbftnncuq/SYVt37fAzFxjVdhs7dJQrDAQzz2c6vLSX29Jqk3+nrtD98D8z x+JewYnduGMRSbJQPrah/vfT/hXTswvxp6uv+AteNKyTdpe5YmRo6zuaZrwNctKxqIQ86s ktK8+hTaNMu5oPlDfDUwLLLafxrgvq6Dv/SP05fe/c8I1YEego6VXNQz2sLWe1POTqNW2/ cvN/qEA4t4K6RJagL+g8cSZoH2yWGh+X6/1suhOgN2Y/MtruLCYU1TLW9dVreQg5QEubco CCZjrXzoGXVsaT3agGwYt6f5XvTGIPo1gb5gafjxc6jyxUoSlgg9CIx0kzbTZA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675710469; a=rsa-sha256; cv=none; b=xWzRmJkZyd1SRLiCuUT++rTRyRt+BQd48ruhKPaPG6sSc0GU0uGEzaq2yAyREAOWQbrhgG AZu5X+FEJ70NHU/Y7oPRri1nlJ++CP3m7/5y0bBobumfQjqfTNKQgAMMIu6C9wFBr6BIU0 JxWbvPIlTq09c5WsDPBeHHuX9NbzJ1KNOA4vse/3L0uiTgaRCUxHNAP6mTWpiKMtJe1P1z vyEVxblfE8glCv3vd7wKWfBliJxZ25qZFvAkcIjzOG5lsma8ETCbDWpxnTlL13CaO64GL7 xeS7w8q9A/awQKx+PZNg+BMSXfbW/HQQVKRuO56PPm93Q5u4HW7QS/nlY44fzw== 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 4P9bPK1xXWzmLx; Mon, 6 Feb 2023 19:07:49 +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 316J7n43076983; Mon, 6 Feb 2023 19:07:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 316J7nEZ076982; Mon, 6 Feb 2023 19:07:49 GMT (envelope-from git) Date: Mon, 6 Feb 2023 19:07:49 GMT Message-Id: <202302061907.316J7nEZ076982@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 69e8cea385db - stable/13 - cpuset: Fix sched_[g|s]etaffinity() for better compatibility with Linux. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 69e8cea385db64f86ccd3e9a2ab6ff61c5976f64 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=69e8cea385db64f86ccd3e9a2ab6ff61c5976f64 commit 69e8cea385db64f86ccd3e9a2ab6ff61c5976f64 Author: Dmitry Chagin AuthorDate: 2023-01-29 13:17:33 +0000 Commit: Dmitry Chagin CommitDate: 2023-02-06 19:06:31 +0000 cpuset: Fix sched_[g|s]etaffinity() for better compatibility with Linux. Under Linux to sched_[g|s]etaffinity() functions the value returned from a call to gettid(2) (thread id) can be passed in the argument pid. Specifying pid as 0 will set the attribute for the calling thread, and passing the value returned from a call to getpid(2) (process id) will set the attribute for the main thread of the thread group. Native cpuset(2) family of system calls has "which" argument to determine how the value of id argument is interpreted, i.e., CPU_WHICH_TID is used to pass a thread id and CPU_WHICH_PID - to pass a process id. For now native sched_[g|s]etaffinity() implementation is wrong as uses "which" CPU_WHICH_PID to pass both (process and thread id) to the kernel. To fix this adding a new "which" CPU_WHICH_TIDPID intended to handle both id's. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D38209 MFC after: 1 week (cherry picked from commit c21b080f3dc2f5e91ada608d4385b7ed6538ba9b) --- lib/libc/gen/sched_getaffinity.c | 2 +- lib/libc/gen/sched_setaffinity.c | 2 +- lib/libc/sys/cpuset.2 | 4 +++- sys/kern/kern_cpuset.c | 42 +++++++++++++++++++++++++++++++++++++++- sys/sys/cpuset.h | 1 + 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c index ed304c111985..95145a1eb019 100644 --- a/lib/libc/gen/sched_getaffinity.c +++ b/lib/libc/gen/sched_getaffinity.c @@ -35,7 +35,7 @@ sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset) { int error; - error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + error = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID, 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 0052521cd081..36ed0f45d417 100644 --- a/lib/libc/gen/sched_setaffinity.c +++ b/lib/libc/gen/sched_setaffinity.c @@ -58,7 +58,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_PID, + error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TIDPID, pid == 0 ? -1 : pid, sizeof(cpuset_t), &c); if (error == -1 && errno == EDEADLK) errno = EINVAL; diff --git a/lib/libc/sys/cpuset.2 b/lib/libc/sys/cpuset.2 index a2e5269ac042..35adf6a9de41 100644 --- a/lib/libc/sys/cpuset.2 +++ b/lib/libc/sys/cpuset.2 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd May 3, 2017 +.Dd January 29, 2023 .Dt CPUSET 2 .Os .Sh NAME @@ -99,6 +99,7 @@ argument may have the following values: .Bl -column CPU_WHICH_INTRHANDLER -offset indent .It Dv CPU_WHICH_TID Ta "id is lwpid_t (thread id)" .It Dv CPU_WHICH_PID Ta "id is pid_t (process id)" +.It Dv CPU_WHICH_TIDPID Ta "id is either a thread or process id" .It Dv CPU_WHICH_JAIL Ta "id is jid (jail id)" .It Dv CPU_WHICH_CPUSET Ta "id is a cpusetid_t (cpuset id)" .It Dv CPU_WHICH_IRQ Ta "id is an irq number" @@ -114,6 +115,7 @@ of '-1' may be used with a of .Dv CPU_WHICH_TID , .Dv CPU_WHICH_PID , +.Dv CPU_WHICH_TIDPID , or .Dv CPU_WHICH_CPUSET to mean the current thread, process, or current thread's diff --git a/sys/kern/kern_cpuset.c b/sys/kern/kern_cpuset.c index d2bbfff1337d..1c8fa09cc224 100644 --- a/sys/kern/kern_cpuset.c +++ b/sys/kern/kern_cpuset.c @@ -927,6 +927,22 @@ cpuset_which(cpuwhich_t which, id_t id, struct proc **pp, struct thread **tdp, return (ESRCH); p = td->td_proc; break; + case CPU_WHICH_TIDPID: + if (id == -1) { + PROC_LOCK(curproc); + td = curthread; + p = curproc; + } else if (id > PID_MAX) { + td = tdfind(id, -1); + if (td == NULL) + return (ESRCH); + p = td->td_proc; + } else { + p = pfind(id); + if (p == NULL) + return (ESRCH); + } + break; case CPU_WHICH_CPUSET: if (id == -1) { thread_lock(curthread); @@ -1739,7 +1755,11 @@ cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which, if (IN_CAPABILITY_MODE(td)) { if (level != CPU_LEVEL_WHICH) return (ECAPMODE); - if (which != CPU_WHICH_TID && which != CPU_WHICH_PID) + if (which != CPU_WHICH_TID && which != CPU_WHICH_PID && + which != CPU_WHICH_TIDPID) + return (ECAPMODE); + if (id != -1 && which == CPU_WHICH_TIDPID && + id != td->td_tid && id != td->td_proc->p_pid) return (ECAPMODE); if (id != -1 && !(which == CPU_WHICH_TID && id == td->td_tid) && @@ -1986,6 +2006,19 @@ kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, thread_unlock(ttd); } break; + case CPU_WHICH_TIDPID: + if (id > PID_MAX || id == -1) { + thread_lock(ttd); + CPU_COPY(&ttd->td_cpuset->cs_mask, mask); + thread_unlock(ttd); + break; + } + FOREACH_THREAD_IN_PROC(p, ttd) { + thread_lock(ttd); + CPU_OR(mask, mask, &ttd->td_cpuset->cs_mask); + thread_unlock(ttd); + } + break; case CPU_WHICH_CPUSET: case CPU_WHICH_JAIL: CPU_COPY(&set->cs_mask, mask); @@ -2135,6 +2168,13 @@ kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, case CPU_WHICH_PID: error = cpuset_setproc(id, NULL, mask, NULL, false); break; + case CPU_WHICH_TIDPID: + if (id > PID_MAX || id == -1) + error = cpuset_setthread(id, mask); + else + error = cpuset_setproc(id, NULL, mask, NULL, + false); + break; case CPU_WHICH_CPUSET: case CPU_WHICH_JAIL: error = cpuset_which(which, id, &p, &ttd, &set); diff --git a/sys/sys/cpuset.h b/sys/sys/cpuset.h index f8fc36b99aa7..55e1ec5def0c 100644 --- a/sys/sys/cpuset.h +++ b/sys/sys/cpuset.h @@ -109,6 +109,7 @@ #define CPU_WHICH_DOMAIN 6 /* Specifies a NUMA domain id. */ #define CPU_WHICH_INTRHANDLER 7 /* Specifies an irq # (not ithread). */ #define CPU_WHICH_ITHREAD 8 /* Specifies an irq's ithread. */ +#define CPU_WHICH_TIDPID 9 /* Specifies a process or thread id. */ /* * Reserved cpuset identifiers.