Received: by 2002:a05:6a10:83d0:0:0:0:0 with SMTP id o16csp239135pxh; Thu, 7 Apr 2022 20:26:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwd0gv/wvFCquafNz2D2BbJcIASnzjaG2b3I9PGX1fH/JZ3C5+zg8xRAKGHW5xd6bvmdIss X-Received: by 2002:a62:82cd:0:b0:505:7ab3:e11b with SMTP id w196-20020a6282cd000000b005057ab3e11bmr1685525pfd.43.1649388367067; Thu, 07 Apr 2022 20:26:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649388367; cv=none; d=google.com; s=arc-20160816; b=wQwBc+FQzJgINghSb3hnPqZN73KS3uuHGaZDbGTa0mfqP/OACASlZmRWNC2gpytzXk hzQ6qiDCWfjz2mpxt77lw2KtrC4xl6eDmVcfbn761+pTj9Jbva5yUlLSTRa12mdnWFis 6tl1xhhmUuxFw5C+td1wu2TJSx34FvN7RXPnHnKiXav8tAPxiCNsCnWVxFNTU8FnqNe6 +uOALqoex8iKlFnIv9CXqYIZ7OIvn3NejYOFyWLKvFEiiEraI7TzqYT2JhNnDb+3ylQz D9taqaaTjdRADjsjpCOULKFoJrYXDtnoFiHnijMzb4E3JJJlyFVQMRP9ZL/xz1jRDvnu d8Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+KACtupekOqWzn/zinGRfIt70LMM9pPdiHf8+KXBdp0=; b=pNK7ofvKSQgKtE7RtbFhjNX05UetBknpJvBO8UMcQHvlDzyzoEkQb9UyD1KQcd2rkY sdYap/T+9V7c2SjbCPcWdIpxGsiOjEQ9OzR+ljDoqVYPNaanHFsqGVaQXozLf2Wfk/It jYkm/AnZ6QPAjiDtfyUTeLzKoOcsvg6ddeNUROk8gvqo7KLdkREaGj3GAMrUexw/lyrJ GElL9Og9Ss18FJ/aT/NFO37fo03Hm59alqSOMKBMoek5fxENHaiFqtxWjWP3iQ9sgVLS 1f8o3nK6VuZPmDfRPp10mIyzMSh/Cou7nHwu79QdC2QhxNCRMRpEffOhOZq7RvcyUY/m Z3Qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=n7GjVukD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 17-20020a621811000000b004fb142acb57si19177183pfy.160.2022.04.07.20.26.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 20:26:07 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=n7GjVukD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BE0632EDF3A; Thu, 7 Apr 2022 19:55:12 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232717AbiDHC5H (ORCPT + 99 others); Thu, 7 Apr 2022 22:57:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233068AbiDHC5F (ORCPT ); Thu, 7 Apr 2022 22:57:05 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C6772E75A2 for ; Thu, 7 Apr 2022 19:55:02 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id bx5so7384686pjb.3 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=dX3O8WlGFTGp6RvJcm9ALN0G+6wLGxxXRiXnhCgicTNGYkuCTKrFsM9/LKJztz0zmw 5osIkrnHs8HH22xfmfJjeiMg7kVzOI2mdV8ldqEpxaUjKx9ok9Um/OYKOU5yOzAO72mf owSxwoj0/M+qTsI0g/Y8V+AaacYvdeAwhoqrlIDJVg6fa6VDLr6HldfNEoaI31Hl475t xwEzLOyyOB+sz9I8S+Y3i9XnZ3+w6qesjtswma/FkVaoxqYK/J85PsG749eQFwZy8572 tfJJQ+VBW5gGBDCEhSY2LAvnnR8n+fdiNmfKMzH3jlgsDTVb3RFvphgpAt+42Evvte6A 8XUw== X-Gm-Message-State: AOAM530FTrpIZj1sG/PjF4FYDpYLzfKoZhy0jYJlItrqX3aZ5kcn+5VO Ltf0d/X971LIrViDsprJlnQEEnHYvIl4oA== 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> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.