Received: by 10.192.165.148 with SMTP id m20csp534836imm; Wed, 25 Apr 2018 03:51:36 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/849JZXC8lJ87Wk3YbPQ3oBaOeZsG9owd8jFrGfRaTZWI8B78Z5fyFkKfgUVJGC18l6GId X-Received: by 10.98.220.78 with SMTP id t75mr26807677pfg.139.1524653496838; Wed, 25 Apr 2018 03:51:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524653496; cv=none; d=google.com; s=arc-20160816; b=Jn+oN1v4k/OfQSpoxuX6DZ59Cv33w3+lVN3TsAMdty1ZqwDuHlSrCMxSmLQvmZABI1 70pDXmmvEFUzqrjYXfcFvWvv2xWm9rJ2X3NDkQcyO58taW659BNdwnL6Bu3GgHiQqXkA jN2GpQOUN+9gv1CMgQvqwjFP7Vom+j0DxOSMTBl2PhDeuWNmPJdDhj4vXmzvoqFfaOc7 S+VTsq64ciVJdj8CEXIMqpLDpAHPNIT+G2eqj8Q61UhP7BuMkKjrRh3C9XhK3xI6qq5c Q8f2mtGGffYh19vJVykIw3bSO5e+3w4NKZRF+Yvh4dL/IrSKmpm0t5TxeqnFroJnu1Pz 1S+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=7vCFnSr7UGIxu2xHGFGzSsF6IopczvwhJ/X3fP+an/Y=; b=vw2VP42H7dTiDMv1SSg1WaLkqFKIvtKacS+24vDJXXLiKH6phzlS/MTKTwTtNzIRzQ i+GAaBJ7je5W2al11GuT+iAcKSaw1ie1w0uae5N/asj47sH9ffwSod3pQU4p7iw91ZZ5 sUGbebN0a/n84Tmd21zKju0YGiI8eDY4XUjejx5uzrCDAaVmHO6lX1NnGFOx5/Ji9Gix oX4fowSYvm98crqmKFjpLqEuuIe0iLa6Or3pS1RVd0S0AMtd/PAxjSqm0axGpjI7VBi5 BNun1NEqMJA8yakk7djhCq3nnA/FbrPCSJrbdYuov6O2jZOd1IqrFRwfFU/LOTHCp6A6 woiw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g62si13135084pgc.693.2018.04.25.03.51.22; Wed, 25 Apr 2018 03:51:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754587AbeDYKuB (ORCPT + 99 others); Wed, 25 Apr 2018 06:50:01 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:53354 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754340AbeDYKof (ORCPT ); Wed, 25 Apr 2018 06:44:35 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id A09FB266; Wed, 25 Apr 2018 10:44:34 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Coly Li , Hannes Reinecke , Michael Lyle , Junhui Tang , Jens Axboe , Sasha Levin Subject: [PATCH 4.14 147/183] bcache: properly set task state in bch_writeback_thread() Date: Wed, 25 Apr 2018 12:36:07 +0200 Message-Id: <20180425103248.448317528@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425103242.532713678@linuxfoundation.org> References: <20180425103242.532713678@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Coly Li [ Upstream commit 99361bbf26337186f02561109c17a4c4b1a7536a ] Kernel thread routine bch_writeback_thread() has the following code block, 447 down_write(&dc->writeback_lock); 448~450 if (check conditions) { 451 up_write(&dc->writeback_lock); 452 set_current_state(TASK_INTERRUPTIBLE); 453 454 if (kthread_should_stop()) 455 return 0; 456 457 schedule(); 458 continue; 459 } If condition check is true, its task state is set to TASK_INTERRUPTIBLE and call schedule() to wait for others to wake up it. There are 2 issues in current code, 1, Task state is set to TASK_INTERRUPTIBLE after the condition checks, if another process changes the condition and call wake_up_process(dc-> writeback_thread), then at line 452 task state is set back to TASK_INTERRUPTIBLE, the writeback kernel thread will lose a chance to be waken up. 2, At line 454 if kthread_should_stop() is true, writeback kernel thread will return to kernel/kthread.c:kthread() with TASK_INTERRUPTIBLE and call do_exit(). It is not good to enter do_exit() with task state TASK_INTERRUPTIBLE, in following code path might_sleep() is called and a warning message is reported by __might_sleep(): "WARNING: do not call blocking ops when !TASK_RUNNING; state=1 set at [xxxx]". For the first issue, task state should be set before condition checks. Ineed because dc->writeback_lock is required when modifying all the conditions, calling set_current_state() inside code block where dc-> writeback_lock is hold is safe. But this is quite implicit, so I still move set_current_state() before all the condition checks. For the second issue, frankley speaking it does not hurt when kernel thread exits with TASK_INTERRUPTIBLE state, but this warning message scares users, makes them feel there might be something risky with bcache and hurt their data. Setting task state to TASK_RUNNING before returning fixes this problem. In alloc.c:allocator_wait(), there is also a similar issue, and is also fixed in this patch. Changelog: v3: merge two similar fixes into one patch v2: fix the race issue in v1 patch. v1: initial buggy fix. Signed-off-by: Coly Li Reviewed-by: Hannes Reinecke Reviewed-by: Michael Lyle Cc: Michael Lyle Cc: Junhui Tang Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/md/bcache/alloc.c | 4 +++- drivers/md/bcache/writeback.c | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) --- 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); \ --- a/drivers/md/bcache/writeback.c +++ b/drivers/md/bcache/writeback.c @@ -420,18 +420,21 @@ static int bch_writeback_thread(void *ar while (!kthread_should_stop()) { down_write(&dc->writeback_lock); + set_current_state(TASK_INTERRUPTIBLE); if (!atomic_read(&dc->has_dirty) || (!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) && !dc->writeback_running)) { up_write(&dc->writeback_lock); - set_current_state(TASK_INTERRUPTIBLE); - if (kthread_should_stop()) + if (kthread_should_stop()) { + set_current_state(TASK_RUNNING); return 0; + } schedule(); continue; } + set_current_state(TASK_RUNNING); searched_full_index = refill_dirty(dc);