From patchwork Tue Jan 6 15:39:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 42781 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f199.google.com (mail-lb0-f199.google.com [209.85.217.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 64D772055F for ; Tue, 6 Jan 2015 15:40:20 +0000 (UTC) Received: by mail-lb0-f199.google.com with SMTP id z12sf7373530lbi.6 for ; Tue, 06 Jan 2015 07:40:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:in-reply-to:references :sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=66cRPzmt6VStLf8Ww+65N2pR/NL9cvm3x4hBksxaD4o=; b=iwkgFv+Pq/DxjjqFJSFkA05nGl9hhu1XM0Ng45Q+PKSJ7QMi/KCnjIYrNpCp8/DXjO kD74dq1uDRg/0z80uwnUbHFL6d3iY29xKkPis4iwezLO2jHTPWcWBWM5BLlOFIaC6+Fa HpplugOWvWJ44n8Rubpsfe1iyUpG2hu3cxjwfDqC9xKisESFY7G8l9aiKVnyYMsEix78 hZOHHFTaj2zIVYc+eE1Kdi20g1Rsnaib/RThtm7h2xcJeeITM2fH6D+pBQRZq0TGSTsR SwWrC0vibp+Qd3CdBNIMFCz6FpGvLdzFbhqfRpKKb/mI7tXYZwVSn4ssXFx0foCQwQoz btSA== X-Gm-Message-State: ALoCoQlMAFaRnPvYwafL1l99lGKTpRJgjSGEnWghqxu3jW5ITDlaqQHC9jJcIV+nsDCY7kI8oPgt X-Received: by 10.180.77.167 with SMTP id t7mr2290830wiw.6.1420558819400; Tue, 06 Jan 2015 07:40:19 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.43.41 with SMTP id t9ls54317lal.51.gmail; Tue, 06 Jan 2015 07:40:19 -0800 (PST) X-Received: by 10.152.3.100 with SMTP id b4mr96785331lab.68.1420558819246; Tue, 06 Jan 2015 07:40:19 -0800 (PST) Received: from mail-lb0-f178.google.com (mail-lb0-f178.google.com. [209.85.217.178]) by mx.google.com with ESMTPS id m3si6993535lbc.51.2015.01.06.07.40.19 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 06 Jan 2015 07:40:19 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.178 as permitted sender) client-ip=209.85.217.178; Received: by mail-lb0-f178.google.com with SMTP id u14so1164794lbd.9 for ; Tue, 06 Jan 2015 07:40:19 -0800 (PST) X-Received: by 10.152.27.8 with SMTP id p8mr101686273lag.69.1420558819157; Tue, 06 Jan 2015 07:40:19 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.9.200 with SMTP id c8csp1209659lbb; Tue, 6 Jan 2015 07:40:18 -0800 (PST) X-Received: by 10.68.242.200 with SMTP id ws8mr145775624pbc.138.1420558817098; Tue, 06 Jan 2015 07:40:17 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id hz4si17325227pbb.8.2015.01.06.07.40.16; Tue, 06 Jan 2015 07:40:17 -0800 (PST) Received-SPF: none (google.com: linux-pm-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755470AbbAFPkP (ORCPT + 12 others); Tue, 6 Jan 2015 10:40:15 -0500 Received: from mail-pa0-f44.google.com ([209.85.220.44]:38117 "EHLO mail-pa0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754388AbbAFPkO (ORCPT ); Tue, 6 Jan 2015 10:40:14 -0500 Received: by mail-pa0-f44.google.com with SMTP id et14so31217261pad.31 for ; Tue, 06 Jan 2015 07:40:13 -0800 (PST) X-Received: by 10.70.36.111 with SMTP id p15mr156018899pdj.122.1420558813866; Tue, 06 Jan 2015 07:40:13 -0800 (PST) Received: from localhost ([122.167.212.196]) by mx.google.com with ESMTPSA id wy1sm50286657pbc.5.2015.01.06.07.40.12 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 06 Jan 2015 07:40:13 -0800 (PST) From: Viresh Kumar To: Rafael Wysocki Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, prarit@redhat.com, skannan@codeaurora.org, Viresh Kumar Subject: [PATCH V3 12/16] cpufreq: stats: create sysfs group once we are ready Date: Tue, 6 Jan 2015 21:09:11 +0530 Message-Id: X-Mailer: git-send-email 2.2.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: viresh.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.178 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Userspace is free to read value of any file from cpufreq/stats directory once they are created. __cpufreq_stats_create_table() is creating the sysfs files first and then allocating resources for them. Though it would be quite difficult to trigger the racy situation here, but for the sake of keeping sensible code lets create sysfs entries only after we are ready to go. This also does some makeup to the routine to make it look better. Reviewed-by: Prarit Bhargava Signed-off-by: Viresh Kumar --- drivers/cpufreq/cpufreq_stats.c | 44 +++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/drivers/cpufreq/cpufreq_stats.c b/drivers/cpufreq/cpufreq_stats.c index 1ed703a3e21e..17df8c507594 100644 --- a/drivers/cpufreq/cpufreq_stats.c +++ b/drivers/cpufreq/cpufreq_stats.c @@ -164,12 +164,13 @@ static void cpufreq_stats_free_table(unsigned int cpu) static int __cpufreq_stats_create_table(struct cpufreq_policy *policy) { - unsigned int i, count = 0, ret = 0; + unsigned int i = 0, count = 0, ret = -ENOMEM; struct cpufreq_stats *stats; unsigned int alloc_size; unsigned int cpu = policy->cpu; struct cpufreq_frequency_table *pos, *table; + /* We need cpufreq table for creating stats table */ table = cpufreq_frequency_get_table(cpu); if (unlikely(!table)) return 0; @@ -179,15 +180,10 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy) return -EEXIST; stats = kzalloc(sizeof(*stats), GFP_KERNEL); - if ((stats) == NULL) + if (!stats) return -ENOMEM; - ret = sysfs_create_group(&policy->kobj, &stats_attr_group); - if (ret) - goto error_out; - - policy->stats = stats; - + /* Find total allocation size */ cpufreq_for_each_valid_entry(pos, table) count++; @@ -196,32 +192,42 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy) #ifdef CONFIG_CPU_FREQ_STAT_DETAILS alloc_size += count * count * sizeof(int); #endif - stats->max_state = count; + + /* Allocate memory for time_in_state/freq_table/trans_table in one go */ stats->time_in_state = kzalloc(alloc_size, GFP_KERNEL); - if (!stats->time_in_state) { - ret = -ENOMEM; - goto error_alloc; - } + if (!stats->time_in_state) + goto free_stat; + stats->freq_table = (unsigned int *)(stats->time_in_state + count); #ifdef CONFIG_CPU_FREQ_STAT_DETAILS stats->trans_table = stats->freq_table + count; #endif - i = 0; + + stats->max_state = count; + + /* Find valid-unique entries */ cpufreq_for_each_valid_entry(pos, table) if (freq_table_get_index(stats, pos->frequency) == -1) stats->freq_table[i++] = pos->frequency; stats->state_num = i; + spin_lock(&cpufreq_stats_lock); stats->last_time = get_jiffies_64(); stats->last_index = freq_table_get_index(stats, policy->cur); spin_unlock(&cpufreq_stats_lock); - return 0; -error_alloc: - sysfs_remove_group(&policy->kobj, &stats_attr_group); -error_out: - kfree(stats); + + policy->stats = stats; + ret = sysfs_create_group(&policy->kobj, &stats_attr_group); + if (!ret) + return 0; + + /* We failed, release resources */ policy->stats = NULL; + kfree(stats->time_in_state); +free_stat: + kfree(stats); + return ret; }