Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2439693rwl; Thu, 30 Mar 2023 10:16:37 -0700 (PDT) X-Google-Smtp-Source: AKy350YrY+4+8ZyvlFpZTf7CEU+jxZxz7O7n6vB9gM0ekOkWgY1M6LWWH7wvf232Mn47FqPokXya X-Received: by 2002:a05:6402:2811:b0:4bb:c3ce:63fc with SMTP id h17-20020a056402281100b004bbc3ce63fcmr27769616ede.3.1680196596861; Thu, 30 Mar 2023 10:16:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680196596; cv=none; d=google.com; s=arc-20160816; b=mYsjBOd4XzZTVT4epC6VSZvpTUj172APSN4y7CFooLThuvWEk1MUoTs8Jo4CNlqAjQ k3SuKu0GNZGVJ1332ZfyXUQWV2IqP/TFjU1nyYoiI6XkeSHjd/23TwIlPHiXRZTsowmk vSbz/6ZRnLQx4YHCCYsZCfI2XI5qL3sBwDIhwztMzz1wXxD9ZhE1R3pXZtGwNpWNil+C czPEktXDz9q4C9m38MPKxiR/lppQwJZk49RznqQDBAhiofYi6Dyvoioqd0f+VU3ddWzk mcCEoynZrC/g+T0g7DzCtoKM6XFvkYeVn+srQBr0r8Ku7VKuy78/LPKquh2T/y+T2/6/ TZgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=0d738d+QC3UXs8nIoFerybvhYve6bkmqD3DY2z0w8Pc=; b=AX98w1JXrdYGugpqeVup5WHHmNfPuXaZ+YeLp2mwhUpRFDrYDSRDA0IIM5ygKamqSt a6vRYGa4P9bB+5ibtjhoqYzkTfQ/ynv9RqUuX77z398Z9Xxvy7S+lwwJbZB0xYlsV8gj 36HdY/SgZh2GBz8VtQq3jliTPQ4yjX1qZ4MD8H84hS+2WDMVEXcMO6Ch6OlkWKvFOSvy 3htTo+FR8BSN3YPP2TzChCDTQUnpBT5/2E6vv0DOA5Cv/0sg+1gfqdEutAj+41dJvVMc 3d5v4EpmN6nko5sgeKzNNgINsBBpbrxz186eFNENHiBWQlNs69TX1f6LRJaN7fAy2IVE uwKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XlCrStym; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay21-20020a056402203500b005021f0d5753si183656edb.666.2023.03.30.10.15.48; Thu, 30 Mar 2023 10:16:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XlCrStym; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230427AbjC3RFV (ORCPT + 99 others); Thu, 30 Mar 2023 13:05:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229470AbjC3RFT (ORCPT ); Thu, 30 Mar 2023 13:05:19 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 221CF10A; Thu, 30 Mar 2023 10:05:18 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id ACEC1B8261D; Thu, 30 Mar 2023 17:05:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54AEBC433D2; Thu, 30 Mar 2023 17:05:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680195915; bh=EF30fAGyUZun/5gZWGNZKE6r2K5Z9qpggd2xNHSKj4I=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=XlCrStym7zoiXAcuFy+ovUGRXaVfqwvoq8MGSyf7SRQptyXV8wK1+h3l+QQdSkITf Z+K3RzdeMKyaBDSGIVl+yVDGzwSvG5WTWFWlRLuhHho+nWqMjlUU4B4XTk8BygAZRQ 15BiPcMO+k7IrIDb1rDE256+j3M0GyPch91wApeNSniR35re9vOp3spjvbFI5oMWiT /qVC+WhrGCko00wypoT0XhGqx2bIJudVqjjoCC46VWJcj39xs+SOxp+caHpQeZ2zRK wUCT84Cq/kLgmrT2klv7uDdHuaegjZQQnTZ9i6An395PUzK46C7NathKSADecOpXJW yUyw8/DrM0uZQ== Received: by paulmck-ThinkPad-P72.home (Postfix, from userid 1000) id E91E21540476; Thu, 30 Mar 2023 10:05:14 -0700 (PDT) Date: Thu, 30 Mar 2023 10:05:14 -0700 From: "Paul E. McKenney" To: "Zhang, Qiang1" Cc: "rcu@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kernel-team@meta.com" , "rostedt@goodmis.org" , "hch@lst.de" , "qiang.zhang1211@gmail.com" Subject: Re: [PATCH RFC rcu 02/19] srcu: Use static init for statically allocated in-module srcu_struct Message-ID: <2d15d553-4c58-4f71-aad8-681d3168c2b1@paulmck-laptop> Reply-To: paulmck@kernel.org References: <3db82572-f156-4a5d-b711-841aa28bd996@paulmck-laptop> <20230324001938.3443499-2-paulmck@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-5.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 30, 2023 at 03:20:15PM +0000, Zhang, Qiang1 wrote: > > >Further shrinking the srcu_struct structure is eased by requiring > > >that in-module srcu_struct structures rely more heavily on static > > >initialization. In particular, this preserves the property that a > > >module-load-time srcu_struct initialization can fail only due to > > >memory-allocation failure of the per-CPU srcu_data structures. > > >It might also slightly improve robustness by keeping the number of > > >memory allocations that must succeed down percpu_alloc() call. > > > > > >This is in preparation for splitting an srcu_usage structure out of > > >the srcu_struct structure. > > > > > >[ paulmck: Fold in qiang1.zhang@intel.com feedback. ] > > > > > >Signed-off-by: Paul E. McKenney > > >Cc: Christoph Hellwig > > >--- > > > include/linux/srcutree.h | 19 ++++++++++++++----- > > > kernel/rcu/srcutree.c | 19 +++++++++++++------ > > > 2 files changed, 27 insertions(+), 11 deletions(-) > > > > > >diff --git a/include/linux/srcutree.h b/include/linux/srcutree.h > > >index ac8af12f93b3..428480152375 100644 > > >--- a/include/linux/srcutree.h > > >+++ b/include/linux/srcutree.h > > >@@ -121,15 +121,24 @@ struct srcu_struct { > > > #define SRCU_STATE_SCAN1 1 > > > #define SRCU_STATE_SCAN2 2 > > > > > >-#define __SRCU_STRUCT_INIT(name, pcpu_name) \ > > >-{ \ > > >- .sda = &pcpu_name, \ > > >+#define __SRCU_STRUCT_INIT_COMMON(name) \ > > > .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ > > > .srcu_gp_seq_needed = -1UL, \ > > > .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \ > > >- __SRCU_DEP_MAP_INIT(name) \ > > >+ __SRCU_DEP_MAP_INIT(name) > > >+ > > >+#define __SRCU_STRUCT_INIT_MODULE(name) \ > > >+{ \ > > >+ __SRCU_STRUCT_INIT_COMMON(name) \ > > > } > > > > > >+#define __SRCU_STRUCT_INIT(name, pcpu_name) \ > > >+{ \ > > >+ .sda = &pcpu_name, \ > > >+ __SRCU_STRUCT_INIT_COMMON(name) \ > > >+} > > >+ > > >+ > > > /* > > > * Define and initialize a srcu struct at build time. > > > * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it. > > >@@ -151,7 +160,7 @@ struct srcu_struct { > > > */ > > > #ifdef MODULE > > > # define __DEFINE_SRCU(name, is_static) \ > > >- is_static struct srcu_struct name; \ > > >+ is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name); \ > > > extern struct srcu_struct * const __srcu_struct_##name; \ > > > struct srcu_struct * const __srcu_struct_##name \ > > > __section("___srcu_struct_ptrs") = &name diff --git > > >a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c index > > >cd46fe063e50..7a6d9452a5d0 100644 > > >--- a/kernel/rcu/srcutree.c > > >+++ b/kernel/rcu/srcutree.c > > >@@ -1895,13 +1895,14 @@ void __init srcu_init(void) static int > > >srcu_module_coming(struct module *mod) { > > > int i; > > >+ struct srcu_struct *ssp; > > > struct srcu_struct **sspp = mod->srcu_struct_ptrs; > > >- int ret; > > > > > > for (i = 0; i < mod->num_srcu_structs; i++) { > > >- ret = init_srcu_struct(*(sspp++)); > > >- if (WARN_ON_ONCE(ret)) > > >- return ret; > > >+ ssp = *(sspp++); > > >+ ssp->sda = alloc_percpu(struct srcu_data); > > >+ if (WARN_ON_ONCE(!ssp->sda)) > > >+ return -ENOMEM; > > > } > > > return 0; > > > } > > >@@ -1910,10 +1911,16 @@ static int srcu_module_coming(struct module > > >*mod) static void srcu_module_going(struct module *mod) { > > > int i; > > >+ struct srcu_struct *ssp; > > > struct srcu_struct **sspp = mod->srcu_struct_ptrs; > > > > > >- for (i = 0; i < mod->num_srcu_structs; i++) > > >- cleanup_srcu_struct(*(sspp++)); > > >+ for (i = 0; i < mod->num_srcu_structs; i++) { > > >+ ssp = *(sspp++); > > >+ if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) && > > >+ !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static)) > > >+ cleanup_srcu_struct(ssp); > > >+ free_percpu(ssp->sda); > > > > > > Hi Paul > > > > About 037b80b8865fb ("srcu: Check for readers at module-exit time ") > > > > --- a/kernel/rcu/srcutree.c > > +++ b/kernel/rcu/srcutree.c > > @@ -1911,7 +1911,8 @@ static void srcu_module_going(struct module *mod) > > if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) && > > !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static)) > > cleanup_srcu_struct(ssp); > > > The srcu_sup->sda_is_static is true, in cleanup_srcu_struct(), the ssp->sda can not be freed. Very good, thank you! I will fold your suggested fix into this commit: 037b80b8865f ("srcu: Check for readers at module-exit time") Thanx, Paul > > - free_percpu(ssp->sda); > > + else if (!WARN_ON(srcu_readers_active(ssp))) > > + free_percpu(ssp->sda); > > > > Should the else statement be removed? like this: > > > > if (!WARN_ON(srcu_readers_active(ssp))) > > free_percpu(ssp->sda); > > > >Mightn't that cause us to double-free ssp->sda? Once in free_percpu(), > >and before that in cleanup_srcu_struct()? > > > how about this? any thought? > > --- a/kernel/rcu/srcutree.c > +++ b/kernel/rcu/srcutree.c > @@ -1937,7 +1937,7 @@ static void srcu_module_going(struct module *mod) > if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) && > !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static)) > cleanup_srcu_struct(ssp); > - else if (!WARN_ON(srcu_readers_active(ssp))) > + if (!WARN_ON(srcu_readers_active(ssp))) > free_percpu(ssp->sda); > } > } > > Thanks > Zqiang > > > > > Thanx, Paul > > > > Thanks > > Zqiang > > > > > > >+ } > > > } > > > > > > /* Handle one module, either coming or going. */ > > >-- > > >2.40.0.rc2 > > >