Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1708477pxb; Mon, 8 Mar 2021 04:35:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJy5pzVnPYmw2l74KCgcpIsV269YBGxWtKFe7XOGze5QuFDzHwFHgpFKPTaPc3CSK/5Q3XB/ X-Received: by 2002:a05:6402:512:: with SMTP id m18mr21664518edv.372.1615206930088; Mon, 08 Mar 2021 04:35:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615206930; cv=none; d=google.com; s=arc-20160816; b=VnTiOARGb4ruuwDacBkX2Q7Tr83lPHD4qkXVr/btJrJJc5A7HIxChapfWEmk+yqGTg FFUj/sSRTLzv0i2Ud15d5mndJz6+lzVZRYpu3EUMEMSrilOVIoFATsm8KVCkdBx/gL5V nWZiKELrQlbT5f2tetOKsaXdq+y/R9Y02xe+FS3fHakHKi5REI//Ri9EUsMd3Dd7bMGZ VvTJW9XcNqDCVR7bzQ1eqMpjTHij4T35nAX4AMLaQocuH98w9gG7PUO3oHUe9/83Lbry /O/akUzptbgGjzfYWlwMuCx57T7ckYq/jDLFQe1vLR66yWP5nTmriTq8vl95stPcwTjf lrgQ== 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=soLdH9ka3qf08esPTM110i9clwGZlxIB0HxqKnAApltagypTpR/MJHQcn895jGWy1k +UPBDCe4NiW8VsC9NGOwOE78lxLHD0k313NMhLZ21a9qvZWp5oQG4gIF9RPv7o8k3tOY 3rEft9MpqxzcoJECXMlPY6G1RYFDfkF5QCOEtDDSH9cCcn67KyKM1PMPIZF0vmd7q1bb yunW7EHgN2MxNC5lceaQeDrOBhyQZKdeP+unbIxm5SiFX5a7VnR2AWqnxW44zN2vnhKG Gqw1TZPTep9pWrV0m6YpQLZWynVbwSohkWaG6KDJHZOSc/BRZz59UTWpYWTEKD1ig7QA FznQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eLUVy5nj; 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 q9si6656565edv.547.2021.03.08.04.35.05; Mon, 08 Mar 2021 04:35:30 -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=eLUVy5nj; 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 S231965AbhCHMcR (ORCPT + 99 others); Mon, 8 Mar 2021 07:32:17 -0500 Received: from mail.kernel.org ([198.145.29.99]:40914 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231585AbhCHMbp (ORCPT ); Mon, 8 Mar 2021 07:31:45 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id EB33B651D1; Mon, 8 Mar 2021 12:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206705; bh=blshZjJYxv2gDWQ04RvTo2V92nJSnamQ+wQkoJ63Ibo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eLUVy5njIxtGtLPWSEuJrVmkCvZb5t9rirg4GjJl22qMbvTjTMhwTBLfi1uy6Ja7a 75TQlr1Sy7lxQ48v4DznkGKTr6G6fLYJ28+ML7XqbW3CxNlU4d8ukjUXXPT/tLWz0e Kbc1W6mOqVjDtAltV8+/28mCA96NjGLKT8Dt2URw= 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.4 09/22] PM: runtime: Update device status before letting suppliers suspend Date: Mon, 8 Mar 2021 13:30:26 +0100 Message-Id: <20210308122714.851102724@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122714.391917404@linuxfoundation.org> References: <20210308122714.391917404@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); }