linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hwmon: (amd_energy) Restore visibility of energy counters
@ 2021-03-31 13:49 Guenter Roeck
  0 siblings, 0 replies; only message in thread
From: Guenter Roeck @ 2021-03-31 13:49 UTC (permalink / raw)
  To: Hardware Monitoring
  Cc: linux-kernel, Jean Delvare, Guenter Roeck,
	Naveen Krishna Chatradhi, Greg Kroah-Hartman

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@amd.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Fixes: 60268b0e8258 ("hwmon: (amd_energy) modify the visibility of the counters")
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-31 13:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31 13:49 [PATCH] hwmon: (amd_energy) Restore visibility of energy counters Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).