Skip to content

Commit

Permalink
ext4: fix panic on module unload when stopping lazyinit thread
Browse files Browse the repository at this point in the history
https://bugzilla.kernel.org/show_bug.cgi?id=27652

If the lazyinit thread is running, the teardown function
ext4_destroy_lazyinit_thread() has problems:

        ext4_clear_request_list();
        while (ext4_li_info->li_task) {
                wake_up(&ext4_li_info->li_wait_daemon);
                wait_event(ext4_li_info->li_wait_task,
                           ext4_li_info->li_task == NULL);
        }

Clearing the request list will cause the thread to exit and free
ext4_li_info, so then we're waiting on something which is getting
freed.

Fix this up by making the thread respond to kthread_stop, and exit,
without the need to wait for that exit in some other homegrown way.

Cc: stable@kernel.org
Reported-and-Tested-by: Tao Ma <boyu.mt@taobao.com>
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
  • Loading branch information
Eric Sandeen authored and Theodore Ts'o committed Feb 3, 2011
1 parent 1bae4ce commit 8f1f745
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions fs/ext4/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
const char *dev_name, void *data);
static void ext4_destroy_lazyinit_thread(void);
static void ext4_unregister_li_request(struct super_block *sb);
static void ext4_clear_request_list(void);

#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
static struct file_system_type ext3_fs_type = {
Expand Down Expand Up @@ -2716,6 +2717,8 @@ static void ext4_unregister_li_request(struct super_block *sb)
mutex_unlock(&ext4_li_info->li_list_mtx);
}

static struct task_struct *ext4_lazyinit_task;

/*
* This is the function where ext4lazyinit thread lives. It walks
* through the request list searching for next scheduled filesystem.
Expand Down Expand Up @@ -2784,6 +2787,10 @@ static int ext4_lazyinit_thread(void *arg)
if (time_before(jiffies, next_wakeup))
schedule();
finish_wait(&eli->li_wait_daemon, &wait);
if (kthread_should_stop()) {
ext4_clear_request_list();
goto exit_thread;
}
}

exit_thread:
Expand All @@ -2808,6 +2815,7 @@ static int ext4_lazyinit_thread(void *arg)
wake_up(&eli->li_wait_task);

kfree(ext4_li_info);
ext4_lazyinit_task = NULL;
ext4_li_info = NULL;
mutex_unlock(&ext4_li_mtx);

Expand All @@ -2830,11 +2838,10 @@ static void ext4_clear_request_list(void)

static int ext4_run_lazyinit_thread(void)
{
struct task_struct *t;

t = kthread_run(ext4_lazyinit_thread, ext4_li_info, "ext4lazyinit");
if (IS_ERR(t)) {
int err = PTR_ERR(t);
ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
ext4_li_info, "ext4lazyinit");
if (IS_ERR(ext4_lazyinit_task)) {
int err = PTR_ERR(ext4_lazyinit_task);
ext4_clear_request_list();
del_timer_sync(&ext4_li_info->li_timer);
kfree(ext4_li_info);
Expand Down Expand Up @@ -2985,16 +2992,10 @@ static void ext4_destroy_lazyinit_thread(void)
* If thread exited earlier
* there's nothing to be done.
*/
if (!ext4_li_info)
if (!ext4_li_info || !ext4_lazyinit_task)
return;

ext4_clear_request_list();

while (ext4_li_info->li_task) {
wake_up(&ext4_li_info->li_wait_daemon);
wait_event(ext4_li_info->li_wait_task,
ext4_li_info->li_task == NULL);
}
kthread_stop(ext4_lazyinit_task);
}

static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Expand Down

0 comments on commit 8f1f745

Please sign in to comment.