mce.c 57 KB

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