mce.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  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. /* MCA banks polled by the period polling timer for corrected events */
  77. DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  78. [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
  79. };
  80. static DEFINE_PER_CPU(struct work_struct, mce_work);
  81. static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
  82. /*
  83. * CPU/chipset specific EDAC code can register a notifier call here to print
  84. * MCE errors in a human-readable form.
  85. */
  86. ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
  87. /* Do initial initialization of a struct mce */
  88. void mce_setup(struct mce *m)
  89. {
  90. memset(m, 0, sizeof(struct mce));
  91. m->cpu = m->extcpu = smp_processor_id();
  92. rdtscll(m->tsc);
  93. /* We hope get_seconds stays lockless */
  94. m->time = get_seconds();
  95. m->cpuvendor = boot_cpu_data.x86_vendor;
  96. m->cpuid = cpuid_eax(1);
  97. m->socketid = cpu_data(m->extcpu).phys_proc_id;
  98. m->apicid = cpu_data(m->extcpu).initial_apicid;
  99. rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
  100. }
  101. DEFINE_PER_CPU(struct mce, injectm);
  102. EXPORT_PER_CPU_SYMBOL_GPL(injectm);
  103. /*
  104. * Lockless MCE logging infrastructure.
  105. * This avoids deadlocks on printk locks without having to break locks. Also
  106. * separate MCEs from kernel messages to avoid bogus bug reports.
  107. */
  108. static struct mce_log mcelog = {
  109. .signature = MCE_LOG_SIGNATURE,
  110. .len = MCE_LOG_LEN,
  111. .recordlen = sizeof(struct mce),
  112. };
  113. void mce_log(struct mce *mce)
  114. {
  115. unsigned next, entry;
  116. int ret = 0;
  117. /* Emit the trace record: */
  118. trace_mce_record(mce);
  119. ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, mce);
  120. if (ret == NOTIFY_STOP)
  121. return;
  122. mce->finished = 0;
  123. wmb();
  124. for (;;) {
  125. entry = rcu_dereference_check_mce(mcelog.next);
  126. for (;;) {
  127. /*
  128. * When the buffer fills up discard new entries.
  129. * Assume that the earlier errors are the more
  130. * interesting ones:
  131. */
  132. if (entry >= MCE_LOG_LEN) {
  133. set_bit(MCE_OVERFLOW,
  134. (unsigned long *)&mcelog.flags);
  135. return;
  136. }
  137. /* Old left over entry. Skip: */
  138. if (mcelog.entry[entry].finished) {
  139. entry++;
  140. continue;
  141. }
  142. break;
  143. }
  144. smp_rmb();
  145. next = entry + 1;
  146. if (cmpxchg(&mcelog.next, entry, next) == entry)
  147. break;
  148. }
  149. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  150. wmb();
  151. mcelog.entry[entry].finished = 1;
  152. wmb();
  153. mce->finished = 1;
  154. set_bit(0, &mce_need_notify);
  155. }
  156. static void drain_mcelog_buffer(void)
  157. {
  158. unsigned int next, i, prev = 0;
  159. next = ACCESS_ONCE(mcelog.next);
  160. do {
  161. struct mce *m;
  162. /* drain what was logged during boot */
  163. for (i = prev; i < next; i++) {
  164. unsigned long start = jiffies;
  165. unsigned retries = 1;
  166. m = &mcelog.entry[i];
  167. while (!m->finished) {
  168. if (time_after_eq(jiffies, start + 2*retries))
  169. retries++;
  170. cpu_relax();
  171. if (!m->finished && retries >= 4) {
  172. pr_err("skipping error being logged currently!\n");
  173. break;
  174. }
  175. }
  176. smp_rmb();
  177. atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
  178. }
  179. memset(mcelog.entry + prev, 0, (next - prev) * sizeof(*m));
  180. prev = next;
  181. next = cmpxchg(&mcelog.next, prev, 0);
  182. } while (next != prev);
  183. }
  184. void mce_register_decode_chain(struct notifier_block *nb)
  185. {
  186. atomic_notifier_chain_register(&x86_mce_decoder_chain, nb);
  187. drain_mcelog_buffer();
  188. }
  189. EXPORT_SYMBOL_GPL(mce_register_decode_chain);
  190. void mce_unregister_decode_chain(struct notifier_block *nb)
  191. {
  192. atomic_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
  193. }
  194. EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
  195. static void print_mce(struct mce *m)
  196. {
  197. int ret = 0;
  198. pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  199. m->extcpu, m->mcgstatus, m->bank, m->status);
  200. if (m->ip) {
  201. pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
  202. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  203. m->cs, m->ip);
  204. if (m->cs == __KERNEL_CS)
  205. print_symbol("{%s}", m->ip);
  206. pr_cont("\n");
  207. }
  208. pr_emerg(HW_ERR "TSC %llx ", m->tsc);
  209. if (m->addr)
  210. pr_cont("ADDR %llx ", m->addr);
  211. if (m->misc)
  212. pr_cont("MISC %llx ", m->misc);
  213. pr_cont("\n");
  214. /*
  215. * Note this output is parsed by external tools and old fields
  216. * should not be changed.
  217. */
  218. pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
  219. m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
  220. cpu_data(m->extcpu).microcode);
  221. /*
  222. * Print out human-readable details about the MCE error,
  223. * (if the CPU has an implementation for that)
  224. */
  225. ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
  226. if (ret == NOTIFY_STOP)
  227. return;
  228. pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
  229. }
  230. #define PANIC_TIMEOUT 5 /* 5 seconds */
  231. static atomic_t mce_paniced;
  232. static int fake_panic;
  233. static atomic_t mce_fake_paniced;
  234. /* Panic in progress. Enable interrupts and wait for final IPI */
  235. static void wait_for_panic(void)
  236. {
  237. long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
  238. preempt_disable();
  239. local_irq_enable();
  240. while (timeout-- > 0)
  241. udelay(1);
  242. if (panic_timeout == 0)
  243. panic_timeout = mca_cfg.panic_timeout;
  244. panic("Panicing machine check CPU died");
  245. }
  246. static void mce_panic(char *msg, struct mce *final, char *exp)
  247. {
  248. int i, apei_err = 0;
  249. if (!fake_panic) {
  250. /*
  251. * Make sure only one CPU runs in machine check panic
  252. */
  253. if (atomic_inc_return(&mce_paniced) > 1)
  254. wait_for_panic();
  255. barrier();
  256. bust_spinlocks(1);
  257. console_verbose();
  258. } else {
  259. /* Don't log too much for fake panic */
  260. if (atomic_inc_return(&mce_fake_paniced) > 1)
  261. return;
  262. }
  263. /* First print corrected ones that are still unlogged */
  264. for (i = 0; i < MCE_LOG_LEN; i++) {
  265. struct mce *m = &mcelog.entry[i];
  266. if (!(m->status & MCI_STATUS_VAL))
  267. continue;
  268. if (!(m->status & MCI_STATUS_UC)) {
  269. print_mce(m);
  270. if (!apei_err)
  271. apei_err = apei_write_mce(m);
  272. }
  273. }
  274. /* Now print uncorrected but with the final one last */
  275. for (i = 0; i < MCE_LOG_LEN; i++) {
  276. struct mce *m = &mcelog.entry[i];
  277. if (!(m->status & MCI_STATUS_VAL))
  278. continue;
  279. if (!(m->status & MCI_STATUS_UC))
  280. continue;
  281. if (!final || memcmp(m, final, sizeof(struct mce))) {
  282. print_mce(m);
  283. if (!apei_err)
  284. apei_err = apei_write_mce(m);
  285. }
  286. }
  287. if (final) {
  288. print_mce(final);
  289. if (!apei_err)
  290. apei_err = apei_write_mce(final);
  291. }
  292. if (cpu_missing)
  293. pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
  294. if (exp)
  295. pr_emerg(HW_ERR "Machine check: %s\n", exp);
  296. if (!fake_panic) {
  297. if (panic_timeout == 0)
  298. panic_timeout = mca_cfg.panic_timeout;
  299. panic(msg);
  300. } else
  301. pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
  302. }
  303. /* Support code for software error injection */
  304. static int msr_to_offset(u32 msr)
  305. {
  306. unsigned bank = __this_cpu_read(injectm.bank);
  307. if (msr == mca_cfg.rip_msr)
  308. return offsetof(struct mce, ip);
  309. if (msr == MSR_IA32_MCx_STATUS(bank))
  310. return offsetof(struct mce, status);
  311. if (msr == MSR_IA32_MCx_ADDR(bank))
  312. return offsetof(struct mce, addr);
  313. if (msr == MSR_IA32_MCx_MISC(bank))
  314. return offsetof(struct mce, misc);
  315. if (msr == MSR_IA32_MCG_STATUS)
  316. return offsetof(struct mce, mcgstatus);
  317. return -1;
  318. }
  319. /* MSR access wrappers used for error injection */
  320. static u64 mce_rdmsrl(u32 msr)
  321. {
  322. u64 v;
  323. if (__this_cpu_read(injectm.finished)) {
  324. int offset = msr_to_offset(msr);
  325. if (offset < 0)
  326. return 0;
  327. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  328. }
  329. if (rdmsrl_safe(msr, &v)) {
  330. WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
  331. /*
  332. * Return zero in case the access faulted. This should
  333. * not happen normally but can happen if the CPU does
  334. * something weird, or if the code is buggy.
  335. */
  336. v = 0;
  337. }
  338. return v;
  339. }
  340. static void mce_wrmsrl(u32 msr, u64 v)
  341. {
  342. if (__this_cpu_read(injectm.finished)) {
  343. int offset = msr_to_offset(msr);
  344. if (offset >= 0)
  345. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  346. return;
  347. }
  348. wrmsrl(msr, v);
  349. }
  350. /*
  351. * Collect all global (w.r.t. this processor) status about this machine
  352. * check into our "mce" struct so that we can use it later to assess
  353. * the severity of the problem as we read per-bank specific details.
  354. */
  355. static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
  356. {
  357. mce_setup(m);
  358. m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  359. if (regs) {
  360. /*
  361. * Get the address of the instruction at the time of
  362. * the machine check error.
  363. */
  364. if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
  365. m->ip = regs->ip;
  366. m->cs = regs->cs;
  367. /*
  368. * When in VM86 mode make the cs look like ring 3
  369. * always. This is a lie, but it's better than passing
  370. * the additional vm86 bit around everywhere.
  371. */
  372. if (v8086_mode(regs))
  373. m->cs |= 3;
  374. }
  375. /* Use accurate RIP reporting if available. */
  376. if (mca_cfg.rip_msr)
  377. m->ip = mce_rdmsrl(mca_cfg.rip_msr);
  378. }
  379. }
  380. /*
  381. * Simple lockless ring to communicate PFNs from the exception handler with the
  382. * process context work function. This is vastly simplified because there's
  383. * only a single reader and a single writer.
  384. */
  385. #define MCE_RING_SIZE 16 /* we use one entry less */
  386. struct mce_ring {
  387. unsigned short start;
  388. unsigned short end;
  389. unsigned long ring[MCE_RING_SIZE];
  390. };
  391. static DEFINE_PER_CPU(struct mce_ring, mce_ring);
  392. /* Runs with CPU affinity in workqueue */
  393. static int mce_ring_empty(void)
  394. {
  395. struct mce_ring *r = &__get_cpu_var(mce_ring);
  396. return r->start == r->end;
  397. }
  398. static int mce_ring_get(unsigned long *pfn)
  399. {
  400. struct mce_ring *r;
  401. int ret = 0;
  402. *pfn = 0;
  403. get_cpu();
  404. r = &__get_cpu_var(mce_ring);
  405. if (r->start == r->end)
  406. goto out;
  407. *pfn = r->ring[r->start];
  408. r->start = (r->start + 1) % MCE_RING_SIZE;
  409. ret = 1;
  410. out:
  411. put_cpu();
  412. return ret;
  413. }
  414. /* Always runs in MCE context with preempt off */
  415. static int mce_ring_add(unsigned long pfn)
  416. {
  417. struct mce_ring *r = &__get_cpu_var(mce_ring);
  418. unsigned next;
  419. next = (r->end + 1) % MCE_RING_SIZE;
  420. if (next == r->start)
  421. return -1;
  422. r->ring[r->end] = pfn;
  423. wmb();
  424. r->end = next;
  425. return 0;
  426. }
  427. int mce_available(struct cpuinfo_x86 *c)
  428. {
  429. if (mca_cfg.disabled)
  430. return 0;
  431. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  432. }
  433. static void mce_schedule_work(void)
  434. {
  435. if (!mce_ring_empty()) {
  436. struct work_struct *work = &__get_cpu_var(mce_work);
  437. if (!work_pending(work))
  438. schedule_work(work);
  439. }
  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);
  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. /*
  1164. * There is no risk of missing notifications because
  1165. * work_pending is always cleared before the function is
  1166. * executed.
  1167. */
  1168. if (mce_helper[0] && !work_pending(&mce_trigger_work))
  1169. schedule_work(&mce_trigger_work);
  1170. if (__ratelimit(&ratelimit))
  1171. pr_info(HW_ERR "Machine check events logged\n");
  1172. return 1;
  1173. }
  1174. return 0;
  1175. }
  1176. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1177. static int __cpuinit __mcheck_cpu_mce_banks_init(void)
  1178. {
  1179. int i;
  1180. u8 num_banks = mca_cfg.banks;
  1181. mce_banks = kzalloc(num_banks * sizeof(struct mce_bank), GFP_KERNEL);
  1182. if (!mce_banks)
  1183. return -ENOMEM;
  1184. for (i = 0; i < num_banks; i++) {
  1185. struct mce_bank *b = &mce_banks[i];
  1186. b->ctl = -1ULL;
  1187. b->init = 1;
  1188. }
  1189. return 0;
  1190. }
  1191. /*
  1192. * Initialize Machine Checks for a CPU.
  1193. */
  1194. static int __cpuinit __mcheck_cpu_cap_init(void)
  1195. {
  1196. unsigned b;
  1197. u64 cap;
  1198. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1199. b = cap & MCG_BANKCNT_MASK;
  1200. if (!mca_cfg.banks)
  1201. pr_info("CPU supports %d MCE banks\n", b);
  1202. if (b > MAX_NR_BANKS) {
  1203. pr_warn("Using only %u machine check banks out of %u\n",
  1204. MAX_NR_BANKS, b);
  1205. b = MAX_NR_BANKS;
  1206. }
  1207. /* Don't support asymmetric configurations today */
  1208. WARN_ON(mca_cfg.banks != 0 && b != mca_cfg.banks);
  1209. mca_cfg.banks = b;
  1210. if (!mce_banks) {
  1211. int err = __mcheck_cpu_mce_banks_init();
  1212. if (err)
  1213. return err;
  1214. }
  1215. /* Use accurate RIP reporting if available. */
  1216. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1217. mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
  1218. if (cap & MCG_SER_P)
  1219. mca_cfg.ser = true;
  1220. return 0;
  1221. }
  1222. static void __mcheck_cpu_init_generic(void)
  1223. {
  1224. enum mcp_flags m_fl = 0;
  1225. mce_banks_t all_banks;
  1226. u64 cap;
  1227. int i;
  1228. if (!mca_cfg.bootlog)
  1229. m_fl = MCP_DONTLOG;
  1230. /*
  1231. * Log the machine checks left over from the previous reset.
  1232. */
  1233. bitmap_fill(all_banks, MAX_NR_BANKS);
  1234. machine_check_poll(MCP_UC | m_fl, &all_banks);
  1235. set_in_cr4(X86_CR4_MCE);
  1236. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1237. if (cap & MCG_CTL_P)
  1238. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1239. for (i = 0; i < mca_cfg.banks; i++) {
  1240. struct mce_bank *b = &mce_banks[i];
  1241. if (!b->init)
  1242. continue;
  1243. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1244. wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  1245. }
  1246. }
  1247. /*
  1248. * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
  1249. * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
  1250. * Vol 3B Table 15-20). But this confuses both the code that determines
  1251. * whether the machine check occurred in kernel or user mode, and also
  1252. * the severity assessment code. Pretend that EIPV was set, and take the
  1253. * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
  1254. */
  1255. static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
  1256. {
  1257. if (bank != 0)
  1258. return;
  1259. if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
  1260. return;
  1261. if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
  1262. MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
  1263. MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
  1264. MCACOD)) !=
  1265. (MCI_STATUS_UC|MCI_STATUS_EN|
  1266. MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
  1267. MCI_STATUS_AR|MCACOD_INSTR))
  1268. return;
  1269. m->mcgstatus |= MCG_STATUS_EIPV;
  1270. m->ip = regs->ip;
  1271. m->cs = regs->cs;
  1272. }
  1273. /* Add per CPU specific workarounds here */
  1274. static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
  1275. {
  1276. struct mca_config *cfg = &mca_cfg;
  1277. if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
  1278. pr_info("unknown CPU type - not enabling MCE support\n");
  1279. return -EOPNOTSUPP;
  1280. }
  1281. /* This should be disabled by the BIOS, but isn't always */
  1282. if (c->x86_vendor == X86_VENDOR_AMD) {
  1283. if (c->x86 == 15 && cfg->banks > 4) {
  1284. /*
  1285. * disable GART TBL walk error reporting, which
  1286. * trips off incorrectly with the IOMMU & 3ware
  1287. * & Cerberus:
  1288. */
  1289. clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
  1290. }
  1291. if (c->x86 <= 17 && cfg->bootlog < 0) {
  1292. /*
  1293. * Lots of broken BIOS around that don't clear them
  1294. * by default and leave crap in there. Don't log:
  1295. */
  1296. cfg->bootlog = 0;
  1297. }
  1298. /*
  1299. * Various K7s with broken bank 0 around. Always disable
  1300. * by default.
  1301. */
  1302. if (c->x86 == 6 && cfg->banks > 0)
  1303. mce_banks[0].ctl = 0;
  1304. /*
  1305. * Turn off MC4_MISC thresholding banks on those models since
  1306. * they're not supported there.
  1307. */
  1308. if (c->x86 == 0x15 &&
  1309. (c->x86_model >= 0x10 && c->x86_model <= 0x1f)) {
  1310. int i;
  1311. u64 val, hwcr;
  1312. bool need_toggle;
  1313. u32 msrs[] = {
  1314. 0x00000413, /* MC4_MISC0 */
  1315. 0xc0000408, /* MC4_MISC1 */
  1316. };
  1317. rdmsrl(MSR_K7_HWCR, hwcr);
  1318. /* McStatusWrEn has to be set */
  1319. need_toggle = !(hwcr & BIT(18));
  1320. if (need_toggle)
  1321. wrmsrl(MSR_K7_HWCR, hwcr | BIT(18));
  1322. for (i = 0; i < ARRAY_SIZE(msrs); i++) {
  1323. rdmsrl(msrs[i], val);
  1324. /* CntP bit set? */
  1325. if (val & BIT_64(62)) {
  1326. val &= ~BIT_64(62);
  1327. wrmsrl(msrs[i], val);
  1328. }
  1329. }
  1330. /* restore old settings */
  1331. if (need_toggle)
  1332. wrmsrl(MSR_K7_HWCR, hwcr);
  1333. }
  1334. }
  1335. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1336. /*
  1337. * SDM documents that on family 6 bank 0 should not be written
  1338. * because it aliases to another special BIOS controlled
  1339. * register.
  1340. * But it's not aliased anymore on model 0x1a+
  1341. * Don't ignore bank 0 completely because there could be a
  1342. * valid event later, merely don't write CTL0.
  1343. */
  1344. if (c->x86 == 6 && c->x86_model < 0x1A && cfg->banks > 0)
  1345. mce_banks[0].init = 0;
  1346. /*
  1347. * All newer Intel systems support MCE broadcasting. Enable
  1348. * synchronization with a one second timeout.
  1349. */
  1350. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1351. cfg->monarch_timeout < 0)
  1352. cfg->monarch_timeout = USEC_PER_SEC;
  1353. /*
  1354. * There are also broken BIOSes on some Pentium M and
  1355. * earlier systems:
  1356. */
  1357. if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
  1358. cfg->bootlog = 0;
  1359. if (c->x86 == 6 && c->x86_model == 45)
  1360. quirk_no_way_out = quirk_sandybridge_ifu;
  1361. }
  1362. if (cfg->monarch_timeout < 0)
  1363. cfg->monarch_timeout = 0;
  1364. if (cfg->bootlog != 0)
  1365. cfg->panic_timeout = 30;
  1366. return 0;
  1367. }
  1368. static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
  1369. {
  1370. if (c->x86 != 5)
  1371. return 0;
  1372. switch (c->x86_vendor) {
  1373. case X86_VENDOR_INTEL:
  1374. intel_p5_mcheck_init(c);
  1375. return 1;
  1376. break;
  1377. case X86_VENDOR_CENTAUR:
  1378. winchip_mcheck_init(c);
  1379. return 1;
  1380. break;
  1381. }
  1382. return 0;
  1383. }
  1384. static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
  1385. {
  1386. switch (c->x86_vendor) {
  1387. case X86_VENDOR_INTEL:
  1388. mce_intel_feature_init(c);
  1389. mce_adjust_timer = mce_intel_adjust_timer;
  1390. break;
  1391. case X86_VENDOR_AMD:
  1392. mce_amd_feature_init(c);
  1393. break;
  1394. default:
  1395. break;
  1396. }
  1397. }
  1398. static void mce_start_timer(unsigned int cpu, struct timer_list *t)
  1399. {
  1400. unsigned long iv = mce_adjust_timer(check_interval * HZ);
  1401. __this_cpu_write(mce_next_interval, iv);
  1402. if (mca_cfg.ignore_ce || !iv)
  1403. return;
  1404. t->expires = round_jiffies(jiffies + iv);
  1405. add_timer_on(t, smp_processor_id());
  1406. }
  1407. static void __mcheck_cpu_init_timer(void)
  1408. {
  1409. struct timer_list *t = &__get_cpu_var(mce_timer);
  1410. unsigned int cpu = smp_processor_id();
  1411. setup_timer(t, mce_timer_fn, cpu);
  1412. mce_start_timer(cpu, t);
  1413. }
  1414. /* Handle unconfigured int18 (should never happen) */
  1415. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  1416. {
  1417. pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
  1418. smp_processor_id());
  1419. }
  1420. /* Call the installed machine check handler for this CPU setup. */
  1421. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  1422. unexpected_machine_check;
  1423. /*
  1424. * Called for each booted CPU to set up machine checks.
  1425. * Must be called with preempt off:
  1426. */
  1427. void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
  1428. {
  1429. if (mca_cfg.disabled)
  1430. return;
  1431. if (__mcheck_cpu_ancient_init(c))
  1432. return;
  1433. if (!mce_available(c))
  1434. return;
  1435. if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
  1436. mca_cfg.disabled = true;
  1437. return;
  1438. }
  1439. machine_check_vector = do_machine_check;
  1440. __mcheck_cpu_init_generic();
  1441. __mcheck_cpu_init_vendor(c);
  1442. __mcheck_cpu_init_timer();
  1443. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1444. init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
  1445. }
  1446. /*
  1447. * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
  1448. */
  1449. static DEFINE_SPINLOCK(mce_chrdev_state_lock);
  1450. static int mce_chrdev_open_count; /* #times opened */
  1451. static int mce_chrdev_open_exclu; /* already open exclusive? */
  1452. static int mce_chrdev_open(struct inode *inode, struct file *file)
  1453. {
  1454. spin_lock(&mce_chrdev_state_lock);
  1455. if (mce_chrdev_open_exclu ||
  1456. (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
  1457. spin_unlock(&mce_chrdev_state_lock);
  1458. return -EBUSY;
  1459. }
  1460. if (file->f_flags & O_EXCL)
  1461. mce_chrdev_open_exclu = 1;
  1462. mce_chrdev_open_count++;
  1463. spin_unlock(&mce_chrdev_state_lock);
  1464. return nonseekable_open(inode, file);
  1465. }
  1466. static int mce_chrdev_release(struct inode *inode, struct file *file)
  1467. {
  1468. spin_lock(&mce_chrdev_state_lock);
  1469. mce_chrdev_open_count--;
  1470. mce_chrdev_open_exclu = 0;
  1471. spin_unlock(&mce_chrdev_state_lock);
  1472. return 0;
  1473. }
  1474. static void collect_tscs(void *data)
  1475. {
  1476. unsigned long *cpu_tsc = (unsigned long *)data;
  1477. rdtscll(cpu_tsc[smp_processor_id()]);
  1478. }
  1479. static int mce_apei_read_done;
  1480. /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
  1481. static int __mce_read_apei(char __user **ubuf, size_t usize)
  1482. {
  1483. int rc;
  1484. u64 record_id;
  1485. struct mce m;
  1486. if (usize < sizeof(struct mce))
  1487. return -EINVAL;
  1488. rc = apei_read_mce(&m, &record_id);
  1489. /* Error or no more MCE record */
  1490. if (rc <= 0) {
  1491. mce_apei_read_done = 1;
  1492. /*
  1493. * When ERST is disabled, mce_chrdev_read() should return
  1494. * "no record" instead of "no device."
  1495. */
  1496. if (rc == -ENODEV)
  1497. return 0;
  1498. return rc;
  1499. }
  1500. rc = -EFAULT;
  1501. if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
  1502. return rc;
  1503. /*
  1504. * In fact, we should have cleared the record after that has
  1505. * been flushed to the disk or sent to network in
  1506. * /sbin/mcelog, but we have no interface to support that now,
  1507. * so just clear it to avoid duplication.
  1508. */
  1509. rc = apei_clear_mce(record_id);
  1510. if (rc) {
  1511. mce_apei_read_done = 1;
  1512. return rc;
  1513. }
  1514. *ubuf += sizeof(struct mce);
  1515. return 0;
  1516. }
  1517. static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
  1518. size_t usize, loff_t *off)
  1519. {
  1520. char __user *buf = ubuf;
  1521. unsigned long *cpu_tsc;
  1522. unsigned prev, next;
  1523. int i, err;
  1524. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1525. if (!cpu_tsc)
  1526. return -ENOMEM;
  1527. mutex_lock(&mce_chrdev_read_mutex);
  1528. if (!mce_apei_read_done) {
  1529. err = __mce_read_apei(&buf, usize);
  1530. if (err || buf != ubuf)
  1531. goto out;
  1532. }
  1533. next = rcu_dereference_check_mce(mcelog.next);
  1534. /* Only supports full reads right now */
  1535. err = -EINVAL;
  1536. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
  1537. goto out;
  1538. err = 0;
  1539. prev = 0;
  1540. do {
  1541. for (i = prev; i < next; i++) {
  1542. unsigned long start = jiffies;
  1543. struct mce *m = &mcelog.entry[i];
  1544. while (!m->finished) {
  1545. if (time_after_eq(jiffies, start + 2)) {
  1546. memset(m, 0, sizeof(*m));
  1547. goto timeout;
  1548. }
  1549. cpu_relax();
  1550. }
  1551. smp_rmb();
  1552. err |= copy_to_user(buf, m, sizeof(*m));
  1553. buf += sizeof(*m);
  1554. timeout:
  1555. ;
  1556. }
  1557. memset(mcelog.entry + prev, 0,
  1558. (next - prev) * sizeof(struct mce));
  1559. prev = next;
  1560. next = cmpxchg(&mcelog.next, prev, 0);
  1561. } while (next != prev);
  1562. synchronize_sched();
  1563. /*
  1564. * Collect entries that were still getting written before the
  1565. * synchronize.
  1566. */
  1567. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1568. for (i = next; i < MCE_LOG_LEN; i++) {
  1569. struct mce *m = &mcelog.entry[i];
  1570. if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
  1571. err |= copy_to_user(buf, m, sizeof(*m));
  1572. smp_rmb();
  1573. buf += sizeof(*m);
  1574. memset(m, 0, sizeof(*m));
  1575. }
  1576. }
  1577. if (err)
  1578. err = -EFAULT;
  1579. out:
  1580. mutex_unlock(&mce_chrdev_read_mutex);
  1581. kfree(cpu_tsc);
  1582. return err ? err : buf - ubuf;
  1583. }
  1584. static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
  1585. {
  1586. poll_wait(file, &mce_chrdev_wait, wait);
  1587. if (rcu_access_index(mcelog.next))
  1588. return POLLIN | POLLRDNORM;
  1589. if (!mce_apei_read_done && apei_check_mce())
  1590. return POLLIN | POLLRDNORM;
  1591. return 0;
  1592. }
  1593. static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
  1594. unsigned long arg)
  1595. {
  1596. int __user *p = (int __user *)arg;
  1597. if (!capable(CAP_SYS_ADMIN))
  1598. return -EPERM;
  1599. switch (cmd) {
  1600. case MCE_GET_RECORD_LEN:
  1601. return put_user(sizeof(struct mce), p);
  1602. case MCE_GET_LOG_LEN:
  1603. return put_user(MCE_LOG_LEN, p);
  1604. case MCE_GETCLEAR_FLAGS: {
  1605. unsigned flags;
  1606. do {
  1607. flags = mcelog.flags;
  1608. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1609. return put_user(flags, p);
  1610. }
  1611. default:
  1612. return -ENOTTY;
  1613. }
  1614. }
  1615. static ssize_t (*mce_write)(struct file *filp, const char __user *ubuf,
  1616. size_t usize, loff_t *off);
  1617. void register_mce_write_callback(ssize_t (*fn)(struct file *filp,
  1618. const char __user *ubuf,
  1619. size_t usize, loff_t *off))
  1620. {
  1621. mce_write = fn;
  1622. }
  1623. EXPORT_SYMBOL_GPL(register_mce_write_callback);
  1624. ssize_t mce_chrdev_write(struct file *filp, const char __user *ubuf,
  1625. size_t usize, loff_t *off)
  1626. {
  1627. if (mce_write)
  1628. return mce_write(filp, ubuf, usize, off);
  1629. else
  1630. return -EINVAL;
  1631. }
  1632. static const struct file_operations mce_chrdev_ops = {
  1633. .open = mce_chrdev_open,
  1634. .release = mce_chrdev_release,
  1635. .read = mce_chrdev_read,
  1636. .write = mce_chrdev_write,
  1637. .poll = mce_chrdev_poll,
  1638. .unlocked_ioctl = mce_chrdev_ioctl,
  1639. .llseek = no_llseek,
  1640. };
  1641. static struct miscdevice mce_chrdev_device = {
  1642. MISC_MCELOG_MINOR,
  1643. "mcelog",
  1644. &mce_chrdev_ops,
  1645. };
  1646. /*
  1647. * mce=off Disables machine check
  1648. * mce=no_cmci Disables CMCI
  1649. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1650. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1651. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1652. * monarchtimeout is how long to wait for other CPUs on machine
  1653. * check, or 0 to not wait
  1654. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1655. * mce=nobootlog Don't log MCEs from before booting.
  1656. * mce=bios_cmci_threshold Don't program the CMCI threshold
  1657. */
  1658. static int __init mcheck_enable(char *str)
  1659. {
  1660. struct mca_config *cfg = &mca_cfg;
  1661. if (*str == 0) {
  1662. enable_p5_mce();
  1663. return 1;
  1664. }
  1665. if (*str == '=')
  1666. str++;
  1667. if (!strcmp(str, "off"))
  1668. cfg->disabled = true;
  1669. else if (!strcmp(str, "no_cmci"))
  1670. cfg->cmci_disabled = true;
  1671. else if (!strcmp(str, "dont_log_ce"))
  1672. cfg->dont_log_ce = true;
  1673. else if (!strcmp(str, "ignore_ce"))
  1674. cfg->ignore_ce = true;
  1675. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1676. cfg->bootlog = (str[0] == 'b');
  1677. else if (!strcmp(str, "bios_cmci_threshold"))
  1678. cfg->bios_cmci_threshold = true;
  1679. else if (isdigit(str[0])) {
  1680. get_option(&str, &(cfg->tolerant));
  1681. if (*str == ',') {
  1682. ++str;
  1683. get_option(&str, &(cfg->monarch_timeout));
  1684. }
  1685. } else {
  1686. pr_info("mce argument %s ignored. Please use /sys\n", str);
  1687. return 0;
  1688. }
  1689. return 1;
  1690. }
  1691. __setup("mce", mcheck_enable);
  1692. int __init mcheck_init(void)
  1693. {
  1694. mcheck_intel_therm_init();
  1695. return 0;
  1696. }
  1697. /*
  1698. * mce_syscore: PM support
  1699. */
  1700. /*
  1701. * Disable machine checks on suspend and shutdown. We can't really handle
  1702. * them later.
  1703. */
  1704. static int mce_disable_error_reporting(void)
  1705. {
  1706. int i;
  1707. for (i = 0; i < mca_cfg.banks; i++) {
  1708. struct mce_bank *b = &mce_banks[i];
  1709. if (b->init)
  1710. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1711. }
  1712. return 0;
  1713. }
  1714. static int mce_syscore_suspend(void)
  1715. {
  1716. return mce_disable_error_reporting();
  1717. }
  1718. static void mce_syscore_shutdown(void)
  1719. {
  1720. mce_disable_error_reporting();
  1721. }
  1722. /*
  1723. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1724. * Only one CPU is active at this time, the others get re-added later using
  1725. * CPU hotplug:
  1726. */
  1727. static void mce_syscore_resume(void)
  1728. {
  1729. __mcheck_cpu_init_generic();
  1730. __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
  1731. }
  1732. static struct syscore_ops mce_syscore_ops = {
  1733. .suspend = mce_syscore_suspend,
  1734. .shutdown = mce_syscore_shutdown,
  1735. .resume = mce_syscore_resume,
  1736. };
  1737. /*
  1738. * mce_device: Sysfs support
  1739. */
  1740. static void mce_cpu_restart(void *data)
  1741. {
  1742. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1743. return;
  1744. __mcheck_cpu_init_generic();
  1745. __mcheck_cpu_init_timer();
  1746. }
  1747. /* Reinit MCEs after user configuration changes */
  1748. static void mce_restart(void)
  1749. {
  1750. mce_timer_delete_all();
  1751. on_each_cpu(mce_cpu_restart, NULL, 1);
  1752. }
  1753. /* Toggle features for corrected errors */
  1754. static void mce_disable_cmci(void *data)
  1755. {
  1756. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1757. return;
  1758. cmci_clear();
  1759. }
  1760. static void mce_enable_ce(void *all)
  1761. {
  1762. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1763. return;
  1764. cmci_reenable();
  1765. cmci_recheck();
  1766. if (all)
  1767. __mcheck_cpu_init_timer();
  1768. }
  1769. static struct bus_type mce_subsys = {
  1770. .name = "machinecheck",
  1771. .dev_name = "machinecheck",
  1772. };
  1773. DEFINE_PER_CPU(struct device *, mce_device);
  1774. __cpuinitdata
  1775. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1776. static inline struct mce_bank *attr_to_bank(struct device_attribute *attr)
  1777. {
  1778. return container_of(attr, struct mce_bank, attr);
  1779. }
  1780. static ssize_t show_bank(struct device *s, struct device_attribute *attr,
  1781. char *buf)
  1782. {
  1783. return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
  1784. }
  1785. static ssize_t set_bank(struct device *s, struct device_attribute *attr,
  1786. const char *buf, size_t size)
  1787. {
  1788. u64 new;
  1789. if (strict_strtoull(buf, 0, &new) < 0)
  1790. return -EINVAL;
  1791. attr_to_bank(attr)->ctl = new;
  1792. mce_restart();
  1793. return size;
  1794. }
  1795. static ssize_t
  1796. show_trigger(struct device *s, struct device_attribute *attr, char *buf)
  1797. {
  1798. strcpy(buf, mce_helper);
  1799. strcat(buf, "\n");
  1800. return strlen(mce_helper) + 1;
  1801. }
  1802. static ssize_t set_trigger(struct device *s, struct device_attribute *attr,
  1803. const char *buf, size_t siz)
  1804. {
  1805. char *p;
  1806. strncpy(mce_helper, buf, sizeof(mce_helper));
  1807. mce_helper[sizeof(mce_helper)-1] = 0;
  1808. p = strchr(mce_helper, '\n');
  1809. if (p)
  1810. *p = 0;
  1811. return strlen(mce_helper) + !!p;
  1812. }
  1813. static ssize_t set_ignore_ce(struct device *s,
  1814. struct device_attribute *attr,
  1815. const char *buf, size_t size)
  1816. {
  1817. u64 new;
  1818. if (strict_strtoull(buf, 0, &new) < 0)
  1819. return -EINVAL;
  1820. if (mca_cfg.ignore_ce ^ !!new) {
  1821. if (new) {
  1822. /* disable ce features */
  1823. mce_timer_delete_all();
  1824. on_each_cpu(mce_disable_cmci, NULL, 1);
  1825. mca_cfg.ignore_ce = true;
  1826. } else {
  1827. /* enable ce features */
  1828. mca_cfg.ignore_ce = false;
  1829. on_each_cpu(mce_enable_ce, (void *)1, 1);
  1830. }
  1831. }
  1832. return size;
  1833. }
  1834. static ssize_t set_cmci_disabled(struct device *s,
  1835. struct device_attribute *attr,
  1836. const char *buf, size_t size)
  1837. {
  1838. u64 new;
  1839. if (strict_strtoull(buf, 0, &new) < 0)
  1840. return -EINVAL;
  1841. if (mca_cfg.cmci_disabled ^ !!new) {
  1842. if (new) {
  1843. /* disable cmci */
  1844. on_each_cpu(mce_disable_cmci, NULL, 1);
  1845. mca_cfg.cmci_disabled = true;
  1846. } else {
  1847. /* enable cmci */
  1848. mca_cfg.cmci_disabled = false;
  1849. on_each_cpu(mce_enable_ce, NULL, 1);
  1850. }
  1851. }
  1852. return size;
  1853. }
  1854. static ssize_t store_int_with_restart(struct device *s,
  1855. struct device_attribute *attr,
  1856. const char *buf, size_t size)
  1857. {
  1858. ssize_t ret = device_store_int(s, attr, buf, size);
  1859. mce_restart();
  1860. return ret;
  1861. }
  1862. static DEVICE_ATTR(trigger, 0644, show_trigger, set_trigger);
  1863. static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
  1864. static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
  1865. static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
  1866. static struct dev_ext_attribute dev_attr_check_interval = {
  1867. __ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
  1868. &check_interval
  1869. };
  1870. static struct dev_ext_attribute dev_attr_ignore_ce = {
  1871. __ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
  1872. &mca_cfg.ignore_ce
  1873. };
  1874. static struct dev_ext_attribute dev_attr_cmci_disabled = {
  1875. __ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
  1876. &mca_cfg.cmci_disabled
  1877. };
  1878. static struct device_attribute *mce_device_attrs[] = {
  1879. &dev_attr_tolerant.attr,
  1880. &dev_attr_check_interval.attr,
  1881. &dev_attr_trigger,
  1882. &dev_attr_monarch_timeout.attr,
  1883. &dev_attr_dont_log_ce.attr,
  1884. &dev_attr_ignore_ce.attr,
  1885. &dev_attr_cmci_disabled.attr,
  1886. NULL
  1887. };
  1888. static cpumask_var_t mce_device_initialized;
  1889. static void mce_device_release(struct device *dev)
  1890. {
  1891. kfree(dev);
  1892. }
  1893. /* Per cpu device init. All of the cpus still share the same ctrl bank: */
  1894. static __cpuinit int mce_device_create(unsigned int cpu)
  1895. {
  1896. struct device *dev;
  1897. int err;
  1898. int i, j;
  1899. if (!mce_available(&boot_cpu_data))
  1900. return -EIO;
  1901. dev = kzalloc(sizeof *dev, GFP_KERNEL);
  1902. if (!dev)
  1903. return -ENOMEM;
  1904. dev->id = cpu;
  1905. dev->bus = &mce_subsys;
  1906. dev->release = &mce_device_release;
  1907. err = device_register(dev);
  1908. if (err)
  1909. return err;
  1910. for (i = 0; mce_device_attrs[i]; i++) {
  1911. err = device_create_file(dev, mce_device_attrs[i]);
  1912. if (err)
  1913. goto error;
  1914. }
  1915. for (j = 0; j < mca_cfg.banks; j++) {
  1916. err = device_create_file(dev, &mce_banks[j].attr);
  1917. if (err)
  1918. goto error2;
  1919. }
  1920. cpumask_set_cpu(cpu, mce_device_initialized);
  1921. per_cpu(mce_device, cpu) = dev;
  1922. return 0;
  1923. error2:
  1924. while (--j >= 0)
  1925. device_remove_file(dev, &mce_banks[j].attr);
  1926. error:
  1927. while (--i >= 0)
  1928. device_remove_file(dev, mce_device_attrs[i]);
  1929. device_unregister(dev);
  1930. return err;
  1931. }
  1932. static __cpuinit void mce_device_remove(unsigned int cpu)
  1933. {
  1934. struct device *dev = per_cpu(mce_device, cpu);
  1935. int i;
  1936. if (!cpumask_test_cpu(cpu, mce_device_initialized))
  1937. return;
  1938. for (i = 0; mce_device_attrs[i]; i++)
  1939. device_remove_file(dev, mce_device_attrs[i]);
  1940. for (i = 0; i < mca_cfg.banks; i++)
  1941. device_remove_file(dev, &mce_banks[i].attr);
  1942. device_unregister(dev);
  1943. cpumask_clear_cpu(cpu, mce_device_initialized);
  1944. per_cpu(mce_device, cpu) = NULL;
  1945. }
  1946. /* Make sure there are no machine checks on offlined CPUs. */
  1947. static void __cpuinit mce_disable_cpu(void *h)
  1948. {
  1949. unsigned long action = *(unsigned long *)h;
  1950. int i;
  1951. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1952. return;
  1953. if (!(action & CPU_TASKS_FROZEN))
  1954. cmci_clear();
  1955. for (i = 0; i < mca_cfg.banks; i++) {
  1956. struct mce_bank *b = &mce_banks[i];
  1957. if (b->init)
  1958. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1959. }
  1960. }
  1961. static void __cpuinit mce_reenable_cpu(void *h)
  1962. {
  1963. unsigned long action = *(unsigned long *)h;
  1964. int i;
  1965. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1966. return;
  1967. if (!(action & CPU_TASKS_FROZEN))
  1968. cmci_reenable();
  1969. for (i = 0; i < mca_cfg.banks; i++) {
  1970. struct mce_bank *b = &mce_banks[i];
  1971. if (b->init)
  1972. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1973. }
  1974. }
  1975. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1976. static int __cpuinit
  1977. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1978. {
  1979. unsigned int cpu = (unsigned long)hcpu;
  1980. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1981. switch (action & ~CPU_TASKS_FROZEN) {
  1982. case CPU_ONLINE:
  1983. mce_device_create(cpu);
  1984. if (threshold_cpu_callback)
  1985. threshold_cpu_callback(action, cpu);
  1986. break;
  1987. case CPU_DEAD:
  1988. if (threshold_cpu_callback)
  1989. threshold_cpu_callback(action, cpu);
  1990. mce_device_remove(cpu);
  1991. mce_intel_hcpu_update(cpu);
  1992. break;
  1993. case CPU_DOWN_PREPARE:
  1994. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1995. del_timer_sync(t);
  1996. break;
  1997. case CPU_DOWN_FAILED:
  1998. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1999. mce_start_timer(cpu, t);
  2000. break;
  2001. }
  2002. if (action == CPU_POST_DEAD) {
  2003. /* intentionally ignoring frozen here */
  2004. cmci_rediscover(cpu);
  2005. }
  2006. return NOTIFY_OK;
  2007. }
  2008. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  2009. .notifier_call = mce_cpu_callback,
  2010. };
  2011. static __init void mce_init_banks(void)
  2012. {
  2013. int i;
  2014. for (i = 0; i < mca_cfg.banks; i++) {
  2015. struct mce_bank *b = &mce_banks[i];
  2016. struct device_attribute *a = &b->attr;
  2017. sysfs_attr_init(&a->attr);
  2018. a->attr.name = b->attrname;
  2019. snprintf(b->attrname, ATTR_LEN, "bank%d", i);
  2020. a->attr.mode = 0644;
  2021. a->show = show_bank;
  2022. a->store = set_bank;
  2023. }
  2024. }
  2025. static __init int mcheck_init_device(void)
  2026. {
  2027. int err;
  2028. int i = 0;
  2029. if (!mce_available(&boot_cpu_data))
  2030. return -EIO;
  2031. zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL);
  2032. mce_init_banks();
  2033. err = subsys_system_register(&mce_subsys, NULL);
  2034. if (err)
  2035. return err;
  2036. for_each_online_cpu(i) {
  2037. err = mce_device_create(i);
  2038. if (err)
  2039. return err;
  2040. }
  2041. register_syscore_ops(&mce_syscore_ops);
  2042. register_hotcpu_notifier(&mce_cpu_notifier);
  2043. /* register character device /dev/mcelog */
  2044. misc_register(&mce_chrdev_device);
  2045. return err;
  2046. }
  2047. device_initcall_sync(mcheck_init_device);
  2048. /*
  2049. * Old style boot options parsing. Only for compatibility.
  2050. */
  2051. static int __init mcheck_disable(char *str)
  2052. {
  2053. mca_cfg.disabled = true;
  2054. return 1;
  2055. }
  2056. __setup("nomce", mcheck_disable);
  2057. #ifdef CONFIG_DEBUG_FS
  2058. struct dentry *mce_get_debugfs_dir(void)
  2059. {
  2060. static struct dentry *dmce;
  2061. if (!dmce)
  2062. dmce = debugfs_create_dir("mce", NULL);
  2063. return dmce;
  2064. }
  2065. static void mce_reset(void)
  2066. {
  2067. cpu_missing = 0;
  2068. atomic_set(&mce_fake_paniced, 0);
  2069. atomic_set(&mce_executing, 0);
  2070. atomic_set(&mce_callin, 0);
  2071. atomic_set(&global_nwo, 0);
  2072. }
  2073. static int fake_panic_get(void *data, u64 *val)
  2074. {
  2075. *val = fake_panic;
  2076. return 0;
  2077. }
  2078. static int fake_panic_set(void *data, u64 val)
  2079. {
  2080. mce_reset();
  2081. fake_panic = val;
  2082. return 0;
  2083. }
  2084. DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
  2085. fake_panic_set, "%llu\n");
  2086. static int __init mcheck_debugfs_init(void)
  2087. {
  2088. struct dentry *dmce, *ffake_panic;
  2089. dmce = mce_get_debugfs_dir();
  2090. if (!dmce)
  2091. return -ENOMEM;
  2092. ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
  2093. &fake_panic_fops);
  2094. if (!ffake_panic)
  2095. return -ENOMEM;
  2096. return 0;
  2097. }
  2098. late_initcall(mcheck_debugfs_init);
  2099. #endif