From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8777C433F5 for ; Fri, 8 Apr 2022 01:41:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0916C6B0071; Thu, 7 Apr 2022 21:41:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0418D6B0072; Thu, 7 Apr 2022 21:41:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E23B96B0074; Thu, 7 Apr 2022 21:41:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id CF1406B0071 for ; Thu, 7 Apr 2022 21:41:08 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A2576253C1 for ; Fri, 8 Apr 2022 01:41:08 +0000 (UTC) X-FDA: 79332008616.10.295BB35 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 28CDA140004 for ; Fri, 8 Apr 2022 01:41:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1649382067; 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=Ktpmzwd74u7ANhLMgaAqSNx1e5RS0CZUCGfDeAq/I89wYgTBlW59+rFVMc8SgpFCRFy8U+ IcYBE8jAK/U8HeOWD0cu+98TTgd+8GSqvZMl3CKK/UYio//HYJtPnj8j/f9oKF2D523Yim WuF+e6kdmBrBOB4fWs5Ovsus3qxHFls= 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-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 28CDA140004 X-Rspam-User: Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ktpmzwd7; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf23.hostedemail.com: domain of ming.lei@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=ming.lei@redhat.com X-Stat-Signature: wehpmhgkcdeecurrkxge7tjtf66nsjyk X-HE-Tag: 1649382067-184726 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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