Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3207795yba; Mon, 8 Apr 2019 13:31:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqwGGosO3r2k8pq1ZPlGzjpq8SeQRLZMXhnoKbyEc/dzWrtzy4FM1HQ4Rw+CuTJynIgI1TD6 X-Received: by 2002:a17:902:54c:: with SMTP id 70mr1757634plf.210.1554755497037; Mon, 08 Apr 2019 13:31:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554755497; cv=none; d=google.com; s=arc-20160816; b=oVFHlm4qnQ5XZlEp+zTJfEqO0elo+VEXUzTy1XfIfuD8SqcKWLCjfTLhh5irD3Wnjt 4FT/vJtbh2hnENyhHZEVGKP91qTVrRWpzWIEokBHSMeZe4KKFV1UOQLCQPUKaavj9zvL fK3riBmavRfjMW6/AiWDVT+fQx5ruDIk3AOKtghAKB8PEYJKMLot/4zCGxg6OeYdgnci thu/WMVuPWnrBfDFXlMjnT7tBYB2d4wQeA4mjAAC14pFucY432//0LFV5AGYbuSB/h3n goiITRwaCrU8u3bhm9yYqr7EVqH/uUgzFdMfFysofTipF2FG3R5Gj+oK399n4ucH86AC ZH5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ut52tSopTGbIMzSwLGcLJn20UHypI1Xs+LCYLMFEEOA=; b=NND/6SwgYWTIiSHzL4bVu/L8PvYnJQQs3sfykwEMlLMsMAdPLqQYXjlCNRkuNsNQMW CSalSbBpIQkSav8Jr+PkoTVEKRsEHYM/okKCpMTbuvFN6eIDbb82+tHUEmGjZhf2q5+d Cfqi1eWsmzqtZJPzZP/mDQJ37TdIDd6XjxbjW+sOMQ24EMx+VUdTcKHZk1I4Cv2AYZbJ 7bvdNmxKM6vVlmDGzmBT/63z5aN8zxmaF64s1rkCdQtocZExI/uSnniIaVjlr9QL5LUw iTvpXsOnRtAEs2q7Cgdm8twEeeK3AVLJu3XBCVCXm5J0PwTbbj65ND1c26DmaLHQXhzN NJ2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=d3LDgVcm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y62si10978836pgd.357.2019.04.08.13.31.20; Mon, 08 Apr 2019 13:31:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=d3LDgVcm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729027AbfDHSJC (ORCPT + 99 others); Mon, 8 Apr 2019 14:09:02 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:37505 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726633AbfDHSJB (ORCPT ); Mon, 8 Apr 2019 14:09:01 -0400 Received: by mail-pg1-f194.google.com with SMTP id e6so7762993pgc.4 for ; Mon, 08 Apr 2019 11:09:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ut52tSopTGbIMzSwLGcLJn20UHypI1Xs+LCYLMFEEOA=; b=d3LDgVcmJDDU8U1vlNv2RKrkTMUyiHHNQz1AtPzI0JpSXS3Y5rRQr2S7ohNB1HhAY/ fi2WDMbVJnfpTmKWMGneixGZkSfjdDYfHw0aCfHBt7MsZMGrtrjQDRheqOMM3DjPCaxH Pbq0YQq1c7pur+NytOt1oWPu/1KA9PO+SfDPA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ut52tSopTGbIMzSwLGcLJn20UHypI1Xs+LCYLMFEEOA=; b=n/lI4ffry4EoFDR/4lNsAn5OaR/cFU7J6I7XLcNcCyRnwK4O65of3JQWCmGHAauU+X ahLYN16EkyrIPMZ5Hc28gTNFrXN9g8eJ60BAqNk6qxg0/J5xBMifh+FBfSWOThQGXzkj ja7AalTefJ+ncjYGP3F4ZQm6F6tRCdFltmcf+JoTzA91Zfdqqrg6beGux0RLw9SI35ma WDueCMQgs0jXUtEF0RTXYG0ekA1Y0rCEXKVDnGlMb0BeaX9ba4lb5RlBx8mF9p+PKcYt rsW4OaQo8SxYCUoOZcL7f6mJqFPWwkM7rvC7MApHpEhTX04qJ978EZOADX9NLRlxJVO/ Ig0w== X-Gm-Message-State: APjAAAWRYrWlhRN9XBu+SOw7m4gtVanN2LPKMeDzX2YtTxlXfEmY40Ax jQj6qNwmlutsuo+UOIZwVnAI9g== X-Received: by 2002:a63:744b:: with SMTP id e11mr29535494pgn.327.1554746940943; Mon, 08 Apr 2019 11:09:00 -0700 (PDT) Received: from localhost ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id x66sm18170366pfb.78.2019.04.08.11.08.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Apr 2019 11:09:00 -0700 (PDT) Date: Mon, 8 Apr 2019 14:08:59 -0400 From: Joel Fernandes To: "Paul E. McKenney" Cc: linux-kernel@vger.kernel.org, oleg@redhat.com, jannh@google.com, Jonathan Corbet , Josh Triplett , Lai Jiangshan , linux-doc@vger.kernel.org, Mathieu Desnoyers , Steven Rostedt Subject: Re: [PATCH v2] doc/rcuref: Document real world examples in kernel Message-ID: <20190408180859.GA133872@google.com> References: <20190329140555.118463-1-joel@joelfernandes.org> <20190404201039.GL14111@linux.ibm.com> <20190406021705.GA6615@localhost> <20190408175213.GT14111@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190408175213.GT14111@linux.ibm.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 08, 2019 at 10:52:13AM -0700, Paul E. McKenney wrote: > On Sat, Apr 06, 2019 at 02:17:05AM +0000, Joel Fernandes wrote: > > On Thu, Apr 04, 2019 at 01:10:39PM -0700, Paul E. McKenney wrote: > > > On Fri, Mar 29, 2019 at 10:05:55AM -0400, Joel Fernandes (Google) wrote: > > > > Document similar real world examples in the kernel corresponding to the > > > > second and third code snippets. Also correct an issue in > > > > release_referenced() in the code snippet example. > > > > > > > > Cc: oleg@redhat.com > > > > Cc: jannh@google.com > > > > Signed-off-by: Joel Fernandes (Google) > > > > > > Good catch, thank you! > > > > > > As usual, I could not resist doing a bit of wordsmithing. Please let me > > > know if I messed anything up in the version shown below. > > > > > > Thanx, Paul > > > > > > ------------------------------------------------------------------------ > > > > > > commit adcd92c0ab303b57b28a3cd097bd9ece824c14f6 > > > Author: Joel Fernandes (Google) > > > Date: Fri Mar 29 10:05:55 2019 -0400 > > > > > > doc/rcuref: Document real world examples in kernel > > > > > > Document similar real world examples in the kernel corresponding to the > > > second and third code snippets. Also correct an issue in > > > release_referenced() in the code snippet example. > > > > > > Cc: oleg@redhat.com > > > Cc: jannh@google.com > > > Signed-off-by: Joel Fernandes (Google) > > > [ paulmck: Do a bit of wordsmithing. ] > > > Signed-off-by: Paul E. McKenney > > > > > > diff --git a/Documentation/RCU/rcuref.txt b/Documentation/RCU/rcuref.txt > > > index 613033ff2b9b..c0bab7fb57e7 100644 > > > --- a/Documentation/RCU/rcuref.txt > > > +++ b/Documentation/RCU/rcuref.txt > > > @@ -12,6 +12,7 @@ please read on. > > > Reference counting on elements of lists which are protected by traditional > > > reader/writer spinlocks or semaphores are straightforward: > > > > > > +CODE LISTING A: > > > 1. 2. > > > add() search_and_reference() > > > { { > > > @@ -28,7 +29,8 @@ add() search_and_reference() > > > release_referenced() delete() > > > { { > > > ... write_lock(&list_lock); > > > - atomic_dec(&el->rc, relfunc) ... > > > + if(atomic_dec_and_test(&el->rc)) ... > > > + kfree(el); > > > ... remove_element > > > } write_unlock(&list_lock); > > > ... > > > @@ -44,6 +46,7 @@ search_and_reference() could potentially hold reference to an element which > > > has already been deleted from the list/array. Use atomic_inc_not_zero() > > > in this scenario as follows: > > > > > > +CODE LISTING B: > > > 1. 2. > > > add() search_and_reference() > > > { { > > > @@ -79,6 +82,7 @@ search_and_reference() code path. In such cases, the > > > atomic_dec_and_test() may be moved from delete() to el_free() > > > as follows: > > > > > > +CODE LISTING C: > > > 1. 2. > > > add() search_and_reference() > > > { { > > > @@ -114,6 +118,16 @@ element can therefore safely be freed. This in turn guarantees that if > > > any reader finds the element, that reader may safely acquire a reference > > > without checking the value of the reference counter. > > > > > > +A clear advantage of the RCU-based pattern in listing C over the one > > > +in listing B is that any call to search_and_reference() that locates > > > +a given object will succeed in obtaining a reference to that object, > > > +even given a concurrent invocation of delete() for that same object. > > > > This part sounds good to me. > > > > > +Similarly, a call to delete() is not delayed even if there are an > > > +arbitrarily large number of calls to search_and_reference() searching > > > +for the same object that delete() was invoked on. Instead, all that is > > > +delayed is the eventual invocation of kfree(), which is usually not a > > > +problem on modern computer systems, even the small ones. > > > + > > > > small nit: > > This part is common to both listing B and C right? The delete() is never > > delayed due to the search_and_reference in either case, and the kfree is what > > is delayed. My patch was highlighting the difference between the 2 > > listings, but this text says what is common between both listings. > > > > As such I am Ok with the changes you made, and thanks for this document in > > the first place. > > Good point! How about the following patch to be merged into the current > patch? > > Thanx, Paul > > ------------------------------------------------------------------------ > > diff --git a/Documentation/RCU/rcuref.txt b/Documentation/RCU/rcuref.txt > index c0bab7fb57e7..5e6429d66c24 100644 > --- a/Documentation/RCU/rcuref.txt > +++ b/Documentation/RCU/rcuref.txt > @@ -122,11 +122,12 @@ A clear advantage of the RCU-based pattern in listing C over the one > in listing B is that any call to search_and_reference() that locates > a given object will succeed in obtaining a reference to that object, > even given a concurrent invocation of delete() for that same object. > -Similarly, a call to delete() is not delayed even if there are an > -arbitrarily large number of calls to search_and_reference() searching > -for the same object that delete() was invoked on. Instead, all that is > -delayed is the eventual invocation of kfree(), which is usually not a > -problem on modern computer systems, even the small ones. > +Similarly, a clear advantage of both listings B and C over listing A is > +that a call to delete() is not delayed even if there are an arbitrarily > +large number of calls to search_and_reference() searching for the same > +object that delete() was invoked on. Instead, all that is delayed is > +the eventual invocation of kfree(), which is usually not a problem on > +modern computer systems, even the small ones. > > In cases where delete() can sleep, synchronize_rcu() can be called from > delete(), so that el_free() can be subsumed into delete as follows: > This one looks better to me, thanks a lot! Reviewed-by: Joel Fernandes (Google) thanks, - Joel