Skip to contentSkip to conversation

block/scsi: safe SCSI quiescing

PATCH In review Ming Lei 4 participants 17 messages Oct 3, 2017 – Nov 7, 2017

Subsystems (5)
Stable branch VFS SCSI Block layer +1 more subsystems
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v8 cover block/scsi: safe SCSI quiescing

Hi Jens,

Please consider this patchset for V4.15, and it fixes one kind of long-term I/O hang issue in either block legacy path or blk-mq.

The current SCSI quiesce isn't safe and easy to trigger I/O deadlock.

Once SCSI device is put into QUIESCE, no new request except for RQF_PREEMPT can be dispatched to SCSI successfully, and scsi_device_quiesce() just simply waits for completion of I/Os dispatched to SCSI stack. It isn't enough at all.

Because new request still can be comming, but all the allocated requests can't be dispatched successfully, so request pool can be consumed up easily.

Then request with RQF_PREEMPT can't be allocated and wait forever, then system hangs forever, such as during system suspend or sending SCSI domain alidation in case of transport_spi.

Both IO hang inside system suspend[1] or SCSI domain validation were reported before.

This patch introduces preempt only mode, and solves the issue by allowing RQF_PREEMP only during SCSI quiesce.

Both SCSI and SCSI_MQ have this IO deadlock issue, this patch fixes them all.

V8:
- fix one race as pointed out by Bart
- pass 'op' to blk_queue_enter() as suggested by Christoph

V7:
- add Reviewed-by & Tested-by
- one line change in patch 5 for checking preempt request

V6:
- borrow Bart's idea of preempt only, with clean implementation(patch 5/patch 6)
- needn't any external driver's dependency, such as MD's change

V5:
- fix one tiny race by introducing blk_queue_enter_preempt_freeze() given this change is small enough compared with V4, I added tested-by directly

V4:
- reorganize patch order to make it more reasonable
- support nested preempt freeze, as required by SCSI transport spi
- check preempt freezing in slow path of of blk_queue_enter()
- add "SCSI: transport_spi: resume a quiesced device"
- wake up freeze queue in setting dying for both blk-mq and legacy
- rename blk_mq_[freeze|unfreeze]_queue() in one patch
- rename .mq_freeze_wq and .mq_freeze_depth
- improve comment

V3:
- introduce q->preempt_unfreezing to fix one bug of preempt freeze
- call blk_queue_enter_live() only when queue is preempt frozen
- cleanup a bit on the implementation of preempt freeze
- only patch 6 and 7 are changed

V2:
- drop the 1st patch in V1 because percpu_ref_is_dying() is enough as pointed by Tejun
- introduce preempt version of blk_[freeze|unfreeze]_queue
- sync between preempt freeze and normal freeze
- fix warning from percpu-refcount as reported by Oleksandr

[1] https://marc.info/?t=150340250100013&r=3&w=2

Thanks,
Ming

Bart Van Assche (1):
block: Convert RQF_PREEMPT into REQ_PREEMPT

Ming Lei (7):
blk-mq: only run hw queues for blk-mq
block: tracking request allocation with q_usage_counter
block: pass 'op' to blk_queue_enter()
percpu-refcount: introduce __percpu_ref_tryget_live
blk-mq: return if queue is frozen via current blk_freeze_queue_start
block: support PREEMPT_ONLY
SCSI: set block queue at preempt only when SCSI device is put into
quiesce

 block/blk-core.c                | 66 +++++++++++++++++++++++++++++++++++++----
 block/blk-mq-debugfs.c          |  2 +-
 block/blk-mq.c                  | 26 ++++++++--------
 block/blk-mq.h                  |  1 -
 block/blk-timeout.c             |  2 +-
 block/blk.h                     |  2 +-
 drivers/ide/ide-atapi.c         |  3 +-
 drivers/ide/ide-io.c            |  2 +-
 drivers/ide/ide-pm.c            |  4 +--
 drivers/scsi/scsi_lib.c         | 31 +++++++++++++++----
 fs/block_dev.c                  |  4 +--
 include/linux/blk-mq.h          |  4 +--
 include/linux/blk_types.h       |  6 ++++
 include/linux/blkdev.h          | 10 ++++---
 include/linux/percpu-refcount.h | 27 ++++++++++-------
 15 files changed, 137 insertions(+), 53 deletions(-)
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH v8 1/8 blk-mq: only run hw queues for blk-mq

This patch just makes it explicitely.

---
 block/blk-mq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/blk-mq.c +2 -1
index 98a18609755e..6fd9f86fc86d 100644
@@ -125,7 +125,8 @@ void blk_freeze_queue_start(struct request_queue *q)
 	freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
 	if (freeze_depth == 1) {
 		percpu_ref_kill(&q->q_usage_counter);
-		blk_mq_run_hw_queues(q, false);
+		if (q->mq_ops)
+			blk_mq_run_hw_queues(q, false);
 	}
 }
 EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Signature · 1 line
2.9.5
author
#2 lore raw

PATCH v8 2/8 block: tracking request allocation with q_usage_counter

In reply to Ming Lei

This usage is basically same with blk-mq, so that we can support to freeze legacy queue easily.

Also 'wake_up_all(&q->mq_freeze_wq)' has to be moved into blk_set_queue_dying() since both legacy and blk-mq may wait on the wait queue of .mq_freeze_wq.

---
 block/blk-core.c | 15 +++++++++++++++
 block/blk-mq.c   |  7 -------
 2 files changed, 15 insertions(+), 7 deletions(-)
block/blk-core.c +15 -0
index 048be4aa6024..900eaa8eefa6 100644
@@ -610,6 +610,12 @@ void blk_set_queue_dying(struct request_queue *q)
 		}
 		spin_unlock_irq(q->queue_lock);
 	}
+
+	/*
+	 * We need to ensure that processes currently waiting on
+	 * the queue are notified as well.
+	 */
+	wake_up_all(&q->mq_freeze_wq);
 }
 EXPORT_SYMBOL_GPL(blk_set_queue_dying);
 
@@ -1395,16 +1401,22 @@ static struct request *blk_old_get_request(struct request_queue *q,
 					   unsigned int op, gfp_t gfp_mask)
 {
 	struct request *rq;
+	int ret = 0;
 
 	WARN_ON_ONCE(q->mq_ops);
 
 	/* create ioc upfront */
 	create_io_context(gfp_mask, q->node);
 
+	ret = blk_queue_enter(q, !(gfp_mask & __GFP_DIRECT_RECLAIM) ||
+			(op & REQ_NOWAIT));
+	if (ret)
+		return ERR_PTR(ret);
 	spin_lock_irq(q->queue_lock);
 	rq = get_request(q, op, NULL, gfp_mask);
 	if (IS_ERR(rq)) {
 		spin_unlock_irq(q->queue_lock);
+		blk_queue_exit(q);
 		return rq;
 	}
 
@@ -1576,6 +1588,7 @@ void __blk_put_request(struct request_queue *q, struct request *req)
 		blk_free_request(rl, req);
 		freed_request(rl, sync, rq_flags);
 		blk_put_rl(rl);
+		blk_queue_exit(q);
 	}
 }
 EXPORT_SYMBOL_GPL(__blk_put_request);
@@ -1857,8 +1870,10 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
 	 * Grab a free request. This is might sleep but can not fail.
 	 * Returns with the queue unlocked.
 	 */
+	blk_queue_enter_live(q);
 	req = get_request(q, bio->bi_opf, bio, GFP_NOIO);
 	if (IS_ERR(req)) {
+		blk_queue_exit(q);
 		__wbt_done(q->rq_wb, wb_acct);
 		if (PTR_ERR(req) == -ENOMEM)
 			bio->bi_status = BLK_STS_RESOURCE;
block/blk-mq.c +0 -7
index 6fd9f86fc86d..10c1f49f663d 100644
@@ -256,13 +256,6 @@ void blk_mq_wake_waiters(struct request_queue *q)
 	queue_for_each_hw_ctx(q, hctx, i)
 		if (blk_mq_hw_queue_mapped(hctx))
 			blk_mq_tag_wakeup_all(hctx->tags, true);
-
-	/*
-	 * If we are called because the queue has now been marked as
-	 * dying, we need to ensure that processes currently waiting on
-	 * the queue are notified as well.
-	 */
-	wake_up_all(&q->mq_freeze_wq);
 }
 
 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
Signature · 1 line
2.9.5
author
#3 lore raw

PATCH v8 3/8 block: Convert RQF_PREEMPT into REQ_PREEMPT

In reply to Ming Lei

From: Bart Van Assche <[email protected]>

This patch does not change any functionality but makes the REQ_PREEMPT flag available to blk_get_request(). A later patch will add code to blk_get_request() that checks the REQ_PREEMPT flag. Note: the IDE sense_rq request is allocated statically so there is no blk_get_request() call that corresponds to this request.

Cc Ming Lei <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 block/blk-mq-debugfs.c    |  2 +-
 drivers/ide/ide-atapi.c   |  3 +--
 drivers/ide/ide-io.c      |  2 +-
 drivers/ide/ide-pm.c      |  4 ++--
 drivers/scsi/scsi_lib.c   | 11 ++++++-----
 include/linux/blk_types.h |  6 ++++++
 include/linux/blkdev.h    |  3 ---
 7 files changed, 17 insertions(+), 14 deletions(-)
block/blk-mq-debugfs.c +1 -1
index 980e73095643..62ac248a4984 100644
@@ -266,6 +266,7 @@ static const char *const cmd_flag_name[] = {
 	CMD_FLAG_NAME(BACKGROUND),
 	CMD_FLAG_NAME(NOUNMAP),
 	CMD_FLAG_NAME(NOWAIT),
+	CMD_FLAG_NAME(PREEMPT),
 };
 #undef CMD_FLAG_NAME
 
@@ -279,7 +280,6 @@ static const char *const rqf_name[] = {
 	RQF_NAME(MIXED_MERGE),
 	RQF_NAME(MQ_INFLIGHT),
 	RQF_NAME(DONTPREP),
-	RQF_NAME(PREEMPT),
 	RQF_NAME(COPY_USER),
 	RQF_NAME(FAILED),
 	RQF_NAME(QUIET),
drivers/ide/ide-atapi.c +1 -2
index 14d1e7d9a1d6..1258739d5fa1 100644
@@ -211,9 +211,8 @@ void ide_prep_sense(ide_drive_t *drive, struct request *rq)
 	}
 
 	sense_rq->rq_disk = rq->rq_disk;
-	sense_rq->cmd_flags = REQ_OP_DRV_IN;
+	sense_rq->cmd_flags = REQ_OP_DRV_IN | REQ_PREEMPT;
 	ide_req(sense_rq)->type = ATA_PRIV_SENSE;
-	sense_rq->rq_flags |= RQF_PREEMPT;
 
 	req->cmd[0] = GPCMD_REQUEST_SENSE;
 	req->cmd[4] = cmd_len;
drivers/ide/ide-io.c +1 -1
index 3a234701d92c..06ffccd0fb10 100644
@@ -539,7 +539,7 @@ void do_ide_request(struct request_queue *q)
 		 */
 		if ((drive->dev_flags & IDE_DFLAG_BLOCKED) &&
 		    ata_pm_request(rq) == 0 &&
-		    (rq->rq_flags & RQF_PREEMPT) == 0) {
+		    (rq->cmd_flags & REQ_PREEMPT) == 0) {
 			/* there should be no pending command at this point */
 			ide_unlock_port(hwif);
 			goto plug_device;
drivers/ide/ide-pm.c +2 -2
index 544f02d673ca..f8d2709dcd56 100644
@@ -89,9 +89,9 @@ int generic_ide_resume(struct device *dev)
 	}
 
 	memset(&rqpm, 0, sizeof(rqpm));
-	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
+	rq = blk_get_request(drive->queue, REQ_OP_DRV_IN | REQ_PREEMPT,
+			     __GFP_RECLAIM);
 	ide_req(rq)->type = ATA_PRIV_PM_RESUME;
-	rq->rq_flags |= RQF_PREEMPT;
 	rq->special = &rqpm;
 	rqpm.pm_step = IDE_PM_START_RESUME;
 	rqpm.pm_state = PM_EVENT_ON;
drivers/scsi/scsi_lib.c +6 -5
index 9cf6a80fe297..62f905b22821 100644
@@ -253,8 +253,9 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
 	int ret = DRIVER_ERROR << 24;
 
 	req = blk_get_request(sdev->request_queue,
-			data_direction == DMA_TO_DEVICE ?
-			REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, __GFP_RECLAIM);
+			      (data_direction == DMA_TO_DEVICE ?
+			       REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN) | REQ_PREEMPT,
+			      __GFP_RECLAIM);
 	if (IS_ERR(req))
 		return ret;
 	rq = scsi_req(req);
@@ -268,7 +269,7 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
 	rq->retries = retries;
 	req->timeout = timeout;
 	req->cmd_flags |= flags;
-	req->rq_flags |= rq_flags | RQF_QUIET | RQF_PREEMPT;
+	req->rq_flags |= rq_flags | RQF_QUIET;
 
 	/*
 	 * head injection *required* here otherwise quiesce won't work
@@ -1301,7 +1302,7 @@ scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
 			/*
 			 * If the devices is blocked we defer normal commands.
 			 */
-			if (!(req->rq_flags & RQF_PREEMPT))
+			if (!(req->cmd_flags & REQ_PREEMPT))
 				ret = BLKPREP_DEFER;
 			break;
 		default:
@@ -1310,7 +1311,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->rq_flags & RQF_PREEMPT))
+			if (!(req->cmd_flags & REQ_PREEMPT))
 				ret = BLKPREP_KILL;
 			break;
 		}
include/linux/blk_types.h +6 -0
index a2d2aa709cef..b6b6bf44e462 100644
@@ -229,6 +229,11 @@ enum req_flag_bits {
 	__REQ_NOUNMAP,		/* do not free blocks when zeroing */
 
 	__REQ_NOWAIT,           /* Don't wait if request will block */
+	/*
+	 * set for "ide_preempt" requests and also for requests for which the
+	 * SCSI "quiesce" state must be ignored.
+	 */
+	__REQ_PREEMPT,
 	__REQ_NR_BITS,		/* stops here */
 };
 
@@ -248,6 +253,7 @@ enum req_flag_bits {
 
 #define REQ_NOUNMAP		(1ULL << __REQ_NOUNMAP)
 #define REQ_NOWAIT		(1ULL << __REQ_NOWAIT)
+#define REQ_PREEMPT		(1ULL << __REQ_PREEMPT)
 
 #define REQ_FAILFAST_MASK \
 	(REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER)
include/linux/blkdev.h +0 -3
index 02fa42d24b52..ab6b6c0392c2 100644
@@ -96,9 +96,6 @@ 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. */
-#define RQF_PREEMPT		((__force req_flags_t)(1 << 8))
 /* contains copies of user pages */
 #define RQF_COPY_USER		((__force req_flags_t)(1 << 9))
 /* vaguely specified driver internal error.  Ignored by the block layer */
Signature · 1 line
2.9.5
author
#4 lore raw

PATCH v8 4/8 block: pass 'op' to blk_queue_enter()

In reply to Ming Lei

We need to check if the request to be allocated is PREEMPT_ONLY, and have to pass REQ_PREEEMPT flag to blk_queue_eneter(), so pass 'op' to blk_queue_enter() directly.

---
 block/blk-core.c       | 11 ++++++-----
 block/blk-mq.c         |  3 ++-
 block/blk-timeout.c    |  2 +-
 fs/block_dev.c         |  4 ++--
 include/linux/blkdev.h |  2 +-
 5 files changed, 12 insertions(+), 10 deletions(-)
block/blk-core.c +6 -5
index 900eaa8eefa6..1bb566245d37 100644
@@ -766,7 +766,7 @@ struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
 }
 EXPORT_SYMBOL(blk_alloc_queue);
 
-int blk_queue_enter(struct request_queue *q, bool nowait)
+int blk_queue_enter(struct request_queue *q, unsigned int op)
 {
 	while (true) {
 		int ret;
@@ -774,7 +774,7 @@ int blk_queue_enter(struct request_queue *q, bool nowait)
 		if (percpu_ref_tryget_live(&q->q_usage_counter))
 			return 0;
 
-		if (nowait)
+		if (op & REQ_NOWAIT)
 			return -EBUSY;
 
 		/*
@@ -1408,8 +1408,8 @@ static struct request *blk_old_get_request(struct request_queue *q,
 	/* create ioc upfront */
 	create_io_context(gfp_mask, q->node);
 
-	ret = blk_queue_enter(q, !(gfp_mask & __GFP_DIRECT_RECLAIM) ||
-			(op & REQ_NOWAIT));
+	ret = blk_queue_enter(q, (gfp_mask & __GFP_DIRECT_RECLAIM) ? op :
+			op | REQ_NOWAIT);
 	if (ret)
 		return ERR_PTR(ret);
 	spin_lock_irq(q->queue_lock);
@@ -1436,6 +1436,7 @@ struct request *blk_get_request(struct request_queue *q, unsigned int op,
 		req = blk_mq_alloc_request(q, op,
 			(gfp_mask & __GFP_DIRECT_RECLAIM) ?
 				0 : BLK_MQ_REQ_NOWAIT);
+
 		if (!IS_ERR(req) && q->mq_ops->initialize_rq_fn)
 			q->mq_ops->initialize_rq_fn(req);
 	} else {
@@ -2216,7 +2217,7 @@ blk_qc_t generic_make_request(struct bio *bio)
 	do {
 		struct request_queue *q = bio->bi_disk->queue;
 
-		if (likely(blk_queue_enter(q, bio->bi_opf & REQ_NOWAIT) == 0)) {
+		if (likely(blk_queue_enter(q, bio->bi_opf) == 0)) {
 			struct bio_list lower, same;
 
 			/* Create a fresh bio_list for all subordinate requests */
block/blk-mq.c +2 -1
index 10c1f49f663d..bf89549074bb 100644
@@ -384,7 +384,8 @@ struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
 	struct request *rq;
 	int ret;
 
-	ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
+	ret = blk_queue_enter(q, !(flags & BLK_MQ_REQ_NOWAIT) ? op :
+			op | REQ_NOWAIT);
 	if (ret)
 		return ERR_PTR(ret);
 
block/blk-timeout.c +1 -1
index 17ec83bb0900..071c6de9154d 100644
@@ -134,7 +134,7 @@ void blk_timeout_work(struct work_struct *work)
 	struct request *rq, *tmp;
 	int next_set = 0;
 
-	if (blk_queue_enter(q, true))
+	if (blk_queue_enter(q, REQ_NOWAIT))
 		return;
 	spin_lock_irqsave(q->queue_lock, flags);
 
fs/block_dev.c +2 -2
index 93d088ffc05c..98cf2d7ee9d3 100644
@@ -674,7 +674,7 @@ int bdev_read_page(struct block_device *bdev, sector_t sector,
 	if (!ops->rw_page || bdev_get_integrity(bdev))
 		return result;
 
-	result = blk_queue_enter(bdev->bd_queue, false);
+	result = blk_queue_enter(bdev->bd_queue, 0);
 	if (result)
 		return result;
 	result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, false);
@@ -710,7 +710,7 @@ int bdev_write_page(struct block_device *bdev, sector_t sector,
 
 	if (!ops->rw_page || bdev_get_integrity(bdev))
 		return -EOPNOTSUPP;
-	result = blk_queue_enter(bdev->bd_queue, false);
+	result = blk_queue_enter(bdev->bd_queue, 0);
 	if (result)
 		return result;
 
include/linux/blkdev.h +1 -1
index ab6b6c0392c2..4c688385d866 100644
@@ -960,7 +960,7 @@ extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
 extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
 			 struct scsi_ioctl_command __user *);
 
-extern int blk_queue_enter(struct request_queue *q, bool nowait);
+extern int blk_queue_enter(struct request_queue *q, unsigned int op);
 extern void blk_queue_exit(struct request_queue *q);
 extern void blk_start_queue(struct request_queue *q);
 extern void blk_start_queue_async(struct request_queue *q);
Signature · 1 line
2.9.5
author
#5 lore raw

PATCH v8 5/8 percpu-refcount: introduce __percpu_ref_tryget_live

In reply to Ming Lei

Block layer need to call this function after holding rcu lock in a real hot path, so introduce this helper.

Cc Bart Van Assche <[email protected]>
Cc Tejun Heo <[email protected]>
---
 include/linux/percpu-refcount.h | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
include/linux/percpu-refcount.h +17 -10
index c13dceb87b60..a0f22586a28d 100644
@@ -221,6 +221,21 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref)
 	return ret;
 }
 
+static inline bool __percpu_ref_tryget_live(struct percpu_ref *ref)
+{
+	unsigned long __percpu *percpu_count;
+	bool ret = false;
+
+	if (__ref_is_percpu(ref, &percpu_count)) {
+		this_cpu_inc(*percpu_count);
+		ret = true;
+	} else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) {
+		ret = atomic_long_inc_not_zero(&ref->count);
+	}
+
+	return ret;
+}
+
 /**
  * percpu_ref_tryget_live - try to increment a live percpu refcount
  * @ref: percpu_ref to try-get
@@ -238,18 +253,10 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref)
  */
 static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)
 {
-	unsigned long __percpu *percpu_count;
-	bool ret = false;
+	bool ret;
 
 	rcu_read_lock_sched();
-
-	if (__ref_is_percpu(ref, &percpu_count)) {
-		this_cpu_inc(*percpu_count);
-		ret = true;
-	} else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) {
-		ret = atomic_long_inc_not_zero(&ref->count);
-	}
-
+	ret = __percpu_ref_tryget_live(ref);
 	rcu_read_unlock_sched();
 
 	return ret;
Signature · 1 line
2.9.5
maintainer
#6 lore raw

Re: [PATCH V8 5/8] percpu-refcount: introduce __percpu_ref_tryget_live

Hello,

Quoted 2 lines · Ming Lei

Block layer need to call this function after holding rcu lock in a real hot path, so introduce this helper.

The patch description is too anemic. It doesn't even describe what changes are being made, let alone justifying them.

Thanks.

Signature · 1 line
tejun
author
#7 lore raw

Re: [PATCH V8 5/8] percpu-refcount: introduce __percpu_ref_tryget_live

Quoted 8 lines · Tejun Heo

Hello,

Quoted 2 lines · Ming Lei

Block layer need to call this function after holding rcu lock in a real hot path, so introduce this helper.

The patch description is too anemic. It doesn't even describe what changes are being made, let alone justifying them.

Sorry for Cc you, and this change is needed by the following patch:

	https://marc.info/?l=linux-scsi&m=150703956929333&w=2

Block layer need to call percpu_ref_tryget_live() with holding RCU read lock, given it is a hot I/O path, that is why I make this patch so that we can avoid nested RCU read lock and save a bit cost.

--
Ming

maintainer
#8 lore raw

Re: [PATCH V8 5/8] percpu-refcount: introduce __percpu_ref_tryget_live

Hello,

Quoted 13 lines · Ming Lei
Quoted 8 lines · Tejun Heo

Hello,

Quoted 2 lines · Ming Lei

Block layer need to call this function after holding rcu lock in a real hot path, so introduce this helper.

The patch description is too anemic. It doesn't even describe what changes are being made, let alone justifying them.

Sorry for Cc you, and this change is needed by the following patch:

Quoted 6 lines
	https://marc.info/?l=linux-scsi&m=150703956929333&w=2

Block layer need to call percpu_ref_tryget_live() with holding RCU read lock, given it is a hot I/O path, that is why I make this patch so that we can avoid nested RCU read lock and save a bit cost.

I'm not necessarily against it but please name it sth like percpu_ref_tryget_live_locked(), add lockdep assertions in the inner function and document properly.

Thanks.

Signature · 1 line
tejun
#9 lore raw

Re: [PATCH V8 5/8] percpu-refcount: introduce __percpu_ref_tryget_live

In reply to Ming Lei
Quoted 2 lines · Ming Lei

Block layer need to call this function after holding rcu lock in a real hot path, so introduce this helper.

Since it is allowed to nest rcu_read_lock_sched() calls I don't think that this patch is necessary.

Bart.

author
#10 lore raw

Re: [PATCH V8 5/8] percpu-refcount: introduce __percpu_ref_tryget_live

Quoted 6 lines · Bart Van Assche
Quoted 2 lines · Ming Lei

Block layer need to call this function after holding rcu lock in a real hot path, so introduce this helper.

Since it is allowed to nest rcu_read_lock_sched() calls I don't think that this patch is necessary.

Yeah, I know that, with this patch, we can avoid the nest RCU lock. As I mentioned, it is a real hot path, so maybe better to not introduce the extra RCU read lock/unlock.

If you guys doesn't care the little cost, I can remove that.

Signature · 1 line
Ming
author
#11 lore raw

PATCH v8 6/8 blk-mq: return if queue is frozen via current blk_freeze_queue_start

In reply to Ming Lei

We need this return value in the following patch to decide if a explicit synchronize_rcu() is needed.

Cc Bart Van Assche <[email protected]>
---
 block/blk-mq.c         | 13 ++++++++-----
 block/blk-mq.h         |  1 -
 block/blk.h            |  2 +-
 include/linux/blk-mq.h |  4 ++--
 4 files changed, 11 insertions(+), 9 deletions(-)
block/blk-mq.c +8 -5
index bf89549074bb..80d6dd99d7f1 100644
@@ -118,7 +118,7 @@ void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
 	blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
 }
 
-void blk_freeze_queue_start(struct request_queue *q)
+bool blk_freeze_queue_start(struct request_queue *q)
 {
 	int freeze_depth;
 
@@ -127,7 +127,9 @@ void blk_freeze_queue_start(struct request_queue *q)
 		percpu_ref_kill(&q->q_usage_counter);
 		if (q->mq_ops)
 			blk_mq_run_hw_queues(q, false);
+		return true;
 	}
+	return false;
 }
 EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
 
@@ -150,7 +152,7 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
  * Guarantee no request is in use, so we can change any data structure of
  * the queue afterward.
  */
-void blk_freeze_queue(struct request_queue *q)
+bool blk_freeze_queue(struct request_queue *q)
 {
 	/*
 	 * In the !blk_mq case we are only calling this to kill the
@@ -159,17 +161,18 @@ void blk_freeze_queue(struct request_queue *q)
 	 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
 	 * exported to drivers as the only user for unfreeze is blk_mq.
 	 */
-	blk_freeze_queue_start(q);
+	bool ret = blk_freeze_queue_start(q);
 	blk_mq_freeze_queue_wait(q);
+	return ret;
 }
 
-void blk_mq_freeze_queue(struct request_queue *q)
+bool blk_mq_freeze_queue(struct request_queue *q)
 {
 	/*
 	 * ...just an alias to keep freeze and unfreeze actions balanced
 	 * in the blk_mq_* namespace
 	 */
-	blk_freeze_queue(q);
+	return blk_freeze_queue(q);
 }
 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
 
block/blk-mq.h +0 -1
index ef15b3414da5..41044a8662ca 100644
@@ -26,7 +26,6 @@ struct blk_mq_ctx {
 } ____cacheline_aligned_in_smp;
 
 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
-void blk_mq_freeze_queue(struct request_queue *q);
 void blk_mq_free_queue(struct request_queue *q);
 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr);
 void blk_mq_wake_waiters(struct request_queue *q);
block/blk.h +1 -1
index fcb9775b997d..fa1b33d3fcbd 100644
@@ -65,7 +65,7 @@ void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
 void blk_queue_bypass_start(struct request_queue *q);
 void blk_queue_bypass_end(struct request_queue *q);
 void __blk_queue_free_tags(struct request_queue *q);
-void blk_freeze_queue(struct request_queue *q);
+bool blk_freeze_queue(struct request_queue *q);
 
 static inline void blk_queue_enter_live(struct request_queue *q)
 {
include/linux/blk-mq.h +2 -2
index 50c6485cb04f..d6195925e6c3 100644
@@ -253,9 +253,9 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async);
 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);
 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
 		busy_tag_iter_fn *fn, void *priv);
-void blk_mq_freeze_queue(struct request_queue *q);
+bool blk_mq_freeze_queue(struct request_queue *q);
 void blk_mq_unfreeze_queue(struct request_queue *q);
-void blk_freeze_queue_start(struct request_queue *q);
+bool blk_freeze_queue_start(struct request_queue *q);
 void blk_mq_freeze_queue_wait(struct request_queue *q);
 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
 				     unsigned long timeout);
Signature · 1 line
2.9.5
author
#12 lore raw

PATCH v8 7/8 block: support PREEMPT_ONLY

In reply to Ming Lei

When queue is in PREEMPT_ONLY mode, only REQ_PREEMPT request can be allocated and dispatched, other requests won't be allowed to enter I/O path.

This is useful for supporting safe SCSI quiesce.

Part of this patch is from Bart's '[PATCH v4 4∕7] block: Add the QUEUE_FLAG_PREEMPT_ONLY request queue flag'.

---
 block/blk-core.c       | 44 +++++++++++++++++++++++++++++++++++++++++---
 include/linux/blkdev.h |  5 +++++
 2 files changed, 46 insertions(+), 3 deletions(-)
block/blk-core.c +41 -3
index 1bb566245d37..7849cc1687bc 100644
@@ -346,6 +346,34 @@ void blk_sync_queue(struct request_queue *q)
 }
 EXPORT_SYMBOL(blk_sync_queue);
 
+void blk_set_preempt_only(struct request_queue *q, bool preempt_only)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(q->queue_lock, flags);
+	if (preempt_only)
+		queue_flag_set(QUEUE_FLAG_PREEMPT_ONLY, q);
+	else
+		queue_flag_clear(QUEUE_FLAG_PREEMPT_ONLY, q);
+	spin_unlock_irqrestore(q->queue_lock, flags);
+
+	/*
+	 * The synchronize_rcu() implicied in blk_mq_freeze_queue()
+	 * or the explicit one will make sure the above write on
+	 * PREEMPT_ONLY is observed in blk_queue_enter() before
+	 * running blk_mq_unfreeze_queue().
+	 *
+	 * blk_mq_freeze_queue() also drains up any request in queue,
+	 * so blk_queue_enter() will see the above updated value of
+	 * PREEMPT flag before any new allocation.
+	 */
+	if (!blk_mq_freeze_queue(q))
+		synchronize_rcu();
+
+	blk_mq_unfreeze_queue(q);
+}
+EXPORT_SYMBOL(blk_set_preempt_only);
+
 /**
  * __blk_run_queue_uncond - run a queue whether or not it has been stopped
  * @q:	The queue to run
@@ -771,8 +799,16 @@ int blk_queue_enter(struct request_queue *q, unsigned int op)
 	while (true) {
 		int ret;
 
-		if (percpu_ref_tryget_live(&q->q_usage_counter))
-			return 0;
+		rcu_read_lock_sched();
+		if (__percpu_ref_tryget_live(&q->q_usage_counter)) {
+			if (likely((op & REQ_PREEMPT) ||
+						!blk_queue_preempt_only(q))) {
+				rcu_read_unlock_sched();
+				return 0;
+			} else
+				percpu_ref_put(&q->q_usage_counter);
+		}
+		rcu_read_unlock_sched();
 
 		if (op & REQ_NOWAIT)
 			return -EBUSY;
@@ -787,7 +823,9 @@ int blk_queue_enter(struct request_queue *q, unsigned int op)
 		smp_rmb();
 
 		ret = wait_event_interruptible(q->mq_freeze_wq,
-				!atomic_read(&q->mq_freeze_depth) ||
+				(!atomic_read(&q->mq_freeze_depth) &&
+				((op & REQ_PREEMPT) ||
+				 !blk_queue_preempt_only(q))) ||
 				blk_queue_dying(q));
 		if (blk_queue_dying(q))
 			return -ENODEV;
include/linux/blkdev.h +5 -0
index 4c688385d866..66d46d9eac29 100644
@@ -628,6 +628,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_DEFAULT	((1 << QUEUE_FLAG_IO_STAT) |		\
 				 (1 << QUEUE_FLAG_STACKABLE)	|	\
@@ -732,6 +733,10 @@ static inline void queue_flag_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)
+
+extern void blk_set_preempt_only(struct request_queue *q, bool preempt_only);
 
 static inline bool blk_account_rq(struct request *rq)
 {
Signature · 1 line
2.9.5
author
#13 lore raw

PATCH v8 8/8 SCSI: set block queue at preempt only when SCSI device is put into quiesce

In reply to Ming Lei

Simply quiesing SCSI device and waiting for completeion of IO dispatched to SCSI queue isn't safe, it is easy to use up request pool because all allocated requests before can't be dispatched when device is put in QIUESCE. Then no request can be allocated for RQF_PREEMPT, and system may hang somewhere, such as When sending commands of sync_cache or start_stop during system suspend path.

Before quiesing SCSI, this patch sets block queue in preempt mode first, so no new normal request can enter queue any more, and all pending requests are drained too once blk_set_preempt_only(true) is returned. Then RQF_PREEMPT can be allocated successfully duirng SCSI quiescing.

This patch fixes one long term issue of IO hang, in either block legacy and blk-mq.

---
 drivers/scsi/scsi_lib.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/scsi/scsi_lib.c +19 -1
index 62f905b22821..f7ffd33a283c 100644
@@ -2929,12 +2929,28 @@ scsi_device_quiesce(struct scsi_device *sdev)
 {
 	int err;
 
+	/*
+	 * Simply quiesing SCSI device isn't safe, it is easy
+	 * to use up requests because all these allocated requests
+	 * can't be dispatched when device is put in QIUESCE.
+	 * Then no request can be allocated and we may hang
+	 * somewhere, such as system suspend/resume.
+	 *
+	 * So we set block queue in preempt only first, no new
+	 * normal request can enter queue any more, and all pending
+	 * requests are drained once blk_set_preempt_only()
+	 * returns. Only RQF_PREEMPT is allowed in preempt only mode.
+	 */
+	blk_set_preempt_only(sdev->request_queue, true);
+
 	mutex_lock(&sdev->state_mutex);
 	err = scsi_device_set_state(sdev, SDEV_QUIESCE);
 	mutex_unlock(&sdev->state_mutex);
 
-	if (err)
+	if (err) {
+		blk_set_preempt_only(sdev->request_queue, false);
 		return err;
+	}
 
 	scsi_run_queue(sdev->request_queue);
 	while (atomic_read(&sdev->device_busy)) {
@@ -2965,6 +2981,8 @@ void scsi_device_resume(struct scsi_device *sdev)
 	    scsi_device_set_state(sdev, SDEV_RUNNING) == 0)
 		scsi_run_queue(sdev->request_queue);
 	mutex_unlock(&sdev->state_mutex);
+
+	blk_set_preempt_only(sdev->request_queue, false);
 }
 EXPORT_SYMBOL(scsi_device_resume);
 
Signature · 1 line
2.9.5
Tested-by on the v8 series
#14 lore raw
In reply to Ming Lei

Also

for whole v8.

Quoted 109 lines · On úterý 3. října 2017 16:03:58 CEST Ming Lei wrote:

Hi Jens,

Please consider this patchset for V4.15, and it fixes one kind of long-term I/O hang issue in either block legacy path or blk-mq.

The current SCSI quiesce isn't safe and easy to trigger I/O deadlock.

Once SCSI device is put into QUIESCE, no new request except for RQF_PREEMPT can be dispatched to SCSI successfully, and scsi_device_quiesce() just simply waits for completion of I/Os dispatched to SCSI stack. It isn't enough at all.

Because new request still can be comming, but all the allocated requests can't be dispatched successfully, so request pool can be consumed up easily.

Then request with RQF_PREEMPT can't be allocated and wait forever, then system hangs forever, such as during system suspend or sending SCSI domain alidation in case of transport_spi.

Both IO hang inside system suspend[1] or SCSI domain validation were reported before.

This patch introduces preempt only mode, and solves the issue by allowing RQF_PREEMP only during SCSI quiesce.

Both SCSI and SCSI_MQ have this IO deadlock issue, this patch fixes them all.

V8:
- fix one race as pointed out by Bart
- pass 'op' to blk_queue_enter() as suggested by Christoph

V7:
- add Reviewed-by & Tested-by
- one line change in patch 5 for checking preempt request

V6:
- borrow Bart's idea of preempt only, with clean implementation(patch 5/patch 6)
- needn't any external driver's dependency, such as MD's change

V5:
- fix one tiny race by introducing blk_queue_enter_preempt_freeze() given this change is small enough compared with V4, I added tested-by directly

V4:
- reorganize patch order to make it more reasonable
- support nested preempt freeze, as required by SCSI transport spi
- check preempt freezing in slow path of of blk_queue_enter()
- add "SCSI: transport_spi: resume a quiesced device"
- wake up freeze queue in setting dying for both blk-mq and legacy
- rename blk_mq_[freeze|unfreeze]_queue() in one patch
- rename .mq_freeze_wq and .mq_freeze_depth
- improve comment

V3:
- introduce q->preempt_unfreezing to fix one bug of preempt freeze
- call blk_queue_enter_live() only when queue is preempt frozen
- cleanup a bit on the implementation of preempt freeze
- only patch 6 and 7 are changed

V2:
- drop the 1st patch in V1 because percpu_ref_is_dying() is enough as pointed by Tejun
- introduce preempt version of blk_[freeze|unfreeze]_queue
- sync between preempt freeze and normal freeze
- fix warning from percpu-refcount as reported by Oleksandr

[1] https://marc.info/?t=150340250100013&r=3&w=2

Thanks,
Ming

Bart Van Assche (1):
block: Convert RQF_PREEMPT into REQ_PREEMPT

Ming Lei (7):
blk-mq: only run hw queues for blk-mq
block: tracking request allocation with q_usage_counter
block: pass 'op' to blk_queue_enter()
percpu-refcount: introduce __percpu_ref_tryget_live
blk-mq: return if queue is frozen via current blk_freeze_queue_start
block: support PREEMPT_ONLY
SCSI: set block queue at preempt only when SCSI device is put into
quiesce

 block/blk-core.c                | 66
+++++++++++++++++++++++++++++++++++++---- block/blk-mq-debugfs.c          |
 2 +-
 block/blk-mq.c                  | 26 ++++++++--------
 block/blk-mq.h                  |  1 -
 block/blk-timeout.c             |  2 +-
 block/blk.h                     |  2 +-
 drivers/ide/ide-atapi.c         |  3 +-
 drivers/ide/ide-io.c            |  2 +-
 drivers/ide/ide-pm.c            |  4 +--
 drivers/scsi/scsi_lib.c         | 31 +++++++++++++++----
 fs/block_dev.c                  |  4 +--
 include/linux/blk-mq.h          |  4 +--
 include/linux/blk_types.h       |  6 ++++
 include/linux/blkdev.h          | 10 ++++---
 include/linux/percpu-refcount.h | 27 ++++++++++-------
 15 files changed, 137 insertions(+), 53 deletions(-)
#15 lore raw
In reply to Ming Lei

Hi Ming, Jens.

What is the fate of this patchset please?

Quoted 109 lines · Ming Lei

Hi Jens,

Please consider this patchset for V4.15, and it fixes one kind of long-term I/O hang issue in either block legacy path or blk-mq.

The current SCSI quiesce isn't safe and easy to trigger I/O deadlock.

Once SCSI device is put into QUIESCE, no new request except for RQF_PREEMPT can be dispatched to SCSI successfully, and scsi_device_quiesce() just simply waits for completion of I/Os dispatched to SCSI stack. It isn't enough at all.

Because new request still can be comming, but all the allocated requests can't be dispatched successfully, so request pool can be consumed up easily.

Then request with RQF_PREEMPT can't be allocated and wait forever, then system hangs forever, such as during system suspend or sending SCSI domain alidation in case of transport_spi.

Both IO hang inside system suspend[1] or SCSI domain validation were reported before.

This patch introduces preempt only mode, and solves the issue by allowing RQF_PREEMP only during SCSI quiesce.

Both SCSI and SCSI_MQ have this IO deadlock issue, this patch fixes them all.

V8:
- fix one race as pointed out by Bart
- pass 'op' to blk_queue_enter() as suggested by Christoph

V7:
- add Reviewed-by & Tested-by
- one line change in patch 5 for checking preempt request

V6:
- borrow Bart's idea of preempt only, with clean implementation(patch 5/patch 6)
- needn't any external driver's dependency, such as MD's change

V5:
- fix one tiny race by introducing blk_queue_enter_preempt_freeze() given this change is small enough compared with V4, I added tested-by directly

V4:
- reorganize patch order to make it more reasonable
- support nested preempt freeze, as required by SCSI transport spi
- check preempt freezing in slow path of of blk_queue_enter()
- add "SCSI: transport_spi: resume a quiesced device"
- wake up freeze queue in setting dying for both blk-mq and legacy
- rename blk_mq_[freeze|unfreeze]_queue() in one patch
- rename .mq_freeze_wq and .mq_freeze_depth
- improve comment

V3:
- introduce q->preempt_unfreezing to fix one bug of preempt freeze
- call blk_queue_enter_live() only when queue is preempt frozen
- cleanup a bit on the implementation of preempt freeze
- only patch 6 and 7 are changed

V2:
- drop the 1st patch in V1 because percpu_ref_is_dying() is enough as pointed by Tejun
- introduce preempt version of blk_[freeze|unfreeze]_queue
- sync between preempt freeze and normal freeze
- fix warning from percpu-refcount as reported by Oleksandr

[1] https://marc.info/?t=150340250100013&r=3&w=2

Thanks,
Ming

Bart Van Assche (1):
block: Convert RQF_PREEMPT into REQ_PREEMPT

Ming Lei (7):
blk-mq: only run hw queues for blk-mq
block: tracking request allocation with q_usage_counter
block: pass 'op' to blk_queue_enter()
percpu-refcount: introduce __percpu_ref_tryget_live
blk-mq: return if queue is frozen via current blk_freeze_queue_start
block: support PREEMPT_ONLY
SCSI: set block queue at preempt only when SCSI device is put into
quiesce

 block/blk-core.c                | 66 
+++++++++++++++++++++++++++++++++++++----
 block/blk-mq-debugfs.c          |  2 +-
 block/blk-mq.c                  | 26 ++++++++--------
 block/blk-mq.h                  |  1 -
 block/blk-timeout.c             |  2 +-
 block/blk.h                     |  2 +-
 drivers/ide/ide-atapi.c         |  3 +-
 drivers/ide/ide-io.c            |  2 +-
 drivers/ide/ide-pm.c            |  4 +--
 drivers/scsi/scsi_lib.c         | 31 +++++++++++++++----
 fs/block_dev.c                  |  4 +--
 include/linux/blk-mq.h          |  4 +--
 include/linux/blk_types.h       |  6 ++++
 include/linux/blkdev.h          | 10 ++++---
 include/linux/percpu-refcount.h | 27 ++++++++++-------
 15 files changed, 137 insertions(+), 53 deletions(-)