Received: by 2002:a05:6a10:83d0:0:0:0:0 with SMTP id o16csp209978pxh; Thu, 7 Apr 2022 19:08:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1J54jcvuG1kqO67lnlpIKrZPO0uYvqH+XJj8L4AE8w+M1mrT8Ln5xrB0NUBb3ZRW0KzMK X-Received: by 2002:a17:903:2291:b0:157:110f:1b34 with SMTP id b17-20020a170903229100b00157110f1b34mr4791486plh.64.1649383731537; Thu, 07 Apr 2022 19:08:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649383731; cv=none; d=google.com; s=arc-20160816; b=NOoa7i2IbEWoTsvMz5y2HkTdVrHktMWT/ZRV9jHA/IAGHCTlC98gk1e3n1bXeWESDX FQNmR2mo/Su2DHu+6tKQk1S4q1aZN714fl/NuAf/tGriFCSaiMmzrEU5sSRDxQ6wkQor B6CmZWvEsRevLTHxtQCIknrD1SS79z0srC6LN58x8sQWgi0+2o0iZXr/ZzuR7Z/2rg6q YJBMwMd6LjWT05oXE8pKheZu6NGuyLAHwazMsGKkFkygGQeuntRrK1i10YHQNpWP2QTE hLBFu6bPRe5imhKo6K9/EuX9dYlzzj28PM1qZnHvr+jw8wc/XBCPmD9kBDA+L62lOWOW Q1gQ== 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:message-id:subject:cc:to:from:date:dkim-signature; bh=t9gnNFA0wDN5XfoET2ND9SZc9CvlqYkCyfw7UidpYlY=; b=uaXSkmzMSPPqOvb49qouBdeoku+a1Q2YNmrWLAEcmbr+ndxGCTe+viWCJqJK55JlSx p7zUolL85xvsdaMbiZGbEWUFTgBJJEo3IOpXIXjlrK1ZkpHXD6tPCTtLbCtQv/lARZtS my68A8aO1DYyPvYMJGmLKaHptH+ZBmDnvXgzAvfIEoUXnpk/SZc7RJNo7/qNoDSFkS2F /PNS3fjy1h6Wk/h4PkemoHy1sBz5DvUv0Xx0pRX74X3//tq+HsHM8fU3GDMu7wSbm0zb W36es2GUCD6wJ+ka7uhuDnvWH1/kmv3lZrWXcEpD2Md+8KDj0DOqS8S+sOyjcZ40eLxc oLSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iNXDe2IY; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id lp12-20020a17090b4a8c00b001ca56c5e70asi3948725pjb.177.2022.04.07.19.08.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 19:08:51 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iNXDe2IY; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E0F5A12C265; Thu, 7 Apr 2022 18:41:14 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233523AbiDHBnK (ORCPT + 99 others); Thu, 7 Apr 2022 21:43:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbiDHBnI (ORCPT ); Thu, 7 Apr 2022 21:43:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 23BE0AA027 for ; Thu, 7 Apr 2022 18:41:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649382065; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=t9gnNFA0wDN5XfoET2ND9SZc9CvlqYkCyfw7UidpYlY=; b=iNXDe2IYu4o4WQeYiffu7DMFZMk/9xuEBlGzjOqKxSALGqI1kA1C0pOV5twEQ4Yi3z0Sum bhOLoznT2R4LbWHMvm+0ah4Jq2xU/9U4RffDRzkevvc3dpdHl0hLsU6rSHHrIucfY8Q1H1 iNQj+RP5iaFJ7BWR7CjTzp3nRoJhK7Q= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-490-HIssGxB6OIm6H1aChm_HDA-1; Thu, 07 Apr 2022 21:41:02 -0400 X-MC-Unique: HIssGxB6OIm6H1aChm_HDA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A12ED3C01C15; Fri, 8 Apr 2022 01:41:01 +0000 (UTC) Received: from T590 (ovpn-8-29.pek2.redhat.com [10.72.8.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A31DE1415136; Fri, 8 Apr 2022 01:40:54 +0000 (UTC) Date: Fri, 8 Apr 2022 09:40:49 +0800 From: Ming Lei To: Andrew Morton Cc: Qi Zheng , dennis@kernel.org, tj@kernel.org, cl@linux.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, songmuchun@bytedance.com Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes Message-ID: References: <20220407103335.36885-1-zhengqi.arch@bytedance.com> <20220407155752.769632b737f79b038cf83742@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220407155752.769632b737f79b038cf83742@linux-foundation.org> X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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, Apr 07, 2022 at 03:57:52PM -0700, Andrew Morton wrote: > (cc Ming Lei) > > On Thu, 7 Apr 2022 18:33:35 +0800 Qi Zheng wrote: > > > In the percpu_ref_call_confirm_rcu(), we call the wake_up_all() > > before calling percpu_ref_put(), which will cause the value of > > percpu_ref to be unstable when percpu_ref_switch_to_atomic_sync() > > returns. > > > > CPU0 CPU1 > > > > percpu_ref_switch_to_atomic_sync(&ref) > > --> percpu_ref_switch_to_atomic(&ref) > > --> percpu_ref_get(ref); /* put after confirmation */ > > call_rcu(&ref->data->rcu, percpu_ref_switch_to_atomic_rcu); > > > > percpu_ref_switch_to_atomic_rcu > > --> percpu_ref_call_confirm_rcu > > --> data->confirm_switch = NULL; > > wake_up_all(&percpu_ref_switch_waitq); > > > > /* here waiting to wake up */ > > wait_event(percpu_ref_switch_waitq, !ref->data->confirm_switch); > > (A)percpu_ref_put(ref); > > /* The value of &ref is unstable! */ > > percpu_ref_is_zero(&ref) > > (B)percpu_ref_put(ref); > > > > As shown above, assuming that the counts on each cpu add up to 0 before > > calling percpu_ref_switch_to_atomic_sync(), we expect that after switching > > to atomic mode, percpu_ref_is_zero() can return true. But actually it will Looks all current users expect the refcount is stable after percpu_ref_switch_to_atomic_sync returns, even though the API itself doesn't mention the point explicitly. > > return different values in the two cases of A and B, which is not what > > we expected. > > > > Maybe the original purpose of percpu_ref_switch_to_atomic_sync() is > > just to ensure that the conversion to atomic mode is completed, but it > > should not return with an extra reference count. > > > > Calling wake_up_all() after percpu_ref_put() ensures that the value of > > percpu_ref is stable after percpu_ref_switch_to_atomic_sync() returns. > > So just do it. > > Thanks. I'll grab this, but shall await input from others before doing > anything else with it. > > > Signed-off-by: Qi Zheng > > +++ b/lib/percpu-refcount.c > > @@ -154,13 +154,14 @@ static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu) > > > > data->confirm_switch(ref); > > data->confirm_switch = NULL; > > - wake_up_all(&percpu_ref_switch_waitq); > > > > if (!data->allow_reinit) > > __percpu_ref_exit(ref); > > > > /* drop ref from percpu_ref_switch_to_atomic() */ > > percpu_ref_put(ref); > > + > > + wake_up_all(&percpu_ref_switch_waitq); > > } Looks fine: Reviewed-by: Ming Lei Thanks, Ming