mce.c 60 KB

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