mce.c 58 KB

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