From patchwork Tue Nov 27 22:43:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thara Gopinath X-Patchwork-Id: 10701557 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EE45414BD for ; Tue, 27 Nov 2018 22:43:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD7952A4EF for ; Tue, 27 Nov 2018 22:43:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF3F12AD34; Tue, 27 Nov 2018 22:43:27 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,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 C48872CA9D for ; Tue, 27 Nov 2018 22:43:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727010AbeK1Jmi (ORCPT ); Wed, 28 Nov 2018 04:42:38 -0500 Received: from mail-qk1-f194.google.com ([209.85.222.194]:44567 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726772AbeK1Jmi (ORCPT ); Wed, 28 Nov 2018 04:42:38 -0500 Received: by mail-qk1-f194.google.com with SMTP id n12so15643296qkh.11 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=RJdbRuj7sfRrPvsTaJ/l/bJ8AkVA6gewhllZHQfu5AEiwIIAakdSrA5j7eO7Ps/7TB ONb93rJFU6eMJBU56ciII0blD9zBMsit/A3m0uo7soNBt7Nh/fBS41CmUq+Kj01FuJUj S1gYLCD6E8or9lAO632yH3ltjEr9FL2MBZ4u3HkhrNq9922yUlSyIQjdrbf4G0iSjGVE 2cSMpJboJodAYxPbcc8UY+DULpdLzJeb7VktRaGjHHGz1j9wVJBGfLiXnJBYhCvaBQNi gr2rLRf2DejBgSaWJ/VNusIvFPrBtEhQsPMyBjAtEWAIxLVCHnNXpM0udakK95GmGrMe hwsw== X-Gm-Message-State: AA+aEWbuM297WVIAlpj97IQzYH2636W8lu0rHS9JznVtb6hiNqBRTlPR 6nzcWTNHnq0F2iZOcn6gajRFhwur2ls= X-Google-Smtp-Source: AFSGD/URovCBwyBBcM3CFyq1E3JQFzPHN5TGA3cOAbRz7dfCXJuDC4vyHdrJVm2fj/8ddmnZjzKIXQ== 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-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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