mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-12-27 10:01:39 -05:00
A recent lockdep[1] splat observed while running blktest block/005 reveals a potential deadlock caused by the cpu_hotplug_lock dependency on ->freeze_lock. This dependency was introduced by commit033b667a82("block: blk-rq-qos: guard rq-qos helpers by static key"). That change added a static key to avoid fetching q->rq_qos when neither blk-wbt nor blk-iolatency is configured. The static key dynamically patches kernel text to a NOP when disabled, eliminating overhead of fetching q->rq_qos in the I/O hot path. However, enabling a static key at runtime requires acquiring both cpu_hotplug_lock and jump_label_mutex. When this happens after the queue has already been frozen (i.e., while holding ->freeze_lock), it creates a locking dependency from cpu_hotplug_lock to ->freeze_lock, which leads to a potential deadlock reported by lockdep [1]. To resolve this, replace the static key mechanism with q->queue_flags: QUEUE_FLAG_QOS_ENABLED. This flag is evaluated in the fast path before accessing q->rq_qos. If the flag is set, we proceed to fetch q->rq_qos; otherwise, the access is skipped. Since q->queue_flags is commonly accessed in IO hotpath and resides in the first cacheline of struct request_queue, checking it imposes minimal overhead while eliminating the deadlock risk. This change avoids the lockdep splat without introducing performance regressions. [1] https://lore.kernel.org/linux-block/4fdm37so3o4xricdgfosgmohn63aa7wj3ua4e5vpihoamwg3ui@fq42f5q5t5ic/ Reported-by: Shinichiro Kawasaki <shinichiro.kawasaki@wdc.com> Closes: https://lore.kernel.org/linux-block/4fdm37so3o4xricdgfosgmohn63aa7wj3ua4e5vpihoamwg3ui@fq42f5q5t5ic/ Fixes:033b667a82("block: blk-rq-qos: guard rq-qos helpers by static key") Tested-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com> Signed-off-by: Nilay Shroff <nilay@linux.ibm.com> Reviewed-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Yu Kuai <yukuai3@huawei.com> Link: https://lore.kernel.org/r/20250814082612.500845-4-nilay@linux.ibm.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
197 lines
5.3 KiB
C
197 lines
5.3 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef RQ_QOS_H
|
|
#define RQ_QOS_H
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/blkdev.h>
|
|
#include <linux/blk_types.h>
|
|
#include <linux/atomic.h>
|
|
#include <linux/wait.h>
|
|
#include <linux/blk-mq.h>
|
|
|
|
#include "blk-mq-debugfs.h"
|
|
|
|
struct blk_mq_debugfs_attr;
|
|
|
|
enum rq_qos_id {
|
|
RQ_QOS_WBT,
|
|
RQ_QOS_LATENCY,
|
|
RQ_QOS_COST,
|
|
};
|
|
|
|
struct rq_wait {
|
|
wait_queue_head_t wait;
|
|
atomic_t inflight;
|
|
};
|
|
|
|
struct rq_qos {
|
|
const struct rq_qos_ops *ops;
|
|
struct gendisk *disk;
|
|
enum rq_qos_id id;
|
|
struct rq_qos *next;
|
|
#ifdef CONFIG_BLK_DEBUG_FS
|
|
struct dentry *debugfs_dir;
|
|
#endif
|
|
};
|
|
|
|
struct rq_qos_ops {
|
|
void (*throttle)(struct rq_qos *, struct bio *);
|
|
void (*track)(struct rq_qos *, struct request *, struct bio *);
|
|
void (*merge)(struct rq_qos *, struct request *, struct bio *);
|
|
void (*issue)(struct rq_qos *, struct request *);
|
|
void (*requeue)(struct rq_qos *, struct request *);
|
|
void (*done)(struct rq_qos *, struct request *);
|
|
void (*done_bio)(struct rq_qos *, struct bio *);
|
|
void (*cleanup)(struct rq_qos *, struct bio *);
|
|
void (*queue_depth_changed)(struct rq_qos *);
|
|
void (*exit)(struct rq_qos *);
|
|
const struct blk_mq_debugfs_attr *debugfs_attrs;
|
|
};
|
|
|
|
struct rq_depth {
|
|
unsigned int max_depth;
|
|
|
|
int scale_step;
|
|
bool scaled_max;
|
|
|
|
unsigned int queue_depth;
|
|
unsigned int default_depth;
|
|
};
|
|
|
|
static inline struct rq_qos *rq_qos_id(struct request_queue *q,
|
|
enum rq_qos_id id)
|
|
{
|
|
struct rq_qos *rqos;
|
|
for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
|
|
if (rqos->id == id)
|
|
break;
|
|
}
|
|
return rqos;
|
|
}
|
|
|
|
static inline struct rq_qos *wbt_rq_qos(struct request_queue *q)
|
|
{
|
|
return rq_qos_id(q, RQ_QOS_WBT);
|
|
}
|
|
|
|
static inline struct rq_qos *iolat_rq_qos(struct request_queue *q)
|
|
{
|
|
return rq_qos_id(q, RQ_QOS_LATENCY);
|
|
}
|
|
|
|
static inline void rq_wait_init(struct rq_wait *rq_wait)
|
|
{
|
|
atomic_set(&rq_wait->inflight, 0);
|
|
init_waitqueue_head(&rq_wait->wait);
|
|
}
|
|
|
|
int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
|
|
const struct rq_qos_ops *ops);
|
|
void rq_qos_del(struct rq_qos *rqos);
|
|
|
|
typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data);
|
|
typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data);
|
|
|
|
void rq_qos_wait(struct rq_wait *rqw, void *private_data,
|
|
acquire_inflight_cb_t *acquire_inflight_cb,
|
|
cleanup_cb_t *cleanup_cb);
|
|
bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit);
|
|
bool rq_depth_scale_up(struct rq_depth *rqd);
|
|
bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle);
|
|
bool rq_depth_calc_max_depth(struct rq_depth *rqd);
|
|
|
|
void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio);
|
|
void __rq_qos_done(struct rq_qos *rqos, struct request *rq);
|
|
void __rq_qos_issue(struct rq_qos *rqos, struct request *rq);
|
|
void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq);
|
|
void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio);
|
|
void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio);
|
|
void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio);
|
|
void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio);
|
|
void __rq_qos_queue_depth_changed(struct rq_qos *rqos);
|
|
|
|
static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos)
|
|
__rq_qos_cleanup(q->rq_qos, bio);
|
|
}
|
|
|
|
static inline void rq_qos_done(struct request_queue *q, struct request *rq)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos && !blk_rq_is_passthrough(rq))
|
|
__rq_qos_done(q->rq_qos, rq);
|
|
}
|
|
|
|
static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos)
|
|
__rq_qos_issue(q->rq_qos, rq);
|
|
}
|
|
|
|
static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos)
|
|
__rq_qos_requeue(q->rq_qos, rq);
|
|
}
|
|
|
|
static inline void rq_qos_done_bio(struct bio *bio)
|
|
{
|
|
struct request_queue *q;
|
|
|
|
if (!bio->bi_bdev || (!bio_flagged(bio, BIO_QOS_THROTTLED) &&
|
|
!bio_flagged(bio, BIO_QOS_MERGED)))
|
|
return;
|
|
|
|
q = bdev_get_queue(bio->bi_bdev);
|
|
|
|
/*
|
|
* If a bio has BIO_QOS_xxx set, it implicitly implies that
|
|
* q->rq_qos is present. So, we skip re-checking q->rq_qos
|
|
* here as an extra optimization and directly call
|
|
* __rq_qos_done_bio().
|
|
*/
|
|
__rq_qos_done_bio(q->rq_qos, bio);
|
|
}
|
|
|
|
static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos) {
|
|
bio_set_flag(bio, BIO_QOS_THROTTLED);
|
|
__rq_qos_throttle(q->rq_qos, bio);
|
|
}
|
|
}
|
|
|
|
static inline void rq_qos_track(struct request_queue *q, struct request *rq,
|
|
struct bio *bio)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos)
|
|
__rq_qos_track(q->rq_qos, rq, bio);
|
|
}
|
|
|
|
static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
|
|
struct bio *bio)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos) {
|
|
bio_set_flag(bio, BIO_QOS_MERGED);
|
|
__rq_qos_merge(q->rq_qos, rq, bio);
|
|
}
|
|
}
|
|
|
|
static inline void rq_qos_queue_depth_changed(struct request_queue *q)
|
|
{
|
|
if (unlikely(test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags)) &&
|
|
q->rq_qos)
|
|
__rq_qos_queue_depth_changed(q->rq_qos);
|
|
}
|
|
|
|
void rq_qos_exit(struct request_queue *);
|
|
|
|
#endif
|