mce.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482
  1. /*
  2. * Machine check handler.
  3. *
  4. * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
  5. * Rest from unknown author(s).
  6. * 2004 Andi Kleen. Rewrote most of it.
  7. * Copyright 2008 Intel Corporation
  8. * Author: Andi Kleen
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/thread_info.h>
  12. #include <linux/capability.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/kobject.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/kernel.h>
  21. #include <linux/percpu.h>
  22. #include <linux/string.h>
  23. #include <linux/device.h>
  24. #include <linux/syscore_ops.h>
  25. #include <linux/delay.h>
  26. #include <linux/ctype.h>
  27. #include <linux/sched.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/types.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <linux/kmod.h>
  33. #include <linux/poll.h>
  34. #include <linux/nmi.h>
  35. #include <linux/cpu.h>
  36. #include <linux/smp.h>
  37. #include <linux/fs.h>
  38. #include <linux/mm.h>
  39. #include <linux/debugfs.h>
  40. #include <linux/irq_work.h>
  41. #include <linux/export.h>
  42. #include <asm/processor.h>
  43. #include <asm/mce.h>
  44. #include <asm/msr.h>
  45. #include "mce-internal.h"
  46. static DEFINE_MUTEX(mce_chrdev_read_mutex);
  47. #define rcu_dereference_check_mce(p) \
  48. rcu_dereference_index_check((p), \
  49. rcu_read_lock_sched_held() || \
  50. lockdep_is_held(&mce_chrdev_read_mutex))
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/mce.h>
  53. #define SPINUNIT 100 /* 100ns */
  54. atomic_t mce_entry;
  55. DEFINE_PER_CPU(unsigned, mce_exception_count);
  56. struct mce_bank *mce_banks __read_mostly;
  57. struct mca_config mca_cfg __read_mostly = {
  58. .bootlog = -1,
  59. /*
  60. * Tolerant levels:
  61. * 0: always panic on uncorrected errors, log corrected errors
  62. * 1: panic or SIGBUS on uncorrected errors, log corrected errors
  63. * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
  64. * 3: never panic or SIGBUS, log all errors (for testing only)
  65. */
  66. .tolerant = 1,
  67. .monarch_timeout = -1
  68. };
  69. /* User mode helper program triggered by machine check event */
  70. static unsigned long mce_need_notify;
  71. static char mce_helper[128];
  72. static char *mce_helper_argv[2] = { mce_helper, NULL };
  73. static DECLARE_WAIT_QUEUE_HEAD(mce_chrdev_wait);
  74. static DEFINE_PER_CPU(struct mce, mces_seen);
  75. static int cpu_missing;
  76. /* MCA banks polled by the period polling timer for corrected events */
  77. DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  78. [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
  79. };
  80. static DEFINE_PER_CPU(struct work_struct, mce_work);
  81. static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
  82. /*
  83. * CPU/chipset specific EDAC code can register a notifier call here to print
  84. * MCE errors in a human-readable form.
  85. */
  86. ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
  87. /* Do initial initialization of a struct mce */
  88. void mce_setup(struct mce *m)
  89. {
  90. memset(m, 0, sizeof(struct mce));
  91. m->cpu = m->extcpu = smp_processor_id();
  92. rdtscll(m->tsc);
  93. /* We hope get_seconds stays lockless */
  94. m->time = get_seconds();
  95. m->cpuvendor = boot_cpu_data.x86_vendor;
  96. m->cpuid = cpuid_eax(1);
  97. m->socketid = cpu_data(m->extcpu).phys_proc_id;
  98. m->apicid = cpu_data(m->extcpu).initial_apicid;
  99. rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
  100. }
  101. DEFINE_PER_CPU(struct mce, injectm);
  102. EXPORT_PER_CPU_SYMBOL_GPL(injectm);
  103. /*
  104. * Lockless MCE logging infrastructure.
  105. * This avoids deadlocks on printk locks without having to break locks. Also
  106. * separate MCEs from kernel messages to avoid bogus bug reports.
  107. */
  108. static struct mce_log mcelog = {
  109. .signature = MCE_LOG_SIGNATURE,
  110. .len = MCE_LOG_LEN,
  111. .recordlen = sizeof(struct mce),
  112. };
  113. void mce_log(struct mce *mce)
  114. {
  115. unsigned next, entry;
  116. int ret = 0;
  117. /* Emit the trace record: */
  118. trace_mce_record(mce);
  119. ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
  120. if (ret == NOTIFY_STOP)
  121. return;
  122. mce->finished = 0;
  123. wmb();
  124. for (;;) {
  125. entry = rcu_dereference_check_mce(mcelog.next);
  126. for (;;) {
  127. /*
  128. * When the buffer fills up discard new entries.
  129. * Assume that the earlier errors are the more
  130. * interesting ones:
  131. */
  132. if (entry >= MCE_LOG_LEN) {
  133. set_bit(MCE_OVERFLOW,
  134. (unsigned long *)&mcelog.flags);
  135. return;
  136. }
  137. /* Old left over entry. Skip: */
  138. if (mcelog.entry[entry].finished) {
  139. entry++;
  140. continue;
  141. }
  142. break;
  143. }
  144. smp_rmb();
  145. next = entry + 1;
  146. if (cmpxchg(&mcelog.next, entry, next) == entry)
  147. break;
  148. }
  149. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  150. wmb();
  151. mcelog.entry[entry].finished = 1;
  152. wmb();
  153. mce->finished = 1;
  154. set_bit(0, &mce_need_notify);
  155. }
  156. static void drain_mcelog_buffer(void)
  157. {
  158. unsigned int next, i, prev = 0;
  159. next = ACCESS_ONCE(mcelog.next);
  160. do {
  161. struct mce *m;
  162. /* drain what was logged during boot */
  163. for (i = prev; i < next; i++) {
  164. unsigned long start = jiffies;
  165. unsigned retries = 1;
  166. m = &mcelog.entry[i];
  167. while (!m->finished) {
  168. if (time_after_eq(jiffies, start + 2*retries))
  169. retries++;
  170. cpu_relax();
  171. if (!m->finished && retries >= 4) {
  172. pr_err("skipping error being logged currently!\n");
  173. break;
  174. }
  175. }
  176. smp_rmb();
  177. atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
  178. }
  179. memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
  180. prev = next;
  181. next = cmpxchg(&mcelog.next, prev, 0);
  182. } while (next != prev);
  183. }
  184. void mce_register_decode_chain(struct notifier_block *nb)
  185. {
  186. atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
  187. drain_mcelog_buffer();
  188. }
  189. EXPORT_SYMBOL_GPL(mce_register_decode_chain);
  190. void mce_unregister_decode_chain(struct notifier_block *nb)
  191. {
  192. atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
  193. }
  194. EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
  195. static void print_mce(struct mce *m)
  196. {
  197. int ret = 0;
  198. pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  199. m->extcpu, m->mcgstatus, m->bank, m->status);
  200. if (m->ip) {
  201. pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
  202. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  203. m->cs, m->ip);
  204. if (m->cs == __KERNEL_CS)
  205. print_symbol("{%s}", m->ip);
  206. pr_cont("\n");
  207. }
  208. pr_emerg(HW_ERR "TSC %llx ", m->tsc);
  209. if (m->addr)
  210. pr_cont("ADDR %llx ", m->addr);
  211. if (m->misc)
  212. pr_cont("MISC %llx ", m->misc);
  213. pr_cont("\n");
  214. /*
  215. * Note this output is parsed by external tools and old fields
  216. * should not be changed.
  217. */
  218. pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
  219. m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
  220. cpu_data(m->extcpu).microcode);
  221. /*
  222. * Print out human-readable details about the MCE error,
  223. * (if the CPU has an implementation for that)
  224. */
  225. ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
  226. if (ret == NOTIFY_STOP)
  227. return;
  228. pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
  229. }
  230. #define PANIC_TIMEOUT 5 /* 5 seconds */
  231. static atomic_t mce_paniced;
  232. static int fake_panic;
  233. static atomic_t mce_fake_paniced;
  234. /* Panic in progress. Enable interrupts and wait for final IPI */
  235. static void wait_for_panic(void)
  236. {
  237. long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
  238. preempt_disable();
  239. local_irq_enable();
  240. while (timeout-- > 0)
  241. udelay(1);
  242. if (panic_timeout == 0)
  243. panic_timeout = mca_cfg.panic_timeout;
  244. panic("Panicing machine check CPU died");
  245. }
  246. static void mce_panic(char *msg, struct mce *final, char *exp)
  247. {
  248. int i, apei_err = 0;
  249. if (!fake_panic) {
  250. /*
  251. * Make sure only one CPU runs in machine check panic
  252. */
  253. if (atomic_inc_return(&mce_paniced) > 1)
  254. wait_for_panic();
  255. barrier();
  256. bust_spinlocks(1);
  257. console_verbose();
  258. } else {
  259. /* Don't log too much for fake panic */
  260. if (atomic_inc_return(&mce_fake_paniced) > 1)
  261. return;
  262. }
  263. /* First print corrected ones that are still unlogged */
  264. for (i = 0; i < MCE_LOG_LEN; i++) {
  265. struct mce *m = &mcelog.entry[i];
  266. if (!(m->status & MCI_STATUS_VAL))
  267. continue;
  268. if (!(m->status & MCI_STATUS_UC)) {
  269. print_mce(m);
  270. if (!apei_err)
  271. apei_err = apei_write_mce(m);
  272. }
  273. }
  274. /* Now print uncorrected but with the final one last */
  275. for (i = 0; i < MCE_LOG_LEN; i++) {
  276. struct mce *m = &mcelog.entry[i];
  277. if (!(m->status & MCI_STATUS_VAL))
  278. continue;
  279. if (!(m->status & MCI_STATUS_UC))
  280. continue;
  281. if (!final || memcmp(m, final, sizeof(struct mce))) {
  282. print_mce(m);
  283. if (!apei_err)
  284. apei_err = apei_write_mce(m);
  285. }
  286. }
  287. if (final) {
  288. print_mce(final);
  289. if (!apei_err)
  290. apei_err = apei_write_mce(final);
  291. }
  292. if (cpu_missing)
  293. pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
  294. if (exp)
  295. pr_emerg(HW_ERR "Machine check: %s\n", exp);
  296. if (!fake_panic) {
  297. if (panic_timeout == 0)
  298. panic_timeout = mca_cfg.panic_timeout;
  299. panic(msg);
  300. } else
  301. pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
  302. }
  303. /* Support code for software error injection */
  304. static int msr_to_offset(u32 msr)
  305. {
  306. unsigned bank = __this_cpu_read(injectm.bank);
  307. if (msr == mca_cfg.rip_msr)
  308. return offsetof(struct mce, ip);
  309. if (msr == MSR_IA32_MCx_STATUS(bank))
  310. return offsetof(struct mce, status);
  311. if (msr == MSR_IA32_MCx_ADDR(bank))
  312. return offsetof(struct mce, addr);
  313. if (msr == MSR_IA32_MCx_MISC(bank))
  314. return offsetof(struct mce, misc);
  315. if (msr == MSR_IA32_MCG_STATUS)
  316. return offsetof(struct mce, mcgstatus);
  317. return -1;
  318. }
  319. /* MSR access wrappers used for error injection */
  320. static u64 mce_rdmsrl(u32 msr)
  321. {
  322. u64 v;
  323. if (__this_cpu_read(injectm.finished)) {
  324. int offset = msr_to_offset(msr);
  325. if (offset < 0)
  326. return 0;
  327. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  328. }
  329. if (rdmsrl_safe(msr, &v)) {
  330. WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
  331. /*
  332. * Return zero in case the access faulted. This should
  333. * not happen normally but can happen if the CPU does
  334. * something weird, or if the code is buggy.
  335. */
  336. v = 0;
  337. }
  338. return v;
  339. }
  340. static void mce_wrmsrl(u32 msr, u64 v)
  341. {
  342. if (__this_cpu_read(injectm.finished)) {
  343. int offset = msr_to_offset(msr);
  344. if (offset >= 0)
  345. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  346. return;
  347. }
  348. wrmsrl(msr, v);
  349. }
  350. /*
  351. * Collect all global (w.r.t. this processor) status about this machine
  352. * check into our "mce" struct so that we can use it later to assess
  353. * the severity of the problem as we read per-bank specific details.
  354. */
  355. static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
  356. {
  357. mce_setup(m);
  358. m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  359. if (regs) {
  360. /*
  361. * Get the address of the instruction at the time of
  362. * the machine check error.
  363. */
  364. if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
  365. m->ip = regs->ip;
  366. m->cs = regs->cs;
  367. /*
  368. * When in VM86 mode make the cs look like ring 3
  369. * always. This is a lie, but it's better than passing
  370. * the additional vm86 bit around everywhere.
  371. */
  372. if (v8086_mode(regs))
  373. m->cs |= 3;
  374. }
  375. /* Use accurate RIP reporting if available. */
  376. if (mca_cfg.rip_msr)
  377. m->ip = mce_rdmsrl(mca_cfg.rip_msr);
  378. }
  379. }
  380. /*
  381. * Simple lockless ring to communicate PFNs from the exception handler with the
  382. * process context work function. This is vastly simplified because there's
  383. * only a single reader and a single writer.
  384. */
  385. #define MCE_RING_SIZE 16 /* we use one entry less */
  386. struct mce_ring {
  387. unsigned short start;
  388. unsigned short end;
  389. unsigned long ring[MCE_RING_SIZE];
  390. };
  391. static DEFINE_PER_CPU(struct mce_ring, mce_ring);
  392. /* Runs with CPU affinity in workqueue */
  393. static int mce_ring_empty(void)
  394. {
  395. struct mce_ring *r = &__get_cpu_var(mce_ring);
  396. return r->start == r->end;
  397. }
  398. static int mce_ring_get(unsigned long *pfn)
  399. {
  400. struct mce_ring *r;
  401. int ret = 0;
  402. *pfn = 0;
  403. get_cpu();
  404. r = &__get_cpu_var(mce_ring);
  405. if (r->start == r->end)
  406. goto out;
  407. *pfn = r->ring[r->start];
  408. r->start = (r->start + 1) % MCE_RING_SIZE;
  409. ret = 1;
  410. out:
  411. put_cpu();
  412. return ret;
  413. }
  414. /* Always runs in MCE context with preempt off */
  415. static int mce_ring_add(unsigned long pfn)
  416. {
  417. struct mce_ring *r = &__get_cpu_var(mce_ring);
  418. unsigned next;
  419. next = (r->end + 1) % MCE_RING_SIZE;
  420. if (next == r->start)
  421. return -1;
  422. r->ring[r->end] = pfn;
  423. wmb();
  424. r->end = next;
  425. return 0;
  426. }
  427. int mce_available(struct cpuinfo_x86 *c)
  428. {
  429. if (mca_cfg.disabled)
  430. return 0;
  431. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  432. }
  433. static void mce_schedule_work(void)
  434. {
  435. if (!mce_ring_empty())
  436. schedule_work(&__get_cpu_var(mce_work));
  437. }
  438. DEFINE_PER_CPU(struct irq_work, mce_irq_work);
  439. static void mce_irq_work_cb(struct irq_work *entry)
  440. {
  441. mce_notify_irq();
  442. mce_schedule_work();
  443. }
  444. static void mce_report_event(struct pt_regs *regs)
  445. {
  446. if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
  447. mce_notify_irq();
  448. /*
  449. * Triggering the work queue here is just an insurance
  450. * policy in case the syscall exit notify handler
  451. * doesn't run soon enough or ends up running on the
  452. * wrong CPU (can happen when audit sleeps)
  453. */
  454. mce_schedule_work();
  455. return;
  456. }
  457. irq_work_queue(&__get_cpu_var(mce_irq_work));
  458. }
  459. /*
  460. * Read ADDR and MISC registers.
  461. */
  462. static void mce_read_aux(struct mce *m, int i)
  463. {
  464. if (m->status & MCI_STATUS_MISCV)
  465. m->misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
  466. if (m->status & MCI_STATUS_ADDRV) {
  467. m->addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
  468. /*
  469. * Mask the reported address by the reported granularity.
  470. */
  471. if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
  472. u8 shift = MCI_MISC_ADDR_LSB(m->misc);
  473. m->addr >>= shift;
  474. m->addr <<= shift;
  475. }
  476. }
  477. }
  478. DEFINE_PER_CPU(unsigned, mce_poll_count);
  479. /*
  480. * Poll for corrected events or events that happened before reset.
  481. * Those are just logged through /dev/mcelog.
  482. *
  483. * This is executed in standard interrupt context.
  484. *
  485. * Note: spec recommends to panic for fatal unsignalled
  486. * errors here. However this would be quite problematic --
  487. * we would need to reimplement the Monarch handling and
  488. * it would mess up the exclusion between exception handler
  489. * and poll hander -- * so we skip this for now.
  490. * These cases should not happen anyways, or only when the CPU
  491. * is already totally * confused. In this case it's likely it will
  492. * not fully execute the machine check handler either.
  493. */
  494. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  495. {
  496. struct mce m;
  497. int i;
  498. this_cpu_inc(mce_poll_count);
  499. mce_gather_info(&m, NULL);
  500. for (i = 0; i < mca_cfg.banks; i++) {
  501. if (!mce_banks[i].ctl || !test_bit(i, *b))
  502. continue;
  503. m.misc = 0;
  504. m.addr = 0;
  505. m.bank = i;
  506. m.tsc = 0;
  507. barrier();
  508. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  509. if (!(m.status & MCI_STATUS_VAL))
  510. continue;
  511. /*
  512. * Uncorrected or signalled events are handled by the exception
  513. * handler when it is enabled, so don't process those here.
  514. *
  515. * TBD do the same check for MCI_STATUS_EN here?
  516. */
  517. if (!(flags & MCP_UC) &&
  518. (m.status & (mca_cfg.ser ? MCI_STATUS_S : MCI_STATUS_UC)))
  519. continue;
  520. mce_read_aux(&m, i);
  521. if (!(flags & MCP_TIMESTAMP))
  522. m.tsc = 0;
  523. /*
  524. * Don't get the IP here because it's unlikely to
  525. * have anything to do with the actual error location.
  526. */
  527. if (!(flags & MCP_DONTLOG) && !mca_cfg.dont_log_ce)
  528. mce_log(&m);
  529. /*
  530. * Clear state for this bank.
  531. */
  532. mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  533. }
  534. /*
  535. * Don't clear MCG_STATUS here because it's only defined for
  536. * exceptions.
  537. */
  538. sync_core();
  539. }
  540. EXPORT_SYMBOL_GPL(machine_check_poll);
  541. /*
  542. * Do a quick check if any of the events requires a panic.
  543. * This decides if we keep the events around or clear them.
  544. */
  545. static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
  546. struct pt_regs *regs)
  547. {
  548. int i, ret = 0;
  549. for (i = 0; i < mca_cfg.banks; i++) {
  550. m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  551. if (m->status & MCI_STATUS_VAL) {
  552. __set_bit(i, validp);
  553. if (quirk_no_way_out)
  554. quirk_no_way_out(i, m, regs);
  555. }
  556. if (mce_severity(m, mca_cfg.tolerant, msg) >= MCE_PANIC_SEVERITY)
  557. ret = 1;
  558. }
  559. return ret;
  560. }
  561. /*
  562. * Variable to establish order between CPUs while scanning.
  563. * Each CPU spins initially until executing is equal its number.
  564. */
  565. static atomic_t mce_executing;
  566. /*
  567. * Defines order of CPUs on entry. First CPU becomes Monarch.
  568. */
  569. static atomic_t mce_callin;
  570. /*
  571. * Check if a timeout waiting for other CPUs happened.
  572. */
  573. static int mce_timed_out(u64 *t)
  574. {
  575. /*
  576. * The others already did panic for some reason.
  577. * Bail out like in a timeout.
  578. * rmb() to tell the compiler that system_state
  579. * might have been modified by someone else.
  580. */
  581. rmb();
  582. if (atomic_read(&mce_paniced))
  583. wait_for_panic();
  584. if (!mca_cfg.monarch_timeout)
  585. goto out;
  586. if ((s64)*t < SPINUNIT) {
  587. /* CHECKME: Make panic default for 1 too? */
  588. if (mca_cfg.tolerant < 1)
  589. mce_panic("Timeout synchronizing machine check over CPUs",
  590. NULL, NULL);
  591. cpu_missing = 1;
  592. return 1;
  593. }
  594. *t -= SPINUNIT;
  595. out:
  596. touch_nmi_watchdog();
  597. return 0;
  598. }
  599. /*
  600. * The Monarch's reign. The Monarch is the CPU who entered
  601. * the machine check handler first. It waits for the others to
  602. * raise the exception too and then grades them. When any
  603. * error is fatal panic. Only then let the others continue.
  604. *
  605. * The other CPUs entering the MCE handler will be controlled by the
  606. * Monarch. They are called Subjects.
  607. *
  608. * This way we prevent any potential data corruption in a unrecoverable case
  609. * and also makes sure always all CPU's errors are examined.
  610. *
  611. * Also this detects the case of a machine check event coming from outer
  612. * space (not detected by any CPUs) In this case some external agent wants
  613. * us to shut down, so panic too.
  614. *
  615. * The other CPUs might still decide to panic if the handler happens
  616. * in a unrecoverable place, but in this case the system is in a semi-stable
  617. * state and won't corrupt anything by itself. It's ok to let the others
  618. * continue for a bit first.
  619. *
  620. * All the spin loops have timeouts; when a timeout happens a CPU
  621. * typically elects itself to be Monarch.
  622. */
  623. static void mce_reign(void)
  624. {
  625. int cpu;
  626. struct mce *m = NULL;
  627. int global_worst = 0;
  628. char *msg = NULL;
  629. char *nmsg = NULL;
  630. /*
  631. * This CPU is the Monarch and the other CPUs have run
  632. * through their handlers.
  633. * Grade the severity of the errors of all the CPUs.
  634. */
  635. for_each_possible_cpu(cpu) {
  636. int severity = mce_severity(&per_cpu(mces_seen, cpu),
  637. mca_cfg.tolerant,
  638. &nmsg);
  639. if (severity > global_worst) {
  640. msg = nmsg;
  641. global_worst = severity;
  642. m = &per_cpu(mces_seen, cpu);
  643. }
  644. }
  645. /*
  646. * Cannot recover? Panic here then.
  647. * This dumps all the mces in the log buffer and stops the
  648. * other CPUs.
  649. */
  650. if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3)
  651. mce_panic("Fatal Machine check", m, msg);
  652. /*
  653. * For UC somewhere we let the CPU who detects it handle it.
  654. * Also must let continue the others, otherwise the handling
  655. * CPU could deadlock on a lock.
  656. */
  657. /*
  658. * No machine check event found. Must be some external
  659. * source or one CPU is hung. Panic.
  660. */
  661. if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
  662. mce_panic("Machine check from unknown source", NULL, NULL);
  663. /*
  664. * Now clear all the mces_seen so that they don't reappear on
  665. * the next mce.
  666. */
  667. for_each_possible_cpu(cpu)
  668. memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
  669. }
  670. static atomic_t global_nwo;
  671. /*
  672. * Start of Monarch synchronization. This waits until all CPUs have
  673. * entered the exception handler and then determines if any of them
  674. * saw a fatal event that requires panic. Then it executes them
  675. * in the entry order.
  676. * TBD double check parallel CPU hotunplug
  677. */
  678. static int mce_start(int *no_way_out)
  679. {
  680. int order;
  681. int cpus = num_online_cpus();
  682. u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
  683. if (!timeout)
  684. return -1;
  685. atomic_add(*no_way_out, &global_nwo);
  686. /*
  687. * global_nwo should be updated before mce_callin
  688. */
  689. smp_wmb();
  690. order = atomic_inc_return(&mce_callin);
  691. /*
  692. * Wait for everyone.
  693. */
  694. while (atomic_read(&mce_callin) != cpus) {
  695. if (mce_timed_out(&timeout)) {
  696. atomic_set(&global_nwo, 0);
  697. return -1;
  698. }
  699. ndelay(SPINUNIT);
  700. }
  701. /*
  702. * mce_callin should be read before global_nwo
  703. */
  704. smp_rmb();
  705. if (order == 1) {
  706. /*
  707. * Monarch: Starts executing now, the others wait.
  708. */
  709. atomic_set(&mce_executing, 1);
  710. } else {
  711. /*
  712. * Subject: Now start the scanning loop one by one in
  713. * the original callin order.
  714. * This way when there are any shared banks it will be
  715. * only seen by one CPU before cleared, avoiding duplicates.
  716. */
  717. while (atomic_read(&mce_executing) < order) {
  718. if (mce_timed_out(&timeout)) {
  719. atomic_set(&global_nwo, 0);
  720. return -1;
  721. }
  722. ndelay(SPINUNIT);
  723. }
  724. }
  725. /*
  726. * Cache the global no_way_out state.
  727. */
  728. *no_way_out = atomic_read(&global_nwo);
  729. return order;
  730. }
  731. /*
  732. * Synchronize between CPUs after main scanning loop.
  733. * This invokes the bulk of the Monarch processing.
  734. */
  735. static int mce_end(int order)
  736. {
  737. int ret = -1;
  738. u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
  739. if (!timeout)
  740. goto reset;
  741. if (order < 0)
  742. goto reset;
  743. /*
  744. * Allow others to run.
  745. */
  746. atomic_inc(&mce_executing);
  747. if (order == 1) {
  748. /* CHECKME: Can this race with a parallel hotplug? */
  749. int cpus = num_online_cpus();
  750. /*
  751. * Monarch: Wait for everyone to go through their scanning
  752. * loops.
  753. */
  754. while (atomic_read(&mce_executing) <= cpus) {
  755. if (mce_timed_out(&timeout))
  756. goto reset;
  757. ndelay(SPINUNIT);
  758. }
  759. mce_reign();
  760. barrier();
  761. ret = 0;
  762. } else {
  763. /*
  764. * Subject: Wait for Monarch to finish.
  765. */
  766. while (atomic_read(&mce_executing) != 0) {
  767. if (mce_timed_out(&timeout))
  768. goto reset;
  769. ndelay(SPINUNIT);
  770. }
  771. /*
  772. * Don't reset anything. That's done by the Monarch.
  773. */
  774. return 0;
  775. }
  776. /*
  777. * Reset all global state.
  778. */
  779. reset:
  780. atomic_set(&global_nwo, 0);
  781. atomic_set(&mce_callin, 0);
  782. barrier();
  783. /*
  784. * Let others run again.
  785. */
  786. atomic_set(&mce_executing, 0);
  787. return ret;
  788. }
  789. /*
  790. * Check if the address reported by the CPU is in a format we can parse.
  791. * It would be possible to add code for most other cases, but all would
  792. * be somewhat complicated (e.g. segment offset would require an instruction
  793. * parser). So only support physical addresses up to page granuality for now.
  794. */
  795. static int mce_usable_address(struct mce *m)
  796. {
  797. if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
  798. return 0;
  799. if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
  800. return 0;
  801. if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
  802. return 0;
  803. return 1;
  804. }
  805. static void mce_clear_state(unsigned long *toclear)
  806. {
  807. int i;
  808. for (i = 0; i < mca_cfg.banks; i++) {
  809. if (test_bit(i, toclear))
  810. mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  811. }
  812. }
  813. /*
  814. * Need to save faulting physical address associated with a process
  815. * in the machine check handler some place where we can grab it back
  816. * later in mce_notify_process()
  817. */
  818. #define MCE_INFO_MAX 16
  819. struct mce_info {
  820. atomic_t inuse;
  821. struct task_struct *t;
  822. __u64 paddr;
  823. int restartable;
  824. } mce_info[MCE_INFO_MAX];
  825. static void mce_save_info(__u64 addr, int c)
  826. {
  827. struct mce_info *mi;
  828. for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++) {
  829. if (atomic_cmpxchg(&mi->inuse, 0, 1) == 0) {
  830. mi->t = current;
  831. mi->paddr = addr;
  832. mi->restartable = c;
  833. return;
  834. }
  835. }
  836. mce_panic("Too many concurrent recoverable errors", NULL, NULL);
  837. }
  838. static struct mce_info *mce_find_info(void)
  839. {
  840. struct mce_info *mi;
  841. for (mi = mce_info; mi < &mce_info[MCE_INFO_MAX]; mi++)
  842. if (atomic_read(&mi->inuse) && mi->t == current)
  843. return mi;
  844. return NULL;
  845. }
  846. static void mce_clear_info(struct mce_info *mi)
  847. {
  848. atomic_set(&mi->inuse, 0);
  849. }
  850. /*
  851. * The actual machine check handler. This only handles real
  852. * exceptions when something got corrupted coming in through int 18.
  853. *
  854. * This is executed in NMI context not subject to normal locking rules. This
  855. * implies that most kernel services cannot be safely used. Don't even
  856. * think about putting a printk in there!
  857. *
  858. * On Intel systems this is entered on all CPUs in parallel through
  859. * MCE broadcast. However some CPUs might be broken beyond repair,
  860. * so be always careful when synchronizing with others.
  861. */
  862. void do_machine_check(struct pt_regs *regs, long error_code)
  863. {
  864. struct mca_config *cfg = &mca_cfg;
  865. struct mce m, *final;
  866. int i;
  867. int worst = 0;
  868. int severity;
  869. /*
  870. * Establish sequential order between the CPUs entering the machine
  871. * check handler.
  872. */
  873. int order;
  874. /*
  875. * If no_way_out gets set, there is no safe way to recover from this
  876. * MCE. If mca_cfg.tolerant is cranked up, we'll try anyway.
  877. */
  878. int no_way_out = 0;
  879. /*
  880. * If kill_it gets set, there might be a way to recover from this
  881. * error.
  882. */
  883. int kill_it = 0;
  884. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  885. DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
  886. char *msg = "Unknown";
  887. atomic_inc(&mce_entry);
  888. this_cpu_inc(mce_exception_count);
  889. if (!cfg->banks)
  890. goto out;
  891. mce_gather_info(&m, regs);
  892. final = &__get_cpu_var(mces_seen);
  893. *final = m;
  894. memset(valid_banks, 0, sizeof(valid_banks));
  895. no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
  896. barrier();
  897. /*
  898. * When no restart IP might need to kill or panic.
  899. * Assume the worst for now, but if we find the
  900. * severity is MCE_AR_SEVERITY we have other options.
  901. */
  902. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  903. kill_it = 1;
  904. /*
  905. * Go through all the banks in exclusion of the other CPUs.
  906. * This way we don't report duplicated events on shared banks
  907. * because the first one to see it will clear it.
  908. */
  909. order = mce_start(&no_way_out);
  910. for (i = 0; i < cfg->banks; i++) {
  911. __clear_bit(i, toclear);
  912. if (!test_bit(i, valid_banks))
  913. continue;
  914. if (!mce_banks[i].ctl)
  915. continue;
  916. m.misc = 0;
  917. m.addr = 0;
  918. m.bank = i;
  919. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  920. if ((m.status & MCI_STATUS_VAL) == 0)
  921. continue;
  922. /*
  923. * Non uncorrected or non signaled errors are handled by
  924. * machine_check_poll. Leave them alone, unless this panics.
  925. */
  926. if (!(m.status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  927. !no_way_out)
  928. continue;
  929. /*
  930. * Set taint even when machine check was not enabled.
  931. */
  932. add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
  933. severity = mce_severity(&m, cfg->tolerant, NULL);
  934. /*
  935. * When machine check was for corrected handler don't touch,
  936. * unless we're panicing.
  937. */
  938. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  939. continue;
  940. __set_bit(i, toclear);
  941. if (severity == MCE_NO_SEVERITY) {
  942. /*
  943. * Machine check event was not enabled. Clear, but
  944. * ignore.
  945. */
  946. continue;
  947. }
  948. mce_read_aux(&m, i);
  949. /*
  950. * Action optional error. Queue address for later processing.
  951. * When the ring overflows we just ignore the AO error.
  952. * RED-PEN add some logging mechanism when
  953. * usable_address or mce_add_ring fails.
  954. * RED-PEN don't ignore overflow for mca_cfg.tolerant == 0
  955. */
  956. if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
  957. mce_ring_add(m.addr >> PAGE_SHIFT);
  958. mce_log(&m);
  959. if (severity > worst) {
  960. *final = m;
  961. worst = severity;
  962. }
  963. }
  964. /* mce_clear_state will clear *final, save locally for use later */
  965. m = *final;
  966. if (!no_way_out)
  967. mce_clear_state(toclear);
  968. /*
  969. * Do most of the synchronization with other CPUs.
  970. * When there's any problem use only local no_way_out state.
  971. */
  972. if (mce_end(order) < 0)
  973. no_way_out = worst >= MCE_PANIC_SEVERITY;
  974. /*
  975. * At insane "tolerant" levels we take no action. Otherwise
  976. * we only die if we have no other choice. For less serious
  977. * issues we try to recover, or limit damage to the current
  978. * process.
  979. */
  980. if (cfg->tolerant < 3) {
  981. if (no_way_out)
  982. mce_panic("Fatal machine check on current CPU", &m, msg);
  983. if (worst == MCE_AR_SEVERITY) {
  984. /* schedule action before return to userland */
  985. mce_save_info(m.addr, m.mcgstatus & MCG_STATUS_RIPV);
  986. set_thread_flag(TIF_MCE_NOTIFY);
  987. } else if (kill_it) {
  988. force_sig(SIGBUS, current);
  989. }
  990. }
  991. if (worst > 0)
  992. mce_report_event(regs);
  993. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  994. out:
  995. atomic_dec(&mce_entry);
  996. sync_core();
  997. }
  998. EXPORT_SYMBOL_GPL(do_machine_check);
  999. #ifndef CONFIG_MEMORY_FAILURE
  1000. int memory_failure(unsigned long pfn, int vector, int flags)
  1001. {
  1002. /* mce_severity() should not hand us an ACTION_REQUIRED error */
  1003. BUG_ON(flags & MF_ACTION_REQUIRED);
  1004. pr_err("Uncorrected memory error in page 0x%lx ignored\n"
  1005. "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
  1006. pfn);
  1007. return 0;
  1008. }
  1009. #endif
  1010. /*
  1011. * Called in process context that interrupted by MCE and marked with
  1012. * TIF_MCE_NOTIFY, just before returning to erroneous userland.
  1013. * This code is allowed to sleep.
  1014. * Attempt possible recovery such as calling the high level VM handler to
  1015. * process any corrupted pages, and kill/signal current process if required.
  1016. * Action required errors are handled here.
  1017. */
  1018. void mce_notify_process(void)
  1019. {
  1020. unsigned long pfn;
  1021. struct mce_info *mi = mce_find_info();
  1022. int flags = MF_ACTION_REQUIRED;
  1023. if (!mi)
  1024. mce_panic("Lost physical address for unconsumed uncorrectable error", NULL, NULL);
  1025. pfn = mi->paddr >> PAGE_SHIFT;
  1026. clear_thread_flag(TIF_MCE_NOTIFY);
  1027. pr_err("Uncorrected hardware memory error in user-access at %llx",
  1028. mi->paddr);
  1029. /*
  1030. * We must call memory_failure() here even if the current process is
  1031. * doomed. We still need to mark the page as poisoned and alert any
  1032. * other users of the page.
  1033. */
  1034. if (!mi->restartable)
  1035. flags |= MF_MUST_KILL;
  1036. if (memory_failure(pfn, MCE_VECTOR, flags) < 0) {
  1037. pr_err("Memory error not recovered");
  1038. force_sig(SIGBUS, current);
  1039. }
  1040. mce_clear_info(mi);
  1041. }
  1042. /*
  1043. * Action optional processing happens here (picking up
  1044. * from the list of faulting pages that do_machine_check()
  1045. * placed into the "ring").
  1046. */
  1047. static void mce_process_work(struct work_struct *dummy)
  1048. {
  1049. unsigned long pfn;
  1050. while (mce_ring_get(&pfn))
  1051. memory_failure(pfn, MCE_VECTOR, 0);
  1052. }
  1053. #ifdef CONFIG_X86_MCE_INTEL
  1054. /***
  1055. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  1056. * @cpu: The CPU on which the event occurred.
  1057. * @status: Event status information
  1058. *
  1059. * This function should be called by the thermal interrupt after the
  1060. * event has been processed and the decision was made to log the event
  1061. * further.
  1062. *
  1063. * The status parameter will be saved to the 'status' field of 'struct mce'
  1064. * and historically has been the register value of the
  1065. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  1066. */
  1067. void mce_log_therm_throt_event(__u64 status)
  1068. {
  1069. struct mce m;
  1070. mce_setup(&m);
  1071. m.bank = MCE_THERMAL_BANK;
  1072. m.status = status;
  1073. mce_log(&m);
  1074. }
  1075. #endif /* CONFIG_X86_MCE_INTEL */
  1076. /*
  1077. * Periodic polling timer for "silent" machine check errors. If the
  1078. * poller finds an MCE, poll 2x faster. When the poller finds no more
  1079. * errors, poll 2x slower (up to check_interval seconds).
  1080. */
  1081. static unsigned long check_interval = 5 * 60; /* 5 minutes */
  1082. static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
  1083. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  1084. static unsigned long mce_adjust_timer_default(unsigned long interval)
  1085. {
  1086. return interval;
  1087. }
  1088. static unsigned long (*mce_adjust_timer)(unsigned long interval) =
  1089. mce_adjust_timer_default;
  1090. static void mce_timer_fn(unsigned long data)
  1091. {
  1092. struct timer_list *t = &__get_cpu_var(mce_timer);
  1093. unsigned long iv;
  1094. WARN_ON(smp_processor_id() != data);
  1095. if (mce_available(__this_cpu_ptr(&cpu_info))) {
  1096. machine_check_poll(MCP_TIMESTAMP,
  1097. &__get_cpu_var(mce_poll_banks));
  1098. mce_intel_cmci_poll();
  1099. }
  1100. /*
  1101. * Alert userspace if needed. If we logged an MCE, reduce the
  1102. * polling interval, otherwise increase the polling interval.
  1103. */
  1104. iv = __this_cpu_read(mce_next_interval);
  1105. if (mce_notify_irq()) {
  1106. iv = max(iv / 2, (unsigned long) HZ/100);
  1107. } else {
  1108. iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
  1109. iv = mce_adjust_timer(iv);
  1110. }
  1111. __this_cpu_write(mce_next_interval, iv);
  1112. /* Might have become 0 after CMCI storm subsided */
  1113. if (iv) {
  1114. t->expires = jiffies + iv;
  1115. add_timer_on(t, smp_processor_id());
  1116. }
  1117. }
  1118. /*
  1119. * Ensure that the timer is firing in @interval from now.
  1120. */
  1121. void mce_timer_kick(unsigned long interval)
  1122. {
  1123. struct timer_list *t = &__get_cpu_var(mce_timer);
  1124. unsigned long when = jiffies + interval;
  1125. unsigned long iv = __this_cpu_read(mce_next_interval);
  1126. if (timer_pending(t)) {
  1127. if (time_before(when, t->expires))
  1128. mod_timer_pinned(t, when);
  1129. } else {
  1130. t->expires = round_jiffies(when);
  1131. add_timer_on(t, smp_processor_id());
  1132. }
  1133. if (interval < iv)
  1134. __this_cpu_write(mce_next_interval, interval);
  1135. }
  1136. /* Must not be called in IRQ context where del_timer_sync() can deadlock */
  1137. static void mce_timer_delete_all(void)
  1138. {
  1139. int cpu;
  1140. for_each_online_cpu(cpu)
  1141. del_timer_sync(&per_cpu(mce_timer, cpu));
  1142. }
  1143. static void mce_do_trigger(struct work_struct *work)
  1144. {
  1145. call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
  1146. }
  1147. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  1148. /*
  1149. * Notify the user(s) about new machine check events.
  1150. * Can be called from interrupt context, but not from machine check/NMI
  1151. * context.
  1152. */
  1153. int mce_notify_irq(void)
  1154. {
  1155. /* Not more than two messages every minute */
  1156. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  1157. if (test_and_clear_bit(0, &mce_need_notify)) {
  1158. /* wake processes polling /dev/mcelog */
  1159. wake_up_interruptible(&mce_chrdev_wait);
  1160. if (mce_helper[0])
  1161. schedule_work(&mce_trigger_work);
  1162. if (__ratelimit(&ratelimit))
  1163. pr_info(HW_ERR "Machine check events logged\n");
  1164. return 1;
  1165. }
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1169. static int __cpuinit __mcheck_cpu_mce_banks_init(void)
  1170. {
  1171. int i;
  1172. u8 num_banks = mca_cfg.banks;
  1173. mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
  1174. if (!mce_banks)
  1175. return -ENOMEM;
  1176. for (i = 0; i < num_banks; i++) {
  1177. struct mce_bank *b = &mce_banks[i];
  1178. b->ctl = -1ULL;
  1179. b->init = 1;
  1180. }
  1181. return 0;
  1182. }
  1183. /*
  1184. * Initialize Machine Checks for a CPU.
  1185. */
  1186. static int __cpuinit __mcheck_cpu_cap_init(void)
  1187. {
  1188. unsigned b;
  1189. u64 cap;
  1190. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1191. b = cap & MCG_BANKCNT_MASK;
  1192. if (!mca_cfg.banks)
  1193. pr_info("CPU supports %d MCE banks\n", b);
  1194. if (b > MAX_NR_BANKS) {
  1195. pr_warn("Using only %u machine check banks out of %u\n",
  1196. MAX_NR_BANKS, b);
  1197. b = MAX_NR_BANKS;
  1198. }
  1199. /* Don't support asymmetric configurations today */
  1200. WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
  1201. mca_cfg.banks = b;
  1202. if (!mce_banks) {
  1203. int err = __mcheck_cpu_mce_banks_init();
  1204. if (err)
  1205. return err;
  1206. }
  1207. /* Use accurate RIP reporting if available. */
  1208. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1209. mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
  1210. if (cap & MCG_SER_P)
  1211. mca_cfg.ser = true;
  1212. return 0;
  1213. }
  1214. static void __mcheck_cpu_init_generic(void)
  1215. {
  1216. enum mcp_flags m_fl = 0;
  1217. mce_banks_t all_banks;
  1218. u64 cap;
  1219. int i;
  1220. if (!mca_cfg.bootlog)
  1221. m_fl = MCP_DONTLOG;
  1222. /*
  1223. * Log the machine checks left over from the previous reset.
  1224. */
  1225. bitmap_fill(all_banks, MAX_NR_BANKS);
  1226. machine_check_poll(MCP_UC | m_fl, &all_banks);
  1227. set_in_cr4(X86_CR4_MCE);
  1228. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1229. if (cap & MCG_CTL_P)
  1230. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1231. for (i = 0; i < mca_cfg.banks; i++) {
  1232. struct mce_bank *b = &mce_banks[i];
  1233. if (!b->init)
  1234. continue;
  1235. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1236. wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  1237. }
  1238. }
  1239. /*
  1240. * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
  1241. * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
  1242. * Vol 3B Table 15-20). But this confuses both the code that determines
  1243. * whether the machine check occurred in kernel or user mode, and also
  1244. * the severity assessment code. Pretend that EIPV was set, and take the
  1245. * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
  1246. */
  1247. static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
  1248. {
  1249. if (bank != 0)
  1250. return;
  1251. if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
  1252. return;
  1253. if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
  1254. MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
  1255. MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
  1256. MCACOD)) !=
  1257. (MCI_STATUS_UC|MCI_STATUS_EN|
  1258. MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
  1259. MCI_STATUS_AR|MCACOD_INSTR))
  1260. return;
  1261. m->mcgstatus |= MCG_STATUS_EIPV;
  1262. m->ip = regs->ip;
  1263. m->cs = regs->cs;
  1264. }
  1265. /* Add per CPU specific workarounds here */
  1266. static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
  1267. {
  1268. struct mca_config *cfg = &mca_cfg;
  1269. if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
  1270. pr_info("unknown CPU type - not enabling MCE support\n");
  1271. return -EOPNOTSUPP;
  1272. }
  1273. /* This should be disabled by the BIOS, but isn't always */
  1274. if (c->x86_vendor == X86_VENDOR_AMD) {
  1275. if (c->x86 == 15 && cfg->banks > 4) {
  1276. /*
  1277. * disable GART TBL walk error reporting, which
  1278. * trips off incorrectly with the IOMMU & 3ware
  1279. * & Cerberus:
  1280. */
  1281. clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
  1282. }
  1283. if (c->x86 <= 17 && cfg->bootlog < 0) {
  1284. /*
  1285. * Lots of broken BIOS around that don't clear them
  1286. * by default and leave crap in there. Don't log:
  1287. */
  1288. cfg->bootlog = 0;
  1289. }
  1290. /*
  1291. * Various K7s with broken bank 0 around. Always disable
  1292. * by default.
  1293. */
  1294. if (c->x86 == 6 && cfg->banks > 0)
  1295. mce_banks[0].ctl = 0;
  1296. /*
  1297. * Turn off MC4_MISC thresholding banks on those models since
  1298. * they're not supported there.
  1299. */
  1300. if (c->x86 == 0x15 &&
  1301. (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
  1302. int i;
  1303. u64 val, hwcr;
  1304. bool need_toggle;
  1305. u32 msrs[] = {
  1306. 0x00000413, /* MC4_MISC0 */
  1307. 0xc0000408, /* MC4_MISC1 */
  1308. };
  1309. rdmsrl(MSR_K7_HWCR, hwcr);
  1310. /* McStatusWrEn has to be set */
  1311. need_toggle = !(hwcr & BIT(18));
  1312. if (need_toggle)
  1313. wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
  1314. for (i = 0; i < ARRAY_SIZE(msrs); i++) {
  1315. rdmsrl(msrs[i], val);
  1316. /* CntP bit set? */
  1317. if (val & BIT_64(62)) {
  1318. val &= ~BIT_64(62);
  1319. wrmsrl(msrs[i], val);
  1320. }
  1321. }
  1322. /* restore old settings */
  1323. if (need_toggle)
  1324. wrmsrl(MSR_K7_HWCR, hwcr);
  1325. }
  1326. }
  1327. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1328. /*
  1329. * SDM documents that on family 6 bank 0 should not be written
  1330. * because it aliases to another special BIOS controlled
  1331. * register.
  1332. * But it's not aliased anymore on model 0x1a+
  1333. * Don't ignore bank 0 completely because there could be a
  1334. * valid event later, merely don't write CTL0.
  1335. */
  1336. if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
  1337. mce_banks[0].init = 0;
  1338. /*
  1339. * All newer Intel systems support MCE broadcasting. Enable
  1340. * synchronization with a one second timeout.
  1341. */
  1342. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1343. cfg->monarch_timeout < 0)
  1344. cfg->monarch_timeout = USEC_PER_SEC;
  1345. /*
  1346. * There are also broken BIOSes on some Pentium M and
  1347. * earlier systems:
  1348. */
  1349. if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
  1350. cfg->bootlog = 0;
  1351. if (c->x86 == 6 && c->x86_model == 45)
  1352. quirk_no_way_out = quirk_sandybridge_ifu;
  1353. }
  1354. if (cfg->monarch_timeout < 0)
  1355. cfg->monarch_timeout = 0;
  1356. if (cfg->bootlog != 0)
  1357. cfg->panic_timeout = 30;
  1358. return 0;
  1359. }
  1360. static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
  1361. {
  1362. if (c->x86 != 5)
  1363. return 0;
  1364. switch (c->x86_vendor) {
  1365. case X86_VENDOR_INTEL:
  1366. intel_p5_mcheck_init(c);
  1367. return 1;
  1368. break;
  1369. case X86_VENDOR_CENTAUR:
  1370. winchip_mcheck_init(c);
  1371. return 1;
  1372. break;
  1373. }
  1374. return 0;
  1375. }
  1376. static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
  1377. {
  1378. switch (c->x86_vendor) {
  1379. case X86_VENDOR_INTEL:
  1380. mce_intel_feature_init(c);
  1381. mce_adjust_timer = mce_intel_adjust_timer;
  1382. break;
  1383. case X86_VENDOR_AMD:
  1384. mce_amd_feature_init(c);
  1385. break;
  1386. default:
  1387. break;
  1388. }
  1389. }
  1390. static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  1391. {
  1392. unsigned long iv = mce_adjust_timer(check_interval * HZ);
  1393. __this_cpu_write(mce_next_interval, iv);
  1394. if (mca_cfg.ignore_ce || !iv)
  1395. return;
  1396. t->expires = round_jiffies(jiffies + iv);
  1397. add_timer_on(t, smp_processor_id());
  1398. }
  1399. static void __mcheck_cpu_init_timer(void)
  1400. {
  1401. struct timer_list *t = &__get_cpu_var(mce_timer);
  1402. unsigned int cpu = smp_processor_id();
  1403. setup_timer(t, mce_timer_fn, cpu);
  1404. mce_start_timer(cpu, t);
  1405. }
  1406. /* Handle unconfigured int18 (should never happen) */
  1407. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  1408. {
  1409. pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
  1410. smp_processor_id());
  1411. }
  1412. /* Call the installed machine check handler for this CPU setup. */
  1413. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  1414. unexpected_machine_check;
  1415. /*
  1416. * Called for each booted CPU to set up machine checks.
  1417. * Must be called with preempt off:
  1418. */
  1419. void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
  1420. {
  1421. if (mca_cfg.disabled)
  1422. return;
  1423. if (__mcheck_cpu_ancient_init(c))
  1424. return;
  1425. if (!mce_available(c))
  1426. return;
  1427. if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
  1428. mca_cfg.disabled = true;
  1429. return;
  1430. }
  1431. machine_check_vector = do_machine_check;
  1432. __mcheck_cpu_init_generic();
  1433. __mcheck_cpu_init_vendor(c);
  1434. __mcheck_cpu_init_timer();
  1435. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1436. init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
  1437. }
  1438. /*
  1439. * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
  1440. */
  1441. static DEFINE_SPINLOCK(mce_chrdev_state_lock);
  1442. static int mce_chrdev_open_count; /* #times opened */
  1443. static int mce_chrdev_open_exclu; /* already open exclusive? */
  1444. static int mce_chrdev_open(struct inode *inode, struct file *file)
  1445. {
  1446. spin_lock(&mce_chrdev_state_lock);
  1447. if (mce_chrdev_open_exclu ||
  1448. (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
  1449. spin_unlock(&mce_chrdev_state_lock);
  1450. return -EBUSY;
  1451. }
  1452. if (file->f_flags & O_EXCL)
  1453. mce_chrdev_open_exclu = 1;
  1454. mce_chrdev_open_count++;
  1455. spin_unlock(&mce_chrdev_state_lock);
  1456. return nonseekable_open(inode, file);
  1457. }
  1458. static int mce_chrdev_release(struct inode *inode, struct file *file)
  1459. {
  1460. spin_lock(&mce_chrdev_state_lock);
  1461. mce_chrdev_open_count--;
  1462. mce_chrdev_open_exclu = 0;
  1463. spin_unlock(&mce_chrdev_state_lock);
  1464. return 0;
  1465. }
  1466. static void collect_tscs(void *data)
  1467. {
  1468. unsigned long *cpu_tsc = (unsigned long *)data;
  1469. rdtscll(cpu_tsc[smp_processor_id()]);
  1470. }
  1471. static int mce_apei_read_done;
  1472. /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
  1473. static int __mce_read_apei(char __user **ubuf, size_t usize)
  1474. {
  1475. int rc;
  1476. u64 record_id;
  1477. struct mce m;
  1478. if (usize < sizeof(struct mce))
  1479. return -EINVAL;
  1480. rc = apei_read_mce(&m, &record_id);
  1481. /* Error or no more MCE record */
  1482. if (rc <= 0) {
  1483. mce_apei_read_done = 1;
  1484. /*
  1485. * When ERST is disabled, mce_chrdev_read() should return
  1486. * "no record" instead of "no device."
  1487. */
  1488. if (rc == -ENODEV)
  1489. return 0;
  1490. return rc;
  1491. }
  1492. rc = -EFAULT;
  1493. if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
  1494. return rc;
  1495. /*
  1496. * In fact, we should have cleared the record after that has
  1497. * been flushed to the disk or sent to network in
  1498. * /sbin/mcelog, but we have no interface to support that now,
  1499. * so just clear it to avoid duplication.
  1500. */
  1501. rc = apei_clear_mce(record_id);
  1502. if (rc) {
  1503. mce_apei_read_done = 1;
  1504. return rc;
  1505. }
  1506. *ubuf += sizeof(struct mce);
  1507. return 0;
  1508. }
  1509. static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
  1510. size_t usize, loff_t *off)
  1511. {
  1512. char __user *buf = ubuf;
  1513. unsigned long *cpu_tsc;
  1514. unsigned prev, next;
  1515. int i, err;
  1516. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1517. if (!cpu_tsc)
  1518. return -ENOMEM;
  1519. mutex_lock(&mce_chrdev_read_mutex);
  1520. if (!mce_apei_read_done) {
  1521. err = __mce_read_apei(&buf, usize);
  1522. if (err || buf != ubuf)
  1523. goto out;
  1524. }
  1525. next = rcu_dereference_check_mce(mcelog.next);
  1526. /* Only supports full reads right now */
  1527. err = -EINVAL;
  1528. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
  1529. goto out;
  1530. err = 0;
  1531. prev = 0;
  1532. do {
  1533. for (i = prev; i < next; i++) {
  1534. unsigned long start = jiffies;
  1535. struct mce *m = &mcelog.entry[i];
  1536. while (!m->finished) {
  1537. if (time_after_eq(jiffies, start + 2)) {
  1538. memset(m, 0, sizeof(*m));
  1539. goto timeout;
  1540. }
  1541. cpu_relax();
  1542. }
  1543. smp_rmb();
  1544. err |= copy_to_user(buf, m, sizeof(*m));
  1545. buf += sizeof(*m);
  1546. timeout:
  1547. ;
  1548. }
  1549. memset(mcelog.entry + prev, 0,
  1550. (next - prev) * sizeof(struct mce));
  1551. prev = next;
  1552. next = cmpxchg(&mcelog.next, prev, 0);
  1553. } while (next != prev);
  1554. synchronize_sched();
  1555. /*
  1556. * Collect entries that were still getting written before the
  1557. * synchronize.
  1558. */
  1559. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1560. for (i = next; i < MCE_LOG_LEN; i++) {
  1561. struct mce *m = &mcelog.entry[i];
  1562. if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
  1563. err |= copy_to_user(buf, m, sizeof(*m));
  1564. smp_rmb();
  1565. buf += sizeof(*m);
  1566. memset(m, 0, sizeof(*m));
  1567. }
  1568. }
  1569. if (err)
  1570. err = -EFAULT;
  1571. out:
  1572. mutex_unlock(&mce_chrdev_read_mutex);
  1573. kfree(cpu_tsc);
  1574. return err ? err : buf - ubuf;
  1575. }
  1576. static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
  1577. {
  1578. poll_wait(file, &mce_chrdev_wait, wait);
  1579. if (rcu_access_index(mcelog.next))
  1580. return POLLIN | POLLRDNORM;
  1581. if (!mce_apei_read_done && apei_check_mce())
  1582. return POLLIN | POLLRDNORM;
  1583. return 0;
  1584. }
  1585. static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
  1586. unsigned long arg)
  1587. {
  1588. int __user *p = (int __user *)arg;
  1589. if (!capable(CAP_SYS_ADMIN))
  1590. return -EPERM;
  1591. switch (cmd) {
  1592. case MCE_GET_RECORD_LEN:
  1593. return put_user(sizeof(struct mce), p);
  1594. case MCE_GET_LOG_LEN:
  1595. return put_user(MCE_LOG_LEN, p);
  1596. case MCE_GETCLEAR_FLAGS: {
  1597. unsigned flags;
  1598. do {
  1599. flags = mcelog.flags;
  1600. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1601. return put_user(flags, p);
  1602. }
  1603. default:
  1604. return -ENOTTY;
  1605. }
  1606. }
  1607. static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
  1608. size_t usize, loff_t *off);
  1609. void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
  1610. const char __user *ubuf,
  1611. size_t usize, loff_t *off))
  1612. {
  1613. mce_write = fn;
  1614. }
  1615. EXPORT_SYMBOL_GPL(register_mce_write_callback);
  1616. ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
  1617. size_t usize, loff_t *off)
  1618. {
  1619. if (mce_write)
  1620. return mce_write(filp, ubuf, usize, off);
  1621. else
  1622. return -EINVAL;
  1623. }
  1624. static const struct file_operations mce_chrdev_ops = {
  1625. .open = mce_chrdev_open,
  1626. .release = mce_chrdev_release,
  1627. .read = mce_chrdev_read,
  1628. .write = mce_chrdev_write,
  1629. .poll = mce_chrdev_poll,
  1630. .unlocked_ioctl = mce_chrdev_ioctl,
  1631. .llseek = no_llseek,
  1632. };
  1633. static struct miscdevice mce_chrdev_device = {
  1634. MISC_MCELOG_MINOR,
  1635. "mcelog",
  1636. &mce_chrdev_ops,
  1637. };
  1638. /*
  1639. * mce=off Disables machine check
  1640. * mce=no_cmci Disables CMCI
  1641. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1642. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1643. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1644. * monarchtimeout is how long to wait for other CPUs on machine
  1645. * check, or 0 to not wait
  1646. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1647. * mce=nobootlog Don't log MCEs from before booting.
  1648. * mce=bios_cmci_threshold Don't program the CMCI threshold
  1649. */
  1650. static int __init mcheck_enable(char *str)
  1651. {
  1652. struct mca_config *cfg = &mca_cfg;
  1653. if (*str == 0) {
  1654. enable_p5_mce();
  1655. return 1;
  1656. }
  1657. if (*str == '=')
  1658. str++;
  1659. if (!strcmp(str, "off"))
  1660. cfg->disabled = true;
  1661. else if (!strcmp(str, "no_cmci"))
  1662. cfg->cmci_disabled = true;
  1663. else if (!strcmp(str, "dont_log_ce"))
  1664. cfg->dont_log_ce = true;
  1665. else if (!strcmp(str, "ignore_ce"))
  1666. cfg->ignore_ce = true;
  1667. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1668. cfg->bootlog = (str[0] == 'b');
  1669. else if (!strcmp(str, "bios_cmci_threshold"))
  1670. cfg->bios_cmci_threshold = true;
  1671. else if (isdigit(str[0])) {
  1672. get_option(&str, &(cfg->tolerant));
  1673. if (*str == ',') {
  1674. ++str;
  1675. get_option(&str, &(cfg->monarch_timeout));
  1676. }
  1677. } else {
  1678. pr_info("mce argument %s ignored. Please use /sys\n", str);
  1679. return 0;
  1680. }
  1681. return 1;
  1682. }
  1683. __setup("mce", mcheck_enable);
  1684. int __init mcheck_init(void)
  1685. {
  1686. mcheck_intel_therm_init();
  1687. return 0;
  1688. }
  1689. /*
  1690. * mce_syscore: PM support
  1691. */
  1692. /*
  1693. * Disable machine checks on suspend and shutdown. We can't really handle
  1694. * them later.
  1695. */
  1696. static int mce_disable_error_reporting(void)
  1697. {
  1698. int i;
  1699. for (i = 0; i < mca_cfg.banks; i++) {
  1700. struct mce_bank *b = &mce_banks[i];
  1701. if (b->init)
  1702. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1703. }
  1704. return 0;
  1705. }
  1706. static int mce_syscore_suspend(void)
  1707. {
  1708. return mce_disable_error_reporting();
  1709. }
  1710. static void mce_syscore_shutdown(void)
  1711. {
  1712. mce_disable_error_reporting();
  1713. }
  1714. /*
  1715. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1716. * Only one CPU is active at this time, the others get re-added later using
  1717. * CPU hotplug:
  1718. */
  1719. static void mce_syscore_resume(void)
  1720. {
  1721. __mcheck_cpu_init_generic();
  1722. __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
  1723. }
  1724. static struct syscore_ops mce_syscore_ops = {
  1725. .suspend = mce_syscore_suspend,
  1726. .shutdown = mce_syscore_shutdown,
  1727. .resume = mce_syscore_resume,
  1728. };
  1729. /*
  1730. * mce_device: Sysfs support
  1731. */
  1732. static void mce_cpu_restart(void *data)
  1733. {
  1734. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1735. return;
  1736. __mcheck_cpu_init_generic();
  1737. __mcheck_cpu_init_timer();
  1738. }
  1739. /* Reinit MCEs after user configuration changes */
  1740. static void mce_restart(void)
  1741. {
  1742. mce_timer_delete_all();
  1743. on_each_cpu(mce_cpu_restart, NULL, 1);
  1744. }
  1745. /* Toggle features for corrected errors */
  1746. static void mce_disable_cmci(void *data)
  1747. {
  1748. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1749. return;
  1750. cmci_clear();
  1751. }
  1752. static void mce_enable_ce(void *all)
  1753. {
  1754. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1755. return;
  1756. cmci_reenable();
  1757. cmci_recheck();
  1758. if (all)
  1759. __mcheck_cpu_init_timer();
  1760. }
  1761. static struct bus_type mce_subsys = {
  1762. .name = "machinecheck",
  1763. .dev_name = "machinecheck",
  1764. };
  1765. DEFINE_PER_CPU(struct device *, mce_device);
  1766. __cpuinitdata
  1767. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1768. static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
  1769. {
  1770. return container_of(attr, struct mce_bank, attr);
  1771. }
  1772. static ssize_t show_bank(struct device *s, struct device_attribute *attr,
  1773. char *buf)
  1774. {
  1775. return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
  1776. }
  1777. static ssize_t set_bank(struct device *s, struct device_attribute *attr,
  1778. const char *buf, size_t size)
  1779. {
  1780. u64 new;
  1781. if (strict_strtoull(buf, 0, &new) < 0)
  1782. return -EINVAL;
  1783. attr_to_bank(attr)->ctl = new;
  1784. mce_restart();
  1785. return size;
  1786. }
  1787. static ssize_t
  1788. show_trigger(struct device *s, struct device_attribute *attr, char *buf)
  1789. {
  1790. strcpy(buf, mce_helper);
  1791. strcat(buf, "\n");
  1792. return strlen(mce_helper) + 1;
  1793. }
  1794. static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
  1795. const char *buf, size_t siz)
  1796. {
  1797. char *p;
  1798. strncpy(mce_helper, buf, sizeof(mce_helper));
  1799. mce_helper[sizeof(mce_helper)-1] = 0;
  1800. p = strchr(mce_helper, '\n');
  1801. if (p)
  1802. *p = 0;
  1803. return strlen(mce_helper) + !!p;
  1804. }
  1805. static ssize_t set_ignore_ce(struct device *s,
  1806. struct device_attribute *attr,
  1807. const char *buf, size_t size)
  1808. {
  1809. u64 new;
  1810. if (strict_strtoull(buf, 0, &new) < 0)
  1811. return -EINVAL;
  1812. if (mca_cfg.ignore_ce ^ !!new) {
  1813. if (new) {
  1814. /* disable ce features */
  1815. mce_timer_delete_all();
  1816. on_each_cpu(mce_disable_cmci, NULL, 1);
  1817. mca_cfg.ignore_ce = true;
  1818. } else {
  1819. /* enable ce features */
  1820. mca_cfg.ignore_ce = false;
  1821. on_each_cpu(mce_enable_ce, (void *)1, 1);
  1822. }
  1823. }
  1824. return size;
  1825. }
  1826. static ssize_t set_cmci_disabled(struct device *s,
  1827. struct device_attribute *attr,
  1828. const char *buf, size_t size)
  1829. {
  1830. u64 new;
  1831. if (strict_strtoull(buf, 0, &new) < 0)
  1832. return -EINVAL;
  1833. if (mca_cfg.cmci_disabled ^ !!new) {
  1834. if (new) {
  1835. /* disable cmci */
  1836. on_each_cpu(mce_disable_cmci, NULL, 1);
  1837. mca_cfg.cmci_disabled = true;
  1838. } else {
  1839. /* enable cmci */
  1840. mca_cfg.cmci_disabled = false;
  1841. on_each_cpu(mce_enable_ce, NULL, 1);
  1842. }
  1843. }
  1844. return size;
  1845. }
  1846. static ssize_t store_int_with_restart(struct device *s,
  1847. struct device_attribute *attr,
  1848. const char *buf, size_t size)
  1849. {
  1850. ssize_t ret = device_store_int(s, attr, buf, size);
  1851. mce_restart();
  1852. return ret;
  1853. }
  1854. static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
  1855. static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
  1856. static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
  1857. static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
  1858. static struct dev_ext_attribute dev_attr_check_interval = {
  1859. __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
  1860. &check_interval
  1861. };
  1862. static struct dev_ext_attribute dev_attr_ignore_ce = {
  1863. __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
  1864. &mca_cfg.ignore_ce
  1865. };
  1866. static struct dev_ext_attribute dev_attr_cmci_disabled = {
  1867. __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
  1868. &mca_cfg.cmci_disabled
  1869. };
  1870. static struct device_attribute *mce_device_attrs[] = {
  1871. &dev_attr_tolerant.attr,
  1872. &dev_attr_check_interval.attr,
  1873. &dev_attr_trigger,
  1874. &dev_attr_monarch_timeout.attr,
  1875. &dev_attr_dont_log_ce.attr,
  1876. &dev_attr_ignore_ce.attr,
  1877. &dev_attr_cmci_disabled.attr,
  1878. NULL
  1879. };
  1880. static cpumask_var_t mce_device_initialized;
  1881. static void mce_device_release(struct device *dev)
  1882. {
  1883. kfree(dev);
  1884. }
  1885. /* Per cpu device init. All of the cpus still share the same ctrl bank: */
  1886. static __cpuinit int mce_device_create(unsigned int cpu)
  1887. {
  1888. struct device *dev;
  1889. int err;
  1890. int i, j;
  1891. if (!mce_available(&boot_cpu_data))
  1892. return -EIO;
  1893. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  1894. if (!dev)
  1895. return -ENOMEM;
  1896. dev->id = cpu;
  1897. dev->bus = &mce_subsys;
  1898. dev->release = &mce_device_release;
  1899. err = device_register(dev);
  1900. if (err)
  1901. return err;
  1902. for (i = 0; mce_device_attrs[i]; i++) {
  1903. err = device_create_file(dev, mce_device_attrs[i]);
  1904. if (err)
  1905. goto error;
  1906. }
  1907. for (j = 0; j < mca_cfg.banks; j++) {
  1908. err = device_create_file(dev, &mce_banks[j].attr);
  1909. if (err)
  1910. goto error2;
  1911. }
  1912. cpumask_set_cpu(cpu, mce_device_initialized);
  1913. per_cpu(mce_device, cpu) = dev;
  1914. return 0;
  1915. error2:
  1916. while (--j >= 0)
  1917. device_remove_file(dev, &mce_banks[j].attr);
  1918. error:
  1919. while (--i >= 0)
  1920. device_remove_file(dev, mce_device_attrs[i]);
  1921. device_unregister(dev);
  1922. return err;
  1923. }
  1924. static __cpuinit void mce_device_remove(unsigned int cpu)
  1925. {
  1926. struct device *dev = per_cpu(mce_device, cpu);
  1927. int i;
  1928. if (!cpumask_test_cpu(cpu, mce_device_initialized))
  1929. return;
  1930. for (i = 0; mce_device_attrs[i]; i++)
  1931. device_remove_file(dev, mce_device_attrs[i]);
  1932. for (i = 0; i < mca_cfg.banks; i++)
  1933. device_remove_file(dev, &mce_banks[i].attr);
  1934. device_unregister(dev);
  1935. cpumask_clear_cpu(cpu, mce_device_initialized);
  1936. per_cpu(mce_device, cpu) = NULL;
  1937. }
  1938. /* Make sure there are no machine checks on offlined CPUs. */
  1939. static void __cpuinit mce_disable_cpu(void *h)
  1940. {
  1941. unsigned long action = *(unsigned long *)h;
  1942. int i;
  1943. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1944. return;
  1945. if (!(action & CPU_TASKS_FROZEN))
  1946. cmci_clear();
  1947. for (i = 0; i < mca_cfg.banks; i++) {
  1948. struct mce_bank *b = &mce_banks[i];
  1949. if (b->init)
  1950. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1951. }
  1952. }
  1953. static void __cpuinit mce_reenable_cpu(void *h)
  1954. {
  1955. unsigned long action = *(unsigned long *)h;
  1956. int i;
  1957. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1958. return;
  1959. if (!(action & CPU_TASKS_FROZEN))
  1960. cmci_reenable();
  1961. for (i = 0; i < mca_cfg.banks; i++) {
  1962. struct mce_bank *b = &mce_banks[i];
  1963. if (b->init)
  1964. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1965. }
  1966. }
  1967. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1968. static int __cpuinit
  1969. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1970. {
  1971. unsigned int cpu = (unsigned long)hcpu;
  1972. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1973. switch (action & ~CPU_TASKS_FROZEN) {
  1974. case CPU_ONLINE:
  1975. mce_device_create(cpu);
  1976. if (threshold_cpu_callback)
  1977. threshold_cpu_callback(action, cpu);
  1978. break;
  1979. case CPU_DEAD:
  1980. if (threshold_cpu_callback)
  1981. threshold_cpu_callback(action, cpu);
  1982. mce_device_remove(cpu);
  1983. mce_intel_hcpu_update(cpu);
  1984. break;
  1985. case CPU_DOWN_PREPARE:
  1986. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1987. del_timer_sync(t);
  1988. break;
  1989. case CPU_DOWN_FAILED:
  1990. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1991. mce_start_timer(cpu, t);
  1992. break;
  1993. }
  1994. if (action == CPU_POST_DEAD) {
  1995. /* intentionally ignoring frozen here */
  1996. cmci_rediscover(cpu);
  1997. }
  1998. return NOTIFY_OK;
  1999. }
  2000. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  2001. .notifier_call = mce_cpu_callback,
  2002. };
  2003. static __init void mce_init_banks(void)
  2004. {
  2005. int i;
  2006. for (i = 0; i < mca_cfg.banks; i++) {
  2007. struct mce_bank *b = &mce_banks[i];
  2008. struct device_attribute *a = &b->attr;
  2009. sysfs_attr_init(&a->attr);
  2010. a->attr.name = b->attrname;
  2011. snprintf(b->attrname, ATTR_LEN, "bank%d", i);
  2012. a->attr.mode = 0644;
  2013. a->show = show_bank;
  2014. a->store = set_bank;
  2015. }
  2016. }
  2017. static __init int mcheck_init_device(void)
  2018. {
  2019. int err;
  2020. int i = 0;
  2021. if (!mce_available(&boot_cpu_data))
  2022. return -EIO;
  2023. zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
  2024. mce_init_banks();
  2025. err = subsys_system_register(&mce_subsys, NULL);
  2026. if (err)
  2027. return err;
  2028. for_each_online_cpu(i) {
  2029. err = mce_device_create(i);
  2030. if (err)
  2031. return err;
  2032. }
  2033. register_syscore_ops(&mce_syscore_ops);
  2034. register_hotcpu_notifier(&mce_cpu_notifier);
  2035. /* register character device /dev/mcelog */
  2036. misc_register(&mce_chrdev_device);
  2037. return err;
  2038. }
  2039. device_initcall_sync(mcheck_init_device);
  2040. /*
  2041. * Old style boot options parsing. Only for compatibility.
  2042. */
  2043. static int __init mcheck_disable(char *str)
  2044. {
  2045. mca_cfg.disabled = true;
  2046. return 1;
  2047. }
  2048. __setup("nomce", mcheck_disable);
  2049. #ifdef CONFIG_DEBUG_FS
  2050. struct dentry *mce_get_debugfs_dir(void)
  2051. {
  2052. static struct dentry *dmce;
  2053. if (!dmce)
  2054. dmce = debugfs_create_dir("mce", NULL);
  2055. return dmce;
  2056. }
  2057. static void mce_reset(void)
  2058. {
  2059. cpu_missing = 0;
  2060. atomic_set(&mce_fake_paniced, 0);
  2061. atomic_set(&mce_executing, 0);
  2062. atomic_set(&mce_callin, 0);
  2063. atomic_set(&global_nwo, 0);
  2064. }
  2065. static int fake_panic_get(void *data, u64 *val)
  2066. {
  2067. *val = fake_panic;
  2068. return 0;
  2069. }
  2070. static int fake_panic_set(void *data, u64 val)
  2071. {
  2072. mce_reset();
  2073. fake_panic = val;
  2074. return 0;
  2075. }
  2076. DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
  2077. fake_panic_set, "%llu\n");
  2078. static int __init mcheck_debugfs_init(void)
  2079. {
  2080. struct dentry *dmce, *ffake_panic;
  2081. dmce = mce_get_debugfs_dir();
  2082. if (!dmce)
  2083. return -ENOMEM;
  2084. ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
  2085. &fake_panic_fops);
  2086. if (!ffake_panic)
  2087. return -ENOMEM;
  2088. return 0;
  2089. }
  2090. late_initcall(mcheck_debugfs_init);
  2091. #endif