Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp833145pxb; Tue, 3 Nov 2020 13:52:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJy5tvxGStVQCVAa4K++su+vIeEorf05WOMd6pPRKmYBWTU2ESifrvYQh0E26nOjcO4hUosC X-Received: by 2002:a17:906:742:: with SMTP id z2mr21437605ejb.57.1604440340288; Tue, 03 Nov 2020 13:52:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604440340; cv=none; d=google.com; s=arc-20160816; b=WTy+0OuJjQVr8F0h6/ggnhQcfinGhrrjK23PusFxtbsNXe/GX/ikPZo1UboTWH4HMd qku55DLO7vPspCyKfOp5YWeXfGGczzBCb2VzRBICM6DkmDz8tHHWZI9UXWXMhfTnNjxL nhyj7OjMPwj8ylJwtvFf5wobR9jD+QFjykRakh26T+9Hsme1bJOGFNWDcjjfgZnSWB6u 9TS2xteKEMAVgNsVuqU7IFLDInZZkWW2dojElfBzI3j9SBK83a49D0o63DRYfanf4sSM Qo/HDrDLZ56fbSFS6yRMFAVoAea2kBLIbNX3q1Ul28sF/5jMHsgNLXpu479u2hhEbURF r23w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zdMT0sqPuN3VqoJsTL/l7rn4ny2H8XuBft9lUqzQtkA=; b=t136Pw47VRJbod2tD/o7JR/QDmOu/65Y5zbE9fKzHk2/rT/G5T1mZ8gTDEjDSX0gRm ig53EABfPUPHyThSHmKZOUW+CwUGvqKBBX+6zPQXpfSfJVRB94g6kctetk+hAvxHfL+r 5Pjj2huuyd7IrRCR1zZxCVLjtzwt9ASDHjvJIoA99/6iJ0aPT1/WNHmoeHDxB2L1C0Vx +UfuVHUEQwU98pSBBTajULEDFX98MeBTWwlXZNhfBPVMiAF8gBBarNjueFPywn0f8Qfz 3FEX/TlcNGCyUpgceupLQHcHVnP4Qe5y+qxAIK4SfrAONy868uYy/hbjEvK3yb+5NEbQ mXdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k2ameiwG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i14si43851eje.464.2020.11.03.13.51.57; Tue, 03 Nov 2020 13:52:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k2ameiwG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730913AbgKCVtZ (ORCPT + 99 others); Tue, 3 Nov 2020 16:49:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:42866 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731188AbgKCUti (ORCPT ); Tue, 3 Nov 2020 15:49:38 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0F63020719; Tue, 3 Nov 2020 20:49:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604436578; bh=W/wWV///xM5VSgh9MqitXakSYutwEYqy9wk82CiwRDU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k2ameiwGFfqo7Mgof+ZnTcTRsH1qFzgfKm9vD3xv3TyukM7Cb33pdIi3NrGZigXX6 +FBhvUMuYDf0bZS/jQXB0Nkdf8Cn+w7ppNnod+5yGVnhf2Stov46QfRLbJ0cM7B5qw jWh3CuxsB7cJrqZy5TNDUBLoW2lZaY+I+qikNQQU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhihao Cheng , syzbot+853639d0cb16c31c7a14@syzkaller.appspotmail.com, Richard Weinberger Subject: [PATCH 5.9 312/391] ubi: check kthread_should_stop() after the setting of task state Date: Tue, 3 Nov 2020 21:36:03 +0100 Message-Id: <20201103203408.163282671@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203348.153465465@linuxfoundation.org> References: <20201103203348.153465465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhihao Cheng commit d005f8c6588efcfbe88099b6edafc6f58c84a9c1 upstream. A detach hung is possible when a race occurs between the detach process and the ubi background thread. The following sequences outline the race: ubi thread: if (list_empty(&ubi->works)... ubi detach: set_bit(KTHREAD_SHOULD_STOP, &kthread->flags) => by kthread_stop() wake_up_process() => ubi thread is still running, so 0 is returned ubi thread: set_current_state(TASK_INTERRUPTIBLE) schedule() => ubi thread will never be scheduled again ubi detach: wait_for_completion() => hung task! To fix that, we need to check kthread_should_stop() after we set the task state, so the ubi thread will either see the stop bit and exit or the task state is reset to runnable such that it isn't scheduled out indefinitely. Signed-off-by: Zhihao Cheng Cc: Fixes: 801c135ce73d5df1ca ("UBI: Unsorted Block Images") Reported-by: syzbot+853639d0cb16c31c7a14@syzkaller.appspotmail.com Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/ubi/wl.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) --- a/drivers/mtd/ubi/wl.c +++ b/drivers/mtd/ubi/wl.c @@ -1639,6 +1639,19 @@ int ubi_thread(void *u) !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) { set_current_state(TASK_INTERRUPTIBLE); spin_unlock(&ubi->wl_lock); + + /* + * Check kthread_should_stop() after we set the task + * state to guarantee that we either see the stop bit + * and exit or the task state is reset to runnable such + * that it's not scheduled out indefinitely and detects + * the stop bit at kthread_should_stop(). + */ + if (kthread_should_stop()) { + set_current_state(TASK_RUNNING); + break; + } + schedule(); continue; }