[PATCH] hwmon: (amd_energy) Restore visibility of energy counters

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Commit 60268b0e8258 ("hwmon: (amd_energy) modify the visibility of
the counters") restricted visibility of AMD energy counters to work
around a side-channel attack. The attack is described in 'PLATYPUS:
Software-based Power Side-Channel Attacks on x86'. It relies on quick
and accurate power readings.

Limiting power readings to privileged users is annoying. A much better
solution is to make power readings unusable for attacks by randomizing
the time between updates. We can do that by caching energy values for
a short and randomized period of time.

Cc: Naveen Krishna Chatradhi <nchatrad@xxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Fixes: 60268b0e8258 ("hwmon: (amd_energy) modify the visibility of the counters")
Signed-off-by: Guenter Roeck <linux@xxxxxxxxxxxx>
---
 drivers/hwmon/amd_energy.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/drivers/hwmon/amd_energy.c b/drivers/hwmon/amd_energy.c
index a86cc8d6d93d..2ee6919eea57 100644
--- a/drivers/hwmon/amd_energy.c
+++ b/drivers/hwmon/amd_energy.c
@@ -18,6 +18,7 @@
 #include <linux/mutex.h>
 #include <linux/processor.h>
 #include <linux/platform_device.h>
+#include <linux/random.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/topology.h>
@@ -35,6 +36,8 @@
 struct sensor_accumulator {
 	u64 energy_ctr;
 	u64 prev_value;
+	u64 cached_value;
+	unsigned long cache_timeout;
 };
 
 struct amd_energy_data {
@@ -93,6 +96,8 @@ static void accumulate_delta(struct amd_energy_data *data,
 			accum->prev_value + input;
 
 	accum->prev_value = input;
+	accum->cached_value = input;
+	accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;
 	mutex_unlock(&data->lock);
 }
 
@@ -125,16 +130,21 @@ static void amd_add_delta(struct amd_energy_data *data, int ch,
 	u64 input;
 
 	mutex_lock(&data->lock);
-	rdmsrl_safe_on_cpu(cpu, reg, &input);
-	input &= AMD_ENERGY_MASK;
 
 	accum = &data->accums[ch];
-	if (input >= accum->prev_value)
-		input += accum->energy_ctr -
-				accum->prev_value;
-	else
-		input += UINT_MAX - accum->prev_value +
-				accum->energy_ctr;
+	if (!accum->cached_value || time_after(jiffies, accum->cache_timeout)) {
+		rdmsrl_safe_on_cpu(cpu, reg, &input);
+		input &= AMD_ENERGY_MASK;
+
+		if (input >= accum->prev_value)
+			input += accum->energy_ctr - accum->prev_value;
+		else
+			input += UINT_MAX - accum->prev_value + accum->energy_ctr;
+		accum->cached_value = input;
+		accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;
+	} else {
+		input = accum->cached_value;
+	}
 
 	/* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */
 	*val = div64_ul(input * 1000000UL, BIT(data->energy_units));
@@ -171,7 +181,7 @@ static umode_t amd_energy_is_visible(const void *_data,
 				     enum hwmon_sensor_types type,
 				     u32 attr, int channel)
 {
-	return 0440;
+	return 0444;
 }
 
 static int energy_accumulator(void *p)
-- 
2.17.1




[Index of Archives]     [LM Sensors]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux