Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3587060pxb; Mon, 24 Jan 2022 12:54:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwUhRU026CMxE4oXv65jvEa13bwKB5pediuMzbsmaeQkbbdywS3ET+3q6urBZ0H8R0iw7a X-Received: by 2002:a17:90a:bc84:: with SMTP id x4mr140571pjr.230.1643057673137; Mon, 24 Jan 2022 12:54:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643057673; cv=none; d=google.com; s=arc-20160816; b=ZJ23oDzffDNyc3QcFuDLtSS48b/Xy1DQ+dGeQQ1i+N47X53pnQh1hc+15XTtjfaYtt DLTEl0exr/tZxj2MuudXBdUT4XUc87vYfP6vuclK32q5uIFGcPf5yfqjTBjGFrLTDa5C fxbTxd7LpEkMDwjgR54ZdxL2a7TV7t/0+BDrhq3tya0AqpbXu2VLoH3LZ5aSr7Ba4OK8 8YjJ68Ha/ZLX8G7c6AhBLlfR6gu8bqjZyrZmbBl2/AaxoDzEpwaspMbUbbFe4hYonlUW +nFwc8vDJu2Tg/IM7QUb9shXcGErYbhx3Nqo3NR8b/RiFEW3ReVB+7zuKn4n9QMznRiK jamg== 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=EAD6F9FYogbdMjG3sl+mVLd5CFsfpII6Z7K0jqHnB4Y=; b=h7nPKG9vpH69loz5R+1QNmNaEt43eE/8HgMfhk17TFTKOA74bGa+T2pbH/Uk8abfzo wPTESWVJr7DbRLJ1CI7SDNrGyxpCi8hfc6rn7Qoh2tE1His9B1EEKSMD7yMS0WvoahXV VG55alrr3Rz6szf9LI5F74Gd8gXvoFsHtNVj5moFse4iRTLBb32pzMifS1WKB30IkN3b I7Mhgch6rrzurVTOEvgHpA81vUZ8KDgyrNjNg67R4YRuI7iTaIFFg8wBXOYdaXZj+D0g VpRzfPivVVnRMOADisa19EH9753EdyJXaRH1pBHsqjgIA1HDLLDl+avlNApLqtoJH2Zu 02IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yoTZEiip; 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 gi2si338309pjb.50.2022.01.24.12.54.18; Mon, 24 Jan 2022 12:54:33 -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=yoTZEiip; 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 S1380824AbiAXUQ5 (ORCPT + 99 others); Mon, 24 Jan 2022 15:16:57 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:44866 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359363AbiAXT7i (ORCPT ); Mon, 24 Jan 2022 14:59:38 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id DAA2FB8119E; Mon, 24 Jan 2022 19:59:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 349C7C340E5; Mon, 24 Jan 2022 19:59:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643054373; bh=H866M6xCOcdYv+r6p734DGCGRCl35R3+8VLYrg5bNJg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yoTZEiippC5IVv/nLR4Eg0BBkU0gMnf57zNBtRV+n0vOnV7tj4pu4zepahpVu+dV7 qNOzD89nXidFLNcGMBc3yu4u46xVA/WlZpDQUdPVjxGi8v64rkv6jqotcW9d0s85i9 ZbJnTz5JmuPGpk2nfcevw93PswewyecgAE9DXGto= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Zijlstra , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 5.10 369/563] PM: runtime: Add safety net to supplier device release Date: Mon, 24 Jan 2022 19:42:14 +0100 Message-Id: <20220124184037.180762615@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184024.407936072@linuxfoundation.org> References: <20220124184024.407936072@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 [ Upstream commit d1579e61192e0e686faa4208500ef4c3b529b16c ] Because refcount_dec_not_one() returns true if the target refcount becomes saturated, it is generally unsafe to use its return value as a loop termination condition, but that is what happens when a device link's supplier device is released during runtime PM suspend operations and on device link removal. To address this, introduce pm_runtime_release_supplier() to be used in the above cases which will check the supplier device's runtime PM usage counter in addition to the refcount_dec_not_one() return value, so the loop can be terminated in case the rpm_active refcount value becomes invalid, and update the code in question to use it as appropriate. This change is not expected to have any visible functional impact. Reported-by: Peter Zijlstra Signed-off-by: Rafael J. Wysocki Acked-by: Greg Kroah-Hartman Acked-by: Peter Zijlstra (Intel) Signed-off-by: Sasha Levin --- drivers/base/core.c | 3 +-- drivers/base/power/runtime.c | 41 ++++++++++++++++++++++++++---------- include/linux/pm_runtime.h | 3 +++ 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/drivers/base/core.c b/drivers/base/core.c index 389d13616d1df..c0566aff53551 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -348,8 +348,7 @@ static void device_link_release_fn(struct work_struct *work) /* Ensure that all references to the link object have been dropped. */ device_link_synchronize_removal(); - while (refcount_dec_not_one(&link->rpm_active)) - pm_runtime_put(link->supplier); + pm_runtime_release_supplier(link, true); put_device(link->consumer); put_device(link->supplier); diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index bc649da4899a0..1573319404888 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -305,19 +305,40 @@ static int rpm_get_suppliers(struct device *dev) return 0; } +/** + * pm_runtime_release_supplier - Drop references to device link's supplier. + * @link: Target device link. + * @check_idle: Whether or not to check if the supplier device is idle. + * + * Drop all runtime PM references associated with @link to its supplier device + * and if @check_idle is set, check if that device is idle (and so it can be + * suspended). + */ +void pm_runtime_release_supplier(struct device_link *link, bool check_idle) +{ + struct device *supplier = link->supplier; + + /* + * The additional power.usage_count check is a safety net in case + * the rpm_active refcount becomes saturated, in which case + * refcount_dec_not_one() would return true forever, but it is not + * strictly necessary. + */ + while (refcount_dec_not_one(&link->rpm_active) && + atomic_read(&supplier->power.usage_count) > 0) + pm_runtime_put_noidle(supplier); + + if (check_idle) + pm_request_idle(supplier); +} + static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend) { struct device_link *link; list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, - device_links_read_lock_held()) { - - while (refcount_dec_not_one(&link->rpm_active)) - pm_runtime_put_noidle(link->supplier); - - if (try_to_suspend) - pm_request_idle(link->supplier); - } + device_links_read_lock_held()) + pm_runtime_release_supplier(link, try_to_suspend); } static void rpm_put_suppliers(struct device *dev) @@ -1755,9 +1776,7 @@ void pm_runtime_drop_link(struct device_link *link) return; pm_runtime_drop_link_count(link->consumer); - - while (refcount_dec_not_one(&link->rpm_active)) - pm_runtime_put(link->supplier); + pm_runtime_release_supplier(link, true); } static bool pm_runtime_need_not_resume(struct device *dev) diff --git a/include/linux/pm_runtime.h b/include/linux/pm_runtime.h index 161acd4ede448..30091ab5de287 100644 --- a/include/linux/pm_runtime.h +++ b/include/linux/pm_runtime.h @@ -58,6 +58,7 @@ extern void pm_runtime_get_suppliers(struct device *dev); extern void pm_runtime_put_suppliers(struct device *dev); extern void pm_runtime_new_link(struct device *dev); extern void pm_runtime_drop_link(struct device_link *link); +extern void pm_runtime_release_supplier(struct device_link *link, bool check_idle); /** * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter. @@ -279,6 +280,8 @@ static inline void pm_runtime_get_suppliers(struct device *dev) {} static inline void pm_runtime_put_suppliers(struct device *dev) {} static inline void pm_runtime_new_link(struct device *dev) {} static inline void pm_runtime_drop_link(struct device_link *link) {} +static inline void pm_runtime_release_supplier(struct device_link *link, + bool check_idle) {} #endif /* !CONFIG_PM */ -- 2.34.1