Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp828520yba; Fri, 5 Apr 2019 19:19:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqxMkS5tVIbE/rrDnKTiYcZAp6o1Z24Z6+kYhc3Ze1ngpELKwGbfqiGgVdjEweT659ml/J6Y X-Received: by 2002:a62:b612:: with SMTP id j18mr16472647pff.124.1554517172333; Fri, 05 Apr 2019 19:19:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554517172; cv=none; d=google.com; s=arc-20160816; b=ES8GL4GjzHTg/qfX+DaL0ZMumJNJvbyEz4kPb3RdAaey4tUS4ak1ZCIS9JflTP3wRB f7+IsBK7KvaoYQF7VeHFy8icFoxBIOHfJQ9+nxT8BLmLf01TcyueGOpIqhNTF9A/kgyG HgEuzmX/kUjyouOVWC4T8UroN4FXaPAMCvy8S+N45hjwBojf9PhFY7CkX3YNch4CalXb EDYXAgf//nU7cRDFrscA8zRnM4hSALTvCSAIzfmdA7lzVjWoxOBYFft656dBC8CbQBfv Sgg9/MgzTWQp7Ua92XKMSS+zNqGU7lf17ghMX8Nkpj1ZAhPx5zSawoHfAVlIY+ivDsHM Mvig== 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=mwnDvhRAXQsmRhUTdqUrC7h2+vpZvRJbp0l/Sl+DWPg=; b=etUQNxl5lb8s7QlyeHgJI6sDK2tgweol+Y/48ERltZs100T7OS6n+EWw6l0aU47P9E g+zhSH3cr5GrdtBSReklyyvbNbWKeUjGOauf2Gq2OO2udUSiRVtHzSqPbGM8uT5qqwcW /KlzUUNzBBDGPWz9/8Wnj/CJPJNiT0GkAO3ZTrZIzBxKuyrCmxH3F00qKmkDEPW/c/4v 7VnA/Sd2pDwKxN3DZEmLBF+TpJ6nfyjNkomK55mndsCcNtHMvgg0GZ5eY4s+rIeuhasX UQJyXpiLDdTwp3jNU3mclqo6VEp1nTwvJO42DsiFUpEuubsV4632s/YbwlTAYGNvjd7n riYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=QlPMs8WS; 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 d9si19396545pgp.336.2019.04.05.19.18.47; Fri, 05 Apr 2019 19:19:32 -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=QlPMs8WS; 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 S1726436AbfDFCRL (ORCPT + 99 others); Fri, 5 Apr 2019 22:17:11 -0400 Received: from mail-qt1-f193.google.com ([209.85.160.193]:38255 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725973AbfDFCRL (ORCPT ); Fri, 5 Apr 2019 22:17:11 -0400 Received: by mail-qt1-f193.google.com with SMTP id d13so9659000qth.5 for ; Fri, 05 Apr 2019 19:17:10 -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=mwnDvhRAXQsmRhUTdqUrC7h2+vpZvRJbp0l/Sl+DWPg=; b=QlPMs8WSaGShCE73RlTERVvarOADFKZNiYsM8Vq8+/jCjzbktW8n0KHTnjoGkzTAl0 slWMlC/L64cXhuXWy7qS2FvyljUDJvZ/c1l3BilipbMLIpMIgL3WW62zY4q8NN4yQrGU bdrhgDJlqFEcbmV8a5815Gi86KuM/9bOofOsU= 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=mwnDvhRAXQsmRhUTdqUrC7h2+vpZvRJbp0l/Sl+DWPg=; b=G2hC4ZyGrjZCvkx/QGJQZaDznJpE0C/GL8fPnHMRSoq+hWvB8DEX66GjBphb1hEgro 57mq7z1WDJOm6cUzp54OrgMhB6jWVgCqNkDe7p2klU9hSE9GgO/NrTIuveFZzO4rT2pI ES7Ns0LUTGNxyPJCo9rYnQmnVjYAAew2c6swZdKnPMIkNmEPMz70n8g9PoOJ5+pebMu8 ODGkldPvU7HF60omLn5fouvPRnn7UpdFrXTrDECjY3pijUWBYkR1zpvYVWC1eEarbNep 84HRfVg1b6N18UHPdfADbgke5J0C8YOCXKGBmLEAwlOjJOsBifBvZ4jL/iRZhfRt/9Sr iXug== X-Gm-Message-State: APjAAAXRz60R/4Pp6dyT9T0Qnj8aOX7aQmnhmV9O2u6yzEcyvS0d4G/R 0+LIfhR4s1M6/ZaetYsEEoqb2A== X-Received: by 2002:aed:3ee7:: with SMTP id o36mr14396378qtf.355.1554517029885; Fri, 05 Apr 2019 19:17:09 -0700 (PDT) Received: from localhost (c-73-216-90-110.hsd1.va.comcast.net. [73.216.90.110]) by smtp.gmail.com with ESMTPSA id k4sm7921188qki.15.2019.04.05.19.17.07 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Apr 2019 19:17:08 -0700 (PDT) Date: Sat, 6 Apr 2019 02:17:05 +0000 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: <20190406021705.GA6615@localhost> References: <20190329140555.118463-1-joel@joelfernandes.org> <20190404201039.GL14111@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190404201039.GL14111@linux.ibm.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. thanks, - Joel > In cases where delete() can sleep, synchronize_rcu() can be called from > delete(), so that el_free() can be subsumed into delete as follows: > > @@ -130,3 +144,7 @@ delete() > kfree(el); > ... > } > + > +As additional examples in the kernel, the pattern in listing C is used by > +reference counting of struct pid, while the pattern in listing B is used by > +struct posix_acl. >