Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5138177imm; Wed, 12 Sep 2018 01:30:27 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZRTiXjkUF6WN8IM57/VnP+mVQp9hWq5BfHvjU4vmjvaO3wXREWMUK0DAVN1YeqhaFCxLG6 X-Received: by 2002:a63:a441:: with SMTP id c1-v6mr927368pgp.182.1536741027283; Wed, 12 Sep 2018 01:30:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536741027; cv=none; d=google.com; s=arc-20160816; b=xl17WaruZ+IAXblr3uej4F05Dulrc9SK3tYLvG/PEQf1PjQrd2kAXuI/EzqT12GBHB /U0bRb5k/u/tbOyQjn12nw7fgmQQs0OczdMkahiHHrXu3EQ5RQ2tDpRKZMxAB2VX8qQz 2enbYR6PfxCPSpP5aIRxe8plgdwDtKiSFlXJSP2Cln5DfErmmiS9/q5G5aEeFUTljniC Spfv95YVQ5mRYDBGsqBwxjFw+4atvuZH1PvoZ9POl0sna/eORQo+RtNmD/KOh/FF/p0Q SHMlLOt3ShmjbY1bplz1LeiBdlJZ9KNSGDPebbN907Jv1pjJvO/TLvVYx4Pd3zOSQ8av 4wMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=5K1iJfqCM5hIrFK8wsKvsyblTx8byAQ2qMnGR+MYBnc=; b=VmIgCIUh+Y06AJ4RXsGbp2bStE64oUSdOJRv5tweNC4Em9zbTtRO3ztzcMow8fo8Lz yxwDYDiKZVmp17PNLpbY4jV7zdeloLEWHVke83sYXiZoL71ocjn93fJC50wDsUl+yh+a T3n7Ue0XPB8S3TB00TRIyJOSr6rxJqsA2R/YN59yffOsIW7pHh7HI7WSiJceWC80STif McGePyMYPZi0POtHF+J+HCwCgiZVRXc4CtdTcQ2CaDoRO0yGJQbkanta/NzPgSu3b8JR exqNIe9as6new4xzEjeaalwyHvQDieao2g/ROKM06G7MBmh/56DRat+SY7KFDAi+5aUp 50HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WWNP9+16; 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; 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 w10-v6si389275pfk.162.2018.09.12.01.30.12; Wed, 12 Sep 2018 01:30:27 -0700 (PDT) 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=@linaro.org header.s=google header.b=WWNP9+16; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727989AbeILNdV (ORCPT + 99 others); Wed, 12 Sep 2018 09:33:21 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:45483 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727942AbeILNdU (ORCPT ); Wed, 12 Sep 2018 09:33:20 -0400 Received: by mail-pl1-f196.google.com with SMTP id j8-v6so612857pll.12 for ; Wed, 12 Sep 2018 01:29:53 -0700 (PDT) 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; bh=5K1iJfqCM5hIrFK8wsKvsyblTx8byAQ2qMnGR+MYBnc=; b=WWNP9+16Q6w6a9ObSV5yFXy4NZC8+D8pCNCf/EyLKZcePLCPJzMiT7hat05dplXjTU 92cSNpgtI3afDLSDD+H4AHKb7eRMK6I0OMgjoJNRlPb8O/5j1XEjL88ZK77BO1+Xl1yJ pya2C5Gxn5wHXUHiMVM396bZMjVmtV5X1UbIE= 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; bh=5K1iJfqCM5hIrFK8wsKvsyblTx8byAQ2qMnGR+MYBnc=; b=fE0pRqVWFLLRtIlTkyEOrkx1NsG120zywmCsK0YURjP/H14Lc7F4Ji5w5Gv9CLufSc PhGsnW7hA+4hDn97vW25pJTuvrK+zf3tG+/9EBpQ9HRGmq+9Nr4R6yPKWXTdpfN7BUba Cujucs29i/hiYM4rPYAWOswfErl9625/Qo8WfpZPJOsjV8CT5oQKQYEXU790vus1EYSO bCVSudULrRD018kPSpEdb7q7b7SwGXJ8Olof42nP3ntFZOAo5Efg0Itn85zjUQu+NxkO j5+viYdpVi6KGQGkRB1wAaHyIsCjmLAVJ9dm/ccI4vdXO2OP8HJSa38SXtVPXzkJhXK3 JWeQ== X-Gm-Message-State: APzg51CBSYEyGen8mvFjYBlhGjJ0axSNlIfaFwxZztpo4tYb7HPxEjeo 9iXgGKgpMrgIpCD6oRvYB9AN8A== X-Received: by 2002:a17:902:6ac5:: with SMTP id i5-v6mr904729plt.232.1536740993019; Wed, 12 Sep 2018 01:29:53 -0700 (PDT) Received: from localhost ([122.171.64.45]) by smtp.gmail.com with ESMTPSA id i7-v6sm601315pgs.17.2018.09.12.01.29.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Sep 2018 01:29:52 -0700 (PDT) From: Viresh Kumar To: niklas.cassel@linaro.org, Viresh Kumar , Nishanth Menon , Stephen Boyd Cc: Viresh Kumar , Rafael Wysocki , linux-pm@vger.kernel.org, Vincent Guittot , linux-kernel@vger.kernel.org Subject: [PATCH 02/11] OPP: Protect dev_list with opp_table lock Date: Wed, 12 Sep 2018 13:58:41 +0530 Message-Id: <547f728dc5f4d9f27dae0a85517d5b2502b3b03d.1536736872.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.18.0.rc1.242.g61856ae69a2c In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Viresh Kumar --- drivers/opp/core.c | 21 +++++++++++++++++++-- drivers/opp/cpu.c | 2 ++ drivers/opp/opp.h | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/drivers/opp/core.c b/drivers/opp/core.c index 31ff03dbeb83..9f8aa31265fe 100644 --- a/drivers/opp/core.c +++ b/drivers/opp/core.c @@ -48,9 +48,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; @@ -766,6 +771,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 */ @@ -773,6 +780,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; } @@ -791,6 +799,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); @@ -812,7 +821,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 */ @@ -854,6 +862,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); @@ -1716,6 +1728,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 */ @@ -1733,6 +1748,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/opp/cpu.c b/drivers/opp/cpu.c index 0c0910709435..2868a022a040 100644 --- a/drivers/opp/cpu.c +++ b/drivers/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/opp/opp.h b/drivers/opp/opp.h index 7c540fd063b2..e0866b1c1f1b 100644 --- a/drivers/opp/opp.h +++ b/drivers/opp/opp.h @@ -126,7 +126,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. -- 2.18.0.rc1.242.g61856ae69a2c