mce.c 55 KB

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