diff --git a/examples/example.c b/examples/example.c index edf2387..e480306 100644 --- a/examples/example.c +++ b/examples/example.c @@ -15,7 +15,6 @@ * */ - #include // printf(3) #include // assert(3) #include // open(2) diff --git a/msr_allowlist.c b/msr_allowlist.c index ea1abbc..790fccb 100644 --- a/msr_allowlist.c +++ b/msr_allowlist.c @@ -40,8 +40,10 @@ static void delete_allowlist(void); static int create_allowlist(int nentries); static struct allowlist_entry *find_in_allowlist(u64 msr); static void add_to_allowlist(struct allowlist_entry *entry); -static int parse_next_allowlist_entry(char *inbuf, char **nextinbuf, struct allowlist_entry *entry); -static ssize_t read_allowlist(struct file *file, char __user *buf, size_t count, loff_t *ppos); +static int parse_next_allowlist_entry(char *inbuf, char **nextinbuf, + struct allowlist_entry *entry); +static ssize_t read_allowlist(struct file *file, char __user *buf, size_t count, + loff_t *ppos); static struct class *cdev_class; static char cdev_created; static char cdev_registered; @@ -85,7 +87,8 @@ static int open_allowlist(struct inode *inode, struct file *file) * valid, we will then delete the current white list and then perform the * second pass to actually create the new white list. */ -static ssize_t write_allowlist(struct file *file, const char __user *buf, size_t count, loff_t *ppos) +static ssize_t write_allowlist(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) { int err = 0; const u32 __user *tmp = (const u32 __user *)buf; @@ -105,16 +108,16 @@ static ssize_t write_allowlist(struct file *file, const char __user *buf, size_t return count; } - if (count+1 > MAX_WLIST_BSIZE) + if (count + 1 > MAX_WLIST_BSIZE) { pr_err("%s: buffer of %zu bytes too large\n", __FUNCTION__, count); return -E2BIG; } - kbuf = kzalloc(count+1, GFP_KERNEL); + kbuf = kzalloc(count + 1, GFP_KERNEL); if (ZERO_OR_NULL_PTR(kbuf)) { - pr_err("%s: memory alloc(%zu) failed\n", __FUNCTION__, count+1); + pr_err("%s: memory alloc(%zu) failed\n", __FUNCTION__, count + 1); return -ENOMEM; } @@ -126,7 +129,7 @@ static ssize_t write_allowlist(struct file *file, const char __user *buf, size_t } /* Pass 1: */ - for (num_entries = 0, s = kbuf, res = 1; res > 0; ) + for (num_entries = 0, s = kbuf, res = 1; res > 0;) { res = parse_next_allowlist_entry(s, &s, 0); if (res < 0) @@ -144,7 +147,8 @@ static ssize_t write_allowlist(struct file *file, const char __user *buf, size_t if (num_entries == 0) { - pr_err("%s: No valid entries found in %zu bytes of input\n", __FUNCTION__, count); + pr_err("%s: No valid entries found in %zu bytes of input\n", __FUNCTION__, + count); err = -ENOMSG; goto out_freebuffer; } @@ -190,7 +194,8 @@ static ssize_t write_allowlist(struct file *file, const char __user *buf, size_t return err ? err : count; } -static ssize_t read_allowlist(struct file *file, char __user *buf, size_t count, loff_t *ppos) +static ssize_t read_allowlist(struct file *file, char __user *buf, size_t count, + loff_t *ppos) { loff_t idx = *ppos; u32 __user *tmp = (u32 __user *) buf; @@ -212,14 +217,14 @@ static ssize_t read_allowlist(struct file *file, char __user *buf, size_t count, if (idx == 0) { - len = sprintf(kbuf, "%-10s %-18s\n" "0x%08llX 0x%016llX\n", "#MSR", "Write mask", e.msr, e.wmask); + len = sprintf(kbuf, "%-10s %-18s\n" "0x%08llX 0x%016llX\n", "#MSR", + "Write mask", e.msr, e.wmask); } else { len = sprintf(kbuf, "0x%08llX 0x%016llX\n", e.msr, e.wmask); } - if (len > count) { return -E2BIG; @@ -230,7 +235,7 @@ static ssize_t read_allowlist(struct file *file, char __user *buf, size_t count, return -EFAULT; } - *ppos = idx+1; + *ppos = idx + 1; return len; } @@ -295,13 +300,13 @@ static struct allowlist_entry *find_in_allowlist(u64 msr) return 0; } - static void add_to_allowlist(struct allowlist_entry *entry) { hash_add(allowlist_hash, &entry->hlist, entry->msr); } -static int parse_next_allowlist_entry(char *inbuf, char **nextinbuf, struct allowlist_entry *entry) +static int parse_next_allowlist_entry(char *inbuf, char **nextinbuf, + struct allowlist_entry *entry) { char *s = skip_spaces(inbuf); int i; @@ -361,7 +366,6 @@ static int parse_next_allowlist_entry(char *inbuf, char **nextinbuf, struct allo return *nextinbuf - inbuf; } - #define msr_allowlist_nodename_selector _Generic(\ (((struct class *)0)->devnode),\ char* (*) (struct device *, mode_t *) : msr_allowlist_nodename1,\ @@ -428,9 +432,9 @@ int msr_allowlist_init(int *majordev) cdev_class = class_create( #if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0) - THIS_MODULE, + THIS_MODULE, #endif - "msr_allowlist"); + "msr_allowlist"); if (IS_ERR(cdev_class)) { err = PTR_ERR(cdev_class); @@ -441,7 +445,8 @@ int msr_allowlist_init(int *majordev) cdev_class->devnode = msr_allowlist_nodename_selector; - dev = device_create(cdev_class, NULL, MKDEV(*majordev, 0), NULL, "msr_allowlist"); + dev = device_create(cdev_class, NULL, MKDEV(*majordev, 0), NULL, + "msr_allowlist"); if (IS_ERR(dev)) { err = PTR_ERR(dev); diff --git a/msr_batch.c b/msr_batch.c index 3fc4082..eec7a82 100644 --- a/msr_batch.c +++ b/msr_batch.c @@ -221,9 +221,9 @@ int msrbatch_init(int *majordev) cdev_class = class_create( #if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0) - THIS_MODULE, + THIS_MODULE, #endif - "msr_batch"); + "msr_batch"); if (IS_ERR(cdev_class)) { err = PTR_ERR(cdev_class); diff --git a/msr_entry.c b/msr_entry.c index bb94196..0007506 100644 --- a/msr_entry.c +++ b/msr_entry.c @@ -94,7 +94,8 @@ static loff_t msr_seek(struct file *file, loff_t offset, int orig) return ret; } -static ssize_t msr_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) +static ssize_t msr_read(struct file *file, char __user *buf, size_t count, + loff_t *ppos) { u32 __user *tmp = (u32 __user *) buf; u32 data[2]; @@ -126,7 +127,8 @@ static ssize_t msr_read(struct file *file, char __user *buf, size_t count, loff_ return 8; } -static ssize_t msr_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) +static ssize_t msr_write(struct file *file, const char __user *buf, + size_t count, loff_t *ppos) { const u32 __user *tmp = (const u32 __user *)buf; u32 curdata[2]; @@ -138,7 +140,7 @@ static ssize_t msr_write(struct file *file, const char __user *buf, size_t count if (count != 8) /* single write only */ { - return -EINVAL; + return -EINVAL; } mask = msr_allowlist_writemask(reg); @@ -211,7 +213,8 @@ static int msr_device_create(unsigned int cpu) { struct device *dev; - dev = device_create(msr_class, NULL, MKDEV(mdev_msr_safe, cpu), NULL, "msr_safe%d", cpu); + dev = device_create(msr_class, NULL, MKDEV(mdev_msr_safe, cpu), NULL, + "msr_safe%d", cpu); return IS_ERR(dev) ? PTR_ERR(dev) : 0; } @@ -229,7 +232,8 @@ static int msr_device_destroy(unsigned int cpu) #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) -static int msr_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) +static int msr_class_cpu_callback(struct notifier_block *nfb, + unsigned long action, void *hcpu) { unsigned int cpu = (unsigned long)hcpu; int err = 0; @@ -317,7 +321,8 @@ static int __init msr_init(void) * number and return zero on success * Return a negative errno on failure */ - err = __register_chrdev(mdev_msr_safe, 0, num_possible_cpus(), "cpu/msr_safe", &msr_fops); + err = __register_chrdev(mdev_msr_safe, 0, num_possible_cpus(), "cpu/msr_safe", + &msr_fops); if (err < 0) { pr_debug("unable to get major %d for msr_safe\n", mdev_msr_safe); @@ -356,7 +361,8 @@ static int __init msr_init(void) } register_hotcpu_notifier(&msr_class_cpu_notifier); #else - err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online", msr_device_create, msr_device_destroy); + err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/msr:online", + msr_device_create, msr_device_destroy); if (err < 0) { goto out_class; diff --git a/msr_version.c b/msr_version.c index 13cc451..31af4bb 100644 --- a/msr_version.c +++ b/msr_version.c @@ -24,11 +24,12 @@ static int open_version(struct inode *inode, struct file *file) return 0; } -static ssize_t read_version(struct file *file, char __user *buf, size_t count, loff_t *ppos) +static ssize_t read_version(struct file *file, char __user *buf, size_t count, + loff_t *ppos) { - size_t len = strlen( THIS_MODULE->version ) + 1 < count ? - strlen( THIS_MODULE->version ) + 1 : - count; + size_t len = strlen(THIS_MODULE->version) + 1 < count ? + strlen(THIS_MODULE->version) + 1 : + count; if (*ppos > 0) { return 0; @@ -128,9 +129,9 @@ int msr_version_init(int *majordev) cdev_class = class_create( #if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0) - THIS_MODULE, + THIS_MODULE, #endif - "msr_safe_version"); + "msr_safe_version"); if (IS_ERR(cdev_class)) { err = PTR_ERR(cdev_class); @@ -141,7 +142,8 @@ int msr_version_init(int *majordev) cdev_class->devnode = msr_version_nodename_selector; - dev = device_create(cdev_class, NULL, MKDEV(*majordev, 0), NULL, "msr_safe_version"); + dev = device_create(cdev_class, NULL, MKDEV(*majordev, 0), NULL, + "msr_safe_version"); if (IS_ERR(dev)) { err = PTR_ERR(dev); diff --git a/msrsave/msrsave.c b/msrsave/msrsave.c index f1603b8..00e4c29 100644 --- a/msrsave/msrsave.c +++ b/msrsave/msrsave.c @@ -35,7 +35,8 @@ static int is_good_value(uint64_t write_val, uint64_t mask) return result; } -static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, uint64_t **msr_offset_ptr, uint64_t **msr_mask_ptr, FILE *error_log) +static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, + uint64_t **msr_offset_ptr, uint64_t **msr_mask_ptr, FILE *error_log) { enum {BUFFER_SIZE = 8192}; int err = 0; @@ -61,7 +62,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (tmp_fd == -1) { err = errno ? errno : -1; - fprintf(error_log, "Creation of temporary file named\"%s\" failed!: %s\n", tmp_path, strerror(errno)); + fprintf(error_log, "Creation of temporary file named\"%s\" failed!: %s\n", + tmp_path, strerror(errno)); goto exit; } @@ -69,7 +71,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (allowlist_fd == -1) { err = errno ? errno : -1; - fprintf(error_log, "Open of allowlist file named \"%s\" failed!: %s\n", allowlist_path, strerror(errno)); + fprintf(error_log, "Open of allowlist file named \"%s\" failed!: %s\n", + allowlist_path, strerror(errno)); goto exit; } @@ -78,7 +81,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (num_read == -1) { err = errno ? errno : -1; - fprintf(error_log, "Read of allowlist file \"%s\" failed!: %s\n", allowlist_path, strerror(errno)); + fprintf(error_log, "Read of allowlist file \"%s\" failed!: %s\n", + allowlist_path, strerror(errno)); goto exit; } @@ -86,7 +90,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (num_write != num_read) { err = errno ? errno : -1; - fprintf(error_log, "Write to temporary file \"%s\" failed!: %s\n", tmp_path, strerror(errno)); + fprintf(error_log, "Write to temporary file \"%s\" failed!: %s\n", tmp_path, + strerror(errno)); goto exit; } } @@ -96,7 +101,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (tmp_err == -1) { err = errno ? errno : -1; - fprintf(error_log, "Close of temporary file named\"%s\" failed!: %s\n", tmp_path, strerror(errno)); + fprintf(error_log, "Close of temporary file named\"%s\" failed!: %s\n", + tmp_path, strerror(errno)); goto exit; } @@ -105,7 +111,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (tmp_err == -1) { err = errno ? errno : -1; - fprintf(error_log, "Close of allowlist file named\"%s\" failed!: %s\n", allowlist_path, strerror(errno)); + fprintf(error_log, "Close of allowlist file named\"%s\" failed!: %s\n", + allowlist_path, strerror(errno)); goto exit; } @@ -124,16 +131,18 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (allowlist_stat.st_size == 0) { err = errno ? errno : -1; - fprintf(error_log, "Approved list file (%s) size is zero!: %s\n", tmp_path, strerror(errno)); + fprintf(error_log, "Approved list file (%s) size is zero!: %s\n", tmp_path, + strerror(errno)); goto exit; } /* Allocate buffer for file contents and null terminator */ - allowlist_buffer = (char*)malloc(allowlist_stat.st_size + 1); + allowlist_buffer = (char *)malloc(allowlist_stat.st_size + 1); if (!allowlist_buffer) { err = errno ? errno : -1; - fprintf(error_log, "Could not allocate array of size %zu!: %s\n", allowlist_stat.st_size, strerror(errno)); + fprintf(error_log, "Could not allocate array of size %zu!: %s\n", + allowlist_stat.st_size, strerror(errno)); goto exit; } allowlist_buffer[allowlist_stat.st_size] = '\0'; @@ -143,7 +152,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (tmp_fd == -1) { err = errno ? errno : -1; - fprintf(error_log, "Could not open allowlist temporary file \"%s\"!: %s\n", tmp_path, strerror(errno)); + fprintf(error_log, "Could not open allowlist temporary file \"%s\"!: %s\n", + tmp_path, strerror(errno)); goto exit; } @@ -152,7 +162,9 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (num_read != allowlist_stat.st_size) { err = errno ? errno : -1; - fprintf(error_log, "Contents read from allowlist file is too small: %zu < %zu!: %s\n", num_read, allowlist_stat.st_size, strerror(errno)); + fprintf(error_log, + "Contents read from allowlist file is too small: %zu < %zu!: %s\n", num_read, + allowlist_stat.st_size, strerror(errno)); goto exit; } @@ -162,7 +174,9 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (tmp_err) { err = errno ? errno : -1; - fprintf(error_log, "Unable to close temporary allowlist file called \"%s\": %s\n", tmp_path, strerror(errno)); + fprintf(error_log, + "Unable to close temporary allowlist file called \"%s\": %s\n", tmp_path, + strerror(errno)); goto exit; } @@ -187,7 +201,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (!msr_offset) { err = errno ? errno : -1; - fprintf(error_log, "Unable to allocate msr offset data of size: %zu!: %s\n", sizeof(uint64_t) * num_msr, strerror(errno)); + fprintf(error_log, "Unable to allocate msr offset data of size: %zu!: %s\n", + sizeof(uint64_t) * num_msr, strerror(errno)); goto exit; } @@ -195,7 +210,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (!msr_mask) { err = errno ? errno : -1; - fprintf(error_log, "Unable to allocate msr mask data of size: %zu!: %s\n", sizeof(uint64_t) * num_msr, strerror(errno)); + fprintf(error_log, "Unable to allocate msr mask data of size: %zu!: %s\n", + sizeof(uint64_t) * num_msr, strerror(errno)); goto exit; } @@ -215,15 +231,18 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, else { err = -1; - fprintf(stderr, "Error: Failed to parse allowlist file named \"%s\"\n", allowlist_path); + fprintf(stderr, "Error: Failed to parse allowlist file named \"%s\"\n", + allowlist_path); goto exit; } } - num_scan = sscanf(allowlist_ptr, allowlist_format, msr_offset + i, msr_mask + i); + num_scan = sscanf(allowlist_ptr, allowlist_format, msr_offset + i, + msr_mask + i); if (num_scan != 2) { err = -1; - fprintf(stderr, "Error: Failed to parse allowlist file named \"%s\"\n", allowlist_path); + fprintf(stderr, "Error: Failed to parse allowlist file named \"%s\"\n", + allowlist_path); goto exit; } allowlist_ptr = strchr(allowlist_ptr, '\n'); @@ -231,7 +250,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, if (!allowlist_ptr) { err = -1; - fprintf(stderr, "Error: Failed to parse allowlist file named \"%s\"\n", allowlist_path); + fprintf(stderr, "Error: Failed to parse allowlist file named \"%s\"\n", + allowlist_path); goto exit; } } @@ -253,7 +273,8 @@ static int msr_parse_allowlist(const char *allowlist_path, size_t *num_msr_ptr, return err; } -int msr_save(const char *save_path, const char *allowlist_path, const char *msr_path_format, int num_cpu, FILE *output_log, FILE *error_log) +int msr_save(const char *save_path, const char *allowlist_path, + const char *msr_path_format, int num_cpu, FILE *output_log, FILE *error_log) { int err = 0; int tmp_err = 0; @@ -265,7 +286,8 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ uint64_t *save_buffer = NULL; FILE *save_fid = NULL; - err = msr_parse_allowlist(allowlist_path, &num_msr, &msr_offset, &msr_mask, error_log); + err = msr_parse_allowlist(allowlist_path, &num_msr, &msr_offset, &msr_mask, + error_log); if (err) { goto exit; @@ -282,7 +304,9 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ if (!save_buffer) { err = errno ? errno : -1; - fprintf(error_log, "Unable to allocate msr save state buffer of size: %zu!: %s\n", num_msr * num_cpu * sizeof(uint64_t), strerror(errno)); + fprintf(error_log, + "Unable to allocate msr save state buffer of size: %zu!: %s\n", + num_msr * num_cpu * sizeof(uint64_t), strerror(errno)); goto exit; } @@ -297,7 +321,8 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ if (msr_fd == -1) { err = errno ? errno : -1; - fprintf(error_log, "Could not open MSR file \"%s\"!: %s\n", msr_file_name, strerror(errno)); + fprintf(error_log, "Could not open MSR file \"%s\"!: %s\n", msr_file_name, + strerror(errno)); goto exit; } for (j = 0; j < num_msr; ++j) @@ -306,7 +331,9 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ sizeof(uint64_t), msr_offset[j]); if (read_count != sizeof(uint64_t)) { - fprintf(error_log, "Warning: Failed to read msr value 0x%zX from MSR file \"%s\"!\n", msr_offset[j], msr_file_name); + fprintf(error_log, + "Warning: Failed to read msr value 0x%zX from MSR file \"%s\"!\n", + msr_offset[j], msr_file_name); errno = 0; if (~(msr_mask[j])) { @@ -323,7 +350,8 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ if (~(msr_mask[j]) == 0ULL && save_buffer[i * num_msr + j] == G_COOKIE_BAD_READ) { - fprintf(error_log, "Error: Extremely unlikely event, read value from MSR that matches our random 64 bit integer used to mark bad reads.\n"); + fprintf(error_log, + "Error: Extremely unlikely event, read value from MSR that matches our random 64 bit integer used to mark bad reads.\n"); err = -1; goto exit; } @@ -334,7 +362,8 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ if (tmp_err) { err = errno ? errno : -1; - fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", msr_file_name, strerror(errno)); + fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", msr_file_name, + strerror(errno)); goto exit; } } @@ -344,15 +373,18 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ if (!save_fid) { err = errno ? errno : -1; - fprintf(error_log, "Could not open output file \"%s\"!: %s\n", save_path, strerror(errno)); + fprintf(error_log, "Could not open output file \"%s\"!: %s\n", save_path, + strerror(errno)); goto exit; } - size_t num_write = fwrite(save_buffer, sizeof(uint64_t), num_msr * num_cpu, save_fid); + size_t num_write = fwrite(save_buffer, sizeof(uint64_t), num_msr * num_cpu, + save_fid); if (num_write != num_msr * num_cpu) { err = errno ? errno : -1; - fprintf(error_log, "Could not write all values to output file \"%s\"!: %s\n", save_path, strerror(errno)); + fprintf(error_log, "Could not write all values to output file \"%s\"!: %s\n", + save_path, strerror(errno)); goto exit; } @@ -361,7 +393,8 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ if (tmp_err) { err = errno ? errno : -1; - fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", save_path, strerror(errno)); + fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", save_path, + strerror(errno)); goto exit; } @@ -390,7 +423,8 @@ int msr_save(const char *save_path, const char *allowlist_path, const char *msr_ return err; } -int msr_restore(const char *restore_path, const char *allowlist_path, const char *msr_path_format, int num_cpu, FILE *output_log, FILE *error_log) +int msr_restore(const char *restore_path, const char *allowlist_path, + const char *msr_path_format, int num_cpu, FILE *output_log, FILE *error_log) { int err = 0; int tmp_err = 0; @@ -409,7 +443,8 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char struct stat allowlist_stat; char err_msg[NAME_MAX]; - err = msr_parse_allowlist(allowlist_path, &num_msr, &msr_offset, &msr_mask, error_log); + err = msr_parse_allowlist(allowlist_path, &num_msr, &msr_offset, &msr_mask, + error_log); if (err) { goto exit; @@ -434,14 +469,16 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char if (tmp_err != 0) { err = errno ? errno : -1; - fprintf(error_log, "stat() of %s failed!: %s\n", allowlist_path, strerror(errno)); + fprintf(error_log, "stat() of %s failed!: %s\n", allowlist_path, + strerror(errno)); goto exit; } if (restore_stat.st_mtime < allowlist_stat.st_mtime) { err = -1; - fprintf(error_log, "Error: allowlist was modified after restore file was written!\n"); + fprintf(error_log, + "Error: allowlist was modified after restore file was written!\n"); goto exit; } @@ -451,7 +488,9 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char if (!restore_buffer) { err = errno ? errno : -1; - fprintf(error_log, "Unable to allocate msr restore state buffer of size: %zu!: %s\n", num_msr * num_cpu * sizeof(uint64_t), strerror(errno)); + fprintf(error_log, + "Unable to allocate msr restore state buffer of size: %zu!: %s\n", + num_msr * num_cpu * sizeof(uint64_t), strerror(errno)); goto exit; } @@ -459,15 +498,18 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char if (!restore_fid) { err = errno ? errno : -1; - fprintf(error_log, "Could not open restore file \"%s\"!: %s\n", restore_path, strerror(errno)); + fprintf(error_log, "Could not open restore file \"%s\"!: %s\n", restore_path, + strerror(errno)); goto exit; } - size_t num_read = fread(restore_buffer, sizeof(uint64_t), num_msr * num_cpu, restore_fid); + size_t num_read = fread(restore_buffer, sizeof(uint64_t), num_msr * num_cpu, + restore_fid); if (num_read != num_msr * num_cpu || fgetc(restore_fid) != EOF) { err = errno ? errno : -1; - fprintf(error_log, "Could not read all values from input file \"%s\"!: %s\n", restore_path, strerror(errno)); + fprintf(error_log, "Could not read all values from input file \"%s\"!: %s\n", + restore_path, strerror(errno)); goto exit; } @@ -476,7 +518,8 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char if (tmp_err) { err = errno ? errno : -1; - fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", restore_path, strerror(errno)); + fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", restore_path, + strerror(errno)); goto exit; } @@ -493,7 +536,8 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char if (msr_fd == -1) { err = errno ? errno : -1; - fprintf(error_log, "Could not open MSR file \"%s\"!: %s\n", msr_file_name, strerror(errno)); + fprintf(error_log, "Could not open MSR file \"%s\"!: %s\n", msr_file_name, + strerror(errno)); goto exit; } for (j = 0; j < num_msr; ++j) @@ -506,7 +550,9 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char masked_val = (read_val & msr_mask[j]); if (count != sizeof(uint64_t)) { - fprintf(error_log, "Warning: Failed to read msr value at offset 0x%016zX from MSR file \"%s\"!: %s\n", msr_offset[j], msr_file_name, strerror(errno)); + fprintf(error_log, + "Warning: Failed to read msr value at offset 0x%016zX from MSR file \"%s\"!: %s\n", + msr_offset[j], msr_file_name, strerror(errno)); errno = 0; } else if (write_val != masked_val) @@ -516,7 +562,9 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char count = pwrite(msr_fd, &write_val, sizeof(uint64_t), msr_offset[j]); if (count != sizeof(uint64_t)) { - fprintf(error_log, "Warning: Failed to write msr value at offset 0x%016zX to MSR file \"%s\"!: %s\n", msr_offset[j], msr_file_name, strerror(errno)); + fprintf(error_log, + "Warning: Failed to write msr value at offset 0x%016zX to MSR file \"%s\"!: %s\n", + msr_offset[j], msr_file_name, strerror(errno)); errno = 0; } else @@ -526,7 +574,8 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char fprintf(output_log, "offset, read, restored\n"); do_print_header = 0; } - fprintf(output_log, "0x%016zX, 0x%016zX, 0x%016zX\n", msr_offset[j], read_val, write_val); + fprintf(output_log, "0x%016zX, 0x%016zX, 0x%016zX\n", msr_offset[j], read_val, + write_val); } } } @@ -536,7 +585,8 @@ int msr_restore(const char *restore_path, const char *allowlist_path, const char if (tmp_err) { err = errno ? errno : -1; - fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", msr_file_name, strerror(errno)); + fprintf(error_log, "Could not close MSR file \"%s\"!: %s\n", msr_file_name, + strerror(errno)); goto exit; } } diff --git a/msrsave/msrsave_main.c b/msrsave/msrsave_main.c index 546bd11..edb7145 100644 --- a/msrsave/msrsave_main.c +++ b/msrsave/msrsave_main.c @@ -60,7 +60,8 @@ int main(int argc, char **argv) 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; @@ -100,11 +101,13 @@ int main(int argc, char **argv) int num_cpu = sysconf(_SC_NPROCESSORS_ONLN); if (do_restore) { - err = msr_restore(file_name, msr_allowlist_path, msr_path, num_cpu, stdout, stderr); + err = msr_restore(file_name, msr_allowlist_path, msr_path, num_cpu, stdout, + stderr); } else { - err = msr_save(file_name, msr_allowlist_path, msr_path, num_cpu, stdout, stderr); + err = msr_save(file_name, msr_allowlist_path, msr_path, num_cpu, stdout, + stderr); } } diff --git a/msrsave/msrsave_test.c b/msrsave/msrsave_test.c index d1fb842..2688c7c 100644 --- a/msrsave/msrsave_test.c +++ b/msrsave/msrsave_test.c @@ -14,10 +14,13 @@ #include "msrsave.h" -void msrsave_test_mock_msr(void *buffer, size_t buffer_size, const char *path_format, int num_cpu); -void msrsave_test_check_msr(uint64_t *buffer, size_t num_check, const char *path_format, int num_cpu); +void msrsave_test_mock_msr(void *buffer, size_t buffer_size, + const char *path_format, int num_cpu); +void msrsave_test_check_msr(uint64_t *buffer, size_t num_check, + const char *path_format, int num_cpu); -void msrsave_test_mock_msr(void *buffer, size_t buffer_size, const char *path_format, int num_cpu) +void msrsave_test_mock_msr(void *buffer, size_t buffer_size, + const char *path_format, int num_cpu) { /* Create mock msr files for each CPU */ int i; @@ -32,7 +35,8 @@ void msrsave_test_mock_msr(void *buffer, size_t buffer_size, const char *path_fo } } -void msrsave_test_check_msr(uint64_t *check_val, size_t num_check, const char *path_format, int num_cpu) +void msrsave_test_check_msr(uint64_t *check_val, size_t num_check, + const char *path_format, int num_cpu) { /* Check that values in the file match expected */ int i, j; @@ -78,29 +82,30 @@ int main(int argc, char **argv) 0x0000000000000090ULL, 0x0000000000000098ULL, 0x1000000000000100ULL /* negative offset should be skipped */ - }; + }; const uint64_t allowlist_mask[] = {0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0x8000000000000000ULL, - 0xFFFFFFFFFFFFFFFFULL, - 0x8000000000000000ULL}; + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0x8000000000000000ULL, + 0xFFFFFFFFFFFFFFFFULL, + 0x8000000000000000ULL + }; enum {NUM_MSR = sizeof(allowlist_off) / sizeof(uint64_t)}; assert(NUM_MSR == sizeof(allowlist_mask) / sizeof(uint64_t)); @@ -136,7 +141,8 @@ int main(int argc, char **argv) const char *log_file_name = "msrsave_test.log"; FILE *log_file = fopen(log_file_name, "w"); /* Save the current state to a file */ - err = msr_save(test_save_path, test_allowlist_path, test_msr_path, num_cpu, log_file, log_file); + err = msr_save(test_save_path, test_allowlist_path, test_msr_path, num_cpu, + log_file, log_file); assert(err == 0); /* Overwrite the mock msr files with new data */ @@ -152,7 +158,8 @@ int main(int argc, char **argv) msrsave_test_mock_msr(msr_val, sizeof(msr_val), test_msr_path, num_cpu); /* Restore to the original values */ - err = msr_restore(test_save_path, test_allowlist_path, test_msr_path, num_cpu, log_file, log_file); + err = msr_restore(test_save_path, test_allowlist_path, test_msr_path, num_cpu, + log_file, log_file); assert(err == 0); /* Check that the values that are writable have been restored. */ @@ -160,16 +167,19 @@ int main(int argc, char **argv) hval = 0x9EADBEEF; for (i = 0; i < NUM_MSR - 1; ++i) { - if (allowlist_mask[i] & 0xFFFFFFFF) { + if (allowlist_mask[i] & 0xFFFFFFFF) + { msr_val[i] = 0xDEADBEEF00000000 | i; } - else { + else + { lval = NUM_MSR - i; msr_val[i] = lval | (hval << 32); } } - msrsave_test_check_msr(msr_val, sizeof(msr_val) / sizeof(uint64_t), test_msr_path, num_cpu); + msrsave_test_check_msr(msr_val, sizeof(msr_val) / sizeof(uint64_t), + test_msr_path, num_cpu); char this_path[NAME_MAX] = {}; for (i = 0; i < num_cpu; ++i) @@ -180,7 +190,8 @@ int main(int argc, char **argv) unlink(test_allowlist_path); unlink(test_save_path); fclose(log_file); - if (!err) { + if (!err) + { unlink(log_file_name); } return err; diff --git a/msrsave/spank_msrsafe.c b/msrsave/spank_msrsafe.c index 7a3bc90..afde37c 100644 --- a/msrsave/spank_msrsafe.c +++ b/msrsave/spank_msrsafe.c @@ -33,7 +33,7 @@ #define SLURM_SPANK_MSRSAFE_BUFFER_SIZE 1024 -SPANK_PLUGIN(msr-safe, 1); +SPANK_PLUGIN(msr - safe, 1); int slurm_spank_init(spank_t spank_ctx, int argc, char **argv); int slurm_spank_slurmd_init(spank_t spank_ctx, int argc, char **argv); @@ -70,24 +70,30 @@ static int slurm_spank_msrsafe_read_log(FILE *log_fid) int err = 0; int character = 0; - if (log_fid == NULL) { + if (log_fid == NULL) + { err = 1; } - else { - do { + else + { + do + { character = fgetc(log_fid); if (character == '\n' || character == EOF || - buffer_pos == SLURM_SPANK_MSRSAFE_BUFFER_SIZE - 1) { + buffer_pos == SLURM_SPANK_MSRSAFE_BUFFER_SIZE - 1) + { buffer[buffer_pos] = '\0'; slurm_info("%s", buffer); buffer_pos = 0; } - else { + else + { buffer[buffer_pos] = character; ++buffer_pos; } - } while (character != EOF); + } + while (character != EOF); } return err; } @@ -121,35 +127,47 @@ int slurm_spank_job_prolog(spank_t spank_ctx, int argc, char **argv) char hostname[NAME_MAX]; hostname[NAME_MAX - 1] = '\0'; err = gethostname(hostname, NAME_MAX - 1); - if (err) { + if (err) + { slurm_info("gethostname failed."); } - if (!err) { - snprintf(out_log_name, NAME_MAX * 2, "/tmp/slurm-msrsave-outlog-%s.XXXXXXXXXX", hostname); - err = mkstemp(out_log_name); - if (err) { - slurm_info("failed to create msrsave output log"); - } + if (!err) + { + snprintf(out_log_name, NAME_MAX * 2, "/tmp/slurm-msrsave-outlog-%s.XXXXXXXXXX", + hostname); + err = mkstemp(out_log_name); + if (err) + { + slurm_info("failed to create msrsave output log"); + } } - if (!err) { + if (!err) + { out_log = fopen(out_log_name, "w+"); - if (out_log == NULL) { - slurm_info("failed to open %s for writing", out_log_name); - } + if (out_log == NULL) + { + slurm_info("failed to open %s for writing", out_log_name); + } } - if (!err) { - snprintf(msrsave_file, NAME_MAX * 2, "%s-%s", SLURM_SPANK_MSRSAVE_FILE_PREFIX, hostname); - err = msr_save(msrsave_file, allowlist_path, msr_path, num_cpu, out_log, out_log); - if (err) { + if (!err) + { + snprintf(msrsave_file, NAME_MAX * 2, "%s-%s", SLURM_SPANK_MSRSAVE_FILE_PREFIX, + hostname); + err = msr_save(msrsave_file, allowlist_path, msr_path, num_cpu, out_log, + out_log); + if (err) + { slurm_info("msr_save failed:"); - } + } rewind(out_log); slurm_spank_msrsafe_read_log(out_log); } - if (!err) { + if (!err) + { slurm_info("Completed msr-safe plugin to save register values."); } - if (out_log) { + if (out_log) + { fclose(out_log); unlink(out_log_name); } @@ -169,35 +187,47 @@ int slurm_spank_job_epilog(spank_t spank_ctx, int argc, char **argv) char hostname[NAME_MAX]; hostname[NAME_MAX - 1] = '\0'; err = gethostname(hostname, NAME_MAX - 1); - if (err) { + if (err) + { slurm_info("gethostname failed."); } - if (!err) { - snprintf(out_log_name, NAME_MAX * 2, "/tmp/slurm-msrsave-outlog-%s.XXXXXXXXXX", hostname); - err = mkstemp(out_log_name); - if (err) { - slurm_info("failed to create msrsave output log"); - } + if (!err) + { + snprintf(out_log_name, NAME_MAX * 2, "/tmp/slurm-msrsave-outlog-%s.XXXXXXXXXX", + hostname); + err = mkstemp(out_log_name); + if (err) + { + slurm_info("failed to create msrsave output log"); + } } - if (!err) { + if (!err) + { out_log = fopen(out_log_name, "w+"); - if (out_log == NULL) { - slurm_info("failed to open %s for writing", out_log_name); - } + if (out_log == NULL) + { + slurm_info("failed to open %s for writing", out_log_name); + } } - if (!err) { - snprintf(msrsave_file, NAME_MAX * 2, "%s-%s", SLURM_SPANK_MSRSAVE_FILE_PREFIX, hostname); - err = msr_restore(msrsave_file, allowlist_path, msr_path, num_cpu, out_log, out_log); - if (err) { + if (!err) + { + snprintf(msrsave_file, NAME_MAX * 2, "%s-%s", SLURM_SPANK_MSRSAVE_FILE_PREFIX, + hostname); + err = msr_restore(msrsave_file, allowlist_path, msr_path, num_cpu, out_log, + out_log); + if (err) + { slurm_info("msr_restore failed:"); - } + } rewind(out_log); slurm_spank_msrsafe_read_log(out_log); } - if (!err) { + if (!err) + { slurm_info("Completed msr-safe plugin to restore register values."); } - if (out_log) { + if (out_log) + { fclose(out_log); unlink(out_log_name); }