Skip to contentSkip to conversation

blk-mq: Enable runtime power management

PATCH Superseded Bart Van Assche 2 participants 18 messages Jul 25, 2018 – Aug 2, 2018

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover blk-mq: Enable runtime power management

Hello Jens,

This patch series not only enables runtime power management for blk-mq but also simplifies power management for the legacy block layer. Please consider this patch series for kernel v4.19.

Thanks,

Bart.

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

Bart Van Assche (5):
block: Fix a comment in a header file
block: Move power management functions into new source files
block: Serialize queue freezing and blk_pre_runtime_suspend() block, scsi: Rework runtime power management
blk-mq: Enable support for runtime power management

 block/Makefile            |   1 +
 block/blk-core.c          | 237 ++++----------------------------------
 block/blk-mq-debugfs.c    |   1 -
 block/blk-mq.c            |   3 +
 block/blk-pm.c            | 235 +++++++++++++++++++++++++++++++++++++
 block/elevator.c          |  11 +-
 drivers/scsi/scsi_pm.c    |   1 +
 drivers/scsi/sd.c         |   5 +-
 drivers/scsi/sr.c         |   1 +
 drivers/scsi/ufs/ufshcd.c |  10 +-
 include/linux/blk-pm.h    |  28 +++++
 include/linux/blkdev.h    |  36 ++----
 12 files changed, 309 insertions(+), 260 deletions(-)
 create mode 100644 block/blk-pm.c
 create mode 100644 include/linux/blk-pm.h
Signature · 1 line
2.18.0
#1 lore raw

PATCH v2 1/5 block: Fix a comment in a header file

Since the REQ_PREEMPT flag has been renamed into RQF_PREEMPT, update the comment that refers to that flag.

---
 include/linux/blkdev.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/blkdev.h +1 -1
index 331a6cb8805f..763482037c9b 100644
@@ -700,7 +700,7 @@ 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_PREEMPT_ONLY	29	/* only process RQF_PREEMPT requests */
 
 #define QUEUE_FLAG_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
 				 (1 << QUEUE_FLAG_SAME_COMP)	|	\
Signature · 1 line
2.18.0
#2 lore raw

PATCH v2 2/5 block: Move power management functions into new source files

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 declarations of the moved functions 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 Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/Makefile         |   1 +
 block/blk-core.c       | 183 ----------------------------------------
 block/blk-pm.c         | 186 +++++++++++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_pm.c |   1 +
 drivers/scsi/sd.c      |   1 +
 drivers/scsi/sr.c      |   1 +
 include/linux/blk-pm.h |  21 +++++
 include/linux/blkdev.h |  23 -----
 8 files changed, 211 insertions(+), 206 deletions(-)
 create mode 100644 block/blk-pm.c
 create mode 100644 include/linux/blk-pm.h
block/Makefile +1 -0
index 572b33f32c07..9dac9393bd4e 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_PM)		+= blk-pm.o
block/blk-core.c +0 -183

189 lines not on this page · raw message

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

186 lines not on this page · raw message

drivers/scsi/scsi_pm.c +1 -0
index b44c1bb687a2..a2b4179bfdf7 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 9421d9877730..e60cd8480a03 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 +21 -0
new file mode 100644
index 000000000000..fe3f4e8efbe9
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _BLK_PM_H_
+#define _BLK_PM_H_
+
+/*
+ * 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 763482037c9b..a0cf2e352a31 100644
@@ -1282,29 +1282,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
#3 lore raw

PATCH v2 3/5 block: Serialize queue freezing and blk_pre_runtime_suspend()

In reply to Bart Van Assche

Serialize these operations because the next patch will add code into blk_pre_runtime_suspend() that should not run concurrently with queue freezing nor unfreezing.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alan Stern <[email protected]>
---
 block/blk-core.c       |  5 +++++
 block/blk-mq.c         |  3 +++
 block/blk-pm.c         | 40 ++++++++++++++++++++++++++++++++++++++++
 include/linux/blk-pm.h |  9 +++++++++
 include/linux/blkdev.h |  4 ++++
 5 files changed, 61 insertions(+)
block/blk-core.c +5 -0
index 14c28197ea42..feac2b4d3b90 100644
@@ -17,6 +17,7 @@
 #include <linux/bio.h>
 #include <linux/blkdev.h>
 #include <linux/blk-mq.h>
+#include <linux/blk-pm.h>
 #include <linux/highmem.h>
 #include <linux/mm.h>
 #include <linux/kernel_stat.h>
@@ -694,6 +695,7 @@ void blk_set_queue_dying(struct request_queue *q)
 	 * prevent I/O from crossing blk_queue_enter().
 	 */
 	blk_freeze_queue_start(q);
+	blk_pm_runtime_unlock(q);
 
 	if (q->mq_ops)
 		blk_mq_wake_waiters(q);
@@ -754,6 +756,7 @@ void blk_cleanup_queue(struct request_queue *q)
 	 * prevent that q->request_fn() gets invoked after draining finished.
 	 */
 	blk_freeze_queue(q);
+	blk_pm_runtime_unlock(q);
 	spin_lock_irq(lock);
 	queue_flag_set(QUEUE_FLAG_DEAD, q);
 	spin_unlock_irq(lock);
@@ -1043,6 +1046,8 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id,
 #ifdef CONFIG_BLK_DEV_IO_TRACE
 	mutex_init(&q->blk_trace_mutex);
 #endif
+	blk_pm_init(q);
+
 	mutex_init(&q->sysfs_lock);
 	spin_lock_init(&q->__queue_lock);
 
block/blk-mq.c +3 -0
index c92ce06fd565..8d845872ea02 100644
@@ -9,6 +9,7 @@
 #include <linux/backing-dev.h>
 #include <linux/bio.h>
 #include <linux/blkdev.h>
+#include <linux/blk-pm.h>
 #include <linux/kmemleak.h>
 #include <linux/mm.h>
 #include <linux/init.h>
@@ -138,6 +139,7 @@ void blk_freeze_queue_start(struct request_queue *q)
 {
 	int freeze_depth;
 
+	blk_pm_runtime_lock(q);
 	freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
 	if (freeze_depth == 1) {
 		percpu_ref_kill(&q->q_usage_counter);
@@ -201,6 +203,7 @@ void blk_mq_unfreeze_queue(struct request_queue *q)
 		percpu_ref_reinit(&q->q_usage_counter);
 		wake_up_all(&q->mq_freeze_wq);
 	}
+	blk_pm_runtime_unlock(q);
 }
 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
 
block/blk-pm.c +40 -0
index 08d7222d4757..7dc9375a2f46 100644
@@ -3,6 +3,41 @@
 #include <linux/blk-pm.h>
 #include <linux/blkdev.h>
 #include <linux/pm_runtime.h>
+#include <linux/wait.h>
+
+/*
+ * Initialize the request queue members used by blk_pm_runtime_lock() and
+ * blk_pm_runtime_unlock().
+ */
+void blk_pm_init(struct request_queue *q)
+{
+	spin_lock_init(&q->rpm_lock);
+	init_waitqueue_head(&q->rpm_wq);
+	q->rpm_owner = NULL;
+	q->rpm_nesting_level = 0;
+}
+
+void blk_pm_runtime_lock(struct request_queue *q)
+{
+	spin_lock(&q->rpm_lock);
+	wait_event_interruptible_locked(q->rpm_wq,
+			  q->rpm_owner == NULL || q->rpm_owner == current);
+	if (q->rpm_owner == NULL)
+		q->rpm_owner = current;
+	q->rpm_nesting_level++;
+	spin_unlock(&q->rpm_lock);
+}
+
+void blk_pm_runtime_unlock(struct request_queue *q)
+{
+	spin_lock(&q->rpm_lock);
+	WARN_ON_ONCE(q->rpm_nesting_level <= 0);
+	if (--q->rpm_nesting_level == 0) {
+		q->rpm_owner = NULL;
+		wake_up(&q->rpm_wq);
+	}
+	spin_unlock(&q->rpm_lock);
+}
 
 /**
  * blk_pm_runtime_init - Block layer runtime PM initialization routine
@@ -66,6 +101,8 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 	if (!q->dev)
 		return ret;
 
+	blk_pm_runtime_lock(q);
+
 	spin_lock_irq(q->queue_lock);
 	if (q->nr_pending) {
 		ret = -EBUSY;
@@ -74,6 +111,9 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 		q->rpm_status = RPM_SUSPENDING;
 	}
 	spin_unlock_irq(q->queue_lock);
+
+	blk_pm_runtime_unlock(q);
+
 	return ret;
 }
 EXPORT_SYMBOL(blk_pre_runtime_suspend);
include/linux/blk-pm.h +9 -0
index fe3f4e8efbe9..aafcc7877e53 100644
@@ -3,10 +3,16 @@
 #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_init(struct request_queue *q);
+extern void blk_pm_runtime_lock(struct request_queue *q);
+extern void blk_pm_runtime_unlock(struct request_queue *q);
 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);
@@ -14,6 +20,9 @@ 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_init(struct request_queue *q) {}
+static inline void blk_pm_runtime_lock(struct request_queue *q) {}
+static inline void blk_pm_runtime_unlock(struct request_queue *q) {}
 static inline void blk_pm_runtime_init(struct request_queue *q,
 				       struct device *dev) {}
 #endif
include/linux/blkdev.h +4 -0
index a0cf2e352a31..3a8c20eafe58 100644
@@ -544,6 +544,10 @@ struct request_queue {
 	struct device		*dev;
 	int			rpm_status;
 	unsigned int		nr_pending;
+	spinlock_t		rpm_lock;
+	wait_queue_head_t	rpm_wq;
+	struct task_struct	*rpm_owner;
+	int			rpm_nesting_level;
 #endif
 
 	/*
Signature · 1 line
2.18.0
#6 lore raw

PATCH v2 4/5 block, scsi: Rework runtime power management

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. Use RQF_PREEMPT to mark power management requests instead of RQF_PM. This is safe because the power management core serializes system-wide suspend/resume and runtime power management state changes.

Cc Martin K. Petersen <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Alan Stern <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 block/blk-core.c          | 49 +++++++++++++++------------------------
 block/blk-mq-debugfs.c    |  1 -
 block/blk-pm.c            | 19 +++++++++++++--
 block/elevator.c          | 11 +--------
 drivers/scsi/sd.c         |  4 ++--
 drivers/scsi/ufs/ufshcd.c | 10 ++++----
 include/linux/blkdev.h    |  7 ++----
 7 files changed, 46 insertions(+), 55 deletions(-)
block/blk-core.c +19 -30
index feac2b4d3b90..195a99de7c7e 100644
@@ -689,6 +689,16 @@ void blk_set_queue_dying(struct request_queue *q)
 {
 	blk_queue_flag_set(QUEUE_FLAG_DYING, q);
 
+#ifdef CONFIG_PM
+	/*
+	 * Avoid that runtime power management tries to modify the state of
+	 * q->q_usage_counter after that counter has been transitioned to the
+	 * "dead" state.
+	 */
+	if (q->dev)
+		pm_runtime_dont_use_autosuspend(q->dev);
+#endif
+
 	/*
 	 * When queue DYING flag is set, we need to block new req
 	 * entering queue, so we call blk_freeze_queue_start() to
@@ -1728,7 +1738,7 @@ EXPORT_SYMBOL_GPL(part_round_stats);
 #ifdef CONFIG_PM
 static void blk_pm_put_request(struct request *rq)
 {
-	if (rq->q->dev && !(rq->rq_flags & RQF_PM) && !--rq->q->nr_pending)
+	if (rq->q->dev && !(rq->rq_flags & RQF_PREEMPT))
 		pm_runtime_mark_last_busy(rq->q->dev);
 }
 #else
@@ -2745,30 +2755,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;
@@ -2814,11 +2800,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-mq-debugfs.c +0 -1
index cb1e6cf7ac48..994bdd41feb2 100644
@@ -324,7 +324,6 @@ static const char *const rqf_name[] = {
 	RQF_NAME(ELVPRIV),
 	RQF_NAME(IO_STAT),
 	RQF_NAME(ALLOCED),
-	RQF_NAME(PM),
 	RQF_NAME(HASHED),
 	RQF_NAME(STATS),
 	RQF_NAME(SPECIAL_PAYLOAD),
block/blk-pm.c +17 -2
index 7dc9375a2f46..9f1130381322 100644
@@ -73,6 +73,13 @@ void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
 }
 EXPORT_SYMBOL(blk_pm_runtime_init);
 
+static void blk_unprepare_runtime_suspend(struct request_queue *q)
+{
+	blk_queue_flag_clear(QUEUE_FLAG_PREEMPT_ONLY, q);
+	/* Because QUEUE_FLAG_PREEMPT_ONLY has been cleared. */
+	wake_up_all(&q->mq_freeze_wq);
+}
+
 /**
  * blk_pre_runtime_suspend - Pre runtime suspend check
  * @q: the queue of the device
@@ -102,9 +109,11 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 		return ret;
 
 	blk_pm_runtime_lock(q);
+	blk_set_preempt_only(q);
+	percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
 
 	spin_lock_irq(q->queue_lock);
-	if (q->nr_pending) {
+	if (!percpu_ref_is_zero(&q->q_usage_counter)) {
 		ret = -EBUSY;
 		pm_runtime_mark_last_busy(q->dev);
 	} else {
@@ -112,6 +121,7 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 	}
 	spin_unlock_irq(q->queue_lock);
 
+	percpu_ref_switch_to_percpu(&q->q_usage_counter);
 	blk_pm_runtime_unlock(q);
 
 	return ret;
@@ -144,6 +154,9 @@ void blk_post_runtime_suspend(struct request_queue *q, int err)
 		pm_runtime_mark_last_busy(q->dev);
 	}
 	spin_unlock_irq(q->queue_lock);
+
+	if (err)
+		blk_unprepare_runtime_suspend(q);
 }
 EXPORT_SYMBOL(blk_post_runtime_suspend);
 
@@ -191,13 +204,15 @@ void blk_post_runtime_resume(struct request_queue *q, int err)
 	spin_lock_irq(q->queue_lock);
 	if (!err) {
 		q->rpm_status = RPM_ACTIVE;
-		__blk_run_queue(q);
 		pm_runtime_mark_last_busy(q->dev);
 		pm_request_autosuspend(q->dev);
 	} else {
 		q->rpm_status = RPM_SUSPENDED;
 	}
 	spin_unlock_irq(q->queue_lock);
+
+	if (!err)
+		blk_unprepare_runtime_suspend(q);
 }
 EXPORT_SYMBOL(blk_post_runtime_resume);
 
block/elevator.c +1 -10
index fa828b5bfd4b..68174953e730 100644
@@ -558,20 +558,13 @@ void elv_bio_merged(struct request_queue *q, struct request *rq,
 }
 
 #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 &&
+	if (q->dev && !(rq->rq_flags & RQF_PREEMPT) &&
 	    (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)
 {
@@ -592,8 +585,6 @@ void elv_requeue_request(struct request_queue *q, struct request *rq)
 
 	rq->rq_flags &= ~RQF_STARTED;
 
-	blk_pm_requeue_request(rq);
-
 	__elv_add_request(q, rq, ELEVATOR_INSERT_REQUEUE);
 }
 
drivers/scsi/sd.c +2 -2
index e60cd8480a03..876401f4764e 100644
@@ -1628,7 +1628,7 @@ static int sd_sync_cache(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr)
 		 * flush everything.
 		 */
 		res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, sshdr,
-				timeout, SD_MAX_RETRIES, 0, RQF_PM, NULL);
+				timeout, SD_MAX_RETRIES, 0, RQF_PREEMPT, NULL);
 		if (res == 0)
 			break;
 	}
@@ -3488,7 +3488,7 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
 		return -ENODEV;
 
 	res = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
-			SD_TIMEOUT, SD_MAX_RETRIES, 0, RQF_PM, NULL);
+			   SD_TIMEOUT, SD_MAX_RETRIES, 0, RQF_PREEMPT, NULL);
 	if (res) {
 		sd_print_result(sdkp, "Start/Stop Unit failed", res);
 		if (driver_byte(res) & DRIVER_SENSE)
drivers/scsi/ufs/ufshcd.c +5 -5
index 397081d320b1..4a16d6e90e65 100644
@@ -7219,7 +7219,7 @@ ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
 
 	ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
 			UFSHCD_REQ_SENSE_SIZE, NULL, NULL,
-			msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
+			msecs_to_jiffies(1000), 3, 0, RQF_PREEMPT, NULL);
 	if (ret)
 		pr_err("%s: failed with err %d\n", __func__, ret);
 
@@ -7280,12 +7280,12 @@ static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
 	cmd[4] = pwr_mode << 4;
 
 	/*
-	 * Current function would be generally called from the power management
-	 * callbacks hence set the RQF_PM flag so that it doesn't resume the
-	 * already suspended childs.
+	 * Current function would be generally called from the power
+	 * management callbacks hence set the RQF_PREEMPT flag so that it
+	 * doesn't resume the already suspended childs.
 	 */
 	ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
-			START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
+			   START_STOP_TIMEOUT, 0, 0, RQF_PREEMPT, NULL);
 	if (ret) {
 		sdev_printk(KERN_WARNING, sdp,
 			    "START_STOP failed for power mode: %d, result %x\n",
include/linux/blkdev.h +2 -5
index 3a8c20eafe58..4cee277fd1af 100644
@@ -99,8 +99,8 @@ 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 requests that must be processed even if QUEUE_FLAG_PREEMPT_ONLY has
+   been set, e.g. power management requests and "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))
@@ -114,8 +114,6 @@ typedef __u32 __bitwise req_flags_t;
 #define RQF_IO_STAT		((__force req_flags_t)(1 << 13))
 /* request came from our alloc pool */
 #define RQF_ALLOCED		((__force req_flags_t)(1 << 14))
-/* runtime pm request */
-#define RQF_PM			((__force req_flags_t)(1 << 15))
 /* on IO scheduler merge hash */
 #define RQF_HASHED		((__force req_flags_t)(1 << 16))
 /* IO stats tracking on */
@@ -543,7 +541,6 @@ struct request_queue {
 #ifdef CONFIG_PM
 	struct device		*dev;
 	int			rpm_status;
-	unsigned int		nr_pending;
 	spinlock_t		rpm_lock;
 	wait_queue_head_t	rpm_wq;
 	struct task_struct	*rpm_owner;
Signature · 1 line
2.18.0
#17 lore raw

PATCH v2 5/5 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 Alan Stern <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 block/blk-pm.c | 4 ----
 1 file changed, 4 deletions(-)
block/blk-pm.c +0 -4
index 9f1130381322..d9e64bfb3007 100644
@@ -62,10 +62,6 @@ void blk_pm_runtime_unlock(struct request_queue *q)
  */
 void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
 {
-	/* not support for RQF_PM and ->rpm_status in blk-mq yet */
-	if (q->mq_ops)
-		return;
-
 	q->dev = dev;
 	q->rpm_status = RPM_ACTIVE;
 	pm_runtime_set_autosuspend_delay(q->dev, -1);
Signature · 1 line
2.18.0
#7 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

In reply to Bart Van Assche

Hi Bart

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

Quoted 5 lines · Bart Van Assche
block/blk-pm.c +1 -0
@@ -102,9 +109,11 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 		return ret;
 
 	blk_pm_runtime_lock(q);
+	blk_set_preempt_only(q);

We only stop non-RQF_PM request entering when RPM_SUSPENDING and RPM_RESUMING. blk_pre_runtime_suspend should only _check_ whether runtime suspend is allowed. So we should not set preempt_only here.

Quoted 14 lines
block/blk-pm.c +3 -1
+	percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
 
 	spin_lock_irq(q->queue_lock);
-	if (q->nr_pending) {
+	if (!percpu_ref_is_zero(&q->q_usage_counter)) {
 		ret = -EBUSY;
 		pm_runtime_mark_last_busy(q->dev);
 	} else {
@@ -112,6 +121,7 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 	}
 	spin_unlock_irq(q->queue_lock);
 
+	percpu_ref_switch_to_percpu(&q->q_usage_counter);
 	blk_pm_runtime_unlock(q);
#4 lore raw

Re: [PATCH v2 3/5] block: Serialize queue freezing and blk_pre_runtime_suspend()

In reply to Bart Van Assche

Hi Bart

block/blk-pm.c line 29

Quoted 11 lines · Bart Van Assche
block/blk-pm.c +11 -0
+
+void blk_pm_runtime_lock(struct request_queue *q)
+{
+	spin_lock(&q->rpm_lock);
+	wait_event_interruptible_locked(q->rpm_wq,
+			  q->rpm_owner == NULL || q->rpm_owner == current);
+	if (q->rpm_owner == NULL)
+		q->rpm_owner = current;
+	q->rpm_nesting_level++;
+	spin_unlock(&q->rpm_lock);
+}

The lock which wait_event_interruptible_locked want to hold is the wq.lock. Please refer to comment of wait_event_interruptible_locked

* It must be called with wq.lock being held. This spinlock is
* unlocked while sleeping but @condition testing is done while lock
* is held and when this macro exits the lock is held.

Thanks
Jianchao

#8 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

In reply to jianchao.wang
Quoted 14 lines · jianchao.wang

Hi Bart

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

Quoted 5 lines · Bart Van Assche
block/blk-pm.c +1 -0
@@ -102,9 +109,11 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 		return ret;
 
 	blk_pm_runtime_lock(q);
+	blk_set_preempt_only(q);

We only stop non-RQF_PM request entering when RPM_SUSPENDING and RPM_RESUMING. blk_pre_runtime_suspend should only _check_ whether runtime suspend is allowed. So we should not set preempt_only here.

Quoted 1 line

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

Quoted 1 line
block/blk-pm.c +1 -0
+	percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);

In addition, .runtime_suspend is invoked under spinlock and irq-disabled. So sleep is forbidden here.
Please refer to rpm_suspend

* This function must be called under dev->power.lock with interrupts disabled

Thanks
Jianchao

Quoted 14 lines
Quoted 13 lines
block/blk-pm.c +2 -1
 	spin_lock_irq(q->queue_lock);
-	if (q->nr_pending) {
+	if (!percpu_ref_is_zero(&q->q_usage_counter)) {
 		ret = -EBUSY;
 		pm_runtime_mark_last_busy(q->dev);
 	} else {
@@ -112,6 +121,7 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 	}
 	spin_unlock_irq(q->queue_lock);
 
+	percpu_ref_switch_to_percpu(&q->q_usage_counter);
 	blk_pm_runtime_unlock(q);
#9 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

Quoted 6 lines · jianchao.wang

In addition, .runtime_suspend is invoked under spinlock and irq-disabled. So sleep is forbidden here.
Please refer to rpm_suspend

* This function must be called under dev->power.lock with interrupts disabled

__rpm_callback will unlock the spinlock before invoke the callback. Sorry for the noise.

Thanks
Jianchao

#5 lore raw

Re: [PATCH v2 3/5] block: Serialize queue freezing and blk_pre_runtime_suspend()

In reply to jianchao.wang
Quoted 3 lines · jianchao.wang
Quoted 2 lines · Bart Van Assche
+-
+-void blk_pm_runtime_lock(struct request_queue=

*q)

Quoted 3 lines
Quoted 3 lines
+-{
+-	spin_lock(&q->rpm_lock);
+-	wait_event_interruptible_locked(q->rpm_w=

q,

Quoted 1 line
Quoted 1 line

+- q->rpm_owner == NULL || q->rpm�

F8-owner == current);

Quoted 7 lines
Quoted 5 lines
+-	if (q->rpm_owner == NULL)
+-		q->rpm_owner = current;
+-	q->rpm_nesting_level+-+-;
+-	spin_unlock(&q->rpm_lock);
+-}

=20
The lock which wait_event_interruptible_locked want to ho=

ld is the wq.lock.

The above code is indeed wrong. This is what I think it should be changed into:

+-void blk_pm_runtime_lock(struct request_queue *q)
+-{
+-	might_sleep();
+-
+-	spin_lock(&q->rpm_lock);
+-	wait_event_exclusive_cmd(q->rpm_wq,
+-			q->rpm_owner == NULL || q->rpm_owner =
PQ- current,
+-			spin_unlock(&q->rpm_lock), spin_lock(&q->r=
pm_lock));
+-	if (q->rpm_owner == NULL)
+-		q->rpm_owner = current;
+-	q->rpm_nesting_level+-+-;
+-	spin_unlock(&q->rpm_lock);
+-}

Bart.

#10 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

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

@@ -102,9 +-109,11 @@ int blk_pre_runtime�

F8-suspend(struct request_queue *q)

Quoted 6 lines
Quoted 4 lines

return ret;
=20
blk_pm_runtime_lock(q);
+- blk_set_preempt_only(q);

=20
We only stop non-RQF_PM request entering when RPM_SUSPENDING =

and RPM_RESUMING.

Quoted 1 line

blk_pre_runtime_suspend should only _check_ wheth=

er runtime suspend is allowed.

Quoted 8 lines

So we should not set preempt_only here.
=20

Quoted 1 line

+- percpu_ref_switch_to_atomic_sync(&q-=

q_usage_counter);

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

Quoted 4 lines

=20

block/blk-pm.c +1 -1
 	spin_lock_irq(q->queue_lock);
-	if (q->nr_pending) {
+-	if (!percpu_ref_is_zero(&q->q_us=

age_counter)) {

Quoted 4 lines
Quoted 4 lines
block/blk-pm.c +0 -0
 		ret = -EBUSY;
 		pm_runtime_mark_last_busy(q->dev);
 	} else {

@@ -112,6 +-121,7 @@ int blk_pre_runtime�

8-suspend(struct request_queue *q)

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
 	}
 	spin_unlock_irq(q->queue_lock);

=20
+- percpu_ref_switch_to_percpu(&q->q�

8-usage_counter);

Quoted 1 line
Quoted 1 line

blk_pm_runtime_unlock(q);

Hello Jianchao,

There is only one caller of blk_post_runtime_suspend(), namely sdev_runtime_suspend(). That function calls pm->runtime_sus= pend() before
it calls blk_post_runtime_suspend(). I think it would be wrong = to set the
PREEMPT_ONLY flag from inside blk_post_runtime_suspend() be= cause that could
cause pm->runtime_suspend() while a request is in progress.

Bart.

#11 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

Quoted 29 lines · Bart Van Assche
Quoted 23 lines · jianchao.wang

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

Quoted 5 lines · Bart Van Assche
block/blk-pm.c +1 -0
@@ -102,9 +109,11 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 		return ret;
 
 	blk_pm_runtime_lock(q);
+	blk_set_preempt_only(q);

We only stop non-RQF_PM request entering when RPM_SUSPENDING and RPM_RESUMING. blk_pre_runtime_suspend should only _check_ whether runtime suspend is allowed. So we should not set preempt_only here.

Quoted 12 lines
block/blk-pm.c +2 -1
+	percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
 
 	spin_lock_irq(q->queue_lock);
-	if (q->nr_pending) {
+	if (!percpu_ref_is_zero(&q->q_usage_counter)) {
 		ret = -EBUSY;
 		pm_runtime_mark_last_busy(q->dev);
 	} else {
@@ -112,6 +121,7 @@ int blk_pre_runtime_suspend(struct request_queue *q)
 	}
 	spin_unlock_irq(q->queue_lock);
 
Quoted 2 lines

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

Quoted 2 lines
block/blk-pm.c +1 -0
+	percpu_ref_switch_to_percpu(&q->q_usage_counter);
 	blk_pm_runtime_unlock(q);

Hello Jianchao,

Quoted 6 lines

There is only one caller of blk_post_runtime_suspend(), namely sdev_runtime_suspend(). That function calls pm->runtime_suspend() before it calls blk_post_runtime_suspend(). I think it would be wrong to set the PREEMPT_ONLY flag from inside blk_post_runtime_suspend() because that could cause pm->runtime_suspend() while a request is in progress.

Hi Bart

If q_usage_counter is not zero here, we will leave the request_queue in preempt only mode. The request_queue should be set to preempt only mode only when we confirm we could set rpm_status to RPM_SUSPENDING or RPM_RESUMING.

Maybe we could set the PREEMPT_ONLY after we confirm we could set the rpm_status to RPM_SUSPENDING.
Something like:

	percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
	if (!percpu_ref_is_zero(&q->q_usage_counter)) {
		ret = -EBUSY;
		pm_runtime_mark_last_busy(q->dev);
     	} else {
		blk_set_preempt_only(q);
		if (!percpu_ref_is_zero(&q->q_usage_counter) {
			ret = -EBUSY;
			pm_runtime_mark_last_busy(q->dev);
			blk_clear_preempt_only(q);
		} else {
			q->rpm_status = RPM_SUSPENDIN;    
		}
        }

Thanks
Jianchao

#12 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

Quoted 1 line · jianchao.wang

If q_usage_counter is not zero here, we will leave the reques=

t_queue in preempt only mode.

That's on purpose. If q_usage_counter is not zero then blk_pre+= AF8-runtime_suspend()
will return -EBUSY. That error code will be passed to blk_post_runt= ime_suspend()
and that will cause that function to clear QUEUE_FLAG_PREEMPT_O= NLY.

Quoted 1 line

The request_queue should be set to preempt only mode only when we=

confirm we could set

Quoted 1 line

rpm_status to RPM_SUSPENDING or RPM_RESUMING.

Why do you think this?

Bart.

#15 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

In reply to jianchao.wang
Quoted 3 lines · jianchao.wang

Something like:
=20

	percpu_ref_switch_to_atomic_sync(&q->q�

F8-usage_counter);

Quoted 1 line
	if (!percpu_ref_is_zero(&q->q_usage_=

counter)) {

Quoted 5 lines
		ret = -EBUSY;
		pm_runtime_mark_last_busy(q->dev);
     	} else {
		blk_set_preempt_only(q);
		if (!percpu_ref_is_zero(&q->q_usage_

-counter) {

Quoted 7 lines
			ret = -EBUSY;
			pm_runtime_mark_last_busy(q->dev);
			blk_clear_preempt_only(q);
		} else {
			q->rpm_status = RPM_SUSPENDIN;   =20
		}
        }

I think this code is racy. Because there is no memory barrier in blk_queue_enter() between the percpu_ref_tryget_live() = and the
blk_queue_preempt_only() calls, the context that sets the PREEM= PT_ONLY flag
has to use synchronize_rcu() or call_rcu() to ensure that blk_q= ueue_enter()
sees the PREEMPT_ONLY flag after it has called percpu_ref_tryge= t_live().
See also http://lwn.net/Articles/573497/.

Bart.

#13 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

In reply to Bart Van Assche

Hi Bart

Quoted 7 lines · Bart Van Assche
Quoted 1 line · jianchao.wang

If q_usage_counter is not zero here, we will leave the request_queue in preempt only mode.

That's on purpose. If q_usage_counter is not zero then blk_pre_runtime_suspend() will return -EBUSY. That error code will be passed to blk_post_runtime_suspend() and that will cause that function to clear QUEUE_FLAG_PREEMPT_ONLY.

static int sdev_runtime_suspend(struct device *dev)

{
	const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
	struct scsi_device *sdev = to_scsi_device(dev);
	int err = 0;

	err = blk_pre_runtime_suspend(sdev->request_queue);
	if (err)
		return err;
	if (pm && pm->runtime_suspend)
		err = pm->runtime_suspend(dev);
	blk_post_runtime_suspend(sdev->request_queue, err);

	return err;
}

If blk_pre_runtime_suspend returns -EBUSY, blk_post_runtime_suspend will not be invoked.

Quoted 4 lines
Quoted 2 lines

The request_queue should be set to preempt only mode only when we confirm we could set rpm_status to RPM_SUSPENDING or RPM_RESUMING.

Why do you think this?

https://marc.info/?l=linux-scsi&m=133727953625963&w=2
"
If q->rpm_status is RPM_SUSPENDED, they shouldn't do anything -- act as though the queue is empty. If q->rpm_status is RPM_SUSPENDING or RPM_RESUMING, they should hand over the request only if it has the REQ_PM flag set.
"
In additon, if we set the preempt only here unconditionally, the normal IO will be blocked during the blk_pre_runtime_suspend. In your patch, q_usage_counter will be switched to atomic mode, this could cost some time. Is it really OK ?

Thanks
Jianchao

Quoted 4 lines

Bart.

#16 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

In reply to Bart Van Assche
Quoted 18 lines · Bart Van Assche
Quoted 16 lines · jianchao.wang

Something like:

	percpu_ref_switch_to_atomic_sync(&q->q_usage_counter);
	if (!percpu_ref_is_zero(&q->q_usage_counter)) {
		ret = -EBUSY;
		pm_runtime_mark_last_busy(q->dev);
     	} else {
		blk_set_preempt_only(q);
		if (!percpu_ref_is_zero(&q->q_usage_counter) {
			ret = -EBUSY;
			pm_runtime_mark_last_busy(q->dev);
			blk_clear_preempt_only(q);
		} else {
			q->rpm_status = RPM_SUSPENDIN;    
		}
        }
Quoted 6 lines

I think this code is racy. Because there is no memory barrier in
blk_queue_enter() between the percpu_ref_tryget_live() and the
blk_queue_preempt_only() calls, the context that sets the PREEMPT_ONLY flag
has to use synchronize_rcu() or call_rcu() to ensure that blk_queue_enter()
sees the PREEMPT_ONLY flag after it has called percpu_ref_tryget_live().
See also https://urldefense.proofpoint.com/v2/url?u=http-3A__lwn.net_Articles_573497_&d=DwIFgQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=7WdAxUBeiTUTCy8v-7zXyr4qk7sx26ATvfo6QSTvZyQ&m=mkAOXQtxuVTAej2tBjOvEed2h4TRvX3mE-EPeNEUD5E&s=xTgTB2x1JwvRUQVyOL8m3rhbbk8xhOkZMC_Io9bmpFc&e=.

Yes, a synchrorize_rcu is indeed needed here to ensure a q_usage_counter is got successfully with increasing one, or unsuccessfully without increasing one.

Thanks
Jianchao

#14 lore raw

Re: [PATCH v2 4/5] block, scsi: Rework runtime power management

In reply to jianchao.wang
Quoted 5 lines · jianchao.wang

static int sdev_runtime_suspend(struct device *dev)

{
	const struct dev_pm_ops *pm = dev->driver ? dev-=

driver->pm : NULL;
struct scsi_device *sdev = to_scsi_device(dev)�

Ds-

Quoted 3 lines
	int err = 0;
=20
	err = blk_pre_runtime_suspend(sdev->request_=

queue);

Quoted 5 lines
	if (err)
		return err;
	if (pm && pm->runtime_suspend)
		err = pm->runtime_suspend(dev);
	blk_post_runtime_suspend(sdev->request_queue, er=

r);

Quoted 5 lines

=20

	return err;
}

=20
If blk_pre_runtime_suspend returns -EBUSY, blk_post�

F8-runtime_suspend will not be invoked.

Right, I will fix this.

Quoted 1 line
Quoted 1 line
Quoted 1 line

The request_queue should be set to preempt only mode =

only when we confirm we could set

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

rpm_status to RPM_SUSPENDING or RPM_RESUMING.

=20
Why do you think this?

=20
https://marc.info/?l=linux-scsi&m=133727953625963&w�

D0-2

Quoted 2 lines

"
If q->rpm_status is RPM_SUSPENDED, they shouldn't do anyt=

hing -- act as though the queue is

Quoted 1 line

empty. If q->rpm_status is RPM_SUSPENDING or RPM_RES=

UMING, they should hand over the request

Quoted 2 lines

only if it has the REQ_PM flag set.
"

I think the blk_pre_runtime_suspend() callers guarantee that q-=

Quoted 1 line

rpm_status == RPM_ACTIVE

before blk_pre_runtime_suspend() is called. I will add a WARN� F8-ON_ONCE() statement that
verifies that.

Quoted 1 line

In additon, if we set the preempt only here unconditionally, the norm=

al IO will be blocked

Quoted 1 line

during the blk_pre_runtime_suspend. In your patch, q_=

usage_counter will be switched to atomic mode,

Quoted 1 line

this could cost some time. Is it really OK ?

I will see what I can do about this.

Bart.