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 7FCD9C433F5 for ; Fri, 8 Apr 2022 02:55:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F0D466B0071; Thu, 7 Apr 2022 22:55:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EBE796B0072; Thu, 7 Apr 2022 22:55:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5D6A6B0074; Thu, 7 Apr 2022 22:55:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id C3F306B0071 for ; Thu, 7 Apr 2022 22:55:04 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7A1C560510 for ; Fri, 8 Apr 2022 02:55:04 +0000 (UTC) X-FDA: 79332194928.10.BE52966 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf24.hostedemail.com (Postfix) with ESMTP id 567C7180002 for ; Fri, 8 Apr 2022 02:55:03 +0000 (UTC) Received: by mail-pl1-f178.google.com with SMTP id y6so6753606plg.2 for ; Thu, 07 Apr 2022 19:55:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=+KACtupekOqWzn/zinGRfIt70LMM9pPdiHf8+KXBdp0=; b=n7GjVukDtuF8wAOKxI5qi0lfKNZjaZviP4zJsFr2zNsA54fhegeTNLDWn0mRLGhiMg PUPMcfQibs8eMDRCLfF8o1fx82nmrzJR4GCh/jMm0LQ5n5ZpkioIBjhhlVo0NwwtQmGc doyZxx9JMouPj0v8LMg9TnB2LDLqZzzehJVbDadUVP1zLr8OVwBGzBxEQJqGHzoxKy8h NA5xb2Tni5qDsJGLbS5kRdDFYv1VoEip6UMNGRmtv9moEBM08t5WRDIutRCzH99rb+Gt 8JWDvfWFwN5VpUPnfdHgz9TuXM8kE+eIVzymmM60CGD+5W4hV02O5koTh8dgwoqMogJ/ ouLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=+KACtupekOqWzn/zinGRfIt70LMM9pPdiHf8+KXBdp0=; b=3BNashjuwunm9xKiUm6wtT9bgrg13w2WoUiR1P3ecslPsqqRIVW9tkvocFXLXgcT7p 19CmW8e2DxyYSNfEabwMfMRK2EKNZcQ8ahpF224o8UHWt8laxIlroTM3wLjmIZYFOQV3 YD/6ZDVi/YGPRmNqQtejaCmBhsRVrj5wiGT7+4Q1nCkf8lvCs/GQdqYsJUtWguQaXEG1 tvpphUweycq3rWs0n+zsuspblvo8vWPQ4+ESlpKJSR9vs/CPknVoRpBE9klt26V34TtN dNXNFtWj6mVWMFSYGoNVaykgSEoPTRiWOG5gMwhShO7E2XxNeJS6ZwaB3NF8RsiXmzSR 7v/A== X-Gm-Message-State: AOAM531bdmgq1FtAUkOzn7Zn8HBEOL0y9yD9Pbejl17xs6mv/OaU+1Kq WegnVNt/boAPJqQsegh3JHaydQ== X-Google-Smtp-Source: ABdhPJyrQu1b1JnWN5WneWpC+wVNYkvXnuCgz09mBClKlkOltg4Mi6CvPV4uv98tS268nP+zzWMNSw== X-Received: by 2002:a17:902:b684:b0:156:80b4:db03 with SMTP id c4-20020a170902b68400b0015680b4db03mr17270771pls.16.1649386501966; Thu, 07 Apr 2022 19:55:01 -0700 (PDT) Received: from localhost ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id b16-20020a056a00115000b004f6ff260c9esm23639716pfm.207.2022.04.07.19.55.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 19:55:01 -0700 (PDT) Date: Fri, 8 Apr 2022 10:54:57 +0800 From: Muchun Song To: Qi Zheng 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 Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes Message-ID: References: <20220407103335.36885-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220407103335.36885-1-zhengqi.arch@bytedance.com> Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=n7GjVukD; spf=pass (imf24.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 567C7180002 X-Stat-Signature: skbzwtaea3zye7ucm54gqo8sx5bo3khb X-HE-Tag: 1649386503-442710 X-Bogosity: Ham, tests=bogofilter, spamicity=0.001157, 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 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. The fix LGTM. Reviewed-by: Muchun Song Thanks.