mce.c 50 KB

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