Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2410543rdb; Thu, 21 Sep 2023 19:14:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFChyfTX6z4yziryrVvXFDw0568VHjE9Xp9AHQVTPEJ51xJOgWGfvIdlc1+9/rd+w9HkuEJ X-Received: by 2002:a05:6102:2b82:b0:452:c729:e9df with SMTP id ib2-20020a0561022b8200b00452c729e9dfmr1939382vsb.33.1695348887765; Thu, 21 Sep 2023 19:14:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695348887; cv=none; d=google.com; s=arc-20160816; b=qiuq4Sp2O6erlWmiaEcwdx6FxY1wkKUj4RmvoLhaml6bQldl1hYZsnBL7d+UoW+HCK bK3AnHkWgpqVExXHeXvP5Z2ejhnLFDoqgFgSdiS+JbWFeqA5YEQproPndlp1ioQM8+Il CsbQVhDYhE8IWwio4xnWCvtuVbS0VL2fnoi61i3cl3NjyLn+mJ5cVtJAMAgZ0N1Mj+Jc BMZGkyFXadxvAjr6NO8xnvAPz6mzp1p7a9RHgkm/1qncqpyFbP/41UwDv7E6E7XQd2Tq HXzpd83Ho4jFcU958xFzEnOFWtDRpAZIepM6LjPcnIJE5N9/3Fk6sXf7beG7VRlmPIJI oAIw== 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=R22aP8cvuvHgsRSlkpYF9RgXwgDt5sXPX6aie3Yt8vI=; fh=FbRi+YY2/yw0zZNc8c8M/oU31OA/xZqt8JQSTRROkyc=; b=KY/QPtUXGhIhlU8AbfIu0OizFmcTYMekTorYxCd4sWmYMebgCGfLrmABbLyj5LGCPr ND+vkpoLVJbmvFreBxujxuYzUD50UzhTBixdWQss1Z41XlQsl2gLc5GYuAMH8tcFWHI9 r3/hEJS9ROJBCJRT5E7eL3SGpuwoFWLK5Ps8e4veW/M2ueLtEWrEOFrIP1JqXihmaxFd lnVqqBEU6hWerY368+Mybo6oiBqP46kiI7eV1c75/evkLggCCDx6EoQv35fqNwe8lvs1 UY5YMOMoaLAXTuH8Xum8nj/7yESFEFbP7LytOQSGUFWpahHnvVeRshofwNkpG9bv5JKF 1cJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QTQTSqD+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id 141-20020a630193000000b005775161c510si2846312pgb.504.2023.09.21.19.14.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 19:14:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QTQTSqD+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id C5B4C807C748; Thu, 21 Sep 2023 18:10:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229840AbjIVBK1 (ORCPT + 99 others); Thu, 21 Sep 2023 21:10:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229806AbjIVBKZ (ORCPT ); Thu, 21 Sep 2023 21:10:25 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90EE4122 for ; Thu, 21 Sep 2023 18:09:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695344972; 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=R22aP8cvuvHgsRSlkpYF9RgXwgDt5sXPX6aie3Yt8vI=; b=QTQTSqD+Fd46pvhuJfT5LgTXstbxEAttZCRrpZFO3ubQ+qzXCZxtq/p4Ov1xo/UN++SZTg DAPaRazSsPUyADMCDzXBPrWpv34UKdiIPMcfGqweV9fPjVWaKfBOaaJfBe88RftWPfW27G 8Xqgoqce331PwTSvORp1sex99mmvIds= 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-316-NdYook5eMLa2EVpSr1Nipg-1; Thu, 21 Sep 2023 21:09:27 -0400 X-MC-Unique: NdYook5eMLa2EVpSr1Nipg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 88C97811E7B; Fri, 22 Sep 2023 01:09:26 +0000 (UTC) Received: from fedora (unknown [10.72.120.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E6A14492C37; Fri, 22 Sep 2023 01:09:19 +0000 (UTC) Date: Fri, 22 Sep 2023 09:09:14 +0800 From: Ming Lei To: Saranya Muruganandam Cc: Jens Axboe , Tejun Heo , stable@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Zhang Wensheng , Zhong Jinghua , Hillf Danton , Yu Kuai , Dennis Zhou Subject: Re: [PATCH] block: fix use-after-free of q->q_usage_counter Message-ID: References: <20230921182012.3965572-1-saranyamohan@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230921182012.3965572-1-saranyamohan@google.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 21 Sep 2023 18:10:29 -0700 (PDT) On Thu, Sep 21, 2023 at 11:20:12AM -0700, Saranya Muruganandam wrote: > From: Ming Lei > > commit d36a9ea5e7766961e753ee38d4c331bbe6ef659b upstream. > > For blk-mq, queue release handler is usually called after > blk_mq_freeze_queue_wait() returns. However, the > q_usage_counter->release() handler may not be run yet at that time, so > this can cause a use-after-free. > > Fix the issue by moving percpu_ref_exit() into blk_free_queue_rcu(). > Since ->release() is called with rcu read lock held, it is agreed that > the race should be covered in caller per discussion from the two links. > > Backport-notes: Not a clean cherry-pick since a lot has changed, > however essentially the same fix. > > Reported-by: Zhang Wensheng > Reported-by: Zhong Jinghua > Link: https://lore.kernel.org/linux-block/Y5prfOjyyjQKUrtH@T590/T/#u > Link: https://lore.kernel.org/lkml/Y4%2FmzMd4evRg9yDi@fedora/ > Cc: Hillf Danton > Cc: Yu Kuai > Cc: Dennis Zhou > Fixes: 2b0d3d3e4fcf ("percpu_ref: reduce memory footprint of percpu_ref in fast path") > Signed-off-by: Ming Lei > Link: https://lore.kernel.org/r/20221215021629.74870-1-ming.lei@redhat.com > Signed-off-by: Jens Axboe > Signed-off-by: Saranya Muruganandam > --- > block/blk-core.c | 2 -- > block/blk-sysfs.c | 2 ++ > 2 files changed, 2 insertions(+), 2 deletions(-) > > diff --git a/block/blk-core.c b/block/blk-core.c > index d0d0dd8151f7..e5eeec801f56 100644 > --- a/block/blk-core.c > +++ b/block/blk-core.c > @@ -414,8 +414,6 @@ void blk_cleanup_queue(struct request_queue *q) > blk_mq_sched_free_requests(q); > mutex_unlock(&q->sysfs_lock); > > - percpu_ref_exit(&q->q_usage_counter); > - > /* @q is and will stay empty, shutdown and put */ > blk_put_queue(q); > } > diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c > index 8c5816364dd1..9174137a913c 100644 > --- a/block/blk-sysfs.c > +++ b/block/blk-sysfs.c > @@ -726,6 +726,8 @@ static void blk_free_queue_rcu(struct rcu_head *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); > } Looks fine. BTW, you should have provided target stable tree release info, otherwise how you expect people to review? Thanks, Ming