mce.c 59 KB

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