Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Add driver auto probing for x86 features v4

There's a growing number of drivers that support a specific x86 feature
or CPU. Currently loading these drivers currently on a generic
distribution requires various driver specific hacks and it often
doesn't work.

This patch adds auto probing for drivers based on the x86 cpuid
information, in particular based on vendor/family/model number
and also based on CPUID feature bits.

For example a common issue is not loading the SSE 4.2 accelerated
CRC module: this can significantly lower the performance of BTRFS
which relies on fast CRC.

Another issue is loading the right CPUFREQ driver for the current CPU.
Currently distributions often try all all possible driver until
one sticks, which is not really a good way to do this.

It works with existing udev without any changes. The code
exports the x86 information as a generic string in sysfs
that can be matched by udev's pattern matching.

This scheme does not support numeric ranges, so if you want to
handle e.g. ranges of model numbers they have to be encoded
in ASCII or simply all models or families listed. Fixing
that would require changing udev.

Another issue is that udev will happily load all drivers that match,
there is currently no nice way to stop a specific driver from
being loaded if it's not needed (e.g. if you don't need fast CRC)
But there are not that many cpu specific drivers around and they're
all not that bloated, so this isn't a particularly serious issue.

Originally this patch added the modalias to the normal cpu
sysdevs. However sysdevs don't have all the infrastructure
needed for udev, so it couldn't really autoload drivers.
This patch instead adds the CPU modaliases to the cpuid devices,
which are real devices with full support for udev. This implies
that the cpuid driver has to be loaded to use this.

This patch just adds infrastructure, some driver conversions
in followups.

Thanks to Kay for helping with some sysfs magic.

v2: Constifcation, some updates
v4: (trenn@suse.de):
- Use kzalloc instead of kmalloc to terminate modalias buffer
- Use uppercase hex values to match correctly against hex values containing
letters

Cc: Dave Jones <davej@redhat.com>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Jen Axboe <axboe@kernel.dk>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Huang Ying <ying.huang@intel.com>
Cc: Len Brown <lenb@kernel.org>
Signed-off-by: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Thomas Renninger <trenn@suse.de>
Acked-by: H. Peter Anvin <hpa@zytor.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Andi Kleen and committed by
Greg Kroah-Hartman
644e9cbb d6e48686

+165 -1
+13
arch/x86/include/asm/cpu_device_id.h
··· 1 + #ifndef _CPU_DEVICE_ID 2 + #define _CPU_DEVICE_ID 1 3 + 4 + /* 5 + * Declare drivers belonging to specific x86 CPUs 6 + * Similar in spirit to pci_device_id and related PCI functions 7 + */ 8 + 9 + #include <linux/mod_devicetable.h> 10 + 11 + extern const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match); 12 + 13 + #endif
+1
arch/x86/kernel/cpu/Makefile
··· 16 16 obj-y += proc.o capflags.o powerflags.o common.o 17 17 obj-y += vmware.o hypervisor.o sched.o mshyperv.o 18 18 obj-y += rdrand.o 19 + obj-y += match.o 19 20 20 21 obj-$(CONFIG_X86_32) += bugs.o 21 22 obj-$(CONFIG_X86_64) += bugs_64.o
+48
arch/x86/kernel/cpu/match.c
··· 1 + #include <asm/cpu_device_id.h> 2 + #include <asm/processor.h> 3 + #include <linux/cpu.h> 4 + #include <linux/module.h> 5 + 6 + /** 7 + * x86_match_cpu - match current CPU again an array of x86_cpu_ids 8 + * @match: Pointer to array of x86_cpu_ids. Last entry terminated with 9 + * {}. 10 + * 11 + * Return the entry if the current CPU matches the entries in the 12 + * passed x86_cpu_id match table. Otherwise NULL. The match table 13 + * contains vendor (X86_VENDOR_*), family, model and feature bits or 14 + * respective wildcard entries. 15 + * 16 + * A typical table entry would be to match a specific CPU 17 + * { X86_VENDOR_INTEL, 6, 0x12 } 18 + * or to match a specific CPU feature 19 + * { X86_FEATURE_MATCH(X86_FEATURE_FOOBAR) } 20 + * 21 + * Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY, 22 + * %X86_MODEL_ANY, %X86_FEATURE_ANY or 0 (except for vendor) 23 + * 24 + * Arrays used to match for this should also be declared using 25 + * MODULE_DEVICE_TABLE(x86_cpu, ...) 26 + * 27 + * This always matches against the boot cpu, assuming models and features are 28 + * consistent over all CPUs. 29 + */ 30 + const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match) 31 + { 32 + const struct x86_cpu_id *m; 33 + struct cpuinfo_x86 *c = &boot_cpu_data; 34 + 35 + for (m = match; m->vendor | m->family | m->model | m->feature; m++) { 36 + if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor) 37 + continue; 38 + if (m->family != X86_FAMILY_ANY && c->x86 != m->family) 39 + continue; 40 + if (m->model != X86_MODEL_ANY && c->x86_model != m->model) 41 + continue; 42 + if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature)) 43 + continue; 44 + return m; 45 + } 46 + return NULL; 47 + } 48 + EXPORT_SYMBOL(x86_match_cpu);
+58 -1
arch/x86/kernel/cpuid.c
··· 40 40 #include <linux/notifier.h> 41 41 #include <linux/uaccess.h> 42 42 #include <linux/gfp.h> 43 + #include <linux/slab.h> 43 44 44 45 #include <asm/processor.h> 45 46 #include <asm/msr.h> ··· 139 138 .open = cpuid_open, 140 139 }; 141 140 141 + static ssize_t print_cpu_modalias(struct device *dev, 142 + struct device_attribute *attr, 143 + char *bufptr) 144 + { 145 + int size = PAGE_SIZE; 146 + int i, n; 147 + char *buf = bufptr; 148 + 149 + n = snprintf(buf, size, "x86cpu:vendor:%04X:family:" 150 + "%04X:model:%04X:feature:", 151 + boot_cpu_data.x86_vendor, 152 + boot_cpu_data.x86, 153 + boot_cpu_data.x86_model); 154 + size -= n; 155 + buf += n; 156 + size -= 2; 157 + for (i = 0; i < NCAPINTS*32; i++) { 158 + if (boot_cpu_has(i)) { 159 + n = snprintf(buf, size, ",%04X", i); 160 + if (n < 0) { 161 + WARN(1, "x86 features overflow page\n"); 162 + break; 163 + } 164 + size -= n; 165 + buf += n; 166 + } 167 + } 168 + *buf++ = ','; 169 + *buf++ = '\n'; 170 + return buf - bufptr; 171 + } 172 + 173 + static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL); 174 + 142 175 static __cpuinit int cpuid_device_create(int cpu) 143 176 { 144 177 struct device *dev; 178 + int err; 145 179 146 180 dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL, 147 181 "cpu%d", cpu); 148 - return IS_ERR(dev) ? PTR_ERR(dev) : 0; 182 + if (IS_ERR(dev)) 183 + return PTR_ERR(dev); 184 + 185 + err = device_create_file(dev, &dev_attr_modalias); 186 + if (err) { 187 + /* keep device around on error. attribute is optional. */ 188 + err = 0; 189 + } 190 + 191 + return 0; 149 192 } 150 193 151 194 static void cpuid_device_destroy(int cpu) ··· 227 182 return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt)); 228 183 } 229 184 185 + static int cpuid_dev_uevent(struct device *dev, struct kobj_uevent_env *env) 186 + { 187 + char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); 188 + if (buf) { 189 + print_cpu_modalias(NULL, NULL, buf); 190 + add_uevent_var(env, "MODALIAS=%s", buf); 191 + kfree(buf); 192 + } 193 + return 0; 194 + } 195 + 230 196 static int __init cpuid_init(void) 231 197 { 232 198 int i, err = 0; ··· 256 200 goto out_chrdev; 257 201 } 258 202 cpuid_class->devnode = cpuid_devnode; 203 + cpuid_class->dev_uevent = cpuid_dev_uevent; 259 204 for_each_online_cpu(i) { 260 205 err = cpuid_device_create(i); 261 206 if (err != 0)
+21
include/linux/mod_devicetable.h
··· 571 571 #endif 572 572 }; 573 573 574 + /* 575 + * Match x86 CPUs for CPU specific drivers. 576 + * See documentation of "x86_match_cpu" for details. 577 + */ 578 + 579 + struct x86_cpu_id { 580 + __u16 vendor; 581 + __u16 family; 582 + __u16 model; 583 + __u16 feature; /* bit index */ 584 + kernel_ulong_t driver_data; 585 + }; 586 + 587 + #define X86_FEATURE_MATCH(x) \ 588 + { X86_VENDOR_ANY, X86_FAMILY_ANY, X86_MODEL_ANY, x } 589 + 590 + #define X86_VENDOR_ANY 0xffff 591 + #define X86_FAMILY_ANY 0 592 + #define X86_MODEL_ANY 0 593 + #define X86_FEATURE_ANY 0 /* Same as FPU, you can't test for that */ 594 + 574 595 #endif /* LINUX_MOD_DEVICETABLE_H */
+24
scripts/mod/file2alias.c
··· 1013 1013 } 1014 1014 ADD_TO_DEVTABLE("amba", struct amba_id, do_amba_entry); 1015 1015 1016 + /* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,* 1017 + * All fields are numbers. It would be nicer to use strings for vendor 1018 + * and feature, but getting those out of the build system here is too 1019 + * complicated. 1020 + */ 1021 + 1022 + static int do_x86cpu_entry(const char *filename, struct x86_cpu_id *id, 1023 + char *alias) 1024 + { 1025 + id->feature = TO_NATIVE(id->feature); 1026 + id->family = TO_NATIVE(id->family); 1027 + id->model = TO_NATIVE(id->model); 1028 + id->vendor = TO_NATIVE(id->vendor); 1029 + 1030 + strcpy(alias, "x86cpu:"); 1031 + ADD(alias, "vendor:", id->vendor != X86_VENDOR_ANY, id->vendor); 1032 + ADD(alias, ":family:", id->family != X86_FAMILY_ANY, id->family); 1033 + ADD(alias, ":model:", id->model != X86_MODEL_ANY, id->model); 1034 + ADD(alias, ":feature:*,", id->feature != X86_FEATURE_ANY, id->feature); 1035 + strcat(alias, ",*"); 1036 + return 1; 1037 + } 1038 + ADD_TO_DEVTABLE("x86cpu", struct x86_cpu_id, do_x86cpu_entry); 1039 + 1016 1040 /* Does namelen bytes of name exactly match the symbol? */ 1017 1041 static bool sym_is(const char *name, unsigned namelen, const char *symbol) 1018 1042 {