Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754787AbbKXQ3E (ORCPT ); Tue, 24 Nov 2015 11:29:04 -0500 Received: from mx2.suse.de ([195.135.220.15]:44673 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753753AbbKXQ20 (ORCPT ); Tue, 24 Nov 2015 11:28:26 -0500 Date: Tue, 24 Nov 2015 17:28:23 +0100 From: Petr Mladek To: Tejun Heo Cc: Andrew Morton , Oleg Nesterov , Ingo Molnar , Peter Zijlstra , 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 Subject: Re: [PATCH v3 07/22] kthread: Detect when a kthread work is used by more workers Message-ID: <20151124162822.GJ10750@pathway.suse.cz> References: <1447853127-3461-1-git-send-email-pmladek@suse.com> <1447853127-3461-8-git-send-email-pmladek@suse.com> <20151123222703.GH19072@mtj.duckdns.org> <20151124100650.GF10750@pathway.suse.cz> <20151124144942.GC17033@mtj.duckdns.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20151124144942.GC17033@mtj.duckdns.org> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2249 Lines: 56 On Tue 2015-11-24 09:49:42, Tejun Heo wrote: > Hello, Petr. > > On Tue, Nov 24, 2015 at 11:06:50AM +0100, Petr Mladek wrote: > > > > @@ -610,6 +625,12 @@ repeat: > > > > if (work) { > > > > __set_current_state(TASK_RUNNING); > > > > work->func(work); > > > > + > > > > + spin_lock_irq(&worker->lock); > > > > + /* Allow to queue the work into another worker */ > > > > + if (!kthread_work_pending(work)) > > > > + work->worker = NULL; > > > > + spin_unlock_irq(&worker->lock); > > > > > > Doesn't this mean that the work item can't be freed from its callback? > > > That pattern tends to happen regularly. > > > > I am not sure if I understand your question. Do you mean switching > > work->func during the life time of the struct kthread_work? This > > should not be affected by the above code. > >IOW, you can't expect the work > item to remain accessible once the work function starts executing. I see, I was not aware of this pattern. > > The above code allows to queue an _unused_ kthread_work into any > > kthread_worker. For example, it is needed for khugepaged, > > see http://marc.info/?l=linux-kernel&m=144785344924871&w=2 > > The work is static but the worker can be started/stopped > > (allocated/freed) repeatedly. It means that the work need > > to be usable with many workers. But it is associated only > > with one worker when being used. > > It can just re-init work items when it restarts workers, right? Yes, this would work. It might be slightly inconvenient but it looks like a good compromise. It helps to keep the API implementation rather simple and rather secure. Alternatively, we could allow to queue the work on another worker if it is not pending. But then we would need to check the pending status without the worker->lock because work->worker might point to an already freed worker. We need to check the pending status in many situations. It might open a can of worms that I probably do not want to catch. Thank you and PeterZ for explanation, Petr -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/