Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp323904ybg; Mon, 1 Jun 2020 02:17:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxyUSxT7jUgw1UgpZKqr0YccsYCvO4YG7Mv/qth10oUD9LaNXpT84GQmCn64/w3hghnWx3 X-Received: by 2002:a50:d1c4:: with SMTP id i4mr20491521edg.344.1591003046790; Mon, 01 Jun 2020 02:17:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591003046; cv=none; d=google.com; s=arc-20160816; b=Dzm5vLwFbi09nCDaIKutObukZ6yrKVbl+NHawCrkfJeGQB+iX6NkfmQ+WvbTKb/TSV POfTfFI1ztE9LNlVAG4BppcDvnMvlmLyiE+QXz5hDRDrwAstcLi1JmEaZLIsmu2AlxRl HguokY8qcl72r7co5aKx+wPUx5nkakRBUgCQVs3QDbn6oOtKu7BkTx355hO1vii8g5aN JeUB9ApVdeAQ1Ba4MH3XbCIJqhFaYqiBX//at83ShpMNhk2iWV4d/qb3I6aZLU8Hw8sV 4r2mjnHAH/OueIXHlt4cFHyL1dJokPhNGhhG3k8ldvLA+49u03G5s4ngcHXVu2SKZbLc hR+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=TdAYuVQSnOk1z7vvRh8qFOARxK0GH2x2UIl2rKYru+w=; b=u+PyQlblK6HKpkWHjxyRf3xhzwkBz9Odycv6tMNxt49Knd/fT0fu6qr4hjwFRxlBdh a866LfxrbGtB/BuzzlS+Zth/nVELq4362VPqRDXulZBlfyiXiByO6+BHtg3/5uU8tVHH 3EiNqkfGn8XxRadu4PVlK1vsqUAnIP/Q2jG+/Qwc9YjyvcIUc5w/NKb7bfT1XoEbRb+z 8O6sxN4EStgZAvuTDjclkyE4O2la7HD5NfvVjjmwG9daP08O4bw9lzoKjNgPpLANgJH9 /5xuUT/w92SSRkSG5y3AHxH/QbG/KQafSTVOfUJqPiMgNqDIykgGWDrZjgGmMN8YdoUT dWsw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f15si10623346ejx.180.2020.06.01.02.17.03; Mon, 01 Jun 2020 02:17:26 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726165AbgFAJNB (ORCPT + 99 others); Mon, 1 Jun 2020 05:13:01 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:5320 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725831AbgFAJNB (ORCPT ); Mon, 1 Jun 2020 05:13:01 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 67979171002AB7AD6DC3; Mon, 1 Jun 2020 17:12:59 +0800 (CST) Received: from huawei.com (10.175.104.175) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.487.0; Mon, 1 Jun 2020 17:12:52 +0800 From: Zhihao Cheng To: , CC: , Subject: [PATCH] ubi: check kthread_should_stop() after the setting of task state Date: Mon, 1 Jun 2020 17:12:31 +0800 Message-ID: <20200601091231.3794350-1-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.175] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- drivers/mtd/ubi/wl.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c index 5146cce5fe32..a4d4343053d7 100644 --- a/drivers/mtd/ubi/wl.c +++ b/drivers/mtd/ubi/wl.c @@ -1628,6 +1628,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; } -- 2.25.4