mce.c 58 KB

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