From patchwork Thu Dec 18 12:00:26 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 42437 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id D018026C8B for ; Thu, 18 Dec 2014 12:01:44 +0000 (UTC) Received: by mail-wi0-f198.google.com with SMTP id r20sf593607wiv.5 for ; Thu, 18 Dec 2014 04:01:44 -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=zGI2Uh3kqlGU6ZXgwSZz40pZnMgYjCPiapcmlFyDKGg=; b=Z9ar/Uql0XxjI/1yh2UK2BEgBXWgRIZguuEoxQJJpIEsGu3iD1e5PCd87c/rA6ElZG ZI5qnJcfKGP3HWzZ0WwheHKRHsIMo1ohmAlXqnbjsvyYDnktqkt7+SkS8kaTk+2+l7O3 OGmkRe50oxZFY8MsuzGFIK2MxolbKmHP5+SgD24/DK9sdEuxnDHllKJWx5yLLD0gjJzk wKQWfSUDUZs1XI82CP+FlYW0He+CWIKsrV8rU2PQeyt3bGkvDY4M5Xl9wfFK1wY+DCsQ 2BoccjAJRxvof8vdLPybDRljjPJue+hsJJ4Kg0ygZ7a6h+RX6d484wlC/cHwe0E+ax4/ mWgQ== X-Gm-Message-State: ALoCoQlAVB7//DCE7WA6g89FIa1IgyZDUTgfsgBkEjU0o6zYWoXU6V4QqGAJ0QsBTrti84coiemK X-Received: by 10.152.2.40 with SMTP id 8mr227959lar.7.1418904103560; Thu, 18 Dec 2014 04:01:43 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.42.170 with SMTP id p10ls1227362lal.35.gmail; Thu, 18 Dec 2014 04:01:43 -0800 (PST) X-Received: by 10.112.136.38 with SMTP id px6mr1868170lbb.88.1418904103147; Thu, 18 Dec 2014 04:01:43 -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 sj12si6722153lac.1.2014.12.18.04.01.42 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 18 Dec 2014 04:01:42 -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 f15so902481lbj.23 for ; Thu, 18 Dec 2014 04:01:42 -0800 (PST) X-Received: by 10.112.135.197 with SMTP id pu5mr1891509lbb.22.1418904102870; Thu, 18 Dec 2014 04:01:42 -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.142.69 with SMTP id ru5csp1680539lbb; Thu, 18 Dec 2014 04:01:40 -0800 (PST) X-Received: by 10.70.36.111 with SMTP id p15mr2770117pdj.122.1418904099650; Thu, 18 Dec 2014 04:01:39 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ov3si9608661pbc.228.2014.12.18.04.01.38; Thu, 18 Dec 2014 04:01:39 -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 S1752468AbaLRMBi (ORCPT + 12 others); Thu, 18 Dec 2014 07:01:38 -0500 Received: from mail-pa0-f54.google.com ([209.85.220.54]:65034 "EHLO mail-pa0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752450AbaLRMBh (ORCPT ); Thu, 18 Dec 2014 07:01:37 -0500 Received: by mail-pa0-f54.google.com with SMTP id fb1so1291268pad.13 for ; Thu, 18 Dec 2014 04:01:37 -0800 (PST) X-Received: by 10.67.24.66 with SMTP id ig2mr2662916pad.87.1418904097098; Thu, 18 Dec 2014 04:01:37 -0800 (PST) Received: from localhost ([122.166.148.73]) by mx.google.com with ESMTPSA id xq16sm6688513pac.31.2014.12.18.04.01.35 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 18 Dec 2014 04:01:36 -0800 (PST) From: Viresh Kumar To: Rafael Wysocki Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, Viresh Kumar Subject: [PATCH 09/13] cpufreq: stats: create sysfs group once we are ready Date: Thu, 18 Dec 2014 17:30:26 +0530 Message-Id: <6a60b60e1ca803c34ee32bd63349bbc5a799feaf.1418902789.git.viresh.kumar@linaro.org> 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. 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 f458fdbc5bfd..dd55d57655ce 100644 --- a/drivers/cpufreq/cpufreq_stats.c +++ b/drivers/cpufreq/cpufreq_stats.c @@ -171,12 +171,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 *stat; 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; @@ -186,15 +187,10 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy) return -EEXIST; stat = kzalloc(sizeof(*stat), GFP_KERNEL); - if ((stat) == NULL) + if (!stat) return -ENOMEM; - ret = sysfs_create_group(&policy->kobj, &stats_attr_group); - if (ret) - goto error_out; - - policy->stats_data = stat; - + /* Find total allocation size */ cpufreq_for_each_valid_entry(pos, table) count++; @@ -203,32 +199,42 @@ static int __cpufreq_stats_create_table(struct cpufreq_policy *policy) #ifdef CONFIG_CPU_FREQ_STAT_DETAILS alloc_size += count * count * sizeof(int); #endif - stat->max_state = count; + + /* Allocate memory for time_in_state/freq_table/trans_table in one go */ stat->time_in_state = kzalloc(alloc_size, GFP_KERNEL); - if (!stat->time_in_state) { - ret = -ENOMEM; - goto error_alloc; - } + if (!stat->time_in_state) + goto free_stat; + stat->freq_table = (unsigned int *)(stat->time_in_state + count); #ifdef CONFIG_CPU_FREQ_STAT_DETAILS stat->trans_table = stat->freq_table + count; #endif - i = 0; + + stat->max_state = count; + + /* Find valid-unique entries */ cpufreq_for_each_valid_entry(pos, table) if (freq_table_get_index(stat, pos->frequency) == -1) stat->freq_table[i++] = pos->frequency; stat->state_num = i; + spin_lock(&cpufreq_stats_lock); stat->last_time = get_jiffies_64(); stat->last_index = freq_table_get_index(stat, policy->cur); spin_unlock(&cpufreq_stats_lock); - return 0; -error_alloc: - sysfs_remove_group(&policy->kobj, &stats_attr_group); -error_out: - kfree(stat); + + policy->stats_data = stat; + ret = sysfs_create_group(&policy->kobj, &stats_attr_group); + if (!ret) + return 0; + + /* We failed, release resources */ policy->stats_data = NULL; + kfree(stat->time_in_state); +free_stat: + kfree(stat); + return ret; }