From e68c619144bfd508f07095829385f914de962cc6 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Sat, 21 Nov 2020 21:43:36 +0000 Subject: [PATCH] Stop using eventhandlers for itimers subsystem exec and exit hooks. While there, do some minor cleanup for kclocks. They are only registered from kern_time.c, make registration function static. Remove event hooks, they are not used by both registered kclocks. Add some consts. Perhaps we can stop registering kclocks at all and statically initialize them. Reviewed by: mjg Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D27305 --- sys/kern/kern_exec.c | 2 ++ sys/kern/kern_exit.c | 3 ++ sys/kern/kern_time.c | 79 ++++++++++++++++++-------------------------- sys/sys/timers.h | 7 ++-- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index 366831c3f096..6a61ce17a989 100644 --- a/sys/kern/kern_exec.c +++ b/sys/kern/kern_exec.c @@ -67,6 +67,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -1049,6 +1050,7 @@ exec_new_vmspace(struct image_params *imgp, struct sysentvec *sv) sigfastblock_clear(td); umtx_exec(p); + itimers_exec(p); EVENTHANDLER_DIRECT_INVOKE(process_exec, p, imgp); diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index dcd0b4ddea32..9d70d95a5522 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -72,6 +72,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #ifdef KTRACE #include @@ -324,6 +325,8 @@ exit1(struct thread *td, int rval, int signo) mtx_unlock(&ppeers_lock); } + itimers_exit(p); + /* * Check if any loadable modules need anything done at process exit. * E.g. SYSV IPC stuff. diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c index aa5ed4b708ec..6222bdc39bee 100644 --- a/sys/kern/kern_time.c +++ b/sys/kern/kern_time.c @@ -43,7 +43,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include @@ -99,8 +98,6 @@ static void itimer_enter(struct itimer *); static void itimer_leave(struct itimer *); static struct itimer *itimer_find(struct proc *, int); static void itimers_alloc(struct proc *); -static void itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp); -static void itimers_event_hook_exit(void *arg, struct proc *p); static int realtimer_create(struct itimer *); static int realtimer_gettime(struct itimer *, struct itimerspec *); static int realtimer_settime(struct itimer *, int, @@ -109,7 +106,7 @@ static int realtimer_delete(struct itimer *); static void realtimer_clocktime(clockid_t, struct timespec *); static void realtimer_expire(void *); -int register_posix_clock(int, struct kclock *); +static int register_posix_clock(int, const struct kclock *); void itimer_fire(struct itimer *it); int itimespecfix(struct timespec *ts); @@ -1086,12 +1083,11 @@ ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) static void itimer_start(void) { - struct kclock rt_clock = { + static const struct kclock rt_clock = { .timer_create = realtimer_create, .timer_delete = realtimer_delete, .timer_settime = realtimer_settime, .timer_gettime = realtimer_gettime, - .event_hook = NULL }; itimer_zone = uma_zcreate("itimer", sizeof(struct itimer), @@ -1101,14 +1097,10 @@ itimer_start(void) p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L); p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX); p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX); - EVENTHANDLER_REGISTER(process_exit, itimers_event_hook_exit, - (void *)ITIMER_EV_EXIT, EVENTHANDLER_PRI_ANY); - EVENTHANDLER_REGISTER(process_exec, itimers_event_hook_exec, - (void *)ITIMER_EV_EXEC, EVENTHANDLER_PRI_ANY); } -int -register_posix_clock(int clockid, struct kclock *clk) +static int +register_posix_clock(int clockid, const struct kclock *clk) { if ((unsigned)clockid >= MAX_CLOCKS) { printf("%s: invalid clockid\n", __func__); @@ -1714,46 +1706,41 @@ itimers_alloc(struct proc *p) } } -static void -itimers_event_hook_exec(void *arg, struct proc *p, struct image_params *imgp __unused) -{ - itimers_event_hook_exit(arg, p); -} - /* Clean up timers when some process events are being triggered. */ static void -itimers_event_hook_exit(void *arg, struct proc *p) +itimers_event_exit_exec(int start_idx, struct proc *p) { struct itimers *its; struct itimer *it; - int event = (int)(intptr_t)arg; int i; - if (p->p_itimers != NULL) { - its = p->p_itimers; - for (i = 0; i < MAX_CLOCKS; ++i) { - if (posix_clocks[i].event_hook != NULL) - CLOCK_CALL(i, event_hook, (p, i, event)); - } - /* - * According to susv3, XSI interval timers should be inherited - * by new image. - */ - if (event == ITIMER_EV_EXEC) - i = 3; - else if (event == ITIMER_EV_EXIT) - i = 0; - else - panic("unhandled event"); - for (; i < TIMER_MAX; ++i) { - if ((it = its->its_timers[i]) != NULL) - kern_ktimer_delete(curthread, i); - } - if (its->its_timers[0] == NULL && - its->its_timers[1] == NULL && - its->its_timers[2] == NULL) { - free(its, M_SUBPROC); - p->p_itimers = NULL; - } + its = p->p_itimers; + if (its == NULL) + return; + + for (i = start_idx; i < TIMER_MAX; ++i) { + if ((it = its->its_timers[i]) != NULL) + kern_ktimer_delete(curthread, i); + } + if (its->its_timers[0] == NULL && its->its_timers[1] == NULL && + its->its_timers[2] == NULL) { + free(its, M_SUBPROC); + p->p_itimers = NULL; } } + +void +itimers_exec(struct proc *p) +{ + /* + * According to susv3, XSI interval timers should be inherited + * by new image. + */ + itimers_event_exit_exec(3, p); +} + +void +itimers_exit(struct proc *p) +{ + itimers_event_exit_exec(0, p); +} diff --git a/sys/sys/timers.h b/sys/sys/timers.h index 01956bd3caaa..aa1912149452 100644 --- a/sys/sys/timers.h +++ b/sys/sys/timers.h @@ -107,13 +107,10 @@ struct kclock { int (*timer_delete)(struct itimer * timer); int (*timer_gettime)(struct itimer * timer, struct itimerspec * cur_value); - void (*event_hook)(struct proc *p, clockid_t clock_id, int event); }; -/* Event values for event_hook() */ -#define ITIMER_EV_EXEC 0 -#define ITIMER_EV_EXIT 1 - +void itimers_exec(struct proc *p); +void itimers_exit(struct proc *p); int itimer_accept(struct proc *p, int tid, ksiginfo_t *ksi); #endif #endif /* !_SYS_TIMERS_H_ */