From patchwork Fri Jun 6 11:17:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudiu X-Patchwork-Id: 894888 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A261128750F for ; Fri, 6 Jun 2025 11:18:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749208682; cv=none; b=Wi3NExVAvBnyc5zrH3n/zPkIeWL5DG2Uvu0tgsE55vjI84waHk7OBuQaR3UREpAV04YCPt9mzjQ0YG4b6O8oeh+rQWWtSh2aGZ5Kg8zuHjFBsk+MzWghX7mBPph4apta3oe+H6KAJ/XLHmUQTjrtMcafeWphxdGTAtpCbH18ch0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749208682; c=relaxed/simple; bh=2Zf9PqQ/xdsUa49ohUw4b0FzbCTktzpyC6txhBl+f2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=srdFrH8Ly7svB6xRtuaEzbVdgziqoyPhAbZK1G6Kx5zKCL6Z419dB2Xdwpv6BBYg3fezfN5LKsl+oy58M7cHzRcWVmpictYi3aC5hludMuXgxbjTTrG814S0I0BVI75gB4r4NOxViWzUZ3gIPzfTVOjZ5FjWFOBmnvRdCgnQkV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev; spf=pass smtp.mailfrom=tuxon.dev; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b=DBKCcI3b; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b="DBKCcI3b" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-acb39c45b4eso305042966b.1 for ; Fri, 06 Jun 2025 04:18:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tuxon.dev; s=google; t=1749208679; x=1749813479; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=K9UeLBEeNgVFZxNvwdqTWlF8knLYtgrdcWBf2xtn+Ko=; b=DBKCcI3bfOOqblSO59w3vot2T5GnEqknF0Su7dLvhZMIBNr8p9BZ95rbGQbs3j4WpD qbCqEYY4hLdbAhb3jWAM/2NyRD+25CDi5k5tBlusNs7gxbHSWTpwAc18MRXAolRyD/x9 KHxaM76gTLUXbwsQtU4VVHC9Bbe1B05dEhSP0wcTErxGdzmOdauyErGGZNi/yIENpjcr GS5mcAYz5FliR/9cUX2FdYs6igndNfZ2S4ql5b9CBk2K4gfygovFfhGcqB8fFbjJwjBu FKDBTtlIxndAzYjsRtVlNuF7Sit+Yo82+6a4us7WK2tZKW+ZsAnHZwe0U5HmIM7NYLlN dvkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749208679; x=1749813479; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K9UeLBEeNgVFZxNvwdqTWlF8knLYtgrdcWBf2xtn+Ko=; b=vFS3/SLIiz4xUzjGfd//WEtEVLPseunyoahivmSlKY7QZDAKCCGoF5UEeXnH3Zocvh tB9s96zXL/+xE2p4Au7mbbTXun22CatP0KwyzZt+YXdw9x2ZSpnoHdDLBk0zPmCf+5M7 n4BlOHmj+eckf5FaaZwjgAu4rKWq0f5NllqKqADPJEynGg0kDbScPfKjGO2WsTVjkIg9 6/edALO7uLv2NYeyZFr7Flv9SnGm3nsEaUAMLT+ogEv7Rim7f+o+c4X+/JaHWbsg/IA5 rdNe88XEitiXCp/KxOJ3PqGEvtXSJ29VBnP3Eqac+WQyb8mHrO3BKSkGDbIUL2pT6TbF 4dcw== X-Forwarded-Encrypted: i=1; AJvYcCXLqS4tyIXvBL1zC/fLn4xiMQvZz4wl3kV//3L6FNCpg4AZrD/Kj6YEtG57l9DKvn/ADo5FoLdhxw==@vger.kernel.org X-Gm-Message-State: AOJu0YxbEpoXgztone3iwY6WfGPikrXysPWFAAm1FFmXDv3EyvLVSk2s oPvdIkGsm4zlWIZNfWFGIUO4Ucx4nlDylY5h/cYFWCdhVhGLejKjL3gy4kPqr+xywK4= X-Gm-Gg: ASbGncv51jGkuPi5Zgsn0gsDMuJEHurBTMnKMMtw7Z+rLp4JyVVxZxWZjADnUpBy8sU YUa9dJrCOxIAmTNAupoM6/7r+WqSxh4h8nErrq9+iXlbzdfkFXtD3OuI6En2GZqTqLy7mNjSnSu 5/bNxeQ2qkX2mzGpNAEo1L88pLwovBb6uSJaEKuUsc0SHD5JhQiaaxQaxLKTUoAH7wQjXcwwus8 YH2vhMA8DlERoovo5hLl6sCRON437islxx+NtaPTTEFdnBuAPRs6nuOCGhYPstBP427RsPRjQcU x5RO/zuoXh7iQeAfVkK9ncpFLsv+ZGVSzzQ7P72rzQdk3tDWt2mVHum8GRTZ7p55DA4ShTpJtTo nrXms9g== X-Google-Smtp-Source: AGHT+IG4VTq1mx5DeffErAs95tbKPfwNXjlN3PvJEQqp28QpBbWUEU9bT6nIWpBgMvdagmloO4L2qg== X-Received: by 2002:a17:907:d24:b0:adb:41b1:feca with SMTP id a640c23a62f3a-ade1ab99b91mr213537666b.61.1749208678682; Fri, 06 Jun 2025 04:17:58 -0700 (PDT) Received: from claudiu-X670E-Pro-RS.. ([82.78.167.126]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ade1dc7b566sm98704566b.164.2025.06.06.04.17.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Jun 2025 04:17:58 -0700 (PDT) From: Claudiu X-Google-Original-From: Claudiu To: gregkh@linuxfoundation.org, rafael@kernel.org, dakr@kernel.org, len.brown@intel.com, pavel@kernel.org, ulf.hansson@linaro.org, jic23@kernel.org, daniel.lezcano@linaro.org, dmitry.torokhov@gmail.com Cc: claudiu.beznea@tuxon.dev, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, bhelgaas@google.com, geert@linux-m68k.org, linux-iio@vger.kernel.org, linux-renesas-soc@vger.kernel.org, fabrizio.castro.jz@renesas.com, Claudiu Beznea Subject: [PATCH v3 1/2] PM: domains: Add devres variant for dev_pm_domain_attach() Date: Fri, 6 Jun 2025 14:17:48 +0300 Message-ID: <20250606111749.3142348-2-claudiu.beznea.uj@bp.renesas.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250606111749.3142348-1-claudiu.beznea.uj@bp.renesas.com> References: <20250606111749.3142348-1-claudiu.beznea.uj@bp.renesas.com> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Claudiu Beznea The dev_pm_domain_attach() function is typically used in bus code alongside dev_pm_domain_detach(), often following patterns like: static int bus_probe(struct device *_dev) { struct bus_driver *drv = to_bus_driver(dev->driver); struct bus_device *dev = to_bus_device(_dev); int ret; // ... ret = dev_pm_domain_attach(_dev, true); if (ret) return ret; if (drv->probe) ret = drv->probe(dev); // ... } static void bus_remove(struct device *_dev) { struct bus_driver *drv = to_bus_driver(dev->driver); struct bus_device *dev = to_bus_device(_dev); if (drv->remove) drv->remove(dev); dev_pm_domain_detach(_dev); } When the driver's probe function uses devres-managed resources that depend on the power domain state, those resources are released later during device_unbind_cleanup(). Releasing devres-managed resources that depend on the power domain state after detaching the device from its PM domain can cause failures. For example, if the driver uses devm_pm_runtime_enable() in its probe function, and the device's clocks are managed by the PM domain, then during removal the runtime PM is disabled in device_unbind_cleanup() after the clocks have been removed from the PM domain. It may happen that the devm_pm_runtime_enable() action causes the device to be runtime-resumed. If the driver specific runtime PM APIs access registers directly, this will lead to accessing device registers without clocks being enabled. Similar issues may occur with other devres actions that access device registers. Add devm_pm_domain_attach(). When replacing the dev_pm_domain_attach() and dev_pm_domain_detach() in bus probe and bus remove, it ensures that the device is detached from its PM domain in device_unbind_cleanup(), only after all driver's devres-managed resources have been release. For flexibility, the implemented devm_pm_domain_attach() has 2 state arguments, one for the domain state on attach, one for the domain state on detach. Signed-off-by: Claudiu Beznea --- Changes in v3: - dropped devm_pm_domain_detach_off(), devm_pm_domain_detach_on() and use a single function devm_pm_domain_detach() Changes in v2: - none; this patch is new drivers/base/power/common.c | 50 +++++++++++++++++++++++++++++++++++++ include/linux/pm_domain.h | 6 +++++ 2 files changed, 56 insertions(+) diff --git a/drivers/base/power/common.c b/drivers/base/power/common.c index 781968a128ff..82ea20b343f5 100644 --- a/drivers/base/power/common.c +++ b/drivers/base/power/common.c @@ -115,6 +115,56 @@ int dev_pm_domain_attach(struct device *dev, bool power_on) } EXPORT_SYMBOL_GPL(dev_pm_domain_attach); +/** + * devm_pm_domain_detach - devres action for devm_pm_domain_attach() to + * detach a device from its domain. + * @dev: device to detach. + * @res: indicate if the device should be powered off + * + * This function reverse the actions from devm_pm_domain_attach(). + * It will be invoked during the remove phase from drivers implicitly. + */ +static void devm_pm_domain_detach(struct device *dev, void *res) +{ + bool *power_off = res; + + dev_pm_domain_detach(dev, *power_off); +} + +/** + * devm_pm_domain_attach - devres-enabled version of dev_pm_domain_attach() + * @dev: Device to attach. + * @power_on: Use to indicate whether we should power on the device + * when attaching. + * + * NOTE: this will also handle calling dev_pm_domain_detach() for + * you during remove phase. + * + * Returns 0 on successfully attached PM domain, or a negative error code in + * case of a failure. + */ +int devm_pm_domain_attach(struct device *dev, bool power_on) +{ + bool *power_off; + int ret; + + power_off = devres_alloc(devm_pm_domain_detach, sizeof(*power_off), GFP_KERNEL); + if (!power_off) + return -ENOMEM; + + ret = dev_pm_domain_attach(dev, power_on); + if (ret) { + devres_free(power_off); + return ret; + } + + *power_off = power_on; + devres_add(dev, power_off); + + return ret; +} +EXPORT_SYMBOL_GPL(devm_pm_domain_attach); + /** * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains. * @dev: The device used to lookup the PM domain. diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h index 0b18160901a2..f78b6b4dd734 100644 --- a/include/linux/pm_domain.h +++ b/include/linux/pm_domain.h @@ -509,6 +509,7 @@ struct device *dev_pm_domain_attach_by_name(struct device *dev, int dev_pm_domain_attach_list(struct device *dev, const struct dev_pm_domain_attach_data *data, struct dev_pm_domain_list **list); +int devm_pm_domain_attach(struct device *dev, bool power_on); int devm_pm_domain_attach_list(struct device *dev, const struct dev_pm_domain_attach_data *data, struct dev_pm_domain_list **list); @@ -539,6 +540,11 @@ static inline int dev_pm_domain_attach_list(struct device *dev, return 0; } +static inline int devm_pm_domain_attach(struct device *dev, bool power_on) +{ + return 0; +} + static inline int devm_pm_domain_attach_list(struct device *dev, const struct dev_pm_domain_attach_data *data, struct dev_pm_domain_list **list)