Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp241832pxf; Wed, 10 Mar 2021 05:28:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJydUcMBlBwyfj0xs6Yv0hH3HJ6fRmPIBII0b2w2+aNYLyDVmPQVHS2x6EmUzOhPuqCmrYmt X-Received: by 2002:a17:906:814b:: with SMTP id z11mr3650293ejw.290.1615382919656; Wed, 10 Mar 2021 05:28:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615382919; cv=none; d=google.com; s=arc-20160816; b=Mfs6AS9d/C+Vv7N4wjNDLnmoQaRXkDnuQq5XNQ6i7sLBz+V6LXNHYw7Ff8ieWIDFa5 GRqHzHQWytdSpD2634P89TaXVU3t8iJgX2rPIj6fn5ie2ahvsw+t3jmkRTWPDaKdLxc7 XDBH0ofQd/Mg3BroCKVTY89/uC1a5uv+7t1bBRmURh6OTc+qoAY2sxJc7T5bF5yZpIW2 wbkDOvEWFcTzmprFQ7MG2qxFenZkJdpW4msO6pliEYXusWIbZhzGIq+n9pTETQ7RAamL cbuZ+pR+ZSCq2s7Qj7Opp4YIH+wRABiGBo27Tt4xy+CCn+uO6oJsg5VmkiUbiWjtWQeK OtDQ== 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=vNAVgn/bQi5uHSVBc7FybpoYY/BqAl+u8CumhqrKLhU=; b=s+ycStuuLxNvYAG+CVleYCVcXXNA/hVe9GAb2YyxHqnNT8m6Q9F2YVv29iNAinlZaZ E4IiqsmqBd6U88I74nCbI2sdPPn4V52dX/PqfBpv7/6W45htoLEZoXBn3+9rj3SuYBdF p0vIkWj7kGmRhIWBmlr9szIvEuB8iks9IqUgDqdJkdu7sh0b1/r9PxeU8+3Fr5+q4Mmr 6i1hgvCGVHkgUrY04cGaw6YDEhqMh1z96Jo/RLI+VMFZFt31vabNHikcFFYQDEZTgR80 Eod9mKLbsVYCER5Ua/o8k2gxJNvr2wncBVVDZLYA848OwttjmUfWT0ZcWdxvzKVGQz85 AlHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KbPsx6aw; 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 x21si11974301eju.471.2021.03.10.05.28.15; Wed, 10 Mar 2021 05:28:39 -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=KbPsx6aw; 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 S233925AbhCJN1e (ORCPT + 99 others); Wed, 10 Mar 2021 08:27:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:46594 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233185AbhCJNZD (ORCPT ); Wed, 10 Mar 2021 08:25:03 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4E89B64FD7; Wed, 10 Mar 2021 13:25:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615382703; bh=QXZ5S/Acj15AmWdZhhP801cSifJnhspu68hy6BN/oSw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KbPsx6awbEjWSQg01GNIpZ0ydvBqB/dm0G1OXyMZZQ0Ci1ADhG1kSt3nhQmWGuBNO G+PTqHociSwHMoYk+a2NhK0N52+AzbUrQ+Bcwd1v2etJEA/zGsklK9wQOyuQ90iqQE jZ6bEejYpRoBwX6QryHNgPYohmvO4HcnpCjmzmfQ= 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.19 06/39] PM: runtime: Update device status before letting suppliers suspend Date: Wed, 10 Mar 2021 14:24:14 +0100 Message-Id: <20210310132319.932470905@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210310132319.708237392@linuxfoundation.org> References: <20210310132319.708237392@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 @@ -304,22 +304,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); @@ -334,24 +334,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); }