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