Received: by 10.213.65.68 with SMTP id h4csp2112615imn; Sun, 8 Apr 2018 20:02:23 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/6gvfj3rV3QeZAsgVhiVp0grI/op4SaNICW6L3SG5K2WfQM9dEwmSHJn02VMx5qlQ3eg4x X-Received: by 10.101.101.10 with SMTP id x10mr24601032pgv.0.1523242943183; Sun, 08 Apr 2018 20:02:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523242943; cv=none; d=google.com; s=arc-20160816; b=Dcko2FrjE5HP3znHcx9nGuRPphtY90FwcpXkIuZI+uPsPApOMlty/OWda7Wcndo0EA IQN3oA+175my0uiLMHExVJPBJj9fYDjzVwryAk4fC18JiHLvNOjsIRQuuC/1DjerPhP4 S4RuUfVkqChXssomMPwbJRQ5PdFM4G+XW4ZvLze9x74VA4lEWzkoChAK8fHt8i9tAkED DgmfBj/AqIP6h9vu8/fnkkEm6EmAPc4YrY5VbZ5f3zclgFge7d+pRRlaHFC0GYgymP9y gkqcQlzTZaRr/sNO4EJGAGwXd1T1xG4XnjeiTel6X1llcnetrhSR4ZnruxCbCvIojmr4 ZjhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=/FzdpxEORD5e5fzx9weTCW4YacEjRn275psqJDP3bMc=; b=00Rr5mYROYW73nQFhe0I4PuBENrFzfWceAYQXVNZGtoho+rwHUCdQiStWgq7Q9BqBv ujn0VJGkIvnqhwEMQ+iFdf089ykpiLaEao86FISX4IKKsEPitqcTAzXOcO6BBEl2uH2y 4PxTSE4GAT08/awrAm5ztdX/qm3GY1Os/q+xgCsN/mQToOQ9rFKKRrEuzIULuuj0VTsg BkRpKEzdpgQMuxDt6sKhU9NK39UpQWSGo3xsTeUV4XzTQl8XWJ8DKu1oXpYpSzxxWUI/ zQlpl7xDmKMjIshjac9Re6K5e8xDG+2aIkQo/1fNTkpMrnxhMRqohSE3XMrEqrlCUsqD 0tBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=Q+7mzN7U; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b60-v6si10728097plc.610.2018.04.08.20.01.46; Sun, 08 Apr 2018 20:02:23 -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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=Q+7mzN7U; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754477AbeDIAZv (ORCPT + 99 others); Sun, 8 Apr 2018 20:25:51 -0400 Received: from mail-sn1nam02on0104.outbound.protection.outlook.com ([104.47.36.104]:48312 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755231AbeDIAZm (ORCPT ); Sun, 8 Apr 2018 20:25:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=/FzdpxEORD5e5fzx9weTCW4YacEjRn275psqJDP3bMc=; b=Q+7mzN7Uo7h2HLefquaDoV8JXvVzZe2bmfvg+fKXVh3CRoyvN3Rqj+B5eXN2RAlTkZRfPA6Ro8ZRd8TLrxwYtjJzbFRBbz5OYLagtTB2CwhB1kUu+8Y0jihacV73BDUgA9f0sX+TtfTnkM+piXNJSfPYEIXc2ioqTolcUgDELAA= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0725.namprd21.prod.outlook.com (10.167.107.167) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.696.0; Mon, 9 Apr 2018 00:25:40 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059%2]) with mapi id 15.20.0696.003; Mon, 9 Apr 2018 00:25:40 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Coly Li , Michael Lyle , Junhui Tang , Jens Axboe , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 136/161] bcache: properly set task state in bch_writeback_thread() Thread-Topic: [PATCH AUTOSEL for 4.14 136/161] bcache: properly set task state in bch_writeback_thread() Thread-Index: AQHTz5i9mkZJgERp2kiL2Ha9LIidig== Date: Mon, 9 Apr 2018 00:21:44 +0000 Message-ID: <20180409001936.162706-136-alexander.levin@microsoft.com> References: <20180409001936.162706-1-alexander.levin@microsoft.com> In-Reply-To: <20180409001936.162706-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0725;7:teV0OBpG6YPuryUXNDvjzhT7b+fyBcLjfYCAfVyOxgD0Mh37TSgb62BRBMSQPN1++598W2jXUgHePNamy9nEGgzghvoBAolJ0L/iVzZKwbSohfqoWwl2l5BRpJvO5hWtue6e+auSHc4YgYh7SsVCSGNIvRuQasL2MeX00MkRui6L6cP1xGZtuhTz4siAZ1CfF+oUa1Z5lKy9RmZT07Ruaqr20/syMiowj8JZk1rplK2meRSbRG1GMsTvc+j79Z4X;20:poE9u4QOOgr8ES6H+0D1Gl4qMbDmcLvVElbsmCy9b0OdJHyDyC8o5JeyYV8U/AOh23do0VpgpG6d+m31adpSI8N4JM2Y5x5Jkldv0dyRulDQzLiiB5NHsqLUvuBd/yRNqh1KTUWiY6Ovjhs+3IlhXjQ1U1yadGuo7fWIWQk54tY= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: 1ae58bcb-de5c-4027-de5a-08d59db06cb0 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0725; x-ms-traffictypediagnostic: DM5PR2101MB0725: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(17755550239193); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(5005006)(8121501046)(10201501046)(3231221)(944501327)(52105095)(93006095)(93001095)(3002001)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123562045)(20161123558120)(20161123560045)(6072148)(201708071742011);SRVR:DM5PR2101MB0725;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0725; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(346002)(366004)(376002)(396003)(39860400002)(54534003)(199004)(189003)(31014005)(6486002)(81166006)(26005)(6512007)(4326008)(6436002)(81156014)(8676002)(97736004)(186003)(36756003)(72206003)(53936002)(107886003)(2906002)(86362001)(76176011)(110136005)(305945005)(10090500001)(486006)(3660700001)(102836004)(5660300001)(14454004)(476003)(86612001)(446003)(99286004)(68736007)(66066001)(11346002)(54906003)(2616005)(1076002)(7736002)(5250100002)(25786009)(8936002)(478600001)(2900100001)(2501003)(3280700002)(105586002)(59450400001)(316002)(106356001)(22452003)(6506007)(3846002)(10290500003)(6116002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0725;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: oxgqlHKtyn3bTf3K5RrwWmIa0RhqYbmLrzszpZ+1H4IClTGumnHKw9jhgc4VQvKiea0FBv4UuCjv4VJyT4eXSCJplwxVOBOUqdEuP20Md5O+Yq/p5Wc4F5vO8xR5WWL0RqIl9RUwkvFXzkeflm03u2oy+Nrnp0I8edcqWdD7CdXzjvWlV/wQwHNjZy4iiKfCezYZrq7BwMAJzK7kq2XuaM91HhGUDvYNxA2h27DlcLOv6oSEdh3dJFizbJiDJib5lLnze2pGoxzfbajLP7VQZ7JJFYoGu1P+VRVxo+LzPI/biHp1oMlcFgqXjHpsFKkrXTIM5jy1cdMjfoubvS9329/zCeJiBgBm6l8re025HP5o2Cud06p4jL6J7uoZALFXKVw1GJM1jggX2R28RwmWTUGwCyJY+5Iy/8RCT+iRes0= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ae58bcb-de5c-4027-de5a-08d59db06cb0 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:21:44.6905 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0725 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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=3D1 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 --- 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 934b1fce4ce1..02b576d55758 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 70454f2ad2fa..f046dedc59ab 100644 --- a/drivers/md/bcache/writeback.c +++ b/drivers/md/bcache/writeback.c @@ -420,18 +420,21 @@ static int bch_writeback_thread(void *arg) =20 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); =20 - if (kthread_should_stop()) + if (kthread_should_stop()) { + set_current_state(TASK_RUNNING); return 0; + } =20 schedule(); continue; } + set_current_state(TASK_RUNNING); =20 searched_full_index =3D refill_dirty(dc); =20 --=20 2.15.1