Skip to contentSkip to conversation

Ensure that a request queue is dissociated from the cgroup controller

PATCH Superseded Bart Van Assche 3 participants 9 messages Jul 30, 2018 – Aug 8, 2018

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 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 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 (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.18.0
#1 lore raw

PATCH v4 1/2 block: Introduce blk_exit_queue()

This patch does not change any functionality.

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]>
---
 block/blk-core.c | 54 +++++++++++++++++++++++++++---------------------
 block/blk.h      |  1 +
 2 files changed, 31 insertions(+), 24 deletions(-)
block/blk-core.c +30 -24
index 7c65e297a4f1..5fbfa8484c95 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
#3 lore raw

PATCH v4 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") # v4.17
Cc Tejun Heo <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Ming Lei <[email protected]>
Cc Johannes Thumshirn <[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 ca1984ecbdeb..26275d9babcb 100644
@@ -802,6 +802,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.18.0
#8 lore raw
In reply to Bart Van Assche
Quoted 1 line · Bart Van Assche

Several block drivers call alloc_disk() followed by put_disk(=

) if something

Quoted 1 line

fails before device_add_disk() is called without calling blk+=

AF8-cleanup_queue().

Quoted 1 line

Make sure that also for this scenario a request queue is dissociated =

from the

Quoted 1 line

cgroup controller. This patch avoids that loading the parport_pc,=

paride and

Quoted 1 line

pf drivers trigger a kernel crash. Since this patch series fixes a re=

gression,

Quoted 1 line

please consider these patches for kernel v4.19.

[ ... ]

Can anyone help with reviewing this patch series? This patch series is seve= ral
months old and fixes a regression that was reported by a Linux user.

Thanks,

Bart.

Reviewed-by on v4 2/2
#4 lore raw

Re: [PATCH v4 2/2] block: Ensure that a request queue is dissociated from the cgroup controller

In reply to Bart Van Assche
Quoted 1 line

From my limited insight into this:

Looks good,

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] block: Introduce blk_exit_queue()

In reply to Bart Van Assche

Looks good,

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
maintainer
#5 lore raw

Re: [PATCH v4 2/2] block: Ensure that a request queue is dissociated from the cgroup controller

In reply to Bart Van Assche
Quoted 58 lines · 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 Johannes Thumshirn <[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 +14 -0
index ca1984ecbdeb..26275d9babcb 100644
@@ -802,6 +802,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;

block/blk-sysfs.c static void __blk_release_queue(struct work_struct *work) · line 828

Quoted 10 lines
block/blk-sysfs.c +10 -0
+
+		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

This last hunk should go in the cgroup code.

Signature · 1 line
Jens Axboe
#6 lore raw

Re: [PATCH v4 2/2] block: Ensure that a request queue is dissociated from the cgroup controller

Quoted 11 lines · Jens Axboe
Quoted 10 lines · Bart Van Assche
+-#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",

Quoted 5 lines
Quoted 3 lines
+-		     q);
+-	}
+-#endif

=20
This last hunk should go in the cgroup code.

Hello Jens,

How about leaving out the #ifdef CONFIG_BLK_CGROUP / #endif= ? Would that be
sufficient? I just noticed that blkg_lookup() returns NULL anyway if cg= roup support
is disabled in the kernel config.

Thanks,

Bart.

maintainer
#7 lore raw

Re: [PATCH v4 2/2] block: Ensure that a request queue is dissociated from the cgroup controller

Quoted 18 lines · Bart Van Assche
Quoted 16 lines · Jens Axboe

block/blk-sysfs.c static void __blk_release_queue(struct work_struct *work) · line 828

Quoted 13 lines · Bart Van Assche
block/blk-sysfs.c +13 -0
+#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

This last hunk should go in the cgroup code.

Quoted 5 lines

Hello Jens,

How about leaving out the #ifdef CONFIG_BLK_CGROUP / #endif? Would that be sufficient? I just noticed that blkg_lookup() returns NULL anyway if cgroup support is disabled in the kernel config.

That's even better.

Signature · 1 line
Jens Axboe