From patchwork Fri Jan 2 05:46:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 42667 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 66A9126CB5 for ; Fri, 2 Jan 2015 05:47:42 +0000 (UTC) Received: by mail-wg0-f72.google.com with SMTP id y19sf10326835wgg.11 for ; Thu, 01 Jan 2015 21:47:41 -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=UUk6lniDJ4+JdFzDMI9f/PiauNLEr/u3x3UqWGaxg5o=; b=Zn4IX0jgkXS3K9xEZUmErFRPGFBzOmcgG8aZOE0ETszEbvJlVJrEBlq8WrX8gv8PKy cYR97TgcsQsTwqTw0p6+4dPmHCubq8p1Kl0Kuf+c68i2PenrmkLNJvjfc7YcnhHJVUOH ALnmjq8EJjUCOReyZTfL/2Pdbiz6vOjUZ2GF7t5CoWYqqyIZfSgHmRvMiRnk0gQsM5Dp ZZLjw2seYKDrypAbbu76JibWFylhCkN2Ub7XPfyVpm9cD7vScVoSBQZ1UA6BWJGrZV0U wFfUS2Q4KA6j9TLBE17cP6YNvwKXrqQHq6E6pnlQjugyTWawQlhAB07Sr1Me4+KjI+M4 nPqQ== X-Gm-Message-State: ALoCoQm3b1dv/izFjwX6oPaObxrIpQL37tkIVT4jRPKHAptXg/IxNx1IvtHwAyqiM/Vw00F1+5lq X-Received: by 10.112.160.41 with SMTP id xh9mr4399lbb.12.1420177661658; Thu, 01 Jan 2015 21:47:41 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.207.33 with SMTP id lt1ls2404938lac.64.gmail; Thu, 01 Jan 2015 21:47:41 -0800 (PST) X-Received: by 10.112.47.135 with SMTP id d7mr67455167lbn.54.1420177661252; Thu, 01 Jan 2015 21:47:41 -0800 (PST) Received: from mail-lb0-f174.google.com (mail-lb0-f174.google.com. [209.85.217.174]) by mx.google.com with ESMTPS id yj3si38979179lbb.26.2015.01.01.21.47.41 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 01 Jan 2015 21:47:41 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.174 as permitted sender) client-ip=209.85.217.174; Received: by mail-lb0-f174.google.com with SMTP id 10so14761681lbg.19 for ; Thu, 01 Jan 2015 21:47:41 -0800 (PST) X-Received: by 10.152.5.226 with SMTP id v2mr74585582lav.34.1420177661160; Thu, 01 Jan 2015 21:47:41 -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 c8csp114496lbb; Thu, 1 Jan 2015 21:47:40 -0800 (PST) X-Received: by 10.70.90.209 with SMTP id by17mr120948310pdb.162.1420177658684; Thu, 01 Jan 2015 21:47:38 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3si68097479pdc.177.2015.01.01.21.47.37; Thu, 01 Jan 2015 21:47:38 -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 S1750736AbbABFrg (ORCPT + 12 others); Fri, 2 Jan 2015 00:47:36 -0500 Received: from mail-pa0-f48.google.com ([209.85.220.48]:44466 "EHLO mail-pa0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750716AbbABFrg (ORCPT ); Fri, 2 Jan 2015 00:47:36 -0500 Received: by mail-pa0-f48.google.com with SMTP id rd3so23523646pab.7 for ; Thu, 01 Jan 2015 21:47:36 -0800 (PST) X-Received: by 10.66.102.1 with SMTP id fk1mr39681536pab.16.1420177655980; Thu, 01 Jan 2015 21:47:35 -0800 (PST) Received: from localhost ([122.167.116.151]) by mx.google.com with ESMTPSA id pr3sm44965371pbb.37.2015.01.01.21.47.34 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 01 Jan 2015 21:47:35 -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 V2 09/14] cpufreq: stats: create sysfs group once we are ready Date: Fri, 2 Jan 2015 11:16:46 +0530 Message-Id: <3f561e978f8f0736a2ec4ab5915f1a1869122993.1420177186.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.174 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 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; }