Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1709337pxb; Mon, 8 Mar 2021 04:36:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJxjQSp/0xUQtuISI79P7LADDHuccaKa9SwvlAkO6AMRvyeLHd2f5GNbLSBMSqjy1AQojuuD X-Received: by 2002:a17:906:bb06:: with SMTP id jz6mr14601876ejb.429.1615206996588; Mon, 08 Mar 2021 04:36:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615206996; cv=none; d=google.com; s=arc-20160816; b=VoTzUyRCiQaAnQ7m+nFlohHqGh4HNHL53pdEFcX/mP6M7GUHMirUh9HkHry+7gqt/B GWhuCtg0sQnmhiG8jCNYE8TnB7bHAd1K5p0DzaPgejFKMg+MEGGJ+SzG9KxgMwKdHi2q VYBufuJH9DyC0CdT9So9yTx9SUBVHowFzUNWfylRJOU6om3iEAyAuUWSUSZSnp2WENKK UlDAHlb3TZfifC46F3SUBVVfd7v/q+p/7rYqvSpyw/pwCQDKDUCzh02p/ynqyvRtOi0w Ld/ILa/bHDsAaAVo7ql+3kGEwGKtZijJ7nTJFVlmF1hC3suL9q+yeT2TGVTMTJjBvGXV CJ3A== 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=XqunUxmmBTUHo0A5BRQ48kN9L7TKdMslamtyyS7tv7g=; b=Dcbp9N0lnA9Vz2J+QBUOPdULGz7PO3SVSd97IZ3QThMdHV0glzvVBRYZlmapVKTTj9 PgvOGWrTtaoVyzfndf8BY+fZD+dcHYJqrp4crLQz5pVJHhpWAoUSOiBXiFSsfvG8EXi7 a8IXOD6Vr/7uyo++8X5UYErMJzXHXRLqCx4Z5+F3bJyOMjrZS42GoxRosmfFhlxL05m5 dipmssnUK5qoSx0dnyWb/uIeVWn4knUsc6uB9qvivNbkcyIjx3gk6RXKM8xQOJqD1PPF 1VvaDsy68vCplq3b+ORA2Du3CYTopnZ2nC4NuRehNYXm61XrPUUxEruYSlaD1nAaDQFA D4Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fI32+rt2; 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 o9si6745890eja.264.2021.03.08.04.36.13; Mon, 08 Mar 2021 04:36:36 -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=fI32+rt2; 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 S231916AbhCHMe7 (ORCPT + 99 others); Mon, 8 Mar 2021 07:34:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:43390 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231520AbhCHMe2 (ORCPT ); Mon, 8 Mar 2021 07:34:28 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8C02F651C3; Mon, 8 Mar 2021 12:34:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206868; bh=blshZjJYxv2gDWQ04RvTo2V92nJSnamQ+wQkoJ63Ibo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fI32+rt2zTwD2qLcYNdoUKuPz2oyy60djQPgF81JxFAUBYc0Z2hzH4hSCV2clFY36 T56uKPmZ36SZcJJ4lJ0c/e41h7Es7CB2el2jcBkecRXZ19aKezDYC94YtUnj3FQVUx EDmqAMVXOXgHehomYNngzHo9COXzqqogF0iYK6Vo= From: Greg Kroah-Hartman 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 5.10 16/42] PM: runtime: Update device status before letting suppliers suspend Date: Mon, 8 Mar 2021 13:30:42 +0100 Message-Id: <20210308122718.932483994@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122718.120213856@linuxfoundation.org> References: <20210308122718.120213856@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: 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 @@ -325,22 +325,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); @@ -355,24 +355,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); }