mce.c 50 KB

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