From mboxrd@z Thu Jan 1 00:00:00 1970 From: Petr Mladek Subject: Re: [PATCH v5 08/20] kthread: Allow to cancel kthread work Date: Fri, 26 Feb 2016 16:38:18 +0100 Message-ID: <20160226153818.GI3305@pathway.suse.cz> References: <1456153030-12400-9-git-send-email-pmladek@suse.com> <201602230025.uuCAc4Tn%fengguang.wu@intel.com> <20160224161805.GB3305@pathway.suse.cz> <20160225125932.GI6357@twins.programming.kicks-ass.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Content-Disposition: inline In-Reply-To: <20160225125932.GI6357-ndre7Fmf5hadTX5a5knrm8zTDFooKrT+cvkQGrU6aU0@public.gmane.org> Sender: linux-api-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org To: Peter Zijlstra Cc: kbuild test robot , kbuild-all-JC7UmRfGjtg@public.gmane.org, Andrew Morton , Oleg Nesterov , Tejun Heo , Ingo Molnar , Steven Rostedt , "Paul E. McKenney" , Josh Triplett , Thomas Gleixner , Linus Torvalds , Jiri Kosina , Borislav Petkov , Michal Hocko , linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org, Vlastimil Babka , linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org List-Id: linux-api@vger.kernel.org On Thu 2016-02-25 13:59:32, Peter Zijlstra wrote: > On Wed, Feb 24, 2016 at 05:18:05PM +0100, Petr Mladek wrote: > > @@ -770,7 +782,22 @@ void delayed_kthread_work_timer_fn(unsigned long __data) > > if (WARN_ON_ONCE(!worker)) > > return; > > > > - spin_lock(&worker->lock); > > + /* > > + * We might be unable to take the lock if someone is trying to > > + * cancel this work and calls del_timer_sync() when this callback > > + * has already been removed from the timer list. > > + */ > > + while (!spin_trylock(&worker->lock)) { > > + /* > > + * Busy wait with spin_is_locked() to avoid cache bouncing. > > + * Break when canceling is set to avoid a deadlock. > > + */ > > + do { > > + if (work->canceling) > > + return; > > + cpu_relax(); > > + } while (spin_is_locked(&worker->lock)); > > + } > > /* Work must not be used with more workers, see queue_kthread_work(). */ > > WARN_ON_ONCE(work->worker != worker); > > > > This is pretty vile; why can't you drop the lock over del_timer_sync() ? We would need to take the lock later and check if nobody has set the timer again in the meantime. Now, timer_active() check is not reliable. It does not check if the timer handler is running at the moment. I tried to implement a safe timer_active()[1] but nobody was keen to take it. Even if we have that timer_active() check, we would need to add some relock/check/try_again stuff around the del_timer_sync(). So, it would just move the complexity somewhere else. I think that the current solution is quite elegant after all. Thanks a lot Tejun for the idea. Reference: [1] http://thread.gmane.org/gmane.linux.kernel.mm/144964/focus=144965 Thanks a lot for review, Petr