mce.c 50 KB

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