mce.c 53 KB

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