Skip to content

Commit

Permalink
This fixes LLNL#37 on github issues, which arose by standardizing the…
Browse files Browse the repository at this point in the history
… format of

the whitelist enumeration and whitelist input from:

MSR: %llx Write Mask: %llx
to
\# Comment in first line
%llx %llx

- Whitelist enumeration will use capital hex letters for MSR offset and write mask
- Use space delimiters instead of tabs in whitelist files
- Update definition of whitelist_format used in msrsave and ignore line if
it begins with a comment
- Allow for comments anywhere in the whitelist file
- Fix potential buffer over-run issue with null terminated string
- Modify msrsave test so that the formatting is consistent
- Fix issues shown by static analysis

Signed-off-by: Christopher M. Cantalupo <[email protected]>
Signed-off-by: Matthias Maiterth <[email protected]>
Signed-off-by: Stephanie Labasan <[email protected]>
  • Loading branch information
Stephanie Labasan committed Dec 4, 2017
1 parent 81c2ea0 commit 9b67496
Show file tree
Hide file tree
Showing 14 changed files with 2,051 additions and 2,019 deletions.
6 changes: 3 additions & 3 deletions msr_whitelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static ssize_t write_whitelist(struct file *file, const char __user *buf, size_t
{
if (find_in_whitelist(entry->msr))
{
pr_err("%s: Duplicate: %llx\n", __FUNCTION__, entry->msr);
pr_err("%s: Duplicate: %llX\n", __FUNCTION__, entry->msr);
err = -EINVAL;
delete_whitelist();
goto out_releasemutex;
Expand Down Expand Up @@ -236,11 +236,11 @@ static ssize_t read_whitelist(struct file *file, char __user *buf, size_t count,

if (idx == 0)
{
len = sprintf(kbuf, "# MSR\t\tWrite Mask\t\t# Comment\n" "0x%08llx\t0x%016llx\n", e.msr, e.wmask);
len = sprintf(kbuf, "# MSR # Write Mask # Comment\n" "0x%08llX 0x%016llX\n", e.msr, e.wmask);
}
else
{
len = sprintf(kbuf, "0x%08llx\t0x%016llx\n", e.msr, e.wmask);
len = sprintf(kbuf, "0x%08llX 0x%016llX\n", e.msr, e.wmask);
}


Expand Down
75 changes: 56 additions & 19 deletions msrsave/msrsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "msrsave.h"

Expand Down Expand Up @@ -144,15 +144,16 @@ static int msr_parse_whitelist(const char *whitelist_path, size_t *num_msr_ptr,
goto exit;
}

/* Allocate buffer for file contents */
whitelist_buffer = (char*)malloc(whitelist_stat.st_size);
/* Allocate buffer for file contents and null terminator */
whitelist_buffer = (char*)malloc(whitelist_stat.st_size + 1);
if (!whitelist_buffer)
{
err = errno ? errno : -1;
snprintf(err_msg, NAME_MAX, "Could not allocate array of size %zu!", whitelist_stat.st_size);
perror(err_msg);
goto exit;
}
whitelist_buffer[whitelist_stat.st_size] = '\0';

/* Open file */
tmp_fd = open(tmp_path, O_RDONLY);
Expand Down Expand Up @@ -185,11 +186,19 @@ static int msr_parse_whitelist(const char *whitelist_path, size_t *num_msr_ptr,
goto exit;
}

/* Count the number of new lines in the file */
/* Count the number of new lines in the file that do not start with # */
whitelist_ptr = whitelist_buffer;
for (num_msr = 0; (whitelist_ptr = strchr(whitelist_ptr, '\n')); ++num_msr)
while (whitelist_ptr && *whitelist_ptr)
{
++whitelist_ptr;
if (*whitelist_ptr != '#')
{
++num_msr;
}
whitelist_ptr = strchr(whitelist_ptr, '\n');
if (whitelist_ptr)
{
++whitelist_ptr;
}
}
*num_msr_ptr = num_msr;

Expand All @@ -213,10 +222,25 @@ static int msr_parse_whitelist(const char *whitelist_path, size_t *num_msr_ptr,
}

/* Parse the whitelist */
const char *whitelist_format = "MSR: %llx Write Mask: %llx\n";
const char *whitelist_format = "%zX %zX\n";
whitelist_ptr = whitelist_buffer;
for (i = 0; i < num_msr; ++i)
{
while (*whitelist_ptr == '#')
{
/* '#' is on first position means line is a comment, treat next line. */
whitelist_ptr = strchr(whitelist_ptr, '\n');
if (whitelist_ptr)
{
whitelist_ptr++; /* Move the pointer to the next line */
}
else
{
err = -1;
fprintf(stderr, "Error: Failed to parse whitelist file named \"%s\"\n", whitelist_path);
goto exit;
}
}
num_scan = sscanf(whitelist_ptr, whitelist_format, msr_offset + i, msr_mask + i);
if (num_scan != 2)
{
Expand Down Expand Up @@ -256,7 +280,7 @@ int msr_save(const char *save_path, const char *whitelist_path, const char *msr_
int err = 0;
int tmp_err = 0;
int i, j;
int msr_fd;
int msr_fd = -1;
char err_msg[NAME_MAX];
size_t num_msr = 0;
uint64_t *msr_offset = NULL;
Expand All @@ -269,6 +293,11 @@ int msr_save(const char *save_path, const char *whitelist_path, const char *msr_
{
goto exit;
}
if (!msr_offset || !msr_mask)
{
err = -1;
goto exit;
}

/* Allocate save buffer, a 2-D array over msr offset and then CPU
(offset major ordering) */
Expand Down Expand Up @@ -303,7 +332,7 @@ int msr_save(const char *save_path, const char *whitelist_path, const char *msr_
if (read_count != sizeof(uint64_t))
{
err = errno ? errno : -1;
snprintf(err_msg, NAME_MAX, "Failed to read msr value from MSR file \"%s\"!", msr_file_name);
snprintf(err_msg, NAME_MAX, "Failed to read msr value 0x%zX from MSR file \"%s\"!", msr_offset[j], msr_file_name);
perror(err_msg);
goto exit;
}
Expand Down Expand Up @@ -380,7 +409,7 @@ int msr_restore(const char *restore_path, const char *whitelist_path, const char
int err = 0;
int tmp_err = 0;
int i, j;
int msr_fd;
int msr_fd = -1;
int do_print_header = 1;
size_t num_msr = 0;
uint64_t read_val = 0;
Expand All @@ -399,6 +428,11 @@ int msr_restore(const char *restore_path, const char *whitelist_path, const char
{
goto exit;
}
if (!msr_offset || !msr_mask)
{
err = -1;
goto exit;
}

/* Check that the timestamp of the restore file is after the timestamp
for the whitelist file */
Expand Down Expand Up @@ -494,20 +528,23 @@ int msr_restore(const char *restore_path, const char *whitelist_path, const char
goto exit;
}
masked_val = (read_val & msr_mask[j]);
if (masked_val != restore_buffer[i * num_msr + j]) {
if (masked_val != restore_buffer[i * num_msr + j])
{
write_val = ((read_val & ~(msr_mask[j])) | restore_buffer[i * num_msr + j]);
count = pwrite(msr_fd, &write_val, sizeof(uint64_t), msr_offset[j]);
if (count != sizeof(uint64_t)) {
if (count != sizeof(uint64_t))
{
err = errno ? errno : -1;
snprintf(err_msg, NAME_MAX, "Failed to write msr value at offset 0x%016xz to MSR file \"%s\"!", msr_offset[j], msr_file_name);
snprintf(err_msg, NAME_MAX, "Failed to write msr value at offset 0x%016zX to MSR file \"%s\"!", msr_offset[j], msr_file_name);
perror(err_msg);
goto exit;
}
if (do_print_header) {
if (do_print_header)
{
printf("offset, read, restored\n");
do_print_header = 0;
}
printf("0x%016zx, 0x%016zx, 0x%016zx\n", msr_offset[j], read_val, write_val);
printf("0x%016zX, 0x%016zX, 0x%016zX\n", msr_offset[j], read_val, write_val);
}
}
tmp_err = close(msr_fd);
Expand Down
79 changes: 37 additions & 42 deletions msrsave/msrsave_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,53 +47,49 @@

int main(int argc, char **argv)
{
const char *usage =
"NAME\n"
" msrsave - msr-safe save and restore application\n"
"\n"
"SYNOPSIS\n"
" SAVE MSR:\n"
" msrsave outfile\n"
"\n"
" RESTORE MSR:\n"
" msrsave -r infile\n"
"\n"
" PRINT VERSION OR HELP:\n"
" msrsave --version | --help\n"
"\n"
"DESCRIPTION\n"
" The msrsave application is used to save to a file the state of all write\n"
" accessible MSR values defined in the current MSR whitelist or restore the MSR\n"
" values from a saved state file. The msrsave application also respects the\n"
" standard --version and --help options for printing the msr-safe pack-\n"
" age version or a brief message about usage.\n"
"\n"
"OPTIONS\n"
" --help\n"
" Print brief summary of the command line usage information, then exit.\n"
"\n"
" --version\n"
" Print version of msr-safe package to standard output, then exit.\n"
"\n"
" -r\n"
" Restore the MSR values that are recorded in an existing MSR saved state\n"
" file.\n"
"\n"
"COPYRIGHT\n"
" Copyright (C) 2016, Intel Corporation. All rights reserved.\n"
"\n"
"\n";
const char *usage = "NAME\n"
" msrsave - msr-safe save and restore application\n"
"\n"
"SYNOPSIS\n"
" SAVE MSR:\n"
" msrsave outfile\n"
"\n"
" RESTORE MSR:\n"
" msrsave -r infile\n"
"\n"
" PRINT VERSION OR HELP:\n"
" msrsave --version | --help\n"
"\n"
"DESCRIPTION\n"
" The msrsave application is used to save to a file the state of all write\n"
" accessible MSR values defined in the current MSR whitelist or restore the MSR\n"
" values from a saved state file. The msrsave application also respects the\n"
" standard --version and --help options for printing the msr-safe pack-\n"
" age version or a brief message about usage.\n"
"\n"
"OPTIONS\n"
" --help\n"
" Print brief summary of the command line usage information, then exit.\n"
"\n"
" --version\n"
" Print version of msr-safe package to standard output, then exit.\n"
"\n"
" -r\n"
" Restore the MSR values that are recorded in an existing MSR saved state\n"
" file.\n"
"\n"
"COPYRIGHT\n"
" Copyright (C) 2016, Intel Corporation. All rights reserved.\n"
"\n"
"\n";

if (argc > 1 &&
strncmp(argv[1], "--version", strlen("--version") + 1) == 0)
if (argc > 1 && strncmp(argv[1], "--version", strlen("--version") + 1) == 0)
{
printf("%s\n", VERSION);
printf("\nCopyright (C) 2016, Intel Corporation. All rights reserved.\n\n");
return 0;
}
if (argc > 1 && (
strncmp(argv[1], "--help", strlen("--help") + 1) == 0 ||
strncmp(argv[1], "-h", strlen("-h") + 1) == 0))
if (argc > 1 && (strncmp(argv[1], "--help", strlen("--help") + 1) == 0 || strncmp(argv[1], "-h", strlen("-h") + 1) == 0))
{
printf(usage, argv[0]);
return 0;
Expand Down Expand Up @@ -143,4 +139,3 @@ int main(int argc, char **argv)

return err;
}

2 changes: 1 addition & 1 deletion msrsave/msrsave_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int main(int argc, char **argv)
const char *test_save_path = "msrsave_test_store";
const char *test_whitelist_path = "msrsave_test_whitelist";
const char *test_msr_path = "msrsave_test_msr.%d";
const char *whitelist_format = "MSR: %.8llx Write Mask: %.16llx\n";
const char *whitelist_format = "0x%.8zX 0x%.16zX\n";
const int num_cpu = 10;
int i;

Expand Down
98 changes: 49 additions & 49 deletions whitelists/wl_062a
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
# MSR Write Mask # Comment
0x000001AD 0xffffffffffffffff # "SMSR_TURBO_RATIO_LIMIT"
0x00000010 0x0000000000000000 # "SMSR_TIME_STAMP_COUNTER"
0x00000017 0x0000000000000000 # "SMSR_PLATFORM_ID"
0x000000C1 0x0000000000000000 # "SMSR_PMC0"
0x000000C2 0x0000000000000000 # "SMSR_PMC1"
0x000000C3 0x0000000000000000 # "SMSR_PMC2"
0x000000C4 0x0000000000000000 # "SMSR_PMC3"
0x000000C5 0x0000000000000000 # "SMSR_PMC4"
0x000000C6 0x0000000000000000 # "SMSR_PMC5"
0x000000C7 0x0000000000000000 # "SMSR_PMC6"
0x000000C8 0x0000000000000000 # "SMSR_PMC7"
0x000000CE 0x0000000000000000 # "SMSR_PLATFORM_INFO"
0x000000E7 0x0000000000000000 # "SMSR_MPERF"
0x000000E8 0x0000000000000000 # "SMSR_APERF"
0x00000186 0x00000000ffffffff # "SMSR_PERFEVTSEL0"
0x00000187 0x00000000ffffffff # "SMSR_PERFEVTSEL1"
0x00000188 0x00000000ffffffff # "SMSR_PERFEVTSEL2"
0x00000189 0x00000000ffffffff # "SMSR_PERFEVTSEL3"
0x0000018A 0x00000000ffffffff # "SMSR_PERFEVTSEL4"
0x0000018B 0x00000000ffffffff # "SMSR_PERFEVTSEL5"
0x0000018C 0x00000000ffffffff # "SMSR_PERFEVTSEL6"
0x0000018D 0x00000000ffffffff # "SMSR_PERFEVTSEL7"
0x00000198 0x0000000000000000 # "SMSR_PERF_STATUS"
0x00000199 0x000000000000ffff # "SMSR_PERF_CTL"
0x0000019A 0x000000000000001f # "SMSR_CLOCK_MODULATION"
0x0000019B 0x0000000001ffff0f # "SMSR_THERM_INTERRUPT"
0x0000019C 0x0000000000000aaa # "SMSR_THERM_STATUS"
0x000001A0 0x0000000000000000 # "SMSR_MISC_ENABLE"
0x000001A2 0x0000000000000000 # "SMSR_TEMPERATURE_TARGET"
0x000001A6 0x0000003fffff8fff # "SMSR_OFFCORE_RSP_0"
0x000001A7 0x0000003fffff8fff # "SMSR_OFFCORE_RSP_1"
0x000001B0 0x000000000000000f # "SMSR_ENERGY_PERF_BIAS"
0x000001B1 0x0000000000000555 # "SMSR_PACKAGE_THERM_STATUS"
0x000001B2 0x0000000001ffff07 # "SMSR_PACKAGE_THERM_INTERRUPT"
0x00000309 0x0000000000000000 # "SMSR_FIXED_CTR0"
0x0000030A 0x0000000000000000 # "SMSR_FIXED_CTR1"
0x0000030B 0x0000000000000000 # "SMSR_FIXED_CTR2"
0x00000345 0x0000000000000000 # "SMSR_PERF_CAPABILITIES"
0x0000038D 0x0000000000000bbb # "SMSR_FIXED_CTR_CTRL"
0x0000038E 0x0000000000000000 # "SMSR_PERF_GLOBAL_STATUS"
0x0000038F 0x0000000700000003 # "SMSR_PERF_GLOBAL_CTRL"
0x00000390 0xc000000700000003 # "SMSR_PERF_GLOBAL_OVF_CTRL"
0x000003F1 0x0000000f0000000f # "SMSR_PEBS_ENABLE"
0x000003F6 0x000000000000ffff # "SMSR_PEBS_LD_LAT"
0x00000606 0x0000000000000000 # "SMSR_RAPL_POWER_UNIT"
0x00000610 0x00ffffff00ffffff # "SMSR_PKG_POWER_LIMIT"
0x00000611 0x0000000000000000 # "SMSR_PKG_ENERGY_STATUS"
0x00000614 0x0000000000000000 # "SMSR_PKG_POWER_INFO"
# MSR # Write Mask # Comment
0x000001AD 0xffffffffffffffff # "SMSR_TURBO_RATIO_LIMIT"
0x00000010 0x0000000000000000 # "SMSR_TIME_STAMP_COUNTER"
0x00000017 0x0000000000000000 # "SMSR_PLATFORM_ID"
0x000000C1 0x0000000000000000 # "SMSR_PMC0"
0x000000C2 0x0000000000000000 # "SMSR_PMC1"
0x000000C3 0x0000000000000000 # "SMSR_PMC2"
0x000000C4 0x0000000000000000 # "SMSR_PMC3"
0x000000C5 0x0000000000000000 # "SMSR_PMC4"
0x000000C6 0x0000000000000000 # "SMSR_PMC5"
0x000000C7 0x0000000000000000 # "SMSR_PMC6"
0x000000C8 0x0000000000000000 # "SMSR_PMC7"
0x000000CE 0x0000000000000000 # "SMSR_PLATFORM_INFO"
0x000000E7 0x0000000000000000 # "SMSR_MPERF"
0x000000E8 0x0000000000000000 # "SMSR_APERF"
0x00000186 0x00000000ffffffff # "SMSR_PERFEVTSEL0"
0x00000187 0x00000000ffffffff # "SMSR_PERFEVTSEL1"
0x00000188 0x00000000ffffffff # "SMSR_PERFEVTSEL2"
0x00000189 0x00000000ffffffff # "SMSR_PERFEVTSEL3"
0x0000018A 0x00000000ffffffff # "SMSR_PERFEVTSEL4"
0x0000018B 0x00000000ffffffff # "SMSR_PERFEVTSEL5"
0x0000018C 0x00000000ffffffff # "SMSR_PERFEVTSEL6"
0x0000018D 0x00000000ffffffff # "SMSR_PERFEVTSEL7"
0x00000198 0x0000000000000000 # "SMSR_PERF_STATUS"
0x00000199 0x000000000000ffff # "SMSR_PERF_CTL"
0x0000019A 0x000000000000001f # "SMSR_CLOCK_MODULATION"
0x0000019B 0x0000000001ffff0f # "SMSR_THERM_INTERRUPT"
0x0000019C 0x0000000000000aaa # "SMSR_THERM_STATUS"
0x000001A0 0x0000000000000000 # "SMSR_MISC_ENABLE"
0x000001A2 0x0000000000000000 # "SMSR_TEMPERATURE_TARGET"
0x000001A6 0x0000003fffff8fff # "SMSR_OFFCORE_RSP_0"
0x000001A7 0x0000003fffff8fff # "SMSR_OFFCORE_RSP_1"
0x000001B0 0x000000000000000f # "SMSR_ENERGY_PERF_BIAS"
0x000001B1 0x0000000000000555 # "SMSR_PACKAGE_THERM_STATUS"
0x000001B2 0x0000000001ffff07 # "SMSR_PACKAGE_THERM_INTERRUPT"
0x00000309 0x0000000000000000 # "SMSR_FIXED_CTR0"
0x0000030A 0x0000000000000000 # "SMSR_FIXED_CTR1"
0x0000030B 0x0000000000000000 # "SMSR_FIXED_CTR2"
0x00000345 0x0000000000000000 # "SMSR_PERF_CAPABILITIES"
0x0000038D 0x0000000000000bbb # "SMSR_FIXED_CTR_CTRL"
0x0000038E 0x0000000000000000 # "SMSR_PERF_GLOBAL_STATUS"
0x0000038F 0x0000000700000003 # "SMSR_PERF_GLOBAL_CTRL"
0x00000390 0xc000000700000003 # "SMSR_PERF_GLOBAL_OVF_CTRL"
0x000003F1 0x0000000f0000000f # "SMSR_PEBS_ENABLE"
0x000003F6 0x000000000000ffff # "SMSR_PEBS_LD_LAT"
0x00000606 0x0000000000000000 # "SMSR_RAPL_POWER_UNIT"
0x00000610 0x00ffffff00ffffff # "SMSR_PKG_POWER_LIMIT"
0x00000611 0x0000000000000000 # "SMSR_PKG_ENERGY_STATUS"
0x00000614 0x0000000000000000 # "SMSR_PKG_POWER_INFO"
Loading

0 comments on commit 9b67496

Please sign in to comment.