Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp341652pxb; Thu, 25 Feb 2021 04:10:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJylS1MaBnT3sK4iVPna98uqQKo9alZM8tKiqtleVBCTCXoXOLkv48DellRJFFPYJdedijZ+ X-Received: by 2002:a05:6402:78d:: with SMTP id d13mr2487122edy.253.1614255043575; Thu, 25 Feb 2021 04:10:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614255043; cv=none; d=google.com; s=arc-20160816; b=x4dT4KNoWf3aeACKdV8Bl42Lqkdbbr0Nal1CTe1NXAq5BbtXzO1d6Q4e+SSJtBCMF1 Mm5FsxgrfnaXrHKxl0UYW/aHs9Bg9Ey+h4D3S2J9isheI2T6dZSFSMNvMwNpKSPfmfyq U8ABHBUOomvp4moOugA34pNkoCZXNDqHses9DjlDsL42LaL+X05YrgdHX9eJGjbRDQ6t 66aJ6iFgyboEJHnRudgqfat4MizuDu9oJVftRxowWJQuAAowAchFk0kehOtm3lRg49Qo ZeJMz8FjMjs2qhpvMyb5rsAl5q1bujsFpSnyskSyyWPaRnTTpzDwMf3yacJRyzYOvdm1 btxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=UhEJQBhw39zimf8xSJsTGK8QgI92+xzv+a86zthTdI8=; b=JrtT0jSBJv2p5/PbXp0q36WUQCIkkPKLMbST26AP8QuSpTsoHywSpEG2+idQVX6vTO 7lP7ZmC1VA2dd/Giwy9DGrraUuQwxMlfGTUWIwIQ7my9Vz3Ugh6nu8g83RIStIuaDsMA haO0j6hR0PCVXuFZV6G15k/GcinHeJjgIKJKABRQdL1GOlhobuWABsT3wZCiKuTOVB4N JFoS6Km8rQmzp+b9hDEAxp1ihFURVOLJMeQ5ADS1tBiueEWDSbnoESz3WvfGUt6ggtQ8 DXfAZaOnXsQGkRdbPs++jUVSBRTn4TGp6XUjozsEEsewp0469voX01Pa15IZ0Gaxswn0 1cXg== 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t1si3399505ejy.51.2021.02.25.04.10.21; Thu, 25 Feb 2021 04:10:43 -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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233269AbhBYMHg convert rfc822-to-8bit (ORCPT + 99 others); Thu, 25 Feb 2021 07:07:36 -0500 Received: from mail-oi1-f176.google.com ([209.85.167.176]:37411 "EHLO mail-oi1-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbhBYMHa (ORCPT ); Thu, 25 Feb 2021 07:07:30 -0500 Received: by mail-oi1-f176.google.com with SMTP id l133so5839784oib.4; Thu, 25 Feb 2021 04:07:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=7UvPTCRgz9liGqcyePe7weHBGn9Fm8WUz4WgzjQSZAA=; b=bjxX+f1rZqGZn6L/2THcBWxECpI1R8VooFV0lINR0kQvkcAgreKSSXGpvG6oV8WMBi NaT/nMzDSA6a0IzCs0uMu0R4v8JYfze3dL9dKEu5lyftNFaiabdpF0v7HlaU4MbfrFKH D+iH9rYdb3LaSisgZVfUvMUA2Rx6gbgLYYQ8zV2b8fKRwp7gzbHxLRWidk8ydH/oCaeU QWlgDvAeHzEJhQ1V2ioVuvNTthFhq8AYvI3cy5aSK3xpPAzz96+Huvoz0HLOhzODdpcr O3EPSiaVcuJyaeMfGLHY4/7XkMJyi38IqOFevZ0T5zdqXeaZwtsTz70loQC3P+u0ZGoq jZpg== X-Gm-Message-State: AOAM533NUEAE3+NTOmrRl0rayEqfBZF2foBQCsxwujl6s9UUIAM5Y9vA EzPwuPoCeUS6c6retIXEGFDDH50wr2HUcQgh6vlOvsha X-Received: by 2002:aca:3bc6:: with SMTP id i189mr1653518oia.71.1614254809370; Thu, 25 Feb 2021 04:06:49 -0800 (PST) MIME-Version: 1.0 References: <1930477.9OOUKYNkGr@kreacher> In-Reply-To: From: "Rafael J. Wysocki" Date: Thu, 25 Feb 2021 13:06:38 +0100 Message-ID: Subject: Re: [PATCH v1] PM: runtime: Update device status before letting suppliers suspend To: "elaine.zhang" Cc: "Rafael J. Wysocki" , Linux PM , LKML , Ulf Hansson Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 25, 2021 at 2:32 AM elaine.zhang wrote: > > Hi, Rafael: > > 在 2021/2/25 上午1:53, Rafael J. Wysocki 写道: > > 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); > > } > drivers/base/power/runtime.c: In function '__rpm_callback': > drivers/base/power/runtime.c:355:13: warning: 'get' may be used > uninitialized in this function [-Wmaybe-uninitialized] > error, forbidden warning:runtime.c:355 This really is a false-positive, because in fact the "get" variable cannot be uninitialized at this point. > put = get && retval; > > There is a compilation error. I change it as: > > put = dev->power.runtime_status == RPM_SUSPENDING && retval; I prefer to initialize "get" to false (even though that is not strictly necessary). > And test works well.Please check it. OK, thanks for testing! I will send an updated patch later today.