Skip to contentSkip to conversation

Ensure that a request queue is dissociated from the cgroup controller

PATCH Superseded Bart Van Assche 1 participant 3 messages May 18, 2018 Block layer

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Ensure that a request queue is dissociated from the cgroup controller

Hello Jens,

Several block drivers call alloc_disk() followed by put_disk() if something fails before device_add_disk() is called without calling blk_cleanup_queue(). Make sure that also for this scenario a request queue is dissociated from the cgroup controller. This patch avoids that loading the parport_pc, paride and pf drivers trigger a kernel crash. Please consider these patches for the upstream kernel.

Thanks,

Bart.

Changes between v2 and v3:
- Avoid code duplication by introducing a new helper function.

Changes between v1 and v2:
- Fixed the build for CONFIG_BLK_CGROUP=n.

Bart Van Assche (2):
block: Introduce blk_exit_queue()
block: Ensure that a request queue is dissociated from the cgroup
controller

 block/blk-core.c  | 54 ++++++++++++++++++++++++++++++------------------------
 block/blk-sysfs.c | 25 +++++++++++++++++++++++++
 block/blk.h       |  1 +
 3 files changed, 56 insertions(+), 24 deletions(-)
Signature · 1 line
2.16.3
#1 lore raw

PATCH v3 1/2 block: Introduce blk_exit_queue()

This patch does not change any functionality.

Cc Alexandru Moise <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Ming Lei <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Joseph Qi <[email protected]>
---
 block/blk-core.c | 54 ++++++++++++++++++++++++++++++------------------------
 block/blk.h      |  1 +
 2 files changed, 31 insertions(+), 24 deletions(-)
block/blk-core.c +30 -24
index 4dadff238b02..ac28a9c7136f 100644
@@ -720,6 +720,35 @@ void blk_set_queue_dying(struct request_queue *q)
 }
 EXPORT_SYMBOL_GPL(blk_set_queue_dying);
 
+/* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
+void blk_exit_queue(struct request_queue *q)
+{
+	/*
+	 * Since the I/O scheduler exit code may access cgroup information,
+	 * perform I/O scheduler exit before disassociating from the block
+	 * cgroup controller.
+	 */
+	if (q->elevator) {
+		ioc_clear_queue(q);
+		elevator_exit(q, q->elevator);
+		q->elevator = NULL;
+	}
+
+	/*
+	 * Remove all references to @q from the block cgroup controller before
+	 * restoring @q->queue_lock to avoid that restoring this pointer causes
+	 * e.g. blkcg_print_blkgs() to crash.
+	 */
+	blkcg_exit_queue(q);
+
+	/*
+	 * Since the cgroup code may dereference the @q->backing_dev_info
+	 * pointer, only decrease its reference count after having removed the
+	 * association with the block cgroup controller.
+	 */
+	bdi_put(q->backing_dev_info);
+}
+
 /**
  * blk_cleanup_queue - shutdown a request queue
  * @q: request queue to shutdown
@@ -785,30 +814,7 @@ void blk_cleanup_queue(struct request_queue *q)
 	 */
 	WARN_ON_ONCE(q->kobj.state_in_sysfs);
 
-	/*
-	 * Since the I/O scheduler exit code may access cgroup information,
-	 * perform I/O scheduler exit before disassociating from the block
-	 * cgroup controller.
-	 */
-	if (q->elevator) {
-		ioc_clear_queue(q);
-		elevator_exit(q, q->elevator);
-		q->elevator = NULL;
-	}
-
-	/*
-	 * Remove all references to @q from the block cgroup controller before
-	 * restoring @q->queue_lock to avoid that restoring this pointer causes
-	 * e.g. blkcg_print_blkgs() to crash.
-	 */
-	blkcg_exit_queue(q);
-
-	/*
-	 * Since the cgroup code may dereference the @q->backing_dev_info
-	 * pointer, only decrease its reference count after having removed the
-	 * association with the block cgroup controller.
-	 */
-	bdi_put(q->backing_dev_info);
+	blk_exit_queue(q);
 
 	if (q->mq_ops)
 		blk_mq_free_queue(q);
block/blk.h +1 -0
index 204a0345996c..ec914ae7130c 100644
@@ -130,6 +130,7 @@ void blk_free_flush_queue(struct blk_flush_queue *q);
 int blk_init_rl(struct request_list *rl, struct request_queue *q,
 		gfp_t gfp_mask);
 void blk_exit_rl(struct request_queue *q, struct request_list *rl);
+void blk_exit_queue(struct request_queue *q);
 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
 			struct bio *bio);
 void blk_queue_bypass_start(struct request_queue *q);
Signature · 1 line
2.16.3
#2 lore raw

PATCH v3 2/2 block: Ensure that a request queue is dissociated from the cgroup controller

In reply to Bart Van Assche

Several block drivers call alloc_disk() followed by put_disk() if something fails before device_add_disk() is called without calling blk_cleanup_queue(). Make sure that also for this scenario a request queue is dissociated from the cgroup controller. This patch avoids that loading the parport_pc, paride and pf drivers triggers the following kernel crash:

BUG: KASAN: null-ptr-deref in pi_init+0x42e/0x580 [paride]
Read of size 4 at addr 0000000000000008 by task modprobe/744
Call Trace:
dump_stack+0x9a/0xeb
kasan_report+0x139/0x350
pi_init+0x42e/0x580 [paride]
pf_init+0x2bb/0x1000 [pf]
do_one_initcall+0x8e/0x405
do_init_module+0xd9/0x2f2
load_module+0x3ab4/0x4700
SYSC_finit_module+0x176/0x1a0
do_syscall_64+0xee/0x2b0
entry_SYSCALL_64_after_hwframe+0x42/0xb7
Fixes a063057d7c73 ("block: Fix a race between request queue removal and the block cgroup controller")
Cc Tejun Heo <[email protected]>
Cc Alexandru Moise <[email protected]>
Cc Joseph Qi <[email protected]>
---
 block/blk-sysfs.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
block/blk-sysfs.c +25 -0
index 9cf41fee3790..a239c73fa20f 100644
@@ -804,6 +804,31 @@ static void __blk_release_queue(struct work_struct *work)
 		blk_stat_remove_callback(q, q->poll_cb);
 	blk_stat_free_callback(q->poll_cb);
 
+	if (!blk_queue_dead(q)) {
+		/*
+		 * Last reference was dropped without having called
+		 * blk_cleanup_queue().
+		 */
+		WARN_ONCE(blk_queue_init_done(q),
+			  "request queue %p has been registered but blk_cleanup_queue() has not been called for that queue\n",
+			  q);
+		blk_exit_queue(q);
+	}
+
+#ifdef CONFIG_BLK_CGROUP
+	{
+		struct blkcg_gq *blkg;
+
+		rcu_read_lock();
+		blkg = blkg_lookup(&blkcg_root, q);
+		rcu_read_unlock();
+
+		WARN(blkg,
+		     "request queue %p is being released but it has not yet been removed from the blkcg controller\n",
+		     q);
+	}
+#endif
+
 	blk_free_queue_stats(q->stats);
 
 	blk_exit_rl(q, &q->root_rl);
Signature · 1 line
2.16.3