From patchwork Wed Sep 19 22:20:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 147060 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp1233684ljw; Wed, 19 Sep 2018 15:21:40 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdau5yueplUwcYkNCKDiSlBJ3oXTL7dXE/gTHLQwIjEOsLAG6pIn/sV3RRbUkiCUvM1FASGd X-Received: by 2002:a63:fe49:: with SMTP id x9-v6mr4091492pgj.152.1537395700856; Wed, 19 Sep 2018 15:21:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537395700; cv=none; d=google.com; s=arc-20160816; b=AKOYeNNuIoOYqBuOdqtfwdtjw13M5ztGwx7QyQOBFMSyj2gmvbRgk8kTLYGP78wxx+ x/sD+pWD8nMlYrWL5Hl26lfbFgXA8UPS3Iu2iAeQSPgNsj1Z2TrtZuQNI577vnFcoPf9 JNmR7JLVxBPOcS7j4mU0A/2Yklytt18XdRgMIB+rsqsWv/hQXOMhiMLhEmJi9hAI6Kes uaaZsYQxQoW9Ko8VyrumE9FnuLsquUvEkotpVTDmm9nDXvNe/BRAXEZzqTzDE2jcPuVA xem4DnFbwokKy617ZNKuzB1TVGz4KIB4FCVjFQPBI+6uIo3TnGsb6yv2ONGIYnv/jdxR PmYg== 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:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=KNbb/GBRT5l235B0JCuUrzsEESNrHyS5fTToF34GITM=; b=dm8O4OdsVLJvwmhN7Z+u5/xgJDjoKMPxjgZgjGLAUxzki3DZgSl2hHLGBU5m4gcD9R t6/k2a9EMSVxvYvzxLDREg5Xtp2hXP1WNFiXPNiHX6vw8gsV9KJ5HlOCT7XYf2f+YbbS l1j7w01cyvOhhtD03Dudw4JpsjZEklKesiXnuwPp/MHe6LiyRQZdbMYF2NTo6UdrVVF3 RJtutnWdbpLmH2K+j7lU5OLgNI87epXX5NHs+Pz06f7MeC4/FkSQjotrhbuq+zgrRCJL cJUgGargpWDZI6bafweP/OQd7FiWo1eW1JKhZZd/7wsOCLxDHTMtKG9ZieSMTI0YzB3G RGYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kb0LyyBA; 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 a24-v6si17927966pgi.515.2018.09.19.15.21.40; Wed, 19 Sep 2018 15:21:40 -0700 (PDT) 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=kb0LyyBA; 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 S1725993AbeITEAs (ORCPT + 10 others); Thu, 20 Sep 2018 00:00:48 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:34907 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726592AbeITEAs (ORCPT ); Thu, 20 Sep 2018 00:00:48 -0400 Received: by mail-pl1-f195.google.com with SMTP id g2-v6so3307027plo.2 for ; Wed, 19 Sep 2018 15:20:47 -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 :in-reply-to:references; bh=KNbb/GBRT5l235B0JCuUrzsEESNrHyS5fTToF34GITM=; b=kb0LyyBAoMOdq8BVQnvoGN9iZbuEZTsvI+BGU1C2Nbd4tVxnR4nkW+xpV48FIidvwW V9fAIAsXEWiLB5gKDmOybT339oXK8ne+05ELmx7mTffsLEes7801R35UJB+176ZVj9r7 bQJrjdt8aL4wZOPVcT0HKAIvA0ueZm7rAT3fs= 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:in-reply-to:references; bh=KNbb/GBRT5l235B0JCuUrzsEESNrHyS5fTToF34GITM=; b=ZXRxAMgNFJG/kQvd/nFBOZW/Ju6pAtfLjyHX/SYs21eU4x9kHBFuoIAlqQC20EZAvN klCI41t+ckXzYr0ZWpeNeiLj18BhmA5kvnC1kpWQoARNd1xsO8NFs2e3nA7Y0fUyLDP6 vA/eGFb6tlwQsB1piZt/0HNqKI4jwqmAJLRqi6EBXYrQfb43wdRB0hWo1H81XCvL86Vx jiUosOQ8B3BinLUBm7XSr/yUapVhZgA24pcnoNkIsJvJwK5ALUcV6qsiEERKt1qeDgyY hlrDDL5tGoDtDbtZoFiAfZ/lQS/TSWVODcS9Ge1ac/ugpX0xl5BHo/ilIllrz+GXDMUr MmiQ== X-Gm-Message-State: APzg51AEuR++vYOOyL74xR3jk0ZsTC12vTBxrD69ZW0/Fq9pZjHyg4HX OVMvCxTitX16uDdrNi+UZUMMIw== X-Received: by 2002:a17:902:286a:: with SMTP id e97-v6mr36378195plb.340.1537395647452; Wed, 19 Sep 2018 15:20:47 -0700 (PDT) Received: from localhost ([209.121.128.187]) by smtp.gmail.com with ESMTPSA id a20-v6sm52014829pfc.14.2018.09.19.15.20.46 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 15:20:46 -0700 (PDT) From: Viresh Kumar To: Viresh Kumar , Nishanth Menon , Stephen Boyd Cc: Viresh Kumar , Rafael Wysocki , linux-pm@vger.kernel.org, Vincent Guittot , Niklas Cassel , linux-kernel@vger.kernel.org Subject: [PATCH V2 03/12] OPP: Protect dev_list with opp_table lock Date: Wed, 19 Sep 2018 15:20:22 -0700 Message-Id: <3d2556992a878a2210d3be498416aee39e0c32aa.1537394233.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@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. Tested-by: Niklas Cassel 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(-) -- 2.14.1 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 2eb5e2e7ff66..36586f66cd83 100644 --- a/drivers/opp/cpu.c +++ b/drivers/opp/cpu.c @@ -226,8 +226,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 a9d22aa534c3..88e9f47aadf1 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.