Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp779117pxf; Thu, 18 Mar 2021 11:18:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6/I+j0O4o0mStC53+5C2RGhux9NzIJGbnvdgbpiOcfVKPGgslXSfZ5Y6qDojBCoPIJ7Tv X-Received: by 2002:a17:906:6a06:: with SMTP id o6mr41337689ejr.306.1616091501800; Thu, 18 Mar 2021 11:18:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616091501; cv=none; d=google.com; s=arc-20160816; b=sJ0CZT5n6+suiE19OFzOPL0+33doL3bTtdjlCumjj9G+eCLPPZv7JrQonqHzm/m+zq 08IqyX9BnaMSk2wwSc7G2Grcj5w01l8jyZtx1lqJmGDtsRbX99xwTSimmx1zWeFr8UU8 2UIzGqfskGSDHkpeSBiUtuWAuflnfUYTTC2xOPEtjWq9TgEUHWj6BwvSRhaEqz8hPVkS zE/8Cwlr+HCJrjzMxYzNYleFdIWM2Tzt5JxdZ3atoikLu3pb7WcEpp5IqW3g2YH+9VMN Vnnhu53OovKHd0tPgfnC1WbP6o1gEk6Rfko6loEgSQom07isir8gYtthDGicfi2QzlLk 1r/w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=fA3cadDlENICTks2Blewvae/1QlLa9Ch5T4oaNKSN+k=; b=THkhyOHiO6ii813SDZik5uinS+u+VevvDExJ6GHA/o9q2A44k24NmzvUh0sM8Z4Sii W8ri2zFize+Xgfr2kgR9fZtFuKMq5QBb3G1/7glHW4AG5/Ex6C+rSyWYg+eaaNXRXa2t 5iqy6KijhNXxJiHhnoXW9gbXsgByXTjOIDCKFNrQWA+fkONBc1XRFtnWXdbggAf6j/KZ qaq6Cm54FG9GfanP9fMnjUqRn5bhlbFj/u/hOJZAJFh9BBjlgvvcswS4yU0MPRgsM8e0 stRI4mbDmEQOfE39IL6mwexIyY4xlLVZsoPXM5wfMVgcK50nXyMcAejhlf1nayqbTZKv a9Vg== 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 h1si2055901edv.113.2021.03.18.11.17.59; Thu, 18 Mar 2021 11:18:21 -0700 (PDT) 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 S232556AbhCRSQQ (ORCPT + 99 others); Thu, 18 Mar 2021 14:16:16 -0400 Received: from cloudserver094114.home.pl ([79.96.170.134]:55420 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232543AbhCRSPp (ORCPT ); Thu, 18 Mar 2021 14:15:45 -0400 Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 2.0.3) id 1e0020f270e78a72; Thu, 18 Mar 2021 19:15:44 +0100 Received: from kreacher.localnet (89-64-80-250.dynamic.chello.pl [89.64.80.250]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 7DEAF668FA9; Thu, 18 Mar 2021 19:15:43 +0100 (CET) From: "Rafael J. Wysocki" To: Linux PM , "elaine.zhang" Cc: LKML , Ulf Hansson Subject: [PATCH v1 1/2] Revert "PM: runtime: Update device status before letting suppliers suspend" Date: Thu, 18 Mar 2021 19:09:06 +0100 Message-ID: <4304785.LvFx2qVVIh@kreacher> In-Reply-To: <5448054.DvuYhMxLoT@kreacher> References: <5448054.DvuYhMxLoT@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduledrudefiedguddufecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvufffkfgjfhgggfgtsehtufertddttddvnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepffeuvddutdelgfdvtefgiefftddvfffgjeelvdethfehgfekfeeluedvueevvedunecuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucfkphepkeelrdeigedrkedtrddvhedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepkeelrdeigedrkedtrddvhedtpdhhvghlohepkhhrvggrtghhvghrrdhlohgtrghlnhgvthdpmhgrihhlfhhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqpdhrtghpthhtoheplhhinhhugidqphhmsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepiihhrghnghhqihhnghesrhhotghkqdgthhhiphhsrdgtohhmpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuhhlfhdrhhgrnhhsshhonheslhhinhgrrhhordhorhhg 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" Revert commit 44cc89f76464 ("PM: runtime: Update device status before letting suppliers suspend") that introduced a race condition into __rpm_callback() which allowed a concurrent rpm_resume() to run and resume the device prematurely after its status had been changed to RPM_SUSPENDED by __rpm_callback(). Fixes: 44cc89f76464 ("PM: runtime: Update device status before letting suppliers suspend") Link: https://lore.kernel.org/linux-pm/24dfb6fc-5d54-6ee2-9195-26428b7ecf8a@intel.com/ Reported by: Adrian Hunter Cc: 4.10+ # 4.10+ Signed-off-by: Rafael J. Wysocki --- drivers/base/power/runtime.c | 62 +++++++++++++++--------------------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index 18b82427d0cb..a46a7e30881b 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -325,22 +325,22 @@ static void rpm_put_suppliers(struct device *dev) static int __rpm_callback(int (*cb)(struct device *), struct device *dev) __releases(&dev->power.lock) __acquires(&dev->power.lock) { - bool use_links = dev->power.links_count > 0; - bool get = false; int retval, idx; - bool put; + bool use_links = dev->power.links_count > 0; 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. */ - if (get) { + /* + * 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) { idx = device_links_read_lock(); retval = rpm_get_suppliers(dev); @@ -355,36 +355,24 @@ static int __rpm_callback(int (*cb)(struct device *), struct device *dev) if (dev->power.irq_safe) { spin_lock(&dev->power.lock); - return retval; - } - - spin_lock_irq(&dev->power.lock); - - 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(); + } 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(); -fail: - rpm_put_suppliers(dev); + fail: + rpm_put_suppliers(dev); - device_links_read_unlock(idx); + device_links_read_unlock(idx); + } spin_lock_irq(&dev->power.lock); } -- 2.26.2