From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933896AbcAYTFB (ORCPT ); Mon, 25 Jan 2016 14:05:01 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:34385 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757063AbcAYTE5 (ORCPT ); Mon, 25 Jan 2016 14:04:57 -0500 Date: Mon, 25 Jan 2016 14:04:54 -0500 From: Tejun Heo To: Petr Mladek 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 v4 08/22] kthread: Initial support for delayed kthread work Message-ID: <20160125190454.GD3628@mtj.duckdns.org> References: <1453736711-6703-1-git-send-email-pmladek@suse.com> <1453736711-6703-9-git-send-email-pmladek@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1453736711-6703-9-git-send-email-pmladek@suse.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, On Mon, Jan 25, 2016 at 04:44:57PM +0100, Petr Mladek wrote: > +/* > + * Returns true when there is a pending operation for this work. > + * In particular, it checks if the work is: > + * - queued > + * - a timer is running to queue this delayed work > + * > + * This function must be called with locked work. > + */ > +static inline bool kthread_work_pending(const struct kthread_work *work) > +{ > + return !list_empty(&work->node) || > + (work->timer && timer_active(work->timer)); > +} Why not just put the work item on a separate list so that lits_empty(&work->node) is always enough? IOW, put delayed work items on timers on worker->delayed or sth. > +/* > + * Queue @work right into the worker queue. > + */ > +static void __queue_kthread_work(struct kthread_worker *worker, > + struct kthread_work *work) > +{ > + insert_kthread_work(worker, work, &worker->work_list); > +} Does this really need to be an inline function? This sort of one liner helpers tend to be obfuscating more than anything else. > @@ -756,6 +779,121 @@ bool queue_kthread_work(struct kthread_worker *worker, > } > EXPORT_SYMBOL_GPL(queue_kthread_work); > > +static bool try_lock_kthread_work(struct kthread_work *work) > +{ > + struct kthread_worker *worker; > + int ret = false; > + > +try_again: > + worker = work->worker; > + > + if (!worker) > + goto out; return false; > + > + spin_lock(&worker->lock); > + if (worker != work->worker) { > + spin_unlock(&worker->lock); > + goto try_again; > + } return true; > + ret = true; > + > +out: > + return ret; > +} Stop building unnecessary structures. Keep it simple. > +static inline void unlock_kthread_work(struct kthread_work *work) > +{ > + spin_unlock(&work->worker->lock); > +} Ditto. Just open code it. It doesn't add anything. > +/** > + * delayed_kthread_work_timer_fn - callback that queues the associated delayed > + * kthread work when the timer expires. > + * @__data: pointer to the data associated with the timer > + * > + * The format of the function is defined by struct timer_list. > + * It should have been called from irqsafe timer with irq already off. > + */ > +void delayed_kthread_work_timer_fn(unsigned long __data) > +{ > + struct delayed_kthread_work *dwork = > + (struct delayed_kthread_work *)__data; > + struct kthread_work *work = &dwork->work; > + > + if (!try_lock_kthread_work(work)) Can you please explain why try_lock is necessary here? That's the most important and non-obvious thing going on here and there's no explanation of that at all. Thanks. -- tejun From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tejun Heo Subject: Re: [PATCH v4 08/22] kthread: Initial support for delayed kthread work Date: Mon, 25 Jan 2016 14:04:54 -0500 Message-ID: <20160125190454.GD3628@mtj.duckdns.org> References: <1453736711-6703-1-git-send-email-pmladek@suse.com> <1453736711-6703-9-git-send-email-pmladek@suse.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Content-Disposition: inline In-Reply-To: <1453736711-6703-9-git-send-email-pmladek-IBi9RG/b67k@public.gmane.org> Sender: linux-api-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org To: Petr Mladek 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-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 Hello, On Mon, Jan 25, 2016 at 04:44:57PM +0100, Petr Mladek wrote: > +/* > + * Returns true when there is a pending operation for this work. > + * In particular, it checks if the work is: > + * - queued > + * - a timer is running to queue this delayed work > + * > + * This function must be called with locked work. > + */ > +static inline bool kthread_work_pending(const struct kthread_work *work) > +{ > + return !list_empty(&work->node) || > + (work->timer && timer_active(work->timer)); > +} Why not just put the work item on a separate list so that lits_empty(&work->node) is always enough? IOW, put delayed work items on timers on worker->delayed or sth. > +/* > + * Queue @work right into the worker queue. > + */ > +static void __queue_kthread_work(struct kthread_worker *worker, > + struct kthread_work *work) > +{ > + insert_kthread_work(worker, work, &worker->work_list); > +} Does this really need to be an inline function? This sort of one liner helpers tend to be obfuscating more than anything else. > @@ -756,6 +779,121 @@ bool queue_kthread_work(struct kthread_worker *worker, > } > EXPORT_SYMBOL_GPL(queue_kthread_work); > > +static bool try_lock_kthread_work(struct kthread_work *work) > +{ > + struct kthread_worker *worker; > + int ret = false; > + > +try_again: > + worker = work->worker; > + > + if (!worker) > + goto out; return false; > + > + spin_lock(&worker->lock); > + if (worker != work->worker) { > + spin_unlock(&worker->lock); > + goto try_again; > + } return true; > + ret = true; > + > +out: > + return ret; > +} Stop building unnecessary structures. Keep it simple. > +static inline void unlock_kthread_work(struct kthread_work *work) > +{ > + spin_unlock(&work->worker->lock); > +} Ditto. Just open code it. It doesn't add anything. > +/** > + * delayed_kthread_work_timer_fn - callback that queues the associated delayed > + * kthread work when the timer expires. > + * @__data: pointer to the data associated with the timer > + * > + * The format of the function is defined by struct timer_list. > + * It should have been called from irqsafe timer with irq already off. > + */ > +void delayed_kthread_work_timer_fn(unsigned long __data) > +{ > + struct delayed_kthread_work *dwork = > + (struct delayed_kthread_work *)__data; > + struct kthread_work *work = &dwork->work; > + > + if (!try_lock_kthread_work(work)) Can you please explain why try_lock is necessary here? That's the most important and non-obvious thing going on here and there's no explanation of that at all. Thanks. -- tejun From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f179.google.com (mail-pf0-f179.google.com [209.85.192.179]) by kanga.kvack.org (Postfix) with ESMTP id 806FB6B0255 for ; Mon, 25 Jan 2016 14:04:57 -0500 (EST) Received: by mail-pf0-f179.google.com with SMTP id e65so86597794pfe.0 for ; Mon, 25 Jan 2016 11:04:57 -0800 (PST) Received: from mail-pf0-x244.google.com (mail-pf0-x244.google.com. [2607:f8b0:400e:c00::244]) by mx.google.com with ESMTPS id iv8si35393327pac.104.2016.01.25.11.04.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jan 2016 11:04:56 -0800 (PST) Received: by mail-pf0-x244.google.com with SMTP id 65so7214655pff.2 for ; Mon, 25 Jan 2016 11:04:56 -0800 (PST) Date: Mon, 25 Jan 2016 14:04:54 -0500 From: Tejun Heo Subject: Re: [PATCH v4 08/22] kthread: Initial support for delayed kthread work Message-ID: <20160125190454.GD3628@mtj.duckdns.org> References: <1453736711-6703-1-git-send-email-pmladek@suse.com> <1453736711-6703-9-git-send-email-pmladek@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1453736711-6703-9-git-send-email-pmladek@suse.com> Sender: owner-linux-mm@kvack.org List-ID: To: Petr Mladek 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 Hello, On Mon, Jan 25, 2016 at 04:44:57PM +0100, Petr Mladek wrote: > +/* > + * Returns true when there is a pending operation for this work. > + * In particular, it checks if the work is: > + * - queued > + * - a timer is running to queue this delayed work > + * > + * This function must be called with locked work. > + */ > +static inline bool kthread_work_pending(const struct kthread_work *work) > +{ > + return !list_empty(&work->node) || > + (work->timer && timer_active(work->timer)); > +} Why not just put the work item on a separate list so that lits_empty(&work->node) is always enough? IOW, put delayed work items on timers on worker->delayed or sth. > +/* > + * Queue @work right into the worker queue. > + */ > +static void __queue_kthread_work(struct kthread_worker *worker, > + struct kthread_work *work) > +{ > + insert_kthread_work(worker, work, &worker->work_list); > +} Does this really need to be an inline function? This sort of one liner helpers tend to be obfuscating more than anything else. > @@ -756,6 +779,121 @@ bool queue_kthread_work(struct kthread_worker *worker, > } > EXPORT_SYMBOL_GPL(queue_kthread_work); > > +static bool try_lock_kthread_work(struct kthread_work *work) > +{ > + struct kthread_worker *worker; > + int ret = false; > + > +try_again: > + worker = work->worker; > + > + if (!worker) > + goto out; return false; > + > + spin_lock(&worker->lock); > + if (worker != work->worker) { > + spin_unlock(&worker->lock); > + goto try_again; > + } return true; > + ret = true; > + > +out: > + return ret; > +} Stop building unnecessary structures. Keep it simple. > +static inline void unlock_kthread_work(struct kthread_work *work) > +{ > + spin_unlock(&work->worker->lock); > +} Ditto. Just open code it. It doesn't add anything. > +/** > + * delayed_kthread_work_timer_fn - callback that queues the associated delayed > + * kthread work when the timer expires. > + * @__data: pointer to the data associated with the timer > + * > + * The format of the function is defined by struct timer_list. > + * It should have been called from irqsafe timer with irq already off. > + */ > +void delayed_kthread_work_timer_fn(unsigned long __data) > +{ > + struct delayed_kthread_work *dwork = > + (struct delayed_kthread_work *)__data; > + struct kthread_work *work = &dwork->work; > + > + if (!try_lock_kthread_work(work)) Can you please explain why try_lock is necessary here? That's the most important and non-obvious thing going on here and there's no explanation of that at all. Thanks. -- tejun -- 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