backing-dev: ensure that a removed bdi no longer has super_block referencing it
authorJens Axboe <jens.axboe@oracle.com>
Thu, 29 Oct 2009 10:46:12 +0000 (11:46 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Thu, 29 Oct 2009 10:46:12 +0000 (11:46 +0100)
When the bdi is being removed, we have to ensure that no super_blocks
currently have that cached in sb->s_bdi. Normally this is ensured by
the sb having a longer life span than the bdi, but if the device is
suddenly yanked, we have to kill this reference. sb->s_bdi is pointed
to freed memory at that point.

This fixes a problem with sync(1) hanging when a USB stick is pulled
without cleanly umounting it first.

Reported-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
mm/backing-dev.c

index 5a37e205571785cfcc93ee7b4b95180df4e951e4..1065b715ef644e74836d4adf3c877c17d654b542 100644 (file)
@@ -610,6 +610,21 @@ static void bdi_wb_shutdown(struct backing_dev_info *bdi)
                kthread_stop(wb->task);
 }
 
+/*
+ * This bdi is going away now, make sure that no super_blocks point to it
+ */
+static void bdi_prune_sb(struct backing_dev_info *bdi)
+{
+       struct super_block *sb;
+
+       spin_lock(&sb_lock);
+       list_for_each_entry(sb, &super_blocks, s_list) {
+               if (sb->s_bdi == bdi)
+                       sb->s_bdi = NULL;
+       }
+       spin_unlock(&sb_lock);
+}
+
 void bdi_unregister(struct backing_dev_info *bdi)
 {
        if (bdi->dev) {
@@ -682,6 +697,7 @@ void bdi_destroy(struct backing_dev_info *bdi)
                spin_unlock(&inode_lock);
        }
 
+       bdi_prune_sb(bdi);
        bdi_unregister(bdi);
 
        for (i = 0; i < NR_BDI_STAT_ITEMS; i++)