Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 39676840 authored by Ingo Molnar's avatar Ingo Molnar
Browse files

x86: mce: Clean up thermal throttling state tracking code



Instead of a mess of three separate percpu variables, consolidate
the state into a single structure.

Also clean up therm_throt_process(), use cleaner and more
understandable variable names and a clearer logic.

This, without changing the logic, makes the code more
streamlined, more readable and smaller as well:

   text	   data	    bss	    dec	    hex	filename
   1487	    169	      4	   1660	    67c	therm_throt.o.before
   1432	    176	      4	   1612	    64c	therm_throt.o.after

Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
LKML-Reference: <new-submission>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent b8c7f1dc
Loading
Loading
Loading
Loading
+39 −24
Original line number Original line Diff line number Diff line
@@ -34,9 +34,17 @@
/* How long to wait between reporting thermal events */
/* How long to wait between reporting thermal events */
#define CHECK_INTERVAL		(300 * HZ)
#define CHECK_INTERVAL		(300 * HZ)


static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
/*
static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
 * Current thermal throttling state:
static DEFINE_PER_CPU(bool, thermal_throttle_active);
 */
struct thermal_state {
	bool			is_throttled;

	u64			next_check;
	unsigned long		throttle_count;
};

static DEFINE_PER_CPU(struct thermal_state, thermal_state);


static atomic_t therm_throt_en	= ATOMIC_INIT(0);
static atomic_t therm_throt_en	= ATOMIC_INIT(0);


@@ -45,7 +53,9 @@ static atomic_t therm_throt_en = ATOMIC_INIT(0);
	static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
	static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)


#define define_therm_throt_sysdev_show_func(name)			\
#define define_therm_throt_sysdev_show_func(name)			\
static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev,	\
									\
static ssize_t therm_throt_sysdev_show_##name(				\
			struct sys_device *dev,				\
			struct sysdev_attribute *attr,			\
			struct sysdev_attribute *attr,			\
			char *buf)					\
			char *buf)					\
{									\
{									\
@@ -55,7 +65,7 @@ static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev, \
	preempt_disable();	/* CPU hotplug */			\
	preempt_disable();	/* CPU hotplug */			\
	if (cpu_online(cpu))						\
	if (cpu_online(cpu))						\
		ret = sprintf(buf, "%lu\n",				\
		ret = sprintf(buf, "%lu\n",				\
			      per_cpu(thermal_throttle_##name, cpu));	\
			      per_cpu(thermal_state, cpu).name);	\
	else								\
	else								\
		ret = 0;						\
		ret = 0;						\
	preempt_enable();						\
	preempt_enable();						\
@@ -63,11 +73,11 @@ static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev, \
	return ret;							\
	return ret;							\
}
}


define_therm_throt_sysdev_show_func(count);
define_therm_throt_sysdev_show_func(throttle_count);
define_therm_throt_sysdev_one_ro(count);
define_therm_throt_sysdev_one_ro(throttle_count);


static struct attribute *thermal_throttle_attrs[] = {
static struct attribute *thermal_throttle_attrs[] = {
	&attr_count.attr,
	&attr_throttle_count.attr,
	NULL
	NULL
};
};


@@ -93,33 +103,38 @@ static struct attribute_group thermal_throttle_attr_group = {
 *          1 : Event should be logged further, and a message has been
 *          1 : Event should be logged further, and a message has been
 *              printed to the syslog.
 *              printed to the syslog.
 */
 */
static int therm_throt_process(int curr)
static int therm_throt_process(bool is_throttled)
{
{
	unsigned int cpu = smp_processor_id();
	struct thermal_state *state;
	__u64 tmp_jiffs = get_jiffies_64();
	unsigned int this_cpu;
	bool was_throttled = __get_cpu_var(thermal_throttle_active);
	bool was_throttled;
	bool is_throttled = __get_cpu_var(thermal_throttle_active) = curr;
	u64 now;

	this_cpu = smp_processor_id();
	now = get_jiffies_64();
	state = &per_cpu(thermal_state, this_cpu);

	was_throttled = state->is_throttled;
	state->is_throttled = is_throttled;


	if (is_throttled)
	if (is_throttled)
		__get_cpu_var(thermal_throttle_count)++;
		state->throttle_count++;


	if (!(was_throttled ^ is_throttled) &&
	if (!(was_throttled ^ is_throttled) &&
	    time_before64(tmp_jiffs, __get_cpu_var(next_check)))
			time_before64(now, state->next_check))
		return 0;
		return 0;


	__get_cpu_var(next_check) = tmp_jiffs + CHECK_INTERVAL;
	state->next_check = now + CHECK_INTERVAL;


	/* if we just entered the thermal event */
	/* if we just entered the thermal event */
	if (is_throttled) {
	if (is_throttled) {
		printk(KERN_CRIT "CPU%d: Temperature above threshold, "
		printk(KERN_CRIT "CPU%d: Temperature above threshold, cpu clock throttled (total events = %lu)\n", this_cpu, state->throttle_count);
		       "cpu clock throttled (total events = %lu)\n",
		       cpu, __get_cpu_var(thermal_throttle_count));


		add_taint(TAINT_MACHINE_CHECK);
		add_taint(TAINT_MACHINE_CHECK);
		return 1;
		return 1;
	}
	}
	if (was_throttled) {
	if (was_throttled) {
		printk(KERN_INFO "CPU%d: Temperature/speed normal\n", cpu);
		printk(KERN_INFO "CPU%d: Temperature/speed normal\n", this_cpu);
		return 1;
		return 1;
	}
	}


@@ -213,7 +228,7 @@ static void intel_thermal_interrupt(void)
	__u64 msr_val;
	__u64 msr_val;


	rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
	rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
	if (therm_throt_process(msr_val & THERM_STATUS_PROCHOT))
	if (therm_throt_process((msr_val & THERM_STATUS_PROCHOT) != 0))
		mce_log_therm_throt_event(msr_val);
		mce_log_therm_throt_event(msr_val);
}
}