Commit cba00d16 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki
Browse files

thermal: core: Add and use thermal zone guard



Add and use a guard for thermal zone locking.

This allows quite a few error code paths to be simplified among
other things and brings in a noticeable code size reduction for
a good measure.

No intentional functional impact.

Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://patch.msgid.link/1930069.tdWV9SEqCh@rjwysocki.net


Reviewed-by: default avatarLukasz Luba <lukasz.luba@arm.com>
parent 17f76be5
Loading
Loading
Loading
Loading
+17 −44
Original line number Diff line number Diff line
@@ -202,16 +202,13 @@ int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
	int ret = -EINVAL;

	mutex_lock(&thermal_governor_lock);
	mutex_lock(&tz->lock);

	gov = __find_governor(strim(policy));
	if (!gov)
		goto exit;
	guard(thermal_zone)(tz);

	gov = __find_governor(strim(policy));
	if (gov)
		ret = thermal_set_governor(tz, gov);

exit:
	mutex_unlock(&tz->lock);
	mutex_unlock(&thermal_governor_lock);

	thermal_notify_tz_gov_change(tz, policy);
@@ -617,26 +614,18 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
{
	int ret;

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	/* do nothing if mode isn't changing */
	if (mode == tz->mode) {
		mutex_unlock(&tz->lock);

	if (mode == tz->mode)
		return 0;
	}

	ret = __thermal_zone_device_set_mode(tz, mode);
	if (ret) {
		mutex_unlock(&tz->lock);

	if (ret)
		return ret;
	}

	__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);

	mutex_unlock(&tz->lock);

	if (mode == THERMAL_DEVICE_ENABLED)
		thermal_notify_tz_enable(tz);
	else
@@ -665,10 +654,10 @@ static bool thermal_zone_is_present(struct thermal_zone_device *tz)
void thermal_zone_device_update(struct thermal_zone_device *tz,
				enum thermal_notify_event event)
{
	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	if (thermal_zone_is_present(tz))
		__thermal_zone_device_update(tz, event);
	mutex_unlock(&tz->lock);
}
EXPORT_SYMBOL_GPL(thermal_zone_device_update);

@@ -972,12 +961,10 @@ static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
				   struct thermal_cooling_device *cdev)
{
	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	if (__thermal_zone_cdev_bind(tz, cdev))
		__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);

	mutex_unlock(&tz->lock);
}

/**
@@ -1284,11 +1271,9 @@ static void __thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
				     struct thermal_cooling_device *cdev)
{
	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	__thermal_zone_cdev_unbind(tz, cdev);

	mutex_unlock(&tz->lock);
}

/**
@@ -1334,7 +1319,7 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
	if (tz->ops.get_crit_temp)
		return tz->ops.get_crit_temp(tz, temp);

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	for_each_trip_desc(tz, td) {
		const struct thermal_trip *trip = &td->trip;
@@ -1346,8 +1331,6 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
		}
	}

	mutex_unlock(&tz->lock);

	return ret;
}
EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
@@ -1360,7 +1343,7 @@ static void thermal_zone_init_complete(struct thermal_zone_device *tz)

	list_add_tail(&tz->node, &thermal_tz_list);

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	/* Bind cooling devices for this zone. */
	list_for_each_entry(cdev, &thermal_cdev_list, node)
@@ -1377,8 +1360,6 @@ static void thermal_zone_init_complete(struct thermal_zone_device *tz)

	__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);

	mutex_unlock(&tz->lock);

	mutex_unlock(&thermal_list_lock);
}

@@ -1615,7 +1596,7 @@ static bool thermal_zone_exit(struct thermal_zone_device *tz)
		goto unlock;
	}

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	tz->state |= TZ_STATE_FLAG_EXIT;
	list_del_init(&tz->node);
@@ -1624,8 +1605,6 @@ static bool thermal_zone_exit(struct thermal_zone_device *tz)
	list_for_each_entry(cdev, &thermal_cdev_list, node)
		__thermal_zone_cdev_unbind(tz, cdev);

	mutex_unlock(&tz->lock);

unlock:
	mutex_unlock(&thermal_list_lock);

@@ -1710,7 +1689,7 @@ static void thermal_zone_device_resume(struct work_struct *work)

	tz = container_of(work, struct thermal_zone_device, poll_queue.work);

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING);

@@ -1720,13 +1699,11 @@ static void thermal_zone_device_resume(struct work_struct *work)
	__thermal_zone_device_update(tz, THERMAL_TZ_RESUME);

	complete(&tz->resume);

	mutex_unlock(&tz->lock);
}

static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
{
	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	if (tz->state & TZ_STATE_FLAG_RESUMING) {
		/*
@@ -1742,13 +1719,11 @@ static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
	}

	tz->state |= TZ_STATE_FLAG_SUSPENDED;

	mutex_unlock(&tz->lock);
}

static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
{
	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	cancel_delayed_work(&tz->poll_queue);

@@ -1762,8 +1737,6 @@ static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
	INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume);
	/* Queue up the work without a delay. */
	mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, 0);

	mutex_unlock(&tz->lock);
}

static int thermal_pm_notify(struct notifier_block *nb,
+4 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
#ifndef __THERMAL_CORE_H__
#define __THERMAL_CORE_H__

#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/thermal.h>

@@ -146,6 +147,9 @@ struct thermal_zone_device {
	struct thermal_trip_desc trips[] __counted_by(num_trips);
};

DEFINE_GUARD(thermal_zone, struct thermal_zone_device *, mutex_lock(&_T->lock),
	     mutex_unlock(&_T->lock))

/* Initial thermal zone temperature. */
#define THERMAL_TEMP_INIT	INT_MIN

+15 −10
Original line number Diff line number Diff line
@@ -885,24 +885,29 @@ void thermal_debug_tz_add(struct thermal_zone_device *tz)
	tz->debugfs = thermal_dbg;
}

void thermal_debug_tz_remove(struct thermal_zone_device *tz)
static struct thermal_debugfs *thermal_debug_tz_clear(struct thermal_zone_device *tz)
{
	struct thermal_debugfs *thermal_dbg;
	struct tz_episode *tze, *tmp;
	struct tz_debugfs *tz_dbg;
	int *trips_crossed;

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	thermal_dbg = tz->debugfs;
	if (!thermal_dbg) {
		mutex_unlock(&tz->lock);
		return;
	if (thermal_dbg)
		tz->debugfs = NULL;

	return thermal_dbg;
}

	tz->debugfs = NULL;
void thermal_debug_tz_remove(struct thermal_zone_device *tz)
{
	struct thermal_debugfs *thermal_dbg;
	struct tz_episode *tze, *tmp;
	struct tz_debugfs *tz_dbg;
	int *trips_crossed;

	mutex_unlock(&tz->lock);
	thermal_dbg = thermal_debug_tz_clear(tz);
	if (!thermal_dbg)
		return;

	tz_dbg = &thermal_dbg->tz_dbg;

+6 −11
Original line number Diff line number Diff line
@@ -60,13 +60,13 @@ bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
{
	bool ret;

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	mutex_lock(&cdev->lock);

	ret = thermal_instance_present(tz, cdev, trip);

	mutex_unlock(&cdev->lock);
	mutex_unlock(&tz->lock);

	return ret;
}
@@ -138,19 +138,14 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
	if (IS_ERR_OR_NULL(tz))
		return -EINVAL;

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	if (!tz->ops.get_temp) {
		ret = -EINVAL;
		goto unlock;
	}
	if (!tz->ops.get_temp)
		return -EINVAL;

	ret = __thermal_zone_get_temp(tz, temp);
	if (!ret && *temp <= THERMAL_TEMP_INVALID)
		ret = -ENODATA;

unlock:
	mutex_unlock(&tz->lock);
		return -ENODATA;

	return ret;
}
+1 −4
Original line number Diff line number Diff line
@@ -78,12 +78,9 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
	int temperature;
	int ret;

	mutex_lock(&tz->lock);
	guard(thermal_zone)(tz);

	ret = tz->ops.get_crit_temp(tz, &temperature);

	mutex_unlock(&tz->lock);

	if (ret)
		return ret;

Loading