Skip to contentSkip to conversation

blk-cgroup: delay calling blkcg_exit_disk until disk_release

PATCH Applied Christoph Hellwig 3 participants 10 messages Feb 8, 2023 – Feb 13, 2023

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

PATCH 1/1 blk-cgroup: delay calling blkcg_exit_disk until disk_release

While del_gendisk ensures there is no outstanding I/O on the queue, it can't prevent block layer users from building new I/O.

This leads to a NULL ->root_blkg reference in bio_associate_blkg when allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems from del_gendisk until disk_release to make sure the blkg and throttle information is still avaіlable for bio submitters, even if those bios will immediately fail.

This now can cause a case where disk_release is called on a disk that hasn't been added. That's mostly harmless, except for a case in blk_throttl_exit that now needs to check for a NULL ->td pointer.

Fixes 178fa7d49815 ("blk-cgroup: delay blk-cgroup initialization until add_disk")
---
 block/blk-throttle.c | 3 ++-
 block/genhd.c        | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)
block/blk-throttle.c +2 -1
index 902203bdddb4b4..e7bd7050d68402 100644
@@ -2411,7 +2411,8 @@ void blk_throtl_exit(struct gendisk *disk)
 {
 	struct request_queue *q = disk->queue;
 
-	BUG_ON(!q->td);
+	if (!q->td)
+		return;
 	del_timer_sync(&q->td->service_queue.pending_timer);
 	throtl_shutdown_wq(q);
 	blkcg_deactivate_policy(disk, &blkcg_policy_throtl);
block/genhd.c +2 -2
index 7e031559bf514c..65373738c70b02 100644
@@ -668,8 +668,6 @@ void del_gendisk(struct gendisk *disk)
 	rq_qos_exit(q);
 	blk_mq_unquiesce_queue(q);
 
-	blkcg_exit_disk(disk);
-
 	/*
 	 * If the disk does not own the queue, allow using passthrough requests
 	 * again.  Else leave the queue frozen to fail all I/O.
@@ -1166,6 +1164,8 @@ static void disk_release(struct device *dev)
 	might_sleep();
 	WARN_ON_ONCE(disk_live(disk));
 
+	blkcg_exit_disk(disk);
+
 	/*
 	 * To undo the all initialization from blk_mq_init_allocated_queue in
 	 * case of a probe failure where add_disk is never called we have to
Signature · 1 line
2.39.1
#1 lore raw
Quoted 9 lines · Christoph Hellwig

While del_gendisk ensures there is no outstanding I/O on the queue, it can't prevent block layer users from building new I/O.

This leads to a NULL ->root_blkg reference in bio_associate_blkg when allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems from del_gendisk until disk_release to make sure the blkg and throttle information is still avaіlable for bio submitters, even if those bios will immediately fail.

Quoted 3 lines

This now can cause a case where disk_release is called on a disk that hasn't been added. That's mostly harmless, except for a case in blk_throttl_exit that now needs to check for a NULL ->td pointer.

With this way, blkcg_init_disk() could be called before q->root_blkg is released in disk unbind & rebind use case, then memory leak?

Thanks,
Ming

#2 lore raw
Quoted 6 lines · Ming Lei
Quoted 3 lines

This now can cause a case where disk_release is called on a disk that hasn't been added. That's mostly harmless, except for a case in blk_throttl_exit that now needs to check for a NULL ->td pointer.

With this way, blkcg_init_disk() could be called before q->root_blkg is released in disk unbind & rebind use case, then memory leak?

q->root_blkg is now disk->root_blkg. So in an unind and rebind case a different disk will be involved.

#3 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Ming Lei
Quoted 3 lines

This now can cause a case where disk_release is called on a disk that hasn't been added. That's mostly harmless, except for a case in blk_throttl_exit that now needs to check for a NULL ->td pointer.

With this way, blkcg_init_disk() could be called before q->root_blkg is released in disk unbind & rebind use case, then memory leak?

q->root_blkg is now disk->root_blkg. So in an unind and rebind case a different disk will be involved.

OK.

Another thing is that blkcg_init_disk() and blkcg_exit_disk() becomes asymmetrical with this patch. So alloc_disk() & add_disk(), del_disk() & put_disk() have to be done together. If it may be documented, this patch looks fine.

Thanks,
Ming

#4 lore raw
Quoted 4 lines · Ming Lei

Another thing is that blkcg_init_disk() and blkcg_exit_disk() becomes asymmetrical with this patch. So alloc_disk() & add_disk(), del_disk() & put_disk() have to be done together. If it may be documented, this patch looks fine.

As in no add_disk is allowed after del_gendisk on the same disk? It's been like that since basically forever. I agree that documenting it probably doesn't hurt though - but that's separate from this fix.

Reviewed-by
#5 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Ming Lei

Another thing is that blkcg_init_disk() and blkcg_exit_disk() becomes asymmetrical with this patch. So alloc_disk() & add_disk(), del_disk() & put_disk() have to be done together. If it may be documented, this patch looks fine.

As in no add_disk is allowed after del_gendisk on the same disk? It's been like that since basically forever. I agree that documenting it probably doesn't hurt though - but that's separate from this fix.

OK, fair enough,

Thanks,
Ming

maintainer Applied
#6 lore raw
In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

While del_gendisk ensures there is no outstanding I/O on the queue, it can't prevent block layer users from building new I/O.

This leads to a NULL ->root_blkg reference in bio_associate_blkg when allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems from del_gendisk until disk_release to make sure the blkg and throttle information is still avaіlable for bio submitters, even if those bios will immediately fail.

[...]

Applied, thanks!

[1/1] blk-cgroup: delay calling blkcg_exit_disk until disk_release

      commit: c43332fe028c252a2a28e46be70a530f64fc3c9d

Best regards,

Signature · 1 line
Jens Axboe
#7 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

While del_gendisk ensures there is no outstanding I/O on the queue, it can't prevent block layer users from building new I/O.

This leads to a NULL ->root_blkg reference in bio_associate_blkg when allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems from del_gendisk until disk_release to make sure the blkg and throttle information is still avaіlable for bio submitters, even if those bios will immediately fail.

Quoted 7 lines

This now can cause a case where disk_release is called on a disk that hasn't been added. That's mostly harmless, except for a case in blk_throttl_exit that now needs to check for a NULL ->td pointer.

Fixes 178fa7d49815 ("blk-cgroup: delay blk-cgroup initialization until add_disk")

hammmmmm, this patch actually causes bigger trouble.

After commit 84d7d462b16d ("blk-cgroup: pin the gendisk in struct blkcg_gq"), blkcg_gq instance grabs disk's reference, so moving blkcg_exit_disk into disk_release() just causes reference cross-dependency, both are leaked.

Thanks,
Ming

#8 lore raw
Quoted 18 lines · Ming Lei
Quoted 9 lines · Christoph Hellwig

While del_gendisk ensures there is no outstanding I/O on the queue, it can't prevent block layer users from building new I/O.

This leads to a NULL ->root_blkg reference in bio_associate_blkg when allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems from del_gendisk until disk_release to make sure the blkg and throttle information is still avaіlable for bio submitters, even if those bios will immediately fail.

Quoted 7 lines

This now can cause a case where disk_release is called on a disk that hasn't been added. That's mostly harmless, except for a case in blk_throttl_exit that now needs to check for a NULL ->td pointer.

Fixes 178fa7d49815 ("blk-cgroup: delay blk-cgroup initialization until add_disk")
Quoted 6 lines

hammmmmm, this patch actually causes bigger trouble.

After commit 84d7d462b16d ("blk-cgroup: pin the gendisk in struct blkcg_gq"), blkcg_gq instance grabs disk's reference, so moving blkcg_exit_disk into disk_release() just causes reference cross-dependency, both are leaked.

Hi Christoph & Jens,

This issue is a bit serious, both blkg & disk & request_queue are leaked by commit c43332fe028c ("blk-cgroup: delay calling blkcg_exit_disk until disk_release").

Can we solve it before merging for-6.3/block into v6.3-rc1?

Thanks,
Ming

#9 lore raw
Quoted 4 lines · Ming Lei

This issue is a bit serious, both blkg & disk & request_queue are leaked by commit c43332fe028c ("blk-cgroup: delay calling blkcg_exit_disk until disk_release").

Can we solve it before merging for-6.3/block into v6.3-rc1?

Yes. I'm testing a fix at the moment.