Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp243597pxf; Wed, 10 Mar 2021 05:31:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlf7Kq5K+2T7qB/21TAjJ8B8YcmbPdLAz0S4WpcYsIQP84pMTx9ioEzJ89Erj2TTuBGzJO X-Received: by 2002:a17:906:38da:: with SMTP id r26mr3721896ejd.251.1615383071893; Wed, 10 Mar 2021 05:31:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615383071; cv=none; d=google.com; s=arc-20160816; b=lfnlAbf/xrSVjk8Fa90wwnacP9L7WbqpvX2yYBepRRar/km/GkMZxx5mHMY7R0WBJt uW9mudesqMMbt2XuoRjdv6bBo9rWEdsHJ8sV4BtdoupnFSoeySb8f9T0vjdDLzm82LBG 7YhQdjgL9fca3ql8ly4tIgU2b4gTBqmWDaLXQLDwvS5d9btlaBJoUlK8c83t6O1flzg+ YuISGp7WHYT9buE6xbnr5E3LKfGzvyH0EW5bbg5KMLDeDCLEV0veOfSYEYz/AV/chRMc 47pv5pMuMyucmE2wO5kM/pPKWsPPYL3GhYqF2YxuYiPkPzR4vOrfmh24PR/FqHkJYDfR 7gYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gWnj3/PbJXsu0ddnbPeFVd4vSGKIu5g5Mp5YVIrfp+c=; b=ppyb/uanPGdSG45wC3EMLl33Q4LAsUNP8jzJr/LHMja28yZB9C4CUjscg2I+1QT7x/ ud7YltS2CmyIe1j5Sk+QNVhXbFYcDTXHsQ6kEdNCxfqWD2RfJUr0SkutwTV0cLJEXiyj fqFIWpT8O+0JYIAJ0uVeDsI5xk1kHUk44xfmtCGuWhJRkrAzxo9G1yosAOMc0Dxw4Yrg a8OSNvh8p1YFWx+JUxW8pJZef7ONE9a9kwB2OOXULn6pbmtyskE83/tIZKrQa35UXEn5 iT/V11e/HNQiQt3wjiSpeiPeQJT/uK4a3tQQqohtFPgttSo/h3wNQX58AkW8JJicn8h5 VdKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=i9g6TfCG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m19si11667220eds.451.2021.03.10.05.30.48; Wed, 10 Mar 2021 05:31:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=i9g6TfCG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233980AbhCJN1z (ORCPT + 99 others); Wed, 10 Mar 2021 08:27:55 -0500 Received: from mail.kernel.org ([198.145.29.99]:46690 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233227AbhCJNZH (ORCPT ); Wed, 10 Mar 2021 08:25:07 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id F2AD464FD8; Wed, 10 Mar 2021 13:25:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615382707; bh=8O5WTqJe4DAwwRP33UWo+XAsbxy43DK2gxOzBEpyR74=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i9g6TfCGBHOTedRMCXcAUNaMO3cyDleht/9wVd+vFKM6d2zsHqhI/8EwKSFOhdfvf 9jD8L8XFijkc6BA428enAF5S/8M3qRULyQJDXGqfKZ29uMTyaB3+yZNCRpLAoKnYfM a4Oqg1Ms3b3sou+r4l0jk9GmEnd/A4ixpiCdvyb0= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Elaine Zhang , "Rafael J. Wysocki" , Elaine Zhang , Ulf Hansson Subject: [PATCH 4.14 03/20] PM: runtime: Update device status before letting suppliers suspend Date: Wed, 10 Mar 2021 14:24:40 +0100 Message-Id: <20210310132320.624261268@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210310132320.512307035@linuxfoundation.org> References: <20210310132320.512307035@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Rafael J. Wysocki commit 44cc89f764646b2f1f2ea5d1a08b230131707851 upstream. Because the PM-runtime status of the device is not updated in __rpm_callback(), attempts to suspend the suppliers of the given device triggered by rpm_put_suppliers() called by it may fail. Fix this by making __rpm_callback() update the device's status to RPM_SUSPENDED before calling rpm_put_suppliers() if the current status of the device is RPM_SUSPENDING and the callback just invoked by it has returned 0 (success). While at it, modify the code in __rpm_callback() to always check the device's PM-runtime status under its PM lock. Link: https://lore.kernel.org/linux-pm/CAPDyKFqm06KDw_p8WXsM4dijDbho4bb6T4k50UqqvR1_COsp8g@mail.gmail.com/ Fixes: 21d5c57b3726 ("PM / runtime: Use device links") Reported-by: Elaine Zhang Diagnosed-by: Ulf Hansson Signed-off-by: Rafael J. Wysocki Tested-by: Elaine Zhang Reviewed-by: Ulf Hansson Cc: 4.10+ # 4.10+ Signed-off-by: Rafael J. Wysocki Signed-off-by: Greg Kroah-Hartman --- drivers/base/power/runtime.c | 62 +++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 25 deletions(-) --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -306,22 +306,22 @@ static void rpm_put_suppliers(struct dev static int __rpm_callback(int (*cb)(struct device *), struct device *dev) __releases(&dev->power.lock) __acquires(&dev->power.lock) { - int retval, idx; bool use_links = dev->power.links_count > 0; + bool get = false; + int retval, idx; + bool put; if (dev->power.irq_safe) { spin_unlock(&dev->power.lock); + } else if (!use_links) { + spin_unlock_irq(&dev->power.lock); } else { + get = dev->power.runtime_status == RPM_RESUMING; + spin_unlock_irq(&dev->power.lock); - /* - * Resume suppliers if necessary. - * - * The device's runtime PM status cannot change until this - * routine returns, so it is safe to read the status outside of - * the lock. - */ - if (use_links && dev->power.runtime_status == RPM_RESUMING) { + /* Resume suppliers if necessary. */ + if (get) { idx = device_links_read_lock(); retval = rpm_get_suppliers(dev); @@ -336,24 +336,36 @@ static int __rpm_callback(int (*cb)(stru if (dev->power.irq_safe) { spin_lock(&dev->power.lock); - } else { - /* - * If the device is suspending and the callback has returned - * success, drop the usage counters of the suppliers that have - * been reference counted on its resume. - * - * Do that if resume fails too. - */ - if (use_links - && ((dev->power.runtime_status == RPM_SUSPENDING && !retval) - || (dev->power.runtime_status == RPM_RESUMING && retval))) { - idx = device_links_read_lock(); + return retval; + } - fail: - rpm_put_suppliers(dev); + spin_lock_irq(&dev->power.lock); - device_links_read_unlock(idx); - } + if (!use_links) + return retval; + + /* + * If the device is suspending and the callback has returned success, + * drop the usage counters of the suppliers that have been reference + * counted on its resume. + * + * Do that if the resume fails too. + */ + put = dev->power.runtime_status == RPM_SUSPENDING && !retval; + if (put) + __update_runtime_status(dev, RPM_SUSPENDED); + else + put = get && retval; + + if (put) { + spin_unlock_irq(&dev->power.lock); + + idx = device_links_read_lock(); + +fail: + rpm_put_suppliers(dev); + + device_links_read_unlock(idx); spin_lock_irq(&dev->power.lock); }