md/bitmap: factor out a helper to set timeout
authorYu Kuai <yukuai3@huawei.com>
Tue, 23 May 2023 02:10:16 +0000 (10:10 +0800)
committerSong Liu <song@kernel.org>
Tue, 13 Jun 2023 22:25:13 +0000 (15:25 -0700)
commit4eeb6535cd51100460ec8873bb68addef17b3e81
treee0ee7a0187df038af55335cead677cedff99189f
parentc333673a78307abe6b1f6998809288fcd86740ed
md/bitmap: factor out a helper to set timeout

Register/unregister 'mddev->thread' are both under 'reconfig_mutex',
however, some context didn't hold the mutex to access mddev->thread,
which can cause null-ptr-deference:

1) md_bitmap_daemon_work() can be called from md_check_recovery() where
'reconfig_mutex' is not held, deference 'mddev->thread' might cause
null-ptr-deference, because md_unregister_thread() reset the pointer
before stopping the thread.

2) timeout_store() access 'mddev->thread' multiple times,
null-ptr-deference can be triggered if 'mddev->thread' is reset in the
middle.

This patch factor out a helper to set timeout, the new helper always
check if 'mddev->thread' is null first, so that problem 1 can be fixed.

Now that this helper only access 'mddev->thread' once, but it's possible
that 'mddev->thread' can be freed while this helper is still in progress,
hence the problem is not fixed yet. Follow up patches will fix this by
protecting md_thread with rcu.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Signed-off-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/r/20230523021017.3048783-5-yukuai1@huaweicloud.com
drivers/md/md-bitmap.c