Skip to contentSkip to conversation

hwmon: (amd_energy) Use unified function to read energy data

PATCH In review Guenter Roeck 3 participants 8 messages Apr 9, 2021 – Apr 19, 2021 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH v2 1/2 hwmon: (amd_energy) Use unified function to read energy data

The driver implements two separate functions to read and update energy values. One function is called periodically and updates cached enery information to avoid loss of data, the other reads energy data on demand to report it to userspace. The second function reads current energy data, adds the difference to the data previously obtained by the first function, and then discards the updated data.

Simplify the code and always call the first function, then report the energy data obtained by this function to userspace.

Cc Naveen Krishna Chatradhi <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Added patch, simplification

 drivers/hwmon/amd_energy.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
drivers/hwmon/amd_energy.c +6 -25
index a86cc8d6d93d..93bad64039f1 100644
@@ -118,35 +118,12 @@ static void read_accumulate(struct amd_energy_data *data)
 	data->core_id++;
 }
 
-static void amd_add_delta(struct amd_energy_data *data, int ch,
-			  int cpu, long *val, u32 reg)
-{
-	struct sensor_accumulator *accum;
-	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;
-
-	/* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */
-	*val = div64_ul(input * 1000000UL, BIT(data->energy_units));
-
-	mutex_unlock(&data->lock);
-}
-
 static int amd_energy_read(struct device *dev,
 			   enum hwmon_sensor_types type,
 			   u32 attr, int channel, long *val)
 {
 	struct amd_energy_data *data = dev_get_drvdata(dev);
+	struct sensor_accumulator *accum;
 	u32 reg;
 	int cpu;
 
@@ -162,7 +139,11 @@ static int amd_energy_read(struct device *dev,
 
 		reg = ENERGY_CORE_MSR;
 	}
-	amd_add_delta(data, channel, cpu, val, reg);
+
+	accumulate_delta(data, channel, cpu, reg);
+	accum = &data->accums[channel];
+
+	*val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));
 
 	return 0;
 }
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v2 2/2 hwmon: (amd_energy) Restore visibility of energy counters

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

Limiting energy readings to privileged users is annoying. A much better solution is to make energy 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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Simplified code by using unified function to accumulate energy data

 drivers/hwmon/amd_energy.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/hwmon/amd_energy.c +21 -8
index 93bad64039f1..1bf0afc2740c 100644
@@ -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,7 @@
 struct sensor_accumulator {
 	u64 energy_ctr;
 	u64 prev_value;
+	unsigned long cache_timeout;
 };
 
 struct amd_energy_data {
@@ -74,17 +76,14 @@ static void get_energy_units(struct amd_energy_data *data)
 	data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;
 }
 
-static void accumulate_delta(struct amd_energy_data *data,
-			     int channel, int cpu, u32 reg)
+static void __accumulate_delta(struct sensor_accumulator *accum,
+			       int cpu, u32 reg)
 {
-	struct sensor_accumulator *accum;
 	u64 input;
 
-	mutex_lock(&data->lock);
 	rdmsrl_safe_on_cpu(cpu, reg, &input);
 	input &= AMD_ENERGY_MASK;
 
-	accum = &data->accums[channel];
 	if (input >= accum->prev_value)
 		accum->energy_ctr +=
 			input - accum->prev_value;
@@ -93,6 +92,14 @@ static void accumulate_delta(struct amd_energy_data *data,
 			accum->prev_value + input;
 
 	accum->prev_value = input;
+	accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;
+}
+
+static void accumulate_delta(struct amd_energy_data *data,
+			     int channel, int cpu, u32 reg)
+{
+	mutex_lock(&data->lock);
+	__accumulate_delta(&data->accums[channel], cpu, reg);
 	mutex_unlock(&data->lock);
 }
 
@@ -124,6 +131,7 @@ static int amd_energy_read(struct device *dev,
 {
 	struct amd_energy_data *data = dev_get_drvdata(dev);
 	struct sensor_accumulator *accum;
+	u64 energy;
 	u32 reg;
 	int cpu;
 
@@ -140,10 +148,15 @@ static int amd_energy_read(struct device *dev,
 		reg = ENERGY_CORE_MSR;
 	}
 
-	accumulate_delta(data, channel, cpu, reg);
 	accum = &data->accums[channel];
 
-	*val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));
+	mutex_lock(&data->lock);
+	if (!accum->energy_ctr || time_after(jiffies, accum->cache_timeout))
+		__accumulate_delta(accum, cpu, reg);
+	energy = accum->energy_ctr;
+	mutex_unlock(&data->lock);
+
+	*val = div64_ul(energy * 1000000UL, BIT(data->energy_units));
 
 	return 0;
 }
@@ -152,7 +165,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)
Signature · 1 line
2.17.1
Reviewed-by on v2 1/2
#6 lore raw
In reply to Guenter Roeck

Hi Guenter,

Quoted 10 lines · Guenter Roeck

The driver implements two separate functions to read and update energy values. One function is called periodically and updates cached enery information to avoid loss of data, the other reads energy data on demand to report it to userspace. The second function reads current energy data, adds the difference to the data previously obtained by the first function, and then discards the updated data.

Simplify the code and always call the first function, then report the energy data obtained by this function to userspace.

I like the idea.

Quoted 51 lines
Cc Naveen Krishna Chatradhi <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Added patch, simplification

 drivers/hwmon/amd_energy.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
drivers/hwmon/amd_energy.c +1 -24
index a86cc8d6d93d..93bad64039f1 100644
@@ -118,35 +118,12 @@ static void read_accumulate(struct amd_energy_data *data)
 	data->core_id++;
 }
 
-static void amd_add_delta(struct amd_energy_data *data, int ch,
-			  int cpu, long *val, u32 reg)
-{
-	struct sensor_accumulator *accum;
-	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;
-
-	/* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */
-	*val = div64_ul(input * 1000000UL, BIT(data->energy_units));
-
-	mutex_unlock(&data->lock);
-}
-
 static int amd_energy_read(struct device *dev,
 			   enum hwmon_sensor_types type,
 			   u32 attr, int channel, long *val)
 {
 	struct amd_energy_data *data = dev_get_drvdata(dev);
+	struct sensor_accumulator *accum;
 	u32 reg;
 	int cpu;
 

drivers/hwmon/amd_energy.c static int amd_energy_read(struct device *dev, · line 146

Quoted 9 lines
drivers/hwmon/amd_energy.c +5 -1
@@ -162,7 +139,11 @@ static int amd_energy_read(struct device *dev,
 
 		reg = ENERGY_CORE_MSR;
 	}
-	amd_add_delta(data, channel, cpu, val, reg);
+
+	accumulate_delta(data, channel, cpu, reg);
+	accum = &data->accums[channel];
+
+	*val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));

Is it considered safe to read accum->energy_ctr while not holding data->lock?

drivers/hwmon/amd_energy.c static int amd_energy_read(struct device *dev, · line 149

Quoted 3 lines
drivers/hwmon/amd_energy.c +0 -0
 	return 0;
 }

If the answer to the question above is "yes" then:

Signature · 2 lines
Jean Delvare
SUSE L3 Support
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] hwmon: (amd_energy) Restore visibility of energy counters

In reply to Guenter Roeck
Quoted 42 lines · Guenter Roeck

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

Limiting energy readings to privileged users is annoying. A much better solution is to make energy 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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Simplified code by using unified function to accumulate energy data

 drivers/hwmon/amd_energy.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/hwmon/amd_energy.c +2 -0
index 93bad64039f1..1bf0afc2740c 100644
@@ -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,7 @@
 struct sensor_accumulator {
 	u64 energy_ctr;
 	u64 prev_value;
+	unsigned long cache_timeout;
 };
 
 struct amd_energy_data {

drivers/hwmon/amd_energy.c static void get_energy_units(struct amd_energy_data *data) · line 78

Quoted 3 lines
drivers/hwmon/amd_energy.c +0 -0
@@ -74,17 +76,14 @@ static void get_energy_units(struct amd_energy_data *data)
 	data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;
 }
 

Add a comment stating that this function must be called with accum's &data->lock held?

Quoted 17 lines
drivers/hwmon/amd_energy.c +2 -5
-static void accumulate_delta(struct amd_energy_data *data,
-			     int channel, int cpu, u32 reg)
+static void __accumulate_delta(struct sensor_accumulator *accum,
+			       int cpu, u32 reg)
 {
-	struct sensor_accumulator *accum;
 	u64 input;
 
-	mutex_lock(&data->lock);
 	rdmsrl_safe_on_cpu(cpu, reg, &input);
 	input &= AMD_ENERGY_MASK;
 
-	accum = &data->accums[channel];
 	if (input >= accum->prev_value)
 		accum->energy_ctr +=
 			input - accum->prev_value;

drivers/hwmon/amd_energy.c static void accumulate_delta(struct amd_energy_data *data, · line 95

Quoted 4 lines
drivers/hwmon/amd_energy.c +1 -0
@@ -93,6 +92,14 @@ static void accumulate_delta(struct amd_energy_data *data,
 			accum->prev_value + input;
 
 	accum->prev_value = input;
+	accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;

Needs #include <linux/jiffies.h> maybe?

Quoted 37 lines
drivers/hwmon/amd_energy.c +15 -2
+}
+
+static void accumulate_delta(struct amd_energy_data *data,
+			     int channel, int cpu, u32 reg)
+{
+	mutex_lock(&data->lock);
+	__accumulate_delta(&data->accums[channel], cpu, reg);
 	mutex_unlock(&data->lock);
 }
 
@@ -124,6 +131,7 @@ static int amd_energy_read(struct device *dev,
 {
 	struct amd_energy_data *data = dev_get_drvdata(dev);
 	struct sensor_accumulator *accum;
+	u64 energy;
 	u32 reg;
 	int cpu;
 
@@ -140,10 +148,15 @@ static int amd_energy_read(struct device *dev,
 		reg = ENERGY_CORE_MSR;
 	}
 
-	accumulate_delta(data, channel, cpu, reg);
 	accum = &data->accums[channel];
 
-	*val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));
+	mutex_lock(&data->lock);
+	if (!accum->energy_ctr || time_after(jiffies, accum->cache_timeout))
+		__accumulate_delta(accum, cpu, reg);
+	energy = accum->energy_ctr;
+	mutex_unlock(&data->lock);
+
+	*val = div64_ul(energy * 1000000UL, BIT(data->energy_units));
 
 	return 0;
 }

drivers/hwmon/amd_energy.c static umode_t amd_energy_is_visible(const void *_data, · line 171

Quoted 8 lines
drivers/hwmon/amd_energy.c +1 -1
@@ -152,7 +165,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)

Very nice. This will make the driver useful again :-)

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#7 lore raw
In reply to Jean Delvare
Quoted 68 lines · Jean Delvare

Hi Guenter,

Quoted 10 lines · Guenter Roeck

The driver implements two separate functions to read and update energy values. One function is called periodically and updates cached enery information to avoid loss of data, the other reads energy data on demand to report it to userspace. The second function reads current energy data, adds the difference to the data previously obtained by the first function, and then discards the updated data.

Simplify the code and always call the first function, then report the energy data obtained by this function to userspace.

I like the idea.

Quoted 52 lines
Cc Naveen Krishna Chatradhi <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Added patch, simplification

 drivers/hwmon/amd_energy.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
drivers/hwmon/amd_energy.c +1 -24
index a86cc8d6d93d..93bad64039f1 100644
@@ -118,35 +118,12 @@ static void read_accumulate(struct amd_energy_data *data)
 	data->core_id++;
 }
 
-static void amd_add_delta(struct amd_energy_data *data, int ch,
-			  int cpu, long *val, u32 reg)
-{
-	struct sensor_accumulator *accum;
-	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;
-
-	/* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */
-	*val = div64_ul(input * 1000000UL, BIT(data->energy_units));
-
-	mutex_unlock(&data->lock);
-}
-
 static int amd_energy_read(struct device *dev,
 			   enum hwmon_sensor_types type,
 			   u32 attr, int channel, long *val)
 {
 	struct amd_energy_data *data = dev_get_drvdata(dev);
+	struct sensor_accumulator *accum;
 	u32 reg;
 	int cpu;
 
@@ -162,7 +139,11 @@ static int amd_energy_read(struct device *dev,
 
Quoted 12 lines

drivers/hwmon/amd_energy.c static int amd_energy_read(struct device *dev, · line 146

Quoted 8 lines
drivers/hwmon/amd_energy.c +5 -1
 		reg = ENERGY_CORE_MSR;
 	}
-	amd_add_delta(data, channel, cpu, val, reg);
+
+	accumulate_delta(data, channel, cpu, reg);
+	accum = &data->accums[channel];
+
+	*val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));

Is it considered safe to read accum->energy_ctr while not holding data->lock?

You mean because it is a 64-bit value ? Good question; it might not be if compiled on a 32-bit system. Good news is that I moved reading accum->energy_ctr under the lock in the next patch, so we should be good.

Quoted 8 lines

drivers/hwmon/amd_energy.c static int amd_energy_read(struct device *dev, · line 149

Quoted 3 lines
drivers/hwmon/amd_energy.c +0 -0
 	return 0;
 }

If the answer to the question above is "yes" then:

Thanks!

Guenter

author maintainer
#3 lore raw

Re: [PATCH v2 2/2] hwmon: (amd_energy) Restore visibility of energy counters

In reply to Jean Delvare
Quoted 118 lines · Jean Delvare
Quoted 42 lines · Guenter Roeck

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

Limiting energy readings to privileged users is annoying. A much better solution is to make energy 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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Simplified code by using unified function to accumulate energy data

 drivers/hwmon/amd_energy.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/hwmon/amd_energy.c +2 -0
index 93bad64039f1..1bf0afc2740c 100644
@@ -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,7 @@
 struct sensor_accumulator {
 	u64 energy_ctr;
 	u64 prev_value;
+	unsigned long cache_timeout;
 };
 
 struct amd_energy_data {

drivers/hwmon/amd_energy.c static void get_energy_units(struct amd_energy_data *data) · line 78

Quoted 3 lines
drivers/hwmon/amd_energy.c +0 -0
@@ -74,17 +76,14 @@ static void get_energy_units(struct amd_energy_data *data)
 	data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;
 }
 

Add a comment stating that this function must be called with accum's &data->lock held?

Quoted 17 lines
drivers/hwmon/amd_energy.c +2 -5
-static void accumulate_delta(struct amd_energy_data *data,
-			     int channel, int cpu, u32 reg)
+static void __accumulate_delta(struct sensor_accumulator *accum,
+			       int cpu, u32 reg)
 {
-	struct sensor_accumulator *accum;
 	u64 input;
 
-	mutex_lock(&data->lock);
 	rdmsrl_safe_on_cpu(cpu, reg, &input);
 	input &= AMD_ENERGY_MASK;
 
-	accum = &data->accums[channel];
 	if (input >= accum->prev_value)
 		accum->energy_ctr +=
 			input - accum->prev_value;

drivers/hwmon/amd_energy.c static void accumulate_delta(struct amd_energy_data *data, · line 95

Quoted 4 lines
drivers/hwmon/amd_energy.c +1 -0
@@ -93,6 +92,14 @@ static void accumulate_delta(struct amd_energy_data *data,
 			accum->prev_value + input;
 
 	accum->prev_value = input;
+	accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;

Needs #include <linux/jiffies.h> maybe?

Quoted 44 lines
drivers/hwmon/amd_energy.c +16 -3
+}
+
+static void accumulate_delta(struct amd_energy_data *data,
+			     int channel, int cpu, u32 reg)
+{
+	mutex_lock(&data->lock);
+	__accumulate_delta(&data->accums[channel], cpu, reg);
 	mutex_unlock(&data->lock);
 }
 
@@ -124,6 +131,7 @@ static int amd_energy_read(struct device *dev,
 {
 	struct amd_energy_data *data = dev_get_drvdata(dev);
 	struct sensor_accumulator *accum;
+	u64 energy;
 	u32 reg;
 	int cpu;
 
@@ -140,10 +148,15 @@ static int amd_energy_read(struct device *dev,
 		reg = ENERGY_CORE_MSR;
 	}
 
-	accumulate_delta(data, channel, cpu, reg);
 	accum = &data->accums[channel];
 
-	*val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));
+	mutex_lock(&data->lock);
+	if (!accum->energy_ctr || time_after(jiffies, accum->cache_timeout))
+		__accumulate_delta(accum, cpu, reg);
+	energy = accum->energy_ctr;
+	mutex_unlock(&data->lock);
+
+	*val = div64_ul(energy * 1000000UL, BIT(data->energy_units));
 
 	return 0;
 }
@@ -152,7 +165,7 @@ static umode_t amd_energy_is_visible(const void *_data,
 				     enum hwmon_sensor_types type,
 				     u32 attr, int channel)
 {
-	return 0440;
+	return 0444;
 }
 
Quoted 6 lines

drivers/hwmon/amd_energy.c static umode_t amd_energy_is_visible(const void *_data, · line 171

Quoted 1 line
drivers/hwmon/amd_energy.c +0 -0
 static int energy_accumulator(void *p)

Very nice. This will make the driver useful again :-)

I made the suggested changes.

Thanks a lot for the review!

Guenter

#4 lore raw

RE: [PATCH v2 2/2] hwmon: (amd_energy) Restore visibility of energy counters

[AMD Official Use Only - Internal Distribution Only]

Hi Guenter,

Quoted 2 lines
Quoted 2 lines
     accum->prev_value = input;

+ accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;

I've noticed this change is reviewed and accepted, please note “AMD guidance remains to restrict the RAPL MSR access to privilege users for the CVE-2020-12912. See 2020 tab in https://www.amd.com/en/corporate/product-security#paragraph-313561”

Regards,
Naveenk

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Monday, April 12, 2021 7:56 PM
To: Jean Delvare <[email protected]>
Cc: Hardware Monitoring <[email protected]>; Chatradhi, Naveen Krishna <[email protected]>; Greg Kroah-Hartman <[email protected]>
Subject: Re: [PATCH v2 2/2] hwmon: (amd_energy) Restore visibility of energy counters

[CAUTION: External Email]

Quoted 116 lines · Jean Delvare
Quoted 43 lines · Guenter Roeck

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

Limiting energy readings to privileged users is annoying. A much better solution is to make energy 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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Simplified code by using unified function to accumulate energy data

 drivers/hwmon/amd_energy.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/hwmon/amd_energy.c +2 -0
index 93bad64039f1..1bf0afc2740c 100644
@@ -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,7 @@
 struct sensor_accumulator {
     u64 energy_ctr;
     u64 prev_value;
+    unsigned long cache_timeout;
 };
 
 struct amd_energy_data {

drivers/hwmon/amd_energy.c static void get_energy_units(struct amd_energy_data *data) · line 78

Quoted 3 lines
drivers/hwmon/amd_energy.c +0 -0
@@ -74,17 +76,14 @@ static void get_energy_units(struct amd_energy_data *data)
     data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;  
 }
 

Add a comment stating that this function must be called with accum's &data->lock held?

Quoted 17 lines
drivers/hwmon/amd_energy.c +2 -5
-static void accumulate_delta(struct amd_energy_data *data,
-                         int channel, int cpu, u32 reg)
+static void __accumulate_delta(struct sensor_accumulator *accum,
+                           int cpu, u32 reg)
 {
-    struct sensor_accumulator *accum;
     u64 input;
 
-    mutex_lock(&data->lock);
     rdmsrl_safe_on_cpu(cpu, reg, &input);
     input &= AMD_ENERGY_MASK;
 
-    accum = &data->accums[channel];
     if (input >= accum->prev_value)
             accum->energy_ctr +=
                     input - accum->prev_value; @@ -93,6 +92,14 @@ 

static void accumulate_delta(struct amd_energy_data *data,

Quoted 4 lines
                     accum->prev_value + input;

     accum->prev_value = input;

+ accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;

Needs #include <linux/jiffies.h> maybe?

Quoted 41 lines
drivers/hwmon/amd_energy.c +15 -3
+}
+
+static void accumulate_delta(struct amd_energy_data *data,
+                         int channel, int cpu, u32 reg) {
+    mutex_lock(&data->lock);
+    __accumulate_delta(&data->accums[channel], cpu, reg);
     mutex_unlock(&data->lock);
 }
@@ -124,6 +131,7 @@ static int amd_energy_read(struct device *dev,  {
     struct amd_energy_data *data = dev_get_drvdata(dev);
     struct sensor_accumulator *accum;
+    u64 energy;
     u32 reg;
     int cpu;
 
@@ -140,10 +148,15 @@ static int amd_energy_read(struct device *dev,
             reg = ENERGY_CORE_MSR;
     }
 
-    accumulate_delta(data, channel, cpu, reg);
     accum = &data->accums[channel];
 
-    *val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));
+    mutex_lock(&data->lock);
+    if (!accum->energy_ctr || time_after(jiffies, accum->cache_timeout))
+            __accumulate_delta(accum, cpu, reg);
+    energy = accum->energy_ctr;
+    mutex_unlock(&data->lock);
+
+    *val = div64_ul(energy * 1000000UL, BIT(data->energy_units));
 
     return 0;
 }
@@ -152,7 +165,7 @@ static umode_t amd_energy_is_visible(const void *_data,
                                  enum hwmon_sensor_types type,
                                  u32 attr, int channel)  {
-    return 0440;
+    return 0444;
 }
 
Quoted 6 lines

drivers/hwmon/amd_energy.c static umode_t amd_energy_is_visible(const void *_data, · line 171

Quoted 1 line
drivers/hwmon/amd_energy.c +0 -0
 static int energy_accumulator(void *p)

Very nice. This will make the driver useful again :-)

I made the suggested changes.

Thanks a lot for the review!

Guenter

author maintainer
#5 lore raw

Re: [PATCH v2 2/2] hwmon: (amd_energy) Restore visibility of energy counters

Quoted 9 lines · Naveen Krishna

[AMD Official Use Only - Internal Distribution Only]

Hi Guenter,

Quoted 2 lines
Quoted 2 lines
     accum->prev_value = input;

+ accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;

I've noticed this change is reviewed and accepted, please note “AMD guidance remains to restrict the RAPL MSR access to privilege users for the CVE-2020-12912. See 2020 tab in https://www.amd.com/en/corporate/product-security#paragraph-313561”

This has been on the list for a while, so your feedback is a tiny bit late.

Please feel free to send a NACK to the patch. If my suggested solution is not acceptable, I'll remove the driver entirely and add a note to the sysfs ABI stating that attributes MUST be world readable for a driver to be acceptable. After all, this patch forces users of the hwmon ABI to run previously restricted applications as super-user (or to revert the patch introducing the restrictions in their private builds), which completely defeats the purpose of the patch and opens up additional unnecessary attack surface.

Thanks,
Guenter

Quoted 140 lines

Regards,
Naveenk

-----Original Message-----
From: Guenter Roeck <[email protected]> On Behalf Of Guenter Roeck
Sent: Monday, April 12, 2021 7:56 PM
To: Jean Delvare <[email protected]>
Cc: Hardware Monitoring <[email protected]>; Chatradhi, Naveen Krishna <[email protected]>; Greg Kroah-Hartman <[email protected]>
Subject: Re: [PATCH v2 2/2] hwmon: (amd_energy) Restore visibility of energy counters

[CAUTION: External Email]

Quoted 116 lines · Jean Delvare
Quoted 43 lines · Guenter Roeck

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

Limiting energy readings to privileged users is annoying. A much better solution is to make energy 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 <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---
v2: Simplified code by using unified function to accumulate energy data

 drivers/hwmon/amd_energy.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/hwmon/amd_energy.c +2 -0
index 93bad64039f1..1bf0afc2740c 100644
@@ -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,7 @@
 struct sensor_accumulator {
     u64 energy_ctr;
     u64 prev_value;
+    unsigned long cache_timeout;
 };
 
 struct amd_energy_data {

drivers/hwmon/amd_energy.c static void get_energy_units(struct amd_energy_data *data) · line 78

Quoted 3 lines
drivers/hwmon/amd_energy.c +0 -0
@@ -74,17 +76,14 @@ static void get_energy_units(struct amd_energy_data *data)
     data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;  
 }
 

Add a comment stating that this function must be called with accum's &data->lock held?

Quoted 17 lines
drivers/hwmon/amd_energy.c +2 -5
-static void accumulate_delta(struct amd_energy_data *data,
-                         int channel, int cpu, u32 reg)
+static void __accumulate_delta(struct sensor_accumulator *accum,
+                           int cpu, u32 reg)
 {
-    struct sensor_accumulator *accum;
     u64 input;
 
-    mutex_lock(&data->lock);
     rdmsrl_safe_on_cpu(cpu, reg, &input);
     input &= AMD_ENERGY_MASK;
 
-    accum = &data->accums[channel];
     if (input >= accum->prev_value)
             accum->energy_ctr +=
                     input - accum->prev_value; @@ -93,6 +92,14 @@ 

static void accumulate_delta(struct amd_energy_data *data,

Quoted 4 lines
                     accum->prev_value + input;

     accum->prev_value = input;

+ accum->cache_timeout = jiffies + HZ + get_random_int() % HZ;

Needs #include <linux/jiffies.h> maybe?

Quoted 41 lines
drivers/hwmon/amd_energy.c +15 -3
+}
+
+static void accumulate_delta(struct amd_energy_data *data,
+                         int channel, int cpu, u32 reg) {
+    mutex_lock(&data->lock);
+    __accumulate_delta(&data->accums[channel], cpu, reg);
     mutex_unlock(&data->lock);
 }
@@ -124,6 +131,7 @@ static int amd_energy_read(struct device *dev,  {
     struct amd_energy_data *data = dev_get_drvdata(dev);
     struct sensor_accumulator *accum;
+    u64 energy;
     u32 reg;
     int cpu;
 
@@ -140,10 +148,15 @@ static int amd_energy_read(struct device *dev,
             reg = ENERGY_CORE_MSR;
     }
 
-    accumulate_delta(data, channel, cpu, reg);
     accum = &data->accums[channel];
 
-    *val = div64_ul(accum->energy_ctr * 1000000UL, BIT(data->energy_units));
+    mutex_lock(&data->lock);
+    if (!accum->energy_ctr || time_after(jiffies, accum->cache_timeout))
+            __accumulate_delta(accum, cpu, reg);
+    energy = accum->energy_ctr;
+    mutex_unlock(&data->lock);
+
+    *val = div64_ul(energy * 1000000UL, BIT(data->energy_units));
 
     return 0;
 }
@@ -152,7 +165,7 @@ static umode_t amd_energy_is_visible(const void *_data,
                                  enum hwmon_sensor_types type,
                                  u32 attr, int channel)  {
-    return 0440;
+    return 0444;
 }
 
Quoted 6 lines

drivers/hwmon/amd_energy.c static umode_t amd_energy_is_visible(const void *_data, · line 171

Quoted 1 line
drivers/hwmon/amd_energy.c +0 -0
 static int energy_accumulator(void *p)

Very nice. This will make the driver useful again :-)

I made the suggested changes.

Thanks a lot for the review!

Guenter