Skip to contentSkip to conversation

Ensure that a request queue is dissociated from the cgroup controller

PATCH Applied Bart Van Assche 3 participants 8 messages Aug 9, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 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. Since this patch series fixes a regression, please consider these patches for kernel v4.19.

Thanks,

Bart.

Changes between v4 and v5:
- Instead of using #ifdef CONFIG_CGROUP / #endif, introduced a new function in the cgroups code.

Changes between v3 and v4:
- Added "Cc: stable" tags.

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 (3):
blkcg: Introduce blkg_root_lookup()
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          | 15 +++++++++++
 block/blk.h                |  1 +
 include/linux/blk-cgroup.h | 18 +++++++++++++
 4 files changed, 64 insertions(+), 24 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH v5 1/3 blkcg: Introduce blkg_root_lookup()

This new function will be used in a later patch to verify whether a queue has been dissociated from the cgroup controller before being released.

Cc Tejun Heo <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Johannes Thumshirn <[email protected]>
Cc Alexandru Moise <[email protected]>
Cc Joseph Qi <[email protected]>
---
 include/linux/blk-cgroup.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
include/linux/blk-cgroup.h +18 -0
index f7b910768306..1361cfc9b878 100644
@@ -341,6 +341,23 @@ static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
 	return __blkg_lookup(blkcg, q, false);
 }
 
+/**
+ * blkg_lookup - look up blkg for the specified request queue
+ * @q: request_queue of interest
+ *
+ * Lookup blkg for @q at the root level. See also blkg_lookup().
+ */
+static inline struct blkcg_gq *blkg_root_lookup(struct request_queue *q)
+{
+	struct blkcg_gq *blkg;
+
+	rcu_read_lock();
+	blkg = blkg_lookup(&blkcg_root, q);
+	rcu_read_unlock();
+
+	return blkg;
+}
+
 /**
  * blkg_to_pdata - get policy private data
  * @blkg: blkg of interest
@@ -864,6 +881,7 @@ static inline bool blk_cgroup_congested(void) { return false; }
 static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
 
 static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
+static inline struct blkcg_gq *blkg_root_lookup(struct request_queue *q) { return NULL; }
 static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
 static inline void blkcg_drain_queue(struct request_queue *q) { }
 static inline void blkcg_exit_queue(struct request_queue *q) { }
Signature · 1 line
2.18.0
#5 lore raw

PATCH v5 2/3 block: Introduce blk_exit_queue()

In reply to Bart Van Assche

This patch does not change any functionality.

Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Alexandru Moise <[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 2e054b65de42..55bc22ef2934 100644
@@ -719,6 +719,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
@@ -788,30 +817,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 6adae8f94279..aeb8026f4d7b 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.18.0
maintainer Applied
#6 lore raw
In reply to Bart Van Assche
Quoted 8 lines · Bart Van Assche

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. Since this patch series fixes a regression, please consider these patches for kernel v4.19.

Thanks, applied for 4.19.

Signature · 1 line
Jens Axboe
#7 lore raw

PATCH v5 3/3 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") # v4.17
Cc Tejun Heo <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Alexandru Moise <[email protected]>
Cc Joseph Qi <[email protected]>
---
 block/blk-sysfs.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
block/blk-sysfs.c +15 -0
index ca1984ecbdeb..fcadea471779 100644
@@ -802,6 +802,21 @@ 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);
+	}
+
+	WARN(blkg_root_lookup(q),
+	     "request queue %p is being released but it has not yet been removed from the blkcg controller\n",
+	     q);
+
 	blk_free_queue_stats(q->stats);
 
 	blk_exit_rl(q, &q->root_rl);
Signature · 1 line
2.18.0
maintainer
#2 lore raw

Re: [PATCH v5 1/3] blkcg: Introduce blkg_root_lookup()

In reply to Bart Van Assche

Hello,

Quoted 6 lines · Bart Van Assche
include/linux/blk-cgroup.h +6 -0
+/**
+ * blkg_lookup - look up blkg for the specified request queue
+ * @q: request_queue of interest
+ *
+ * Lookup blkg for @q at the root level. See also blkg_lookup().
+ */

include/linux/blk-cgroup.h static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, · line 359

Quoted 10 lines
include/linux/blk-cgroup.h +10 -0
+static inline struct blkcg_gq *blkg_root_lookup(struct request_queue *q)
+{
+	struct blkcg_gq *blkg;
+
+	rcu_read_lock();
+	blkg = blkg_lookup(&blkcg_root, q);
+	rcu_read_unlock();
+
+	return blkg;
+}

So, root blkg is always available as long as the request_queue is alive. Sth like the following would be simpler?

static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)

{
	return q->root_blkg;
}
Signature · 1 line
tejun
#3 lore raw

Re: [PATCH v5 1/3] blkcg: Introduce blkg_root_lookup()

Quoted 5 lines · Tejun Heo

Hello,
=20

Quoted 2 lines · Bart Van Assche
+-/**
+- * blkg_lookup - look up blkg for the specified reque=

st queue

Quoted 3 lines
Quoted 3 lines
+- * @q: request_queue of interest
+- *
+- * Lookup blkg for @q at the root level. See also blk=

g_lookup().

Quoted 2 lines
Quoted 2 lines
+- */
+-static inline struct blkcg_gq *blkg_root_look=

up(struct request_queue *q)

Quoted 9 lines
Quoted 9 lines
+-{
+-	struct blkcg_gq *blkg;
+-
+-	rcu_read_lock();
+-	blkg = blkg_lookup(&blkcg_root, q);
+-	rcu_read_unlock();
+-
+-	return blkg;
+-}
Quoted 5 lines

=20
So, root blkg is always available as long as the request_queue is alive. Sth like the following would be simpler?
=20
static inline struct blkcg_gq *blk_queue_root_blk=

g(struct request_queue *q)

Quoted 3 lines
{
	return q->root_blkg;
}

That would not only be simpler, it would also avoid that blk_queue_= root_blkg()
returns NULL if bypass mode is enabled and the request queue is still assoc= iated
with the block cgroup controller. How do you want to proceed with this chan= ge?

Bart.

maintainer
#4 lore raw

Re: [PATCH v5 1/3] blkcg: Introduce blkg_root_lookup()

Quoted 24 lines · Bart Van Assche
Quoted 23 lines · Tejun Heo

Hello,

Quoted 6 lines · Bart Van Assche
include/linux/blk-cgroup.h +6 -0
+/**
+ * blkg_lookup - look up blkg for the specified request queue
+ * @q: request_queue of interest
+ *
+ * Lookup blkg for @q at the root level. See also blkg_lookup().
+ */

include/linux/blk-cgroup.h static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, · line 359

Quoted 10 lines
include/linux/blk-cgroup.h +10 -0
+static inline struct blkcg_gq *blkg_root_lookup(struct request_queue *q)
+{
+	struct blkcg_gq *blkg;
+
+	rcu_read_lock();
+	blkg = blkg_lookup(&blkcg_root, q);
+	rcu_read_unlock();
+
+	return blkg;
+}

So, root blkg is always available as long as the request_queue is alive. Sth like the following would be simpler?

Quoted 8 lines
Quoted 4 lines

static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)

{
	return q->root_blkg;
}

That would not only be simpler, it would also avoid that blk_queue_root_blkg() returns NULL if bypass mode is enabled and the request queue is still associated with the block cgroup controller. How do you want to proceed with this change?

Do a followup patch?

Signature · 1 line
Jens Axboe