Skip to contentSkip to conversation

blk-mq: Implement runtime power management

PATCH In review Bart Van Assche 3 participants 26 messages Aug 9, 2018 – Aug 13, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover blk-mq: Implement runtime power management

Hello Jens,

This patch series not only implements runtime power management for blk-mq but also fixes a starvation issue in the power management code for the legacy block layer. Please consider this patch series for the upstream kernel.

Thanks,

Bart.

Changes compared to v5:
- Introduced a new flag RQF_DV that replaces RQF_PREEMPT for SCSI domain validation.
- Introduced a new request queue state QUEUE_FLAG_DV_ONLY for SCSI domain validation.
- Instead of using SDEV_QUIESCE for both runtime suspend and SCSI domain validation, use that state for domain validation only and introduce a new state for runtime suspend, namely SDEV_QUIESCE.
- Reallow system suspend during SCSI domain validation.
- Moved the runtime resume call from the request allocation code into blk_queue_enter().
- Instead of relying on q_usage_counter, iterate over the tag set to determine whether or not any requests are in flight.

Changes compared to v4:
- Dropped the patches "Give RQF_PREEMPT back its original meaning" and "Serialize queue freezing and blk_pre_runtime_suspend()".
- Replaced "percpu_ref_read()" with "percpu_is_in_use()".
- Inserted pm_request_resume() calls in the block layer request allocation code such that the context that submits a request no longer has to call pm_runtime_get().

Changes compared to v3:
- Avoid adverse interactions between system-wide suspend/resume and runtime power management by changing the PREEMPT_ONLY flag into a counter.
- Give RQF_PREEMPT back its original meaning, namely that it is only set for ide_preempt requests.
- Remove the flag BLK_MQ_REQ_PREEMPT.
- Removed the pm_request_resume() call.

Changes compared to v2:
- Fixed the build for CONFIG_BLOCK=n.
- Added a patch that introduces percpu_ref_read() in the percpu-counter code.
- Added a patch that makes it easier to detect missing pm_runtime_get*() calls.
- Addressed Jianchao's feedback including the comment about runtime overhead of switching a per-cpu counter to atomic mode.

Changes compared to v1:
- Moved the runtime power management code into a separate file.
- Addressed Ming's feedback.

Bart Van Assche (12):
block, scsi: Introduce request flag RQF_DV
scsi: Alter handling of RQF_DV requests
scsi: Only set RQF_DV for requests used for domain validation
scsi: Introduce the SDEV_SUSPENDED device status
block, scsi: Rename QUEUE_FLAG_PREEMPT_ONLY into DV_ONLY and introduce
PM_ONLY
scsi: Reallow SPI domain validation during system suspend
block: Move power management code into a new source file block, scsi: Introduce blk_pm_runtime_exit()
block: Split blk_pm_add_request() and blk_pm_put_request()
block: Change the runtime power management approach (1/2)
block: Change the runtime power management approach (2/2)
blk-mq: Enable support for runtime power management

 block/Kconfig                     |   5 +
 block/Makefile                    |   1 +
 block/blk-core.c                  | 310 ++++++------------------------
 block/blk-mq-debugfs.c            |   4 +-
 block/blk-mq.c                    |   4 +-
 block/blk-pm.c                    | 262 +++++++++++++++++++++++++
 block/blk-pm.h                    |  69 +++++++
 block/elevator.c                  |  22 +--
 drivers/ide/ide-pm.c              |   3 +-
 drivers/scsi/scsi_lib.c           | 266 ++++++++++++++++++-------
 drivers/scsi/scsi_pm.c            |   7 +-
 drivers/scsi/scsi_sysfs.c         |   2 +
 drivers/scsi/scsi_transport_spi.c |  26 +--
 drivers/scsi/sd.c                 |  10 +-
 drivers/scsi/sr.c                 |   4 +-
 include/linux/blk-mq.h            |   6 +-
 include/linux/blk-pm.h            |  26 +++
 include/linux/blkdev.h            |  41 ++--
 include/scsi/scsi_device.h        |  15 +-
 include/scsi/scsi_transport_spi.h |   1 -
 20 files changed, 673 insertions(+), 411 deletions(-)
 create mode 100644 block/blk-pm.c
 create mode 100644 block/blk-pm.h
 create mode 100644 include/linux/blk-pm.h
Signature · 1 line
2.18.0
#1 lore raw

PATCH v6 1/12 block, scsi: Introduce request flag RQF_DV

Instead of marking all power management, SCSI domain validation and IDE preempt requests with RQF_PREEMPT, only mark IDE preempt requests with RQF_PREEMPT. Use RQF_DV to mark requests submitted by scsi_execute() and RQF_PM to mark power management requests. Most but not all power management requests already have the RQF_PM flag set.

Cc Martin K. Petersen <[email protected]>
Cc David S. Miller <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-core.c        | 11 +++++------
 block/blk-mq-debugfs.c  |  1 +
 block/blk-mq.c          |  2 --
 drivers/ide/ide-pm.c    |  3 ++-
 drivers/scsi/scsi_lib.c | 11 ++++++++---
 include/linux/blk-mq.h  |  6 ++++--
 include/linux/blkdev.h  |  5 +++--
 7 files changed, 23 insertions(+), 16 deletions(-)
block/blk-core.c +5 -6
index 12550340418d..c4ff58491758 100644
@@ -913,11 +913,11 @@ EXPORT_SYMBOL(blk_alloc_queue);
 /**
  * blk_queue_enter() - try to increase q->q_usage_counter
  * @q: request queue pointer
- * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PREEMPT
+ * @flags: BLK_MQ_REQ_NOWAIT, BLK_MQ_REQ_PM and/or BLK_MQ_REQ_DV
  */
 int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
 {
-	const bool preempt = flags & BLK_MQ_REQ_PREEMPT;
+	const bool preempt = flags & (BLK_MQ_REQ_PM | BLK_MQ_REQ_DV);
 
 	while (true) {
 		bool success = false;
@@ -1436,8 +1436,6 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,
 	blk_rq_set_rl(rq, rl);
 	rq->cmd_flags = op;
 	rq->rq_flags = rq_flags;
-	if (flags & BLK_MQ_REQ_PREEMPT)
-		rq->rq_flags |= RQF_PREEMPT;
 
 	/* init elvpriv */
 	if (rq_flags & RQF_ELVPRIV) {
@@ -1575,7 +1573,7 @@ static struct request *get_request(struct request_queue *q, unsigned int op,
 	goto retry;
 }
 
-/* flags: BLK_MQ_REQ_PREEMPT and/or BLK_MQ_REQ_NOWAIT. */
+/* flags: BLK_MQ_REQ_NOWAIT, BLK_MQ_REQ_PM and/or BLK_MQ_REQ_DV. */
 static struct request *blk_old_get_request(struct request_queue *q,
 				unsigned int op, blk_mq_req_flags_t flags)
 {
@@ -1618,7 +1616,8 @@ struct request *blk_get_request(struct request_queue *q, unsigned int op,
 	struct request *req;
 
 	WARN_ON_ONCE(op & REQ_NOWAIT);
-	WARN_ON_ONCE(flags & ~(BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_PREEMPT));
+	WARN_ON_ONCE(flags & ~(BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_PM |
+			       BLK_MQ_REQ_DV));
 
 	if (q->mq_ops) {
 		req = blk_mq_alloc_request(q, op, flags);
block/blk-mq-debugfs.c +1 -0
index cb1e6cf7ac48..b4e722311ae1 100644
@@ -330,6 +330,7 @@ static const char *const rqf_name[] = {
 	RQF_NAME(SPECIAL_PAYLOAD),
 	RQF_NAME(ZONE_WRITE_LOCKED),
 	RQF_NAME(MQ_POLL_SLEPT),
+	RQF_NAME(DV),
 };
 #undef RQF_NAME
 
block/blk-mq.c +0 -2
index 72a0033ccee9..2a0eb058ba5a 100644
@@ -300,8 +300,6 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
 	rq->rq_flags = rq_flags;
 	rq->cpu = -1;
 	rq->cmd_flags = op;
-	if (data->flags & BLK_MQ_REQ_PREEMPT)
-		rq->rq_flags |= RQF_PREEMPT;
 	if (blk_queue_io_stat(data->q))
 		rq->rq_flags |= RQF_IO_STAT;
 	INIT_LIST_HEAD(&rq->queuelist);
drivers/ide/ide-pm.c +2 -1
index 59217aa1d1fb..8bf378164aee 100644
@@ -90,8 +90,9 @@ int generic_ide_resume(struct device *dev)
 	}
 
 	memset(&rqpm, 0, sizeof(rqpm));
-	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_PREEMPT);
+	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_PM);
 	ide_req(rq)->type = ATA_PRIV_PM_RESUME;
+	rq->rq_flags |= RQF_PM;
 	rq->special = &rqpm;
 	rqpm.pm_step = IDE_PM_START_RESUME;
 	rqpm.pm_state = PM_EVENT_ON;
drivers/scsi/scsi_lib.c +8 -3
index 9cb9a166fa0c..a65a03e2bcc4 100644
@@ -263,11 +263,16 @@ int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
 {
 	struct request *req;
 	struct scsi_request *rq;
+	blk_mq_req_flags_t blk_mq_req_flags = 0;
 	int ret = DRIVER_ERROR << 24;
 
+	if (rq_flags & RQF_PM)
+		blk_mq_req_flags |= BLK_MQ_REQ_PM;
+	rq_flags |= RQF_DV;
+	blk_mq_req_flags |= BLK_MQ_REQ_DV;
 	req = blk_get_request(sdev->request_queue,
 			data_direction == DMA_TO_DEVICE ?
-			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, BLK_MQ_REQ_PREEMPT);
+			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, blk_mq_req_flags);
 	if (IS_ERR(req))
 		return ret;
 	rq = scsi_req(req);
@@ -1356,7 +1361,7 @@ scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
 			/*
 			 * If the devices is blocked we defer normal commands.
 			 */
-			if (req && !(req->rq_flags & RQF_PREEMPT))
+			if (req && !(req->rq_flags & (RQF_PM | RQF_DV)))
 				ret = BLKPREP_DEFER;
 			break;
 		default:
@@ -1365,7 +1370,7 @@ scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
 			 * special commands.  In particular any user initiated
 			 * command is not allowed.
 			 */
-			if (req && !(req->rq_flags & RQF_PREEMPT))
+			if (req && !(req->rq_flags & (RQF_PM | RQF_DV)))
 				ret = BLKPREP_KILL;
 			break;
 		}
include/linux/blk-mq.h +4 -2
index 1da59c16f637..4ac964eeabc8 100644
@@ -221,8 +221,10 @@ enum {
 	BLK_MQ_REQ_RESERVED	= (__force blk_mq_req_flags_t)(1 << 1),
 	/* allocate internal/sched tag */
 	BLK_MQ_REQ_INTERNAL	= (__force blk_mq_req_flags_t)(1 << 2),
-	/* set RQF_PREEMPT */
-	BLK_MQ_REQ_PREEMPT	= (__force blk_mq_req_flags_t)(1 << 3),
+	/* RQF_PM will be set by the caller */
+	BLK_MQ_REQ_PM		= (__force blk_mq_req_flags_t)(1 << 3),
+	/* RQF_DV will be set by the caller */
+	BLK_MQ_REQ_DV		= (__force blk_mq_req_flags_t)(1 << 4),
 };
 
 struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
include/linux/blkdev.h +3 -2
index d6869e0e2b64..938725ef492b 100644
@@ -97,8 +97,7 @@ typedef __u32 __bitwise req_flags_t;
 #define RQF_MQ_INFLIGHT		((__force req_flags_t)(1 << 6))
 /* don't call prep for this one */
 #define RQF_DONTPREP		((__force req_flags_t)(1 << 7))
-/* set for "ide_preempt" requests and also for requests for which the SCSI
-   "quiesce" state must be ignored. */
+/* set for "ide_preempt" requests */
 #define RQF_PREEMPT		((__force req_flags_t)(1 << 8))
 /* contains copies of user pages */
 #define RQF_COPY_USER		((__force req_flags_t)(1 << 9))
@@ -127,6 +126,8 @@ typedef __u32 __bitwise req_flags_t;
 #define RQF_MQ_POLL_SLEPT	((__force req_flags_t)(1 << 20))
 /* ->timeout has been called, don't expire again */
 #define RQF_TIMED_OUT		((__force req_flags_t)(1 << 21))
+/* set for SCSI domain validation requests */
+#define RQF_DV			((__force req_flags_t)(1 << 22))
 
 /* flags that prevent us from merging requests: */
 #define RQF_NOMERGE_FLAGS \
Signature · 1 line
2.18.0
#2 lore raw

PATCH v6 2/12 scsi: Alter handling of RQF_DV requests

In reply to Bart Van Assche

Process all requests in state SDEV_CREATED instead of only RQF_DV requests. This does not change the behavior of the SCSI core because the SCSI device state is modified into another state before SCSI devices become visible in sysfs and before any device nodes are created in /dev. Do not process RQF_DV requests in state SDEV_CANCEL because only power management requests should be processed in this state. Handle all SCSI device states explicitly in scsi_prep_state_check() instead of using a default case in the switch/case statement in scsi_prep_state_check(). This allows the compiler to verify whether all states have been handled.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 drivers/scsi/scsi_lib.c | 78 +++++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 42 deletions(-)
drivers/scsi/scsi_lib.c +36 -42
index a65a03e2bcc4..8685704f6c8b 100644
@@ -1331,49 +1331,43 @@ scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
 	 * If the device is not in running state we will reject some
 	 * or all commands.
 	 */
-	if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
-		switch (sdev->sdev_state) {
-		case SDEV_OFFLINE:
-		case SDEV_TRANSPORT_OFFLINE:
-			/*
-			 * If the device is offline we refuse to process any
-			 * commands.  The device must be brought online
-			 * before trying any recovery commands.
-			 */
-			sdev_printk(KERN_ERR, sdev,
-				    "rejecting I/O to offline device\n");
-			ret = BLKPREP_KILL;
-			break;
-		case SDEV_DEL:
-			/*
-			 * If the device is fully deleted, we refuse to
-			 * process any commands as well.
-			 */
-			sdev_printk(KERN_ERR, sdev,
-				    "rejecting I/O to dead device\n");
-			ret = BLKPREP_KILL;
-			break;
-		case SDEV_BLOCK:
-		case SDEV_CREATED_BLOCK:
+	switch (sdev->sdev_state) {
+	case SDEV_RUNNING:
+	case SDEV_CREATED:
+		break;
+	case SDEV_OFFLINE:
+	case SDEV_TRANSPORT_OFFLINE:
+		/*
+		 * If the device is offline we refuse to process any commands.
+		 * The device must be brought online before trying any
+		 * recovery commands.
+		 */
+		sdev_printk(KERN_ERR, sdev,
+			    "rejecting I/O to offline device\n");
+		ret = BLKPREP_KILL;
+		break;
+	case SDEV_DEL:
+		/*
+		 * If the device is fully deleted, we refuse to process any
+		 * commands as well.
+		 */
+		sdev_printk(KERN_ERR, sdev, "rejecting I/O to dead device\n");
+		ret = BLKPREP_KILL;
+		break;
+	case SDEV_BLOCK:
+	case SDEV_CREATED_BLOCK:
+		ret = BLKPREP_DEFER;
+		break;
+	case SDEV_QUIESCE:
+		/* Only allow RQF_PM and RQF_DV requests. */
+		if (!(req->rq_flags & (RQF_PM | RQF_DV)))
 			ret = BLKPREP_DEFER;
-			break;
-		case SDEV_QUIESCE:
-			/*
-			 * If the devices is blocked we defer normal commands.
-			 */
-			if (req && !(req->rq_flags & (RQF_PM | RQF_DV)))
-				ret = BLKPREP_DEFER;
-			break;
-		default:
-			/*
-			 * For any other not fully online state we only allow
-			 * special commands.  In particular any user initiated
-			 * command is not allowed.
-			 */
-			if (req && !(req->rq_flags & (RQF_PM | RQF_DV)))
-				ret = BLKPREP_KILL;
-			break;
-		}
+		break;
+	case SDEV_CANCEL:
+		/* Only allow RQF_PM requests. */
+		if (!(req->rq_flags & RQF_PM))
+			ret = BLKPREP_KILL;
+		break;
 	}
 	return ret;
 }
Signature · 1 line
2.18.0
#7 lore raw

PATCH v6 5/12 block, scsi: Rename QUEUE_FLAG_PREEMPT_ONLY into DV_ONLY and introduce PM_ONLY

In reply to Bart Van Assche

Instead of having one queue state (PREEMPT_ONLY) in which both power management and SCSI domain validation requests are processed, rename the PREEMPT_ONLY state into DV_ONLY and introduce a new queue flag QUEUE_FLAG_PM_ONLY. Provide the new functions blk_set_pm_only() and blk_clear_pm_only() for power management and blk_set_dv_only() and blk_clear_dv_only() functions for SCSI domain validation purposes.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-core.c        | 65 ++++++++++++++++++++++++++++++-----------
 block/blk-mq-debugfs.c  |  3 +-
 drivers/scsi/scsi_lib.c | 16 +++++-----
 include/linux/blkdev.h  | 13 +++++----
 4 files changed, 66 insertions(+), 31 deletions(-)
block/blk-core.c +48 -17
index c4ff58491758..401f4927a8db 100644
@@ -421,24 +421,44 @@ void blk_sync_queue(struct request_queue *q)
 EXPORT_SYMBOL(blk_sync_queue);
 
 /**
- * blk_set_preempt_only - set QUEUE_FLAG_PREEMPT_ONLY
+ * blk_set_dv_only - set QUEUE_FLAG_DV_ONLY
  * @q: request queue pointer
  *
- * Returns the previous value of the PREEMPT_ONLY flag - 0 if the flag was not
+ * Returns the previous value of the DV_ONLY flag - 0 if the flag was not
  * set and 1 if the flag was already set.
  */
-int blk_set_preempt_only(struct request_queue *q)
+int blk_set_dv_only(struct request_queue *q)
 {
-	return blk_queue_flag_test_and_set(QUEUE_FLAG_PREEMPT_ONLY, q);
+	return blk_queue_flag_test_and_set(QUEUE_FLAG_DV_ONLY, q);
 }
-EXPORT_SYMBOL_GPL(blk_set_preempt_only);
+EXPORT_SYMBOL_GPL(blk_set_dv_only);
 
-void blk_clear_preempt_only(struct request_queue *q)
+void blk_clear_dv_only(struct request_queue *q)
 {
-	blk_queue_flag_clear(QUEUE_FLAG_PREEMPT_ONLY, q);
+	blk_queue_flag_clear(QUEUE_FLAG_DV_ONLY, q);
 	wake_up_all(&q->mq_freeze_wq);
 }
-EXPORT_SYMBOL_GPL(blk_clear_preempt_only);
+EXPORT_SYMBOL_GPL(blk_clear_dv_only);
+
+/**
+ * blk_set_pm_only - set QUEUE_FLAG_PM_ONLY
+ * @q: request queue pointer
+ *
+ * Returns the previous value of the PM_ONLY flag - 0 if the flag was not
+ * set and 1 if the flag was already set.
+ */
+int blk_set_pm_only(struct request_queue *q)
+{
+	return blk_queue_flag_test_and_set(QUEUE_FLAG_PM_ONLY, q);
+}
+EXPORT_SYMBOL_GPL(blk_set_pm_only);
+
+void blk_clear_pm_only(struct request_queue *q)
+{
+	blk_queue_flag_clear(QUEUE_FLAG_PM_ONLY, q);
+	wake_up_all(&q->mq_freeze_wq);
+}
+EXPORT_SYMBOL_GPL(blk_clear_pm_only);
 
 /**
  * __blk_run_queue_uncond - run a queue whether or not it has been stopped
@@ -910,6 +930,20 @@ struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
 }
 EXPORT_SYMBOL(blk_alloc_queue);
 
+/*
+ * Whether or not blk_queue_enter() should proceed. RQF_PM requests are always
+ * allowed. RQF_DV requests are allowed if the PM_ONLY queue flag has not been
+ * set. Other requests are only allowed if neither PM_ONLY nor DV_ONLY has been
+ * set.
+ */
+static inline bool blk_enter_allowed(struct request_queue *q,
+				     blk_mq_req_flags_t flags)
+{
+	return flags & BLK_MQ_REQ_PM ||
+		(!blk_queue_pm_only(q) &&
+		 (flags & BLK_MQ_REQ_DV || !blk_queue_dv_only(q)));
+}
+
 /**
  * blk_queue_enter() - try to increase q->q_usage_counter
  * @q: request queue pointer
@@ -917,23 +951,20 @@ EXPORT_SYMBOL(blk_alloc_queue);
  */
 int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
 {
-	const bool preempt = flags & (BLK_MQ_REQ_PM | BLK_MQ_REQ_DV);
-
 	while (true) {
 		bool success = false;
 
 		rcu_read_lock();
 		if (percpu_ref_tryget_live(&q->q_usage_counter)) {
 			/*
-			 * The code that sets the PREEMPT_ONLY flag is
-			 * responsible for ensuring that that flag is globally
-			 * visible before the queue is unfrozen.
+			 * The code that sets the PM_ONLY or DV_ONLY queue
+			 * flags is responsible for ensuring that that flag is
+			 * globally visible before the queue is unfrozen.
 			 */
-			if (preempt || !blk_queue_preempt_only(q)) {
+			if (blk_enter_allowed(q, flags))
 				success = true;
-			} else {
+			else
 				percpu_ref_put(&q->q_usage_counter);
-			}
 		}
 		rcu_read_unlock();
 
@@ -954,7 +985,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
 
 		wait_event(q->mq_freeze_wq,
 			   (atomic_read(&q->mq_freeze_depth) == 0 &&
-			    (preempt || !blk_queue_preempt_only(q))) ||
+			    blk_enter_allowed(q, flags)) ||
 			   blk_queue_dying(q));
 		if (blk_queue_dying(q))
 			return -ENODEV;
block/blk-mq-debugfs.c +2 -1
index b4e722311ae1..2ac9949bf68e 100644
@@ -132,7 +132,8 @@ static const char *const blk_queue_flag_name[] = {
 	QUEUE_FLAG_NAME(REGISTERED),
 	QUEUE_FLAG_NAME(SCSI_PASSTHROUGH),
 	QUEUE_FLAG_NAME(QUIESCED),
-	QUEUE_FLAG_NAME(PREEMPT_ONLY),
+	QUEUE_FLAG_NAME(PM_ONLY),
+	QUEUE_FLAG_NAME(DV_ONLY),
 };
 #undef QUEUE_FLAG_NAME
 
drivers/scsi/scsi_lib.c +8 -8
index eb914d8e17fd..d466f846043f 100644
@@ -2997,11 +2997,11 @@ scsi_device_suspend(struct scsi_device *sdev)
 	if (sdev->sdev_state == SDEV_SUSPENDED)
 		return 0;
 
-	blk_set_preempt_only(q);
+	blk_set_pm_only(q);
 
 	blk_mq_freeze_queue(q);
 	/*
-	 * Ensure that the effect of blk_set_preempt_only() will be visible
+	 * Ensure that the effect of blk_set_pm_only() will be visible
 	 * for percpu_ref_tryget() callers that occur after the queue
 	 * unfreeze even if the queue was already frozen before this function
 	 * was called. See also https://lwn.net/Articles/573497/.
@@ -3012,7 +3012,7 @@ scsi_device_suspend(struct scsi_device *sdev)
 	mutex_lock(&sdev->state_mutex);
 	err = scsi_device_set_state(sdev, SDEV_SUSPENDED);
 	if (err)
-		blk_clear_preempt_only(q);
+		blk_clear_pm_only(q);
 	mutex_unlock(&sdev->state_mutex);
 
 	return err;
@@ -3030,7 +3030,7 @@ EXPORT_SYMBOL(scsi_device_suspend);
 void scsi_device_unsuspend(struct scsi_device *sdev)
 {
 	mutex_lock(&sdev->state_mutex);
-	blk_clear_preempt_only(sdev->request_queue);
+	blk_clear_pm_only(sdev->request_queue);
 	if (sdev->sdev_state == SDEV_SUSPENDED)
 		scsi_device_set_state(sdev, SDEV_RUNNING);
 	mutex_unlock(&sdev->state_mutex);
@@ -3088,11 +3088,11 @@ scsi_device_quiesce(struct scsi_device *sdev)
 	 */
 	WARN_ON_ONCE(sdev->quiesced_by && sdev->quiesced_by != current);
 
-	blk_set_preempt_only(q);
+	blk_set_dv_only(q);
 
 	blk_mq_freeze_queue(q);
 	/*
-	 * Ensure that the effect of blk_set_preempt_only() will be visible
+	 * Ensure that the effect of blk_set_dv_only() will be visible
 	 * for percpu_ref_tryget() callers that occur after the queue
 	 * unfreeze even if the queue was already frozen before this function
 	 * was called. See also https://lwn.net/Articles/573497/.
@@ -3105,7 +3105,7 @@ scsi_device_quiesce(struct scsi_device *sdev)
 	if (err == 0)
 		sdev->quiesced_by = current;
 	else
-		blk_clear_preempt_only(q);
+		blk_clear_dv_only(q);
 	mutex_unlock(&sdev->state_mutex);
 
 	return err;
@@ -3125,7 +3125,7 @@ void scsi_device_unquiesce(struct scsi_device *sdev)
 	mutex_lock(&sdev->state_mutex);
 	WARN_ON_ONCE(!sdev->quiesced_by);
 	sdev->quiesced_by = NULL;
-	blk_clear_preempt_only(sdev->request_queue);
+	blk_clear_dv_only(sdev->request_queue);
 	if (sdev->sdev_state == SDEV_QUIESCE)
 		scsi_device_set_state(sdev, SDEV_RUNNING);
 	mutex_unlock(&sdev->state_mutex);
include/linux/blkdev.h +8 -5
index 938725ef492b..7717b71c6da3 100644
@@ -699,7 +699,8 @@ struct request_queue {
 #define QUEUE_FLAG_REGISTERED  26	/* queue has been registered to a disk */
 #define QUEUE_FLAG_SCSI_PASSTHROUGH 27	/* queue supports SCSI commands */
 #define QUEUE_FLAG_QUIESCED    28	/* queue has been quiesced */
-#define QUEUE_FLAG_PREEMPT_ONLY	29	/* only process REQ_PREEMPT requests */
+#define QUEUE_FLAG_PM_ONLY	29	/* only process RQF_PM requests */
+#define QUEUE_FLAG_DV_ONLY	30	/* only process RQF_DV requests */
 
 #define QUEUE_FLAG_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
 				 (1 << QUEUE_FLAG_SAME_COMP)	|	\
@@ -737,12 +738,14 @@ bool blk_queue_flag_test_and_clear(unsigned int flag, struct request_queue *q);
 	((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
 			     REQ_FAILFAST_DRIVER))
 #define blk_queue_quiesced(q)	test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
-#define blk_queue_preempt_only(q)				\
-	test_bit(QUEUE_FLAG_PREEMPT_ONLY, &(q)->queue_flags)
+#define blk_queue_pm_only(q)	test_bit(QUEUE_FLAG_PM_ONLY, &(q)->queue_flags)
+#define blk_queue_dv_only(q)	test_bit(QUEUE_FLAG_DV_ONLY, &(q)->queue_flags)
 #define blk_queue_fua(q)	test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags)
 
-extern int blk_set_preempt_only(struct request_queue *q);
-extern void blk_clear_preempt_only(struct request_queue *q);
+extern int blk_set_pm_only(struct request_queue *q);
+extern void blk_clear_pm_only(struct request_queue *q);
+extern int blk_set_dv_only(struct request_queue *q);
+extern void blk_clear_dv_only(struct request_queue *q);
 
 static inline int queue_in_flight(struct request_queue *q)
 {
Signature · 1 line
2.18.0
#10 lore raw

PATCH v6 6/12 scsi: Reallow SPI domain validation during system suspend

In reply to Bart Van Assche

Now that SCSI power management and SPI domain validation use different mechanisms for blocking SCSI command execution, remove the mechanism again that prevents system suspend during SPI domain validation.

This patch reverts 203f8c250e21 ("block, scsi: Fix race between SPI domain validation and system suspend").

Cc Martin K. Petersen <[email protected]>
Cc Woody Suwalski <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 drivers/scsi/scsi_lib.c           | 37 +++++++++++++++++++++++++++++--
 drivers/scsi/scsi_sysfs.c         |  1 +
 drivers/scsi/scsi_transport_spi.c | 15 ++-----------
 include/scsi/scsi_device.h        |  1 +
 4 files changed, 39 insertions(+), 15 deletions(-)
drivers/scsi/scsi_lib.c +35 -2
index d466f846043f..89f790e73ed6 100644
@@ -1366,6 +1366,7 @@ scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
 			ret = BLKPREP_DEFER;
 		break;
 	case SDEV_SUSPENDED:
+	case SDEV_QUIESCED_SUSPENDED:
 		/* Process RQF_PM requests only. */
 		if (!(req->rq_flags & RQF_PM))
 			ret = BLKPREP_DEFER;
@@ -2683,6 +2684,17 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
 		break;
 
 	case SDEV_QUIESCE:
+		switch (oldstate) {
+		case SDEV_RUNNING:
+		case SDEV_QUIESCED_SUSPENDED:
+		case SDEV_OFFLINE:
+		case SDEV_TRANSPORT_OFFLINE:
+			break;
+		default:
+			goto illegal;
+		}
+		break;
+
 	case SDEV_SUSPENDED:
 		switch (oldstate) {
 		case SDEV_RUNNING:
@@ -2694,6 +2706,15 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
 		}
 		break;
 
+	case SDEV_QUIESCED_SUSPENDED:
+		switch (oldstate) {
+		case SDEV_QUIESCE:
+			break;
+		default:
+			goto illegal;
+		}
+		break;
+
 	case SDEV_OFFLINE:
 	case SDEV_TRANSPORT_OFFLINE:
 		switch (oldstate) {
@@ -2994,8 +3015,13 @@ scsi_device_suspend(struct scsi_device *sdev)
 	struct request_queue *q = sdev->request_queue;
 	int err;
 
-	if (sdev->sdev_state == SDEV_SUSPENDED)
+	switch (sdev->sdev_state) {
+	case SDEV_SUSPENDED:
+	case SDEV_QUIESCED_SUSPENDED:
 		return 0;
+	default:
+		break;
+	}
 
 	blk_set_pm_only(q);
 
@@ -3011,6 +3037,8 @@ scsi_device_suspend(struct scsi_device *sdev)
 
 	mutex_lock(&sdev->state_mutex);
 	err = scsi_device_set_state(sdev, SDEV_SUSPENDED);
+	if (err)
+		err = scsi_device_set_state(sdev, SDEV_QUIESCED_SUSPENDED);
 	if (err)
 		blk_clear_pm_only(q);
 	mutex_unlock(&sdev->state_mutex);
@@ -3031,8 +3059,13 @@ void scsi_device_unsuspend(struct scsi_device *sdev)
 {
 	mutex_lock(&sdev->state_mutex);
 	blk_clear_pm_only(sdev->request_queue);
-	if (sdev->sdev_state == SDEV_SUSPENDED)
+	switch (sdev->sdev_state) {
+	case SDEV_SUSPENDED:
+	case SDEV_QUIESCED_SUSPENDED:
 		scsi_device_set_state(sdev, SDEV_RUNNING);
+	default:
+		break;
+	}
 	mutex_unlock(&sdev->state_mutex);
 }
 EXPORT_SYMBOL(scsi_device_unsuspend);
drivers/scsi/scsi_sysfs.c +1 -0
index 496c5eff4859..8c152e35ff77 100644
@@ -37,6 +37,7 @@ static const struct {
 	{ SDEV_DEL, "deleted" },
 	{ SDEV_QUIESCE, "quiesce" },
 	{ SDEV_SUSPENDED, "suspended" },
+	{ SDEV_QUIESCED_SUSPENDED, "quiesced-suspended" },
 	{ SDEV_OFFLINE,	"offline" },
 	{ SDEV_TRANSPORT_OFFLINE, "transport-offline" },
 	{ SDEV_BLOCK,	"blocked" },
drivers/scsi/scsi_transport_spi.c +2 -13
index 16bec4884249..582b18efc3fb 100644
@@ -26,7 +26,6 @@
 #include <linux/mutex.h>
 #include <linux/sysfs.h>
 #include <linux/slab.h>
-#include <linux/suspend.h>
 #include <scsi/scsi.h>
 #include "scsi_priv.h"
 #include <scsi/scsi_device.h>
@@ -1008,19 +1007,11 @@ spi_dv_device(struct scsi_device *sdev)
 	u8 *buffer;
 	const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
 
-	/*
-	 * Because this function and the power management code both call
-	 * scsi_device_quiesce(), it is not safe to perform domain validation
-	 * while suspend or resume is in progress. Hence the
-	 * lock/unlock_system_sleep() calls.
-	 */
-	lock_system_sleep();
-
 	if (unlikely(starget->spi_dv_context))
-		goto unlock;
+		return;
 
 	if (unlikely(scsi_device_get(sdev)))
-		goto unlock;
+		return;
 
 	buffer = kzalloc(len, GFP_KERNEL);
 
@@ -1056,8 +1047,6 @@ spi_dv_device(struct scsi_device *sdev)
 	kfree(buffer);
  out_put:
 	scsi_device_put(sdev);
-unlock:
-	unlock_system_sleep();
 }
 EXPORT_SYMBOL(spi_dv_device);
 
include/scsi/scsi_device.h +1 -0
index a2e3edf8be12..a67dc459b70d 100644
@@ -44,6 +44,7 @@ enum scsi_device_state {
 				 * no commands allowed */
 	SDEV_QUIESCE,		/* Only RQF_DV requests are accepted. */
 	SDEV_SUSPENDED,		/* Only RQF_PM requests are accepted. */
+	SDEV_QUIESCED_SUSPENDED,/* Only RQF_PM requests are accepted. */
 	SDEV_OFFLINE,		/* Device offlined (by error handling or
 				 * user request */
 	SDEV_TRANSPORT_OFFLINE,	/* Offlined by transport class error handler */
Signature · 1 line
2.18.0
#11 lore raw

PATCH v6 7/12 block: Move power management code into a new source file

In reply to Bart Van Assche

Move the code for runtime power management from blk-core.c into the new source file blk-pm.c. Move the corresponding declarations from <linux/blkdev.h> into <linux/blk-pm.h>. For CONFIG_PM=n, leave out the declarations of the functions that are not used in that mode. This patch not only reduces the number of #ifdefs in the block layer core code but also reduces the size of header file <linux/blkdev.h> and hence should help to reduce the build time of the Linux kernel if CONFIG_PM is not defined.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/Kconfig          |   5 ++
 block/Makefile         |   1 +
 block/blk-core.c       | 196 +----------------------------------------
 block/blk-pm.c         | 188 +++++++++++++++++++++++++++++++++++++++
 block/blk-pm.h         |  43 +++++++++
 block/elevator.c       |  22 +----
 drivers/scsi/scsi_pm.c |   1 +
 drivers/scsi/sd.c      |   1 +
 drivers/scsi/sr.c      |   1 +
 include/linux/blk-pm.h |  24 +++++
 include/linux/blkdev.h |  23 -----
 11 files changed, 266 insertions(+), 239 deletions(-)
 create mode 100644 block/blk-pm.c
 create mode 100644 block/blk-pm.h
 create mode 100644 include/linux/blk-pm.h
block/Kconfig +5 -0
index 1f2469a0123c..e213d90a5e64 100644
@@ -228,4 +228,9 @@ config BLK_MQ_RDMA
 	depends on BLOCK && INFINIBAND
 	default y
 
+config BLK_PM
+	bool
+	depends on BLOCK && PM
+	default y
+
 source block/Kconfig.iosched
block/Makefile +1 -0
index 572b33f32c07..27eac600474f 100644
@@ -37,3 +37,4 @@ obj-$(CONFIG_BLK_WBT)		+= blk-wbt.o
 obj-$(CONFIG_BLK_DEBUG_FS)	+= blk-mq-debugfs.o
 obj-$(CONFIG_BLK_DEBUG_FS_ZONED)+= blk-mq-debugfs-zoned.o
 obj-$(CONFIG_BLK_SED_OPAL)	+= sed-opal.o
+obj-$(CONFIG_BLK_PM)		+= blk-pm.o
block/blk-core.c +1 -195

214 lines not on this page · raw message

block/blk-pm.c new file +188 -0

188 lines not on this page · raw message

block/blk-pm.h new file +43 -0

43 lines not on this page · raw message

block/elevator.c +1 -21
index fa828b5bfd4b..4c15f0240c99 100644
@@ -41,6 +41,7 @@
 
 #include "blk.h"
 #include "blk-mq-sched.h"
+#include "blk-pm.h"
 #include "blk-wbt.h"
 
 static DEFINE_SPINLOCK(elv_list_lock);
@@ -557,27 +558,6 @@ void elv_bio_merged(struct request_queue *q, struct request *rq,
 		e->type->ops.sq.elevator_bio_merged_fn(q, rq, bio);
 }
 
-#ifdef CONFIG_PM
-static void blk_pm_requeue_request(struct request *rq)
-{
-	if (rq->q->dev && !(rq->rq_flags & RQF_PM))
-		rq->q->nr_pending--;
-}
-
-static void blk_pm_add_request(struct request_queue *q, struct request *rq)
-{
-	if (q->dev && !(rq->rq_flags & RQF_PM) && q->nr_pending++ == 0 &&
-	    (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
-		pm_request_resume(q->dev);
-}
-#else
-static inline void blk_pm_requeue_request(struct request *rq) {}
-static inline void blk_pm_add_request(struct request_queue *q,
-				      struct request *rq)
-{
-}
-#endif
-
 void elv_requeue_request(struct request_queue *q, struct request *rq)
 {
 	/*
drivers/scsi/scsi_pm.c +1 -0
index 6300e168701d..eb790b679eaf 100644
@@ -8,6 +8,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/export.h>
 #include <linux/async.h>
+#include <linux/blk-pm.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_device.h>
drivers/scsi/sd.c +1 -0
index bbebdc3769b0..69ab459abb98 100644
@@ -45,6 +45,7 @@
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/blkpg.h>
+#include <linux/blk-pm.h>
 #include <linux/delay.h>
 #include <linux/mutex.h>
 #include <linux/string_helpers.h>
drivers/scsi/sr.c +1 -0
index 3f3cb72e0c0c..de4413e66eca 100644
@@ -43,6 +43,7 @@
 #include <linux/interrupt.h>
 #include <linux/init.h>
 #include <linux/blkdev.h>
+#include <linux/blk-pm.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/pm_runtime.h>
include/linux/blk-pm.h new file +24 -0
new file mode 100644
index 000000000000..b80c65aba249
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _BLK_PM_H_
+#define _BLK_PM_H_
+
+struct device;
+struct request_queue;
+
+/*
+ * block layer runtime pm functions
+ */
+#ifdef CONFIG_PM
+extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev);
+extern int blk_pre_runtime_suspend(struct request_queue *q);
+extern void blk_post_runtime_suspend(struct request_queue *q, int err);
+extern void blk_pre_runtime_resume(struct request_queue *q);
+extern void blk_post_runtime_resume(struct request_queue *q, int err);
+extern void blk_set_runtime_active(struct request_queue *q);
+#else
+static inline void blk_pm_runtime_init(struct request_queue *q,
+				       struct device *dev) {}
+#endif
+
+#endif /* _BLK_PM_H_ */
include/linux/blkdev.h +0 -23
index 7717b71c6da3..7560c88a0fe2 100644
@@ -1284,29 +1284,6 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id,
 extern void blk_put_queue(struct request_queue *);
 extern void blk_set_queue_dying(struct request_queue *);
 
-/*
- * block layer runtime pm functions
- */
-#ifdef CONFIG_PM
-extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev);
-extern int blk_pre_runtime_suspend(struct request_queue *q);
-extern void blk_post_runtime_suspend(struct request_queue *q, int err);
-extern void blk_pre_runtime_resume(struct request_queue *q);
-extern void blk_post_runtime_resume(struct request_queue *q, int err);
-extern void blk_set_runtime_active(struct request_queue *q);
-#else
-static inline void blk_pm_runtime_init(struct request_queue *q,
-	struct device *dev) {}
-static inline int blk_pre_runtime_suspend(struct request_queue *q)
-{
-	return -ENOSYS;
-}
-static inline void blk_post_runtime_suspend(struct request_queue *q, int err) {}
-static inline void blk_pre_runtime_resume(struct request_queue *q) {}
-static inline void blk_post_runtime_resume(struct request_queue *q, int err) {}
-static inline void blk_set_runtime_active(struct request_queue *q) {}
-#endif
-
 /*
  * blk_plug permits building a queue of related requests by holding the I/O
  * fragments for a short period. This allows merging of sequential requests
Signature · 1 line
2.18.0
#19 lore raw

PATCH v6 10/12 block: Change the runtime power management approach (1/2)

In reply to Bart Van Assche

Instead of scheduling runtime resume of a request queue after a request has been queued, schedule asynchronous resume during request allocation. The new pm_request_resume() calls occur after blk_queue_enter() has increased the q_usage_counter request queue member. This change is needed for a later patch that will make request allocation block while the queue status is not RPM_ACTIVE.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-core.c | 2 ++
 block/blk-mq.c   | 2 ++
 block/elevator.c | 1 -
 3 files changed, 4 insertions(+), 1 deletion(-)
block/blk-core.c +2 -0
index 59dd98585eb0..f30545fb2de2 100644
@@ -972,6 +972,8 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
 		if (success)
 			return 0;
 
+		blk_pm_request_resume(q);
+
 		if (flags & BLK_MQ_REQ_NOWAIT)
 			return -EBUSY;
 
block/blk-mq.c +2 -0
index 2a0eb058ba5a..24439735f20b 100644
@@ -33,6 +33,7 @@
 #include "blk-mq.h"
 #include "blk-mq-debugfs.h"
 #include "blk-mq-tag.h"
+#include "blk-pm.h"
 #include "blk-stat.h"
 #include "blk-mq-sched.h"
 #include "blk-rq-qos.h"
@@ -473,6 +474,7 @@ static void __blk_mq_free_request(struct request *rq)
 	struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
 	const int sched_tag = rq->internal_tag;
 
+	blk_pm_mark_last_busy(rq);
 	if (rq->tag != -1)
 		blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
 	if (sched_tag != -1)
block/elevator.c +0 -1
index 00c5d8dbce16..4c15f0240c99 100644
@@ -601,7 +601,6 @@ void __elv_add_request(struct request_queue *q, struct request *rq, int where)
 	trace_block_rq_insert(q, rq);
 
 	blk_pm_add_request(q, rq);
-	blk_pm_request_resume(q);
 
 	rq->q = q;
 
Signature · 1 line
2.18.0
#22 lore raw

PATCH v6 11/12 block: Change the runtime power management approach (2/2)

In reply to Bart Van Assche

Instead of allowing requests that are not power management requests to enter the queue in runtime suspended status (RPM_SUSPENDED), make the blk_get_request() caller block. This change fixes a starvation
issue: it is now guaranteed that power management requests will be executed no matter how many blk_get_request() callers are waiting. Instead of maintaining the q->nr_pending counter, rely on q->q_usage_counter. Call pm_runtime_mark_last_busy() every time a request finishes instead of only if the queue depth drops to zero.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-core.c | 37 ++++++-------------------
 block/blk-pm.c   | 72 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 75 insertions(+), 34 deletions(-)
block/blk-core.c +8 -29
index f30545fb2de2..b0bb6b5320fe 100644
@@ -2772,30 +2772,6 @@ void blk_account_io_done(struct request *req, u64 now)
 	}
 }
 
-#ifdef CONFIG_PM
-/*
- * Don't process normal requests when queue is suspended
- * or in the process of suspending/resuming
- */
-static bool blk_pm_allow_request(struct request *rq)
-{
-	switch (rq->q->rpm_status) {
-	case RPM_RESUMING:
-	case RPM_SUSPENDING:
-		return rq->rq_flags & RQF_PM;
-	case RPM_SUSPENDED:
-		return false;
-	default:
-		return true;
-	}
-}
-#else
-static bool blk_pm_allow_request(struct request *rq)
-{
-	return true;
-}
-#endif
-
 void blk_account_io_start(struct request *rq, bool new_io)
 {
 	struct hd_struct *part;
@@ -2841,11 +2817,14 @@ static struct request *elv_next_request(struct request_queue *q)
 
 	while (1) {
 		list_for_each_entry(rq, &q->queue_head, queuelist) {
-			if (blk_pm_allow_request(rq))
-				return rq;
-
-			if (rq->rq_flags & RQF_SOFTBARRIER)
-				break;
+#ifdef CONFIG_PM
+			/*
+			 * If a request gets queued in state RPM_SUSPENDED
+			 * then that's a kernel bug.
+			 */
+			WARN_ON_ONCE(q->rpm_status == RPM_SUSPENDED);
+#endif
+			return rq;
 		}
 
 		/*
block/blk-pm.c +67 -5

114 lines not on this page · raw message

Signature · 1 line
2.18.0
#25 lore raw

PATCH v6 12/12 blk-mq: Enable support for runtime power management

In reply to Bart Van Assche

Now that the blk-mq core processes power management requests (marked with RQF_PREEMPT) in other states than RPM_ACTIVE, enable runtime power management for blk-mq.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-pm.c | 6 ------
 1 file changed, 6 deletions(-)
block/blk-pm.c +0 -6
index 977beffdccd2..e76d85dcdd67 100644
@@ -30,12 +30,6 @@
  */
 void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
 {
-	/* Don't enable runtime PM for blk-mq until it is ready */
-	if (q->mq_ops) {
-		pm_runtime_disable(dev);
-		return;
-	}
-
 	q->dev = dev;
 	q->rpm_status = RPM_ACTIVE;
 	pm_runtime_set_autosuspend_delay(q->dev, -1);
Signature · 1 line
2.18.0
#5 lore raw

PATCH v6 3/12 scsi: Only set RQF_DV for requests used for domain validation

In reply to Bart Van Assche

Instead of setting RQF_DV for all requests submitted by scsi_execute(), only set that flag for requests that are used for domain validation. Move the SCSI Parallel Interface (SPI) domain validation status from the transport data to struct scsi_target such that this status information can be accessed easily from inside scsi_execute(). This patch prevents that e.g. event checking can occur during domain validation.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 drivers/scsi/scsi_lib.c           | 6 ++++--
 drivers/scsi/scsi_transport_spi.c | 9 +++------
 include/scsi/scsi_device.h        | 1 +
 include/scsi/scsi_transport_spi.h | 1 -
 4 files changed, 8 insertions(+), 9 deletions(-)
drivers/scsi/scsi_lib.c +4 -2
index 8685704f6c8b..4d7411a7985f 100644
@@ -268,8 +268,10 @@ int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
 
 	if (rq_flags & RQF_PM)
 		blk_mq_req_flags |= BLK_MQ_REQ_PM;
-	rq_flags |= RQF_DV;
-	blk_mq_req_flags |= BLK_MQ_REQ_DV;
+	if (scsi_target(sdev)->spi_dv_context == current) {
+		rq_flags |= RQF_DV;
+		blk_mq_req_flags |= BLK_MQ_REQ_DV;
+	}
 	req = blk_get_request(sdev->request_queue,
 			data_direction == DMA_TO_DEVICE ?
 			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, blk_mq_req_flags);
drivers/scsi/scsi_transport_spi.c +3 -6
index 2ca150b16764..bf6b18768e79 100644
@@ -66,7 +66,6 @@ static struct {
 };
 
 /* Private data accessors (keep these out of the header file) */
-#define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
 #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
 
 struct spi_internal {
@@ -268,7 +267,6 @@ static int spi_setup_transport_attrs(struct transport_container *tc,
 	spi_pcomp_en(starget) = 0;
 	spi_hold_mcs(starget) = 0;
 	spi_dv_pending(starget) = 0;
-	spi_dv_in_progress(starget) = 0;
 	spi_initial_dv(starget) = 0;
 	mutex_init(&spi_dv_mutex(starget));
 
@@ -1018,14 +1016,12 @@ spi_dv_device(struct scsi_device *sdev)
 	 */
 	lock_system_sleep();
 
-	if (unlikely(spi_dv_in_progress(starget)))
+	if (unlikely(starget->spi_dv_context))
 		goto unlock;
 
 	if (unlikely(scsi_device_get(sdev)))
 		goto unlock;
 
-	spi_dv_in_progress(starget) = 1;
-
 	buffer = kzalloc(len, GFP_KERNEL);
 
 	if (unlikely(!buffer))
@@ -1043,7 +1039,9 @@ spi_dv_device(struct scsi_device *sdev)
 
 	starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
 
+	starget->spi_dv_context = current;
 	spi_dv_device_internal(sdev, buffer);
+	starget->spi_dv_context = NULL;
 
 	starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
 
@@ -1057,7 +1055,6 @@ spi_dv_device(struct scsi_device *sdev)
  out_free:
 	kfree(buffer);
  out_put:
-	spi_dv_in_progress(starget) = 0;
 	scsi_device_put(sdev);
 unlock:
 	unlock_system_sleep();
include/scsi/scsi_device.h +1 -0
index 202f4d6a4342..440834f4252e 100644
@@ -310,6 +310,7 @@ struct scsi_target {
 
 	char			scsi_level;
 	enum scsi_target_state	state;
+	struct task_struct	*spi_dv_context;
 	void 			*hostdata; /* available to low-level driver */
 	unsigned long		starget_data[0]; /* for the transport */
 	/* starget_data must be the last element!!!! */
include/scsi/scsi_transport_spi.h +0 -1
index a4fa52b4d5c5..36934ac0a5ca 100644
@@ -56,7 +56,6 @@ struct spi_transport_attrs {
 	unsigned int support_qas; /* supports quick arbitration and selection */
 	/* Private Fields */
 	unsigned int dv_pending:1; /* Internal flag: DV Requested */
-	unsigned int dv_in_progress:1;	/* Internal: DV started */
 	struct mutex dv_mutex; /* semaphore to serialise dv */
 };
 
Signature · 1 line
2.18.0
#6 lore raw

PATCH v6 4/12 scsi: Introduce the SDEV_SUSPENDED device status

In reply to Bart Van Assche

Instead of using the SDEV_QUIESCE state for both SCSI domain validation and runtime suspend, use the SDEV_QUIESCE state only for SCSI domain validation. Keep using scsi_device_quiesce() and scsi_device_unquiesce() for SCSI domain validation. Add new functions scsi_device_suspend() and scsi_device_unsuspend() for power management. Rename scsi_device_resume() into scsi_device_unquiesce() to avoid confusion.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 drivers/scsi/scsi_lib.c           | 138 ++++++++++++++++++++++++------
 drivers/scsi/scsi_pm.c            |   6 +-
 drivers/scsi/scsi_sysfs.c         |   1 +
 drivers/scsi/scsi_transport_spi.c |   2 +-
 include/scsi/scsi_device.h        |  13 +--
 5 files changed, 125 insertions(+), 35 deletions(-)
drivers/scsi/scsi_lib.c +112 -26
index 4d7411a7985f..eb914d8e17fd 100644
@@ -1365,6 +1365,11 @@ scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
 		if (!(req->rq_flags & (RQF_PM | RQF_DV)))
 			ret = BLKPREP_DEFER;
 		break;
+	case SDEV_SUSPENDED:
+		/* Process RQF_PM requests only. */
+		if (!(req->rq_flags & RQF_PM))
+			ret = BLKPREP_DEFER;
+		break;
 	case SDEV_CANCEL:
 		/* Only allow RQF_PM requests. */
 		if (!(req->rq_flags & RQF_PM))
@@ -2669,6 +2674,7 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
 		case SDEV_OFFLINE:
 		case SDEV_TRANSPORT_OFFLINE:
 		case SDEV_QUIESCE:
+		case SDEV_SUSPENDED:
 		case SDEV_BLOCK:
 			break;
 		default:
@@ -2677,6 +2683,7 @@ scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
 		break;
 
 	case SDEV_QUIESCE:
+	case SDEV_SUSPENDED:
 		switch (oldstate) {
 		case SDEV_RUNNING:
 		case SDEV_OFFLINE:
@@ -2970,19 +2977,103 @@ static void scsi_wait_for_queuecommand(struct scsi_device *sdev)
 }
 
 /**
- *	scsi_device_quiesce - Block user issued commands.
- *	@sdev:	scsi device to quiesce.
+ * scsi_device_suspend - only process RQF_PM requests
+ * @sdev: scsi device to suspend.
+ *
+ * This works by trying to transition to the SDEV_SUSPENDED state (which must be
+ * a legal transition).  When the device is in this state, only RQF_DV
+ * requests will be accepted, all others will be deferred.
+ *
+ * Must be called with user context, may sleep.
+ *
+ * Returns zero if unsuccessful or an error if not.
+ */
+int
+scsi_device_suspend(struct scsi_device *sdev)
+{
+	struct request_queue *q = sdev->request_queue;
+	int err;
+
+	if (sdev->sdev_state == SDEV_SUSPENDED)
+		return 0;
+
+	blk_set_preempt_only(q);
+
+	blk_mq_freeze_queue(q);
+	/*
+	 * Ensure that the effect of blk_set_preempt_only() will be visible
+	 * for percpu_ref_tryget() callers that occur after the queue
+	 * unfreeze even if the queue was already frozen before this function
+	 * was called. See also https://lwn.net/Articles/573497/.
+	 */
+	synchronize_rcu();
+	blk_mq_unfreeze_queue(q);
+
+	mutex_lock(&sdev->state_mutex);
+	err = scsi_device_set_state(sdev, SDEV_SUSPENDED);
+	if (err)
+		blk_clear_preempt_only(q);
+	mutex_unlock(&sdev->state_mutex);
+
+	return err;
+}
+EXPORT_SYMBOL(scsi_device_suspend);
+
+/**
+ * scsi_device_unsuspend - unsuspend processing non-RQF_DV requests
+ * @sdev: scsi device to unsuspend.
  *
- *	This works by trying to transition to the SDEV_QUIESCE state
- *	(which must be a legal transition).  When the device is in this
- *	state, only special requests will be accepted, all others will
- *	be deferred.  Since special requests may also be requeued requests,
- *	a successful return doesn't guarantee the device will be 
- *	totally quiescent.
+ * Moves the device from suspended back to running and restarts the queues.
  *
- *	Must be called with user context, may sleep.
+ * Must be called with user context, may sleep.
+ */
+void scsi_device_unsuspend(struct scsi_device *sdev)
+{
+	mutex_lock(&sdev->state_mutex);
+	blk_clear_preempt_only(sdev->request_queue);
+	if (sdev->sdev_state == SDEV_SUSPENDED)
+		scsi_device_set_state(sdev, SDEV_RUNNING);
+	mutex_unlock(&sdev->state_mutex);
+}
+EXPORT_SYMBOL(scsi_device_unsuspend);
+
+static void
+device_suspend_fn(struct scsi_device *sdev, void *data)
+{
+	scsi_device_suspend(sdev);
+}
+
+void
+scsi_target_suspend(struct scsi_target *starget)
+{
+	starget_for_each_device(starget, NULL, device_suspend_fn);
+}
+EXPORT_SYMBOL(scsi_target_suspend);
+
+static void
+device_unsuspend_fn(struct scsi_device *sdev, void *data)
+{
+	scsi_device_unsuspend(sdev);
+}
+
+void
+scsi_target_unsuspend(struct scsi_target *starget)
+{
+	starget_for_each_device(starget, NULL, device_unsuspend_fn);
+}
+EXPORT_SYMBOL(scsi_target_unsuspend);
+
+/**
+ * scsi_device_quiesce - only process RQF_DV requests
+ * @sdev: scsi device to quiesce.
  *
- *	Returns zero if unsuccessful or an error if not.
+ * This works by trying to transition to the SDEV_QUIESCE state (which must be
+ * a legal transition).  When the device is in this state, only RQF_DV
+ * requests will be accepted, all others will be deferred.
+ *
+ * Must be called with user context, may sleep.
+ *
+ * Returns zero if unsuccessful or an error if not.
  */
 int
 scsi_device_quiesce(struct scsi_device *sdev)
@@ -3022,20 +3113,15 @@ scsi_device_quiesce(struct scsi_device *sdev)
 EXPORT_SYMBOL(scsi_device_quiesce);
 
 /**
- *	scsi_device_resume - Restart user issued commands to a quiesced device.
- *	@sdev:	scsi device to resume.
+ * scsi_device_unquiesce - unquiesce processing non-RQF_DV requests
+ * @sdev: scsi device to unquiesce.
  *
- *	Moves the device from quiesced back to running and restarts the
- *	queues.
+ * Moves the device from quiesced back to running and restarts the queues.
  *
- *	Must be called with user context, may sleep.
+ * Must be called with user context, may sleep.
  */
-void scsi_device_resume(struct scsi_device *sdev)
+void scsi_device_unquiesce(struct scsi_device *sdev)
 {
-	/* check if the device state was mutated prior to resume, and if
-	 * so assume the state is being managed elsewhere (for example
-	 * device deleted during suspend)
-	 */
 	mutex_lock(&sdev->state_mutex);
 	WARN_ON_ONCE(!sdev->quiesced_by);
 	sdev->quiesced_by = NULL;
@@ -3044,7 +3130,7 @@ void scsi_device_resume(struct scsi_device *sdev)
 		scsi_device_set_state(sdev, SDEV_RUNNING);
 	mutex_unlock(&sdev->state_mutex);
 }
-EXPORT_SYMBOL(scsi_device_resume);
+EXPORT_SYMBOL(scsi_device_unquiesce);
 
 static void
 device_quiesce_fn(struct scsi_device *sdev, void *data)
@@ -3060,17 +3146,17 @@ scsi_target_quiesce(struct scsi_target *starget)
 EXPORT_SYMBOL(scsi_target_quiesce);
 
 static void
-device_resume_fn(struct scsi_device *sdev, void *data)
+device_unquiesce_fn(struct scsi_device *sdev, void *data)
 {
-	scsi_device_resume(sdev);
+	scsi_device_unquiesce(sdev);
 }
 
 void
-scsi_target_resume(struct scsi_target *starget)
+scsi_target_unquiesce(struct scsi_target *starget)
 {
-	starget_for_each_device(starget, NULL, device_resume_fn);
+	starget_for_each_device(starget, NULL, device_unquiesce_fn);
 }
-EXPORT_SYMBOL(scsi_target_resume);
+EXPORT_SYMBOL(scsi_target_unquiesce);
 
 /**
  * scsi_internal_device_block_nowait - try to transition to the SDEV_BLOCK state
drivers/scsi/scsi_pm.c +3 -3
index b44c1bb687a2..6300e168701d 100644
@@ -57,11 +57,11 @@ static int scsi_dev_type_suspend(struct device *dev,
 	/* flush pending in-flight resume operations, suspend is synchronous */
 	async_synchronize_full_domain(&scsi_sd_pm_domain);
 
-	err = scsi_device_quiesce(to_scsi_device(dev));
+	err = scsi_device_suspend(to_scsi_device(dev));
 	if (err == 0) {
 		err = cb(dev, pm);
 		if (err)
-			scsi_device_resume(to_scsi_device(dev));
+			scsi_device_unsuspend(to_scsi_device(dev));
 	}
 	dev_dbg(dev, "scsi suspend: %d\n", err);
 	return err;
@@ -74,7 +74,7 @@ static int scsi_dev_type_resume(struct device *dev,
 	int err = 0;
 
 	err = cb(dev, pm);
-	scsi_device_resume(to_scsi_device(dev));
+	scsi_device_unsuspend(to_scsi_device(dev));
 	dev_dbg(dev, "scsi resume: %d\n", err);
 
 	if (err == 0) {
drivers/scsi/scsi_sysfs.c +1 -0
index 7943b762c12d..496c5eff4859 100644
@@ -36,6 +36,7 @@ static const struct {
 	{ SDEV_CANCEL, "cancel" },
 	{ SDEV_DEL, "deleted" },
 	{ SDEV_QUIESCE, "quiesce" },
+	{ SDEV_SUSPENDED, "suspended" },
 	{ SDEV_OFFLINE,	"offline" },
 	{ SDEV_TRANSPORT_OFFLINE, "transport-offline" },
 	{ SDEV_BLOCK,	"blocked" },
drivers/scsi/scsi_transport_spi.c +1 -1
index bf6b18768e79..16bec4884249 100644
@@ -1048,7 +1048,7 @@ spi_dv_device(struct scsi_device *sdev)
 	mutex_unlock(&spi_dv_mutex(starget));
 	spi_dv_pending(starget) = 0;
 
-	scsi_target_resume(starget);
+	scsi_target_unquiesce(starget);
 
 	spi_initial_dv(starget) = 1;
 
include/scsi/scsi_device.h +8 -5
index 440834f4252e..a2e3edf8be12 100644
@@ -42,9 +42,8 @@ enum scsi_device_state {
 				 * Only error handler commands allowed */
 	SDEV_DEL,		/* device deleted 
 				 * no commands allowed */
-	SDEV_QUIESCE,		/* Device quiescent.  No block commands
-				 * will be accepted, only specials (which
-				 * originate in the mid-layer) */
+	SDEV_QUIESCE,		/* Only RQF_DV requests are accepted. */
+	SDEV_SUSPENDED,		/* Only RQF_PM requests are accepted. */
 	SDEV_OFFLINE,		/* Device offlined (by error handling or
 				 * user request */
 	SDEV_TRANSPORT_OFFLINE,	/* Offlined by transport class error handler */
@@ -415,9 +414,13 @@ extern void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt);
 extern void sdev_evt_send_simple(struct scsi_device *sdev,
 			  enum scsi_device_event evt_type, gfp_t gfpflags);
 extern int scsi_device_quiesce(struct scsi_device *sdev);
-extern void scsi_device_resume(struct scsi_device *sdev);
+extern void scsi_device_unquiesce(struct scsi_device *sdev);
 extern void scsi_target_quiesce(struct scsi_target *);
-extern void scsi_target_resume(struct scsi_target *);
+extern void scsi_target_unquiesce(struct scsi_target *);
+extern int scsi_device_suspend(struct scsi_device *sdev);
+extern void scsi_device_unsuspend(struct scsi_device *sdev);
+extern void scsi_target_suspend(struct scsi_target *);
+extern void scsi_target_unsuspend(struct scsi_target *);
 extern void scsi_scan_target(struct device *parent, unsigned int channel,
 			     unsigned int id, u64 lun,
 			     enum scsi_scan_mode rescan);
Signature · 1 line
2.18.0
#12 lore raw

PATCH v6 8/12 block, scsi: Introduce blk_pm_runtime_exit()

In reply to Bart Van Assche

Since it is possible to unbind a SCSI ULD and since unbinding removes the association between a request queue and struct device, the q->dev pointer has to be reset during unbind. Hence introduce a function in the block layer that clears request_queue.dev.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-pm.c         | 18 ++++++++++++++++++
 drivers/scsi/sd.c      |  9 ++++-----
 drivers/scsi/sr.c      |  3 ++-
 include/linux/blk-pm.h |  2 ++
 4 files changed, 26 insertions(+), 6 deletions(-)
block/blk-pm.c +18 -0
index 9b636960d285..bf8532da952d 100644
@@ -40,6 +40,24 @@ void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
 }
 EXPORT_SYMBOL(blk_pm_runtime_init);
 
+/**
+ * blk_pm_runtime_exit - runtime PM exit routine
+ * @q: the queue of the device
+ *
+ * This function should be called from the device_driver.remove() callback
+ * function to avoid that further calls to runtime power management functions
+ * occur.
+ */
+void blk_pm_runtime_exit(struct request_queue *q)
+{
+	if (!q->dev)
+		return;
+
+	pm_runtime_get_sync(q->dev);
+	q->dev = NULL;
+}
+EXPORT_SYMBOL(blk_pm_runtime_exit);
+
 /**
  * blk_pre_runtime_suspend - Pre runtime suspend check
  * @q: the queue of the device
drivers/scsi/sd.c +4 -5
index 69ab459abb98..5537762dfcfd 100644
@@ -3420,12 +3420,11 @@ static int sd_probe(struct device *dev)
  **/
 static int sd_remove(struct device *dev)
 {
-	struct scsi_disk *sdkp;
-	dev_t devt;
+	struct scsi_disk *sdkp = dev_get_drvdata(dev);
+	struct scsi_device *sdp = sdkp->device;
+	dev_t devt = disk_devt(sdkp->disk);
 
-	sdkp = dev_get_drvdata(dev);
-	devt = disk_devt(sdkp->disk);
-	scsi_autopm_get_device(sdkp->device);
+	blk_pm_runtime_exit(sdp->request_queue);
 
 	async_synchronize_full_domain(&scsi_sd_pm_domain);
 	async_synchronize_full_domain(&scsi_sd_probe_domain);
drivers/scsi/sr.c +2 -1
index de4413e66eca..476987f7ed48 100644
@@ -1002,8 +1002,9 @@ static void sr_kref_release(struct kref *kref)
 static int sr_remove(struct device *dev)
 {
 	struct scsi_cd *cd = dev_get_drvdata(dev);
+	struct scsi_device *sdev = cd->device;
 
-	scsi_autopm_get_device(cd->device);
+	blk_pm_runtime_exit(sdev->request_queue);
 
 	del_gendisk(cd->disk);
 	dev_set_drvdata(dev, NULL);
include/linux/blk-pm.h +2 -0
index b80c65aba249..6d654f41acbf 100644
@@ -11,6 +11,7 @@ struct request_queue;
  */
 #ifdef CONFIG_PM
 extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev);
+extern void blk_pm_runtime_exit(struct request_queue *q);
 extern int blk_pre_runtime_suspend(struct request_queue *q);
 extern void blk_post_runtime_suspend(struct request_queue *q, int err);
 extern void blk_pre_runtime_resume(struct request_queue *q);
@@ -19,6 +20,7 @@ extern void blk_set_runtime_active(struct request_queue *q);
 #else
 static inline void blk_pm_runtime_init(struct request_queue *q,
 				       struct device *dev) {}
+static inline void blk_pm_runtime_exit(struct request_queue *q) {}
 #endif
 
 #endif /* _BLK_PM_H_ */
Signature · 1 line
2.18.0
#18 lore raw

PATCH v6 9/12 block: Split blk_pm_add_request() and blk_pm_put_request()

In reply to Bart Van Assche

Move the pm_request_resume() and pm_runtime_mark_last_busy() calls into two new functions.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-core.c |  1 +
 block/blk-pm.h   | 36 +++++++++++++++++++++++++++++++-----
 block/elevator.c |  1 +
 3 files changed, 33 insertions(+), 5 deletions(-)
block/blk-core.c +1 -0
index 3770a36e85be..59dd98585eb0 100644
@@ -1774,6 +1774,7 @@ void __blk_put_request(struct request_queue *q, struct request *req)
 
 	blk_req_zone_write_unlock(req);
 	blk_pm_put_request(req);
+	blk_pm_mark_last_busy(req);
 
 	elv_completed_request(q, req);
 
block/blk-pm.h +31 -5
index 1ffc8ef203ec..a8564ea72a41 100644
@@ -6,8 +6,23 @@
 #include <linux/pm_runtime.h>
 
 #ifdef CONFIG_PM
+static inline void blk_pm_request_resume(struct request_queue *q)
+{
+	if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
+		       q->rpm_status == RPM_SUSPENDING))
+		pm_request_resume(q->dev);
+}
+
+static inline void blk_pm_mark_last_busy(struct request *rq)
+{
+	if (rq->q->dev && !(rq->rq_flags & RQF_PM))
+		pm_runtime_mark_last_busy(rq->q->dev);
+}
+
 static inline void blk_pm_requeue_request(struct request *rq)
 {
+	lockdep_assert_held(rq->q->queue_lock);
+
 	if (rq->q->dev && !(rq->rq_flags & RQF_PM))
 		rq->q->nr_pending--;
 }
@@ -15,17 +30,28 @@ static inline void blk_pm_requeue_request(struct request *rq)
 static inline void blk_pm_add_request(struct request_queue *q,
 				      struct request *rq)
 {
-	if (q->dev && !(rq->rq_flags & RQF_PM) && q->nr_pending++ == 0 &&
-	    (q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
-		pm_request_resume(q->dev);
+	lockdep_assert_held(q->queue_lock);
+
+	if (q->dev && !(rq->rq_flags & RQF_PM))
+		q->nr_pending++;
 }
 
 static inline void blk_pm_put_request(struct request *rq)
 {
-	if (rq->q->dev && !(rq->rq_flags & RQF_PM) && !--rq->q->nr_pending)
-		pm_runtime_mark_last_busy(rq->q->dev);
+	lockdep_assert_held(rq->q->queue_lock);
+
+	if (rq->q->dev && !(rq->rq_flags & RQF_PM))
+		--rq->q->nr_pending;
 }
 #else
+static inline void blk_pm_request_resume(struct request_queue *q)
+{
+}
+
+static inline void blk_pm_mark_last_busy(struct request *rq)
+{
+}
+
 static inline void blk_pm_requeue_request(struct request *rq)
 {
 }
block/elevator.c +1 -0
index 4c15f0240c99..00c5d8dbce16 100644
@@ -601,6 +601,7 @@ void __elv_add_request(struct request_queue *q, struct request *rq, int where)
 	trace_block_rq_insert(q, rq);
 
 	blk_pm_add_request(q, rq);
+	blk_pm_request_resume(q);
 
 	rq->q = q;
 
Signature · 1 line
2.18.0
#3 lore raw

Re: [PATCH v6 02/12] scsi: Alter handling of RQF_DV requests

In reply to Bart Van Assche
Quoted 7 lines · Bart Van Assche

Process all requests in state SDEV_CREATED instead of only RQF_DV requests. This does not change the behavior of the SCSI core because the SCSI device state is modified into another state before SCSI devices become visible in sysfs and before any device nodes are created in /dev. Do not process RQF_DV requests in state SDEV_CANCEL because only power management requests should be processed in this state.

Could you explain a bit why only PM requests should be processed in SDEV_CANCEL?

Thanks,
Ming

#8 lore raw

Re: [PATCH v6 05/12] block, scsi: Rename QUEUE_FLAG_PREEMPT_ONLY into DV_ONLY and introduce PM_ONLY

In reply to Bart Van Assche

Hi Bart

block/blk-core.c struct request_queue *blk_alloc_queue(gfp_t gfp_mask) · line 945

Quoted 13 lines · Bart Van Assche
block/blk-core.c +13 -0
+/*
+ * Whether or not blk_queue_enter() should proceed. RQF_PM requests are always
+ * allowed. RQF_DV requests are allowed if the PM_ONLY queue flag has not been
+ * set. Other requests are only allowed if neither PM_ONLY nor DV_ONLY has been
+ * set.
+ */
+static inline bool blk_enter_allowed(struct request_queue *q,
+				     blk_mq_req_flags_t flags)
+{
+	return flags & BLK_MQ_REQ_PM ||
+		(!blk_queue_pm_only(q) &&
+		 (flags & BLK_MQ_REQ_DV || !blk_queue_dv_only(q)));
+}

If a new state is indeed necessary, I think this kind of checking in hot path is inefficient. How about introduce a new state into request_queue, such as request_queue->gate_state. Set the PM_ONLY and DV_ONLY into this state, then we could just check request_queue->gate_state > 0 before do further checking.

Thanks
Jianchao

#23 lore raw

Re: [PATCH v6 11/12] block: Change the runtime power management approach (2/2)

In reply to Bart Van Assche

Hi Bart

Quoted 10 lines · Bart Van Assche
block/blk-pm.c +10 -0
+
+	blk_set_pm_only(q);
+	/*
+	 * This function only gets called if the most recent
+	 * pm_request_resume() call occurred at least autosuspend_delay_ms
+	 * ago. Since blk_queue_enter() is called by the request allocation
+	 * code before pm_request_resume(), if no requests have a tag assigned
+	 * it is safe to suspend the device.
+	 */
+	ret = -EBUSY;

block/blk-pm.c int blk_pre_runtime_suspend(struct request_queue *q) · line 142

Quoted 10 lines
block/blk-pm.c +10 -0
+	if (blk_requests_in_flight(q) == 0) {
+		/*
+		 * Call synchronize_rcu() such that later blk_queue_enter()
+		 * calls see the preempt-only state. See also
+		 * https://urldefense.proofpoint.com/v2/url?u=http-3A__lwn.net_Articles_573497_&d=DwIBAg&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=7WdAxUBeiTUTCy8v-7zXyr4qk7sx26ATvfo6QSTvZyQ&m=U9uPCJD2WnkXvdzrWaKPh2wJuk8-IHvxZ9sWDVrg2Tg&s=c9E23TPCpNQkiZpuzGztwHxjWF8qrESfRnPmI-e-Z48&e=.
+		 */
+		synchronize_rcu();
+		if (blk_requests_in_flight(q) == 0)
+			ret = 0;
+	}

I still think blk_set_pm_only should be moved after blk_requests_in_flight. Otherwise, the normal IO will be blocked for a little while if there are still busy requests.

Thanks
Jianchao

#20 lore raw

Re: [PATCH v6 10/12] block: Change the runtime power management approach (1/2)

In reply to Bart Van Assche

Hi Bart

Quoted 4 lines · Bart Van Assche

Instead of scheduling runtime resume of a request queue after a request has been queued, schedule asynchronous resume during request allocation. The new pm_request_resume() calls occur after blk_queue_enter() has increased the q_usage_counter request queue

                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 2 lines

member. This change is needed for a later patch that will make request allocation block while the queue status is not RPM_ACTIVE.

Is it "after getting q->q_usage_counter fails" ?
And also this blk_pm_request_resume will not affect the normal path. ;)

Thanks
Jianchao

#13 lore raw

Re: [PATCH v6 08/12] block, scsi: Introduce blk_pm_runtime_exit()

In reply to Bart Van Assche

Hi Bart

Quoted 21 lines · Bart Van Assche
block/blk-pm.c +10 -0
+void blk_pm_runtime_exit(struct request_queue *q)
+{
+	if (!q->dev)
+		return;
+
+	pm_runtime_get_sync(q->dev);
+	q->dev = NULL;
+}
+EXPORT_SYMBOL(blk_pm_runtime_exit);
+
 /**
  * blk_pre_runtime_suspend - Pre runtime suspend check
  * @q: the queue of the device
drivers/scsi/sd.c +0 -0
index 69ab459abb98..5537762dfcfd 100644
@@ -3420,12 +3420,11 @@ static int sd_probe(struct device *dev)
  **/
 static int sd_remove(struct device *dev)
 {

drivers/scsi/sd.c static int sd_probe(struct device *dev) · line 3427

Quoted 10 lines
drivers/scsi/sd.c +4 -5
-	struct scsi_disk *sdkp;
-	dev_t devt;
+	struct scsi_disk *sdkp = dev_get_drvdata(dev);
+	struct scsi_device *sdp = sdkp->device;
+	dev_t devt = disk_devt(sdkp->disk);
 
-	sdkp = dev_get_drvdata(dev);
-	devt = disk_devt(sdkp->disk);
-	scsi_autopm_get_device(sdkp->device);
+	blk_pm_runtime_exit(sdp->request_queue)

Based on __scsi_device_remove, sd_remove will be invoked before blk_cleanup_queue. So it should be not safe that we set the q->dev to NULL here.

We have following operations in followed patch:
+static inline void blk_pm_mark_last_busy(struct request *rq) +{
+ if (rq->q->dev && !(rq->rq_flags & RQF_PM))
+ pm_runtime_mark_last_busy(rq->q->dev);
+}

Thanks
Jianchao

#4 lore raw

Re: [PATCH v6 02/12] scsi: Alter handling of RQF_DV requests

In reply to Ming Lei
Quoted 2 lines · Ming Lei
Quoted 1 line · Bart Van Assche

Process all requests in state SDEV_CREATED instead of only =

RQF_DV

Quoted 1 line
Quoted 1 line

requests. This does not change the behavior of the SCSI core be=

cause

Quoted 1 line
Quoted 1 line

the SCSI device state is modified into another state before SCS=

I

Quoted 2 lines
Quoted 2 lines

devices become visible in sysfs and before any device nodes are created in /dev. Do not process RQF_DV requests in state SD=

EV_CANCEL

Quoted 1 line
Quoted 1 line

because only power management requests should be processed in t=

his

Quoted 4 lines
Quoted 1 line

state.

=20
Could you explain a bit why only PM requests should be processed in SDEV_CANCEL?

Hi Ming,

There is only one function that changes the device state into SDEV_CANC= EL,
namely __scsi_remove_device(). I think that in the SDEV_C= ANCEL state all
newly queued requests should be ignored except the SYNCHRONIZE CACHE and STOP commands submitted by sd_shutdown(). More information about the SC= SI
device states is available in the commit message of 9b22a8fb0edd ("Upda= ted
state model for SCSI devices").

Bart.

#9 lore raw

Re: [PATCH v6 05/12] block, scsi: Rename QUEUE_FLAG_PREEMPT_ONLY into DV_ONLY and introduce PM_ONLY

In reply to jianchao.wang
Quoted 3 lines · jianchao.wang
Quoted 2 lines · Bart Van Assche
+-/*
+- * Whether or not blk_queue_enter() should procee=

d. RQF_PM requests are always

Quoted 1 line
Quoted 1 line

+- * allowed. RQF_DV requests are allowed if the PM_

-ONLY queue flag has not been

Quoted 1 line
Quoted 1 line

+- * set. Other requests are only allowed if neither PM_

-ONLY nor DV_ONLY has been

Quoted 3 lines
Quoted 3 lines
+- * set.
+- */
+-static inline bool blk_enter_allowed(struct request�

F8-queue *q,

Quoted 5 lines
Quoted 5 lines
+-				     blk_mq_req_flags_t flags)
+-{
+-	return flags & BLK_MQ_REQ_PM ||
+-		(!blk_queue_pm_only(q) &&
+-		 (flags & BLK_MQ_REQ_DV || !blk�

F8-queue_dv_only(q)));

Quoted 3 lines
Quoted 1 line

+-}

=20
If a new state is indeed necessary, I think this kind of checking in =

hot path is inefficient.

Quoted 1 line

How about introduce a new state into request_queue, such as reque=

st_queue->gate_state.

Quoted 1 line

Set the PM_ONLY and DV_ONLY into this state, then we could ju=

st check request_queue->gate_state > 0

Quoted 1 line

before do further checking.

Hello Jianchao,

I agree with you that the hot path should be as efficient as possible. But = I
don't think that the above code adds significantly more overhead to the hot path. The requests for which we care about performance are the requests tha= t
read and write data. BLK_MQ_REQ_PM is not set for any of these = requests.
For the high performance case we care about the pm-only flag won't be set. If that flag is not set the rest of the boolean expression does not have to be evaluated (flags & BLK_MQ_REQ_DV || !blk_q=
ueue_dv_only(q)). So I think the
above change only adds one additional boolean test to the hot path. That shouldn't have a measurable performance impact.

Bart.

#21 lore raw

Re: [PATCH v6 10/12] block: Change the runtime power management approach (1/2)

In reply to jianchao.wang
Quoted 3 lines · jianchao.wang
Quoted 2 lines · Bart Van Assche

Instead of scheduling runtime resume of a request queue after a request has been queued, schedule asynchronous resume during re=

quest

Quoted 1 line
Quoted 1 line

allocation. The new pm_request_resume() calls occur aft=

er

Quoted 1 line
Quoted 1 line

blk_queue_enter() has increased the q_usage_cou=

nter request queue

Quoted 2 lines

=20

                        ^^^^^^^^^^^^^^^^

XgBeAF4AXgBeAF4AXgBeAF4AXgBeAF4AXgBeAF4AXgBeAF4AXgBeAF4AXgBeAF4AXgBeAF4-

Quoted 1 line
Quoted 1 line

member. This change is needed for a later patch that will make =

request

Quoted 4 lines
Quoted 1 line

allocation block while the queue status is not RPM_ACTIVE.

=20
Is it "after getting q->q_usage_counter fails" ? And also this blk_pm_request_resume will not affect the =

normal path. ;)

Right, the commit message needs to be brought in sync with the code.

Bart.

#24 lore raw

Re: [PATCH v6 11/12] block: Change the runtime power management approach (2/2)

In reply to jianchao.wang
Quoted 6 lines · jianchao.wang
Quoted 5 lines · Bart Van Assche
+-
+-	blk_set_pm_only(q);
+-	/*
+-	 * This function only gets called if the most recent
+-	 * pm_request_resume() call occurred at least au=

tosuspend_delay_ms

Quoted 1 line
Quoted 1 line

+- * ago. Since blk_queue_enter() is called by the=

request allocation

Quoted 1 line
Quoted 1 line

+- * code before pm_request_resume(), if no reques=

ts have a tag assigned

Quoted 6 lines
Quoted 6 lines
+-	 * it is safe to suspend the device.
+-	 */
+-	ret = -EBUSY;
+-	if (blk_requests_in_flight(q) == 0) {
+-		/*
+-		 * Call synchronize_rcu() such that later blk_q=

ueue_enter()

Quoted 3 lines
Quoted 3 lines
+-		 * calls see the preempt-only state. See also
+-		 *=20

https://urldefense.proofpoint.com/v2/url?u=http-3A__l=

wn.net_Articles_573497_&d=DwIBAg&c=RoP1YumCXCgaWH= vlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=7WdAxUBeiTUTCy8v-7zXyr4qk7sx26A= Tvfo6QSTvZyQ&m=U9uPCJD2WnkXvdzrWaKPh2wJuk8-IHvxZ9sWDVrg2Tg&s� 0-c9E23TPCpNQkiZpuzGztwHxjWF8qrESfRnPmI-e-Z48&e=

Quoted 8 lines
Quoted 6 lines

.

+-		 */
+-		synchronize_rcu();
+-		if (blk_requests_in_flight(q) == 0)
+-			ret = 0;
+-	}

=20
I still think blk_set_pm_only should be moved after blk�

F8-requests_in_flight.

Quoted 1 line

Otherwise, the normal IO will be blocked for a little while if there =

are still

Quoted 1 line

busy requests.

Hi Jianchao,

Although I think it is unlikely that the scenario you described will happen= , I
will make the change you requested.

Bart.

#14 lore raw

Re: [PATCH v6 08/12] block, scsi: Introduce blk_pm_runtime_exit()

In reply to jianchao.wang
Quoted 2 lines · jianchao.wang
Quoted 1 line · Bart Van Assche

+-void blk_pm_runtime_exit(struct request_queue=

*q)

Quoted 11 lines
Quoted 11 lines
+-{
+-	if (!q->dev)
+-		return;
+-
+-	pm_runtime_get_sync(q->dev);
+-	q->dev = NULL;
+-}
+-EXPORT_SYMBOL(blk_pm_runtime_exit);
+-

/**
* blk_pre_runtime_suspend - Pre runtime suspe=

nd check

Quoted 6 lines
Quoted 6 lines

* @q: the queue of the device

drivers/scsi/sd.c +1 -1
index 69ab459abb98..5537762dfcfd 100644
--- a/drivers/scsi/sd.c
+-+-+- b/drivers/scsi/sd.c

@@ -3420,12 +-3420,11 @@ static int sd_probe(st=

ruct device *dev)

Quoted 6 lines

drivers/scsi/sd.c static int sd_probe(struct device *dev)

Quoted 6 lines

**/
static int sd_remove(struct device *dev)
{

drivers/scsi/sd.c +1 -2
-	struct scsi_disk *sdkp;
-	dev_t devt;
+-	struct scsi_disk *sdkp = dev_get_drvdata=

(dev);

Quoted 10 lines

drivers/scsi/sd.c static int sd_probe(struct device *dev)

Quoted 7 lines
+-	struct scsi_device *sdp = sdkp->device;
+-	dev_t devt = disk_devt(sdkp->disk);

=20

drivers/scsi/sd.c +1 -3
-	sdkp = dev_get_drvdata(dev);
-	devt = disk_devt(sdkp->disk);
-	scsi_autopm_get_device(sdkp->device);
+-	blk_pm_runtime_exit(sdp->request_queue)

=20
=20
Based on __scsi_device_remove, sd_remove will be in=

voked before blk_cleanup_queue.

Quoted 4 lines

So it should be not safe that we set the q->dev to NULL here. =20
We have following operations in followed patch:
+-static inline void blk_pm_mark_last_busy(struct req=

uest *rq)

Quoted 2 lines
+-{
+-	if (rq->q->dev && !(rq->rq_flags & R=

QF_PM))

Quoted 1 line

+- pm_runtime_mark_last_busy(rq->q->dev);

-

Quoted 1 line

+-}

Hello Jianchao,

How about moving the blk_pm_runtime_exit() call into blk_cl= eanup_queue() at
a point where it is sure that there are no requests in progress? That shoul= d
avoid that blk_pm_mark_last_busy() races against blk_pm= _runtime_exit().

Thanks,

Bart.

#15 lore raw

Re: [PATCH v6 08/12] block, scsi: Introduce blk_pm_runtime_exit()

Quoted 3 lines · Bart Van Assche
Quoted 2 lines · jianchao.wang
Quoted 1 line · Bart Van Assche

+-void blk_pm_runtime_exit(struct request_

-queue *q)

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
+-{
+-	if (!q->dev)
+-		return;
+-
+-	pm_runtime_get_sync(q->dev);
+-	q->dev = NULL;
+-}
+-EXPORT_SYMBOL(blk_pm_runtime_exit);
+-

/**
* blk_pre_runtime_suspend - Pre runtime=

suspend check

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

* @q: the queue of the device

drivers/scsi/sd.c +1 -1
index 69ab459abb98..5537762dfcfd 100644
--- a/drivers/scsi/sd.c
+-+-+- b/drivers/scsi/sd.c

@@ -3420,12 +-3420,11 @@ static int sd_pr=

obe(struct device *dev)

Quoted 6 lines
Quoted 6 lines

drivers/scsi/sd.c static int sd_probe(struct device *dev)

Quoted 6 lines

**/
static int sd_remove(struct device *dev)
{

drivers/scsi/sd.c +1 -2
-	struct scsi_disk *sdkp;
-	dev_t devt;
+-	struct scsi_disk *sdkp = dev_get_d=

rvdata(dev);

Quoted 1 line
Quoted 1 line
Quoted 1 line

+- struct scsi_device *sdp = sdkp->device=

;

Quoted 1 line
Quoted 1 line
Quoted 1 line

+- dev_t devt = disk_devt(sdkp->disk);

-

Quoted 4 lines
Quoted 4 lines

drivers/scsi/sd.c static int sd_probe(struct device *dev)

Quoted 4 lines

=20

drivers/scsi/sd.c +0 -3
-	sdkp = dev_get_drvdata(dev);
-	devt = disk_devt(sdkp->disk);
-	scsi_autopm_get_device(sdkp->device)�

s-

Quoted 1 line
Quoted 1 line
Quoted 1 line

+- blk_pm_runtime_exit(sdp->request_q=

ueue)

Quoted 3 lines
Quoted 3 lines

=20
=20
Based on __scsi_device_remove, sd_remove will=

be invoked before blk_cleanup_queue.

Quoted 1 line
Quoted 1 line

So it should be not safe that we set the q->dev to NULL her=

e.

Quoted 3 lines
Quoted 3 lines

=20
We have following operations in followed patch:
+-static inline void blk_pm_mark_last_busy(stru=

ct request *rq)

Quoted 2 lines
Quoted 2 lines
+-{
+-	if (rq->q->dev && !(rq->rq_flags +=

ACY- RQF_PM))

Quoted 1 line
Quoted 1 line

+- pm_runtime_mark_last_busy(rq->q->de=

v);

Quoted 3 lines
Quoted 1 line

+-}

=20
How about moving the blk_pm_runtime_exit() call into blk+=

AF8-cleanup_queue() at

Quoted 1 line

a point where it is sure that there are no requests in progress? That=

should

Quoted 1 line

avoid that blk_pm_mark_last_busy() races against blk+=

AF8-pm_runtime_exit().

That wouldn't work because unbinding the SCSI ULD doesn't remove the reques= t
queue. How about changing blk_pm_runtime_exit() into something = like the following?

void blk_pm_runtime_exit(struct request_queue *q)

{
	if (!q->dev)
		return;

	pm_runtime_get_sync(q->dev);
	blk_freeze_queue(q);
	q->dev = NULL;
	blk_unfreeze_queue(q);
}

Bart.

#16 lore raw

Re: [PATCH v6 08/12] block, scsi: Introduce blk_pm_runtime_exit()

Hi Bart

Quoted 10 lines · Bart Van Assche

void blk_pm_runtime_exit(struct request_queue *q)

{
	if (!q->dev)
		return;

	pm_runtime_get_sync(q->dev);
	blk_freeze_queue(q);
	q->dev = NULL;
	blk_unfreeze_queue(q);
}

I'm afraid this will not work.

In following patch:

@@ -972,6 +972,8 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
if (success)
return 0;

+ blk_pm_request_resume(q);
+
if (flags & BLK_MQ_REQ_NOWAIT)
return -EBUSY;

We could still invoke blk_pm_request_resume even if the queue is frozen.

And we have blk_pm_request_resume later as following:

+static inline void blk_pm_request_resume(struct request_queue *q) +{
+ if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
+ q->rpm_status == RPM_SUSPENDING))
+ pm_request_resume(q->dev);
+}

Thanks
Jianchao

#17 lore raw

Re: [PATCH v6 08/12] block, scsi: Introduce blk_pm_runtime_exit()

Quoted 1 line · jianchao.wang

I'm afraid this will not work.

Since this patch fixes a bug that nobody has reported so far and since no later patches rely on this patch, I will leave it out.

Thanks,

Bart.