[v1,1/3] thermal: core: Make thermal_zone_device_unregister() return after freeing the zone
Message ID | 13414639.uLZWGnKmhe@kreacher |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5669554vqy; Fri, 8 Dec 2023 11:21:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZI3vCJTGkTdtXqeNvn3gGgOxDgxlNmlcXFTnUkl21OV8oVtvGmqpobqc4phuJJKIfogmy X-Received: by 2002:a17:90a:6f61:b0:286:c55a:d9e0 with SMTP id d88-20020a17090a6f6100b00286c55ad9e0mr592055pjk.32.1702063298229; Fri, 08 Dec 2023 11:21:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702063298; cv=none; d=google.com; s=arc-20160816; b=umLUdaIIh5henMf+teVIUZI4zWBQ0FEjGyp7FeMfiBppEo9R9G56FnlSAcX8h5qWMS 851yyL5zgSmvRv1otZMcrFegRtmJds36B82YOKENHyKr5U7dwBxF0SGWi+uP2rtsw3lZ Nmc4rrPJCvvU2mcbEqK1BwrmwbXFhLno/YWX5gHn1em6qCDxfQ2ls/zTgVBjfIGrmAFD PV2l8bBJiKOkEbvT0DmwuwymUlVWEPXPac9kK7DJhW/iEI7K0ozBqWCaNvI0DS6OGJPQ 5dsOso+pe4UpbIfs4wu9iKh6jNaXATmOhc19rKQM9hwEQmM91nsu4/rN+KdotxmQWI3r uLTw== 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=ajDTGTVDu2PAGcWQ9jRpIS9Qk3bR3Xt9ElchTqSC6Qk=; fh=PrtSuD01Zw+FPDhsRSNqLGqZbVeJkC2wpKMU0Il46xE=; b=GzY7Zko369Hdf13xSctb8nZG3d70jYf2Pg4Ny6dQdVr0ZFrOeXq4vFIJHZ3RoR/CYx 9Q24e57gZyVzKgmg5eEvJ47Ub4XzLac0aRmhTb6oFk6CI1x39lSIBYOFz+xQfz5hf2xe En3GJgOzIOXIbHdkQ7MnhlQClxpo4UhmxBxaErN57pnnQNsADk211aasrDCrjuk1SIfe OboaC2hURvBnGIlSwvbGWeUf6PoIoOybofEe2W7iIOHMrSa9RRAFyt0w8twFQe8kEsHk 4QJQuvEVl8KWLUJ5o1dEhyIaLmKkO7U4Qi4tq1QrCnp/1SzQOkNQEUvvB3aehDVlDFOv hd0g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id pv14-20020a17090b3c8e00b00286a5319395si2193606pjb.123.2023.12.08.11.21.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 11:21:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id DE79E80A1907; Fri, 8 Dec 2023 11:20:59 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1574632AbjLHTUq (ORCPT <rfc822;ezelljr.billy@gmail.com> + 99 others); Fri, 8 Dec 2023 14:20:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229938AbjLHTUk (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 8 Dec 2023 14:20:40 -0500 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 249C110D2; Fri, 8 Dec 2023 11:20:45 -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.4.0) id 458223ef91cacc58; Fri, 8 Dec 2023 20:20:44 +0100 Received: from kreacher.localnet (unknown [195.136.19.94]) (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 cloudserver094114.home.pl (Postfix) with ESMTPSA id 2E9DA6688FC; Fri, 8 Dec 2023 20:20:44 +0100 (CET) From: "Rafael J. Wysocki" <rjw@rjwysocki.net> To: Linux PM <linux-pm@vger.kernel.org> Cc: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>, Daniel Lezcano <daniel.lezcano@linaro.org>, Zhang Rui <rui.zhang@intel.com>, Linux ACPI <linux-acpi@vger.kernel.org>, LKML <linux-kernel@vger.kernel.org>, Lukasz Luba <lukasz.luba@arm.com> Subject: [PATCH v1 1/3] thermal: core: Make thermal_zone_device_unregister() return after freeing the zone Date: Fri, 08 Dec 2023 20:13:44 +0100 Message-ID: <13414639.uLZWGnKmhe@kreacher> In-Reply-To: <1880915.tdWV9SEqCh@kreacher> References: <1880915.tdWV9SEqCh@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvkedrudekiedguddvhecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvvefufffkjghfggfgtgesthfuredttddtjeenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpedvffeuiedtgfdvtddugeeujedtffetteegfeekffdvfedttddtuefhgeefvdejhfenucfkphepudelhedrudefiedrudelrdelgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeduleehrddufeeirdduledrleegpdhhvghlohepkhhrvggrtghhvghrrdhlohgtrghlnhgvthdpmhgrihhlfhhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqpdhnsggprhgtphhtthhopeejpdhrtghpthhtoheplhhinhhugidqphhmsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepshhrihhnihhvrghsrdhprghnughruhhvrggurgeslhhinhhugidrihhnthgvlhdrtghomhdprhgtphhtthhopegurghnihgvlhdrlhgviigtrghnoheslhhinhgrrhhordhorhhgpdhrtghpthhtoheprhhuihdriihhrghnghesihhnthgvlhdrtghomhdprhgt phhtthhopehlihhnuhigqdgrtghpihesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrgh X-DCC--Metrics: v370.home.net.pl 1024; Body=7 Fuz1=7 Fuz2=7 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 08 Dec 2023 11:21:00 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784742724933899016 X-GMAIL-MSGID: 1784742724933899016 |
Series |
thermal: core: Remove thermal zones during unregistration
|
|
Commit Message
Rafael J. Wysocki
Dec. 8, 2023, 7:13 p.m. UTC
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Make thermal_zone_device_unregister() wait until all of the references to the given thermal zone object have been dropped and free it before returning. This guarantees that when thermal_zone_device_unregister() returns, there is no leftover activity regarding the thermal zone in question which is required by some of its callers (for instance, modular driver code that wants to know when it is safe to let the module go away). Subsequently, this will allow some confusing device_is_registered() checks to be dropped from the thermal sysfs and core code. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> --- drivers/thermal/thermal_core.c | 6 +++++- include/linux/thermal.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-)
Comments
On 08/12/2023 20:13, Rafael J. Wysocki wrote: > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > > Make thermal_zone_device_unregister() wait until all of the references > to the given thermal zone object have been dropped and free it before > returning. > > This guarantees that when thermal_zone_device_unregister() returns, > there is no leftover activity regarding the thermal zone in question > which is required by some of its callers (for instance, modular driver > code that wants to know when it is safe to let the module go away). > > Subsequently, this will allow some confusing device_is_registered() > checks to be dropped from the thermal sysfs and core code. > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > --- Definitively agree on the change Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org> Would it make sense to use kref_get/put ? > drivers/thermal/thermal_core.c | 6 +++++- > include/linux/thermal.h | 2 ++ > 2 files changed, 7 insertions(+), 1 deletion(-) > > Index: linux-pm/drivers/thermal/thermal_core.c > =================================================================== > --- linux-pm.orig/drivers/thermal/thermal_core.c > +++ linux-pm/drivers/thermal/thermal_core.c > @@ -822,7 +822,7 @@ static void thermal_release(struct devic > tz = to_thermal_zone(dev); > thermal_zone_destroy_device_groups(tz); > mutex_destroy(&tz->lock); > - kfree(tz); > + complete(&tz->removal); > } else if (!strncmp(dev_name(dev), "cooling_device", > sizeof("cooling_device") - 1)) { > cdev = to_cooling_device(dev); > @@ -1315,6 +1315,7 @@ thermal_zone_device_register_with_trips( > INIT_LIST_HEAD(&tz->thermal_instances); > ida_init(&tz->ida); > mutex_init(&tz->lock); > + init_completion(&tz->removal); > id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); > if (id < 0) { > result = id; > @@ -1494,6 +1495,9 @@ void thermal_zone_device_unregister(stru > put_device(&tz->device); > > thermal_notify_tz_delete(tz_id); > + > + wait_for_completion(&tz->removal); > + kfree(tz); > } > EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); > > Index: linux-pm/include/linux/thermal.h > =================================================================== > --- linux-pm.orig/include/linux/thermal.h > +++ linux-pm/include/linux/thermal.h > @@ -117,6 +117,7 @@ struct thermal_cooling_device { > * @id: unique id number for each thermal zone > * @type: the thermal zone device type > * @device: &struct device for this thermal zone > + * @removal: removal completion > * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature > * @trip_type_attrs: attributes for trip points for sysfs: trip type > * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis > @@ -156,6 +157,7 @@ struct thermal_zone_device { > int id; > char type[THERMAL_NAME_LENGTH]; > struct device device; > + struct completion removal; > struct attribute_group trips_attribute_group; > struct thermal_attr *trip_temp_attrs; > struct thermal_attr *trip_type_attrs; > > >
On Mon, Dec 11, 2023 at 5:28 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote: > > On 08/12/2023 20:13, Rafael J. Wysocki wrote: > > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > > > > Make thermal_zone_device_unregister() wait until all of the references > > to the given thermal zone object have been dropped and free it before > > returning. > > > > This guarantees that when thermal_zone_device_unregister() returns, > > there is no leftover activity regarding the thermal zone in question > > which is required by some of its callers (for instance, modular driver > > code that wants to know when it is safe to let the module go away). > > > > Subsequently, this will allow some confusing device_is_registered() > > checks to be dropped from the thermal sysfs and core code. > > > > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > > --- > > Definitively agree on the change > > Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org> Thanks! > Would it make sense to use kref_get/put ? Why and where?
On 11/12/2023 17:42, Rafael J. Wysocki wrote: > On Mon, Dec 11, 2023 at 5:28 PM Daniel Lezcano > <daniel.lezcano@linaro.org> wrote: >> >> On 08/12/2023 20:13, Rafael J. Wysocki wrote: >>> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com> >>> >>> Make thermal_zone_device_unregister() wait until all of the references >>> to the given thermal zone object have been dropped and free it before >>> returning. >>> >>> This guarantees that when thermal_zone_device_unregister() returns, >>> there is no leftover activity regarding the thermal zone in question >>> which is required by some of its callers (for instance, modular driver >>> code that wants to know when it is safe to let the module go away). >>> >>> Subsequently, this will allow some confusing device_is_registered() >>> checks to be dropped from the thermal sysfs and core code. >>> >>> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> >>> --- >> >> Definitively agree on the change >> >> Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org> > > Thanks! > >> Would it make sense to use kref_get/put ? > > Why and where? Well it is a general question. Usually this kind of removal is tied with a refcount
On Mon, Dec 11, 2023 at 6:35 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote: > > On 11/12/2023 17:42, Rafael J. Wysocki wrote: > > On Mon, Dec 11, 2023 at 5:28 PM Daniel Lezcano > > <daniel.lezcano@linaro.org> wrote: > >> > >> On 08/12/2023 20:13, Rafael J. Wysocki wrote: > >>> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > >>> > >>> Make thermal_zone_device_unregister() wait until all of the references > >>> to the given thermal zone object have been dropped and free it before > >>> returning. > >>> > >>> This guarantees that when thermal_zone_device_unregister() returns, > >>> there is no leftover activity regarding the thermal zone in question > >>> which is required by some of its callers (for instance, modular driver > >>> code that wants to know when it is safe to let the module go away). > >>> > >>> Subsequently, this will allow some confusing device_is_registered() > >>> checks to be dropped from the thermal sysfs and core code. > >>> > >>> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> > >>> --- > >> > >> Definitively agree on the change > >> > >> Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org> > > > > Thanks! > > > >> Would it make sense to use kref_get/put ? > > > > Why and where? > > Well it is a general question. Usually this kind of removal is tied with > a refcount It is tied to a refcount already, but the problem is that the last reference can be dropped from a thread concurrent to the removal one. The completion effectively causes the removal thread to wait for the refcont to become 0.
Index: linux-pm/drivers/thermal/thermal_core.c =================================================================== --- linux-pm.orig/drivers/thermal/thermal_core.c +++ linux-pm/drivers/thermal/thermal_core.c @@ -822,7 +822,7 @@ static void thermal_release(struct devic tz = to_thermal_zone(dev); thermal_zone_destroy_device_groups(tz); mutex_destroy(&tz->lock); - kfree(tz); + complete(&tz->removal); } else if (!strncmp(dev_name(dev), "cooling_device", sizeof("cooling_device") - 1)) { cdev = to_cooling_device(dev); @@ -1315,6 +1315,7 @@ thermal_zone_device_register_with_trips( INIT_LIST_HEAD(&tz->thermal_instances); ida_init(&tz->ida); mutex_init(&tz->lock); + init_completion(&tz->removal); id = ida_alloc(&thermal_tz_ida, GFP_KERNEL); if (id < 0) { result = id; @@ -1494,6 +1495,9 @@ void thermal_zone_device_unregister(stru put_device(&tz->device); thermal_notify_tz_delete(tz_id); + + wait_for_completion(&tz->removal); + kfree(tz); } EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); Index: linux-pm/include/linux/thermal.h =================================================================== --- linux-pm.orig/include/linux/thermal.h +++ linux-pm/include/linux/thermal.h @@ -117,6 +117,7 @@ struct thermal_cooling_device { * @id: unique id number for each thermal zone * @type: the thermal zone device type * @device: &struct device for this thermal zone + * @removal: removal completion * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature * @trip_type_attrs: attributes for trip points for sysfs: trip type * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis @@ -156,6 +157,7 @@ struct thermal_zone_device { int id; char type[THERMAL_NAME_LENGTH]; struct device device; + struct completion removal; struct attribute_group trips_attribute_group; struct thermal_attr *trip_temp_attrs; struct thermal_attr *trip_type_attrs;