mce.c 57 KB

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