From patchwork Wed Feb 7 19:41:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Lyle X-Patchwork-Id: 10205891 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 607EB602D8 for ; Wed, 7 Feb 2018 19:42:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5093E29100 for ; Wed, 7 Feb 2018 19:42:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 44E2E2910C; Wed, 7 Feb 2018 19:42:06 +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,DKIM_SIGNED, DKIM_VALID,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 7662029100 for ; Wed, 7 Feb 2018 19:42:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754157AbeBGTmC (ORCPT ); Wed, 7 Feb 2018 14:42:02 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:42647 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754266AbeBGTmB (ORCPT ); Wed, 7 Feb 2018 14:42:01 -0500 Received: by mail-pf0-f195.google.com with SMTP id b25so765766pfd.9 for ; Wed, 07 Feb 2018 11:42:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lyle-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GtQUPs0wGJIxahsBKm+odL92GIQfI9c+Vwri8s4jmEY=; b=Gut6EeFHvQfp1bN+Wk8QTK1EMTR4YeaMHr7TnZ7kSDbGWS67gXQxzmSWJYJkCbecy6 ZTjYa9fiJ/7xNUMBuV/VJEOiXfRX5Vzbwg/y2u4u7rjzhtQXOxYpJ019ZFewR4aPa0lv hpuKhakCQE1Bjm+1ayVva6a8gusKEeVD5Yy3OW9z+f2+lKWVXKXnyWs7CIqOjgh6OAIg HkdAJY+jHjSjNsc33BrfnF3++tjwW742GJws0to16EEvU8v+eX1CNRBdtihO5zBOW2fC 9Di+mbt1vAmhRQN7iHVER8QKgVqMBpMh149520/kzqy/uZ96ElAAbwyQHkdU6wU6Ij7s mAKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GtQUPs0wGJIxahsBKm+odL92GIQfI9c+Vwri8s4jmEY=; b=OSP6PWsG3CeazWVtnj4HW2JA3H1igFMaf9ASCQj+2DvSsYtR+FhLP7q2OCTKTpIPZv Fs7uxh4eB6O9a7+/3x03wYVWpoFAbBTYYnhCM8A9PEBxq6CU3ORc8uRMUy/si4jM2Vy/ mpTOsXN/kqS8D7tVF+iYn5+nSTJz5eQb73+2gOgu8IX71MEMC2KeiDN5z/G7EGFji+ii Mf+tITsmFzMJiPCOKL6mACMet/sVTqhX96yPZdRkfLqA3ewM8aUw/t5ZN2HGYnnCspEd tQoaRXqDLW6KEN6YjlV9Z2YZjOFAVousgFQ8xZ0kCT+CSeSnD9RXvtvZiEav69kHUS+f 97NQ== X-Gm-Message-State: APf1xPBDWcunkP3IXmaR32oiFDcZzSy2jQVqYhwltzxq3RfiKZYLYglw 6CE+OsjgAGiGELZYREuH1Xu6qA== X-Google-Smtp-Source: AH8x226d/LpfqW9VAFjrJ2/D855sKJMSXxvSPM+2UDOV0Tpydb9rO4elCmbWJDAX6tgfwwUhfxRNvQ== X-Received: by 10.101.86.201 with SMTP id w9mr5756198pgs.434.1518032520357; Wed, 07 Feb 2018 11:42:00 -0800 (PST) Received: from midnight.lan (2600-6c52-6200-09b7-0000-0000-0000-0d66.dhcp6.chtrptr.net. [2600:6c52:6200:9b7::d66]) by smtp.gmail.com with ESMTPSA id q24sm6136554pgn.46.2018.02.07.11.41.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Feb 2018 11:41:59 -0800 (PST) From: Michael Lyle To: linux-bcache@vger.kernel.org, linux-block@vger.kernel.org Cc: axboe@fb.com, Coly Li , Michael Lyle , Junhui Tang Subject: [PATCH 3/8] bcache: properly set task state in bch_writeback_thread() Date: Wed, 7 Feb 2018 11:41:41 -0800 Message-Id: <20180207194146.5095-4-mlyle@lyle.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180207194146.5095-1-mlyle@lyle.org> References: <20180207194146.5095-1-mlyle@lyle.org> 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 From: Coly Li 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 --- drivers/md/bcache/alloc.c | 4 +++- drivers/md/bcache/writeback.c | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) 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); \ diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c index 51306a19ab03..58218f7e77c3 100644 --- a/drivers/md/bcache/writeback.c +++ b/drivers/md/bcache/writeback.c @@ -564,18 +564,21 @@ static int bch_writeback_thread(void *arg) 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);