2580 lines · 77.2 KB · C
RawDownload
 		if (!(tf & TIMER_MIGRATING)) {			base = get_timer_base(tf);			raw_spin_lock_irqsave(&base->lock, *flags);			if (timer->flags == tf)				return base;			raw_spin_unlock_irqrestore(&base->lock, *flags);		}		cpu_relax();	}} #define MOD_TIMER_PENDING_ONLY		0x01#define MOD_TIMER_REDUCE		0x02#define MOD_TIMER_NOTPENDING		0x04 static inline int__mod_timer(struct timer_list *timer, unsigned long expires, unsigned int options){	unsigned long clk = 0, flags, bucket_expiry;	struct timer_base *base, *new_base;	unsigned int idx = UINT_MAX;	int ret = 0; 	debug_assert_init(timer); 	/*	 * This is a common optimization triggered by the networking code - if	 * the timer is re-modified to have the same timeout or ends up in the	 * same array bucket then just return:	 */	if (!(options & MOD_TIMER_NOTPENDING) && timer_pending(timer)) {		/*		 * The downside of this optimization is that it can result in		 * larger granularity than you would get from adding a new		 * timer with this expiry.		 */		long diff = timer->expires - expires; 		if (!diff)			return 1;		if (options & MOD_TIMER_REDUCE && diff <= 0)			return 1; 		/*		 * We lock timer base and calculate the bucket index right		 * here. If the timer ends up in the same bucket, then we		 * just update the expiry time and avoid the whole		 * dequeue/enqueue dance.		 */		base = lock_timer_base(timer, &flags);		/*		 * Has @timer been shutdown? This needs to be evaluated		 * while holding base lock to prevent a race against the		 * shutdown code.		 */		if (!timer->function)			goto out_unlock; 		forward_timer_base(base); 		if (timer_pending(timer) && (options & MOD_TIMER_REDUCE) &&		    time_before_eq(timer->expires, expires)) {			ret = 1;			goto out_unlock;		} 		clk = base->clk;		idx = calc_wheel_index(expires, clk, &bucket_expiry); 		/*		 * Retrieve and compare the array index of the pending		 * timer. If it matches set the expiry to the new value so a		 * subsequent call will exit in the expires check above.		 */		if (idx == timer_get_idx(timer)) {			if (!(options & MOD_TIMER_REDUCE))				timer->expires = expires;			else if (time_after(timer->expires, expires))				timer->expires = expires;			ret = 1;			goto out_unlock;		}	} else {		base = lock_timer_base(timer, &flags);		/*		 * Has @timer been shutdown? This needs to be evaluated		 * while holding base lock to prevent a race against the		 * shutdown code.		 */		if (!timer->function)			goto out_unlock; 		forward_timer_base(base);	} 	ret = detach_if_pending(timer, base, false);	if (!ret && (options & MOD_TIMER_PENDING_ONLY))		goto out_unlock; 	new_base = get_timer_this_cpu_base(timer->flags); 	if (base != new_base) {		/*		 * We are trying to schedule the timer on the new base.		 * However we can't change timer's base while it is running,		 * otherwise timer_delete_sync() can't detect that the timer's		 * handler yet has not finished. This also guarantees that the		 * timer is serialized wrt itself.		 */		if (likely(base->running_timer != timer)) {			/* See the comment in lock_timer_base() */			timer->flags |= TIMER_MIGRATING; 			raw_spin_unlock(&base->lock);			base = new_base;			raw_spin_lock(&base->lock);			WRITE_ONCE(timer->flags,				   (timer->flags & ~TIMER_BASEMASK) | base->cpu);			forward_timer_base(base);		}	} 	debug_timer_activate(timer); 	timer->expires = expires;	/*	 * If 'idx' was calculated above and the base time did not advance	 * between calculating 'idx' and possibly switching the base, only	 * enqueue_timer() is required. Otherwise we need to (re)calculate	 * the wheel index via internal_add_timer().	 */	if (idx != UINT_MAX && clk == base->clk)		enqueue_timer(base, timer, idx, bucket_expiry);	else		internal_add_timer(base, timer); out_unlock:	raw_spin_unlock_irqrestore(&base->lock, flags); 	return ret;} /** * mod_timer_pending - Modify a pending timer's timeout * @timer:	The pending timer to be modified * @expires:	New absolute timeout in jiffies * * mod_timer_pending() is the same for pending timers as mod_timer(), but * will not activate inactive timers. * * If @timer->function == NULL then the start operation is silently * discarded. * * Return: * * %0 - The timer was inactive and not modified or was in *	  shutdown state and the operation was discarded * * %1 - The timer was active and requeued to expire at @expires */int mod_timer_pending(struct timer_list *timer, unsigned long expires){	return __mod_timer(timer, expires, MOD_TIMER_PENDING_ONLY);}EXPORT_SYMBOL(mod_timer_pending); /** * mod_timer - Modify a timer's timeout * @timer:	The timer to be modified * @expires:	New absolute timeout in jiffies * * mod_timer(timer, expires) is equivalent to: * *     timer_delete(timer); timer->expires = expires; add_timer(timer); * * mod_timer() is more efficient than the above open coded sequence. In * case that the timer is inactive, the timer_delete() part is a NOP. The * timer is in any case activated with the new expiry time @expires. * * Note that if there are multiple unserialized concurrent users of the * same timer, then mod_timer() is the only safe way to modify the timeout, * since add_timer() cannot modify an already running timer. * * If @timer->function == NULL then the start operation is silently * discarded. In this case the return value is 0 and meaningless. * * Return: * * %0 - The timer was inactive and started or was in shutdown *	  state and the operation was discarded * * %1 - The timer was active and requeued to expire at @expires or *	  the timer was active and not modified because @expires did *	  not change the effective expiry time */int mod_timer(struct timer_list *timer, unsigned long expires){	return __mod_timer(timer, expires, 0);}EXPORT_SYMBOL(mod_timer); /** * timer_reduce - Modify a timer's timeout if it would reduce the timeout * @timer:	The timer to be modified * @expires:	New absolute timeout in jiffies * * timer_reduce() is very similar to mod_timer(), except that it will only * modify an enqueued timer if that would reduce the expiration time. If * @timer is not enqueued it starts the timer. * * If @timer->function == NULL then the start operation is silently * discarded. * * Return: * * %0 - The timer was inactive and started or was in shutdown *	  state and the operation was discarded * * %1 - The timer was active and requeued to expire at @expires or *	  the timer was active and not modified because @expires *	  did not change the effective expiry time such that the *	  timer would expire earlier than already scheduled */int timer_reduce(struct timer_list *timer, unsigned long expires){	return __mod_timer(timer, expires, MOD_TIMER_REDUCE);}EXPORT_SYMBOL(timer_reduce); /** * add_timer - Start a timer * @timer:	The timer to be started * * Start @timer to expire at @timer->expires in the future. @timer->expires * is the absolute expiry time measured in 'jiffies'. When the timer expires * timer->function(timer) will be invoked from soft interrupt context. * * The @timer->expires and @timer->function fields must be set prior * to calling this function. * * If @timer->function == NULL then the start operation is silently * discarded. * * If @timer->expires is already in the past @timer will be queued to * expire at the next timer tick. * * This can only operate on an inactive timer. Attempts to invoke this on * an active timer are rejected with a warning. */void add_timer(struct timer_list *timer){	if (WARN_ON_ONCE(timer_pending(timer)))		return;	__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);}EXPORT_SYMBOL(add_timer); /** * add_timer_local() - Start a timer on the local CPU * @timer:	The timer to be started * * Same as add_timer() except that the timer flag TIMER_PINNED is set. * * See add_timer() for further details. */void add_timer_local(struct timer_list *timer){	if (WARN_ON_ONCE(timer_pending(timer)))		return;	timer->flags |= TIMER_PINNED;	__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);}EXPORT_SYMBOL(add_timer_local); /** * add_timer_global() - Start a timer without TIMER_PINNED flag set * @timer:	The timer to be started * * Same as add_timer() except that the timer flag TIMER_PINNED is unset. * * See add_timer() for further details. */void add_timer_global(struct timer_list *timer){	if (WARN_ON_ONCE(timer_pending(timer)))		return;	timer->flags &= ~TIMER_PINNED;	__mod_timer(timer, timer->expires, MOD_TIMER_NOTPENDING);}EXPORT_SYMBOL(add_timer_global); /** * add_timer_on - Start a timer on a particular CPU * @timer:	The timer to be started * @cpu:	The CPU to start it on * * Same as add_timer() except that it starts the timer on the given CPU and * the TIMER_PINNED flag is set. When timer shouldn't be a pinned timer in * the next round, add_timer_global() should be used instead as it unsets * the TIMER_PINNED flag. * * See add_timer() for further details. */void add_timer_on(struct timer_list *timer, int cpu){	struct timer_base *new_base, *base;	unsigned long flags; 	debug_assert_init(timer); 	if (WARN_ON_ONCE(timer_pending(timer)))		return; 	/* Make sure timer flags have TIMER_PINNED flag set */	timer->flags |= TIMER_PINNED; 	new_base = get_timer_cpu_base(timer->flags, cpu); 	/*	 * If @timer was on a different CPU, it should be migrated with the	 * old base locked to prevent other operations proceeding with the	 * wrong base locked.  See lock_timer_base().	 */	base = lock_timer_base(timer, &flags);	/*	 * Has @timer been shutdown? This needs to be evaluated while	 * holding base lock to prevent a race against the shutdown code.	 */	if (!timer->function)		goto out_unlock; 	if (base != new_base) {		timer->flags |= TIMER_MIGRATING; 		raw_spin_unlock(&base->lock);		base = new_base;		raw_spin_lock(&base->lock);		WRITE_ONCE(timer->flags,			   (timer->flags & ~TIMER_BASEMASK) | cpu);	}	forward_timer_base(base); 	debug_timer_activate(timer);	internal_add_timer(base, timer);out_unlock:	raw_spin_unlock_irqrestore(&base->lock, flags);}EXPORT_SYMBOL_GPL(add_timer_on); /** * __timer_delete - Internal function: Deactivate a timer * @timer:	The timer to be deactivated * @shutdown:	If true, this indicates that the timer is about to be *		shutdown permanently. * * If @shutdown is true then @timer->function is set to NULL under the * timer base lock which prevents further rearming of the time. In that * case any attempt to rearm @timer after this function returns will be * silently ignored. * * Return: * * %0 - The timer was not pending * * %1 - The timer was pending and deactivated */static int __timer_delete(struct timer_list *timer, bool shutdown){	struct timer_base *base;	unsigned long flags;	int ret = 0; 	debug_assert_init(timer); 	/*	 * If @shutdown is set then the lock has to be taken whether the	 * timer is pending or not to protect against a concurrent rearm	 * which might hit between the lockless pending check and the lock	 * acquisition. By taking the lock it is ensured that such a newly	 * enqueued timer is dequeued and cannot end up with	 * timer->function == NULL in the expiry code.	 *	 * If timer->function is currently executed, then this makes sure	 * that the callback cannot requeue the timer.	 */	if (timer_pending(timer) || shutdown) {		base = lock_timer_base(timer, &flags);		ret = detach_if_pending(timer, base, true);		if (shutdown)			timer->function = NULL;		raw_spin_unlock_irqrestore(&base->lock, flags);	} 	return ret;} /** * timer_delete - Deactivate a timer * @timer:	The timer to be deactivated * * The function only deactivates a pending timer, but contrary to * timer_delete_sync() it does not take into account whether the timer's * callback function is concurrently executed on a different CPU or not. * It neither prevents rearming of the timer.  If @timer can be rearmed * concurrently then the return value of this function is meaningless. * * Return: * * %0 - The timer was not pending * * %1 - The timer was pending and deactivated */int timer_delete(struct timer_list *timer){	return __timer_delete(timer, false);}EXPORT_SYMBOL(timer_delete); /** * timer_shutdown - Deactivate a timer and prevent rearming * @timer:	The timer to be deactivated * * The function does not wait for an eventually running timer callback on a * different CPU but it prevents rearming of the timer. Any attempt to arm * @timer after this function returns will be silently ignored. * * This function is useful for teardown code and should only be used when * timer_shutdown_sync() cannot be invoked due to locking or context constraints. * * Return: * * %0 - The timer was not pending * * %1 - The timer was pending */int timer_shutdown(struct timer_list *timer){	return __timer_delete(timer, true);}EXPORT_SYMBOL_GPL(timer_shutdown); /** * __try_to_del_timer_sync - Internal function: Try to deactivate a timer * @timer:	Timer to deactivate * @shutdown:	If true, this indicates that the timer is about to be *		shutdown permanently. * * If @shutdown is true then @timer->function is set to NULL under the * timer base lock which prevents further rearming of the timer. Any * attempt to rearm @timer after this function returns will be silently * ignored. * * This function cannot guarantee that the timer cannot be rearmed * right after dropping the base lock if @shutdown is false. That * needs to be prevented by the calling code if necessary. * * Return: * * %0  - The timer was not pending * * %1  - The timer was pending and deactivated * * %-1 - The timer callback function is running on a different CPU */static int __try_to_del_timer_sync(struct timer_list *timer, bool shutdown){	struct timer_base *base;	unsigned long flags;	int ret = -1; 	debug_assert_init(timer); 	base = lock_timer_base(timer, &flags); 	if (base->running_timer != timer) {		ret = detach_if_pending(timer, base, true);		if (shutdown)			timer->function = NULL;	} 	raw_spin_unlock_irqrestore(&base->lock, flags); 	return ret;} /** * timer_delete_sync_try - Try to deactivate a timer * @timer:	Timer to deactivate * * This function tries to deactivate a timer. On success the timer is not * queued and the timer callback function is not running on any CPU. * * This function does not guarantee that the timer cannot be rearmed right * after dropping the base lock. That needs to be prevented by the calling * code if necessary. * * Return: * * %0  - The timer was not pending * * %1  - The timer was pending and deactivated * * %-1 - The timer callback function is running on a different CPU */int timer_delete_sync_try(struct timer_list *timer){	return __try_to_del_timer_sync(timer, false);}EXPORT_SYMBOL(timer_delete_sync_try); #ifdef CONFIG_PREEMPT_RTstatic __init void timer_base_init_expiry_lock(struct timer_base *base){	spin_lock_init(&base->expiry_lock);} static inline void timer_base_lock_expiry(struct timer_base *base)

Lines 1001–1500 of 2580. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t