Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752546AbdLSU4Z (ORCPT ); Tue, 19 Dec 2017 15:56:25 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:34664 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751171AbdLSU4W (ORCPT ); Tue, 19 Dec 2017 15:56:22 -0500 Date: Tue, 19 Dec 2017 12:56:29 -0800 From: "Paul E. McKenney" To: Jesper Dangaard Brouer Cc: rao.shoaib@oracle.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] kfree_rcu() should use the new kfree_bulk() interface for freeing rcu structures Reply-To: paulmck@linux.vnet.ibm.com References: <1513705948-31072-1-git-send-email-rao.shoaib@oracle.com> <20171219214158.353032f0@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171219214158.353032f0@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-GCONF: 00 x-cbid: 17121920-2213-0000-0000-0000024D6500 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008228; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000244; SDB=6.00962643; UDB=6.00486933; IPR=6.00742637; BA=6.00005752; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00018630; XFM=3.00000015; UTC=2017-12-19 20:56:20 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17121920-2214-0000-0000-0000587DD694 Message-Id: <20171219205629.GH7829@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-12-19_12:,, 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 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1712190297 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4165 Lines: 124 On Tue, Dec 19, 2017 at 09:41:58PM +0100, Jesper Dangaard Brouer wrote: > > On Tue, 19 Dec 2017 09:52:27 -0800 rao.shoaib@oracle.com wrote: > > > +/* Main RCU function that is called to free RCU structures */ > > +static void > > +__rcu_bulk_free(struct rcu_head *head, rcu_callback_t func, int cpu, bool lazy) > > +{ > > + unsigned long offset; > > + void *ptr; > > + struct rcu_bulk_free *rbf; > > + struct rcu_bulk_free_container *rbfc = NULL; > > + > > + rbf = this_cpu_ptr(&cpu_rbf); > > + > > + if (unlikely(!rbf->rbf_init)) { > > + spin_lock_init(&rbf->rbf_lock); > > + rbf->rbf_cpu = smp_processor_id(); > > + rbf->rbf_init = true; > > + } > > + > > + /* hold lock to protect against other cpu's */ > > + spin_lock_bh(&rbf->rbf_lock); > > I'm not sure this will be faster. Having to take a cross CPU lock here > (+ BH-disable) could cause scaling issues. Hopefully this lock will > not be used intensively by other CPUs, right? > > > The current cost of __call_rcu() is a local_irq_save/restore (which is > quite expensive, but doesn't cause cross CPU chatter). > > Later in __rcu_process_callbacks() we have a local_irq_save/restore for > the entire list, plus a per object cost doing local_bh_disable/enable. > And for each object we call __rcu_reclaim(), which in some cases > directly call kfree(). Isn't this lock in a per-CPU object? It -might- go cross-CPU in response to CPU-hotplug operations, but that should be rare. Thanx, Paul > If I had to implement this: I would choose to do the optimization in > __rcu_process_callbacks() create small on-call-stack ptr-array for > kfree_bulk(). I would only optimize the case that call kfree() > directly. In the while(list) loop I would defer calling > __rcu_reclaim() for __is_kfree_rcu_offset(head->func), and instead add > them to the ptr-array (and flush if the array is full in loop, and > kfree_bulk flush after loop). > > The real advantage of kfree_bulk() comes from amortizing the per kfree > (behind-the-scenes) sync cost. There is an additional benefit, because > objects comes from RCU and will hit a slower path in SLUB. The SLUB > allocator is very fast for objects that gets recycled quickly (short > lifetime), non-locked (cpu-local) double-cmpxchg. But slower for > longer-lived/more-outstanding objects, as this hits a slower code-path, > fully locked (cross-cpu) double-cmpxchg. > > > + > > + rbfc = rbf->rbf_container; > > + > > + if (rbfc == NULL) { > > + if (rbf->rbf_cached_container == NULL) { > > + rbf->rbf_container = > > + kmalloc(sizeof(struct rcu_bulk_free_container), > > + GFP_ATOMIC); > > + rbf->rbf_container->rbfc_rbf = rbf; > > + } else { > > + rbf->rbf_container = rbf->rbf_cached_container; > > + rbf->rbf_container->rbfc_rbf = rbf; > > + cmpxchg(&rbf->rbf_cached_container, > > + rbf->rbf_cached_container, NULL); > > + } > > + > > + if (unlikely(rbf->rbf_container == NULL)) { > > + > > + /* Memory allocation failed maintain a list */ > > + > > + head->func = (void *)func; > > + head->next = rbf->rbf_list_head; > > + rbf->rbf_list_head = head; > > + rbf->rbf_list_size++; > > + if (rbf->rbf_list_size == RCU_MAX_ACCUMULATE_SIZE) > > + __rcu_bulk_schedule_list(rbf); > > + > > + goto done; > > + } > > + > > + rbfc = rbf->rbf_container; > > + rbfc->rbfc_entries = 0; > > + > > + if (rbf->rbf_list_head != NULL) > > + __rcu_bulk_schedule_list(rbf); > > + } > > + > > + offset = (unsigned long)func; > > + ptr = (void *)head - offset; > > + > > + rbfc->rbfc_data[rbfc->rbfc_entries++] = ptr; > > + if (rbfc->rbfc_entries == RCU_MAX_ACCUMULATE_SIZE) { > > + > > + WRITE_ONCE(rbf->rbf_container, NULL); > > + spin_unlock_bh(&rbf->rbf_lock); > > + call_rcu(&rbfc->rbfc_rcu, __rcu_bulk_free_impl); > > + return; > > + } > > + > > +done: > > + if (!rbf->rbf_monitor) { > > + > > + call_rcu(&rbf->rbf_rcu, __rcu_bulk_free_monitor); > > + rbf->rbf_monitor = true; > > + } > > + > > + spin_unlock_bh(&rbf->rbf_lock); > > +} > > > -- > Best regards, > Jesper Dangaard Brouer > MSc.CS, Principal Kernel Engineer at Red Hat > LinkedIn: http://www.linkedin.com/in/brouer >