From patchwork Sun Jan 14 14:42:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Coly Li X-Patchwork-Id: 10162631 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AF53260390 for ; Sun, 14 Jan 2018 14:43:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 99F9328A9B for ; Sun, 14 Jan 2018 14:43:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8D2A028AAE; Sun, 14 Jan 2018 14:43:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0D42128A9B for ; Sun, 14 Jan 2018 14:43:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751586AbeANOnh (ORCPT ); Sun, 14 Jan 2018 09:43:37 -0500 Received: from mx2.suse.de ([195.135.220.15]:48804 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751496AbeANOng (ORCPT ); Sun, 14 Jan 2018 09:43:36 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3E6BFAAB6; Sun, 14 Jan 2018 14:43:35 +0000 (UTC) From: Coly Li To: linux-bcache@vger.kernel.org Cc: linux-block@vger.kernel.org, Coly Li , Michael Lyle , Hannes Reinecke , Junhui Tang Subject: [PATCH v3 03/13] bcache: set task properly in allocator_wait() Date: Sun, 14 Jan 2018 22:42:26 +0800 Message-Id: <20180114144236.28213-4-colyli@suse.de> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180114144236.28213-1-colyli@suse.de> References: <20180114144236.28213-1-colyli@suse.de> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Kernel thread routine bch_allocator_thread() references macro allocator_wait() to wait for a condition or quit to do_exit() when kthread_should_stop() is true. Here is the code block, 284 while (1) { \ 285 set_current_state(TASK_INTERRUPTIBLE); \ 286 if (cond) \ 287 break; \ 288 \ 289 mutex_unlock(&(ca)->set->bucket_lock); \ 290 if (kthread_should_stop()) \ 291 return 0; \ 292 \ 293 schedule(); \ 294 mutex_lock(&(ca)->set->bucket_lock); \ 295 } \ 296 __set_current_state(TASK_RUNNING); \ At line 285, task state is set to TASK_INTERRUPTIBLE, if at line 290 kthread_should_stop() is true, the kernel thread will terminate and return to kernel/kthread.s:kthread(), then calls do_exit() with TASK_INTERRUPTIBLE state. This is not a suggested behavior and a warning message will be reported by might_sleep() in do_exit() code path: "WARNING: do not call blocking ops when !TASK_RUNNING; state=1 set at [xxxx]". This patch fixes this problem by setting task state to TASK_RUNNING if kthread_should_stop() is true and before kernel thread returns back to kernel/kthread.s:kthread(). Changelog: v2: fix the race issue in v1 patch. v1: initial buggy fix. Signed-off-by: Coly Li Cc: Michael Lyle Cc: Hannes Reinecke Cc: Junhui Tang Reviewed-by: Hannes Reinecke --- drivers/md/bcache/alloc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c index 6cc6c0f9c3a9..458e1d38577d 100644 --- a/drivers/md/bcache/alloc.c +++ b/drivers/md/bcache/alloc.c @@ -287,8 +287,10 @@ do { \ break; \ \ mutex_unlock(&(ca)->set->bucket_lock); \ - if (kthread_should_stop()) \ + if (kthread_should_stop()) { \ + set_current_state(TASK_RUNNING); \ return 0; \ + } \ \ schedule(); \ mutex_lock(&(ca)->set->bucket_lock); \