Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp4021534imj; Tue, 12 Feb 2019 08:29:31 -0800 (PST) X-Google-Smtp-Source: AHgI3Ibn/RxxFj4lyDLHQMHcLJcJk0JVVAlSeAa5Jh0bTCNWYN7vgZiDjWEUXTEwPVf/5YhFW3hg X-Received: by 2002:a63:c0e:: with SMTP id b14mr4387442pgl.236.1549988971134; Tue, 12 Feb 2019 08:29:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549988971; cv=none; d=google.com; s=arc-20160816; b=WsAnRG1EfQCE+HZ66X/YBVlqEqaBeuZwIxWsYmUZtrzhtcjGNnJz3/b7oGBprbyf11 4nJLm0UDEhgK8/SnQJX7kTq0Mi2WBjH3i3nPpjlcEtz6ySOd8vbNIAtjt8R7Qpj7ZQLQ ZMuO4ZRgFNNOVtxQ9fjtEFc/37ysLnU+SBPzSEKZcSOFsscyTJBHNoAcM0k8j/QU/VB+ EvUZ2OF/EmjZate0/x5j4Qq7IJBFlKY7bUjWiN7LI3ggWGphAGso3NPz2NGdrDr3DFQO HvUPCGoXEGvQih21bsF0vu+VCiuDEx7xUgkqWSiPEI7JhXXE6WjlXmQTnYFvjMqriIG6 UpWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=fseXW1aQ+MNpVWzEhj80Xv7V/c9aS3/ACQPeP06BvyA=; b=nZ1BBQwMFfGpIVORdEA1E2tCczXE58UTzadAImu3T+eNpGGDkcIJGKU/3P/6v/POOd f7K0PeGt8P9LPzrpdYaaCwYonmHvekj7cOJWOLtHBnhSWIbMapvZBGNQXx5QwfrLNtHp evnpx1pXZEurnqIfFxX49oFI7WDyHjARvJaHOV4AIuDoHLlh8xlhikAuu3lGqCwuooWe MXsyra9YiMevVDGI2qVeBmC22HOK9cSC/aAn0f3F9iudGdg+NR/5/2MIrzxqktVmyUeW 9+H1EpJF/BQyjX4Q3zfEBFJjdIhXooChxPW34hCrgsZRPZMlsMu14YIy8uZg2L7jcrQZ NFww== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d14si13588607pll.30.2019.02.12.08.29.14; Tue, 12 Feb 2019 08:29:31 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731061AbfBLQ27 (ORCPT + 99 others); Tue, 12 Feb 2019 11:28:59 -0500 Received: from mail-ot1-f68.google.com ([209.85.210.68]:43285 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727442AbfBLQ26 (ORCPT ); Tue, 12 Feb 2019 11:28:58 -0500 Received: by mail-ot1-f68.google.com with SMTP id n71so5373583ota.10; Tue, 12 Feb 2019 08:28:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fseXW1aQ+MNpVWzEhj80Xv7V/c9aS3/ACQPeP06BvyA=; b=QcI5GRn5cPGAFIYKsoLhDGEACIrCACFB7oMamxPi41aeKrKo/elVgHCYIDxzMkikrU t3bjYM7agQC30aNSgoVVEBW99kkFSTU2BsMBGCTxeXmZAEoaRSxw8dSJBzZbktAQCBsP c8JzpN94Vc8y+1m7mk+XSciEEiW+wrz39ldJrZXewj5SHAT5PJcC2bo8j1em6l7Xz2A0 u1AQPdVjs9JmmSCj6eGaG4FyVS5is39ALGaMx+vLb4vlLWKzaQVVtWQstV6Fd9nwDoYq YM+Ht/vBOjCt+i6jPoZW2zFvlNhCt9r07OGzPPlzTN+Gh5A61crip+hf2/Oqfh5DBSzs Sq9Q== X-Gm-Message-State: AHQUAuaKSum21sMwxivV6+jBYJlDedlKockUQ6V0kEg6cnNareL8I7Pt SFpd6HefteN8mC1YybNPTVihLVfcSH4mT4f94uk= X-Received: by 2002:a9d:7990:: with SMTP id h16mr4467156otm.222.1549988937515; Tue, 12 Feb 2019 08:28:57 -0800 (PST) MIME-Version: 1.0 References: <5510642.nRbR3bcduN@aspire.rjw.lan> <1670239.00ZbddH1IV@aspire.rjw.lan> In-Reply-To: From: "Rafael J. Wysocki" Date: Tue, 12 Feb 2019 17:28:46 +0100 Message-ID: Subject: Re: [PATCH 1/2] PM-runtime: Fix __pm_runtime_set_status() race with runtime resume To: Ulf Hansson Cc: "Rafael J. Wysocki" , Greg Kroah-Hartman , LKML , Linux PM , Daniel Vetter , Lukas Wunner , Andrzej Hajda , Russell King - ARM Linux , Lucas Stach , Linus Walleij , Thierry Reding , Laurent Pinchart , Marek Szyprowski Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 12, 2019 at 5:18 PM Ulf Hansson wrote: > > On Tue, 12 Feb 2019 at 13:10, Rafael J. Wysocki wrote: > > > > 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); > > pm_runtime_enable() uses spin_lock_irqsave(), rather than > spin_lock_irq() - is there a reason to why you want to allow that > here, but not earlier in the function? Device links locking cannot be used in interrupt context.