diff mbox series

[stable-4.19,8/8] md: Set prev_flush_start and flush_bio in an atomic way

Message ID 20210203132022.92406-9-jinpu.wang@cloud.ionos.com
State Superseded
Headers show
Series Some misc fixes | expand

Commit Message

Jinpu Wang Feb. 3, 2021, 1:20 p.m. UTC
From: Xiao Ni <xni@redhat.com>

One customer reports a crash problem which causes by flush request. It
triggers a warning before crash.

        /* new request after previous flush is completed */
        if (ktime_after(req_start, mddev->prev_flush_start)) {
                WARN_ON(mddev->flush_bio);
                mddev->flush_bio = bio;
                bio = NULL;
        }

The WARN_ON is triggered. We use spin lock to protect prev_flush_start and
flush_bio in md_flush_request. But there is no lock protection in
md_submit_flush_data. It can set flush_bio to NULL first because of
compiler reordering write instructions.

For example, flush bio1 sets flush bio to NULL first in
md_submit_flush_data. An interrupt or vmware causing an extended stall
happen between updating flush_bio and prev_flush_start. Because flush_bio
is NULL, flush bio2 can get the lock and submit to underlayer disks. Then
flush bio1 updates prev_flush_start after the interrupt or extended stall.

Then flush bio3 enters in md_flush_request. The start time req_start is
behind prev_flush_start. The flush_bio is not NULL(flush bio2 hasn't
finished). So it can trigger the WARN_ON now. Then it calls INIT_WORK
again. INIT_WORK() will re-initialize the list pointers in the
work_struct, which then can result in a corrupted work list and the
work_struct queued a second time. With the work list corrupted, it can
lead in invalid work items being used and cause a crash in
process_one_work.

We need to make sure only one flush bio can be handled at one same time.
So add spin lock in md_submit_flush_data to protect prev_flush_start and
flush_bio in an atomic way.

Reviewed-by: David Jeffery <djeffery@redhat.com>
Signed-off-by: Xiao Ni <xni@redhat.com>
Signed-off-by: Song Liu <songliubraving@fb.com>
[jwang: backport dc5d17a3c39b06aef866afca19245a9cfb533a79 to 4.19]
Signed-off-by: Jack Wang <jinpu.wang@cloud.ionos.com>
---
 drivers/md/md.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Greg KH Feb. 4, 2021, 3:12 p.m. UTC | #1
On Wed, Feb 03, 2021 at 02:20:22PM +0100, Jack Wang wrote:
> From: Xiao Ni <xni@redhat.com>
> 
> One customer reports a crash problem which causes by flush request. It
> triggers a warning before crash.
> 
>         /* new request after previous flush is completed */
>         if (ktime_after(req_start, mddev->prev_flush_start)) {
>                 WARN_ON(mddev->flush_bio);
>                 mddev->flush_bio = bio;
>                 bio = NULL;
>         }
> 
> The WARN_ON is triggered. We use spin lock to protect prev_flush_start and
> flush_bio in md_flush_request. But there is no lock protection in
> md_submit_flush_data. It can set flush_bio to NULL first because of
> compiler reordering write instructions.
> 
> For example, flush bio1 sets flush bio to NULL first in
> md_submit_flush_data. An interrupt or vmware causing an extended stall
> happen between updating flush_bio and prev_flush_start. Because flush_bio
> is NULL, flush bio2 can get the lock and submit to underlayer disks. Then
> flush bio1 updates prev_flush_start after the interrupt or extended stall.
> 
> Then flush bio3 enters in md_flush_request. The start time req_start is
> behind prev_flush_start. The flush_bio is not NULL(flush bio2 hasn't
> finished). So it can trigger the WARN_ON now. Then it calls INIT_WORK
> again. INIT_WORK() will re-initialize the list pointers in the
> work_struct, which then can result in a corrupted work list and the
> work_struct queued a second time. With the work list corrupted, it can
> lead in invalid work items being used and cause a crash in
> process_one_work.
> 
> We need to make sure only one flush bio can be handled at one same time.
> So add spin lock in md_submit_flush_data to protect prev_flush_start and
> flush_bio in an atomic way.
> 
> Reviewed-by: David Jeffery <djeffery@redhat.com>
> Signed-off-by: Xiao Ni <xni@redhat.com>
> Signed-off-by: Song Liu <songliubraving@fb.com>
> [jwang: backport dc5d17a3c39b06aef866afca19245a9cfb533a79 to 4.19]

I can not take patches backported to older kernels that "skip" kernel
releases.

For example, if I take this into 4.19.y, and then someone moves to 5.4
or 5.10, they will hit the same issue.

So please provide a backported series for all affected releases, back as
far as you want, but never skip releases.

I can't take this series, I'll drop it for now and wait for an updated
set of patches.

thanks,

greg k-h
Jinpu Wang Feb. 5, 2021, 6:57 a.m. UTC | #2
On Thu, Feb 4, 2021 at 4:12 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>

> On Wed, Feb 03, 2021 at 02:20:22PM +0100, Jack Wang wrote:

> > From: Xiao Ni <xni@redhat.com>

> >

> > One customer reports a crash problem which causes by flush request. It

> > triggers a warning before crash.

> >

> >         /* new request after previous flush is completed */

> >         if (ktime_after(req_start, mddev->prev_flush_start)) {

> >                 WARN_ON(mddev->flush_bio);

> >                 mddev->flush_bio = bio;

> >                 bio = NULL;

> >         }

> >

> > The WARN_ON is triggered. We use spin lock to protect prev_flush_start and

> > flush_bio in md_flush_request. But there is no lock protection in

> > md_submit_flush_data. It can set flush_bio to NULL first because of

> > compiler reordering write instructions.

> >

> > For example, flush bio1 sets flush bio to NULL first in

> > md_submit_flush_data. An interrupt or vmware causing an extended stall

> > happen between updating flush_bio and prev_flush_start. Because flush_bio

> > is NULL, flush bio2 can get the lock and submit to underlayer disks. Then

> > flush bio1 updates prev_flush_start after the interrupt or extended stall.

> >

> > Then flush bio3 enters in md_flush_request. The start time req_start is

> > behind prev_flush_start. The flush_bio is not NULL(flush bio2 hasn't

> > finished). So it can trigger the WARN_ON now. Then it calls INIT_WORK

> > again. INIT_WORK() will re-initialize the list pointers in the

> > work_struct, which then can result in a corrupted work list and the

> > work_struct queued a second time. With the work list corrupted, it can

> > lead in invalid work items being used and cause a crash in

> > process_one_work.

> >

> > We need to make sure only one flush bio can be handled at one same time.

> > So add spin lock in md_submit_flush_data to protect prev_flush_start and

> > flush_bio in an atomic way.

> >

> > Reviewed-by: David Jeffery <djeffery@redhat.com>

> > Signed-off-by: Xiao Ni <xni@redhat.com>

> > Signed-off-by: Song Liu <songliubraving@fb.com>

> > [jwang: backport dc5d17a3c39b06aef866afca19245a9cfb533a79 to 4.19]

>

> I can not take patches backported to older kernels that "skip" kernel

> releases.

>

> For example, if I take this into 4.19.y, and then someone moves to 5.4

> or 5.10, they will hit the same issue.

>

> So please provide a backported series for all affected releases, back as

> far as you want, but never skip releases.

>

> I can't take this series, I'll drop it for now and wait for an updated

> set of patches.

>

> thanks,

>

> greg k-h

Hi Greg,

Thanks for reply, only this patch should be backported also to
5.4/5.10, this backport can be applied cleanly to stable/linux-5.4.y
and stable/linux-5.10.y,

I will send the backport for them later today!

Thanks!

J
diff mbox series

Patch

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 80ca13594c18..09f0d8e70b70 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -474,8 +474,10 @@  static void md_submit_flush_data(struct work_struct *ws)
 	 * could wait for this and below md_handle_request could wait for those
 	 * bios because of suspend check
 	 */
+	spin_lock_irq(&mddev->lock);
 	mddev->last_flush = mddev->start_flush;
 	mddev->flush_bio = NULL;
+	spin_unlock_irq(&mddev->lock);
 	wake_up(&mddev->sb_wait);
 
 	if (bio->bi_iter.bi_size == 0) {