1850 lines · 49.4 KB · C
RawDownload
		    !tbase_get_deferrable(timer->base))			base->next_timer = base->timer_jiffies;		ret = 1;	}out:	spin_unlock_irqrestore(&base->lock, flags); 	return ret;}EXPORT_SYMBOL(try_to_del_timer_sync); #ifdef CONFIG_SMP/** * del_timer_sync - deactivate a timer and wait for the handler to finish. * @timer: the timer to be deactivated * * This function only differs from del_timer() on SMP: besides deactivating * the timer it also makes sure the handler has finished executing on other * CPUs. * * Synchronization rules: Callers must prevent restarting of the timer, * otherwise this function is meaningless. It must not be called from * interrupt contexts. The caller must not hold locks which would prevent * completion of the timer's handler. The timer's handler must not call * add_timer_on(). Upon exit the timer is not queued and the handler is * not running on any CPU. * * Note: You must not hold locks that are held in interrupt context *   while calling this function. Even if the lock has nothing to do *   with the timer in question.  Here's why: * *    CPU0                             CPU1 *    ----                             ---- *                                   <SOFTIRQ> *                                   call_timer_fn(); *                                     base->running_timer = mytimer; *  spin_lock_irq(somelock); *                                     <IRQ> *                                        spin_lock(somelock); *  del_timer_sync(mytimer); *   while (base->running_timer == mytimer); * * Now del_timer_sync() will never return and never release somelock. * The interrupt on the other CPU is waiting to grab somelock but * it has interrupted the softirq that CPU0 is waiting to finish. * * The function returns whether it has deactivated a pending timer or not. */int del_timer_sync(struct timer_list *timer){#ifdef CONFIG_LOCKDEP	unsigned long flags; 	/*	 * If lockdep gives a backtrace here, please reference	 * the synchronization rules above.	 */	local_irq_save(flags);	lock_map_acquire(&timer->lockdep_map);	lock_map_release(&timer->lockdep_map);	local_irq_restore(flags);#endif	/*	 * don't use it in hardirq context, because it	 * could lead to deadlock.	 */	WARN_ON(in_irq());	for (;;) {		int ret = try_to_del_timer_sync(timer);		if (ret >= 0)			return ret;		cpu_relax();	}}EXPORT_SYMBOL(del_timer_sync);#endif static int cascade(struct tvec_base *base, struct tvec *tv, int index){	/* cascade all the timers from tv up one level */	struct timer_list *timer, *tmp;	struct list_head tv_list; 	list_replace_init(tv->vec + index, &tv_list); 	/*	 * We are removing _all_ timers from the list, so we	 * don't have to detach them individually.	 */	list_for_each_entry_safe(timer, tmp, &tv_list, entry) {		BUG_ON(tbase_get_base(timer->base) != base);		internal_add_timer(base, timer);	} 	return index;} static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),			  unsigned long data){	int preempt_count = preempt_count(); #ifdef CONFIG_LOCKDEP	/*	 * It is permissible to free the timer from inside the	 * function that is called from it, this we need to take into	 * account for lockdep too. To avoid bogus "held lock freed"	 * warnings as well as problems when looking into	 * timer->lockdep_map, make a copy and use that here.	 */	struct lockdep_map lockdep_map; 	lockdep_copy_map(&lockdep_map, &timer->lockdep_map);#endif	/*	 * Couple the lock chain with the lock chain at	 * del_timer_sync() by acquiring the lock_map around the fn()	 * call here and in del_timer_sync().	 */	lock_map_acquire(&lockdep_map); 	trace_timer_expire_entry(timer);	fn(data);	trace_timer_expire_exit(timer); 	lock_map_release(&lockdep_map); 	if (preempt_count != preempt_count()) {		WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",			  fn, preempt_count, preempt_count());		/*		 * Restore the preempt count. That gives us a decent		 * chance to survive and extract information. If the		 * callback kept a lock held, bad luck, but not worse		 * than the BUG() we had.		 */		preempt_count() = preempt_count;	}} #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) /** * __run_timers - run all expired timers (if any) on this CPU. * @base: the timer vector to be processed. * * This function cascades all vectors and executes all expired timer * vectors. */static inline void __run_timers(struct tvec_base *base){	struct timer_list *timer; 	spin_lock_irq(&base->lock);	while (time_after_eq(jiffies, base->timer_jiffies)) {		struct list_head work_list;		struct list_head *head = &work_list;		int index = base->timer_jiffies & TVR_MASK; 		/*		 * Cascade timers:		 */		if (!index &&			(!cascade(base, &base->tv2, INDEX(0))) &&				(!cascade(base, &base->tv3, INDEX(1))) &&					!cascade(base, &base->tv4, INDEX(2)))			cascade(base, &base->tv5, INDEX(3));		++base->timer_jiffies;		list_replace_init(base->tv1.vec + index, &work_list);		while (!list_empty(head)) {			void (*fn)(unsigned long);			unsigned long data; 			timer = list_first_entry(head, struct timer_list,entry);			fn = timer->function;			data = timer->data; 			timer_stats_account_timer(timer); 			base->running_timer = timer;			detach_timer(timer, 1); 			spin_unlock_irq(&base->lock);			call_timer_fn(timer, fn, data);			spin_lock_irq(&base->lock);		}	}	base->running_timer = NULL;	spin_unlock_irq(&base->lock);} #ifdef CONFIG_NO_HZ/* * Find out when the next timer event is due to happen. This * is used on S/390 to stop all activity when a CPU is idle. * This function needs to be called with interrupts disabled. */static unsigned long __next_timer_interrupt(struct tvec_base *base){	unsigned long timer_jiffies = base->timer_jiffies;	unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA;	int index, slot, array, found = 0;	struct timer_list *nte;	struct tvec *varray[4]; 	/* Look for timer events in tv1. */	index = slot = timer_jiffies & TVR_MASK;	do {		list_for_each_entry(nte, base->tv1.vec + slot, entry) {			if (tbase_get_deferrable(nte->base))				continue; 			found = 1;			expires = nte->expires;			/* Look at the cascade bucket(s)? */			if (!index || slot < index)				goto cascade;			return expires;		}		slot = (slot + 1) & TVR_MASK;	} while (slot != index); cascade:	/* Calculate the next cascade event */	if (index)		timer_jiffies += TVR_SIZE - index;	timer_jiffies >>= TVR_BITS; 	/* Check tv2-tv5. */	varray[0] = &base->tv2;	varray[1] = &base->tv3;	varray[2] = &base->tv4;	varray[3] = &base->tv5; 	for (array = 0; array < 4; array++) {		struct tvec *varp = varray[array]; 		index = slot = timer_jiffies & TVN_MASK;		do {			list_for_each_entry(nte, varp->vec + slot, entry) {				if (tbase_get_deferrable(nte->base))					continue; 				found = 1;				if (time_before(nte->expires, expires))					expires = nte->expires;			}			/*			 * Do we still search for the first timer or are			 * we looking up the cascade buckets ?			 */			if (found) {				/* Look at the cascade bucket(s)? */				if (!index || slot < index)					break;				return expires;			}			slot = (slot + 1) & TVN_MASK;		} while (slot != index); 		if (index)			timer_jiffies += TVN_SIZE - index;		timer_jiffies >>= TVN_BITS;	}	return expires;} /* * Check, if the next hrtimer event is before the next timer wheel * event: */static unsigned long cmp_next_hrtimer_event(unsigned long now,					    unsigned long expires){	ktime_t hr_delta = hrtimer_get_next_event();	struct timespec tsdelta;	unsigned long delta; 	if (hr_delta.tv64 == KTIME_MAX)		return expires; 	/*	 * Expired timer available, let it expire in the next tick	 */	if (hr_delta.tv64 <= 0)		return now + 1; 	tsdelta = ktime_to_timespec(hr_delta);	delta = timespec_to_jiffies(&tsdelta); 	/*	 * Limit the delta to the max value, which is checked in	 * tick_nohz_stop_sched_tick():	 */	if (delta > NEXT_TIMER_MAX_DELTA)		delta = NEXT_TIMER_MAX_DELTA; 	/*	 * Take rounding errors in to account and make sure, that it	 * expires in the next tick. Otherwise we go into an endless	 * ping pong due to tick_nohz_stop_sched_tick() retriggering	 * the timer softirq	 */	if (delta < 1)		delta = 1;	now += delta;	if (time_before(now, expires))		return now;	return expires;} /** * get_next_timer_interrupt - return the jiffy of the next pending timer * @now: current time (in jiffies) */unsigned long get_next_timer_interrupt(unsigned long now){	struct tvec_base *base = __this_cpu_read(tvec_bases);	unsigned long expires; 	/*	 * Pretend that there is no timer pending if the cpu is offline.	 * Possible pending timers will be migrated later to an active cpu.	 */	if (cpu_is_offline(smp_processor_id()))		return now + NEXT_TIMER_MAX_DELTA;	spin_lock(&base->lock);	if (time_before_eq(base->next_timer, base->timer_jiffies))		base->next_timer = __next_timer_interrupt(base);	expires = base->next_timer;	spin_unlock(&base->lock); 	if (time_before_eq(expires, now))		return now; 	return cmp_next_hrtimer_event(now, expires);}#endif /* * Called from the timer interrupt handler to charge one tick to the current * process.  user_tick is 1 if the tick is user time, 0 for system. */void update_process_times(int user_tick){	struct task_struct *p = current;	int cpu = smp_processor_id(); 	/* Note: this timer irq context must be accounted for as well. */	account_process_tick(p, user_tick);	run_local_timers();	rcu_check_callbacks(cpu, user_tick);	printk_tick();#ifdef CONFIG_IRQ_WORK	if (in_irq())		irq_work_run();#endif	scheduler_tick();	run_posix_cpu_timers(p);} /* * This function runs timers and the timer-tq in bottom half context. */static void run_timer_softirq(struct softirq_action *h){	struct tvec_base *base = __this_cpu_read(tvec_bases); 	hrtimer_run_pending(); 	if (time_after_eq(jiffies, base->timer_jiffies))		__run_timers(base);} /* * Called by the local, per-CPU timer interrupt on SMP. */void run_local_timers(void){	hrtimer_run_queues();	raise_softirq(TIMER_SOFTIRQ);} #ifdef __ARCH_WANT_SYS_ALARM /* * For backwards compatibility?  This can be done in libc so Alpha * and all newer ports shouldn't need it. */SYSCALL_DEFINE1(alarm, unsigned int, seconds){	return alarm_setitimer(seconds);} #endif #ifndef __alpha__ /* * The Alpha uses getxpid, getxuid, and getxgid instead.  Maybe this * should be moved into arch/i386 instead? */ /** * sys_getpid - return the thread group id of the current process * * Note, despite the name, this returns the tgid not the pid.  The tgid and * the pid are identical unless CLONE_THREAD was specified on clone() in * which case the tgid is the same in all threads of the same group. * * This is SMP safe as current->tgid does not change. */SYSCALL_DEFINE0(getpid){	return task_tgid_vnr(current);} /* * Accessing ->real_parent is not SMP-safe, it could * change from under us. However, we can use a stale * value of ->real_parent under rcu_read_lock(), see * release_task()->call_rcu(delayed_put_task_struct). */SYSCALL_DEFINE0(getppid){	int pid; 	rcu_read_lock();	pid = task_tgid_vnr(rcu_dereference(current->real_parent));	rcu_read_unlock(); 	return pid;} SYSCALL_DEFINE0(getuid){	/* Only we change this so SMP safe */	return from_kuid_munged(current_user_ns(), current_uid());} SYSCALL_DEFINE0(geteuid){	/* Only we change this so SMP safe */	return from_kuid_munged(current_user_ns(), current_euid());} SYSCALL_DEFINE0(getgid){	/* Only we change this so SMP safe */	return from_kgid_munged(current_user_ns(), current_gid());} SYSCALL_DEFINE0(getegid){	/* Only we change this so SMP safe */	return from_kgid_munged(current_user_ns(), current_egid());} #endif static void process_timeout(unsigned long __data){	wake_up_process((struct task_struct *)__data);} /** * schedule_timeout - sleep until timeout * @timeout: timeout value in jiffies * * Make the current task sleep until @timeout jiffies have * elapsed. The routine will return immediately unless * the current task state has been set (see set_current_state()). * * You can set the task state as follows - * * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to * pass before the routine returns. The routine will return 0 * * %TASK_INTERRUPTIBLE - the routine may return early if a signal is * delivered to the current task. In this case the remaining time * in jiffies will be returned, or 0 if the timer expired in time * * The current task state is guaranteed to be TASK_RUNNING when this * routine returns. * * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule * the CPU away without a bound on the timeout. In this case the return * value will be %MAX_SCHEDULE_TIMEOUT. * * In all cases the return value is guaranteed to be non-negative. */signed long __sched schedule_timeout(signed long timeout){	struct timer_list timer;	unsigned long expire; 	switch (timeout)	{	case MAX_SCHEDULE_TIMEOUT:		/*

Lines 1001–1500 of 1850. Previous lines