mce.c 57 KB

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