Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932221AbcLLBPg (ORCPT ); Sun, 11 Dec 2016 20:15:36 -0500 Received: from mga07.intel.com ([134.134.136.100]:64060 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752306AbcLLBPf (ORCPT ); Sun, 11 Dec 2016 20:15:35 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,334,1477983600"; d="scan'208";a="41404430" From: "Huang\, Ying" To: Joel Fernandes Cc: , Huang Ying , Ingo Molnar , Will Deacon , "Paul McKenney" , Mathieu Desnoyers Subject: Re: [PATCH v2] llist: Clarify comments about when locking is needed References: <1481393024-22310-1-git-send-email-joelaf@google.com> Date: Mon, 12 Dec 2016 09:15:32 +0800 In-Reply-To: <1481393024-22310-1-git-send-email-joelaf@google.com> (Joel Fernandes's message of "Sat, 10 Dec 2016 10:03:42 -0800") Message-ID: <87y3zmvtfv.fsf@yhuang-dev.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3653 Lines: 80 Joel Fernandes writes: > llist.h comments are a bit confusing about when locking is needed versus when > it isn't. Clarify these comments a bit more by being a bit more descriptive > about why locking is needed for llist_del_first. > > Cc: Huang Ying > Cc: Ingo Molnar > Cc: Will Deacon > Cc: Paul McKenney > Acked-by: Mathieu Desnoyers > Signed-off-by: Joel Fernandes Acked-by: "Huang, Ying" Best Regards, Huang, Ying > --- > v2 changes: > Minor changes to comment and commit message based on Mathieu's suggestions > (https://lkml.org/lkml/2016/12/10/39) > > include/linux/llist.h | 37 +++++++++++++++++++++---------------- > 1 file changed, 21 insertions(+), 16 deletions(-) > > diff --git a/include/linux/llist.h b/include/linux/llist.h > index fd4ca0b..31822bb 100644 > --- a/include/linux/llist.h > +++ b/include/linux/llist.h > @@ -3,28 +3,33 @@ > /* > * Lock-less NULL terminated single linked list > * > - * If there are multiple producers and multiple consumers, llist_add > - * can be used in producers and llist_del_all can be used in > - * consumers. They can work simultaneously without lock. But > - * llist_del_first can not be used here. Because llist_del_first > - * depends on list->first->next does not changed if list->first is not > - * changed during its operation, but llist_del_first, llist_add, > - * llist_add (or llist_del_all, llist_add, llist_add) sequence in > - * another consumer may violate that. > - * > - * If there are multiple producers and one consumer, llist_add can be > - * used in producers and llist_del_all or llist_del_first can be used > - * in the consumer. > - * > - * This can be summarized as follow: > + * Cases where locking is not needed: > + * If there are multiple producers and multiple consumers, llist_add can be > + * used in producers and llist_del_all can be used in consumers simultaneously > + * without locking. Also a single consumer can use llist_del_first while > + * multiple producers simultaneously use llist_add, without any locking. > + * > + * Cases where locking is needed: > + * If we have multiple consumers with llist_del_first used in one consumer, and > + * llist_del_first or llist_del_all used in other consumers, then a lock is > + * needed. This is because llist_del_first depends on list->first->next not > + * changing, but without lock protection, there's no way to be sure about that > + * if a preemption happens in the middle of the delete operation and on being > + * preempted back, the list->first is the same as before causing the cmpxchg in > + * llist_del_first to succeed. For example, while a llist_del_first operation > + * is in progress in one consumer, then a llist_del_first, llist_add, > + * llist_add (or llist_del_all, llist_add, llist_add) sequence in another > + * consumer may cause violations. > + * > + * This can be summarized as follows: > * > * | add | del_first | del_all > * add | - | - | - > * del_first | | L | L > * del_all | | | - > * > - * Where "-" stands for no lock is needed, while "L" stands for lock > - * is needed. > + * Where, a particular row's operation can happen concurrently with a column's > + * operation, with "-" being no lock needed, while "L" being lock is needed. > * > * The list entries deleted via llist_del_all can be traversed with > * traversing function such as llist_for_each etc. But the list