From patchwork Fri Feb 1 06:15:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 157187 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp11629jaa; Thu, 31 Jan 2019 22:15:59 -0800 (PST) X-Google-Smtp-Source: ALg8bN739kBQsEbwpUJU05TwBTS8vTMhnEbbz7qq+FL4IPcivNH4y6+33OY3FEC7uKk7wohK4Zhl X-Received: by 2002:a17:902:b40d:: with SMTP id x13mr38681673plr.237.1549001759106; Thu, 31 Jan 2019 22:15:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549001759; cv=none; d=google.com; s=arc-20160816; b=tHfv1N63sDrNQNzZ5sfXq4mOS5pbpYIL3Jk0TicqBo2WR/+zGfbJdnsHpYrqmHo5qm GVfRzYjDpveoL0LfJvwaBSaB3PkEf3V+w7GreSKV0urBpezSIovFrYg/TOFHro8Q0b95 MJWc4Z+QJEMIqmaO11E/1SmG2bSEMgtJMxOl9D0jJ5cbH3LNIEogabx8KxbrDN5mUcKm X71gOY5GFFH6jgHcrZWKSlfi858IrAZYuyQur6QewBIzZElGOeQUUwDIbsAjIbo19wn0 GwUWN4isBUG2y6/+q1d0hUTmXU2qaCYcWr8gfZYRbxcw69KJybNifGbafOWg9KX0FsH/ iCMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=5LRFuPEyKq3EzFrX7KUyuTOgSVNw77Yo4U+tkF/rdBw=; b=SZ/e67vmbEhulGOCguQ3kEjLORIC696vy6CYpI+ExY8XW867OTzUx4aH4lzPl5OUXB kpt8Ya43B0xoVhKyVBv81OqlW1jTK4ZVZ9BjPqUS9qVBcJq+ta64GpvIgKpuPT+MwjWV Oopfqks0SwFgLed1Y7fX9fLCmR7MbkP8CvOAYiT3OTMtEwJygUjwGuz//uZ9Vd1fXbA8 D65iguawCNeT68CNgIOCWYXVKxqgMoMwYAcdIKPi6i1Ca1n0vqYomB6Sjy7ToKMFt/U5 oTXCUUMh8WcNc4PcLEmUj/vxcHiaO3+jHkpnWYqAi0fOudJO7UIjInuKTOyTv7NDVHEP s/ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NVS0O+3n; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-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 k38si6110644pgi.235.2019.01.31.22.15.57; Thu, 31 Jan 2019 22:15:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-pm-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=NVS0O+3n; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-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 S1726470AbfBAGP5 (ORCPT + 11 others); Fri, 1 Feb 2019 01:15:57 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:32969 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726421AbfBAGP5 (ORCPT ); Fri, 1 Feb 2019 01:15:57 -0500 Received: by mail-pl1-f196.google.com with SMTP id z23so2680603plo.0 for ; Thu, 31 Jan 2019 22:15:56 -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:mime-version :content-transfer-encoding; bh=5LRFuPEyKq3EzFrX7KUyuTOgSVNw77Yo4U+tkF/rdBw=; b=NVS0O+3nfN4m7YXiUDPq9aDSUFNnYrUgZIaweslK2RonDnychwZ3k1Nh+XWdjXlndB l/LKV3UEP5UPu00vLzsL1S6BWNoITlJSZ//LarAmFduzZT3GwbyMvZzjz47A3OLTtRFb rQ+bMGyxbsNhY9530ya1635UeN3O8c3owBnKg= 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:mime-version :content-transfer-encoding; bh=5LRFuPEyKq3EzFrX7KUyuTOgSVNw77Yo4U+tkF/rdBw=; b=DAW741SfNeFCinJxs1DIaqr5WGNxBeZQvNAWsZ0c7QWRNvOGbYTl+OSlWObZ0CaPM3 S3IekyZBckIxwHaMXI4XL8a6ObpQEtqQHJfq7cbGcbSLa4rV3IAQtHIgFCi5d6md12u8 MnWrC8aozj0P0Jev1Uxqbk2xKGBxb1E7Il8cUXJBnCWeb8ioiyWp9mvEZtR/EHC83Y7i MPBMHAxH/hBCNsYpfItvo1Ln/Uu9SEC93T79HeyhdtaaPqx05jMW3/Gj3+HhE+35i0mN Z+3RIDj3dTQtZAM1N0Koc3xQ9CICzaiFSUFqw5NJuF8ud2XPQ51bNFzcJOJWRzcMVOJk Pqsw== X-Gm-Message-State: AJcUukcZzoyPAkTStOo1lCGFAZb2YmBH3LSrLut8z8xNGIwQnl2CrHGT 7QzOj5D3QKjCg4B7NUzP+S/Z8A== X-Received: by 2002:a17:902:a60f:: with SMTP id u15mr36242602plq.275.1549001756370; Thu, 31 Jan 2019 22:15:56 -0800 (PST) Received: from localhost ([122.172.102.63]) by smtp.gmail.com with ESMTPSA id f67sm9337844pfc.141.2019.01.31.22.15.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 31 Jan 2019 22:15:54 -0800 (PST) From: Viresh Kumar To: Rafael Wysocki Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] cpufreq: stats: Declare freq-attr right after their callbacks Date: Fri, 1 Feb 2019 11:45:44 +0530 Message-Id: X-Mailer: git-send-email 2.20.1.321.g9e740568ce00 MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Freq attribute for "trans_table" is defined right after its callback (without any blank line between them), but the others are defined separately later on. Keep this consistent and define all attributes right after their callbacks. Signed-off-by: Viresh Kumar --- drivers/cpufreq/cpufreq_stats.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) -- 2.20.1.321.g9e740568ce00 diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c index 1572129844a5..941e63e3e652 100644 --- a/drivers/cpufreq/cpufreq_stats.c +++ b/drivers/cpufreq/cpufreq_stats.c @@ -51,6 +51,7 @@ static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf) { return sprintf(buf, "%d\n", policy->stats->total_trans); } +cpufreq_freq_attr_ro(total_trans); static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf) { @@ -69,6 +70,7 @@ static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf) } return len; } +cpufreq_freq_attr_ro(time_in_state); static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf, size_t count) @@ -77,6 +79,7 @@ static ssize_t store_reset(struct cpufreq_policy *policy, const char *buf, cpufreq_stats_clear_table(policy->stats); return count; } +cpufreq_freq_attr_wo(reset); static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) { @@ -126,10 +129,6 @@ static ssize_t show_trans_table(struct cpufreq_policy *policy, char *buf) } cpufreq_freq_attr_ro(trans_table); -cpufreq_freq_attr_ro(total_trans); -cpufreq_freq_attr_ro(time_in_state); -cpufreq_freq_attr_wo(reset); - static struct attribute *default_attrs[] = { &total_trans.attr, &time_in_state.attr, From patchwork Fri Feb 1 06:15:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 157188 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp11721jaa; Thu, 31 Jan 2019 22:16:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN5Og6h7qJfSHA5QzHvzj2k7C9G95DlR1sl0peqlRuh/tbWL8WmnAnUZW8uAmVia46gnYIgE X-Received: by 2002:a62:d448:: with SMTP id u8mr37851337pfl.105.1549001766771; Thu, 31 Jan 2019 22:16:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549001766; cv=none; d=google.com; s=arc-20160816; b=byWmSLojbTTjavwbFOdqo2+k46LzLFoild1SMD9zHhATlHZWkvbt4oR7vMvkGa6hUy 5dyAo5DqB6f9nlw9Mty8YpiDqtbyklpvLv5Hul242jeSQe+aXQmm4ND9zwjos0LY1PKz gjLqW02aND3xgAfIpBT609nLR4Z9SDmdQSOaki/xOTNkv0QHris8l8MPqz8dol6PNcmE h+y2Ddume6NVuEj0nik98GwAeD0sDOg4NSUydG9Jwq9GcCgLIgDT4SuSfUPzIvl7gGJv +YjOeOwyxDz62ablpe1dstN/d682rnfh8udlbjGzmUQ0SgE8QvoEdd5cgsRrKp2L0sz5 vQyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OWAwfc5wYY5PtG8pBPOfVRPrY8t3iMD9QATzlWF/GMo=; b=Fr0QewIJ4eO7+rn58vfHpCd9xbQnMvoABtTvnBXG/XzILU2KotfH+dOSnmye/hWl8R /uUw5vN+YmPPHO4RCTY56/0W7V2R0qQkwA0Q04Jst5ZN6fvl9R5oJ5Gj9N/bMOtHOpiU 7nkJ1XLWNkQXVX99b/G+3G0bOjJHyeVKK8a4bHKecYa/G+dLJcsMfn8U0O+n+mtnbASN Qbg5cQRJLF88kndndvp5RVN6TkfAnCxl7ZC6EU/1ZidjDgOHU5zq3LfIIDnyMswF2BKK 7LvVK7nPptQdy3Wom9Wf3Kd8bqrsGdtOxzT9iwzAIczUjNLQqGihh4P/Z2U6L0qOwOq6 f9Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bYqqeHxu; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-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 p4si6397827pgm.342.2019.01.31.22.16.06; Thu, 31 Jan 2019 22:16:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-pm-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=bYqqeHxu; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-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 S1726659AbfBAGQA (ORCPT + 11 others); Fri, 1 Feb 2019 01:16:00 -0500 Received: from mail-pl1-f194.google.com ([209.85.214.194]:43609 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726467AbfBAGQA (ORCPT ); Fri, 1 Feb 2019 01:16:00 -0500 Received: by mail-pl1-f194.google.com with SMTP id gn14so2655690plb.10 for ; Thu, 31 Jan 2019 22:16:00 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=OWAwfc5wYY5PtG8pBPOfVRPrY8t3iMD9QATzlWF/GMo=; b=bYqqeHxuhWXUZATJfWLOrNfmFCQW9gujoTXIX3aKUkbBC8WV6YN4mlX4cZhFQdpzCf g/i7uoJlUX/RLzMm8/vxCsf0aX8RzFAHLjIeQ2GP0Q76iXL8vgmgbFPwALiCHMTBWcw8 TIPdjkwdI8oG6Gg67BMLF7X+2Qkv6AY/gHjQM= 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:mime-version:content-transfer-encoding; bh=OWAwfc5wYY5PtG8pBPOfVRPrY8t3iMD9QATzlWF/GMo=; b=L8EQgTj8IuWSi5KkNZNyAdRl7VY27X3VMh5wB/Sx2hO38+jZd1BtbddsYKYcoXNc6C IipAe04u+SRns8QyIUkxgWfXW0wuUsen0UvmZwiu/7kz6Hbt2BWAJOngRuWP/5gRWAP5 S6IPDnTS8vEUyeeac4uBzlrxf2svTzdu8sRp1hNynQjw6Vlx6DhU/QI/kd9+F4XGD70M qK+RsSvofW7udhl1QaCs5eGnk3BFAIu82aHKUZ18C56qsN7nJBmTEBXCkoxnWLBNGf9f 71G6TXN1+9QJjIV6wLXO2fwHEM+vXFAOVmUW2MPkdEle6ci4SIrEyVJldmA8wckotjXH yeCA== X-Gm-Message-State: AJcUukeZZqKT7XNWLiGQv2+upFHfxrOP5vdY3aNYuWwggR4sTZRTPe44 v8B8nno/u6sPPNCC1nh3j/9Ykw== X-Received: by 2002:a17:902:7791:: with SMTP id o17mr37608663pll.60.1549001759710; Thu, 31 Jan 2019 22:15:59 -0800 (PST) Received: from localhost ([122.172.102.63]) by smtp.gmail.com with ESMTPSA id o4sm10080341pgs.12.2019.01.31.22.15.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 31 Jan 2019 22:15:58 -0800 (PST) From: Viresh Kumar To: Rafael Wysocki Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] cpufreq: stats: Fix concurrency issues while resetting stats Date: Fri, 1 Feb 2019 11:45:45 +0530 Message-Id: <927dcc1de6acad3dd2cfae4e300e6fee4f665101.1549001732.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.20.1.321.g9e740568ce00 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org It is possible for cpufreq_stats_clear_table() and cpufreq_stats_record_transition() to get called concurrently and they will try to update same variables simultaneously and may lead to corruption of data. Prevent that with the help of existing spinlock. Signed-off-by: Viresh Kumar --- drivers/cpufreq/cpufreq_stats.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) -- 2.20.1.321.g9e740568ce00 Reviewed-by: Matthias Kaehlcke diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c index 941e63e3e652..e2db5581489a 100644 --- a/drivers/cpufreq/cpufreq_stats.c +++ b/drivers/cpufreq/cpufreq_stats.c @@ -31,20 +31,20 @@ static void cpufreq_stats_update(struct cpufreq_stats *stats) { unsigned long long cur_time = get_jiffies_64(); - spin_lock(&cpufreq_stats_lock); stats->time_in_state[stats->last_index] += cur_time - stats->last_time; stats->last_time = cur_time; - spin_unlock(&cpufreq_stats_lock); } static void cpufreq_stats_clear_table(struct cpufreq_stats *stats) { unsigned int count = stats->max_state; + spin_lock(&cpufreq_stats_lock); memset(stats->time_in_state, 0, count * sizeof(u64)); memset(stats->trans_table, 0, count * count * sizeof(int)); stats->last_time = get_jiffies_64(); stats->total_trans = 0; + spin_unlock(&cpufreq_stats_lock); } static ssize_t show_total_trans(struct cpufreq_policy *policy, char *buf) @@ -62,7 +62,10 @@ static ssize_t show_time_in_state(struct cpufreq_policy *policy, char *buf) if (policy->fast_switch_enabled) return 0; + spin_lock(&cpufreq_stats_lock); cpufreq_stats_update(stats); + spin_unlock(&cpufreq_stats_lock); + for (i = 0; i < stats->state_num; i++) { len += sprintf(buf + len, "%u %llu\n", stats->freq_table[i], (unsigned long long) @@ -239,9 +242,11 @@ void cpufreq_stats_record_transition(struct cpufreq_policy *policy, if (old_index == -1 || new_index == -1 || old_index == new_index) return; + spin_lock(&cpufreq_stats_lock); cpufreq_stats_update(stats); stats->last_index = new_index; stats->trans_table[old_index * stats->max_state + new_index]++; stats->total_trans++; + spin_unlock(&cpufreq_stats_lock); }