From: Nathan Zadoks <nathan@nathan7.eu>
To: musl@lists.openwall.com
Cc: Nathan Zadoks <nathan@nathan7.eu>
Subject: [PATCH] add sched_getcpu, with vDSO support
Date: Tue, 1 Mar 2016 16:56:06 +0100 [thread overview]
Message-ID: <1456847766-23953-1-git-send-email-nathan@nathan7.eu> (raw)
In-Reply-To: <1456839940-10330-1-git-send-email-nathan@nathan7.eu>
This is a GNU extension, but a fairly minor one, for a system call that
otherwise has no libc wrapper.
Adding it was discussed previously, without any objections:
http://www.openwall.com/lists/musl/2015/05/08/24
---
Whoops, forgot to make getcpu_init a static function in the previous version.
Fixed!
---
arch/x86_64/syscall_arch.h | 2 ++
include/sched.h | 1 +
src/sched/sched_getcpu.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 48 insertions(+)
create mode 100644 src/sched/sched_getcpu.c
diff --git a/arch/x86_64/syscall_arch.h b/arch/x86_64/syscall_arch.h
index a7a7b5a..54e05ff 100644
--- a/arch/x86_64/syscall_arch.h
+++ b/arch/x86_64/syscall_arch.h
@@ -64,3 +64,5 @@ static __inline long __syscall6(long n, long a1, long a2, long a3, long a4, long
#define VDSO_USEFUL
#define VDSO_CGT_SYM "__vdso_clock_gettime"
#define VDSO_CGT_VER "LINUX_2.6"
+#define VDSO_GETCPU_SYM "__vdso_getcpu"
+#define VDSO_GETCPU_VER "LINUX_2.6"
diff --git a/include/sched.h b/include/sched.h
index 3e34a72..7e88f09 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -76,6 +76,7 @@ void free(void *);
typedef struct cpu_set_t { unsigned long __bits[128/sizeof(long)]; } cpu_set_t;
int __sched_cpucount(size_t, const cpu_set_t *);
+int sched_getcpu(void);
int sched_getaffinity(pid_t, size_t, cpu_set_t *);
int sched_setaffinity(pid_t, size_t, const cpu_set_t *);
diff --git a/src/sched/sched_getcpu.c b/src/sched/sched_getcpu.c
new file mode 100644
index 0000000..096f06f
--- /dev/null
+++ b/src/sched/sched_getcpu.c
@@ -0,0 +1,45 @@
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <errno.h>
+#include <sched.h>
+#include "syscall.h"
+#include "atomic.h"
+
+#ifdef VDSO_GETCPU_SYM
+
+void *__vdsosym(const char *, const char *);
+
+static void *volatile vdso_func;
+
+typedef long (*getcpu_f)(unsigned *, unsigned *, void *);
+
+static long getcpu_init(unsigned *cpu, unsigned *node, void *unused)
+{
+ void *p = __vdsosym(VDSO_GETCPU_VER, VDSO_GETCPU_SYM);
+ getcpu_f f = (getcpu_f)p;
+ a_cas_p(&vdso_func, (void *)getcpu_init, p);
+ return f ? f(cpu, node, unused) : -ENOSYS;
+}
+
+static void *volatile vdso_func = (void *)getcpu_init;
+
+#endif
+
+int sched_getcpu(void)
+{
+ int r;
+ unsigned cpu;
+
+#ifdef VDSO_CGT_SYM
+ getcpu_f f = (getcpu_f)vdso_func;
+ if (f) {
+ r = f(&cpu, NULL, NULL);
+ if (!r) return cpu;
+ if (r != -ENOSYS) return __syscall_ret(r);
+ }
+#endif
+
+ r = __syscall(SYS_getcpu, &cpu, NULL, NULL);
+ if (!r) return cpu;
+ return __syscall_ret(r);
+}
--
2.7.1
next prev parent reply other threads:[~2016-03-01 15:56 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-29 16:49 [PATCH] add sched_getcpu Nathan Zadoks
2016-02-29 16:57 ` Nathan Zadoks
2016-02-29 16:57 ` Nathan Zadoks
2016-02-29 17:00 ` Nathan Zadoks
2016-02-29 17:23 ` Alexander Monakov
2016-02-29 17:33 ` Alexander Monakov
2016-03-01 13:45 ` [PATCH] add sched_getcpu, with vDSO support Nathan Zadoks
2016-03-01 15:56 ` Nathan Zadoks [this message]
2016-03-02 5:55 ` Rich Felker
2016-03-02 16:26 ` [PATCH 0/2] add sched_getcpu, take n+1 Nathan Zadoks
2016-03-02 16:26 ` [PATCH 1/2] add sched_getcpu Nathan Zadoks
2016-03-02 16:26 ` [PATCH 2/2] add sched_getcpu vDSO support Nathan Zadoks
2016-03-03 3:01 ` [PATCH 0/2] add sched_getcpu, take n+1 Rich Felker
2016-02-29 17:49 ` [PATCH] add sched_getcpu nathan
2016-02-29 17:52 ` nathan
2016-02-29 20:17 ` Alexander Monakov
2016-02-29 20:49 ` Nathan Zadoks
2016-02-29 18:38 ` Rich Felker
2016-02-29 19:59 ` Alexander Monakov
2016-02-29 20:05 ` Rich Felker
2016-02-29 20:10 ` Alexander Monakov
2016-02-29 20:17 ` Rich Felker
2016-02-29 21:09 ` Tomasz Sterna
2016-02-29 21:21 ` Nathan Zadoks
2016-02-29 21:30 ` Rich Felker
2016-03-01 20:35 ` Tomasz Sterna
2016-03-01 22:34 ` Rich Felker
2016-03-02 20:46 ` Tomasz Sterna
2016-03-02 21:19 ` Szabolcs Nagy
2016-03-02 23:26 ` Rich Felker
2016-03-04 22:21 ` Tomasz Sterna
2016-03-04 23:33 ` Rich Felker
2016-03-05 11:40 ` Tomasz Sterna
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1456847766-23953-1-git-send-email-nathan@nathan7.eu \
--to=nathan@nathan7.eu \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).