blk-cgroup: delay calling blkcg_exit_disk until disk_release
Subsystems (2)
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.
--- block/blk-throttle.c | 3 ++- block/genhd.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-)
block/blk-throttle.c +2 -1
@@ -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
@@ -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
Quoted 9 lines · Christoph Hellwig
While
del_gendiskensures 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_blkgwhen allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems fromdel_gendiskuntildisk_releaseto 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_releaseis called on a disk that hasn't been added. That's mostly harmless, except for a case inblk_throttl_exitthat 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
Quoted 6 lines · Ming Lei
Quoted 3 lines
This now can cause a case where
disk_releaseis called on a disk that hasn't been added. That's mostly harmless, except for a case inblk_throttl_exitthat now needs to check for a NULL ->td pointer.With this way,
blkcg_init_disk()could be called beforeq->root_blkgis 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.
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Ming Lei
Quoted 3 lines
This now can cause a case where
disk_releaseis called on a disk that hasn't been added. That's mostly harmless, except for a case inblk_throttl_exitthat now needs to check for a NULL ->td pointer.With this way,
blkcg_init_disk()could be called beforeq->root_blkgis released in disk unbind & rebind use case, then memory leak?
q->root_blkgis nowdisk->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
Quoted 4 lines · Ming Lei
Another thing is that
blkcg_init_disk()andblkcg_exit_disk()becomes asymmetrical with this patch. Soalloc_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.
Ming Lei
Reviewed-byQuoted 9 lines · Christoph Hellwig
Quoted 4 lines · Ming Lei
Another thing is that
blkcg_init_disk()andblkcg_exit_disk()becomes asymmetrical with this patch. Soalloc_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_diskis allowed afterdel_gendiskon 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
Quoted 10 lines · Christoph Hellwig
While
del_gendiskensures 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_blkgwhen allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems fromdel_gendiskuntildisk_releaseto 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
Quoted 9 lines · Christoph Hellwig
While
del_gendiskensures 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_blkgwhen allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems fromdel_gendiskuntildisk_releaseto 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_releaseis called on a disk that hasn't been added. That's mostly harmless, except for a case inblk_throttl_exitthat now needs to check for a NULL ->td pointer.Fixes 178fa7d49815 ("blk-cgroup: delay blk-cgroup initialization until add_disk")Reported-by Ming Lei <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>
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
Quoted 18 lines · Ming Lei
Quoted 9 lines · Christoph Hellwig
While
del_gendiskensures 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_blkgwhen allocating a new bio on a shut down file system. Delay freeing the blk-cgroup subsystems fromdel_gendiskuntildisk_releaseto 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_releaseis called on a disk that hasn't been added. That's mostly harmless, except for a case inblk_throttl_exitthat now needs to check for a NULL ->td pointer.Fixes 178fa7d49815 ("blk-cgroup: delay blk-cgroup initialization until add_disk")Reported-by Ming Lei <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>
Quoted 6 lines
hammmmmm, this patch actually causes bigger trouble.
After commit
84d7d462b16d("blk-cgroup: pin the gendisk in structblkcg_gq"),blkcg_gqinstance grabs disk's reference, so movingblkcg_exit_diskintodisk_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
Quoted 4 lines · Ming Lei
This issue is a bit serious, both blkg & disk &
request_queueare leaked by commitc43332fe028c("blk-cgroup: delay callingblkcg_exit_diskuntildisk_release").Can we solve it before merging for-6.3/block into v6.3-rc1?
Yes. I'm testing a fix at the moment.