Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp3755019imj; Tue, 12 Feb 2019 04:11:48 -0800 (PST) X-Google-Smtp-Source: AHgI3IbT2W08rhdAoUOxGLLftf+jbPe8l7DUelFvnWweD6nXdvEoa57fHh/qFzB5b2yVcji3LAGW X-Received: by 2002:a17:902:ba8b:: with SMTP id k11mr3703048pls.177.1549973508602; Tue, 12 Feb 2019 04:11:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549973508; cv=none; d=google.com; s=arc-20160816; b=NU8CLVGtWBV1CBqt0016Rj1yInZ1JxmSgOofJGQb8C9pjEaRsEDJasQNlbJq3rc4my N5i47VZVR7TWfmYITMett9kk/+N9AxLerxVcNVi35GcB58Zx8LH65scRvGuJlZpLPpOd HpRQ/nvAxuN9NdiDQr6SW/GiSIioT8v143RYOX6IM1P4ngORUen/AorVDsIflNbCBEk6 C8h4oD3FO0h5lPLgfdnVwWTlIt5MbAut+vne4go8eO2PqSEo1AhVFE6mVJISt1+eTrzN IRI4xDcQZsxlPrMs18kWlDj3Nn4CfsoXTed3W7YmLEnB7GDPiUaRdE01ZGPz36ArzTf5 XA8w== 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; bh=v7uwsZ7LHJ6GRnsfpXKlvXWbj7Rw8obO8xrHi1g3VRU=; b=Go+t9lizGFjrPZmWsqlfl469svSWqceXepUOVhgyAA1UCoq/1sI2EpKVUYK/RAT1fg 18eUr1fa6MEKLRUAVf1ofJmLKkNcpaHnoJUWequs66SKeAkmH0aDcF+bm0h5j01+FE2e EYrFB3vYVGobs9aiGHJqJ3s1dR/QHvbELh0LDMcDWVgE+80+PIWeW/TdT0bF1tpOa3iH gWczxOqJYN4GaYQHW6JhiSF+BAcKaEAdjn6b3OlSKWertWLYyzZmiS8vV14nvRLdC35S znN1TAx72p8XnZfVoAHE7kpInSNJSLsmSU5M0Dr/NHGyQhbKnPEkZqTzC4ZV02NooBdK /aNA== ARC-Authentication-Results: i=1; mx.google.com; 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 k1si11861999pgq.144.2019.02.12.04.11.31; Tue, 12 Feb 2019 04:11:48 -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; 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 S1728873AbfBLMK5 (ORCPT + 99 others); Tue, 12 Feb 2019 07:10:57 -0500 Received: from cloudserver094114.home.pl ([79.96.170.134]:48583 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727553AbfBLMK4 (ORCPT ); Tue, 12 Feb 2019 07:10:56 -0500 Received: from 79.184.254.36.ipv4.supernova.orange.pl (79.184.254.36) (HELO aspire.rjw.lan) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.183) id 3f8de3721c143816; Tue, 12 Feb 2019 13:10:54 +0100 From: "Rafael J. Wysocki" To: Greg Kroah-Hartman Cc: LKML , Linux PM , Ulf Hansson , Daniel Vetter , Lukas Wunner , Andrzej Hajda , Russell King - ARM Linux , Lucas Stach , Linus Walleij , Thierry Reding , Laurent Pinchart , Marek Szyprowski Subject: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume Date: Tue, 12 Feb 2019 13:04:12 +0100 Message-ID: <1670239.00ZbddH1IV@aspire.rjw.lan> In-Reply-To: <5510642.nRbR3bcduN@aspire.rjw.lan> References: <5510642.nRbR3bcduN@aspire.rjw.lan> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki Commit 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") introduced a race condition that may trigger if __pm_runtime_set_status() is used incorrectly (that is, if it is called when PM-runtime is enabled for the target device and working). In that case, if the original PM-runtime status of the device is RPM_SUSPENDED, a runtime resume of the device may occur after __pm_runtime_set_status() has dropped its power.lock spinlock and before deactivating its suppliers, so the suppliers may be deactivated while the device is PM-runtime-active which may lead to functional issues. To avoid that, modify __pm_runtime_set_status() to check whether or not PM-runtime is enabled for the device before activating its suppliers (if the new status is RPM_ACTIVE) and either return an error if that's the case or increment the device's disable_depth counter to prevent PM-runtime from being enabled for it while the remaining part of the function is running (disable_depth is then decremented on the way out). Fixes: 4080ab083000 ("PM-runtime: Take suppliers into account in __pm_runtime_set_status()") Signed-off-by: Rafael J. Wysocki --- drivers/base/power/runtime.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) Index: linux-pm/drivers/base/power/runtime.c =================================================================== --- linux-pm.orig/drivers/base/power/runtime.c +++ linux-pm/drivers/base/power/runtime.c @@ -1129,6 +1129,22 @@ int __pm_runtime_set_status(struct devic if (status != RPM_ACTIVE && status != RPM_SUSPENDED) return -EINVAL; + spin_lock_irq(&dev->power.lock); + + /* + * Prevent PM-runtime from being enabled for the device or return an + * error if it is enabled already and working. + */ + if (dev->power.runtime_error || dev->power.disable_depth) + dev->power.disable_depth++; + else + error = -EAGAIN; + + spin_unlock_irq(&dev->power.lock); + + if (error) + return error; + /* * If the new status is RPM_ACTIVE, the suppliers can be activated * upfront regardless of the current status, because next time @@ -1147,12 +1163,6 @@ int __pm_runtime_set_status(struct devic spin_lock_irq(&dev->power.lock); - if (!dev->power.runtime_error && !dev->power.disable_depth) { - status = dev->power.runtime_status; - error = -EAGAIN; - goto out; - } - if (dev->power.runtime_status == status || !parent) goto out_set; @@ -1205,6 +1215,8 @@ int __pm_runtime_set_status(struct devic device_links_read_unlock(idx); } + pm_runtime_enable(dev); + return error; } EXPORT_SYMBOL_GPL(__pm_runtime_set_status);