|
Message-ID: <20160302055542.GN9349@brightrain.aerifal.cx> Date: Wed, 2 Mar 2016 00:55:42 -0500 From: Rich Felker <dalias@...c.org> To: musl@...ts.openwall.com Subject: Re: [PATCH] add sched_getcpu, with vDSO support On Tue, Mar 01, 2016 at 04:56:06PM +0100, Nathan Zadoks wrote: > 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 Wrong macro in the #ifdef. > + getcpu_f f = (getcpu_f)vdso_func; > + if (f) { > + r = f(&cpu, NULL, NULL); Not a big deal, but usually in musl we use 0 rather than the NULL macro. Actually I wondered if the function actually needs to take the useless extra 2 arguments, but I think for the sake of correctness it's best to do it this way -- the callee in the vdso has 3 args, so it should be called with the correct type. > + 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); > +} Also tabs should be used for indention, not spaces. One other thing I thought might be nice is initially committing the trivial syscall-only version that adds the public prototype, then doing vdso support as a separate commit, but if that's a pain to do don't worry about it. Rich
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.