Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1598329yba; Thu, 4 Apr 2019 14:08:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqyYrochf7rzWMFmeWmnDO1WxtKxH16RD+OtKW0Xvwr+Z1ddfFgs8mgLTxsAEeTKUJc7fW9D X-Received: by 2002:a62:b40b:: with SMTP id h11mr8269589pfn.133.1554412114049; Thu, 04 Apr 2019 14:08:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554412114; cv=none; d=google.com; s=arc-20160816; b=iquzopl2f1RioBZXhX1G9awtrSy3s+hUxJkeReywNqzvRzr4ljJf0d6mlKMMZX84sP qy7NPezO9ox35L23J014FhBHUrw8KeOy7bAwg8iXXdy863/TeC+OQ9SYW8WH4ZBXlxQk ijM0XranwPZEgHu+A9vp0uek2QgAlMF0dkaXJlGjCdiqtgjadIAJtOwB+I2bRQ38SuUe xXea/+Cm66q9t0pQWrN6CK2BVvQETPF4TXU1ZJMKbk3LYnKMyr8RHNxWl1gnVstbWvPY JOagHhUjFv6JKBP9z4GQPydUC4ZDegxKxDZBIzfN8sW/fCz1QuhGeAEO3qC2f8dLu8tP BYFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:subject:cc:to :from:date; bh=7MaO4RUJl/zLRCBSG3v9yJsPsLayk6tc0BEqqUODmqg=; b=s4ItudBL0sTgwtW5rWwFzRiRW3iQXGKNDUggxAz2FWJXxJl6oUBrU7zLkfnvh6QsSu KbtjhV7XSzOyJ7VZ6wS/KuY403VMUn2v+3a5j0F8RMBbeZn+ut4uhlYIHYqiJMRYjY72 7Dx0eL7pelMyYjWdxQLtm8neKPoO60TUgct3l5yAzq/UAt8o3v+tEmKFR1aVEShVaXu/ iCOA9EE0l9lOomqdKfXRGwDm2Gp7kfdjsOf/FaicZ5Z+pGwc7R04Y2zLUV+6U4N3HozG G9AhcLg5EPKaPSYLHacpfZ5XJAiDW9L32vxFZqSVHEvZgpiPTI10wh+qVSJMiuQdyIi4 IzxQ== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b17si17621801pfj.200.2019.04.04.14.08.15; Thu, 04 Apr 2019 14:08:34 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730658AbfDDULo (ORCPT + 99 others); Thu, 4 Apr 2019 16:11:44 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:35166 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729712AbfDDUKp (ORCPT ); Thu, 4 Apr 2019 16:10:45 -0400 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x34K3Shc028312 for ; Thu, 4 Apr 2019 16:10:43 -0400 Received: from e11.ny.us.ibm.com (e11.ny.us.ibm.com [129.33.205.201]) by mx0b-001b2d01.pphosted.com with ESMTP id 2rnqkkk4e6-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 04 Apr 2019 16:10:43 -0400 Received: from localhost by e11.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 4 Apr 2019 21:10:42 +0100 Received: from b01cxnp23032.gho.pok.ibm.com (9.57.198.27) by e11.ny.us.ibm.com (146.89.104.198) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Thu, 4 Apr 2019 21:10:38 +0100 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp23032.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x34KAbms25231442 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 4 Apr 2019 20:10:37 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BB885B2064; Thu, 4 Apr 2019 20:10:37 +0000 (GMT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8CE4CB205F; Thu, 4 Apr 2019 20:10:37 +0000 (GMT) Received: from paulmck-ThinkPad-W541 (unknown [9.70.82.188]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Thu, 4 Apr 2019 20:10:37 +0000 (GMT) Received: by paulmck-ThinkPad-W541 (Postfix, from userid 1000) id 6EDAE16C2A41; Thu, 4 Apr 2019 13:10:39 -0700 (PDT) Date: Thu, 4 Apr 2019 13:10:39 -0700 From: "Paul E. McKenney" To: "Joel Fernandes (Google)" 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 Reply-To: paulmck@linux.ibm.com References: <20190329140555.118463-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190329140555.118463-1-joel@joelfernandes.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-GCONF: 00 x-cbid: 19040420-2213-0000-0000-000003718407 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00010873; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000284; SDB=6.01184350; UDB=6.00620121; IPR=6.00965103; MB=3.00026298; MTD=3.00000008; XFM=3.00000015; UTC=2019-04-04 20:10:41 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19040420-2214-0000-0000-00005DE7A305 Message-Id: <20190404201039.GL14111@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-04-04_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1904040127 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. +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. + 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.