mce.c 56 KB

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