From patchwork Tue Nov 19 05:19:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 179698 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp169373ilf; Mon, 18 Nov 2019 21:50:31 -0800 (PST) X-Google-Smtp-Source: APXvYqxwbnPaGETZZoGRFhEAVIhEpG6MNSdfC+9J4Q/E0lhbiCGWcu+3A4pxnmN1M8nRLeF18RNX X-Received: by 2002:a17:906:2e81:: with SMTP id o1mr32189254eji.284.1574142630121; Mon, 18 Nov 2019 21:50:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574142630; cv=none; d=google.com; s=arc-20160816; b=bqPSMqW3k8AqHKAOOXQKLZF1Zz3gjY3bakf1lvDLTyY50o9TbxsBBts5/X08rj9zZ0 UsOG4CUe67522PxtsZG1PUuB0zQTUp9zFW/6OTY9x1nlfSRCJiEc7Vm6bKGkB5ksuqKF aMMuNldkgeFP/aZsmjlh3SaG6K5uzjsLcsR7lm/U5YyZWzJiWs4wHJxEbBA5JGIoyFHo usc1Rl09MxVkmC4A4a/DV6w0+5TsyRxPvdJTo7hvyumUhMYwc+P+JYYQcrqI7Ilo30S0 bw6C6PhrN+VD2MKWccfBeKG/moxIMxYrP/MTKb0XhV18y+KVbze3KGn8Cbc41z/hOVRF SpnQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=O20zWLghK5fBjdBX9/BDdLUg9iReA+4X5nlUhZ2Y1Ss=; b=GDKoG2lVlLKqY0M0vVx/NxU0tYhierTsnXmMxf5nbqJ3IoXtavTCIUBF019byYEw9n LJbB2e3KPeK7SdrBDBtuTu/3UUfurCYehNmB2I5dWJ5Ykxh5abHHXXBLosoIagZgSGZr erXhUWz/lihZD069cuY9QsIrofIZqdXkxhJTOrgmpodRZjSplksiF9M28CESucIvZRdy w5klzwRIW85cHTDnQDl+0LkL20m1Oydtt3kppn2JSHDBKGDTJsaGboJeTYIp/I8+dP3E hmhwjzDVRcmXo0066wnDoX95wjX1KVUDInytTYyORj0ZO6PwcTHncrOOt8IH8LGOYG7/ lEAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vLCMPMXs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gu7si13277786ejb.405.2019.11.18.21.50.29; Mon, 18 Nov 2019 21:50:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=@kernel.org header.s=default header.b=vLCMPMXs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731629AbfKSFu2 (ORCPT + 26 others); Tue, 19 Nov 2019 00:50:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:47450 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731611AbfKSFuY (ORCPT ); Tue, 19 Nov 2019 00:50:24 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4782B21783; Tue, 19 Nov 2019 05:50:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574142624; bh=eCG85q6t7YJoJKlRi9ND0119j3qWg7j8OP8APe9k2Zg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vLCMPMXskpmsffM4DNq/HbWmvPZLoOv8AMtu3EzHTjBvG4ZIIOHciL6e53nqdC9xd EqDarZEiPZc7/geayIrjJbPrcILj2MpjL6Z/cEqDHCMVFurmSWdEcrJkhX9bIC1rzX so7S3NAmVwEaz2p8sOjvvPMQ/M7T63weHwWrW+fs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Niklas Cassel , Viresh Kumar , Sasha Levin Subject: [PATCH 4.14 144/239] OPP: Protect dev_list with opp_table lock Date: Tue, 19 Nov 2019 06:19:04 +0100 Message-Id: <20191119051332.034700923@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191119051255.850204959@linuxfoundation.org> References: <20191119051255.850204959@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Viresh Kumar [ Upstream commit 3d2556992a878a2210d3be498416aee39e0c32aa ] The dev_list needs to be protected with a lock, else we may have simultaneous access (addition/removal) to it and that would be racy. Extend scope of the opp_table lock to protect dev_list as well. Tested-by: Niklas Cassel Signed-off-by: Viresh Kumar Signed-off-by: Sasha Levin --- drivers/base/power/opp/core.c | 21 +++++++++++++++++++-- drivers/base/power/opp/cpu.c | 2 ++ drivers/base/power/opp/opp.h | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) -- 2.20.1 diff --git a/drivers/base/power/opp/core.c b/drivers/base/power/opp/core.c index d5e7e8cc4f221..8100c87691497 100644 --- a/drivers/base/power/opp/core.c +++ b/drivers/base/power/opp/core.c @@ -49,9 +49,14 @@ static struct opp_device *_find_opp_dev(const struct device *dev, static struct opp_table *_find_opp_table_unlocked(struct device *dev) { struct opp_table *opp_table; + bool found; list_for_each_entry(opp_table, &opp_tables, node) { - if (_find_opp_dev(dev, opp_table)) { + mutex_lock(&opp_table->lock); + found = !!_find_opp_dev(dev, opp_table); + mutex_unlock(&opp_table->lock); + + if (found) { _get_opp_table_kref(opp_table); return opp_table; @@ -711,6 +716,8 @@ struct opp_device *_add_opp_dev(const struct device *dev, /* Initialize opp-dev */ opp_dev->dev = dev; + + mutex_lock(&opp_table->lock); list_add(&opp_dev->node, &opp_table->dev_list); /* Create debugfs entries for the opp_table */ @@ -718,6 +725,7 @@ struct opp_device *_add_opp_dev(const struct device *dev, if (ret) dev_err(dev, "%s: Failed to register opp debugfs (%d)\n", __func__, ret); + mutex_unlock(&opp_table->lock); return opp_dev; } @@ -736,6 +744,7 @@ static struct opp_table *_allocate_opp_table(struct device *dev) if (!opp_table) return NULL; + mutex_init(&opp_table->lock); INIT_LIST_HEAD(&opp_table->dev_list); opp_dev = _add_opp_dev(dev, opp_table); @@ -757,7 +766,6 @@ static struct opp_table *_allocate_opp_table(struct device *dev) BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); INIT_LIST_HEAD(&opp_table->opp_list); - mutex_init(&opp_table->lock); kref_init(&opp_table->kref); /* Secure the device table modification */ @@ -799,6 +807,10 @@ static void _opp_table_kref_release(struct kref *kref) if (!IS_ERR(opp_table->clk)) clk_put(opp_table->clk); + /* + * No need to take opp_table->lock here as we are guaranteed that no + * references to the OPP table are taken at this point. + */ opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device, node); @@ -1702,6 +1714,9 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, { struct dev_pm_opp *opp, *tmp; + /* Protect dev_list */ + mutex_lock(&opp_table->lock); + /* Find if opp_table manages a single device */ if (list_is_singular(&opp_table->dev_list)) { /* Free static OPPs */ @@ -1712,6 +1727,8 @@ void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, } else { _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table); } + + mutex_unlock(&opp_table->lock); } void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all) diff --git a/drivers/base/power/opp/cpu.c b/drivers/base/power/opp/cpu.c index 2d87bc1adf38b..66e406bd4d628 100644 --- a/drivers/base/power/opp/cpu.c +++ b/drivers/base/power/opp/cpu.c @@ -222,8 +222,10 @@ int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) cpumask_clear(cpumask); if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { + mutex_lock(&opp_table->lock); list_for_each_entry(opp_dev, &opp_table->dev_list, node) cpumask_set_cpu(opp_dev->dev->id, cpumask); + mutex_unlock(&opp_table->lock); } else { cpumask_set_cpu(cpu_dev->id, cpumask); } diff --git a/drivers/base/power/opp/opp.h b/drivers/base/power/opp/opp.h index 166eef9905995..0a206c6b90868 100644 --- a/drivers/base/power/opp/opp.h +++ b/drivers/base/power/opp/opp.h @@ -124,7 +124,7 @@ enum opp_table_access { * @dev_list: list of devices that share these OPPs * @opp_list: table of opps * @kref: for reference count of the table. - * @lock: mutex protecting the opp_list. + * @lock: mutex protecting the opp_list and dev_list. * @np: struct device_node pointer for opp's DT node. * @clock_latency_ns_max: Max clock latency in nanoseconds. * @shared_opp: OPP is shared between multiple devices.