Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp426284imu; Tue, 27 Nov 2018 14:46:10 -0800 (PST) X-Google-Smtp-Source: AFSGD/U8Vj6MKQiY7lHOhjLpRWmGgtFGiQJh1QxLvf+k0CUDhsS2PFIhX/7wF4S7067iPJcuHB50 X-Received: by 2002:a17:902:29a7:: with SMTP id h36mr34708562plb.244.1543358770704; Tue, 27 Nov 2018 14:46:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543358770; cv=none; d=google.com; s=arc-20160816; b=Y+lPNJJmRcp+WoXDRUabzmebTobop7lkTWt5aYW01aW5Bzpjobr4krybMq2IueKIc9 FGgr1jgohAybdz9TLFjNs6K+RDREUvKH1BxwaqKWOnnXCPMDBoiiRF5ORf8Ka6EtY3Yk rN9LkUHk135oI1e9UWc+ANG5l7PDMBuh8UjWVZKtmuOdklOhQNzr3vo3X8AY0si0BRMy dJFCQsIKIV2cqtieh98SFKh3k5pjuILxvnJxeld5g2pLdg88zpnke77VBnCo3tsf1qm4 J61fJLCGw0dAODFuKA/MnIEBcs79TNEeXq/dDko9L/IV+PlKkCl50wq5IBjkAZ07ckdn Rqkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=Cq3ZiD+tGx4j1Y3n8GtCqDnN/1DyE4U5i4dIPs02Xnw=; b=mpu2C8Qy4B6vdXhUXMdRAAdytBviZEgeBiDmojO7esLW2AgyBpFVti+LGE4Mx/ajNv GXQGvdfWLal7oaVFaqCAVtshV8QwWEErkyE5XCcUcPdPapQN87B3kbuYwHClyZGQ6zUw MJ7T/x1dyFHt0MQ+Tx3HEgCUCRGw/kZMvf0CKaHXi0GoIIp6uiaqTb25KruVd+lEtS+F apGZFS/qhJe8NZ0iO1ecD7lpGZLxTSfVQP/RN/y/fe+FkIBg8g0XEd5hKusegeciaHyc wwL1WKhT6I53s8s9pdJCPoV+qT9pfTiKLBvCPVaIDW7J5heQS/i55RdubqBjX4y1e4cF Vb9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YqKBgcXu; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q4si5567234pfq.56.2018.11.27.14.45.54; Tue, 27 Nov 2018 14:46:10 -0800 (PST) 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=@linaro.org header.s=google header.b=YqKBgcXu; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727086AbeK1Jmi (ORCPT + 99 others); Wed, 28 Nov 2018 04:42:38 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:42922 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726299AbeK1Jmi (ORCPT ); Wed, 28 Nov 2018 04:42:38 -0500 Received: by mail-qk1-f196.google.com with SMTP id m5so15643532qka.9 for ; Tue, 27 Nov 2018 14:43:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=Cq3ZiD+tGx4j1Y3n8GtCqDnN/1DyE4U5i4dIPs02Xnw=; b=YqKBgcXuzfqf6mvM/JRx1ZUyRRCtXurID5eUHr6bo74c5ugs2KjfjCiYirZgjMVssA or4TQjBGtQ4LcTlagZXkWWFgIQ28Bjs/PZw25QyGEAuRQobyjmEx8AcWC62VJcFwwovA T7zn7vDE2YhhRrDCYz77COdyyeZjXfbiYszC8= 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; bh=Cq3ZiD+tGx4j1Y3n8GtCqDnN/1DyE4U5i4dIPs02Xnw=; b=KEVWu1Sz8jwNmVct1dk5W29ISAp84uBPwdsxzb1Ka6LbzPCPxH6AVWb79PjuoIfOcb moK1ku5l+xRbwuwu/9EKBWk9/F5frvlxLDFvwmaXB1/TYuqk9wXJGD3qcrzxJZAu3I0o qbEPgXxSh9iiYSc94KA1itn1KOf90hi9mrHA/gYHDXdoGfteWnSUqh0Y1FeQNDvEaBcv NIms3GG/rKJZF3JZRelDYnZt/tiZsmEuffWY+OOSTu22oGNmOn60TBbDvFjF+kfnmEuA FpRx9fBcBFyR8bzXfbQ/gGx3r3CWWRGX/Ki4dC1E6kGnNqwx/slM9yWr6IXae4WJLF9D Io/Q== X-Gm-Message-State: AA+aEWaf4Cu2mXAU1aUDuN+a0T6lVc/tETdUrTwXR8xzuY57lwLyt0RE 2/ROIdwKoy3c7BRgwATyxNQYuQ== X-Received: by 2002:a37:b084:: with SMTP id z126mr30666051qke.154.1543358593549; Tue, 27 Nov 2018 14:43:13 -0800 (PST) Received: from Thara-Work-Ubuntu.fios-router.home (pool-71-255-245-97.washdc.fios.verizon.net. [71.255.245.97]) by smtp.googlemail.com with ESMTPSA id d32sm3226108qte.36.2018.11.27.14.43.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Nov 2018 14:43:12 -0800 (PST) From: Thara Gopinath To: rui.zhang@intel.com, edubezval@gmail.com, daniel.lezcano@linaro.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V2] thermal: Fix locking in cooling device sysfs update cur_state Date: Tue, 27 Nov 2018 17:43:11 -0500 Message-Id: <1543358591-25189-1-git-send-email-thara.gopinath@linaro.org> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sysfs interface to update cooling device cur_state does not currently holding cooling device lock sometimes leading to stale values in cur_state if getting updated simultanelously from user space and thermal framework. Adding the proper locking code fixes this issue. Signed-off-by: Thara Gopinath --- V1->V2: Rearranged the code as per Daniel's review comment drivers/thermal/thermal_sysfs.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 2241cea..aa99edb 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -712,11 +712,14 @@ cur_state_store(struct device *dev, struct device_attribute *attr, if ((long)state < 0) return -EINVAL; + mutex_lock(&cdev->lock); + result = cdev->ops->set_cur_state(cdev, state); - if (result) - return result; - thermal_cooling_device_stats_update(cdev, state); - return count; + if (!result) + thermal_cooling_device_stats_update(cdev, state); + + mutex_unlock(&cdev->lock); + return result ? result : count; } static struct device_attribute -- 2.1.4