From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.7 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72F14C4CECF for ; Mon, 23 Sep 2019 16:27:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4D68A20820 for ; Mon, 23 Sep 2019 16:27:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728457AbfIWQ1c (ORCPT ); Mon, 23 Sep 2019 12:27:32 -0400 Received: from inva021.nxp.com ([92.121.34.21]:33354 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726328AbfIWQ1c (ORCPT ); Mon, 23 Sep 2019 12:27:32 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 3CD8320031D; Mon, 23 Sep 2019 18:27:31 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 2FCE2200177; Mon, 23 Sep 2019 18:27:31 +0200 (CEST) Received: from fsr-ub1864-112.ea.freescale.net (fsr-ub1864-112.ea.freescale.net [10.171.82.98]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id AC72E20634; Mon, 23 Sep 2019 18:27:30 +0200 (CEST) From: Leonard Crestez To: Matthias Kaehlcke , MyungJoo Ham , Kyungmin Park Cc: Chanwoo Choi , =?UTF-8?q?Artur=20=C5=9Awigo=C5=84?= , Krzysztof Kozlowski , Georgi Djakov , Lukasz Luba , NXP Linux Team , linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH] PM / devfreq: Lock devfreq in trans_stat_show Date: Mon, 23 Sep 2019 19:27:27 +0300 Message-Id: <714675448e7fbf3c930b0dca6fbe54fa5f87211b.1569256001.git.leonard.crestez@nxp.com> X-Mailer: git-send-email 2.17.1 X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org There is no locking in this sysfs show function so stats printing can race with a devfreq_update_status called as part of freq switching or with initialization. Also add an assert in devfreq_update_status to make it clear that lock must be held by caller. Signed-off-by: Leonard Crestez --- drivers/devfreq/devfreq.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) Changes since v1: * Split from series: low-priority bugfix not strictly required for PM QoS * Only keep lock during update, release before sprintf Link to v1: https://patchwork.kernel.org/patch/11149493/ diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c index 4c58fbf7d4e4..00fc23fea5b2 100644 --- a/drivers/devfreq/devfreq.c +++ b/drivers/devfreq/devfreq.c @@ -206,10 +206,11 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) { int lev, prev_lev, ret = 0; unsigned long cur_time; cur_time = jiffies; + lockdep_assert_held(&devfreq->lock); /* Immediately exit if previous_freq is not initialized yet. */ if (!devfreq->previous_freq) goto out; @@ -1507,16 +1508,22 @@ static ssize_t trans_stat_show(struct device *dev, struct devfreq *devfreq = to_devfreq(dev); ssize_t len; int i, j; unsigned int max_state = devfreq->profile->max_state; - if (!devfreq->stop_polling && - devfreq_update_status(devfreq, devfreq->previous_freq)) - return 0; if (max_state == 0) return sprintf(buf, "Not Supported.\n"); + /* lock and update */ + mutex_lock(&devfreq->lock); + if (!devfreq->stop_polling && + devfreq_update_status(devfreq, devfreq->previous_freq)) { + mutex_unlock(&devfreq->lock); + return 0; + } + mutex_unlock(&devfreq->lock); + len = sprintf(buf, " From : To\n"); len += sprintf(buf + len, " :"); for (i = 0; i < max_state; i++) len += sprintf(buf + len, "%10lu", devfreq->profile->freq_table[i]); -- 2.17.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DDAF4C4CECF for ; Mon, 23 Sep 2019 16:27:40 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AFCFD20820 for ; Mon, 23 Sep 2019 16:27:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="CjpRKLFz" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AFCFD20820 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nxp.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=6sKJfh4JiGy7JXAfpiw+tZYdjjmslMKikjgmsVHVnxU=; b=Cjp RKLFz7JGbK2q4nUsDpOHo9VabLz81zUAacpIzn2B/n8J0dnz1eGMYprqH51h9Mmlan1yW6c5RpGBJ tYN2brLg0LRU5pCNrCY4uFPVCdPqvtI/VYw46dt7FyEfvCM7eFEObMskaJvguPXmvkxBC9teSPR/N svDi72pRnjJVxETZJGwdYOGflgFW1+TqzaJLUjy+ttjO/L6yqwdI8XoBxZ2tYBPB4RfkSLRa9MSSr 608O2mLbZ5ce/eXwBhwX6vH7ociR9aibtH/AQ0Ewlngd8YO283LuEwjyZbHw3R1dRJbQhN9hbFGuO SgcKGHY/B0WA7yE1Jt9i5/rD56nV6ng==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.2 #3 (Red Hat Linux)) id 1iCRBY-0001xr-9Y; Mon, 23 Sep 2019 16:27:36 +0000 Received: from inva021.nxp.com ([92.121.34.21]) by bombadil.infradead.org with esmtps (Exim 4.92.2 #3 (Red Hat Linux)) id 1iCRBU-0001xQ-Ok for linux-arm-kernel@lists.infradead.org; Mon, 23 Sep 2019 16:27:34 +0000 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 3CD8320031D; Mon, 23 Sep 2019 18:27:31 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 2FCE2200177; Mon, 23 Sep 2019 18:27:31 +0200 (CEST) Received: from fsr-ub1864-112.ea.freescale.net (fsr-ub1864-112.ea.freescale.net [10.171.82.98]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id AC72E20634; Mon, 23 Sep 2019 18:27:30 +0200 (CEST) From: Leonard Crestez To: Matthias Kaehlcke , MyungJoo Ham , Kyungmin Park Subject: [PATCH] PM / devfreq: Lock devfreq in trans_stat_show Date: Mon, 23 Sep 2019 19:27:27 +0300 Message-Id: <714675448e7fbf3c930b0dca6fbe54fa5f87211b.1569256001.git.leonard.crestez@nxp.com> X-Mailer: git-send-email 2.17.1 X-Virus-Scanned: ClamAV using ClamSMTP X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190923_092732_945848_525FB319 X-CRM114-Status: GOOD ( 10.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Artur=20=C5=9Awigo=C5=84?= , linux-pm@vger.kernel.org, Krzysztof Kozlowski , Lukasz Luba , Chanwoo Choi , NXP Linux Team , Georgi Djakov , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org There is no locking in this sysfs show function so stats printing can race with a devfreq_update_status called as part of freq switching or with initialization. Also add an assert in devfreq_update_status to make it clear that lock must be held by caller. Signed-off-by: Leonard Crestez --- drivers/devfreq/devfreq.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) Changes since v1: * Split from series: low-priority bugfix not strictly required for PM QoS * Only keep lock during update, release before sprintf Link to v1: https://patchwork.kernel.org/patch/11149493/ diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c index 4c58fbf7d4e4..00fc23fea5b2 100644 --- a/drivers/devfreq/devfreq.c +++ b/drivers/devfreq/devfreq.c @@ -206,10 +206,11 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) { int lev, prev_lev, ret = 0; unsigned long cur_time; cur_time = jiffies; + lockdep_assert_held(&devfreq->lock); /* Immediately exit if previous_freq is not initialized yet. */ if (!devfreq->previous_freq) goto out; @@ -1507,16 +1508,22 @@ static ssize_t trans_stat_show(struct device *dev, struct devfreq *devfreq = to_devfreq(dev); ssize_t len; int i, j; unsigned int max_state = devfreq->profile->max_state; - if (!devfreq->stop_polling && - devfreq_update_status(devfreq, devfreq->previous_freq)) - return 0; if (max_state == 0) return sprintf(buf, "Not Supported.\n"); + /* lock and update */ + mutex_lock(&devfreq->lock); + if (!devfreq->stop_polling && + devfreq_update_status(devfreq, devfreq->previous_freq)) { + mutex_unlock(&devfreq->lock); + return 0; + } + mutex_unlock(&devfreq->lock); + len = sprintf(buf, " From : To\n"); len += sprintf(buf + len, " :"); for (i = 0; i < max_state; i++) len += sprintf(buf + len, "%10lu", devfreq->profile->freq_table[i]); -- 2.17.1 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel