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 X-Spam-Level: X-Spam-Status: No, score=-8.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED, USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 493E8C10F11 for ; Sat, 13 Apr 2019 07:27:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0BD8E2084D for ; Sat, 13 Apr 2019 07:27:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726457AbfDMH1q (ORCPT ); Sat, 13 Apr 2019 03:27:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44276 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725776AbfDMH1p (ORCPT ); Sat, 13 Apr 2019 03:27:45 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0C80F308339A; Sat, 13 Apr 2019 07:27:45 +0000 (UTC) Received: from ming.t460p (ovpn-8-18.pek2.redhat.com [10.72.8.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7C74619C56; Sat, 13 Apr 2019 07:27:36 +0000 (UTC) Date: Sat, 13 Apr 2019 15:27:31 +0800 From: Ming Lei To: Hannes Reinecke Cc: Jens Axboe , linux-block@vger.kernel.org, Dongli Zhang , James Smart , Bart Van Assche , linux-scsi@vger.kernel.org, "Martin K . Petersen" , Christoph Hellwig , "James E . J . Bottomley" , jianchao wang Subject: Re: [PATCH V5 6/9] blk-mq: always free hctx after request queue is freed Message-ID: <20190413072730.GC9108@ming.t460p> References: <20190412033032.10418-1-ming.lei@redhat.com> <20190412033032.10418-7-ming.lei@redhat.com> <3e4c2137-2a3e-cf00-45ec-3d54dbc4fc6a@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3e4c2137-2a3e-cf00-45ec-3d54dbc4fc6a@suse.de> User-Agent: Mutt/1.9.1 (2017-09-22) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Sat, 13 Apr 2019 07:27:45 +0000 (UTC) Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org On Fri, Apr 12, 2019 at 01:06:07PM +0200, Hannes Reinecke wrote: > On 4/12/19 5:30 AM, Ming Lei wrote: > > In normal queue cleanup path, hctx is released after request queue > > is freed, see blk_mq_release(). > > > > However, in __blk_mq_update_nr_hw_queues(), hctx may be freed because > > of hw queues shrinking. This way is easy to cause use-after-free, > > because: one implicit rule is that it is safe to call almost all block > > layer APIs if the request queue is alive; and one hctx may be retrieved > > by one API, then the hctx can be freed by blk_mq_update_nr_hw_queues(); > > finally use-after-free is triggered. > > > > Fixes this issue by always freeing hctx after releasing request queue. > > If some hctxs are removed in blk_mq_update_nr_hw_queues(), introduce > > a per-queue list to hold them, then try to resuse these hctxs if numa > > node is matched. > > > > Cc: Dongli Zhang > > Cc: James Smart > > Cc: Bart Van Assche > > Cc: linux-scsi@vger.kernel.org, > > Cc: Martin K . Petersen , > > Cc: Christoph Hellwig , > > Cc: James E . J . Bottomley , > > Cc: jianchao wang > > Signed-off-by: Ming Lei > > --- > > block/blk-mq.c | 40 +++++++++++++++++++++++++++------------- > > include/linux/blk-mq.h | 2 ++ > > include/linux/blkdev.h | 7 +++++++ > > 3 files changed, 36 insertions(+), 13 deletions(-) > > > > diff --git a/block/blk-mq.c b/block/blk-mq.c > > index 71996fe494eb..886fbb678617 100644 > > --- a/block/blk-mq.c > > +++ b/block/blk-mq.c > > @@ -2260,6 +2260,10 @@ static void blk_mq_exit_hctx(struct request_queue *q, > > set->ops->exit_hctx(hctx, hctx_idx); > > blk_mq_remove_cpuhp(hctx); > > + > > + spin_lock(&q->dead_hctx_lock); > > + list_add(&hctx->hctx_list, &q->dead_hctx_list); > > + spin_unlock(&q->dead_hctx_lock); > > } > > static void blk_mq_exit_hw_queues(struct request_queue *q, > > @@ -2660,15 +2664,13 @@ static int blk_mq_alloc_ctxs(struct request_queue *q) > > */ > > void blk_mq_release(struct request_queue *q) > > { > > - struct blk_mq_hw_ctx *hctx; > > - unsigned int i; > > + struct blk_mq_hw_ctx *hctx, *next; > > cancel_delayed_work_sync(&q->requeue_work); > > - /* hctx kobj stays in hctx */ > > - queue_for_each_hw_ctx(q, hctx, i) { > > - if (!hctx) > > - continue; > > + /* all hctx are in .dead_hctx_list now */ > > + list_for_each_entry_safe(hctx, next, &q->dead_hctx_list, hctx_list) { > > + list_del_init(&hctx->hctx_list); > > kobject_put(&hctx->kobj); > > } > > @@ -2735,9 +2737,22 @@ static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx( > > struct blk_mq_tag_set *set, struct request_queue *q, > > int hctx_idx, int node) > > { > > - struct blk_mq_hw_ctx *hctx; > > + struct blk_mq_hw_ctx *hctx = NULL, *tmp; > > + > > + /* reuse dead hctx first */ > > + spin_lock(&q->dead_hctx_lock); > > + list_for_each_entry(tmp, &q->dead_hctx_list, hctx_list) { > > + if (tmp->numa_node == node) { > > + hctx = tmp; > > + break; > > + } > > + } > > + if (hctx) > > + list_del_init(&hctx->hctx_list); > > + spin_unlock(&q->dead_hctx_lock); > > - hctx = blk_mq_alloc_hctx(q, set, hctx_idx, node); > > + if (!hctx) > > + hctx = blk_mq_alloc_hctx(q, set, hctx_idx, node); > > if (!hctx) > > goto fail; > > @@ -2775,10 +2790,8 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, > > hctx = blk_mq_alloc_and_init_hctx(set, q, i, node); > > if (hctx) { > > - if (hctxs[i]) { > > + if (hctxs[i]) > > blk_mq_exit_hctx(q, set, hctxs[i], i); > > - kobject_put(&hctxs[i]->kobj); > > - } > > hctxs[i] = hctx; > > } else { > > if (hctxs[i]) > > @@ -2809,9 +2822,7 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, > > if (hctx->tags) > > blk_mq_free_map_and_requests(set, j); > > blk_mq_exit_hctx(q, set, hctx, j); > > - kobject_put(&hctx->kobj); > > hctxs[j] = NULL; > > - > > } > > } > > mutex_unlock(&q->sysfs_lock); > > @@ -2854,6 +2865,9 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, > > if (!q->queue_hw_ctx) > > goto err_sys_init; > > + INIT_LIST_HEAD(&q->dead_hctx_list); > > + spin_lock_init(&q->dead_hctx_lock); > > + > > blk_mq_realloc_hw_ctxs(set, q); > > if (!q->nr_hw_queues) > > goto err_hctxs; > > diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h > > index cb2aa7ecafff..a44c3f95dcc1 100644 > > --- a/include/linux/blk-mq.h > > +++ b/include/linux/blk-mq.h > > @@ -70,6 +70,8 @@ struct blk_mq_hw_ctx { > > struct dentry *sched_debugfs_dir; > > #endif > > + struct list_head hctx_list; > > + > > /* Must be the last member - see also blk_mq_hw_ctx_size(). */ > > struct srcu_struct srcu[0]; > > }; > > diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h > > index 4b85dc066264..1325f941f0be 100644 > > --- a/include/linux/blkdev.h > > +++ b/include/linux/blkdev.h > > @@ -535,6 +535,13 @@ struct request_queue { > > struct mutex sysfs_lock; > > + /* > > + * for reusing dead hctx instance in case of updating > > + * nr_hw_queues > > + */ > > + struct list_head dead_hctx_list; > > + spinlock_t dead_hctx_lock; > > + > > atomic_t mq_freeze_depth; > > #if defined(CONFIG_BLK_DEV_BSG) > > > I actually had been looking into this, too, but couldn't convince myself > that the code really is a problem. > Did you see this happening in real life? User-after-free on hctx can be triggered if the hctx is freed in blk_mq_update_nr_hw_queues(). This patch aligns to normal cleanup queue model, and will avoid this kind of issue. Updating nr_hw_queues is only triggered on very specific situation, so in practice, it isn't easy to trigger. However, if we play CPU hotplug & timeout test on nvme-loop, it should have been easy to trigger, but nvme-loop's reset/timeout handler is simply broken. The same test can be done on nvme-pci too, just needs real hardware and environment and takes time... Thanks, Ming