From patchwork Mon Oct 21 22:42:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 11203135 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 39D6013BD for ; Mon, 21 Oct 2019 22:43:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 22DAE20B7C for ; Mon, 21 Oct 2019 22:43:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730203AbfJUWnI (ORCPT ); Mon, 21 Oct 2019 18:43:08 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:38427 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727264AbfJUWnI (ORCPT ); Mon, 21 Oct 2019 18:43:08 -0400 Received: by mail-pg1-f196.google.com with SMTP id w3so8686743pgt.5 for ; Mon, 21 Oct 2019 15:43:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kH6arMtNKhExAQtmx+B/ga34kxbYtksoL7+f/odhzAo=; b=k3vk8cZREe6Y5YF9OM62KK8nARXffDtkvOMx6R9xPJqBA6wyo+CObuAuRF/kKfxnhs jc0mINEo5rVnMOt5isCOxjMPytkBn9hqZfresHHdKZJcmR50w0oxLe+x4RedBUrKyyNK jQRo2ZgnXwPYNXGDuHMNlm/ZnsF65vqpfN2HCLuggncUSrxtAbVamvvUvAdNg1iM1wtz aDYviEN6Df06Zy5bx5Mkh3oibVQBm12VeVQO7xoEJ8xSxYcs4/ROe5Nq9xmcrBG6Ooe1 PTHA6j7kmKR72ihRwW6Tj3R+RhuycaCr7lixaebxY0AuAYnkGABf1UECAhwFB8r2gGuo wW+Q== X-Gm-Message-State: APjAAAWh7kWxsY9k06TZwu0e8Xl5LHqf1PPWm9/NyQ+LUMclEBwAYBwT 4DxY8R7nXy9vkMal7k+QvQ0= X-Google-Smtp-Source: APXvYqxu22ftwVOeFHXKBFRqIfeISWgt//PMqeTzZsrK/3ofmcaUADIAD3+TZ3Nj2rWoV2MjIDXqkg== X-Received: by 2002:a62:38d5:: with SMTP id f204mr470676pfa.75.1571697787688; Mon, 21 Oct 2019 15:43:07 -0700 (PDT) Received: from desktop-bart.svl.corp.google.com ([2620:15c:2cd:202:4308:52a3:24b6:2c60]) by smtp.gmail.com with ESMTPSA id u9sm15944763pjb.4.2019.10.21.15.43.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2019 15:43:06 -0700 (PDT) From: Bart Van Assche To: Jens Axboe Cc: linux-block@vger.kernel.org, Christoph Hellwig , Bart Van Assche , Christoph Hellwig , Ming Lei , Hannes Reinecke , Johannes Thumshirn Subject: [PATCH 1/4] block: Remove the synchronize_rcu() call from __blk_mq_update_nr_hw_queues() Date: Mon, 21 Oct 2019 15:42:56 -0700 Message-Id: <20191021224259.209542-2-bvanassche@acm.org> X-Mailer: git-send-email 2.23.0.866.gb869b98d4c-goog In-Reply-To: <20191021224259.209542-1-bvanassche@acm.org> References: <20191021224259.209542-1-bvanassche@acm.org> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Since the blk_mq_{,un}freeze_queue() calls in __blk_mq_update_nr_hw_queues() already serialize __blk_mq_update_nr_hw_queues() against blk_mq_queue_tag_busy_iter(), the synchronize_rcu() call in __blk_mq_update_nr_hw_queues() is not necessary. Hence remove it. Note: the synchronize_rcu() call in __blk_mq_update_nr_hw_queues() was introduced by commit f5bbbbe4d635 ("blk-mq: sync the update nr_hw_queues with blk_mq_queue_tag_busy_iter"). Commit 530ca2c9bd69 ("blk-mq: Allow blocking queue tag iter callbacks") removed the rcu_read_{,un}lock() calls that correspond to the synchronize_rcu() call in __blk_mq_update_nr_hw_queues(). Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Johannes Thumshirn Signed-off-by: Bart Van Assche Reviewed-by: Ming Lei --- block/blk-mq.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 8538dc415499..7528678ef41f 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3242,10 +3242,6 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, list_for_each_entry(q, &set->tag_list, tag_set_list) blk_mq_freeze_queue(q); - /* - * Sync with blk_mq_queue_tag_busy_iter. - */ - synchronize_rcu(); /* * Switch IO scheduler to 'none', cleaning up the data associated * with the previous scheduler. We will switch back once we are done From patchwork Mon Oct 21 22:42:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 11203139 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D0F4B1895 for ; Mon, 21 Oct 2019 22:43:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B8CDB20882 for ; Mon, 21 Oct 2019 22:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727264AbfJUWnL (ORCPT ); Mon, 21 Oct 2019 18:43:11 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:39890 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730276AbfJUWnL (ORCPT ); Mon, 21 Oct 2019 18:43:11 -0400 Received: by mail-pf1-f193.google.com with SMTP id v4so9332253pff.6 for ; Mon, 21 Oct 2019 15:43:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K03SxJG5UM5QLbF/Apnz7RCTvF9NtEiDW4IzmGypazw=; b=MmLf0mRGD9CodF/T9HX6f5m4CEBXyVtZEkI/0Xxx5sJs6LG3WbV3xtw2LR1AB10viu DoJPTxSdZ4sLm6XAjaPIxUD5McchzxknWMfYor+2bPV2rDazlKqYFIn/pNnW8XfkFuhE zAacLVhnu/bWqj4Sc+tX9sLQKuStWdx1nyaPTR4kU/ndZRBSV4yA6JUM5DyxiV5ykJC3 vk4BzW1mHRnM/Trlj38u0iz3CXGK3QkD5na1S+icdzjqguAFsDpxwPyu6OqH6adGzkI/ ILP0Y1AYBpl5f0r/nZQqDINl4s0UmvRSJBKLsh87FwrYmu7XDXAZpy2S5jQsLKzymrbc naog== X-Gm-Message-State: APjAAAWaD3HG280VWzQ/ZlsCWnsm8N0+CQtgZdOfPyfg7jvBCM2W9hOh VdhlTcaq9vtlNGRMyWJsmhfMSqhHMd0= X-Google-Smtp-Source: APXvYqw+oxZDfXFKLck8hk+qGu21exc/GLYHCRDm/JguV1JgrDJ907UAZGMtnfQc+d4dRl8knQj3ig== X-Received: by 2002:a63:dd11:: with SMTP id t17mr266772pgg.242.1571697788920; Mon, 21 Oct 2019 15:43:08 -0700 (PDT) Received: from desktop-bart.svl.corp.google.com ([2620:15c:2cd:202:4308:52a3:24b6:2c60]) by smtp.gmail.com with ESMTPSA id u9sm15944763pjb.4.2019.10.21.15.43.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2019 15:43:08 -0700 (PDT) From: Bart Van Assche To: Jens Axboe Cc: linux-block@vger.kernel.org, Christoph Hellwig , Bart Van Assche , Christoph Hellwig , Ming Lei , Hannes Reinecke , Johannes Thumshirn Subject: [PATCH 2/4] block: Fix a race between blk_poll() and blk_mq_update_nr_hw_queues() Date: Mon, 21 Oct 2019 15:42:57 -0700 Message-Id: <20191021224259.209542-3-bvanassche@acm.org> X-Mailer: git-send-email 2.23.0.866.gb869b98d4c-goog In-Reply-To: <20191021224259.209542-1-bvanassche@acm.org> References: <20191021224259.209542-1-bvanassche@acm.org> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org If blk_poll() is called if no requests are in progress, it may happen that blk_mq_update_nr_hw_queues() modifies the data structures used by blk_poll(), e.g. q->queue_hw_ctx[]. Fix this race by serializing blk_poll() against blk_mq_update_nr_hw_queues(). Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Johannes Thumshirn Signed-off-by: Bart Van Assche --- block/blk-mq.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 7528678ef41f..ea64d951f411 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -3439,19 +3439,7 @@ static bool blk_mq_poll_hybrid(struct request_queue *q, return blk_mq_poll_hybrid_sleep(q, hctx, rq); } -/** - * blk_poll - poll for IO completions - * @q: the queue - * @cookie: cookie passed back at IO submission time - * @spin: whether to spin for completions - * - * Description: - * Poll for completions on the passed in queue. Returns number of - * completed entries found. If @spin is true, then blk_poll will continue - * looping until at least one completion is found, unless the task is - * otherwise marked running (or we need to reschedule). - */ -int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin) +static int __blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin) { struct blk_mq_hw_ctx *hctx; long state; @@ -3503,6 +3491,30 @@ int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin) __set_current_state(TASK_RUNNING); return 0; } + +/** + * blk_poll - poll for IO completions + * @q: the queue + * @cookie: cookie passed back at IO submission time + * @spin: whether to spin for completions + * + * Description: + * Poll for completions on the passed in queue. Returns number of + * completed entries found. If @spin is true, then blk_poll will continue + * looping until at least one completion is found, unless the task is + * otherwise marked running (or we need to reschedule). + */ +int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin) +{ + int ret; + + if (!percpu_ref_tryget(&q->q_usage_counter)) + return 0; + ret = __blk_poll(q, cookie, spin); + blk_queue_exit(q); + + return ret; +} EXPORT_SYMBOL_GPL(blk_poll); unsigned int blk_mq_rq_cpu(struct request *rq) From patchwork Mon Oct 21 22:42:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 11203137 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 94A5413BD for ; Mon, 21 Oct 2019 22:43:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7C8F02089C for ; Mon, 21 Oct 2019 22:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730289AbfJUWnL (ORCPT ); Mon, 21 Oct 2019 18:43:11 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:42783 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727264AbfJUWnK (ORCPT ); Mon, 21 Oct 2019 18:43:10 -0400 Received: by mail-pf1-f196.google.com with SMTP id q12so9324929pff.9 for ; Mon, 21 Oct 2019 15:43:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=f8rxoisaPFhyyWWqrkbV3n8HS8qyHhu9JwL53xw7tZ8=; b=DJC7rKhe5cKlid2pHsA78xFB1l9hHrbkXDqi44kqmnygJZrWRMi9BI6KRwKVBPoIvf k2WenRTD7hYfABCayftuzBaXcz6TP7MARG1OnJP5QN4oea8kkv35BDH9SS0DdBo7ZLUz 7Sk66kNl0gOzwJBX5MhIM53t2W+J1wKat6NJ17+iWJ3GXvFbbjpXVvw9LjiO3LvGc3pc PANdMh+U+K4ojv0kEsWU5ijXPkb9igZLQdQdsjGNXYk3QA/wlu2TPptESzWyQ4d8GWKu 71gYIv3+U+tM5OWzjc2Dea9lr2XsryXvHx7hCEgYbOSGV4ZwOQeu1Qs93sEU8SUpavjV nP0w== X-Gm-Message-State: APjAAAXFB2+Q4VN3h0SLzSTIQsTUCRwseR1jH9a1h2D5+iPRy4QwYp/y 8rZBRPk5BMBD6AGIwuQoKgLEME66rtA= X-Google-Smtp-Source: APXvYqw53SLMCtaUJknOG0dec3io/QHQpVFf/7V63NydWBZgM4yVY6scnM00zdouriE+50L57E0vcQ== X-Received: by 2002:a17:90a:17ad:: with SMTP id q42mr634276pja.100.1571697790211; Mon, 21 Oct 2019 15:43:10 -0700 (PDT) Received: from desktop-bart.svl.corp.google.com ([2620:15c:2cd:202:4308:52a3:24b6:2c60]) by smtp.gmail.com with ESMTPSA id u9sm15944763pjb.4.2019.10.21.15.43.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2019 15:43:09 -0700 (PDT) From: Bart Van Assche To: Jens Axboe Cc: linux-block@vger.kernel.org, Christoph Hellwig , Bart Van Assche , Keith Busch , Christoph Hellwig , Ming Lei , Hannes Reinecke , Johannes Thumshirn Subject: [PATCH 3/4] block: Reduce the amount of memory required per request queue Date: Mon, 21 Oct 2019 15:42:58 -0700 Message-Id: <20191021224259.209542-4-bvanassche@acm.org> X-Mailer: git-send-email 2.23.0.866.gb869b98d4c-goog In-Reply-To: <20191021224259.209542-1-bvanassche@acm.org> References: <20191021224259.209542-1-bvanassche@acm.org> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Instead of always allocating at least nr_cpu_ids hardware queues per request queue, reallocate q->queue_hw_ctx if it has to grow. This patch improves behavior that was introduced by commit 868f2f0b7206 ("blk-mq: dynamic h/w context count"). Cc: Keith Busch Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Johannes Thumshirn Signed-off-by: Bart Van Assche --- block/blk-mq.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index ea64d951f411..86f6852130fc 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2761,6 +2761,23 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, int i, j, end; struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; + if (q->nr_hw_queues < set->nr_hw_queues) { + struct blk_mq_hw_ctx **new_hctxs; + + new_hctxs = kcalloc_node(set->nr_hw_queues, + sizeof(*new_hctxs), GFP_KERNEL, + set->numa_node); + if (!new_hctxs) + return; + if (hctxs) + memcpy(new_hctxs, hctxs, q->nr_hw_queues * + sizeof(*hctxs)); + q->queue_hw_ctx = new_hctxs; + q->nr_hw_queues = set->nr_hw_queues; + kfree(hctxs); + hctxs = new_hctxs; + } + /* protect against switching io scheduler */ mutex_lock(&q->sysfs_lock); for (i = 0; i < set->nr_hw_queues; i++) { @@ -2848,12 +2865,6 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, /* init q->mq_kobj and sw queues' kobjects */ blk_mq_sysfs_init(q); - q->queue_hw_ctx = kcalloc_node(nr_hw_queues(set), - sizeof(*(q->queue_hw_ctx)), GFP_KERNEL, - set->numa_node); - if (!q->queue_hw_ctx) - goto err_sys_init; - INIT_LIST_HEAD(&q->unused_hctx_list); spin_lock_init(&q->unused_hctx_lock); @@ -2901,7 +2912,6 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, err_hctxs: kfree(q->queue_hw_ctx); q->nr_hw_queues = 0; -err_sys_init: blk_mq_sysfs_deinit(q); err_poll: blk_stat_free_callback(q->poll_cb); From patchwork Mon Oct 21 22:42:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 11203141 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 056591895 for ; Mon, 21 Oct 2019 22:43:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E195020882 for ; Mon, 21 Oct 2019 22:43:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730304AbfJUWnM (ORCPT ); Mon, 21 Oct 2019 18:43:12 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:36666 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730276AbfJUWnM (ORCPT ); Mon, 21 Oct 2019 18:43:12 -0400 Received: by mail-pg1-f196.google.com with SMTP id 23so8691395pgk.3 for ; Mon, 21 Oct 2019 15:43:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iOoP7x/uL+UUHCxeNCIbvREN3P8PxzS++rPEUCMI99Y=; b=svL8YXRlZqezJW+5Y9EcUjUEtjhMWkKkjGWm+djFS7dSxcR1gx4ifWzgfjchdUCZFw T8dX1upQyAF/i+32mRLmcL2f99Vh8v1Bw8C11qk5+QH3oHMfXjTXGv0pxU9e8y3HGMtn Fjgchd51JbEOnIwRrHnU2KbLDc+iQeqPIJJ5toLu5z+PatBupsVFMs33mWQTkl7bteVL 85Zni7UJW9cNyPbGLDezvDCQKpu2Qdex46IIMCtF1sR+TQeoBhtm8wTCyTTY2+WEvXT7 ETsXSscGgFgWyopNYr8Dqli/kCVxpNIsNbLmRs0PbhOhQseXjcvd9VZdvFmU9n3H3EaC UY4g== X-Gm-Message-State: APjAAAWEBWj5cZ4lZfRa2GUDHnTRW779ubSPvz8xBD6j4gzVhN29FXkA 3P3rOMcYhcuqHJX3CPsso4U= X-Google-Smtp-Source: APXvYqwQRGWb+OqJe7jCcakOTl3eEkkVGbc9EUy5zBl7hNywwtca0rOWaH+N2U9U+Yy1bdtgU17gOg== X-Received: by 2002:a63:934d:: with SMTP id w13mr231433pgm.185.1571697791320; Mon, 21 Oct 2019 15:43:11 -0700 (PDT) Received: from desktop-bart.svl.corp.google.com ([2620:15c:2cd:202:4308:52a3:24b6:2c60]) by smtp.gmail.com with ESMTPSA id u9sm15944763pjb.4.2019.10.21.15.43.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2019 15:43:10 -0700 (PDT) From: Bart Van Assche To: Jens Axboe Cc: linux-block@vger.kernel.org, Christoph Hellwig , Bart Van Assche , Keith Busch , Christoph Hellwig , Ming Lei , Hannes Reinecke , Johannes Thumshirn Subject: [PATCH 4/4] block: Reduce the amount of memory used for tag sets Date: Mon, 21 Oct 2019 15:42:59 -0700 Message-Id: <20191021224259.209542-5-bvanassche@acm.org> X-Mailer: git-send-email 2.23.0.866.gb869b98d4c-goog In-Reply-To: <20191021224259.209542-1-bvanassche@acm.org> References: <20191021224259.209542-1-bvanassche@acm.org> MIME-Version: 1.0 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Instead of allocating an array of size nr_cpu_ids for set->tags, allocate an array of size set->nr_hw_queues. This patch improves behavior that was introduced by commit 868f2f0b7206 ("blk-mq: dynamic h/w context count"). Reallocating tag sets from inside __blk_mq_update_nr_hw_queues() is safe because: - All request queues that share the tag sets are frozen before the tag sets are reallocated. - blk_mq_queue_tag_busy_iter() holds q->q_usage_counter while active and hence is serialized against __blk_mq_update_nr_hw_queues(). Cc: Keith Busch Cc: Christoph Hellwig Cc: Ming Lei Cc: Hannes Reinecke Cc: Johannes Thumshirn Signed-off-by: Bart Van Assche --- block/blk-mq.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 86f6852130fc..1279db579fa2 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2833,19 +2833,6 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, mutex_unlock(&q->sysfs_lock); } -/* - * Maximum number of hardware queues we support. For single sets, we'll never - * have more than the CPUs (software queues). For multiple sets, the tag_set - * user may have set ->nr_hw_queues larger. - */ -static unsigned int nr_hw_queues(struct blk_mq_tag_set *set) -{ - if (set->nr_maps == 1) - return nr_cpu_ids; - - return max(set->nr_hw_queues, nr_cpu_ids); -} - struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set, struct request_queue *q, bool elevator_init) @@ -3012,6 +2999,29 @@ static int blk_mq_update_queue_map(struct blk_mq_tag_set *set) } } +static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set, + int cur_nr_hw_queues, int new_nr_hw_queues) +{ + struct blk_mq_tags **new_tags; + + if (cur_nr_hw_queues >= new_nr_hw_queues) + return 0; + + new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *), + GFP_KERNEL, set->numa_node); + if (!new_tags) + return -ENOMEM; + + if (set->tags) + memcpy(new_tags, set->tags, cur_nr_hw_queues * + sizeof(*set->tags)); + kfree(set->tags); + set->tags = new_tags; + set->nr_hw_queues = new_nr_hw_queues; + + return 0; +} + /* * Alloc a tag set to be associated with one or more request queues. * May fail with EINVAL for various error conditions. May adjust the @@ -3065,9 +3075,7 @@ int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set) if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids) set->nr_hw_queues = nr_cpu_ids; - set->tags = kcalloc_node(nr_hw_queues(set), sizeof(struct blk_mq_tags *), - GFP_KERNEL, set->numa_node); - if (!set->tags) + if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0) return -ENOMEM; ret = -ENOMEM; @@ -3108,7 +3116,7 @@ void blk_mq_free_tag_set(struct blk_mq_tag_set *set) { int i, j; - for (i = 0; i < nr_hw_queues(set); i++) + for (i = 0; i < set->nr_hw_queues; i++) blk_mq_free_map_and_requests(set, i); for (j = 0; j < set->nr_maps; j++) { @@ -3266,6 +3274,10 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, blk_mq_sysfs_unregister(q); } + if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) < + 0) + goto reregister; + prev_nr_hw_queues = set->nr_hw_queues; set->nr_hw_queues = nr_hw_queues; blk_mq_update_queue_map(set); @@ -3282,6 +3294,7 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, blk_mq_map_swqueue(q); } +reregister: list_for_each_entry(q, &set->tag_list, tag_set_list) { blk_mq_sysfs_register(q); blk_mq_debugfs_register_hctxs(q);