All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed
@ 2022-01-27 15:37 Ming Lei
  2022-01-27 17:19 ` Bart Van Assche
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ming Lei @ 2022-01-27 15:37 UTC (permalink / raw)
  To: Martin K . Petersen, linux-scsi
  Cc: Ming Lei, Bart Van Assche, Martin Wilck, Martin Wilck

Martin reported that sdev->queue_depth can often be changed in
->slave_configure(), and now we uses ->cmd_per_lun as initial queue
depth for setting up sdev->budget_map. And some extreme ->cmd_per_lun
or ->can_queue won't be used at default actually, if they are used to
allocate sdev->budget_map, huge memory may be consumed just because
of bad ->cmd_per_lun.

Fix the issue by reallocating sdev->budget_map after ->slave_configure()
returns, at that time, queue_depth should be much more reasonable.

Cc: Bart Van Assche <bvanassche@acm.org>
Reported-by: Martin Wilck <martin.wilck@suse.com>
Suggested-by: Martin Wilck <martin.wilck@suse.com>
Tested-by: Martin Wilck <mwilck@suse.com>
Reviewed-by: Martin Wilck <mwilck@suse.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
V2:
	- rename one local variable, and fix a comment grammar issue, as
	reported by Bart

 drivers/scsi/scsi_scan.c | 56 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 5 deletions(-)

diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 3520b9384428..4c7fb5a5ea4a 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -214,6 +214,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
@@ -306,11 +348,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;
@@ -1017,6 +1055,14 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
 			}
 			return SCSI_SCAN_NO_RESPONSE;
 		}
+
+		/*
+		 * queue_depth is often changed in ->slave_configure, so
+		 * setup budget map again for getting better memory uses
+		 * since memory consumption of the map depends on queue
+		 * depth heavily
+		 */
+		scsi_realloc_sdev_budget_map(sdev, sdev->queue_depth);
 	}
 
 	if (sdev->scsi_level >= SCSI_3)
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed
  2022-01-27 15:37 [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed Ming Lei
@ 2022-01-27 17:19 ` Bart Van Assche
  2022-01-31 17:56 ` Martin K. Petersen
  2022-02-01  2:03 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2022-01-27 17:19 UTC (permalink / raw)
  To: Ming Lei, Martin K . Petersen, linux-scsi; +Cc: Martin Wilck, Martin Wilck

On 1/27/22 07:37, Ming Lei wrote:
> Martin reported that sdev->queue_depth can often be changed in
> ->slave_configure(), and now we uses ->cmd_per_lun as initial queue
> depth for setting up sdev->budget_map. And some extreme ->cmd_per_lun
> or ->can_queue won't be used at default actually, if they are used to
> allocate sdev->budget_map, huge memory may be consumed just because
> of bad ->cmd_per_lun.
> 
> Fix the issue by reallocating sdev->budget_map after ->slave_configure()
> returns, at that time, queue_depth should be much more reasonable.

Reviewed-by: Bart Van Assche <bvanassche@acm.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed
  2022-01-27 15:37 [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed Ming Lei
  2022-01-27 17:19 ` Bart Van Assche
@ 2022-01-31 17:56 ` Martin K. Petersen
  2022-02-01  2:03 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2022-01-31 17:56 UTC (permalink / raw)
  To: Ming Lei
  Cc: Martin K . Petersen, linux-scsi, Bart Van Assche, Martin Wilck,
	Martin Wilck


Ming,

> Martin reported that sdev->queue_depth can often be changed in
> ->slave_configure(), and now we uses ->cmd_per_lun as initial queue
> depth for setting up sdev->budget_map. And some extreme ->cmd_per_lun
> or ->can_queue won't be used at default actually, if they are used to
> allocate sdev->budget_map, huge memory may be consumed just because of
> bad ->cmd_per_lun.

Applied to 5.17/scsi-fixes, thanks!

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed
  2022-01-27 15:37 [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed Ming Lei
  2022-01-27 17:19 ` Bart Van Assche
  2022-01-31 17:56 ` Martin K. Petersen
@ 2022-02-01  2:03 ` Martin K. Petersen
  2 siblings, 0 replies; 4+ messages in thread
From: Martin K. Petersen @ 2022-02-01  2:03 UTC (permalink / raw)
  To: Ming Lei, linux-scsi
  Cc: Martin K . Petersen, Bart Van Assche, Martin Wilck, Martin Wilck

On Thu, 27 Jan 2022 23:37:33 +0800, Ming Lei wrote:

> Martin reported that sdev->queue_depth can often be changed in
> ->slave_configure(), and now we uses ->cmd_per_lun as initial queue
> depth for setting up sdev->budget_map. And some extreme ->cmd_per_lun
> or ->can_queue won't be used at default actually, if they are used to
> allocate sdev->budget_map, huge memory may be consumed just because
> of bad ->cmd_per_lun.
> 
> [...]

Applied to 5.17/scsi-fixes, thanks!

[1/1] scsi: core: reallocate scsi device's budget map if default queue depth is changed
      https://git.kernel.org/mkp/scsi/c/edb854a3680b

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-01  2:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-27 15:37 [PATCH V2] scsi: core: reallocate scsi device's budget map if default queue depth is changed Ming Lei
2022-01-27 17:19 ` Bart Van Assche
2022-01-31 17:56 ` Martin K. Petersen
2022-02-01  2:03 ` Martin K. Petersen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.