From patchwork Mon Jan 23 20:44:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rafael J. Wysocki" X-Patchwork-Id: 47377 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1807399wrn; Mon, 23 Jan 2023 12:45:40 -0800 (PST) X-Google-Smtp-Source: AMrXdXvXZLHoJKjGf5n/TTDAsU3A0eSjzYYs6VHNglxP557WxZbfhdkp8P0O59tGDRgRxxacFewo X-Received: by 2002:a05:6402:4cd:b0:46c:8544:42be with SMTP id n13-20020a05640204cd00b0046c854442bemr26116290edw.5.1674506740704; Mon, 23 Jan 2023 12:45:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674506740; cv=none; d=google.com; s=arc-20160816; b=sBlCDcDqAXyPNLJ+aA5nECfibjN4qM5jMvBSm9pHftOkGuBXvQWk5sqYQZMZCxS1XY BMmyWPon8Qpl4pab3xlWb9sNlNc874DNksMWNE1RGC41cN1NCIq8Fbr0jvj1knTWmEyY UA1LRjvJVBBDUzm3a/hav8YKoEiLbfXOXHwTaI9v+PyrG6+bA0UgOrOtZglX6gnl4OMI 3PvpxuhiSiUvZ/lAI8BikynTHYWwzQzPJXpsdgMx2Dte+OHddaspHKCVkBhcQsDUJ64A txS77FPXEhwHmiwF9hHB/t2SypMJ2KzfVh9/Dqlyb2ca0j9X64Rpsz2jFFU9Ix1CE1lL 7vJA== 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=P6tAKmyNGEeFwkFICP7E6QLEtBf4Rpu+YQxNI+5BKrQ=; b=ghhqk95Q8ojw/EVKEBrO5gk5Ndoi1jGSCCln+jjex44lMmhsfgRmR2W6rNIxvpxD7K srtFGwIIP4QrVy+/xXBU4qqd8+nraUr5OmhDW9MhzDyVMF/20kGwQz/npYrlFUr7S48B HsGti25l67WdAwu2KQQ5pVbZ5Z7O81zP4qI4lXI+TAsuxQFZ9S+rGKZGbLsKlfc8Jif6 07GkaJEGi/vwUyUEjEvbVn57zaWAuhBxMGSYYUgUDzYS8zAe3KhNZIsc/m6eGbbuna8l DXrMMgDgrWqPMmV8GWfSvYnSCj7HP4f6AE2dH+ZBCN94xZnEfz3J8AbU9kdH71g/L6gC Su1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x7-20020a05640225c700b0049e24913887si404037edb.356.2023.01.23.12.45.16; Mon, 23 Jan 2023 12:45:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231305AbjAWUo1 (ORCPT + 99 others); Mon, 23 Jan 2023 15:44:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229502AbjAWUoI (ORCPT ); Mon, 23 Jan 2023 15:44:08 -0500 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6B8A4C25; Mon, 23 Jan 2023 12:44:06 -0800 (PST) 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 5.1.0) id 81c69f774a457d50; Mon, 23 Jan 2023 21:44:05 +0100 Received: from kreacher.localnet (unknown [213.134.188.170]) (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 3E48E21322A8; Mon, 23 Jan 2023 21:44:04 +0100 (CET) From: "Rafael J. Wysocki" To: Linux PM , Daniel Lezcano Cc: LKML , Srinivas Pandruvada , Zhang Rui Subject: [PATCH v2] thermal: Fail object registration if thermal class is not registered Date: Mon, 23 Jan 2023 21:44:03 +0100 Message-ID: <5660360.DvuYhMxLoT@kreacher> MIME-Version: 1.0 X-CLIENT-IP: 213.134.188.170 X-CLIENT-HOSTNAME: 213.134.188.170 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvhedruddukedguddvlecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvvefufffkggfgtgesthfuredttddtjeenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpeegfffhudejlefhtdegffekteduhfethffhieettefhkeevgfdvgfefieekiefgheenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecukfhppedvudefrddufeegrddukeekrddujedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepvddufedrudefgedrudekkedrudejtddphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedpnhgspghrtghpthhtohephedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegurghnihgvlhdrlhgviigtrghnoheslhhinhgrrhhordhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepshhrihhnihhvrghs rdhprghnughruhhvrggurgeslhhinhhugidrihhnthgvlhdrtghomhdprhgtphhtthhopehruhhirdiihhgrnhhgsehinhhtvghlrdgtohhm X-DCC--Metrics: v370.home.net.pl 1024; Body=5 Fuz1=5 Fuz2=5 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755847580015703392?= X-GMAIL-MSGID: =?utf-8?q?1755847580015703392?= From: Rafael J. Wysocki If thermal_class is not registered with the driver core, there is no way to expose the interfaces used by the thermal control framework, so prevent thermal zones and cooling devices from being registered in that case by returning an error from object registration functions. For this purpose, use a thermal_class pointer that will be NULL if the class is not registered. To avoid wasting memory in that case, allocate the thermal class object dynamically and if it fails to register, free it and clear the thermal_class pointer to NULL. Signed-off-by: Rafael J. Wysocki Acked-by: Daniel Lezcano Reviewed-by: Zhang Rui Reviewed-by: Greg Kroah-Hartman --- -> v2: This replaces the series at https://lore.kernel.org/linux-pm/5905717.lOV4Wx5bFT@kreacher/ --- drivers/thermal/thermal_core.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) Index: linux-pm/drivers/thermal/thermal_core.c =================================================================== --- linux-pm.orig/drivers/thermal/thermal_core.c +++ linux-pm/drivers/thermal/thermal_core.c @@ -774,10 +774,7 @@ static void thermal_release(struct devic } } -static struct class thermal_class = { - .name = "thermal", - .dev_release = thermal_release, -}; +static struct class *thermal_class; static inline void print_bind_err_msg(struct thermal_zone_device *tz, @@ -880,6 +877,9 @@ __thermal_cooling_device_register(struct !ops->set_cur_state) return ERR_PTR(-EINVAL); + if (!thermal_class) + return ERR_PTR(-ENODEV); + cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); if (!cdev) return ERR_PTR(-ENOMEM); @@ -901,7 +901,7 @@ __thermal_cooling_device_register(struct cdev->np = np; cdev->ops = ops; cdev->updated = false; - cdev->device.class = &thermal_class; + cdev->device.class = thermal_class; cdev->devdata = devdata; ret = cdev->ops->get_max_state(cdev, &cdev->max_state); @@ -1349,6 +1349,9 @@ thermal_zone_device_register_with_trips( if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips) return ERR_PTR(-EINVAL); + if (!thermal_class) + return ERR_PTR(-ENODEV); + tz = kzalloc(sizeof(*tz), GFP_KERNEL); if (!tz) return ERR_PTR(-ENOMEM); @@ -1370,7 +1373,7 @@ thermal_zone_device_register_with_trips( tz->ops = ops; tz->tzp = tzp; - tz->device.class = &thermal_class; + tz->device.class = thermal_class; tz->devdata = devdata; tz->trips = trips; tz->num_trips = num_trips; @@ -1615,9 +1618,21 @@ static int __init thermal_init(void) if (result) goto error; - result = class_register(&thermal_class); - if (result) + thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL); + if (!thermal_class) { + result = -ENOMEM; + goto unregister_governors; + } + + thermal_class->name = "thermal"; + thermal_class->dev_release = thermal_release; + + result = class_register(thermal_class); + if (result) { + kfree(thermal_class); + thermal_class = NULL; goto unregister_governors; + } result = register_pm_notifier(&thermal_pm_nb); if (result)