mce.c 58 KB

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