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