ktimers subsystem V2
Subsystems (8)
PATCH 1/1 ktimers subsystem V2
This is an updated version which contains following changes:
- per cpu data reworked
- possible deadlock of switching timer bases fixed
Thanks for review and feedback
tglx
ktimers seperate the "timer API" from the "timeout API". ktimers are used for:
- nanosleep
- posixtimers
- itimers
The patch contains the base implementation of ktimers and the conversion of nanosleep, posixtimers and itimers to ktimer users.
The patch does not require other changes to the Linux time(r) core system.
The implementation was done with following constraints in mind:
- Not bound to jiffies
- Multiple time sources
- Per CPU timer queues
- Simplification of absolute CLOCK_REALTIME posix timers
- High resolution timer aware
- Allows the timeout API to reschedule the next event (for tickless systems)
Ktimers enqueue the timers into a time sorted list, which is implemented with a rbtree, which is effiecient and already used in other performance critical parts of the kernel. This is a bit slower than the timer wheel, but due to the fact that the vast majority of timers is actually expiring it has to be waged versus the cascading penalty.
The code supports multiple time sources. Currently implemented are CLOCK_REALTIME and CLOCK_MONOTONIC. They provide seperate timer queues and support functions.
--- fs/exec.c | 9 fs/proc/array.c | 6 include/asm-generic/div64.h | 18 include/linux/ktimer.h | 142 +++++++ include/linux/posix-timers.h | 87 ++-- include/linux/sched.h | 4 include/linux/time.h | 65 ++- include/linux/timer.h | 2 init/main.c | 1 kernel/Makefile | 3 kernel/exit.c | 2 kernel/fork.c | 5 kernel/itimer.c | 83 +--- kernel/ktimers.c | 826 ++++++++++++++++++++++++++++++++++++++++++ kernel/posix-cpu-timers.c | 23 - kernel/posix-timers.c | 832 ++++++++----------------------------------- kernel/timer.c | 57 -- 17 files changed, 1308 insertions(+), 857 deletions(-)
fs/exec.c +5 -4
@@ -645,9 +645,10 @@ static inline int de_thread(struct task_* synchronize with any firing (by calling del_timer_sync) * before we can safely let the old group leader die. */ - sig->real_timer.data = (unsigned long )current; -if (del_timer_sync (&sig->real_timer)) -add_timer (&sig->real_timer); + sig->real_timer.data = current; +if (stop_ktimer (&sig->real_timer)) +start_ktimer (&sig->real_timer,NULL , +KTIMER_RESTART |KTIMER_NOCHECK ); }while (atomic_read (&sig->count) > count) { sig->group_exit_task = current;
@@ -659,7 +660,7 @@ static inline int de_thread(struct task_ } sig->group_exit_task =NULL ; sig->notify_count =0 ; - sig->real_timer.data = (unsigned long )current; + sig->real_timer.data = current;spin_unlock_irq (lock);/*
fs/proc/array.c +3 -3
@@ -330,7 +330,7 @@ static int do_task_stat(struct task_struunsigned long min_flt =0 , maj_flt =0 ;cputime_t cutime, cstime, utime, stime;unsigned long rsslim =0 ; -unsigned long it_real_value =0 ; +nsec_t it_real_value =0 ;struct task_struct *t;char tcomm[sizeof (task->comm)];
@@ -386,7 +386,7 @@ static int do_task_stat(struct task_stru utime =cputime_add (utime, task->signal->utime); stime =cputime_add (stime, task->signal->stime); } - it_real_value = task->signal->it_real_value; + it_real_value = task->signal->real_timer.expires; } ppid =pid_alive (task) ? task->group_leader->real_parent->tgid :0 ;read_unlock (&tasklist_lock);
@@ -435,7 +435,7 @@ static int do_task_stat(struct task_stru priority, nice, num_threads, -jiffies_to_clock_t (it_real_value), + (clock_t )nsec_to_clock_t (it_real_value), start_time, vsize, mm ? get_mm_counter(mm, rss) : 0, /* you might want to shift this left 3 */
include/asm-generic/div64.h +18 -0
@@ -30,6 +30,24 @@ __rem; \ }) +/* +* (long)X = ((long long)divs) / (long)div +* (long)rem = ((long long)divs) % (long)div +* +* Warning, this will do an exception if X overflows. +*/ +#define div_long_long_rem (a,b,c) div_ll_X_l_rem(a,b,c) + +/* x = divs / div; *rem = divs % div; */ +static inline unsigned long div_ll_X_l_rem (unsigned long long divs, +unsigned long div, +unsigned long * rem) +{ +unsigned long long it = divs; + *rem =do_div (it, div); +return (unsigned long )it; +} +#elif BITS_PER_LONG ==32 extern uint32_t __div64_32 (uint64_t *dividend,uint32_t divisor);
include/linux/ktimer.h new file +142 -0
142 lines not on this page · raw message
include/linux/posix-timers.h +58 -29
122 lines not on this page · raw message
include/linux/sched.h +2 -2
@@ -104,6 +104,7 @@ extern unsigned long nr_iowait(void);#include <linux/param.h> #include <linux/resource.h> #include <linux/timer.h> +#include <linux/ktimer.h> #include <asm/processor.h>
@@ -346,8 +347,7 @@ struct signal_struct {struct list_head posix_timers;/* ITIMER_REAL timer for the process */ -struct timer_list real_timer; -unsigned long it_real_value, it_real_incr; +struct ktimer real_timer;/* ITIMER_PROF and ITIMER_VIRTUAL timers for the process */ cputime_t it_prof_expires, it_virt_expires;
include/linux/time.h +54 -11
103 lines not on this page · raw message
include/linux/timer.h +1 -1
@@ -91,6 +91,6 @@ static inline void add_timer(struct timeextern void init_timers (void );extern void run_local_timers (void ); -extern void it_real_fn (unsigned long ); +extern void it_real_fn (void *);#endif
init/main.c +1 -0
@@ -485,6 +485,7 @@ asmlinkage void __init start_kernel(voidinit_IRQ ();pidhash_init ();init_timers (); +init_ktimers ();softirq_init ();time_init ();
kernel/Makefile +2 -1
@@ -7,7 +7,8 @@ obj-y = sched.o fork.o exec_domain.o sysctl.o capability.o ptrace.o timer.o user.o \ signal.o sys.o kmod.o workqueue.o pid.o \ rcupdate.o intermodule.o extable.o params.o posix-timers.o \ - kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o + kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o \ + ktimers.o obj-$(CONFIG_FUTEX) += futex.o obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
kernel/exit.c +1 -1
@@ -842,7 +842,7 @@ fastcall NORET_TYPE void do_exit(long coupdate_mem_hiwater (tsk); group_dead =atomic_dec_and_test (&tsk->signal->live);if (group_dead) { -del_timer_sync (&tsk->signal->real_timer); +stop_ktimer (&tsk->signal->real_timer);acct_process (code); }exit_mm (tsk);
kernel/fork.c +2 -3
@@ -804,10 +804,9 @@ static inline int copy_signal(unsigned linit_sigpending (&sig->shared_pending);INIT_LIST_HEAD (&sig->posix_timers); - sig->it_real_value = sig->it_real_incr =0 ; +init_ktimer_mono (&sig->real_timer); sig->real_timer.function = it_real_fn; - sig->real_timer.data = (unsigned long ) tsk; -init_timer (&sig->real_timer); + sig->real_timer.data = tsk; sig->it_virt_expires = cputime_zero; sig->it_virt_incr = cputime_zero;
kernel/itimer.c +22 -61
117 lines not on this page · raw message
kernel/ktimers.c new file +823 -0
823 lines not on this page · raw message
kernel/posix-cpu-timers.c +14 -9
@@ -1394,7 +1394,7 @@ void set_process_cpu_timer(struct task_sstatic long posix_cpu_clock_nanosleep_restart (struct restart_block *);int posix_cpu_nsleep (clockid_t which_clock,int flags, -struct timespec *rqtp) +struct timespec *rqtp,struct timespec __user *rmtp) {struct restart_block *restart_block = ¤t_thread_info ()->restart_block;
@@ -1419,7 +1419,6 @@ int posix_cpu_nsleep(clockid_t which_clo error =posix_cpu_timer_create (&timer); timer.it_process = current;if (!error) { -struct timespec __user *rmtp;static struct itimerspec zero_it;struct itimerspec it = { .it_value = *rqtp, .it_interval = {} };
@@ -1466,7 +1465,6 @@ int posix_cpu_nsleep(clockid_t which_clo/* * Report back to the user the time still remaining. */ - rmtp = (struct timespec __user *) restart_block->arg1;if (rmtp !=NULL && !(flags &TIMER_ABSTIME ) &©_to_user (rmtp, &it.it_value,sizeof *rmtp))return -EFAULT ;
@@ -1474,6 +1472,7 @@ int posix_cpu_nsleep(clockid_t which_clo restart_block->fn = posix_cpu_clock_nanosleep_restart;/* Caller already set restart_block->arg1 */ restart_block->arg0 = which_clock; + restart_block->arg1 = (unsigned long ) rmtp; restart_block->arg2 = rqtp->tv_sec; restart_block->arg3 = rqtp->tv_nsec;
@@ -1487,10 +1486,15 @@ static long posix_cpu_clock_nanosleep_restart(struct restart_block *restart_block) {clockid_t which_clock = restart_block->arg0; -struct timespec t = { .tv_sec = restart_block->arg2, - .tv_nsec = restart_block->arg3 }; +struct timespec __user *rmtp; +struct timespec t; + + rmtp = (struct timespec __user *) restart_block->arg1; + t.tv_sec = restart_block->arg2; + t.tv_nsec = restart_block->arg3; + restart_block->fn = do_no_restart_syscall; -return posix_cpu_nsleep (which_clock,TIMER_ABSTIME , &t); +return posix_cpu_nsleep (which_clock,TIMER_ABSTIME , &t, rmtp); }
@@ -1511,9 +1515,10 @@ static int process_cpu_timer_create(strureturn posix_cpu_timer_create (timer); }static int process_cpu_nsleep (clockid_t which_clock,int flags, -struct timespec *rqtp) +struct timespec *rqtp, +struct timespec __user *rmtp) { -return posix_cpu_nsleep (PROCESS_CLOCK , flags, rqtp); +return posix_cpu_nsleep (PROCESS_CLOCK , flags, rqtp, rmtp); }static int thread_cpu_clock_getres (clockid_t which_clock,struct timespec *tp) {
@@ -1529,7 +1534,7 @@ static int thread_cpu_timer_create(strucreturn posix_cpu_timer_create (timer); }static int thread_cpu_nsleep (clockid_t which_clock,int flags, -struct timespec *rqtp) +struct timespec *rqtp,struct timespec __user *rmtp) {return -EINVAL ; }
kernel/posix-timers.c +156 -676
1,031 lines not on this page · raw message
kernel/timer.c +1 -56
69 lines not on this page · raw message