mce.c 58 KB

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