Merge branch 'kvm-updates/2.6.29' of git://git.kernel.org/pub/scm/linux/kernel/git...
[safe/jmp/linux-2.6] / arch / x86 / kernel / msr.c
index df85c9c..82a7c7e 100644 (file)
@@ -1,6 +1,6 @@
 /* ----------------------------------------------------------------------- *
- *   
- *   Copyright 2000 H. Peter Anvin - All Rights Reserved
+ *
+ *   Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
@@ -45,9 +45,10 @@ static struct class *msr_class;
 
 static loff_t msr_seek(struct file *file, loff_t offset, int orig)
 {
-       loff_t ret = -EINVAL;
+       loff_t ret;
+       struct inode *inode = file->f_mapping->host;
 
-       lock_kernel();
+       mutex_lock(&inode->i_mutex);
        switch (orig) {
        case 0:
                file->f_pos = offset;
@@ -56,33 +57,43 @@ static loff_t msr_seek(struct file *file, loff_t offset, int orig)
        case 1:
                file->f_pos += offset;
                ret = file->f_pos;
+               break;
+       default:
+               ret = -EINVAL;
        }
-       unlock_kernel();
+       mutex_unlock(&inode->i_mutex);
        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];
        u32 reg = *ppos;
        int cpu = iminor(file->f_path.dentry->d_inode);
-       int err;
+       int err = 0;
+       ssize_t bytes = 0;
 
        if (count % 8)
                return -EINVAL; /* Invalid chunk size */
 
        for (; count; count -= 8) {
                err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
-               if (err)
-                       return -EIO;
-               if (copy_to_user(tmp, &data, 8))
-                       return -EFAULT;
+               if (err) {
+                       if (err == -EFAULT) /* Fix idiotic error code */
+                               err = -EIO;
+                       break;
+               }
+               if (copy_to_user(tmp, &data, 8)) {
+                       err = -EFAULT;
+                       break;
+               }
                tmp += 2;
+               bytes += 8;
        }
 
-       return ((char __user *)tmp) - buf;
+       return bytes ? bytes : err;
 }
 
 static ssize_t msr_write(struct file *file, const char __user *buf,
@@ -92,34 +103,49 @@ static ssize_t msr_write(struct file *file, const char __user *buf,
        u32 data[2];
        u32 reg = *ppos;
        int cpu = iminor(file->f_path.dentry->d_inode);
-       int err;
+       int err = 0;
+       ssize_t bytes = 0;
 
        if (count % 8)
                return -EINVAL; /* Invalid chunk size */
 
        for (; count; count -= 8) {
-               if (copy_from_user(&data, tmp, 8))
-                       return -EFAULT;
+               if (copy_from_user(&data, tmp, 8)) {
+                       err = -EFAULT;
+                       break;
+               }
                err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
-               if (err)
-                       return -EIO;
+               if (err) {
+                       if (err == -EFAULT) /* Fix idiotic error code */
+                               err = -EIO;
+                       break;
+               }
                tmp += 2;
+               bytes += 8;
        }
 
-       return ((char __user *)tmp) - buf;
+       return bytes ? bytes : err;
 }
 
 static int msr_open(struct inode *inode, struct file *file)
 {
        unsigned int cpu = iminor(file->f_path.dentry->d_inode);
-       struct cpuinfo_x86 *c = &(cpu_data)[cpu];
+       struct cpuinfo_x86 *c = &cpu_data(cpu);
+       int ret = 0;
 
-       if (cpu >= NR_CPUS || !cpu_online(cpu))
-               return -ENXIO;  /* No such CPU */
-       if (!cpu_has(c, X86_FEATURE_MSR))
-               return -EIO;    /* MSR not supported */
+       lock_kernel();
+       cpu = iminor(file->f_path.dentry->d_inode);
 
-       return 0;
+       if (cpu >= NR_CPUS || !cpu_online(cpu)) {
+               ret = -ENXIO;   /* No such CPU */
+               goto out;
+       }
+       c = &cpu_data(cpu);
+       if (!cpu_has(c, X86_FEATURE_MSR))
+               ret = -EIO;     /* MSR not supported */
+out:
+       unlock_kernel();
+       return ret;
 }
 
 /*
@@ -133,37 +159,40 @@ static const struct file_operations msr_fops = {
        .open = msr_open,
 };
 
-static int __cpuinit msr_device_create(int i)
+static int __cpuinit msr_device_create(int cpu)
 {
-       int err = 0;
        struct device *dev;
 
-       dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, i), "msr%d",i);
-       if (IS_ERR(dev))
-               err = PTR_ERR(dev);
-       return err;
+       dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
+                           "msr%d", cpu);
+       return IS_ERR(dev) ? PTR_ERR(dev) : 0;
+}
+
+static void msr_device_destroy(int cpu)
+{
+       device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
 }
 
 static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
                                unsigned long action, void *hcpu)
 {
        unsigned int cpu = (unsigned long)hcpu;
+       int err = 0;
 
        switch (action) {
-       case CPU_ONLINE:
-       case CPU_ONLINE_FROZEN:
-               msr_device_create(cpu);
+       case CPU_UP_PREPARE:
+               err = msr_device_create(cpu);
                break;
+       case CPU_UP_CANCELED:
+       case CPU_UP_CANCELED_FROZEN:
        case CPU_DEAD:
-       case CPU_DEAD_FROZEN:
-               device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
+               msr_device_destroy(cpu);
                break;
        }
-       return NOTIFY_OK;
+       return err ? NOTIFY_BAD : NOTIFY_OK;
 }
 
-static struct notifier_block __cpuinitdata msr_class_cpu_notifier =
-{
+static struct notifier_block __refdata msr_class_cpu_notifier = {
        .notifier_call = msr_class_cpu_callback,
 };
 
@@ -196,7 +225,7 @@ static int __init msr_init(void)
 out_class:
        i = 0;
        for_each_online_cpu(i)
-               device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
+               msr_device_destroy(i);
        class_destroy(msr_class);
 out_chrdev:
        unregister_chrdev(MSR_MAJOR, "cpu/msr");
@@ -208,7 +237,7 @@ static void __exit msr_exit(void)
 {
        int cpu = 0;
        for_each_online_cpu(cpu)
-               device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
+               msr_device_destroy(cpu);
        class_destroy(msr_class);
        unregister_chrdev(MSR_MAJOR, "cpu/msr");
        unregister_hotcpu_notifier(&msr_class_cpu_notifier);