Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp601735pxb; Wed, 24 Feb 2021 09:58:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJzs9+kLYC1cWxtKeoCp6JIM4TAYFvs+n/vp2A+qvfdz45Tg+VzcA3SGGQPVPi9oNtNtSzjd X-Received: by 2002:a17:906:654d:: with SMTP id u13mr31433337ejn.299.1614189508423; Wed, 24 Feb 2021 09:58:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614189508; cv=none; d=google.com; s=arc-20160816; b=S7Ip6s06U8CICrhteCIlVfeBTUw+d+NtD5kkYrMT+bqvTUsiJxvnwnnLtqcEf8b84h 7wWA7ih5TASi7SgRdukAtphT5ebq8NbKOPSOekoL5L4JYmuQhjPFcQOdrb19aCQ074sj N9o80a8Wq3qmmfdGxOYeOpuXfPnbo0rZMdvSFQYWFrKJLjmtr0O7Edf4zGq/CAXVH9ZZ pHZPVjNhYzXo6Sg3KnJdzJuSNsAMzMNpvb8JhYswHBq6N91GDbXvImUdL9qPkI3lu1X0 O4OjAJ3pxQKIo6n9XQn7GenDi3cOdz0p/DeElAlcN7d4d+7Aqb8nnaArwaKCvsh7NfKE EptA== 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 :message-id:date:subject:cc:to:from; bh=lu8Es8ObNX6USo7DdGr7Wu3fsJLn3uk9zXtu1V/DIy8=; b=qwC1G6gK6ioS2QtEfJZbPIqhudhk4j0KdhtfQhMG4fDcza3UCV2AhZlLjvM3TGTqzk FcddfxI2+WW3Jfn9h4yJnUokX86+frrlMio/BI5EW+rhPZu/S0Oh5SteZwe/tcH32xlr ZrYB5nNfjaiPwEHh/jQ2i/zN//aULI1Pp8TFM+Wa2IGZ/mOiE5CwAOSCxgfGiD5D720z dG/2P2fg+LcERzonBqgX9ykd/NUt/WlFhhvfj75hPmxgvgUVHTwf7M8dcEerYvBQkknt 8jDdTwva5usOakm46oiVZq7hIE6wo0ZBHAfvBP66DlBz2wHnmj20FnNgsqSCCwby1ei0 yF5Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m1si1758510ejq.592.2021.02.24.09.58.04; Wed, 24 Feb 2021 09:58:28 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233161AbhBXRya (ORCPT + 99 others); Wed, 24 Feb 2021 12:54:30 -0500 Received: from cloudserver094114.home.pl ([79.96.170.134]:52828 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229571AbhBXRy1 (ORCPT ); Wed, 24 Feb 2021 12:54:27 -0500 Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_smtp) via UNIX with SMTP (IdeaSmtpServer 0.83.537) id 41ccb729f266e117; Wed, 24 Feb 2021 18:53:44 +0100 Received: from kreacher.localnet (89-64-80-80.dynamic.chello.pl [89.64.80.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 79D6666152E; Wed, 24 Feb 2021 18:53:43 +0100 (CET) From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Ulf Hansson , "elaine.zhang" Subject: [PATCH v1] PM: runtime: Update device status before letting suppliers suspend Date: Wed, 24 Feb 2021 18:53:42 +0100 Message-ID: <1930477.9OOUKYNkGr@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduledrkeejgddutdegucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvffufffkggfgtgesthfuredttddtvdenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpeeiueevhfeigffhffevueekgedtleeitdfhffejleevtddvtdettedvfffffffhjeenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecukfhppeekledrieegrdektddrkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepkeelrdeigedrkedtrdektddphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehulhhfrdhhrghnshhsohhnsehlihhnrghrohdrohhrghdprhgtphhtthhopeiihhgrnhhgqhhinhhgsehrohgtkhdqtghhihhpshdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=4 Fuz1=4 Fuz2=4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki 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 --- This is different from the previously posted tentative patch, please retest. --- drivers/base/power/runtime.c | 61 +++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 25 deletions(-) Index: linux-pm/drivers/base/power/runtime.c =================================================================== --- linux-pm.orig/drivers/base/power/runtime.c +++ linux-pm/drivers/base/power/runtime.c @@ -325,22 +325,21 @@ 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; + int retval, idx; + bool get, 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 +354,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); }