mce.c 55 KB

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