📄 posix-timers.c
字号:
/* * linux/kernel/posix-timers.c * * * 2002-10-15 Posix Clocks & timers * by George Anzinger george@mvista.com * * Copyright (C) 2002 2003 by MontaVista Software. * * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug. * Copyright (C) 2004 Boris Hu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA *//* These are all the functions necessary to implement * POSIX clocks & timers */#include <linux/mm.h>#include <linux/interrupt.h>#include <linux/slab.h>#include <linux/time.h>#include <linux/mutex.h>#include <asm/uaccess.h>#include <asm/semaphore.h>#include <linux/list.h>#include <linux/init.h>#include <linux/compiler.h>#include <linux/idr.h>#include <linux/posix-timers.h>#include <linux/syscalls.h>#include <linux/wait.h>#include <linux/workqueue.h>#include <linux/module.h>/* * Management arrays for POSIX timers. Timers are kept in slab memory * Timer ids are allocated by an external routine that keeps track of the * id and the timer. The external interface is: * * void *idr_find(struct idr *idp, int id); to find timer_id <id> * int idr_get_new(struct idr *idp, void *ptr); to get a new id and * related it to <ptr> * void idr_remove(struct idr *idp, int id); to release <id> * void idr_init(struct idr *idp); to initialize <idp> * which we supply. * The idr_get_new *may* call slab for more memory so it must not be * called under a spin lock. Likewise idr_remore may release memory * (but it may be ok to do this under a lock...). * idr_find is just a memory look up and is quite fast. A -1 return * indicates that the requested id does not exist. *//* * Lets keep our timers in a slab cache :-) */static struct kmem_cache *posix_timers_cache;static struct idr posix_timers_id;static DEFINE_SPINLOCK(idr_lock);/* * we assume that the new SIGEV_THREAD_ID shares no bits with the other * SIGEV values. Here we put out an error if this assumption fails. */#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \ ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"#endif/* * The timer ID is turned into a timer address by idr_find(). * Verifying a valid ID consists of: * * a) checking that idr_find() returns other than -1. * b) checking that the timer id matches the one in the timer itself. * c) that the timer owner is in the callers thread group. *//* * CLOCKs: The POSIX standard calls for a couple of clocks and allows us * to implement others. This structure defines the various * clocks and allows the possibility of adding others. We * provide an interface to add clocks to the table and expect * the "arch" code to add at least one clock that is high * resolution. Here we define the standard CLOCK_REALTIME as a * 1/HZ resolution clock. * * RESOLUTION: Clock resolution is used to round up timer and interval * times, NOT to report clock times, which are reported with as * much resolution as the system can muster. In some cases this * resolution may depend on the underlying clock hardware and * may not be quantifiable until run time, and only then is the * necessary code is written. The standard says we should say * something about this issue in the documentation... * * FUNCTIONS: The CLOCKs structure defines possible functions to handle * various clock functions. For clocks that use the standard * system timer code these entries should be NULL. This will * allow dispatch without the overhead of indirect function * calls. CLOCKS that depend on other sources (e.g. WWV or GPS) * must supply functions here, even if the function just returns * ENOSYS. The standard POSIX timer management code assumes the * following: 1.) The k_itimer struct (sched.h) is used for the * timer. 2.) The list, it_lock, it_clock, it_id and it_process * fields are not modified by timer code. * * At this time all functions EXCEPT clock_nanosleep can be * redirected by the CLOCKS structure. Clock_nanosleep is in * there, but the code ignores it. * * Permissions: It is assumed that the clock_settime() function defined * for each clock will take care of permission checks. Some * clocks may be set able by any user (i.e. local process * clocks) others not. Currently the only set able clock we * have is CLOCK_REALTIME and its high res counter part, both of * which we beg off on and pass to do_sys_settimeofday(). */static struct k_clock posix_clocks[MAX_CLOCKS];/* * These ones are defined below. */static int common_nsleep(const clockid_t, int flags, struct timespec *t, struct timespec __user *rmtp);static void common_timer_get(struct k_itimer *, struct itimerspec *);static int common_timer_set(struct k_itimer *, int, struct itimerspec *, struct itimerspec *);static int common_timer_del(struct k_itimer *timer);static enum hrtimer_restart posix_timer_fn(struct hrtimer *data);static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);static inline void unlock_timer(struct k_itimer *timr, unsigned long flags){ spin_unlock_irqrestore(&timr->it_lock, flags);}/* * Call the k_clock hook function if non-null, or the default function. */#define CLOCK_DISPATCH(clock, call, arglist) \ ((clock) < 0 ? posix_cpu_##call arglist : \ (posix_clocks[clock].call != NULL \ ? (*posix_clocks[clock].call) arglist : common_##call arglist))/* * Default clock hook functions when the struct k_clock passed * to register_posix_clock leaves a function pointer null. * * The function common_CALL is the default implementation for * the function pointer CALL in struct k_clock. */static inline int common_clock_getres(const clockid_t which_clock, struct timespec *tp){ tp->tv_sec = 0; tp->tv_nsec = posix_clocks[which_clock].res; return 0;}/* * Get real time for posix timers */static int common_clock_get(clockid_t which_clock, struct timespec *tp){ ktime_get_real_ts(tp); return 0;}static inline int common_clock_set(const clockid_t which_clock, struct timespec *tp){ return do_sys_settimeofday(tp, NULL);}static int common_timer_create(struct k_itimer *new_timer){ hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0); return 0;}/* * Return nonzero if we know a priori this clockid_t value is bogus. */static inline int invalid_clockid(const clockid_t which_clock){ if (which_clock < 0) /* CPU clock, posix_cpu_* will check it */ return 0; if ((unsigned) which_clock >= MAX_CLOCKS) return 1; if (posix_clocks[which_clock].clock_getres != NULL) return 0; if (posix_clocks[which_clock].res != 0) return 0; return 1;}/* * Get monotonic time for posix timers */static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp){ ktime_get_ts(tp); return 0;}/* * Initialize everything, well, just everything in Posix clocks/timers ;) */static __init int init_posix_timers(void){ struct k_clock clock_realtime = { .clock_getres = hrtimer_get_res, }; struct k_clock clock_monotonic = { .clock_getres = hrtimer_get_res, .clock_get = posix_ktime_get_ts, .clock_set = do_posix_clock_nosettime, }; register_posix_clock(CLOCK_REALTIME, &clock_realtime); register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic); posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof (struct k_itimer), 0, 0, NULL, NULL); idr_init(&posix_timers_id); return 0;}__initcall(init_posix_timers);static void schedule_next_timer(struct k_itimer *timr){ struct hrtimer *timer = &timr->it.real.timer; if (timr->it.real.interval.tv64 == 0) return; timr->it_overrun += hrtimer_forward(timer, timer->base->get_time(), timr->it.real.interval); timr->it_overrun_last = timr->it_overrun; timr->it_overrun = -1; ++timr->it_requeue_pending; hrtimer_restart(timer);}/* * This function is exported for use by the signal deliver code. It is * called just prior to the info block being released and passes that * block to us. It's function is to update the overrun entry AND to * restart the timer. It should only be called if the timer is to be * restarted (i.e. we have flagged this in the sys_private entry of the * info block). * * To protect aginst the timer going away while the interrupt is queued, * we require that the it_requeue_pending flag be set. */void do_schedule_next_timer(struct siginfo *info){ struct k_itimer *timr; unsigned long flags; timr = lock_timer(info->si_tid, &flags); if (timr && timr->it_requeue_pending == info->si_sys_private) { if (timr->it_clock < 0) posix_cpu_timer_schedule(timr); else schedule_next_timer(timr); info->si_overrun = timr->it_overrun_last; } if (timr) unlock_timer(timr, flags);}int posix_timer_event(struct k_itimer *timr,int si_private){ memset(&timr->sigq->info, 0, sizeof(siginfo_t)); timr->sigq->info.si_sys_private = si_private; /* Send signal to the process that owns this timer.*/ timr->sigq->info.si_signo = timr->it_sigev_signo; timr->sigq->info.si_errno = 0; timr->sigq->info.si_code = SI_TIMER; timr->sigq->info.si_tid = timr->it_id; timr->sigq->info.si_value = timr->it_sigev_value; if (timr->it_sigev_notify & SIGEV_THREAD_ID) { struct task_struct *leader; int ret = send_sigqueue(timr->it_sigev_signo, timr->sigq, timr->it_process); if (likely(ret >= 0)) return ret; timr->it_sigev_notify = SIGEV_SIGNAL; leader = timr->it_process->group_leader; put_task_struct(timr->it_process); timr->it_process = leader; } return send_group_sigqueue(timr->it_sigev_signo, timr->sigq, timr->it_process);}EXPORT_SYMBOL_GPL(posix_timer_event);/* * This function gets called when a POSIX.1b interval timer expires. It * is used as a callback from the kernel internal timer. The * run_timer_list code ALWAYS calls with interrupts on. * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers. */static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer){ struct k_itimer *timr; unsigned long flags; int si_private = 0; enum hrtimer_restart ret = HRTIMER_NORESTART; timr = container_of(timer, struct k_itimer, it.real.timer); spin_lock_irqsave(&timr->it_lock, flags); if (timr->it.real.interval.tv64 != 0) si_private = ++timr->it_requeue_pending; if (posix_timer_event(timr, si_private)) { /* * signal was not sent because of sig_ignor * we will not get a call back to restart it AND * it should be restarted. */ if (timr->it.real.interval.tv64 != 0) { ktime_t now = hrtimer_cb_get_time(timer); /* * FIXME: What we really want, is to stop this * timer completely and restart it in case the * SIG_IGN is removed. This is a non trivial * change which involves sighand locking * (sigh !), which we don't want to do late in * the release cycle. * * For now we just let timers with an interval * less than a jiffie expire every jiffie to * avoid softirq starvation in case of SIG_IGN * and a very small interval, which would put * the timer right back on the softirq pending * list. By moving now ahead of time we trick * hrtimer_forward() to expire the timer * later, while we still maintain the overrun * accuracy, but have some inconsistency in * the timer_gettime() case. This is at least * better than a starved softirq. A more * complex fix which solves also another related * inconsistency is already in the pipeline. */#ifdef CONFIG_HIGH_RES_TIMERS { ktime_t kj = ktime_set(0, NSEC_PER_SEC / HZ); if (timr->it.real.interval.tv64 < kj.tv64) now = ktime_add(now, kj); }#endif timr->it_overrun += hrtimer_forward(timer, now, timr->it.real.interval); ret = HRTIMER_RESTART; ++timr->it_requeue_pending; } } unlock_timer(timr, flags); return ret;}static struct task_struct * good_sigevent(sigevent_t * event){ struct task_struct *rtn = current->group_leader; if ((event->sigev_notify & SIGEV_THREAD_ID ) && (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) || rtn->tgid != current->tgid || (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL)) return NULL; if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) && ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX))) return NULL; return rtn;}void register_posix_clock(const clockid_t clock_id, struct k_clock *new_clock){ if ((unsigned) clock_id >= MAX_CLOCKS) { printk("POSIX clock register failed for clock_id %d\n", clock_id); return; } posix_clocks[clock_id] = *new_clock;}EXPORT_SYMBOL_GPL(register_posix_clock);static struct k_itimer * alloc_posix_timer(void){ struct k_itimer *tmr; tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL); if (!tmr) return tmr; if (unlikely(!(tmr->sigq = sigqueue_alloc()))) { kmem_cache_free(posix_timers_cache, tmr); tmr = NULL; } return tmr;}#define IT_ID_SET 1#define IT_ID_NOT_SET 0static void release_posix_timer(struct k_itimer *tmr, int it_id_set){ if (it_id_set) { unsigned long flags; spin_lock_irqsave(&idr_lock, flags); idr_remove(&posix_timers_id, tmr->it_id); spin_unlock_irqrestore(&idr_lock, flags); } sigqueue_free(tmr->sigq); if (unlikely(tmr->it_process) && tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID)) put_task_struct(tmr->it_process); kmem_cache_free(posix_timers_cache, tmr);}/* Create a POSIX.1b interval timer. */asmlinkage longsys_timer_create(const clockid_t which_clock, struct sigevent __user *timer_event_spec, timer_t __user * created_timer_id){ int error = 0; struct k_itimer *new_timer = NULL; int new_timer_id; struct task_struct *process = NULL; unsigned long flags; sigevent_t event; int it_id_set = IT_ID_NOT_SET; if (invalid_clockid(which_clock)) return -EINVAL; new_timer = alloc_posix_timer(); if (unlikely(!new_timer)) return -EAGAIN; spin_lock_init(&new_timer->it_lock); retry: if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) { error = -EAGAIN; goto out; } spin_lock_irq(&idr_lock); error = idr_get_new(&posix_timers_id, (void *) new_timer, &new_timer_id); spin_unlock_irq(&idr_lock); if (error == -EAGAIN) goto retry; else if (error) { /* * Wierd looking, but we return EAGAIN if the IDR is * full (proper POSIX return value for this) */ error = -EAGAIN; goto out; } it_id_set = IT_ID_SET; new_timer->it_id = (timer_t) new_timer_id; new_timer->it_clock = which_clock; new_timer->it_overrun = -1; error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer)); if (error) goto out; /* * return the timer_id now. The next step is hard to * back out if there is an error. */ if (copy_to_user(created_timer_id,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -