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 01A0FC433F5 for ; Fri, 8 Apr 2022 03:50:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD7FE6B0072; Thu, 7 Apr 2022 23:50:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A872B6B0074; Thu, 7 Apr 2022 23:50:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 892846B0078; Thu, 7 Apr 2022 23:50:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0217.hostedemail.com [216.40.44.217]) by kanga.kvack.org (Postfix) with ESMTP id 654BB6B0074 for ; Thu, 7 Apr 2022 23:50:14 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 0C975ACF4D for ; Fri, 8 Apr 2022 03:50:14 +0000 (UTC) X-FDA: 79332333948.21.A7E13C0 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf05.hostedemail.com (Postfix) with ESMTP id 42F09100003 for ; Fri, 8 Apr 2022 03:50:13 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id ch16-20020a17090af41000b001ca867ef52bso7716718pjb.0 for ; Thu, 07 Apr 2022 20:50:13 -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=Yri1+dD9YTtfEYoo0sek4XbFS6BxTvyBbf5TNk3U90o=; b=WODuTTNn47Q2SBC5Dxw0wpqxb9ydKvbKEXjfJrxlphLYM5jZo75Ao+s4Rp4Jnct4Ri JKRnje18SncYap4SF6hKm1LHj/Jg8MJU2H+lusXwU77kDdI+f6PP+/qsm6kYd1tjKoZC H6lAmkjBN/Mx3Jj9Y3t3HJVdJIkH/ZWSAqJf/IafIxNN8g4yntO9wmPOh2S1tjCwFQN9 bRpodm0/QpjyG4eHxDjVOKLWfn6FQK56DkJi51tdUVDkJIbxIC9+4hjDWa8oE2IgOKQY OazY8XwIprG+ry6nOk4zX4e8QQBd+yU45fnp5+3ynOyC9+mKx6WGq78m/5whGjA4JMIY TLeQ== 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=Yri1+dD9YTtfEYoo0sek4XbFS6BxTvyBbf5TNk3U90o=; b=dxjOkgngjUR291U0GHZqxWWbx/OjKKuulxpDEFAwXS4CVlhXjIQVzfy/keT8ZeZ1fx cs43Z0+IThWP3mTsm/opFynNtOfa1/aP1cnNmydjpG7nBjRWHMnwcWgGAm+ri07/1DWS 7ak5GS5Qpy54UK9dVr8zs/U3sloCbDzsIVYsqqbTLgGBGk/D7MnAqjgAacxHMnud3bg9 wsFcb5UyGpkLpnO6qFyIoz9Kj2DEAic78SpxNYkj39K004TzEmgLDgzeH0JR+kCw8y5C 8bwqj+asf17/Owe2jzHXw8xZ3Cz5n4+iGwDz/9Ps6C+qm7wfr0t8OG97r6i8uJpVX5OQ bRKA== X-Gm-Message-State: AOAM531Cowf+E2V1pnHWHZ484Yb9qDPjYIhqdchr6O0r0/g195D5RWSf RioWvcTVbo+NG1ZZulKD/xzW+Q== X-Google-Smtp-Source: ABdhPJxJhrpv+/5wGqS06IUZyOqAj6WaVRKhTM/L7ukRNxpkiNA7K2w4rulR6PU9LDEe/khCqVIi2w== X-Received: by 2002:a17:90a:6389:b0:1c9:ee11:6c2c with SMTP id f9-20020a17090a638900b001c9ee116c2cmr19697230pjj.107.1649389812176; Thu, 07 Apr 2022 20:50:12 -0700 (PDT) Received: from [10.255.182.146] ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id s31-20020a056a001c5f00b00504e93e536bsm4201026pfw.97.2022.04.07.20.50.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 Apr 2022 20:50:11 -0700 (PDT) Message-ID: Date: Fri, 8 Apr 2022 11:50:05 +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: Muchun Song Cc: dennis@kernel.org, tj@kernel.org, cl@linux.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com References: <20220407103335.36885-1-zhengqi.arch@bytedance.com> From: Qi Zheng In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 42F09100003 X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=WODuTTNn; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com X-Stat-Signature: 8nq85ptjizdcf598xm61rww56ipwtuci X-HE-Tag: 1649389813-109754 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 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? Fixes: 490c79a65708 ("percpu_ref: decouple switching to atomic mode and killing") Thanks, Qi > > The fix LGTM. > > Reviewed-by: Muchun Song > > Thanks. > -- Thanks, Qi