git: a48d9f1900d4 - stable/13 - Add sched_getcpu()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Fri, 14 Jan 2022 18:10:19 UTC
The branch stable/13 has been updated by kib:

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

commit a48d9f1900d48eb7222ade05df10df73097b8cca
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-10-22 15:35:53 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-01-14 16:17:29 +0000

    Add sched_getcpu()
    
    (cherry picked from commit 77b2c2f81451db8119e4ea6398fe76813db790de)
---
 include/sched.h                      |  1 +
 lib/libc/aarch64/sys/Makefile.inc    |  3 ++-
 lib/libc/arm/sys/Makefile.inc        |  3 ++-
 lib/libc/gen/Symbol.map              |  1 +
 lib/libc/gen/sched_getcpu_gen.c      | 36 ++++++++++++++++++++++++++++++++++++
 lib/libc/include/libc_private.h      |  1 +
 lib/libc/mips/sys/Makefile.inc       |  3 ++-
 lib/libc/powerpc/sys/Makefile.inc    |  4 +++-
 lib/libc/powerpc64/sys/Makefile.inc  |  4 +++-
 lib/libc/riscv/sys/Makefile.inc      |  3 ++-
 lib/libc/sys/Makefile.inc            |  3 +++
 lib/libc/x86/sys/Makefile.inc        |  3 ++-
 sys/compat/freebsd32/syscalls.master |  2 +-
 sys/kern/capabilities.conf           |  1 +
 sys/kern/kern_synch.c                |  7 +++++++
 sys/kern/syscalls.master             |  4 +++-
 16 files changed, 70 insertions(+), 9 deletions(-)

diff --git a/include/sched.h b/include/sched.h
index 531ecb1bb9a3..1c41cda79046 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -47,6 +47,7 @@ __BEGIN_DECLS
 #if __BSD_VISIBLE
 int sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset);
 int sched_setaffinity(int pid, size_t cpusetsz, const cpuset_t *cpuset);
+int sched_getcpu(void);
 #endif /* __BSD_VISIBLE */
 __END_DECLS
 
diff --git a/lib/libc/aarch64/sys/Makefile.inc b/lib/libc/aarch64/sys/Makefile.inc
index 623a06b7faf8..97aff8ba92fe 100644
--- a/lib/libc/aarch64/sys/Makefile.inc
+++ b/lib/libc/aarch64/sys/Makefile.inc
@@ -2,7 +2,8 @@
 
 MIASM:=	${MIASM:Nfreebsd[467]_*}
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
 
 MDASM=	cerror.S \
 	syscall.S \
diff --git a/lib/libc/arm/sys/Makefile.inc b/lib/libc/arm/sys/Makefile.inc
index be6a58f8aaf5..c6e854e7954a 100644
--- a/lib/libc/arm/sys/Makefile.inc
+++ b/lib/libc/arm/sys/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
 
 MDASM= Ovfork.S cerror.S syscall.S
 
diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map
index a52131b34476..5554c32ab4f2 100644
--- a/lib/libc/gen/Symbol.map
+++ b/lib/libc/gen/Symbol.map
@@ -441,6 +441,7 @@ FBSD_1.7 {
 	 posix_spawn_file_actions_addfchdir_np;
 	 sched_getaffinity;
 	 sched_setaffinity;
+	 sched_getcpu;
 };
 
 FBSDprivate_1.0 {
diff --git a/lib/libc/gen/sched_getcpu_gen.c b/lib/libc/gen/sched_getcpu_gen.c
new file mode 100644
index 000000000000..ef1bebc2915a
--- /dev/null
+++ b/lib/libc/gen/sched_getcpu_gen.c
@@ -0,0 +1,36 @@
+/*-
+ * Copyright (c) 2021 The FreeBSD Foundation
+ *
+ * This software were developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sched.h>
+#include "libc_private.h"
+
+int
+sched_getcpu(void)
+{
+	return (__sys_sched_getcpu());
+}
diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h
index 5a524c0211d1..61c1eb438142 100644
--- a/lib/libc/include/libc_private.h
+++ b/lib/libc/include/libc_private.h
@@ -371,6 +371,7 @@ __ssize_t	__sys_recv(int, void *, __size_t, int);
 __ssize_t	__sys_recvfrom(int, void *, __size_t, int, struct sockaddr *,
 		    __socklen_t *);
 __ssize_t	__sys_recvmsg(int, struct msghdr *, int);
+int		__sys_sched_getcpu(void);
 int		__sys_select(int, struct fd_set *, struct fd_set *,
 		    struct fd_set *, struct timeval *);
 __ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
diff --git a/lib/libc/mips/sys/Makefile.inc b/lib/libc/mips/sys/Makefile.inc
index d7cc70d01b07..fa6d9b32739d 100644
--- a/lib/libc/mips/sys/Makefile.inc
+++ b/lib/libc/mips/sys/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
-SRCS+=	trivial-vdso_tc.c
+SRCS+=	trivial-vdso_tc.c \
+	sched_getcpu_gen.c
 
 MDASM=  Ovfork.S cerror.S syscall.S
 
diff --git a/lib/libc/powerpc/sys/Makefile.inc b/lib/libc/powerpc/sys/Makefile.inc
index 99a141b5cb1e..6f07795b8870 100644
--- a/lib/libc/powerpc/sys/Makefile.inc
+++ b/lib/libc/powerpc/sys/Makefile.inc
@@ -1,4 +1,6 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
+
 MDASM+=	cerror.S
diff --git a/lib/libc/powerpc64/sys/Makefile.inc b/lib/libc/powerpc64/sys/Makefile.inc
index 99a141b5cb1e..6f07795b8870 100644
--- a/lib/libc/powerpc64/sys/Makefile.inc
+++ b/lib/libc/powerpc64/sys/Makefile.inc
@@ -1,4 +1,6 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
+
 MDASM+=	cerror.S
diff --git a/lib/libc/riscv/sys/Makefile.inc b/lib/libc/riscv/sys/Makefile.inc
index b322cece382c..2eb12bf11cad 100644
--- a/lib/libc/riscv/sys/Makefile.inc
+++ b/lib/libc/riscv/sys/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
 
 MDASM=	cerror.S \
 	syscall.S \
diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc
index 33e87e513b05..5c30f7d6b796 100644
--- a/lib/libc/sys/Makefile.inc
+++ b/lib/libc/sys/Makefile.inc
@@ -36,6 +36,9 @@ SRCS+=	\
 
 SRCS+= getdents.c lstat.c mknod.c stat.c
 
+NOASM+=  sched_getcpu.o
+PSEUDO+= _sched_getcpu.o
+
 SRCS+= fstat.c fstatat.c fstatfs.c getfsstat.c statfs.c
 NOASM+= fstat.o fstatat.o fstatfs.o getfsstat.o statfs.o
 PSEUDO+= _fstat.o _fstatat.o _fstatfs.o _getfsstat.o _statfs.o
diff --git a/lib/libc/x86/sys/Makefile.inc b/lib/libc/x86/sys/Makefile.inc
index 41e0d1a8ad66..8aa4948a3295 100644
--- a/lib/libc/x86/sys/Makefile.inc
+++ b/lib/libc/x86/sys/Makefile.inc
@@ -4,7 +4,8 @@
 
 SRCS+= \
 	__vdso_gettc.c \
-	pkru.c
+	pkru.c \
+	sched_getcpu_gen.c
 
 MAN+=	\
 	pkru.3
diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master
index 1a356bf2832e..e5b86c1f066f 100644
--- a/sys/compat/freebsd32/syscalls.master
+++ b/sys/compat/freebsd32/syscalls.master
@@ -1177,6 +1177,6 @@
 579	AUE_AIO_READV	STD	{ int freebsd32_aio_readv( \
 				    struct aiocb32 *aiocbp); }
 580	AUE_NULL	UNIMPL	fspacectl
-581	AUE_NULL	UNIMPL	sched_getcpu
+581	AUE_NULL	NOPROTO	{ int sched_getcpu(void); }
 582	AUE_NULL	UNIMPL	swapoff
 ; vim: syntax=off
diff --git a/sys/kern/capabilities.conf b/sys/kern/capabilities.conf
index 602ec7088fc6..7f73ea40b9f7 100644
--- a/sys/kern/capabilities.conf
+++ b/sys/kern/capabilities.conf
@@ -562,6 +562,7 @@ sbrk
 ##
 sched_get_priority_max
 sched_get_priority_min
+sched_getcpu
 
 ##
 ## Allow various thread/process scheduler operations.
diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c
index 46d14a35d1ea..88f47ba78601 100644
--- a/sys/kern/kern_synch.c
+++ b/sys/kern/kern_synch.c
@@ -685,3 +685,10 @@ sys_yield(struct thread *td, struct yield_args *uap)
 	td->td_retval[0] = 0;
 	return (0);
 }
+
+int
+sys_sched_getcpu(struct thread *td, struct sched_getcpu_args *uap)
+{
+	td->td_retval[0] = td->td_oncpu;
+	return (0);
+}
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index 141da7eb7fc5..72e3038ea352 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -3274,7 +3274,9 @@
 		);
 	}
 580	AUE_NULL	UNIMPL	fspacectl
-581	AUE_NULL	UNIMPL	sched_getcpu
+581	AUE_NULL	STD|CAPENABLED {
+		int sched_getcpu(void);
+	}
 582	AUE_SWAPOFF	STD {
 		int swapoff(
 		    _In_z_ const char *name,