mce.c 51 KB

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