mce.c 50 KB

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