From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757788AbcAYPrl (ORCPT ); Mon, 25 Jan 2016 10:47:41 -0500 Received: from mx2.suse.de ([195.135.220.15]:35969 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757765AbcAYPrg (ORCPT ); Mon, 25 Jan 2016 10:47:36 -0500 From: Petr Mladek To: Andrew Morton , Oleg Nesterov , Tejun Heo , Ingo Molnar , Peter Zijlstra Cc: Steven Rostedt , "Paul E. McKenney" , Josh Triplett , Thomas Gleixner , Linus Torvalds , Jiri Kosina , Borislav Petkov , Michal Hocko , linux-mm@kvack.org, Vlastimil Babka , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH v4 01/22] timer: Allow to check when the timer callback has not finished yet Date: Mon, 25 Jan 2016 16:44:50 +0100 Message-Id: <1453736711-6703-2-git-send-email-pmladek@suse.com> X-Mailer: git-send-email 1.8.5.6 In-Reply-To: <1453736711-6703-1-git-send-email-pmladek@suse.com> References: <1453736711-6703-1-git-send-email-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org timer_pending() checks whether the list of callbacks is empty. Each callback is removed from the list before it is called, see call_timer_fn() in __run_timers(). Sometimes we need to make sure that the callback has finished. For example, if we want to free some resources that are accessed by the callback. For this purpose, this patch adds timer_active(). It checks both the list of callbacks and the running_timer. It takes the base_lock to see a consistent state. I plan to use it to implement delayed works in kthread worker. But I guess that it will have wider use. In fact, I wonder if timer_pending() is misused in some situations. Signed-off-by: Petr Mladek --- include/linux/timer.h | 2 ++ kernel/time/timer.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/timer.h b/include/linux/timer.h index 61aa61dc410c..237b7c3e2b4e 100644 --- a/include/linux/timer.h +++ b/include/linux/timer.h @@ -165,6 +165,8 @@ static inline int timer_pending(const struct timer_list * timer) return timer->entry.pprev != NULL; } +extern int timer_active(struct timer_list *timer); + extern void add_timer_on(struct timer_list *timer, int cpu); extern int del_timer(struct timer_list * timer); extern int mod_timer(struct timer_list *timer, unsigned long expires); diff --git a/kernel/time/timer.c b/kernel/time/timer.c index bbc5d1114583..1c16f3230771 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -778,6 +778,30 @@ static struct tvec_base *lock_timer_base(struct timer_list *timer, } } +/** + * timer_active - is a timer still in use? + * @timer: the timer in question + * + * timer_in_use() will tell whether the timer is pending or if the callback + * is curretly running. + * + * Use this function if you want to make sure that some resources + * will not longer get accessed by the timer callback. timer_pending() + * is not safe in this case. + */ +int timer_active(struct timer_list *timer) +{ + struct tvec_base *base; + unsigned long flags; + int ret; + + base = lock_timer_base(timer, &flags); + ret = timer_pending(timer) || base->running_timer == timer; + spin_unlock_irqrestore(&base->lock, flags); + + return ret; +} + static inline int __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only, int pinned) -- 1.8.5.6 From mboxrd@z Thu Jan 1 00:00:00 1970 From: Petr Mladek Subject: [PATCH v4 01/22] timer: Allow to check when the timer callback has not finished yet Date: Mon, 25 Jan 2016 16:44:50 +0100 Message-ID: <1453736711-6703-2-git-send-email-pmladek@suse.com> References: <1453736711-6703-1-git-send-email-pmladek@suse.com> Return-path: In-Reply-To: <1453736711-6703-1-git-send-email-pmladek@suse.com> Sender: owner-linux-mm@kvack.org To: Andrew Morton , Oleg Nesterov , Tejun Heo , Ingo Molnar , Peter Zijlstra Cc: Steven Rostedt , "Paul E. McKenney" , Josh Triplett , Thomas Gleixner , Linus Torvalds , Jiri Kosina , Borislav Petkov , Michal Hocko , linux-mm@kvack.org, Vlastimil Babka , linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek List-Id: linux-api@vger.kernel.org timer_pending() checks whether the list of callbacks is empty. Each callback is removed from the list before it is called, see call_timer_fn() in __run_timers(). Sometimes we need to make sure that the callback has finished. For example, if we want to free some resources that are accessed by the callback. For this purpose, this patch adds timer_active(). It checks both the list of callbacks and the running_timer. It takes the base_lock to see a consistent state. I plan to use it to implement delayed works in kthread worker. But I guess that it will have wider use. In fact, I wonder if timer_pending() is misused in some situations. Signed-off-by: Petr Mladek --- include/linux/timer.h | 2 ++ kernel/time/timer.c | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/linux/timer.h b/include/linux/timer.h index 61aa61dc410c..237b7c3e2b4e 100644 --- a/include/linux/timer.h +++ b/include/linux/timer.h @@ -165,6 +165,8 @@ static inline int timer_pending(const struct timer_list * timer) return timer->entry.pprev != NULL; } +extern int timer_active(struct timer_list *timer); + extern void add_timer_on(struct timer_list *timer, int cpu); extern int del_timer(struct timer_list * timer); extern int mod_timer(struct timer_list *timer, unsigned long expires); diff --git a/kernel/time/timer.c b/kernel/time/timer.c index bbc5d1114583..1c16f3230771 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -778,6 +778,30 @@ static struct tvec_base *lock_timer_base(struct timer_list *timer, } } +/** + * timer_active - is a timer still in use? + * @timer: the timer in question + * + * timer_in_use() will tell whether the timer is pending or if the callback + * is curretly running. + * + * Use this function if you want to make sure that some resources + * will not longer get accessed by the timer callback. timer_pending() + * is not safe in this case. + */ +int timer_active(struct timer_list *timer) +{ + struct tvec_base *base; + unsigned long flags; + int ret; + + base = lock_timer_base(timer, &flags); + ret = timer_pending(timer) || base->running_timer == timer; + spin_unlock_irqrestore(&base->lock, flags); + + return ret; +} + static inline int __mod_timer(struct timer_list *timer, unsigned long expires, bool pending_only, int pinned) -- 1.8.5.6 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: email@kvack.org