From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: linux-kernel@vger.kernel.org, Jean Delvare <jdelvare@suse.com>,
Guenter Roeck <linux@roeck-us.net>,
Naveen Krishna Chatradhi <nchatrad@amd.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH] hwmon: (amd_energy) Restore visibility of energy counters
Date: Wed, 31 Mar 2021 06:49:02 -0700 [thread overview]
Message-ID: <20210331134902.94720-1-linux@roeck-us.net> (raw)
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
reply other threads:[~2021-03-31 13:50 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210331134902.94720-1-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=gregkh@linuxfoundation.org \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nchatrad@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox