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 E35C4C433F5 for ; Fri, 8 Apr 2022 03:54:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CB4D6B0071; Thu, 7 Apr 2022 23:54:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 57A5E6B0072; Thu, 7 Apr 2022 23:54:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 442386B0074; Thu, 7 Apr 2022 23:54:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0179.hostedemail.com [216.40.44.179]) by kanga.kvack.org (Postfix) with ESMTP id 362446B0071 for ; Thu, 7 Apr 2022 23:54:25 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id E90451836945C for ; Fri, 8 Apr 2022 03:54:24 +0000 (UTC) X-FDA: 79332344448.27.DEC23EB Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf04.hostedemail.com (Postfix) with ESMTP id 6645A40005 for ; Fri, 8 Apr 2022 03:54:24 +0000 (UTC) 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 54735B829AE; Fri, 8 Apr 2022 03:54:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC2C2C385A1; Fri, 8 Apr 2022 03:54:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1649390061; bh=1Y2UYLvXIUmwvQoPN0InwWQ3wXYXh87vwoIWlmTeZV8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=c3movuKx8RCKiLY5fJcN98r6JVGTASOxlDirWlw4KUEfpH3ckiSCbrFoiaq9KPRAe TndtDJBiozSYi/57BS4yeuna6299luYfZiZT1YELCjPfitgqrj7665Amq+vNlau23T xU8bU+XO3O2lkI4K4Ea7W7bCUMqG+O5bOaXP0AVM= Date: Thu, 7 Apr 2022 20:54:19 -0700 From: Andrew Morton To: Qi Zheng Cc: Muchun Song , dennis@kernel.org, tj@kernel.org, cl@linux.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes Message-Id: <20220407205419.f656419a8f4665a2dc781133@linux-foundation.org> In-Reply-To: References: <20220407103335.36885-1-zhengqi.arch@bytedance.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Stat-Signature: igg1rwscq6rcbxb56zykogddyco3qmjh Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=c3movuKx; dmarc=none; spf=pass (imf04.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 6645A40005 X-HE-Tag: 1649390064-257478 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 Fri, 8 Apr 2022 11:50:05 +0800 Qi Zheng wrote: > > > On 2022/4/8 10:54 AM, Muchun Song wrote: > > On Thu, Apr 07, 2022 at 06:33:35PM +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 > >> 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. > >> > >> Signed-off-by: Qi Zheng > > > > Are any users affected by this? If so, I think a Fixes tag > > is necessary. > > Looks all current users(blk_pre_runtime_suspend() and set_in_sync()) are > affected by this. > > I see that this patch has been merged into the mm tree, can Andrew help > me add the following Fixes tag? Andrew is helpful ;) Do you see reasons why we should backport this into -stable trees? It's 8 years old, so my uninformed guess is "no"?