Skip to content

Commit 36a7bd9

Browse files
Can GuoZiqi Chen
Can Guo
authored and
Ziqi Chen
committed
block: pm: Fix possible unbalanced nr_pending
When someone calls blk_get_request() and blk_put_request() without invoking blk_execute_rq() in between, nr_pending will be unbalanced. Fix this by adding a flag, namely the RQF_PM_ADDED, to track the usage of nr_pending, so that blk_pm_put_request() won't touch nr_pending if the flag is not set. Extra care should be given to blk_pm_requeue_request(), because blk-flush.c inserts flush requests into queue without calling blk_pm_add_request(), if the flush requests get requeued by LLD, blk_pm_requeue_request() should anyways decrease nr_pending in this situation to keep it balanced. Change-Id: I7baa2049ac532e4fd22a0ce358133ba88266a314 Signed-off-by: Ziqi Chen <[email protected]> Signed-off-by: Can Guo <[email protected]>
1 parent ef299a6 commit 36a7bd9

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

block/blk-core.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -1741,8 +1741,12 @@ EXPORT_SYMBOL_GPL(part_round_stats);
17411741
#ifdef CONFIG_PM
17421742
static void blk_pm_put_request(struct request *rq)
17431743
{
1744-
if (rq->q->dev && !(rq->rq_flags & RQF_PM) && !--rq->q->nr_pending)
1745-
pm_runtime_mark_last_busy(rq->q->dev);
1744+
if (rq->q->dev && !(rq->rq_flags & RQF_PM) &&
1745+
(rq->rq_flags & RQF_PM_ADDED)) {
1746+
rq->rq_flags &= ~RQF_PM_ADDED;
1747+
if (!--rq->q->nr_pending)
1748+
pm_runtime_mark_last_busy(rq->q->dev);
1749+
}
17461750
}
17471751
#else
17481752
static inline void blk_pm_put_request(struct request *rq) {}

block/elevator.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,22 @@ void elv_bio_merged(struct request_queue *q, struct request *rq,
560560
#ifdef CONFIG_PM
561561
static void blk_pm_requeue_request(struct request *rq)
562562
{
563-
if (rq->q->dev && !(rq->rq_flags & RQF_PM))
563+
if (rq->q->dev && !(rq->rq_flags & RQF_PM) &&
564+
(rq->rq_flags & (RQF_PM_ADDED | RQF_FLUSH_SEQ))) {
565+
rq->rq_flags &= ~RQF_PM_ADDED;
564566
rq->q->nr_pending--;
567+
}
565568
}
566569

567570
static void blk_pm_add_request(struct request_queue *q, struct request *rq)
568571
{
569-
if (q->dev && !(rq->rq_flags & RQF_PM) && q->nr_pending++ == 0 &&
570-
(q->rpm_status == RPM_SUSPENDED || q->rpm_status == RPM_SUSPENDING))
571-
pm_request_resume(q->dev);
572+
if (q->dev && !(rq->rq_flags & RQF_PM)) {
573+
rq->rq_flags |= RQF_PM_ADDED;
574+
if (q->nr_pending++ == 0 &&
575+
(q->rpm_status == RPM_SUSPENDED ||
576+
q->rpm_status == RPM_SUSPENDING))
577+
pm_request_resume(q->dev);
578+
}
572579
}
573580
#else
574581
static inline void blk_pm_requeue_request(struct request *rq) {}

include/linux/blkdev.h

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ typedef __u32 __bitwise req_flags_t;
128128
#define RQF_MQ_POLL_SLEPT ((__force req_flags_t)(1 << 20))
129129
/* ->timeout has been called, don't expire again */
130130
#define RQF_TIMED_OUT ((__force req_flags_t)(1 << 21))
131+
/* increased nr_pending for this request */
132+
#define RQF_PM_ADDED ((__force req_flags_t)(1 << 22))
131133

132134
/* flags that prevent us from merging requests: */
133135
#define RQF_NOMERGE_FLAGS \

0 commit comments

Comments
 (0)