linux-bcache.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Coly Li <colyli@suse.de>
To: linux-bcache@vger.kernel.org
Cc: linux-block@vger.kernel.org, Coly Li <colyli@suse.de>,
	Michael Lyle <mlyle@lyle.org>, Hannes Reinecke <hare@suse.de>,
	Junhui Tang <tang.junhui@zte.com.cn>
Subject: [PATCH v2 03/12] bcache: set task properly in allocator_wait()
Date: Sun, 14 Jan 2018 01:10:41 +0800	[thread overview]
Message-ID: <20180113171050.22467-4-colyli@suse.de> (raw)
In-Reply-To: <20180113171050.22467-1-colyli@suse.de>

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 <colyli@suse.de>
Cc: Michael Lyle <mlyle@lyle.org>
Cc: Hannes Reinecke <hare@suse.de>
Cc: Junhui Tang <tang.junhui@zte.com.cn>
---
 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);			\
-- 
2.15.1

  parent reply	other threads:[~2018-01-13 17:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-13 17:10 [PATCH v2 00/12] bcache: device failure handling improvement Coly Li
2018-01-13 17:10 ` [PATCH v2 01/12] bcache: set writeback_rate_update_seconds in range [1, 60] seconds Coly Li
2018-01-13 17:10 ` [PATCH v2 02/12] bcache: properly set task state in bch_writeback_thread() Coly Li
2018-01-13 17:10 ` Coly Li [this message]
2018-01-13 17:10 ` [PATCH v2 04/12] bcache: fix cached_dev->count usage for bch_cache_set_error() Coly Li
2018-01-13 17:10 ` [PATCH v2 05/12] bcache: stop dc->writeback_rate_update properly Coly Li
2018-01-13 17:10 ` [PATCH v2 06/12] bcache: set error_limit correctly Coly Li
2018-01-13 17:10 ` [PATCH v2 07/12] bcache: add CACHE_SET_IO_DISABLE to struct cache_set flags Coly Li
2018-01-13 17:10 ` [PATCH v2 08/12] bcache: stop all attached bcache devices for a retired cache set Coly Li
2018-01-13 17:10 ` [PATCH v2 09/12] bcache: fix inaccurate io state for detached bcache devices Coly Li
2018-01-13 17:10 ` [PATCH v2 10/12] bcache: add backing_request_endio() for bi_end_io of attached backing device I/O Coly Li
2018-01-13 17:10 ` [PATCH v2 11/12] bcache: add io_disable to struct cached_dev Coly Li
2018-01-13 17:10 ` [PATCH v2 12/12] bcache: stop bcache device when backing device is offline Coly Li
  -- strict thread matches above, loose matches on Subject: below --
2018-01-13 17:01 [PATCH v2 00/12] bcache: device failure handling improvement Coly Li
2018-01-13 17:01 ` [PATCH v2 03/12] bcache: set task properly in allocator_wait() Coly Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180113171050.22467-4-colyli@suse.de \
    --to=colyli@suse.de \
    --cc=hare@suse.de \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=mlyle@lyle.org \
    --cc=tang.junhui@zte.com.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).