Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1022022rwb; Wed, 14 Dec 2022 05:47:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf6kF7FaDLKdy/Xv39H/3qAmr1b+dS1F4DaLeBJPhgq1FpJz2YJuQkHXvm5weHEAnPyvwizQ X-Received: by 2002:a17:906:28c7:b0:7c1:2478:a4d8 with SMTP id p7-20020a17090628c700b007c12478a4d8mr12008863ejd.54.1671025661194; Wed, 14 Dec 2022 05:47:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671025661; cv=none; d=google.com; s=arc-20160816; b=sbkOnA8MYqr6IEwvUZD/kn4m29CpRD9dGFYnn72jOXZm3o2YqTZBORqqb6GnZlVBFr nOHfB76QmPdH6fB0d8CF/3uJRRxHwIh+OXwYgCQk445qtB1+b9b7IP5bT+0rznPLS5LU uBKYvo0+wgZi18986E8TsAW1mhfjjPc2lYbikILHEXSSN/Ggy0/eqU77paG07ArM+50S nvS6RY4Ii756DFg2m+fzkbOraHtssD0MaEhMX1Wl3feLOcanZK61h5XmrgB0sg+O6XOI SzDYTy4yNHOOC9gSAJ9rRWF7YHwmTZnqiDIPy+gpNjayZKO/rTESYGyNsmU9vKkWus5M dikA== 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=p5nDSovtGcMOmECeVBK2QeZhfWdznopYC9ScdOLMtVw=; b=s1p4dyavDLE064Hi0Zv/aasODkHRbgYAQ0MfplxiyG/fTCwv+4ko3BPkh471W9rPwp bbbuSy82ZMLGXodLBm0l+Yi6OvbpycLSxqw/2cEXADUKRkQmunvu8re5LUX/mVRhRoLb CyJd/xUEcbAKlWr2f4g1+6ePsJ/UeV3sjxvbMEXt/3CU+fNnEWI6PppB5lIpwjFQNcW5 CVfR7l4PmLvvwjCfb5AJOhWxGNZp0b5mTdMlOG71VxEAiFu4Yo4vRCKZKnBfRHkEbbez 1c9l5YRsVyCVoB4TtS0tOLF6yQDekXKwDkKXJDQqu2gjZ2yTsyzTSaE44jYuozqixKa5 7bgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ro3mMm7Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hd30-20020a170907969e00b007addbdb9fbbsi11868449ejc.558.2022.12.14.05.47.21; Wed, 14 Dec 2022 05:47:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ro3mMm7Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238234AbiLNNbJ (ORCPT + 70 others); Wed, 14 Dec 2022 08:31:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238126AbiLNNbG (ORCPT ); Wed, 14 Dec 2022 08:31:06 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3C4E248FE for ; Wed, 14 Dec 2022 05:30:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671024621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=p5nDSovtGcMOmECeVBK2QeZhfWdznopYC9ScdOLMtVw=; b=Ro3mMm7YnH7Ptu+BItXyzI49B5S1yb5yruA2AT2RqtdChW47xPMMe1FXHYoztKLu3R7x/D 8RMBzivkE9mzL4q+AcL1cXs1HAgo0j0vuGK1lSq+eGsak7dImg+H+b4b4bCrXm7cTj/POf d8Hl03T5R76eNBeS6n+k4UBthoqVLNg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-381-SkU2T6obNHq0jzOTvgDD4g-1; Wed, 14 Dec 2022 08:30:17 -0500 X-MC-Unique: SkU2T6obNHq0jzOTvgDD4g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 74DA9101A52E; Wed, 14 Dec 2022 13:30:17 +0000 (UTC) Received: from T590 (ovpn-8-24.pek2.redhat.com [10.72.8.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C956514171C0; Wed, 14 Dec 2022 13:30:13 +0000 (UTC) Date: Wed, 14 Dec 2022 21:30:08 +0800 From: Ming Lei To: Hillf Danton Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Zhong Jinghua , Dennis Zhou Subject: Re: [PATCH 3/3] lib/percpu-refcount: drain ->release() in perpcu_ref_exit() Message-ID: References: <20221214025101.1268437-1-ming.lei@redhat.com> <20221214081651.954-1-hdanton@sina.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221214081651.954-1-hdanton@sina.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 Wed, Dec 14, 2022 at 04:16:51PM +0800, Hillf Danton wrote: > On 14 Dec 2022 10:51:01 +0800 Ming Lei > > The pattern of wait_event(percpu_ref_is_zero()) has been used in several > > For example? blk_mq_freeze_queue_wait() and target_wait_for_sess_cmds(). > > > kernel components, and this way actually has the following risk: > > > > - percpu_ref_is_zero() can be returned just between > > atomic_long_sub_and_test() and ref->data->release(ref) > > > > - given the refcount is found as zero, percpu_ref_exit() could > > be called, and the host data structure is freed > > > > - then use-after-free is triggered in ->release() when the user host > > data structure is freed after percpu_ref_exit() returns > > The race between exit and the release callback should be considered at the > corresponding callsite, given the comment below, and closed for instance > by synchronizing rcu. > > /** > * percpu_ref_put_many - decrement a percpu refcount > * @ref: percpu_ref to put > * @nr: number of references to put > * > * Decrement the refcount, and if 0, call the release function (which was passed > * to percpu_ref_init()) > * > * This function is safe to call as long as @ref is between init and exit. > */ Not sure if the above comment implies that the callsite should cover the race. But blk-mq can really avoid the trouble by using the existed call_rcu(): diff --git a/block/blk-core.c b/block/blk-core.c index 3866b6c4cd88..9321767470dc 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -254,14 +254,15 @@ EXPORT_SYMBOL_GPL(blk_clear_pm_only); static void blk_free_queue_rcu(struct rcu_head *rcu_head) { - kmem_cache_free(blk_requestq_cachep, - container_of(rcu_head, struct request_queue, rcu_head)); + struct request_queue *q = container_of(rcu_head, + struct request_queue, rcu_head); + + percpu_ref_exit(&q->q_usage_counter); + kmem_cache_free(blk_requestq_cachep, q); } static void blk_free_queue(struct request_queue *q) { - percpu_ref_exit(&q->q_usage_counter); - if (q->poll_stat) blk_stat_remove_callback(q, q->poll_cb); blk_stat_free_callback(q->poll_cb); Thanks, Ming