Bug 260487 - sched_getaffinity(3)/sched_setaffinity(3): wrong handling of PID 0
Summary: sched_getaffinity(3)/sched_setaffinity(3): wrong handling of PID 0
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-12-17 05:21 UTC by sigsys
Modified: 2022-05-12 13:20 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sigsys 2021-12-17 05:21:04 UTC
sched_getaffinity(3)/sched_setaffinity(3) are documented to use PID 0 to refer to the current process, but the underlying cpuset_getaffinity(2)/cpuset_setaffinity(2) only do so with PID -1.

I noticed FFmpeg library code passing PID 0 to sched_getaffinity().

diff --git a/lib/libc/gen/sched_getaffinity.c b/lib/libc/gen/sched_getaffinity.c
index 5557d3d93b47..7fca53e39b4b 100644
--- a/lib/libc/gen/sched_getaffinity.c
+++ b/lib/libc/gen/sched_getaffinity.c
@@ -32,6 +32,6 @@
 int
 sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset)
 {
-	return (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
-	    cpusetsz, cpuset));
+	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 ad775b5dbce5..1c083b4b108a 100644
--- a/lib/libc/gen/sched_setaffinity.c
+++ b/lib/libc/gen/sched_setaffinity.c
@@ -32,6 +32,6 @@
 int
 sched_setaffinity(pid_t pid, size_t cpusetsz, const cpuset_t *cpuset)
 {
-	return (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
-	    cpusetsz, cpuset));
+	return (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
+	    pid == 0 ? -1 : pid, cpusetsz, cpuset));
 }
Comment 1 commit-hook freebsd_committer freebsd_triage 2021-12-17 05:35:57 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=caacda7a3e898a8b142d27732a9f13a525b08ea7

commit caacda7a3e898a8b142d27732a9f13a525b08ea7
Author:     Math Ieu <sigsys@gmail.com>
AuthorDate: 2021-12-17 05:31:09 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-12-17 05:31:09 +0000

    sched_get/setaffinity(3): pid 0 should designate current process

    while FreeBSD' native sched_get/setaffinity use pid -1 for this.

    PR:     260487
    MFC after:      1 week

 lib/libc/gen/sched_getaffinity.c | 4 ++--
 lib/libc/gen/sched_setaffinity.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2022-01-14 18:11:14 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=3f8542d3968e24222c66844833dfa5b8707dd807

commit 3f8542d3968e24222c66844833dfa5b8707dd807
Author:     Math Ieu <sigsys@gmail.com>
AuthorDate: 2021-12-17 05:31:09 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-01-14 16:17:30 +0000

    sched_get/setaffinity(3): pid 0 should designate current process

    PR:     260487

    (cherry picked from commit caacda7a3e898a8b142d27732a9f13a525b08ea7)

 lib/libc/gen/sched_getaffinity.c | 4 ++--
 lib/libc/gen/sched_setaffinity.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Comment 3 Bjoern A. Zeeb freebsd_committer freebsd_triage 2022-05-12 13:11:47 UTC
Can this be closed?