mce.c 59 KB

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