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 3905CC433EF for ; Fri, 8 Apr 2022 04:06:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 990076B0071; Fri, 8 Apr 2022 00:06:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 93D386B0072; Fri, 8 Apr 2022 00:06:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DE396B0074; Fri, 8 Apr 2022 00:06:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0225.hostedemail.com [216.40.44.225]) by kanga.kvack.org (Postfix) with ESMTP id 6B1F96B0071 for ; Fri, 8 Apr 2022 00:06:29 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 279FD1844E381 for ; Fri, 8 Apr 2022 04:06:29 +0000 (UTC) X-FDA: 79332374898.25.BDD9E64 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf22.hostedemail.com (Postfix) with ESMTP id 82F43C0004 for ; Fri, 8 Apr 2022 04:06:27 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id t4so6753254pgc.1 for ; Thu, 07 Apr 2022 21:06:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=lC/S2KdK6htJ/0XVw5t63OQ6XQqy6Czgh49cIxpFamo=; b=OchzGeA90SPwbI5e/TKwnVYF6eponyzo16YY6YV3WAdcJWFCrwon7p5/SuunSFpTSF KMJAzi6Lh0fDtcXRzyTcxVNSL7SFq45xs3CoV+T024uucXl8Sv0cBI5SiTSRTIHI2fPd IL/O9XUux2/dpn6KIl+ImaLl7gsyt/dDmIUfpKKJGlq2Vlcvwh48Lt5W9gWGMxhMnx6j JeXIiryUCutyaqmkgvLrNiHpi7OHkuTjHDPstovLtzwCksu01qbZuuivZtXv8jfKEAcD XbbkhA/0KYmjIR4LlSqdK69OPJdfOLj8Hz5dz1h+WJnQAB7ZfC5KCYx9i5LnRTHO881J HNMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=lC/S2KdK6htJ/0XVw5t63OQ6XQqy6Czgh49cIxpFamo=; b=udnMcvjgG3GvdZDf0EOzHQES2xrr9wt84n6PNb5dmk2fSHuRnPX5rUYvlXV1OK/G8w Wxk9CBI6xkDahGrJGljIgF9fIq4UcT/Eo4KldkEn6/EHkDFIQl6ve4fLcxUhMpuD43Fa bLIawo5drYLIyCpK58I7SO1HgJ/6FBxpw6Y3sZ1BE1WOzxKcpjs2RZoh2KGg6Ni2rDDh nPf5xj8QuuDaMvEP2JhLu/FOy3t9GNt/Bt81mxsqBSC4m8GvXJC0Qk4vnft0Nqcy4ASf zzmH0K7djH8bqJBO/37HJGRJGBWeuAtzLN1wAr6xEXvVXeJNFPBTyy6kso1nxS8RRD9L aggg== X-Gm-Message-State: AOAM531aQNMUOcHtcor9zZ9IW+QDkrFHkJQ719zdLOLuWU/BtSk/zD8W /GP+KDQCwxT5tkDJGlafvsw8aw== X-Google-Smtp-Source: ABdhPJxwbpowte1lbWo5ckVfns8sKuidwHES4wRQvJgShvpcBXb7aUjn412jk2xtuJW/r4/YkGhJhw== X-Received: by 2002:aa7:8154:0:b0:505:68a6:600d with SMTP id d20-20020aa78154000000b0050568a6600dmr6000019pfn.35.1649390786223; Thu, 07 Apr 2022 21:06:26 -0700 (PDT) Received: from [10.255.182.146] ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id u19-20020a056a00125300b004fafa43330csm23723547pfi.163.2022.04.07.21.06.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 Apr 2022 21:06:25 -0700 (PDT) Message-ID: <35195a61-d531-aeb2-5565-146e345f8bf6@bytedance.com> Date: Fri, 8 Apr 2022 12:06:20 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes Content-Language: en-US To: Andrew Morton Cc: Muchun Song , dennis@kernel.org, tj@kernel.org, cl@linux.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com References: <20220407103335.36885-1-zhengqi.arch@bytedance.com> <20220407205419.f656419a8f4665a2dc781133@linux-foundation.org> From: Qi Zheng In-Reply-To: <20220407205419.f656419a8f4665a2dc781133@linux-foundation.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=OchzGeA9; spf=pass (imf22.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 82F43C0004 X-Stat-Signature: 7b1ioqk4j33oertsatgzctwtiwcxd4eq X-HE-Tag: 1649390787-51430 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 2022/4/8 11:54 AM, Andrew Morton wrote: > 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"? Hmm, although the commit 490c79a65708 add wake_up_all(), it is no problem for the usage at that time, maybe the correct Fixes tag is the following: Fixes: 210f7cdcf088 ("percpu-refcount: support synchronous switch to atomic mode.") But in fact, there is no problem with it, but all current users expect the refcount is stable after percpu_ref_switch_to_atomic_sync() returns. I have no idea as which Fixes tag to add. > -- Thanks, Qi