From patchwork Fri Jan 4 09:44:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 154754 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp438560ljp; Fri, 4 Jan 2019 01:44:50 -0800 (PST) X-Google-Smtp-Source: ALg8bN5GiABSRfNBrG04htMMAiTHA6MWTiYRG44FkIZjT77smgGPFETi/o2Pgl/pdgr20wLakDg+ X-Received: by 2002:a63:2849:: with SMTP id o70mr1025703pgo.155.1546595090127; Fri, 04 Jan 2019 01:44:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546595090; cv=none; d=google.com; s=arc-20160816; b=sauPGUF2/boC5GFFxnDmSc7wcnVRiBokCXV6hfYnfwlV3X43gT/NpNONM3X4bvWd05 VEbpSPXpQAnwuhIngMBbwIgt7iGFEp8JWQc+zoUrhfhTVAAOgb+CsnqSUJHvn0WsG2ei kZglvqlgv419ek1hIFn+0l1afo+GL2dOPN4MDVnb+3CFfCp1+YmxGXr9Bxp9UF+ibYb7 n9hkKJr3MS7x1p9W0jags6/jp2qy4zdTX3J4BOGS6hmkE0MLnp+cR+l2IRGGgdGuOEeS bCIjLPPg7hPYHfk0jBiKAOIfGkV774yc7lvJEtZAFqR28ru6r7IYrwgJsFSK9GiXRGE2 iqKQ== 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=1MG9cGxllvxcFxqe5/7vWOoA1Mh9LTZ2vq6D1pcTPoI=; b=pVb6jlYIlI10kQerlq+KpvhcQp5vtMkQrHNqhDdVfDYH9leeVI69ZjvSRWOrlHOfFw hsXfdqcxj2WcS8J3SMTFAifwSPpR/iLJxMqYx2S+21ffN0mEx8Und50pPdPHxfb9PHur hkRmcICbLJFYOFBGlkkRWAn6oBrrKyU5cr+aXe0RncbzkcTsc7QbxCwqXsGFVWpHMqFU 26Fi4rVE3+co/Acd7O7fuM3bIMzgkSLYahTVHrFXaL/ZSRNZw4+5+YpISTKkX4MyW/yW F4lxRcOHQUEYG+sA37TzLAJfzPdy3cJzaBtzvCmmjP1B4+/oJm5KW1I0iDM90bUC1kDt 1ezQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=j5NLY8yF; 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 d25si10526024pgd.88.2019.01.04.01.44.49; Fri, 04 Jan 2019 01:44:50 -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=j5NLY8yF; 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 S1727468AbfADJot (ORCPT + 11 others); Fri, 4 Jan 2019 04:44:49 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:40678 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727022AbfADJos (ORCPT ); Fri, 4 Jan 2019 04:44:48 -0500 Received: by mail-pl1-f193.google.com with SMTP id u18so17210954plq.7 for ; Fri, 04 Jan 2019 01:44:48 -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=1MG9cGxllvxcFxqe5/7vWOoA1Mh9LTZ2vq6D1pcTPoI=; b=j5NLY8yFJNAfhdYLl5QwcLqQeYHDAz0znfBJy5meFTErqfVSUpbpEHcBhyjx+PEWOQ QVtECS/uHnxSMKHGyk60CKDJoEip0ssV5dUVUa4j5xZQbYcmL7+OpcXPL03AGytijW8C qAWY/RAyow6cs3p3W6OmMOFcQA6s2BiZ5jYqg= 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=1MG9cGxllvxcFxqe5/7vWOoA1Mh9LTZ2vq6D1pcTPoI=; b=BBZ0k0Qwc6mziZywuOpFWEcEyCwRcyNjwNZ3LCtOJpqm7ycA0+mt9Qyle0TQH8zR6K hpDP0AXKxM1hOYzROvWoeB9FI7xAiuJczRGRiBYgvtNN1PMJ9MyBTU/FtcpMwj471KqH wA/qdbncuS2J5UTOAdTLlf1YXVYFPiPAj1R2j369jXTXJnY2Srd+3MJ6cnGYS6E5efiT DsyA7SopcdWUQw68Uo1KRweSq9Ts9WCoCONud5ucgfN8oEa//LjnsNrrVtVSIfG+fJRL 2R8E0f0zMVBIOFPTBF3XVEbDyT6OZgR2Qqsew4P/VTzWDIqtkMpUnd0jZJp3P0IEjyGj ZhfA== X-Gm-Message-State: AJcUukfKT4KnZSAcZ4AmaY26lX/VE7TQ8N+Edv8PJScRaRJTj0v6HxEi mVC7rNqaPa5wRpzgZVrLcMjlBA== X-Received: by 2002:a17:902:e290:: with SMTP id cf16mr51346700plb.81.1546595087296; Fri, 04 Jan 2019 01:44:47 -0800 (PST) Received: from localhost ([122.166.131.155]) by smtp.gmail.com with ESMTPSA id u126sm121771528pgb.2.2019.01.04.01.44.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Jan 2019 01:44:46 -0800 (PST) From: Viresh Kumar To: Valentin Schneider , Sudeep Holla , "Rafael J. Wysocki" , Nishanth Menon , Stephen Boyd Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , quentin.perret@arm.com, dietmar.eggemann@arm.com, Douglas.Raillard@arm.com, "4 . 20" , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] cpufreq: scpi/scmi: Fix freeing of dynamic OPPs Date: Fri, 4 Jan 2019 15:14:33 +0530 Message-Id: <7dddbeabb434225d9b3d02600ea4a2313622ca26.1546594910.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.19.1.568.g152ad8e3369a 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 Since the commit 2a4eb7358aba ("OPP: Don't remove dynamic OPPs from _dev_pm_opp_remove_table()"), dynamically created OPP aren't automatically removed anymore by dev_pm_opp_cpumask_remove_table(). This affects the scpi and scmi cpufreq drivers which no longer free OPPs on failures or on invocations of the policy->exit() callback. Create a generic OPP helper dev_pm_opp_remove_all_dynamic() which can be called from these drivers instead of dev_pm_opp_cpumask_remove_table(). In dev_pm_opp_remove_all_dynamic(), we need to make sure that the opp_list isn't getting accessed simultaneously from other parts of the OPP core while the helper is freeing dynamic OPPs, i.e. we can't drop the opp_table->lock while traversing through the OPP list. And to accomplish that, this patch also creates _opp_kref_release_unlocked() which can be called from this new helper with the opp_table lock already held. Cc: 4.20 # v4.20 Reported-by: Valentin Schneider Fixes: 2a4eb7358aba ("OPP: Don't remove dynamic OPPs from _dev_pm_opp_remove_table()") Signed-off-by: Viresh Kumar --- drivers/cpufreq/scmi-cpufreq.c | 4 +-- drivers/cpufreq/scpi-cpufreq.c | 4 +-- drivers/opp/core.c | 63 +++++++++++++++++++++++++++++++--- include/linux/pm_opp.h | 5 +++ 4 files changed, 67 insertions(+), 9 deletions(-) -- 2.19.1.568.g152ad8e3369a diff --git a/drivers/cpufreq/scmi-cpufreq.c b/drivers/cpufreq/scmi-cpufreq.c index 50b1551ba894..c2e66528f5ee 100644 --- a/drivers/cpufreq/scmi-cpufreq.c +++ b/drivers/cpufreq/scmi-cpufreq.c @@ -176,7 +176,7 @@ static int scmi_cpufreq_init(struct cpufreq_policy *policy) out_free_priv: kfree(priv); out_free_opp: - dev_pm_opp_cpumask_remove_table(policy->cpus); + dev_pm_opp_remove_all_dynamic(cpu_dev); return ret; } @@ -188,7 +188,7 @@ static int scmi_cpufreq_exit(struct cpufreq_policy *policy) cpufreq_cooling_unregister(priv->cdev); dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); kfree(priv); - dev_pm_opp_cpumask_remove_table(policy->related_cpus); + dev_pm_opp_remove_all_dynamic(priv->cpu_dev); return 0; } diff --git a/drivers/cpufreq/scpi-cpufreq.c b/drivers/cpufreq/scpi-cpufreq.c index 87a98ec77773..99449738faa4 100644 --- a/drivers/cpufreq/scpi-cpufreq.c +++ b/drivers/cpufreq/scpi-cpufreq.c @@ -177,7 +177,7 @@ static int scpi_cpufreq_init(struct cpufreq_policy *policy) out_free_priv: kfree(priv); out_free_opp: - dev_pm_opp_cpumask_remove_table(policy->cpus); + dev_pm_opp_remove_all_dynamic(cpu_dev); return ret; } @@ -190,7 +190,7 @@ static int scpi_cpufreq_exit(struct cpufreq_policy *policy) clk_put(priv->clk); dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); kfree(priv); - dev_pm_opp_cpumask_remove_table(policy->related_cpus); + dev_pm_opp_remove_all_dynamic(priv->cpu_dev); return 0; } diff --git a/drivers/opp/core.c b/drivers/opp/core.c index e5507add8f04..18f1639dbc4a 100644 --- a/drivers/opp/core.c +++ b/drivers/opp/core.c @@ -988,11 +988,9 @@ void _opp_free(struct dev_pm_opp *opp) kfree(opp); } -static void _opp_kref_release(struct kref *kref) +static void _opp_kref_release(struct dev_pm_opp *opp, + struct opp_table *opp_table) { - struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); - struct opp_table *opp_table = opp->opp_table; - /* * Notify the changes in the availability of the operable * frequency/voltage list. @@ -1002,7 +1000,22 @@ static void _opp_kref_release(struct kref *kref) opp_debug_remove_one(opp); list_del(&opp->node); kfree(opp); +} +static void _opp_kref_release_unlocked(struct kref *kref) +{ + struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); + struct opp_table *opp_table = opp->opp_table; + + _opp_kref_release(opp, opp_table); +} + +static void _opp_kref_release_locked(struct kref *kref) +{ + struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); + struct opp_table *opp_table = opp->opp_table; + + _opp_kref_release(opp, opp_table); mutex_unlock(&opp_table->lock); } @@ -1013,10 +1026,16 @@ void dev_pm_opp_get(struct dev_pm_opp *opp) void dev_pm_opp_put(struct dev_pm_opp *opp) { - kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); + kref_put_mutex(&opp->kref, _opp_kref_release_locked, + &opp->opp_table->lock); } EXPORT_SYMBOL_GPL(dev_pm_opp_put); +static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp) +{ + kref_put(&opp->kref, _opp_kref_release_unlocked); +} + /** * dev_pm_opp_remove() - Remove an OPP from OPP table * @dev: device for which we do this operation @@ -1060,6 +1079,40 @@ void dev_pm_opp_remove(struct device *dev, unsigned long freq) } EXPORT_SYMBOL_GPL(dev_pm_opp_remove); +/** + * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs + * @dev: device for which we do this operation + * + * This function removes all dynamically created OPPs from the opp table. + */ +void dev_pm_opp_remove_all_dynamic(struct device *dev) +{ + struct opp_table *opp_table; + struct dev_pm_opp *opp, *temp; + int count = 0; + + opp_table = _find_opp_table(dev); + if (IS_ERR(opp_table)) + return; + + mutex_lock(&opp_table->lock); + list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) { + if (opp->dynamic) { + dev_pm_opp_put_unlocked(opp); + count++; + } + } + mutex_unlock(&opp_table->lock); + + /* Drop the references taken by dev_pm_opp_add() */ + while (count--) + dev_pm_opp_put_opp_table(opp_table); + + /* Drop the reference taken by _find_opp_table() */ + dev_pm_opp_put_opp_table(opp_table); +} +EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); + struct dev_pm_opp *_opp_allocate(struct opp_table *table) { struct dev_pm_opp *opp; diff --git a/include/linux/pm_opp.h b/include/linux/pm_opp.h index 0a2a88e5a383..b895f4e79868 100644 --- a/include/linux/pm_opp.h +++ b/include/linux/pm_opp.h @@ -108,6 +108,7 @@ void dev_pm_opp_put(struct dev_pm_opp *opp); int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt); void dev_pm_opp_remove(struct device *dev, unsigned long freq); +void dev_pm_opp_remove_all_dynamic(struct device *dev); int dev_pm_opp_enable(struct device *dev, unsigned long freq); @@ -217,6 +218,10 @@ static inline void dev_pm_opp_remove(struct device *dev, unsigned long freq) { } +static inline void dev_pm_opp_remove_all_dynamic(struct device *dev) +{ +} + static inline int dev_pm_opp_enable(struct device *dev, unsigned long freq) { return 0;