Received: by 2002:a05:6a10:83d0:0:0:0:0 with SMTP id o16csp182433pxh; Thu, 7 Apr 2022 18:09:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzR27JTLs6uBc69KImD0SuiOLIo3leklJiV4awJEc4cz/LzLoVoPrejBSyLIBmIvqzc4PeD X-Received: by 2002:a05:6a00:440c:b0:4fa:da3f:251c with SMTP id br12-20020a056a00440c00b004fada3f251cmr16897785pfb.73.1649380149298; Thu, 07 Apr 2022 18:09:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649380149; cv=none; d=google.com; s=arc-20160816; b=Y1THznQAIGCVF8mP+Kvgf0XW+xDA+w0ARIVG/kIKpCVcp/Ei3Gs3gHR1riUpDClKYY m9kelJny9XyT0HIG6xhcZVEoDhUR1b6+7PL11TWv4jRpt/+hFW2fHCzTyfszp8E4olFj fm5HuZjN/4486TdVVZlKc+xZHld013Y2xojhyzmZpmDw85EarbTEZDMbmRIxufSw3pLW 4zF4xAUmho3vAPodj0ewx66/jjV3nu4KltFSUw4d0giU6C3FOiJJiup1b9XGL15cZ+fN DFUP9TuiWbgWHQjkdFmK3mVpKpGiHstungW3dN49b3wFjMqsGpFJnn4OZaf6zRoODHji 14Hg== 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; bh=0Xk1+fs4EReZHznvydunkPsq1TyAFPHWXf8m9Tth19E=; b=GNcWVRflFosLP26aMeHGb3ACrbToSoJVwDcOj27oc4bUg322mF8bBRBTbAFKeZcpRH rhyqZRXZueiDGlFjBVfFKFUWnJPfOgl1qiJ/8cOApvKuyaDYW1S6OH22UucIN+BH0Uvq Yd9CMW+o9o2owDhX0nEjt/Nf3V251zkWDa6DzHE7mS30MbQjRKS+LkeeTj/IUYuZEb5H O2e5Tc+DAYoskll7Xp22bIYNOwj3rzZ0/pkbEs4blzomUBpLGTukN09h6ieCBk1Z3vQ1 d84gtumkOObdJAsUvYETpldnE2Ga5z92eRJe50JhGHPEEbvwdpaz76t2gslFb7I5DtAx FNvg== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l189-20020a633ec6000000b003980aecb0d8si20133765pga.556.2022.04.07.18.09.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 18:09:09 -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; 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=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 97BCC176642; Thu, 7 Apr 2022 17:39:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233008AbiDHAlt (ORCPT + 99 others); Thu, 7 Apr 2022 20:41:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232174AbiDHAls (ORCPT ); Thu, 7 Apr 2022 20:41:48 -0400 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13002200 for ; Thu, 7 Apr 2022 17:39:47 -0700 (PDT) Received: by mail-pg1-f178.google.com with SMTP id 125so6374464pgc.11 for ; Thu, 07 Apr 2022 17:39:47 -0700 (PDT) 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=0Xk1+fs4EReZHznvydunkPsq1TyAFPHWXf8m9Tth19E=; b=fI5tUm7ynALpjJ8wDQj5vFRDAYLzHnU5gAS4asSzYXKeN+XyPXfKmi0RYelE/q26jB EkU4Y/N0mLVwh80d7bDmgjTPdfkCB6q55thNrgVNZYe/ubEKN9eBDHUZMwNGQgvhx5eu 0UG2Dy4QaAqaFvqlBjKnppf2blRCdWXSdMZY8MLQPQzEKTLpdzg5EKZ/N8L+Y9oncsp4 rqogK0z8jTcHm6dwUhecbFJ5wHUvhizjH0Di0Ru/hg4/RtTSYBIYiE9IUtobsAbiq1NW 5gXORgUybpWIntDpO3ArreFtf/aylajof6TO1Zla5t0WTgNSOkxTKU1XYhCk4z7iAUNA teCw== X-Gm-Message-State: AOAM5314Kfd+gpY0j07BgpnAtjaE3BxfP4rtVD+O0H34/hMQ/vouDQgB qJKKMt45d8OE28gffKsC9Ys= X-Received: by 2002:a05:6a00:3018:b0:4fa:d533:45e5 with SMTP id ay24-20020a056a00301800b004fad53345e5mr16703828pfb.13.1649378386475; Thu, 07 Apr 2022 17:39:46 -0700 (PDT) Received: from fedora (136-24-99-118.cab.webpass.net. [136.24.99.118]) by smtp.gmail.com with ESMTPSA id k11-20020a056a00168b00b004f7e1555538sm23597254pfc.190.2022.04.07.17.39.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 17:39:46 -0700 (PDT) Date: Thu, 7 Apr 2022 17:39:43 -0700 From: Dennis Zhou To: Andrew Morton Cc: Qi Zheng , tj@kernel.org, cl@linux.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, songmuchun@bytedance.com, Ming Lei Subject: Re: [PATCH] percpu_ref: call wake_up_all() after percpu_ref_put() completes Message-ID: References: <20220407103335.36885-1-zhengqi.arch@bytedance.com> <20220407155752.769632b737f79b038cf83742@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220407155752.769632b737f79b038cf83742@linux-foundation.org> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,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 Hello, On Thu, Apr 07, 2022 at 03:57:52PM -0700, Andrew Morton wrote: > (cc Ming Lei) > > On Thu, 7 Apr 2022 18:33:35 +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. > > Thanks. I'll grab this, but shall await input from others before doing > anything else with it. > Seems right to me. The percpu_ref protects the __percpu_ref_exit(), not the waiters. Acked-by: Dennis Zhou Thanks, Dennis > > Signed-off-by: Qi Zheng > > +++ b/lib/percpu-refcount.c > > @@ -154,13 +154,14 @@ static void percpu_ref_call_confirm_rcu(struct rcu_head *rcu) > > > > data->confirm_switch(ref); > > data->confirm_switch = NULL; > > - wake_up_all(&percpu_ref_switch_waitq); > > > > if (!data->allow_reinit) > > __percpu_ref_exit(ref); > > > > /* drop ref from percpu_ref_switch_to_atomic() */ > > percpu_ref_put(ref); > > + > > + wake_up_all(&percpu_ref_switch_waitq); > > } > > > > static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu) >