mce.c 54 KB

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