0118-x86-virt-x86-platform-Merge-struct-x86_hyper-into-st.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. From 59011dc6f59cc32a499fb926d95dad0dabb75a8f Mon Sep 17 00:00:00 2001
  2. From: Juergen Gross <[email protected]>
  3. Date: Thu, 9 Nov 2017 14:27:35 +0100
  4. Subject: [PATCH 118/232] x86/virt, x86/platform: Merge 'struct x86_hyper' into
  5. 'struct x86_platform' and 'struct x86_init'
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. Instead of x86_hyper being either NULL on bare metal or a pointer to a
  11. struct hypervisor_x86 in case of the kernel running as a guest merge
  12. the struct into x86_platform and x86_init.
  13. This will remove the need for wrappers making it hard to find out what
  14. is being called. With dummy functions added for all callbacks testing
  15. for a NULL function pointer can be removed, too.
  16. Suggested-by: Ingo Molnar <[email protected]>
  17. Signed-off-by: Juergen Gross <[email protected]>
  18. Acked-by: Thomas Gleixner <[email protected]>
  19. Cc: Linus Torvalds <[email protected]>
  20. Cc: Peter Zijlstra <[email protected]>
  21. Cc: [email protected]
  22. Cc: [email protected]
  23. Cc: [email protected]
  24. Cc: [email protected]
  25. Cc: [email protected]
  26. Cc: [email protected]
  27. Cc: [email protected]
  28. Cc: [email protected]
  29. Cc: [email protected]
  30. Cc: [email protected]
  31. Cc: [email protected]
  32. Cc: [email protected]
  33. Link: http://lkml.kernel.org/r/[email protected]
  34. Signed-off-by: Ingo Molnar <[email protected]>
  35. (cherry picked from commit f72e38e8ec8869ac0ba5a75d7d2f897d98a1454e)
  36. Signed-off-by: Andy Whitcroft <[email protected]>
  37. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  38. (cherry picked from commit 2d0b017b38623bca666acbcb5ab251315845fa55)
  39. Signed-off-by: Fabian Grünbichler <[email protected]>
  40. ---
  41. arch/x86/include/asm/hypervisor.h | 25 ++++--------------
  42. arch/x86/include/asm/x86_init.h | 24 +++++++++++++++++
  43. include/linux/hypervisor.h | 8 ++++--
  44. arch/x86/kernel/apic/apic.c | 2 +-
  45. arch/x86/kernel/cpu/hypervisor.c | 54 +++++++++++++++++++--------------------
  46. arch/x86/kernel/cpu/mshyperv.c | 2 +-
  47. arch/x86/kernel/cpu/vmware.c | 4 +--
  48. arch/x86/kernel/kvm.c | 2 +-
  49. arch/x86/kernel/x86_init.c | 9 +++++++
  50. arch/x86/mm/init.c | 2 +-
  51. arch/x86/xen/enlighten_hvm.c | 8 +++---
  52. arch/x86/xen/enlighten_pv.c | 2 +-
  53. 12 files changed, 81 insertions(+), 61 deletions(-)
  54. diff --git a/arch/x86/include/asm/hypervisor.h b/arch/x86/include/asm/hypervisor.h
  55. index 0ead9dbb9130..0eca7239a7aa 100644
  56. --- a/arch/x86/include/asm/hypervisor.h
  57. +++ b/arch/x86/include/asm/hypervisor.h
  58. @@ -23,6 +23,7 @@
  59. #ifdef CONFIG_HYPERVISOR_GUEST
  60. #include <asm/kvm_para.h>
  61. +#include <asm/x86_init.h>
  62. #include <asm/xen/hypervisor.h>
  63. /*
  64. @@ -35,17 +36,11 @@ struct hypervisor_x86 {
  65. /* Detection routine */
  66. uint32_t (*detect)(void);
  67. - /* Platform setup (run once per boot) */
  68. - void (*init_platform)(void);
  69. + /* init time callbacks */
  70. + struct x86_hyper_init init;
  71. - /* X2APIC detection (run once per boot) */
  72. - bool (*x2apic_available)(void);
  73. -
  74. - /* pin current vcpu to specified physical cpu (run rarely) */
  75. - void (*pin_vcpu)(int);
  76. -
  77. - /* called during init_mem_mapping() to setup early mappings. */
  78. - void (*init_mem_mapping)(void);
  79. + /* runtime callbacks */
  80. + struct x86_hyper_runtime runtime;
  81. };
  82. extern const struct hypervisor_x86 *x86_hyper;
  83. @@ -58,17 +53,7 @@ extern const struct hypervisor_x86 x86_hyper_xen_hvm;
  84. extern const struct hypervisor_x86 x86_hyper_kvm;
  85. extern void init_hypervisor_platform(void);
  86. -extern bool hypervisor_x2apic_available(void);
  87. -extern void hypervisor_pin_vcpu(int cpu);
  88. -
  89. -static inline void hypervisor_init_mem_mapping(void)
  90. -{
  91. - if (x86_hyper && x86_hyper->init_mem_mapping)
  92. - x86_hyper->init_mem_mapping();
  93. -}
  94. #else
  95. static inline void init_hypervisor_platform(void) { }
  96. -static inline bool hypervisor_x2apic_available(void) { return false; }
  97. -static inline void hypervisor_init_mem_mapping(void) { }
  98. #endif /* CONFIG_HYPERVISOR_GUEST */
  99. #endif /* _ASM_X86_HYPERVISOR_H */
  100. diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h
  101. index 7ba7e90a9ad6..4d95e5a13c0b 100644
  102. --- a/arch/x86/include/asm/x86_init.h
  103. +++ b/arch/x86/include/asm/x86_init.h
  104. @@ -113,6 +113,18 @@ struct x86_init_pci {
  105. void (*fixup_irqs)(void);
  106. };
  107. +/**
  108. + * struct x86_hyper_init - x86 hypervisor init functions
  109. + * @init_platform: platform setup
  110. + * @x2apic_available: X2APIC detection
  111. + * @init_mem_mapping: setup early mappings during init_mem_mapping()
  112. + */
  113. +struct x86_hyper_init {
  114. + void (*init_platform)(void);
  115. + bool (*x2apic_available)(void);
  116. + void (*init_mem_mapping)(void);
  117. +};
  118. +
  119. /**
  120. * struct x86_init_ops - functions for platform specific setup
  121. *
  122. @@ -126,6 +138,7 @@ struct x86_init_ops {
  123. struct x86_init_timers timers;
  124. struct x86_init_iommu iommu;
  125. struct x86_init_pci pci;
  126. + struct x86_hyper_init hyper;
  127. };
  128. /**
  129. @@ -198,6 +211,15 @@ struct x86_legacy_features {
  130. struct x86_legacy_devices devices;
  131. };
  132. +/**
  133. + * struct x86_hyper_runtime - x86 hypervisor specific runtime callbacks
  134. + *
  135. + * @pin_vcpu: pin current vcpu to specified physical cpu (run rarely)
  136. + */
  137. +struct x86_hyper_runtime {
  138. + void (*pin_vcpu)(int cpu);
  139. +};
  140. +
  141. /**
  142. * struct x86_platform_ops - platform specific runtime functions
  143. * @calibrate_cpu: calibrate CPU
  144. @@ -217,6 +239,7 @@ struct x86_legacy_features {
  145. * possible in x86_early_init_platform_quirks() by
  146. * only using the current x86_hardware_subarch
  147. * semantics.
  148. + * @hyper: x86 hypervisor specific runtime callbacks
  149. */
  150. struct x86_platform_ops {
  151. unsigned long (*calibrate_cpu)(void);
  152. @@ -232,6 +255,7 @@ struct x86_platform_ops {
  153. void (*apic_post_init)(void);
  154. struct x86_legacy_features legacy;
  155. void (*set_legacy_features)(void);
  156. + struct x86_hyper_runtime hyper;
  157. };
  158. struct pci_dev;
  159. diff --git a/include/linux/hypervisor.h b/include/linux/hypervisor.h
  160. index 3fa5ef2b3759..35e170ca87a8 100644
  161. --- a/include/linux/hypervisor.h
  162. +++ b/include/linux/hypervisor.h
  163. @@ -6,8 +6,12 @@
  164. * Juergen Gross <[email protected]>
  165. */
  166. -#ifdef CONFIG_HYPERVISOR_GUEST
  167. -#include <asm/hypervisor.h>
  168. +#ifdef CONFIG_X86
  169. +#include <asm/x86_init.h>
  170. +static inline void hypervisor_pin_vcpu(int cpu)
  171. +{
  172. + x86_platform.hyper.pin_vcpu(cpu);
  173. +}
  174. #else
  175. static inline void hypervisor_pin_vcpu(int cpu)
  176. {
  177. diff --git a/arch/x86/kernel/apic/apic.c b/arch/x86/kernel/apic/apic.c
  178. index 4a7f962b53ff..bb63c1350524 100644
  179. --- a/arch/x86/kernel/apic/apic.c
  180. +++ b/arch/x86/kernel/apic/apic.c
  181. @@ -1666,7 +1666,7 @@ static __init void try_to_enable_x2apic(int remap_mode)
  182. * under KVM
  183. */
  184. if (max_physical_apicid > 255 ||
  185. - !hypervisor_x2apic_available()) {
  186. + !x86_init.hyper.x2apic_available()) {
  187. pr_info("x2apic: IRQ remapping doesn't support X2APIC mode\n");
  188. x2apic_disable();
  189. return;
  190. diff --git a/arch/x86/kernel/cpu/hypervisor.c b/arch/x86/kernel/cpu/hypervisor.c
  191. index 4fa90006ac68..22226c1bf092 100644
  192. --- a/arch/x86/kernel/cpu/hypervisor.c
  193. +++ b/arch/x86/kernel/cpu/hypervisor.c
  194. @@ -44,51 +44,49 @@ static const __initconst struct hypervisor_x86 * const hypervisors[] =
  195. const struct hypervisor_x86 *x86_hyper;
  196. EXPORT_SYMBOL(x86_hyper);
  197. -static inline void __init
  198. +static inline const struct hypervisor_x86 * __init
  199. detect_hypervisor_vendor(void)
  200. {
  201. - const struct hypervisor_x86 *h, * const *p;
  202. + const struct hypervisor_x86 *h = NULL, * const *p;
  203. uint32_t pri, max_pri = 0;
  204. for (p = hypervisors; p < hypervisors + ARRAY_SIZE(hypervisors); p++) {
  205. - h = *p;
  206. - pri = h->detect();
  207. - if (pri != 0 && pri > max_pri) {
  208. + pri = (*p)->detect();
  209. + if (pri > max_pri) {
  210. max_pri = pri;
  211. - x86_hyper = h;
  212. + h = *p;
  213. }
  214. }
  215. - if (max_pri)
  216. - pr_info("Hypervisor detected: %s\n", x86_hyper->name);
  217. + if (h)
  218. + pr_info("Hypervisor detected: %s\n", h->name);
  219. +
  220. + return h;
  221. }
  222. -void __init init_hypervisor_platform(void)
  223. +static void __init copy_array(const void *src, void *target, unsigned int size)
  224. {
  225. + unsigned int i, n = size / sizeof(void *);
  226. + const void * const *from = (const void * const *)src;
  227. + const void **to = (const void **)target;
  228. - detect_hypervisor_vendor();
  229. -
  230. - if (!x86_hyper)
  231. - return;
  232. -
  233. - if (x86_hyper->init_platform)
  234. - x86_hyper->init_platform();
  235. + for (i = 0; i < n; i++)
  236. + if (from[i])
  237. + to[i] = from[i];
  238. }
  239. -bool __init hypervisor_x2apic_available(void)
  240. +void __init init_hypervisor_platform(void)
  241. {
  242. - return x86_hyper &&
  243. - x86_hyper->x2apic_available &&
  244. - x86_hyper->x2apic_available();
  245. -}
  246. + const struct hypervisor_x86 *h;
  247. -void hypervisor_pin_vcpu(int cpu)
  248. -{
  249. - if (!x86_hyper)
  250. + h = detect_hypervisor_vendor();
  251. +
  252. + if (!h)
  253. return;
  254. - if (x86_hyper->pin_vcpu)
  255. - x86_hyper->pin_vcpu(cpu);
  256. - else
  257. - WARN_ONCE(1, "vcpu pinning requested but not supported!\n");
  258. + copy_array(&h->init, &x86_init.hyper, sizeof(h->init));
  259. + copy_array(&h->runtime, &x86_platform.hyper, sizeof(h->runtime));
  260. +
  261. + x86_hyper = h;
  262. + x86_init.hyper.init_platform();
  263. }
  264. diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
  265. index 70e717fccdd6..9707e431da27 100644
  266. --- a/arch/x86/kernel/cpu/mshyperv.c
  267. +++ b/arch/x86/kernel/cpu/mshyperv.c
  268. @@ -255,6 +255,6 @@ static void __init ms_hyperv_init_platform(void)
  269. const __refconst struct hypervisor_x86 x86_hyper_ms_hyperv = {
  270. .name = "Microsoft HyperV",
  271. .detect = ms_hyperv_platform,
  272. - .init_platform = ms_hyperv_init_platform,
  273. + .init.init_platform = ms_hyperv_init_platform,
  274. };
  275. EXPORT_SYMBOL(x86_hyper_ms_hyperv);
  276. diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c
  277. index 40ed26852ebd..4804c1d063c8 100644
  278. --- a/arch/x86/kernel/cpu/vmware.c
  279. +++ b/arch/x86/kernel/cpu/vmware.c
  280. @@ -208,7 +208,7 @@ static bool __init vmware_legacy_x2apic_available(void)
  281. const __refconst struct hypervisor_x86 x86_hyper_vmware = {
  282. .name = "VMware",
  283. .detect = vmware_platform,
  284. - .init_platform = vmware_platform_setup,
  285. - .x2apic_available = vmware_legacy_x2apic_available,
  286. + .init.init_platform = vmware_platform_setup,
  287. + .init.x2apic_available = vmware_legacy_x2apic_available,
  288. };
  289. EXPORT_SYMBOL(x86_hyper_vmware);
  290. diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
  291. index 9e3798b00e40..54e373bfeab9 100644
  292. --- a/arch/x86/kernel/kvm.c
  293. +++ b/arch/x86/kernel/kvm.c
  294. @@ -547,7 +547,7 @@ static uint32_t __init kvm_detect(void)
  295. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  296. .name = "KVM",
  297. .detect = kvm_detect,
  298. - .x2apic_available = kvm_para_available,
  299. + .init.x2apic_available = kvm_para_available,
  300. };
  301. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  302. diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c
  303. index a088b2c47f73..5b2d10c1973a 100644
  304. --- a/arch/x86/kernel/x86_init.c
  305. +++ b/arch/x86/kernel/x86_init.c
  306. @@ -28,6 +28,8 @@ void x86_init_noop(void) { }
  307. void __init x86_init_uint_noop(unsigned int unused) { }
  308. int __init iommu_init_noop(void) { return 0; }
  309. void iommu_shutdown_noop(void) { }
  310. +bool __init bool_x86_init_noop(void) { return false; }
  311. +void x86_op_int_noop(int cpu) { }
  312. /*
  313. * The platform setup functions are preset with the default functions
  314. @@ -81,6 +83,12 @@ struct x86_init_ops x86_init __initdata = {
  315. .init_irq = x86_default_pci_init_irq,
  316. .fixup_irqs = x86_default_pci_fixup_irqs,
  317. },
  318. +
  319. + .hyper = {
  320. + .init_platform = x86_init_noop,
  321. + .x2apic_available = bool_x86_init_noop,
  322. + .init_mem_mapping = x86_init_noop,
  323. + },
  324. };
  325. struct x86_cpuinit_ops x86_cpuinit = {
  326. @@ -101,6 +109,7 @@ struct x86_platform_ops x86_platform __ro_after_init = {
  327. .get_nmi_reason = default_get_nmi_reason,
  328. .save_sched_clock_state = tsc_save_sched_clock_state,
  329. .restore_sched_clock_state = tsc_restore_sched_clock_state,
  330. + .hyper.pin_vcpu = x86_op_int_noop,
  331. };
  332. EXPORT_SYMBOL_GPL(x86_platform);
  333. diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
  334. index af5c1ed21d43..a22c2b95e513 100644
  335. --- a/arch/x86/mm/init.c
  336. +++ b/arch/x86/mm/init.c
  337. @@ -671,7 +671,7 @@ void __init init_mem_mapping(void)
  338. load_cr3(swapper_pg_dir);
  339. __flush_tlb_all();
  340. - hypervisor_init_mem_mapping();
  341. + x86_init.hyper.init_mem_mapping();
  342. early_memtest(0, max_pfn_mapped << PAGE_SHIFT);
  343. }
  344. diff --git a/arch/x86/xen/enlighten_hvm.c b/arch/x86/xen/enlighten_hvm.c
  345. index de503c225ae1..7b1622089f96 100644
  346. --- a/arch/x86/xen/enlighten_hvm.c
  347. +++ b/arch/x86/xen/enlighten_hvm.c
  348. @@ -229,9 +229,9 @@ static uint32_t __init xen_platform_hvm(void)
  349. const struct hypervisor_x86 x86_hyper_xen_hvm = {
  350. .name = "Xen HVM",
  351. .detect = xen_platform_hvm,
  352. - .init_platform = xen_hvm_guest_init,
  353. - .pin_vcpu = xen_pin_vcpu,
  354. - .x2apic_available = xen_x2apic_para_available,
  355. - .init_mem_mapping = xen_hvm_init_mem_mapping,
  356. + .init.init_platform = xen_hvm_guest_init,
  357. + .init.x2apic_available = xen_x2apic_para_available,
  358. + .init.init_mem_mapping = xen_hvm_init_mem_mapping,
  359. + .runtime.pin_vcpu = xen_pin_vcpu,
  360. };
  361. EXPORT_SYMBOL(x86_hyper_xen_hvm);
  362. diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
  363. index e7b213047724..4110fc9e5ee9 100644
  364. --- a/arch/x86/xen/enlighten_pv.c
  365. +++ b/arch/x86/xen/enlighten_pv.c
  366. @@ -1461,6 +1461,6 @@ static uint32_t __init xen_platform_pv(void)
  367. const struct hypervisor_x86 x86_hyper_xen_pv = {
  368. .name = "Xen PV",
  369. .detect = xen_platform_pv,
  370. - .pin_vcpu = xen_pin_vcpu,
  371. + .runtime.pin_vcpu = xen_pin_vcpu,
  372. };
  373. EXPORT_SYMBOL(x86_hyper_xen_pv);
  374. --
  375. 2.14.2