Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4405346pxb; Mon, 21 Feb 2022 20:31:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjb9PO/jnquFGDPozHnEV56z/+3NqlvafNI3cmrSYn5u4ChAodYrAQRSZVbTSNFeFoKVDs X-Received: by 2002:a62:868e:0:b0:4cd:df75:b021 with SMTP id x136-20020a62868e000000b004cddf75b021mr23039763pfd.81.1645504280796; Mon, 21 Feb 2022 20:31:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645504280; cv=none; d=google.com; s=arc-20160816; b=VHSkP1IEBLRn20OLZbQJTyij0FPPmRjafTq+LugBAmnw67PmlqLDZjCd30lSiE9kWd iIxmEL0lJyzmPTDcye10OToC+8dljyGNxrkRw++bCFfGjR/CCbJBv67vr67Wv4LdqW/4 CaUssJszA6a7XCfDeMmCqjBZrjj1NKJqGgyoe0wli8JH4sCD6d5Quc5OD1SbTlbzT1Fe SsRQ/anqDoaTTPf8vdaMkJfLxKlKps2AXwEPJWvPtbf7Cd7XqGkNFC67zlPMQ3y3fPy8 7PxTA/tihiMtAym0LNnPHam3BTEqyce4z6IURZbf+FB7uh/ZuDxdDRdEAcNfo7X54jag BkFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ctLCRiqGQWae9Liyv281LX2FYLjNjSVjUfKNmHxb8uo=; b=u8uV7srlV1se2YLzWRqKxEHNnHb5bY0VtPztynIIjEk8cNAWTOFzDTguM2VnkpMGZf cLGZoayytwCJPhelwwgMc5W85UXcGud+Zs7O6WccdS8OZ+URBB8CT7dQKZeBJU/FP+dp KlaDu7DGGhDLNfrQNof9hrWCxrEjWQZIbXGtAWJP/FjDWfXQcI20fhBT/fuFsZk2UBSz +K6hWMJcRHnKX+F4lEnt9MQbI7j6ea0qQ4MVApoWa3ESxS7O4Awq2ACugPmqm9tnjFKB uXJxoWEsoZmth5OOq2ND4m3xaP91sDjJdTu9JGHbCI38PdHad0C0WYZTy+UrBbJ4ur4d S4zA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MXoKhJIt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k23si16083027pls.49.2022.02.21.20.31.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 20:31:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MXoKhJIt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 83B5BDDB; Mon, 21 Feb 2022 20:21:51 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350425AbiBUJbE (ORCPT + 99 others); Mon, 21 Feb 2022 04:31:04 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:36960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349739AbiBUJVl (ORCPT ); Mon, 21 Feb 2022 04:21:41 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D1BC26AF4; Mon, 21 Feb 2022 01:08:56 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9F13B6077B; Mon, 21 Feb 2022 09:08:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7682EC340F4; Mon, 21 Feb 2022 09:08:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645434535; bh=lBS1PiFdP3yxD/PYLR6hsDp3zi/r/zvvJWP4zHDftYk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MXoKhJIt3wBv1QTlYPgWYiNeihTS5lsnlDNPaAcFLkePzOB+5QtiXJGQiIKoQx+9B SdaeO2j7IamZyoNFEW8ho70WZbkgJDjwsxY9JfWDtfndag7Z2++IcXFGopcdTgeZQP T78nmhJYQc96+M1iyl/PsiY9RDcx0xeubEusZOHw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bart Van Assche , Martin Wilck , Martin Wilck , Ming Lei , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 5.15 040/196] scsi: core: Reallocate devices budget map on queue depth change Date: Mon, 21 Feb 2022 09:47:52 +0100 Message-Id: <20220221084932.279177221@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084930.872957717@linuxfoundation.org> References: <20220221084930.872957717@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 From: Ming Lei [ Upstream commit edb854a3680bacc9ef9b91ec0c5ff6105886f6f3 ] We currently use ->cmd_per_lun as initial queue depth for setting up the budget_map. Martin Wilck reported that it is common for the queue_depth to be subsequently updated in slave_configure() based on detected hardware characteristics. As a result, for some drivers, the static host template settings for cmd_per_lun and can_queue won't actually get used in practice. And if the default values are used to allocate the budget_map, memory may be consumed unnecessarily. Fix the issue by reallocating the budget_map after ->slave_configure() returns. At that time the device queue_depth should accurately reflect what the hardware needs. Link: https://lore.kernel.org/r/20220127153733.409132-1-ming.lei@redhat.com Cc: Bart Van Assche Reported-by: Martin Wilck Suggested-by: Martin Wilck Tested-by: Martin Wilck Reviewed-by: Martin Wilck Reviewed-by: Bart Van Assche Signed-off-by: Ming Lei Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/scsi_scan.c | 55 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index fe22191522a3b..7266880c70c21 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -198,6 +198,48 @@ static void scsi_unlock_floptical(struct scsi_device *sdev, SCSI_TIMEOUT, 3, NULL); } +static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev, + unsigned int depth) +{ + int new_shift = sbitmap_calculate_shift(depth); + bool need_alloc = !sdev->budget_map.map; + bool need_free = false; + int ret; + struct sbitmap sb_backup; + + /* + * realloc if new shift is calculated, which is caused by setting + * up one new default queue depth after calling ->slave_configure + */ + if (!need_alloc && new_shift != sdev->budget_map.shift) + need_alloc = need_free = true; + + if (!need_alloc) + return 0; + + /* + * Request queue has to be frozen for reallocating budget map, + * and here disk isn't added yet, so freezing is pretty fast + */ + if (need_free) { + blk_mq_freeze_queue(sdev->request_queue); + sb_backup = sdev->budget_map; + } + ret = sbitmap_init_node(&sdev->budget_map, + scsi_device_max_queue_depth(sdev), + new_shift, GFP_KERNEL, + sdev->request_queue->node, false, true); + if (need_free) { + if (ret) + sdev->budget_map = sb_backup; + else + sbitmap_free(&sb_backup); + ret = 0; + blk_mq_unfreeze_queue(sdev->request_queue); + } + return ret; +} + /** * scsi_alloc_sdev - allocate and setup a scsi_Device * @starget: which target to allocate a &scsi_device for @@ -291,11 +333,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, * default device queue depth to figure out sbitmap shift * since we use this queue depth most of times. */ - if (sbitmap_init_node(&sdev->budget_map, - scsi_device_max_queue_depth(sdev), - sbitmap_calculate_shift(depth), - GFP_KERNEL, sdev->request_queue->node, - false, true)) { + if (scsi_realloc_sdev_budget_map(sdev, depth)) { put_device(&starget->dev); kfree(sdev); goto out; @@ -1001,6 +1039,13 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, } return SCSI_SCAN_NO_RESPONSE; } + + /* + * The queue_depth is often changed in ->slave_configure. + * Set up budget map again since memory consumption of + * the map depends on actual queue depth. + */ + scsi_realloc_sdev_budget_map(sdev, sdev->queue_depth); } if (sdev->scsi_level >= SCSI_3) -- 2.34.1